-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors.middleware.ts
More file actions
37 lines (33 loc) · 887 Bytes
/
Copy pathcors.middleware.ts
File metadata and controls
37 lines (33 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import type {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Context,
} from "aws-lambda";
/**
* CORS middleware to add headers for open CORS.
*/
const corsMiddleware = async (
event: APIGatewayProxyEvent,
context: Context,
response?: APIGatewayProxyResult | null,
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
): Promise<APIGatewayProxyResult | void> => {
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
if (response) {
response.headers = { ...response.headers, ...headers };
return response;
}
// Pre-flight request handling
if (event.httpMethod === "OPTIONS") {
return {
statusCode: 204,
headers,
body: "",
};
}
};
export default corsMiddleware;