/** * 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 } from '../platform/interfaces'; /** * Express Adapter Implementation * * Provides NAuth integration for Express.js applications. */ export declare class ExpressAdapter implements NAuthAdapter { readonly name = "ExpressAdapter"; /** * 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