Repository navigation
feat: add createWithSupabase to choose Database before the composite is built - #207
Closed
johnstonmatt wants to merge 3 commits into
Closed
johnstonmatt wants to merge 3 commits into
johnstonmatt wants to merge 3 commits into
Conversation
…is built - It returns the engine's Composite, so the engine's collision checks apply to its keys. - Its value is defineComposite over withSupabase's parts, with both clients typed for Database. - withChecks keeps withSupabase's two construction-time checks; withSupabase calls the factory. - JSR needs the return type written out; everyKeyDeclared checks it against the parts. - The explicit Database on withSupabase is marked deprecated in its @typeparam docs. Refs FUNC-1182
The typed-database guide, the MCP guide and the API reference use the factory. Refs FUNC-1182
commit: |
The Supabase CLI reads import specifiers out of @example comments in the vendored dist. A relative './database.types.ts' there makes `supabase start` abort in the e2e job. Refs FUNC-1182
Contributor
Author
|
Closing: the factory approach is ruled out. It only helps code rewritten to call |
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
createWithSupabase<Database>()choosesDatabasebefore the composite is built and returns the engine's ownCompositetype, so the engine's checks apply to it: a key it contributes can't be contributed again below it, in apipelineor in an anchored nested stack. The chosenDatabasereachesctx.supabaseandctx.supabaseAdminin every form.defineCompositeover the same parts aswithSupabase.withChecksrunsrejectMiddlewareOptionandrejectPreAuthBehindGateexactly wherewithSupabaseran them, andwithSupabase's implementation callscreateWithSupabase(), so both go through one runtime path. Every existingwithSupabasetest passes unchanged.jsr publish --dry-runrejects an inferred return type on an exported function (missing-explicit-return-type), so the type can't be left to inference. Thereturnstatement checks it against the parts for anyDatabase: a declared key no part contributes, or a client typed for the wrongDatabase, fails to compile.everyKeyDeclaredchecks the other direction, a public key the parts contribute thatSupabaseContextleaves out.EntryIsSoundis removed: the return statement covers what it checked. DeclaringSupabaseContext<Database>also keepsctxidentical towithSupabase's, so moving to the factory changes no handler types.withSupabasekeeps its overloads. The explicitDatabaseis marked deprecated in each overload's@typeParam Databaseand in the docs, pointing tocreateWithSupabase. There is no@deprecatedtag: TypeScript attaches one to a signature, andwithSupabase<Db>(…)andwithSupabase(…)resolve to the same overload, so the tag would strike through every call.withSupabaseClientandwithSupabaseAdminClientstay as they are. They return the engine'sEntryand takeDatabaseper call, with no handler form, so they have none ofwithSupabase's gaps:pipelinevalidates them fully, andcreateWithSupabaseuses them as typed parts. A factory for each would add two exports without closing a type gap.docs/typescript-generics.md,docs/mcp.mdanddocs/api-reference.mdusecreateWithSupabase; the README's exports table lists it. The adapters' ownwithSupabase<Database>(Hono, Elysia) is a different function and is unchanged.Evidence
Server's CI steps (
typecheck,typecheck:examples,lint,check-deps,test,build,smoke,smoke:pack,check-exports,jsr publish --dry-run) all pass locally and in this PR'scijob, and thee2ejob passes. Tests: 587 onmain, 599 here.New tests in
src/with-supabase.test.ts:createWithSupabase type guarantees (tsc-verified):Databasereachesctx.supabaseandctx.supabaseAdmin(toEqualTypeOf<SupabaseClient<Database>>, and.from('missing')is refused);pipelineentry; nested middleware with a prerequisite (withPostgresClient);Baseflowing in fromwithOAuthProtectedResourcealongsideDatabase; a handler annotatedSupabaseContext<Database>; collisions as apipelineentry, with a nestedwithClaims, and with a nested secondwithSupabase.createWithSupabase at runtime: same context keys aswithSupabase, the gate 401,withOAuthProtectedResourcebehind the gate refused in both forms, and themiddlewareoption refused in both forms.tscerrors added by this branch relative tomain, per TypeScript version, against@supabase/middleware1.0.0 and against #56's preview (pkg.pr.new/@supabase/middleware@56, via apathsoverride). Projects: root (src, tests included),examples,src/adapters/nestjs,e2e(against each tree's own build).main's errors on 5.4 to 5.6 are the existing test-file ones (Uint8Arrayis not generic before 5.7). With every@ts-expect-errorin the new type tests removed, each version reports the intended error at each site:middleware-conflict: key 'jwtClaims'(and everywithSupabasekey for the nested secondwithSupabase), and supabase-js refusing the unknown table. TypeScript 7.0.2 prints only the last overload's message for the nestedwithClaimscollision; the same stack with a non-colliding key compiles on 7.0.2, so the failure is the collision.A
pipelineas the factory's handler argument needs #56's typing, so this test is not in the committed suite:Against 1.0.0 it fails on every version (
middleware-prereq: key 'jwtClaims' is not yet on the context, since thepipelineseeds an empty context). Against #56's preview it passes on 5.4.2, 5.5.4, 5.6.2, 5.6.3, 5.7.3, 5.8.3, 5.9.2, 5.9.3, 6.0.3 and 7.0.2.deno checkof a README-style function installed with npm (this branch packed,@supabase/middlewareoverridden to one copy).index.tsusescreateWithSupabase<Database>()plain, with a middleware nested inside it, and as apipelineentry, with@ts-expect-erroron an unknown table in each to proveDatabasearrives.pipeline-arg.tsputs apipelinein its handler position, anchored bysatisfies FetchHandlerand byDeno.serve.index.tspipeline-arg.tsindex.tspipeline-arg.tsMerge Danger
Door: one-way
Blast Radius: low
createWithSupabasebecomes public API at the next release. Changing its name or return type after that is a breaking change.withSupabase's implementation runs throughcreateWithSupabase(). The parts, the checks and their order are the same, and all of its existing runtime tests pass unchanged.@supabase/middleware1.0.0, apipelinewritten as the factory's handler argument sees an empty upstream, as it does underwithSupabasetoday. It sees the factory's keys once a middleware release carries chore(deps-dev): bump hono from 4.12.14 to 4.12.16 in the npm_and_yarn group across 1 directory #56's typing. The docs don't claim it yet.@examplecomments in the packeddist, and abortssupabase starton a relative path.createWithSupabase's TSDoc example therefore namesDatabasein a comment instead of importing it; the set of specifiers indistcomments is the same asmain's.withSupabase's two construction-time checks stay inwithChecksuntil FUNC-1181 replacesrejectPreAuthBehindGateandrejectMiddlewareOptionis dropped or moved into a part. After that, the factory returns the composite directly.