-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.ts
43 lines (38 loc) · 1.16 KB
/
middleware.ts
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
38
39
40
41
42
43
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import Cors from "cors";
export async function middleware(request: NextRequest) {
const headers = await executeCors(request);
const response = NextResponse.next();
for (const [key, value] of headers.entries()) {
response.headers.set(key, value);
}
return response;
}
async function executeCors(request: NextRequest): Promise<Headers> {
const mockRequest = {
method: request.method,
headers: Object.fromEntries(request.headers.entries()),
};
return new Promise((resolve, reject) => {
const headers = new Headers();
const mockResponse = {
getHeader: (key: string) => headers.get(key),
setHeader: (key: string, value: string) => headers.set(key, value),
end: () => resolve(headers),
};
Cors({
origin: [
"https://giveffektivt.dk",
...(process.env.DEV_WEBSITE_DOMAINS
? process.env.DEV_WEBSITE_DOMAINS.split(",")
: []),
],
})(mockRequest, mockResponse, (error) =>
error ? reject(error) : resolve(headers),
);
});
}
export const config = {
matcher: "/api/:path*",
};