/** * Express Adapter for smrt-tenancy * * Provides Express middleware that sets up tenant context for each request. * * @example * ```typescript * import express from 'express'; * import { createExpressMiddleware } from '@happyvertical/smrt-tenancy/adapters'; * * const app = express(); * * app.use(createExpressMiddleware({ * resolveTenantId: (req) => req.headers['x-tenant-id'] as string, * })); * ``` */ /** * Express Request interface (minimal to avoid direct dependency) */ interface ExpressRequest { headers: Record; url: string; path: string; query: Record; cookies?: Record; } /** * Express Response interface */ interface ExpressResponse { status(code: number): ExpressResponse; json(data: unknown): ExpressResponse; send(data: unknown): ExpressResponse; } /** * Express NextFunction */ type ExpressNext = (error?: unknown) => void; /** * Configuration options for the Express tenancy middleware created by * `createExpressMiddleware()`. * * Only `resolveTenantId` is required. All callback options receive the raw * Express `Request` object so you can extract tenant information from headers, * subdomains, cookies, or any other request property. * * @see createExpressMiddleware */ export interface ExpressMiddlewareOptions { /** * Resolve tenant ID from the request */ resolveTenantId: (req: ExpressRequest) => Promise | string | null | undefined; /** * Resolve user ID from the request (optional) */ resolveUserId?: (req: ExpressRequest) => Promise | string | null | undefined; /** * Resolve permissions (optional) */ resolvePermissions?: (req: ExpressRequest, tenantId: string, userId?: string) => Promise> | Set; /** * Check if user is super admin (optional) */ isSuperAdmin?: (req: ExpressRequest, tenantId: string, userId?: string) => Promise | boolean; /** * Called when no tenant ID could be resolved * Return true to continue, false to stop with 400 error. */ onNoTenant?: (req: ExpressRequest, res: ExpressResponse) => Promise | boolean; /** * Paths to exclude from tenant context */ excludePaths?: string[]; } /** * Create an Express middleware function that establishes tenant context for * every incoming request. * * Uses `enterTenantContext()` (rather than `withTenant()`) because Express * middleware returns before route handlers execute. `enterWith()` sets the * context on the current async resource so it propagates to handlers that run * after `next()` is called. * * The resolved context is also attached directly to the request object for * convenience: * - `req.tenantContext` — full `TenantContextData` * - `req.tenantId` — string tenant ID shortcut * * When no tenant ID can be resolved, the default behaviour returns a `400` * JSON response. Customise this with the `onNoTenant` option. * * @param options - Middleware configuration including the required * `resolveTenantId` callback. * @returns An Express-compatible middleware function `(req, res, next) => void`. * * @example * ```typescript * import express from 'express'; * import { createExpressMiddleware } from '@happyvertical/smrt-tenancy/adapters'; * * const app = express(); * app.use(createExpressMiddleware({ * resolveTenantId: (req) => req.headers['x-tenant-id'] as string, * excludePaths: ['/health', '/public/*'], * })); * ``` * * @see ExpressMiddlewareOptions * @see createSvelteKitHandle */ export declare function createExpressMiddleware(options: ExpressMiddlewareOptions): (req: ExpressRequest, res: ExpressResponse, next: ExpressNext) => Promise; export {}; //# sourceMappingURL=express.d.ts.map