Skip to content

feat: check a config callback's ctx annotation as a prerequisite - #60

Draft
johnstonmatt wants to merge 2 commits into
FUNC-1185/middleware/pipeline-anchored-stackfrom
FUNC-1178/middleware/config-callback-prereqs
Draft

johnstonmatt wants to merge 2 commits into
FUNC-1185/middleware/pipeline-anchored-stackfrom
FUNC-1178/middleware/config-callback-prereqs

Conversation

@johnstonmatt

@johnstonmatt johnstonmatt commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #56.

What a config callback declares about ctx is now a prerequisite of that call, checked in both composition forms. withFeatureFlag's evaluate is the example; any defineMiddleware or defineComposite config with a callback (req: Request, ctx: BaseContext) => … gets the same treatment, with no hand-written signature.

// pipeline: the annotation joins the entry's In
pipeline(
  [withRegion(), withFeatureFlag({ name: 'beta', evaluate: (_req, ctx: { user: User }) => testers.has(ctx.user.id) })],
  handler,
) // middleware-prereq: key 'user' is not yet on the context (check ordering)

// nested: checked in place when anchored, carried outward like a declared In otherwise
withUser(withFeatureFlag({ name: 'beta', evaluate: (_req, ctx: { user: User }) => testers.has(ctx.user.id) }, handler)) satisfies FetchHandler // ok
withRegion(withFeatureFlag({ name: 'beta', evaluate: (_req, ctx: { user: User }) => testers.has(ctx.user.id) }, handler)) satisfies FetchHandler // error

The three call forms of Middleware (and Composite), sketched:

// cascade: still one type parameter, Base, with no inference site; the callback's ctx is NoInfer<Base>
<Base extends In & BaseContext>(config: ConfigReading<Config, In, NoInfer<Base>>, handler: NoConflict<…>): Produced<Base, In>
// propagation: Need is inferred from the annotation and travels outward; a Base that disagrees is a Mismatch
<Base, Need = BaseContext, Ctx = BaseContext & Need & { [K in Key]: Contribution }>(
  config: [Mistyped<Need, Base>] extends [never] ? ConfigReading<Config, In, Need> : Mismatch<…>,
  handler: NoConflict<…>,
): Produced<Base & Omit<Ctx, Key | keyof Need> & Need, In & Omit<Ctx, Key | keyof Need> & Pick<Need, RequiredKeys<Need>>>
// config-only: config is the only argument, so Need is inferred there and joins the entry's In
<Need = BaseContext>(config: ConfigReading<Config, In, Need>): Entry<{ [K in Key]: Contribution }, In & Need>

ConfigReading returns Config unchanged when it has no config callback, so most middleware keep their signatures and error messages. pipeline reports 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 config an 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 single Base with NoInfer and no default, and only the propagation and config-only forms infer from config.

Docs: withFeatureFlag's TSDoc and README annotate evaluate's ctx instead of casting it. The authoring guide's config-callback variant builds withRequestLog with defineMiddleware and 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 optional In key in pipeline.

Before, on the base branch, an annotated evaluate did not compile in either form, which is why the docs cast:

Type '(_req: Request, ctx: { user: User; }) => boolean' is not assignable to type '(req: Request, ctx: object) => …'.
  Property 'user' is missing in type '{}' but required in type '{ user: User; }'.

After, with a provider it compiles; without one, pipeline reports middleware-prereq: key 'user' is not yet on the context (check ordering), a provider of the wrong type reports middleware-prereq: key 'user' is on the upstream context with an incompatible type, and a nested stack with no provider fails where it is checked against FetchHandler.

Typecheck matrix. "Repo" is tsc over src, which includes the test a middleware with a hand-written signature. "Probes" is a set of 95 shapes: evaluate nested and in pipeline, 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 like withSupabase), and unannotated callbacks.

TypeScript Repo, base Repo, this branch Probes passing, base Probes passing, this branch Probes that regress
5.4.2 pass pass 49 78 none
5.5.4, 5.6.2, 5.6.3, 5.7.3, 5.8.3 pass pass 52 82 none
5.9.2, 5.9.3, 6.0.3, 7.0.2 pass pass 53 85 none

Dependents against 1.0.0, through a tsconfig paths override, 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 inside withSupabase (around lines 346, 561 and 728 of with-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 check with this branch's build and @supabase/server 1.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 in withSupabase, and evaluate nested, in pipeline, optional and under Deno.serve all pass. A negative file fails at both of its exports.

Merge Danger

Door: two-way

Blast Radius: typings

  • No runtime change. The published types of Middleware and Composite change: the propagation form gains a Need type parameter ahead of Ctx, Ctx's default includes the contributed key, and the config-only form gains Need. 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.
  • Known gaps, none of them regressions: (1) an optional key of the wrong type, nested in a stack without an anchor, is not caught on any TypeScript version, whether the inner layer is written inline or built apart; pipeline and 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 to FetchHandler, 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 as withSupabase'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.
  • pipeline no longer reports an optional key of a declared In as unmet. Nesting is unchanged, so such a middleware still loses its optional ctx; the guide's optional-key variant says so.
  • The guide's hand-written-signature variant keeps withRequestLog as its worked example, now a case defineMiddleware covers on its own. Its intro says so.
  • Conflicts: fix: type a stack written inline in a host-typed export without the host's values #58 (the same Middleware and Composite overloads) 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's context callback could be checked this way if it were built on defineMiddleware, but its hand-written signature is still needed to type ctx.flags from flags.

Refs FUNC-1178

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
@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@supabase/middleware@60

commit: 61b74af

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant