Context
The NestJS app existed as a bootstrap with a health route. Before any feature module lands, the decisions that every later module inherits had to be fixed: how a request is traced, what an error looks like, where validation comes from, and how a route becomes private.
A working reference was available in a sibling project (VDCD_Backend, NestJS 11 + TypeORM). Its layout is good and was adopted: common/{decorators,filters,guards,interceptors}, config/, modules/*, plus helmet, cookie-parser, environment-aware CORS and a versioned global prefix.
Its implementations were not adopted, for four reasons: no request id anywhere, any in the filter and the response interceptor (this repo runs strict + noUncheckedIndexedAccess), a bespoke error shape, and a TransformInterceptor that wraps every response in {statusCode, data} — which fights OpenAPI typing and wraps errors too.
Decision
- Request id via
AsyncLocalStorage.RequestContextMiddlewareruns first, honours an inboundx-request-id(shape-checked) or mints one, echoes it on the response, and opens the context. The filter, the logging interceptor, the JWT strategy and any queued job read from there. One id follows a request through API → queue → worker. - RFC 7807 Problem Details as the only error shape, served as
application/problem+json, carryingrequestId. Defined once inpackages/shared, so the web client parses the same type the API produces. 4xx log at debug, 5xx log with the stack, and stacks never cross the wire in production. No response envelope.Superseded by ADR-0003: every response is enveloped.- Zod as the only validator, via
nestjs-zod: DTOs arecreateZodDto(sharedSchema), so one definition validates the request, types the handler and generates the OpenAPI schema. This requires zod v4 —nestjs-zod@5cannot infer through zod 3'sparsesignature, which silently degrades every DTO tounknown. The workspace was moved to zod 4 for this. - Private by default.
JwtAuthGuardis global; a route opts out with@Public(). Forgetting a guard can only fail closed.RolesGuardis global too and typed against the sharedRoleunion. Ownership checks are a separate per-module guard — role alone never answers "is this the instructor who owns this course?". - Env validated at boot with Zod. Missing or weak values stop the process; messages name the variable and the reason, never the value.
- Prisma pinned to the v6 line.
prisma@latestcurrently resolves to 8.0.0-rc, whose CLI is a different product —prisma generatedoes not exist in it. Every tutorial the team will find describes v6. Pinned deliberately, not accidentally. - Refresh tokens are opaque, hashed and rotated. Stored as SHA-256 so a database dump is not a set of live sessions; presenting a revoked token revokes the whole chain for that user.
Alternatives considered
- class-validator DTOs (NestJS default, already installed). Rejected: it duplicates every shape that
packages/sharedalready defines, and the two drift. nestjs-pinofor logging. Deferred. TheLogger+ interceptor pair covers the need today and adds no transport to configure; pino goes in when logs need shipping somewhere.- Keeping
tsxas the worker runtime. Rejected: it forces TypeScript into the production image.packages/sharednow builds to dual ESM/CJS via tsup, so the worker runsnode dist/main.js.
Consequences
- Easy: every new module inherits tracing, error shape, auth and validation for free.
- Constrained:
@typescript-eslint/consistent-type-importsis off insideapps/api. NestJS resolves constructor dependencies fromemitDecoratorMetadata; rewriting those imports toimport typeerases the class at runtime and the container fails to inject it — code that compiles and then crashes on boot. The rule stays on everywhere else. packages/sharednow has a build step. Turbo'sdependsOn: ["^build"]orders it; consumers resolve through itsexportsmap rather than raw source.- Owed:
.jsspecifiers belong in the worker's own relative imports (it is ESM on NodeNext) and must not appear insidepackages/shared— Turbopack cannot map those back to.ts, which breaks the web build.