Skip to content

Commit 5b9b540

Browse files
authored
Update cloudflare_workers.md
1 parent 14fb0b0 commit 5b9b540

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

docs/cloudflare_workers.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,29 @@
3636
> 在左侧的代码编辑器中,删除现有的所有代码,然后复制粘贴以下内容到代码编辑器:
3737
3838
```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;
5160
}
61+
5262
```
5363

5464

@@ -162,4 +172,4 @@ curl --location 'https://openai.1rmb.tk/dashboard/billing/credit_grants' \
162172

163173
### 查询 OPENAI API 余额
164174

165-
[https://checkbilling.1rmb.tk/](https://checkbilling.1rmb.tk/)
175+
[https://checkbilling.1rmb.tk/](https://checkbilling.1rmb.tk/)

0 commit comments

Comments
 (0)