Skip to content

Request context

ContextService is an AsyncLocalStorage-backed singleton — per-request isolation without request-scoped DI. ContextMiddleware populates it from headers.

AccessorSource
userIdx-user-id header / JWT
tenantx-tenant header
langaccept-language / x-language (raw string; consumer parses to a locale)
token, user, permissionsfilled by AuthGuard after JWT validation
hasPermission(code)checks the synced permissions set
getCustom<T>(key)reads a consumer value from ctx.custom

The library keeps only framework-generic keys. Domain values go in ctx.custom, or add typed fields via declaration merging:

ts
declare module '@sdcorejs/nestjs/core' {
  interface RequestContext {
    departmentCode?: string;
    isSystemAdmin?: boolean;
  }
}

Configure which headers populate the context via SdCoreModule.forRoot({ context: { headers: { tenant: 'x-tenant', userId: 'x-user-id' } } }).

Released under the MIT License.