|
| 1 | +import type { |
| 2 | + CorsOptions, |
| 3 | + Middleware, |
| 4 | +} from '../../types/rest.js'; |
| 5 | +import { |
| 6 | + DEFAULT_CORS_OPTIONS, |
| 7 | + HttpErrorCodes, |
| 8 | + HttpVerbs, |
| 9 | +} from '../constants.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Resolves the origin value based on the configuration |
| 13 | + */ |
| 14 | +const resolveOrigin = ( |
| 15 | + originConfig: NonNullable<CorsOptions['origin']>, |
| 16 | + requestOrigin: string | null, |
| 17 | +): string => { |
| 18 | + if (Array.isArray(originConfig)) { |
| 19 | + return requestOrigin && originConfig.includes(requestOrigin) ? requestOrigin : ''; |
| 20 | + } |
| 21 | + return originConfig; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates a CORS middleware that adds appropriate CORS headers to responses |
| 26 | + * and handles OPTIONS preflight requests. |
| 27 | + * |
| 28 | + * @example |
| 29 | + * ```typescript |
| 30 | + * import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest'; |
| 31 | + * import { cors } from '@aws-lambda-powertools/event-handler/experimental-rest/middleware'; |
| 32 | + * |
| 33 | + * const app = new Router(); |
| 34 | + * |
| 35 | + * // Use default configuration |
| 36 | + * app.use(cors()); |
| 37 | + * |
| 38 | + * // Custom configuration |
| 39 | + * app.use(cors({ |
| 40 | + * origin: 'https://example.com', |
| 41 | + * allowMethods: ['GET', 'POST'], |
| 42 | + * credentials: true, |
| 43 | + * })); |
| 44 | + * |
| 45 | + * // Dynamic origin with function |
| 46 | + * app.use(cors({ |
| 47 | + * origin: (origin, reqCtx) => { |
| 48 | + * const allowedOrigins = ['https://app.com', 'https://admin.app.com']; |
| 49 | + * return origin && allowedOrigins.includes(origin); |
| 50 | + * } |
| 51 | + * })); |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * @param options.origin - The origin to allow requests from |
| 55 | + * @param options.allowMethods - The HTTP methods to allow |
| 56 | + * @param options.allowHeaders - The headers to allow |
| 57 | + * @param options.exposeHeaders - The headers to expose |
| 58 | + * @param options.credentials - Whether to allow credentials |
| 59 | + * @param options.maxAge - The maximum age for the preflight response |
| 60 | + */ |
| 61 | +export const cors = (options?: CorsOptions): Middleware => { |
| 62 | + const config = { |
| 63 | + ...DEFAULT_CORS_OPTIONS, |
| 64 | + ...options |
| 65 | + }; |
| 66 | + |
| 67 | + return async (_params, reqCtx, next) => { |
| 68 | + const requestOrigin = reqCtx.request.headers.get('Origin'); |
| 69 | + const resolvedOrigin = resolveOrigin(config.origin, requestOrigin); |
| 70 | + |
| 71 | + reqCtx.res.headers.set('access-control-allow-origin', resolvedOrigin); |
| 72 | + if (resolvedOrigin !== '*') { |
| 73 | + reqCtx.res.headers.set('Vary', 'Origin'); |
| 74 | + } |
| 75 | + config.allowMethods.forEach(method => { |
| 76 | + reqCtx.res.headers.append('access-control-allow-methods', method); |
| 77 | + }); |
| 78 | + config.allowHeaders.forEach(header => { |
| 79 | + reqCtx.res.headers.append('access-control-allow-headers', header); |
| 80 | + }); |
| 81 | + config.exposeHeaders.forEach(header => { |
| 82 | + reqCtx.res.headers.append('access-control-expose-headers', header); |
| 83 | + }); |
| 84 | + reqCtx.res.headers.set('access-control-allow-credentials', config.credentials.toString()); |
| 85 | + if (config.maxAge !== undefined) { |
| 86 | + reqCtx.res.headers.set('access-control-max-age', config.maxAge.toString()); |
| 87 | + } |
| 88 | + |
| 89 | + // Handle preflight OPTIONS request |
| 90 | + if (reqCtx.request.method === HttpVerbs.OPTIONS && reqCtx.request.headers.has('Access-Control-Request-Method')) { |
| 91 | + return new Response(null, { |
| 92 | + status: HttpErrorCodes.NO_CONTENT, |
| 93 | + headers: reqCtx.res.headers, |
| 94 | + }); |
| 95 | + } |
| 96 | + await next(); |
| 97 | + }; |
| 98 | +}; |
0 commit comments