feat: add middleware option to withSupabase#88
Draft
mandarini wants to merge 3 commits into
Draft
Conversation
commit: |
0e747ac to
ad449a9
Compare
1 task
…lution TypeScript doesn't apply excess property checking during overload resolution, so calls with plugins: [...] were silently matching overload 1 and typing ctx as SupabaseContext<unknown>. Adding plugins?: never to overload 1's config makes it definitively fail when plugins is present, falling through to the correct overload. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The array holds middleware entries (per-request behavior from
defineMiddleware) — the word 'plugins' is reserved for the package-level
concept whose client namespace goes in createClient({ plugins }). One
word per concept: server-side composition is 'middleware', client-side
namespaces are 'plugins', a Plugin is the package that ships both.
PluginsCtx -> MiddlewareCtx; overload trick unchanged
(middleware?: never on overload 1).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
47791e0 to
a1d9563
Compare
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
Adds a
middlewareoption towithSupabaseso that@supabase/web-middlewareentries can compose around the handler after the Supabase context is established.The middleware receive
ctx.supabase,ctx.userClaims, etc. already populated — they run after the Supabase context is created, not before. This is the key difference from wrappingwithSupabaseitself.Architecture
What changed
src/with-supabase.ts— two named overloads (no-middleware / with-middleware) so TypeScript can infer the handler'sctxtype concretely.MiddlewareCtx<Entries>accumulates the entries' key contributions via a recursive conditional type. At runtime, entries are folded right-to-left withreduceRight;_runtimeis seeded on the ctx soweb-middlewareentries recognise it as an upstream context (viaisContext())src/with-supabase.test.ts— 5 new tests: composition after Supabase context, middleware receivesctx.supabase, short-circuit before handler, array order, CORS still appliedpackage.json— adds@supabase/web-middlewareas a dependency (PR build:pkg.pr.new/supabase/web-middleware/@supabase/web-middleware@9)pnpm-workspace.yaml— allows@supabase/web-middlewarebuild scriptsTry it
Notes
withSupabase(config, handler)API is unchanged — overload 1 is identical to the current signatureMiddlewareCtx<Entries>mirrorspipeline's internalAccumulatetype without depending on the internal exportMiddlewareCtxto includeSupabaseContext, so middleware that declareInprerequisites onsupabaseoruserClaimsget compile-time checking. Left as a follow-up (tracked in Linear SDK-1164)Merge order
@supabase/web-middleware@9in package.json). Before this merges: fix: key name resolution for client creation #9 must be merged and published to npm, and the dependency repointed to the published version.pluginsarray oncreateClient— the package-level namespaces; this PR's array is the package's middleware); no code dependency in either direction.