Created
October 27, 2020 13:01
-
-
Save marxian/f7ccbecde5e3cb986845030860c84062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cors from 'cors' | |
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next' | |
const cors = Cors({ | |
methods: ['POST'], | |
}) | |
function runMiddleware(req, res, fn) { | |
return new Promise((resolve, reject) => { | |
fn(req, res, result => { | |
if (result instanceof Error) { | |
return reject(result) | |
} | |
return resolve(result) | |
}) | |
}) | |
} | |
export default async (req: NextApiRequest, res: NextApiResponse) => { | |
await runMiddleware(req, res, cors) | |
res.status(200).json(['some', 'stuff']) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment