UNPKG

next-armored

Version:
61 lines (56 loc) 2.05 kB
import { NextMiddlewareResult } from 'next/dist/server/web/types'; import { NextRequest, NextResponse } from 'next/server'; type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'; type Origin = string | RegExp; type CorsConfig = { origins: Origin[]; methods?: Method[]; headers?: string[]; allowCredentials?: boolean; exposedHeaders?: string[]; maxAge?: number; preflightContinue?: boolean; optionsSuccessStatus?: number; }; type NextCorsMiddleware = (request: NextRequest, response?: NextResponse) => NextMiddlewareResult; type PathMatcher = { startWith: string; additionalIncludes?: string[]; }; type PathOptions = { includes?: PathMatcher[]; excludes?: PathMatcher[]; }; /** * CORS middleware builder * @param config - The CORS configuration (origins, methods, headers, etc.) * @param pathOptions - Path options selector (includes, excludes) to enable or disable CORS for specific paths * @returns The corsMiddleware function * @usage * ```ts * export const corsMiddleware = createCorsMiddleware(config, pathOptions); * ``` */ declare const createCorsMiddleware: (config: CorsConfig, pathOptions?: PathOptions) => NextCorsMiddleware; type CorsConfigDefaults = { origins: undefined; methods: Method[]; headers: string[]; allowCredentials: boolean; exposedHeaders: string[]; preflightContinue: boolean; maxAge: number; optionsSuccessStatus: number; }; /** * @default origins is undefined, you must specify it manually to prevent unwanted * which can be a security risk * @default methods is ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'] * @default headers is ['Content-Type', 'Authorization'] * @default allowCredentials is true * @default preflightContinue is false * @default optionsSuccessStatus is 204 * @default exposedHeaders is [] * @default maxAge is 1 day */ declare const DEFAULT_CORS_CONFIG: CorsConfigDefaults; export { type CorsConfig, DEFAULT_CORS_CONFIG, createCorsMiddleware, createCorsMiddleware as default };