feat: openapi cors

This commit is contained in:
archer
2023-05-19 12:01:59 +08:00
parent bb312441c6
commit 2843178ede
12 changed files with 71 additions and 209 deletions

View File

@@ -1,4 +1,5 @@
import type { NextApiResponse } from 'next';
import type { NextApiResponse, NextApiHandler, NextApiRequest } from 'next';
import NextCors from 'nextjs-cors';
import crypto from 'crypto';
import jwt from 'jsonwebtoken';
@@ -37,11 +38,19 @@ export const axiosConfig = () => ({
}
});
/**
* get error message
*/
export const getErrMessage = (err: any, defaultMsg = ''): string => {
const msg = typeof err === 'string' ? err : err?.message || defaultMsg || '';
msg && console.log('error =>', msg);
return msg;
};
export function withNextCors(handler: NextApiHandler): NextApiHandler {
return async function nextApiHandlerWrappedWithNextCors(
req: NextApiRequest,
res: NextApiResponse
) {
const methods = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'];
const origin = req.headers.origin;
await NextCors(req, res, {
methods,
origin: origin,
optionsSuccessStatus: 200
});
return handler(req, res);
};
}