/** * Express Framework Adapter * * Adapts NAuth to work with Express.js (4.x and 5.x compatible). * * **Context Management:** * - First middleware (clientInfo) initializes AsyncLocalStorage context * - Context automatically propagates through Express middleware chain * - Route handlers have automatic context access (no wrapper needed) * * **Express 5.x Compatibility:** * - Uses req.hostname instead of deprecated req.host * - Handles async middleware errors automatically * - Compatible with new path matching behavior */ import { NAuthAdapter, NAuthRequestAttributes, NAuthMiddlewareHandler, NAuthResponseInterceptorHandler, NAuthRouteHandler, MiddlewareOptions, RawHttpHandler, RawMountPredicate } from '../platform/interfaces'; /** * Express Adapter Implementation * * Provides NAuth integration for Express.js applications. */ export declare class ExpressAdapter implements NAuthAdapter { readonly name = "ExpressAdapter"; /** * Claim raw HTTP for matching paths. * * Registered as ordinary Express middleware, so it runs in registration order. Two * consequences worth knowing, both load-bearing for the OpenID Connect provider: * * - Express does no path rewriting here, unlike `app.use('/prefix', ...)`. A provider * whose routes carry their prefix already sees the full path, and discovery stays at * the origin root even when the host framework applies a global prefix. * - Register before any body parser to leave the request stream intact. Mounted after * one, the body arrives pre-parsed and the upstream parser's size limit applies * instead of the protocol handler's own. * * @param app - The Express application * @param predicate - Receives the query-stripped path; return true to claim the request * @param handler - Owns the raw Node objects and must end the response * @throws {Error} When `app` has no `use()` method */ mountRaw(app: unknown, predicate: RawMountPredicate, handler: RawHttpHandler): void; /** * Register a middleware handler * * Wraps the generic NAuth handler into Express middleware format. * Handles context initialization for the first middleware. */ registerMiddleware(name: string, handler: NAuthMiddlewareHandler, options?: MiddlewareOptions): ExpressMiddleware; /** * Execute handler with proper async flow control */ private executeHandler; /** * Register a response interceptor * * Uses res.json monkey-patching to intercept JSON responses. * This is the standard pattern for Express response interception. */ registerResponseInterceptor(handler: NAuthResponseInterceptorHandler): ExpressMiddleware; /** * Wrap a route handler * * For Express, this provides: * - NAuthRequest/NAuthResponse wrappers * - Automatic error handling * - Context is automatically available (no restoration needed) */ wrapRouteHandler(handler: NAuthRouteHandler): ExpressMiddleware; } /** * Express Request type (minimal interface for our needs) * Compatible with Express 4.x and 5.x */ interface ExpressRequest { method: string; path: string; url: string; originalUrl: string; body: unknown; query: unknown; params: unknown; headers: Record; cookies?: Record; ip: string; socket?: { remoteAddress?: string; }; get?(name: string): string | undefined; /** NAuth attribute storage - isolated from raw request */ _nauthAttributes?: NAuthRequestAttributes; } /** * Express Response type (minimal interface for our needs) * Compatible with Express 4.x and 5.x */ interface ExpressResponse { status(code: number): this; setHeader(name: string, value: string | string[]): void; cookie(name: string, value: string, options?: Record): void; clearCookie(name: string, options?: Record): void; send(body: unknown): void; json(body: unknown): this; redirect(status: number, url: string): void; redirect(url: string): void; headersSent: boolean; } /** * Express NextFunction type */ type ExpressNextFunction = (err?: unknown) => void; /** * Express middleware signature */ type ExpressMiddleware = (req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) => void | Promise; /** * Express middleware type for use with app.use() * * Consumer apps should cast nauth.middleware.* to this type: * ```typescript * app.use(nauth.middleware.clientInfo as ExpressMiddlewareType); * ``` */ export type ExpressMiddlewareType = (req: unknown, res: unknown, next: (err?: unknown) => void) => void | Promise; export {}; //# sourceMappingURL=express.adapter.d.ts.map