File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
src/pages/api/proxy-github-stats Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments