|
36 | 36 | > 在左侧的代码编辑器中,删除现有的所有代码,然后复制粘贴以下内容到代码编辑器:
|
37 | 37 |
|
38 | 38 | ```js
|
39 |
| -export default { |
40 |
| - async fetch(request, env) { |
41 |
| - const url = new URL(request.url); |
42 |
| - url.host = "api.openai.com"; |
43 |
| - // openai is already set all CORS heasders |
44 |
| - return fetch(url, { |
45 |
| - headers: request.headers, |
46 |
| - method: request.method, |
47 |
| - body: request.body, |
48 |
| - redirect: 'follow' |
49 |
| - }); |
50 |
| - } |
| 39 | +const TELEGRAPH_URL = 'https://api.openai.com'; |
| 40 | + |
| 41 | +addEventListener('fetch', event => { |
| 42 | + event.respondWith(handleRequest(event.request)) |
| 43 | +}) |
| 44 | + |
| 45 | +async function handleRequest(request) { |
| 46 | + const url = new URL(request.url); |
| 47 | + const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*" |
| 48 | + url.host = TELEGRAPH_URL.replace(/^https?:\/\//, ''); |
| 49 | + const modifiedRequest = new Request(url.toString(), { |
| 50 | + headers: request.headers, |
| 51 | + method: request.method, |
| 52 | + body: request.body, |
| 53 | + redirect: 'follow' |
| 54 | + }); |
| 55 | + const response = await fetch(modifiedRequest); |
| 56 | + const modifiedResponse = new Response(response.body, response); |
| 57 | + // 添加允许跨域访问的响应头 |
| 58 | + modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin); |
| 59 | + return modifiedResponse; |
51 | 60 | }
|
| 61 | + |
52 | 62 | ```
|
53 | 63 |
|
54 | 64 |
|
@@ -162,4 +172,4 @@ curl --location 'https://openai.1rmb.tk/dashboard/billing/credit_grants' \
|
162 | 172 |
|
163 | 173 | ### 查询 OPENAI API 余额
|
164 | 174 |
|
165 |
| -[https://checkbilling.1rmb.tk/](https://checkbilling.1rmb.tk/) |
| 175 | +[https://checkbilling.1rmb.tk/](https://checkbilling.1rmb.tk/) |
0 commit comments