refactor(api): cors duplication (#109)

* refactor(api): cors duplication

* refactor(middleware): moved content-type setter to top
This commit is contained in:
Nguyen Thanh Quang 2021-11-28 06:00:18 +07:00 committed by GitHub
parent 1b78ffaa91
commit 7eff77ccc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -48,12 +48,11 @@ export type NextApiRes = NextApiResponse & {
// }
export const withZipline = (handler: (req: NextApiRequest, res: NextApiResponse) => unknown) => (req: NextApiReq, res: NextApiRes) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Content-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
res.setHeader('Access-Control-Max-Age', '86400');
res.error = (message: string) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Content-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
res.setHeader('Access-Control-Max-Age', '86400');
res.json({
error: message,
});
@ -61,10 +60,6 @@ export const withZipline = (handler: (req: NextApiRequest, res: NextApiResponse)
res.forbid = (message: string) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Content-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
res.setHeader('Access-Control-Max-Age', '86400');
res.status(403);
res.json({
error: '403: ' + message,
@ -72,11 +67,6 @@ export const withZipline = (handler: (req: NextApiRequest, res: NextApiResponse)
};
res.bad = (message: string) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Content-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
res.setHeader('Access-Control-Max-Age', '86400');
res.status(401);
res.json({
error: '403: ' + message,
@ -84,11 +74,6 @@ export const withZipline = (handler: (req: NextApiRequest, res: NextApiResponse)
};
res.json = (json: any) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Content-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
res.setHeader('Access-Control-Max-Age', '86400');
res.end(JSON.stringify(json));
};
@ -158,4 +143,4 @@ export const setCookie = (
const signed = sign64(String(value), config.core.secret);
res.setHeader('Set-Cookie', serialize(name, signed, options));
};
};