Files
dbg-otherway/utils/initMiddleware.ts
Fergal Moran e6ccc6b8f0 Initial commit
2023-02-23 11:35:19 +00:00

18 lines
616 B
TypeScript

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
import type { NextApiRequest, NextApiResponse } from 'next';
export default function initMiddleware(middleware: any) {
return (request: NextApiRequest, response: NextApiResponse) =>
new Promise((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
middleware(request, response, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}