Skip to content

feat: type withSupabase with the engine's Composite - #209

Draft
johnstonmatt wants to merge 2 commits into
mainfrom
FUNC-1187/server/engine-typed-with-supabase
Draft

johnstonmatt wants to merge 2 commits into
mainfrom
FUNC-1187/server/engine-typed-with-supabase

Conversation

@johnstonmatt

Copy link
Copy Markdown
Contributor

Summary

withSupabase is typed with the engine's own Composite, with the signatures that take a Database type kept in front of it. Every call spelling in use compiles as before, and a middleware nested inside withSupabase stays typed under the engine's handler-position typing (supabase/middleware#55) on every TypeScript version from 5.4.

type WithSupabase = DatabaseForms & Composite<SupabaseContext, WithSupabaseConfig, Record<never, never>>
export const withSupabase: WithSupabase = withSupabaseImpl as unknown as WithSupabase
withSupabase(config, …)                    DatabaseForms' config is `never` without a Database → Composite's signatures
withSupabase<Db>(config, …)                DatabaseForms, as before (an explicit argument would otherwise bind to Composite's Base)
withSupabase(config, async (r, ctx: SupabaseContext<Db>) => …)   Database inferred from the annotation → DatabaseForms, as before

The hand-written generic signature it replaces breaks a nested middleware before TypeScript 5.9 once #55's typing ships: its unresolved Base reaches the inner call, which then fails with No overload matches this call. That includes deno check on Deno 2.0 to 2.4, and Edge Functions' runtime is built on Deno 2.1.4. The engine's Composite doesn't have the problem. The runtime is unchanged.

Evidence

  • Type tests in src/with-supabase.test.ts (withSupabase call spellings): nested without an anchor, nested inside another middleware, a collision below it, an explicit Database in the handler, nested, entry and <Db, Upstream> forms, and a handler annotation choosing Database. Each @ts-expect-error on ctx.supabase.from('nope') proves the client carries the chosen Database.
  • Typecheck of the source and tests, the NestJS adapter and the examples, against main on the same middleware build:
Middleware TS 5.4.2 TS 5.5.4 – 5.8.3 TS 5.9.2 – 7.0.2
1.0.0 the 3 tests that nest inside withSupabase, failing on main too, report 2 errors instead of 3 unchanged unchanged
supabase/middleware#56 as 1.0.0 unchanged unchanged
supabase/middleware#55 those 3 tests pass those 3 tests pass; they fail on main unchanged
  • deno check of the packed build, with middleware sharing one copy, on Deno 2.1.4, 2.4.5, 2.5.0 and 2.9.8:
                                         1.0.0    #56      #55
nested middleware inside withSupabase    pass     pass     pass   (@supabase/server 1.9.2 with #55: fails on 2.1.4 and 2.4.5)
README handlers, composed stacks         pass     pass     pass
every call spelling                      pass*    pass*    pass
* except a pipeline as the handler argument: anchored needs #56, unanchored needs #55
  • typecheck, typecheck:examples, typecheck:e2e, lint, check-deps, the unit and NestJS tests, build, both smoke tests, attw, the JSR dry run and the docs build pass. The docs build has main's warnings and no new ones.

Merge Danger

Door: two-way

Blast Radius: types

  • withSupabase gets the engine's collision check: a middleware below it that contributes one of its keys fails to compile in a stack anchored with satisfies FetchHandler, as a pipeline already refuses the same stack.
  • withSupabase is now a const of the exported type WithSupabase rather than an overloaded function. Code that reads its type with typeof withSupabase sees the new type.
  • On TypeScript 5.4 the error text changes in the three nesting tests that already fail there.
  • This must be released before feat: type a pipeline in an unanchored stack against the enclosing context middleware#55. A project that pins an older @supabase/server and picks up a middleware release with feat: add NestJS adapter #55 still breaks on Deno 2.0 to 2.4.

Refs FUNC-1187

Calls without a type argument resolve against the engine's Composite signatures.

Calls that choose Database, explicitly or by annotation, resolve as before.

A middleware nested inside it type-checks with supabase/middleware#55 on TypeScript 5.4 to 7.0.

The runtime is unchanged. WithSupabase is exported, and type tests cover each call spelling.

Refs FUNC-1187
@pkg-pr-new

pkg-pr-new Bot commented Nov 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@supabase/server@209

commit: d6429df

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