-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmiddleware.ts
38 lines (34 loc) · 1.06 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
import Cors from "cors"
import {NextApiRequest, NextApiResponse} from "next"
type CorsMiddleware = (
req: NextApiRequest,
res: {
statusCode?: number | undefined
setHeader(key: string, value: string): unknown
end(): unknown
},
next: (err?: unknown) => unknown
) => void
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
export default function initMiddleware(middleware: CorsMiddleware) {
return (req: NextApiRequest, res: NextApiResponse) =>
new Promise((resolve, reject) => {
const result = () => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
}
middleware(req, res, result)
})
}
// Initialize the cors middleware
const cors = initMiddleware(
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
Cors({
// Only allow requests with GET, POST and OPTIONS
methods: ["GET", "POST", "OPTIONS"],
})
)
export {cors}