jackson/lib/middleware.ts

26 lines
722 B
TypeScript

import Cors from 'cors';
import { NextApiRequest, NextApiResponse } from 'next';
// Initializing the cors middleware
const corsFunction = Cors({
methods: ['GET', 'HEAD'],
});
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: any) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}
export async function cors(req: NextApiRequest, res: NextApiResponse) {
return await runMiddleware(req, res, corsFunction);
}