Skip to content

Commit 4e142bb

Browse files
committed
feat: add github-stats proxy
1 parent e932b16 commit 4e142bb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// pages/api/proxy-github-stats/[...path].ts
2+
import type {NextApiRequest, NextApiResponse} from 'next';
3+
import https from 'https';
4+
5+
export default function handler(req: NextApiRequest, res: NextApiResponse) {
6+
const path = Array.isArray(req.query.path)
7+
? req.query.path.join('/')
8+
: req.query.path;
9+
10+
const options = {
11+
hostname: 'stats.hyo.dev',
12+
port: 443,
13+
path: `/api/github-stats/${path}`,
14+
method: req.method,
15+
headers: {
16+
...req.headers,
17+
host: 'stats.hyo.dev',
18+
},
19+
};
20+
21+
const proxy = https.request(options, (response) => {
22+
res.writeHead(response.statusCode || 500, response.headers);
23+
response.pipe(res);
24+
});
25+
26+
proxy.on('error', (error) => {
27+
console.error(error);
28+
res.status(500).send('Proxy request failed.');
29+
});
30+
31+
if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') {
32+
req.pipe(proxy);
33+
} else {
34+
proxy.end();
35+
}
36+
}

0 commit comments

Comments
 (0)