fix: export getAuthKitContext as the public auth context accessor#98
Conversation
The lazy dynamic-import shell introduced in #85 (v0.8.3) severed TypeScript's inference of authkitMiddleware's downstream context type: the middleware body moved behind `await import()`, so the inline `args.next({ context })` call that carried the type no longer exists in the `.server()` callback. Consumers who read the middleware context in their own server functions or middleware lost all typing, with no public replacement. Rather than hand-maintaining a type cast on the shell (unchecked against the body's real context shape, and fragile against TanStack Start beta type changes), expose the accessor the SDK already uses internally: - Add public `AuthKitContext` type (`auth`, `request`, `redirectUri`); internal `AuthKitServerContext` extends it with `__setPendingHeader` - Export `getAuthKitContext()` / `getAuthKitContextOrNull()` from the package barrel, returning the narrowed public type - Internal `__setPendingHeader` consumers (storage, server-fn bodies) switch to a new internal-only `getInternalAuthKitContextOrNull()`
Greptile SummaryThis PR fixes a regression from #85 where the lazy dynamic-import shell for
Confidence Score: 5/5Safe to merge. The change is a well-scoped public API surface addition with no logic mutations in the runtime paths. The diff is clean and consistent: the new public/internal type split is correctly enforced, all three internal callers (storage.ts, server-fn-bodies.ts, and the now-delegating getAuthKitContextOrNull) have been updated, the barrel export does not accidentally expose the internal accessor, and the new tests cover both runtime identity and type-level guarantees. No logic is changed in auth, cookie, or session handling. No files require special attention. Important Files Changed
Reviews (1): Last reviewed commit: "fix: export getAuthKitContext as the pub..." | Re-trigger Greptile |
Summary
A customer upgrading 0.8.2 → 0.8.5 reported that
authkitMiddlewareno longer provides a downstream context type, and that thegetAuthKitContext()helper that replaces it isn't publicly exported.They're right, and it was unintentional: the lazy dynamic-import shell from #85 (shipped in v0.8.3) severed TypeScript's inference of the middleware context. The inline
args.next({ context })call that carried the type moved intomiddleware-body.tsbehindawait import(), so the.server()callback's return type — and with it the downstream context type — collapsed to untyped.Approach
Export the accessor the SDK already uses internally, rather than restoring the inferred type with a cast on the shell. A cast would be unchecked against the body's real context shape (
middlewareBodyreturnsany) and fragile against TanStack Start beta type churn — andcontext.tsalready didn't trust the inferred typing (it castsgetGlobalStartContext()itself).AuthKitContexttype:auth(),request,redirectUri. The internalAuthKitServerContextextends it with__setPendingHeader, keeping the header-plumbing channel out of the public API.getAuthKitContext()andgetAuthKitContextOrNull()are exported from the package barrel with the narrowed return type. Deep imports aren't in theexportsmap, so the barrel is the public boundary.__setPendingHeaderconsumers (storage.ts,server-fn-bodies.ts) move to a new@internalgetInternalAuthKitContextOrNull().middleware.tsis untouched — no change to the bundle-leak guard surface.Testing
pnpm typecheck,pnpm lint(oxlint regression guard intact)pnpm test— 222 tests pass; added type-level tests proving__setPendingHeaderis absent from the public type, plus export assertions intests/exports.spec.tspnpm buildandcd example && pnpm build— no Node-only module warningspnpm run build:check— no server fingerprints in the example client bundle