Repository navigation
fix(express): ship the Express.Request type augmentation - #213
Merged
Merged
Conversation
The augmentation that types req.user lived in src/types/express.d.ts, an ambient file tsc never copies into dist, so the published package left req.user untyped and consumers had to declare it themselves. Move it into a module (src/types/express.ts) imported from the entry, and add req.cookiePayload, which createEnsureCookiesMiddleware sets. The FastifyRequest augmentation moves the same way so it also loads under NodeNext, where the entry's extensionless relative imports do not resolve. Add consumer type tests that typecheck a fixture against the built dist under bundler and NodeNext resolution.
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.
Bug
@seamless-auth/express@0.20.0does not ship itsExpress.Requestaugmentation. In a TypeScript consumer,req.userset byrequireAuthdoes not typecheck unless the app declares the augmentation itself.Root cause: the augmentation lived in
packages/express/src/types/express.d.ts, an ambient declaration file.tscdoes not copy.d.tssources into its output, and nothing in the entry referenced it, so it never reacheddist. In-repo it appeared to work only because it was in thesrcprogram (andskipLibCheckhid that itsSeamlessAuthUserreference did not resolve). ThetypeRootsentry intsconfig.jsonpointed at a path that does not exist.@seamless-auth/fastify@0.11.1ships itsFastifyRequestaugmentation (indist/options.d.ts) and it works undermoduleResolution: "bundler", but it is lost under"NodeNext": the entry reaches it only through the extensionlessexport type ... from "./options", which NodeNext does not resolve.Reproduction
Published tarballs (
npm pack @seamless-auth/express@0.20.0 @seamless-auth/fastify@0.11.1) installed into a scratch consumer with stricttsc --noEmit:TS2339: Property 'user' does not exist on type 'Request<...>'; fastify passes.request.userandrequest.cookiePayload.Fix
src/types/express.ts, that usesdeclare global { namespace Express { interface Request { ... } } }plusexport {}, and import it fromsrc/index.ts. tsc now emitsdist/types/express.d.tsanddist/index.d.tsstarts withimport "./types/express.js", so importing the package applies it. The augmentation also gainscookiePayload, whichcreateEnsureCookiesMiddlewaresets, matching what Fastify already declares.useris typed as core'sSeamlessAuthUser, which is whatrequireAuthassigns.declare module "fastify"block out ofoptions.tsintosrc/types/fastify.ts, imported from the entry the same way..jsextension so they resolve under NodeNext as well as bundler. The runtime bundle is unchanged (the module is type-only and tsup drops it).typeRootssetting from the expresstsconfig.json.Regression tests
packages/{express,fastify}/tests/consumerTypes.test.jsruntsc --noEmit --strictagainsttests/types/consumer.ts, which imports the package by name. It resolves through the package's ownexportsto the builtdist, as an installed consumer would. Each runs under bundler and NodeNext resolution and asserts no diagnostics. Before the fix, express fails both cases and fastify fails NodeNext.Verification
pnpm build: passes.pnpm test: core 408, express 213, fastify 169, nextjs 166, all passing.tsc --noEmit -p tsconfig.jsonin each package: passes.pnpm check:types-current: passes.pnpm packof the fixed express and fastify, installed into the same scratch consumer.tsc --noEmitpasses under both bundler and NodeNext.Out of scope, noted
Under NodeNext, every extensionless relative import in the emitted
.d.tsfiles of all three adapters fails to resolve, so the exported function types collapse toany. That is hidden byskipLibCheckand is a separate fix. This PR only makes sure the request augmentation still loads there.No API contract change for
seamless-auth-apior the SDKs.