Created
March 6, 2024 08:44
-
-
Save real-jiakai/b741342f12d21d25ec705422306a87a8 to your computer and use it in GitHub Desktop.
反代github api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
async fetch(request, env, ctx) { | |
try { | |
// 用您要代理的 GitHub API URL 替换这个 URL | |
const url = new URL('https://api.github.com' + new URL(request.url).pathname) | |
// 克隆原始请求,但更改其 URL | |
const newRequest = new Request(url, { | |
body: request.body, | |
headers: request.headers, | |
method: request.method | |
}) | |
// 转发请求到 GitHub API | |
const response = await fetch(newRequest) | |
// 克隆响应对象,以便可以修改头部 | |
const newResponse = new Response(response.body, response) | |
// 允许跨域请求 | |
newResponse.headers.set('Access-Control-Allow-Origin', '*') | |
// 返回新的响应对象 | |
return newResponse | |
} catch (e) { | |
// 如果有错误,返回错误信息 | |
return new Response(e.message || 'An error occurred', { status: 500 }) | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment