We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5bc0d7a commit dd64b50Copy full SHA for dd64b50
src/addCORSHeaders.ts
@@ -0,0 +1,25 @@
1
+import type { MiddlewareObj, Request } from '@middy/core'
2
+import type {
3
+ APIGatewayProxyEventV2,
4
+ APIGatewayProxyStructuredResultV2,
5
+} from 'aws-lambda'
6
+
7
+export const addCORSHeaders = (
8
+ AccessControlAllowOrigin: string = '*',
9
+): MiddlewareObj<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2> => {
10
+ const setCORSHeaders = async (req: Request) => {
11
+ if (req.response === null) return
12
+ req.response = {
13
+ ...req.response,
14
+ headers: {
15
+ ...(req.response.headers ?? {}),
16
+ 'Access-Control-Allow-Origin': AccessControlAllowOrigin,
17
+ },
18
+ }
19
20
21
+ return {
22
+ after: setCORSHeaders,
23
+ onError: setCORSHeaders,
24
25
+}
0 commit comments