Repository navigation
feat: check a config callback's ctx annotation as a prerequisite - #60
Draft
johnstonmatt wants to merge 2 commits into
Draft
johnstonmatt wants to merge 2 commits into
johnstonmatt wants to merge 2 commits into
Conversation
What a config callback declares about `ctx` joins that call's prerequisites. In a `pipeline`, the config-only form infers it from the annotation and adds it to the entry's `In`, so a missing provider fails with `middleware-prereq`. Nested, the cascade types the callback's `ctx` as `NoInfer<Base>`, so an anchored stack checks it in place, and the propagation form carries what the cascade cannot confirm outward like any other requirement. The cascade keeps its single `Base` type parameter with no inference site, so nested handlers stay on the return-type path. Optional keys stay optional: `pipeline` reports a key as unmet only when it is required, and still checks the type of one that is present. Refs FUNC-1178
`withFeatureFlag`'s docs annotate `evaluate`'s `ctx` and describe the check. The authoring guide's config-callback variant builds `withRequestLog` with `defineMiddleware`, and describes the annotation as a checked prerequisite. Refs FUNC-1178
commit: |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #56.
What a config callback declares about
ctxis now a prerequisite of that call, checked in both composition forms.withFeatureFlag'sevaluateis the example; anydefineMiddlewareordefineCompositeconfig with a callback(req: Request, ctx: BaseContext) => …gets the same treatment, with no hand-written signature.The three call forms of
Middleware(andComposite), sketched:ConfigReadingreturnsConfigunchanged when it has no config callback, so most middleware keep their signatures and error messages.pipelinereports a key as unmet only when it is required, so an optional annotation never needs a provider; its type is still checked where one is present.Making
configan inference site in every handler overload puts nested handlers on the ordinary inferences: 34 errors in this repo's own tests and failing nesting inside hand-written signatures, on every TypeScript version from 5.4 to 7.0. Here the cascade keeps its singleBasewithNoInferand no default, and only the propagation and config-only forms infer fromconfig.Docs:
withFeatureFlag's TSDoc and README annotateevaluate'sctxinstead of casting it. The authoring guide's config-callback variant buildswithRequestLogwithdefineMiddlewareand describes the check, in place of the "assertion, not a contract" caveat.Evidence
Repo checks on this branch:
typecheck,lint,format:check,test(162 passed),build,smoke,typecheck:min(TS 5.4.2),typecheck:consumer,check-exports,jsr publish --dry-run,test:deno. New tsc-verified tests:a config callback’s ctx annotation(define-middleware),withFeatureFlag — evaluate reads the upstream, a composite config callback, and an optionalInkey inpipeline.Before, on the base branch, an annotated
evaluatedid not compile in either form, which is why the docs cast:After, with a provider it compiles; without one,
pipelinereportsmiddleware-prereq: key 'user' is not yet on the context (check ordering), a provider of the wrong type reportsmiddleware-prereq: key 'user' is on the upstream context with an incompatible type, and a nested stack with no provider fails where it is checked againstFetchHandler.Typecheck matrix. "Repo" is
tscoversrc, which includes the testa middleware with a hand-written signature. "Probes" is a set of 95 shapes:evaluatenested and inpipeline, anchored, unanchored and built apart, optional and mistyped keys, composites, nesting inside a hand-written signature (one overload set like the guide's, one shaped likewithSupabase), and unannotated callbacks.Dependents against 1.0.0, through a tsconfig
pathsoverride, on all ten TypeScript versions:@supabase/server(src, examples, e2e),@supabase-labs/middleware-openfeature(src and negative type tests), and an internal dependent (src and negative type tests). No errors added or removed. The diagnostic text matches 1.0.0's everywhere except on TS 5.4.2: openfeature's negative test at line 70 differs the same way on the base branch, and the server's three tests that nest a middleware insidewithSupabase(around lines 346, 561 and 728 ofwith-supabase.test.ts) already fail against 1.0.0 there, with different text. Those three tests pass on 5.5.4 through 7.0.2 with this branch, as with 1.0.0.deno checkwith this branch's build and@supabase/server1.9.2, on Deno 2.1.4, 2.4.5, 2.5.0 and 2.9.8: the server README's files, a middleware nested inwithSupabase, andevaluatenested, inpipeline, optional and underDeno.serveall pass. A negative file fails at both of its exports.Merge Danger
Door: two-way
Blast Radius: typings
MiddlewareandCompositechange: the propagation form gains aNeedtype parameter ahead ofCtx,Ctx's default includes the contributed key, and the config-only form gainsNeed. Code that compiled against 1.0.0 compiles the same in every dependent checked. Once released, reverting breaks code that relies on a callback annotation.pipelineand anchored stacks catch it. With fix: check a handler with an optional ctx against a middleware with no prerequisites #61, inline and built-apart nesting catch it too, and only nesting at distance without an anchor still compiles. (2) an unanchored nested stack whose arrow handler reads the enclosing keys fails on TS 5.4 to 5.8 when later assigned toFetchHandler, as it does on the base branch without a config callback; (3) an annotation that needs a key from outside a hand-written signature with no propagation form, such aswithSupabase's, fails to compile when the stack isn't anchored, and compiles when it is; (4) an unannotated callback sees the enclosing keys only in an anchored stack, the same as the guide's hand-written signature.pipelineno longer reports an optional key of a declaredInas unmet. Nesting is unchanged, so such a middleware still loses its optionalctx; the guide's optional-key variant says so.withRequestLogas its worked example, now a casedefineMiddlewarecovers on its own. Its intro says so.MiddlewareandCompositeoverloads) and feat: let a middleware declare that it must run before another #59 (those overloads,pipeline.ts,types.ts) touch the same lines. Whichever lands second rebases.withOpenFeature'scontextcallback could be checked this way if it were built ondefineMiddleware, but its hand-written signature is still needed to typectx.flagsfromflags.Refs FUNC-1178