Skip to content

Commit

Permalink
feat: add github-stats proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Nov 8, 2024
1 parent e932b16 commit 4e142bb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/pages/api/proxy-github-stats/[...path].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// pages/api/proxy-github-stats/[...path].ts
import type {NextApiRequest, NextApiResponse} from 'next';
import https from 'https';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
const path = Array.isArray(req.query.path)
? req.query.path.join('/')
: req.query.path;

const options = {
hostname: 'stats.hyo.dev',
port: 443,
path: `/api/github-stats/${path}`,
method: req.method,
headers: {
...req.headers,
host: 'stats.hyo.dev',
},
};

const proxy = https.request(options, (response) => {
res.writeHead(response.statusCode || 500, response.headers);
response.pipe(res);
});

proxy.on('error', (error) => {
console.error(error);
res.status(500).send('Proxy request failed.');
});

if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') {
req.pipe(proxy);
} else {
proxy.end();
}
}

0 comments on commit 4e142bb

Please sign in to comment.