Skip to content

Commit

Permalink
chore: cors config for github-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Nov 8, 2024
1 parent 4e142bb commit ea37a2d
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/pages/api/github-stats.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// pages/api/github-stats.ts

export const revalidate = 3600;

import {track} from '@amplitude/analytics-node';
Expand All @@ -13,15 +15,27 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse<string | unknown | {message: string}>,
): Promise<void> {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Methods',
'GET,POST,PUT,PATCH,DELETE,OPTIONS',
);
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}

initNodeAmplitude();

const locale = currentLocale(req);
const method = <string>req.method;
const method = req.method as string;
const {common} = await getTranslates(locale);

switch (method) {
case 'GET':
const loginParam = <string>req.query.login;
case 'GET': {
const loginParam = req.query.login as string;
assert(loginParam, common.badRequest);

try {
Expand All @@ -32,7 +46,6 @@ export default async function handler(

if (!stats) {
res.status(404).json({message: common.notFound});

return;
}

Expand All @@ -50,8 +63,9 @@ export default async function handler(
}

break;
case 'POST':
const loginBody = <string>req.body.login;
}
case 'POST': {
const loginBody = req.body.login as string;
assert(loginBody, common.badRequest);

try {
Expand All @@ -62,7 +76,6 @@ export default async function handler(

if (!stats) {
res.status(404).json({message: common.notFound});

return;
}

Expand All @@ -72,12 +85,13 @@ export default async function handler(
extra: {login: loginBody, lang: locale},
});

res.send({stats});
res.json({stats});
} catch (err) {
res.status(500).send(err);
}

break;
}
default:
res.status(404).json({message: common.notFound});
}
Expand Down

0 comments on commit ea37a2d

Please sign in to comment.