Skip to content

Commit ea37a2d

Browse files
committed
chore: cors config for github-stats
1 parent 4e142bb commit ea37a2d

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/pages/api/github-stats.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// pages/api/github-stats.ts
2+
13
export const revalidate = 3600;
24

35
import {track} from '@amplitude/analytics-node';
@@ -13,15 +15,27 @@ export default async function handler(
1315
req: NextApiRequest,
1416
res: NextApiResponse<string | unknown | {message: string}>,
1517
): Promise<void> {
18+
res.setHeader('Access-Control-Allow-Origin', '*');
19+
res.setHeader(
20+
'Access-Control-Allow-Methods',
21+
'GET,POST,PUT,PATCH,DELETE,OPTIONS',
22+
);
23+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
24+
25+
if (req.method === 'OPTIONS') {
26+
res.status(200).end();
27+
return;
28+
}
29+
1630
initNodeAmplitude();
1731

1832
const locale = currentLocale(req);
19-
const method = <string>req.method;
33+
const method = req.method as string;
2034
const {common} = await getTranslates(locale);
2135

2236
switch (method) {
23-
case 'GET':
24-
const loginParam = <string>req.query.login;
37+
case 'GET': {
38+
const loginParam = req.query.login as string;
2539
assert(loginParam, common.badRequest);
2640

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

3347
if (!stats) {
3448
res.status(404).json({message: common.notFound});
35-
3649
return;
3750
}
3851

@@ -50,8 +63,9 @@ export default async function handler(
5063
}
5164

5265
break;
53-
case 'POST':
54-
const loginBody = <string>req.body.login;
66+
}
67+
case 'POST': {
68+
const loginBody = req.body.login as string;
5569
assert(loginBody, common.badRequest);
5670

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

6377
if (!stats) {
6478
res.status(404).json({message: common.notFound});
65-
6679
return;
6780
}
6881

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

75-
res.send({stats});
88+
res.json({stats});
7689
} catch (err) {
7790
res.status(500).send(err);
7891
}
7992

8093
break;
94+
}
8195
default:
8296
res.status(404).json({message: common.notFound});
8397
}

0 commit comments

Comments
 (0)