forked from TransCircle/TransCircle-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
57 lines (52 loc) · 2.05 KB
/
Copy pathworker.js
File metadata and controls
57 lines (52 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* TransCircle Frontend — Cloudflare Workers entry point
*
* - API 请求(/v1/*)代理到后端服务器
* - 静态资源由 wrangler assets 托管
* - SPA fallback 由 wrangler.jsonc 的 single_page_application 处理
*
* API_BACKEND_URL 通过 wrangler.jsonc 的 [env.*.vars] 或 Cloudflare Dashboard 配置。
* 未配置时抛错而非回退到生产地址,防止开发环境意外写入生产数据。
*/
/** @param {Request} request */
/** @param {{ ASSETS: { fetch: (req: Request) => Promise<Response> }; API_BACKEND_URL?: string }} env */
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// Proxy API requests to backend server
if (url.pathname.startsWith('/v1/')) {
const backend = env.API_BACKEND_URL;
if (!backend) {
return new Response('API_BACKEND_URL not configured', { status: 502 });
}
// 用户没有 refresh_token cookie 时跳过向后端转发 /auth/refresh,
// 直接返回 200(accessToken: null),避免产生 400/401 响应。
if (url.pathname === '/v1/auth/refresh' && request.method === 'POST') {
const cookies = request.headers.get('cookie') || '';
const hasRefreshToken = cookies.split(';').some(c =>
c.trim().startsWith('refresh_token='),
);
if (!hasRefreshToken) {
return new Response(
JSON.stringify({
data: { accessToken: null, tokenType: 'Bearer', expiresIn: 0 },
requestId: `req_skipped`,
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
},
);
}
}
return fetch(`${backend}${url.pathname}${url.search}`, {
method: request.method,
headers: request.headers,
body: request.body,
});
}
// SPA fallback is handled by wrangler.jsonc asset config
// (not_found_handling: single_page_application)
return env.ASSETS.fetch(request);
},
};