Created
March 13, 2023 16:12
-
-
Save discountry/36268da4f35988bcfda69ab4f13382fb to your computer and use it in GitHub Desktop.
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
| const OPENAI_URL = 'https://api.openai.com'; | |
| addEventListener('fetch', event => { | |
| event.respondWith(handleRequest(event.request)) | |
| }) | |
| async function handleRequest(request) { | |
| const url = new URL(request.url); | |
| url.host = OPENAI_URL.replace(/^https?:\/\//, ''); | |
| const modifiedRequest = new Request(url.toString(), { | |
| headers: request.headers, | |
| method: request.method, | |
| body: request.body, | |
| redirect: 'follow' | |
| }); | |
| const response = await fetch(modifiedRequest); | |
| const modifiedResponse = new Response(response.body, response); | |
| // 添加允许跨域访问的响应头 | |
| modifiedResponse.headers.set('Access-Control-Allow-Origin', '*'); | |
| return modifiedResponse; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment