refactor: remove api.js file containing API request handling logic

This commit is contained in:
wood chen 2025-01-06 18:16:02 +08:00
parent 0661ae261f
commit f7e2b35f52
2 changed files with 64 additions and 74 deletions

View File

@ -1,74 +0,0 @@
export async function onRequest(context) {
const { request, env } = context;
// 获取原始URL并解析路径
const url = new URL(request.url);
const path = url.pathname;
// 确保环境变量存在
if (!env.API_BASEURL) {
return new Response('API_BASEURL environment variable is not set', { status: 500 });
}
// 处理 WebSocket 升级请求
if (request.headers.get('Upgrade') === 'websocket') {
const apiBaseUrl = new URL(env.API_BASEURL);
// 将 http/https 转换为 ws/wss
const wsBaseUrl = `${apiBaseUrl.protocol === 'https:' ? 'wss:' : 'ws:'}//${apiBaseUrl.host}`;
const targetURL = `${wsBaseUrl}${path}${url.search}`;
return fetch(new Request(targetURL, {
method: request.method,
headers: request.headers,
body: request.body
}));
}
// 处理普通 HTTP 请求
const apiBaseUrl = new URL(env.API_BASEURL);
const targetURL = new URL(path, apiBaseUrl.origin);
targetURL.search = url.search;
// 创建新的请求头,移除可能导致问题的头部
const newHeaders = new Headers(request.headers);
newHeaders.delete('host');
newHeaders.delete('cf-connecting-ip');
newHeaders.delete('cf-ipcountry');
// 创建新的请求
const newRequest = new Request(targetURL.toString(), {
method: request.method,
headers: newHeaders,
body: request.body,
});
try {
// 转发请求到目标服务器
const response = await fetch(newRequest);
// 创建新的响应头,设置 CORS 和其他必要的头部
const newResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
});
// 复制所有响应头
for (const [key, value] of response.headers.entries()) {
newResponse.headers.set(key, value);
}
// 设置 CORS 头
newResponse.headers.set('Access-Control-Allow-Origin', '*');
newResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
newResponse.headers.set('Access-Control-Allow-Headers', '*');
return newResponse;
} catch (error) {
return new Response(`Proxy error: ${error.message}`, {
status: 500,
headers: {
'Access-Control-Allow-Origin': '*'
}
});
}
}

View File

@ -0,0 +1,64 @@
// 处理所有 HTTP 方法
export const onRequest = async (context) => {
const { request, env } = context;
// 确保环境变量存在
if (!env.API_BASEURL) {
return new Response('API_BASEURL environment variable is not set', { status: 500 });
}
try {
// 构建目标URL
const url = new URL(request.url);
const apiBaseUrl = env.API_BASEURL.endsWith('/') ? env.API_BASEURL.slice(0, -1) : env.API_BASEURL;
const targetURL = apiBaseUrl + url.pathname + url.search;
// 处理 WebSocket 升级请求
if (request.headers.get('Upgrade') === 'websocket') {
const wsURL = targetURL.replace(/^http/, 'ws');
return fetch(wsURL, request);
}
// 创建新的请求
const newRequest = new Request(targetURL, {
method: request.method,
headers: request.headers,
body: request.body,
redirect: 'follow',
});
// 发送请求到目标服务器
const response = await fetch(newRequest);
// 创建新的响应
const newResponse = new Response(response.body, response);
// 设置 CORS 头
newResponse.headers.set('Access-Control-Allow-Origin', '*');
newResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
newResponse.headers.set('Access-Control-Allow-Headers', '*');
return newResponse;
} catch (error) {
return new Response(`Proxy error: ${error.message}`, {
status: 500,
headers: {
'Access-Control-Allow-Origin': '*'
}
});
}
}
// 处理 OPTIONS 请求
export const onRequestOptions = async (context) => {
return new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '*',
'Access-Control-Max-Age': '86400',
},
});
}