Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ZeroClover/301505cbc950ce88b8b00e758684e575 to your computer and use it in GitHub Desktop.

Select an option

Save ZeroClover/301505cbc950ce88b8b00e758684e575 to your computer and use it in GitHub Desktop.
const handleRequest = () => {
const { url } = $request;
// 解析 URL
const urlObj = new URL(url);
const pathSegments = urlObj.pathname.split('/').filter(Boolean);
// 检查 AI Vendor 是否为 openai
const openaiIndex = pathSegments.findIndex(segment => segment === 'openai');
if (openaiIndex === -1) {
console.log(`不是 OpenAI 的请求,不做修改。原始 URL: ${url}`);
return $done({});
}
// 检查是否存在需要移除的 API 版本部分
const apiVersionIndex = pathSegments.findIndex((segment, index) =>
/^v\d+$/.test(segment) && index > openaiIndex
);
if (apiVersionIndex === -1) {
console.log(`未找到需要移除的 API 版本,原始 URL: ${url}`);
return $done({});
}
// 移除 API 版本部分
const removedVersion = pathSegments[apiVersionIndex];
pathSegments.splice(apiVersionIndex, 1);
// 重建 URL
urlObj.pathname = '/' + pathSegments.join('/');
const newUrl = urlObj.toString();
console.log(`URL 已修改:`);
console.log(`原始 URL: ${url}`);
console.log(`新 URL: ${newUrl}`);
console.log(`移除的 API 版本: ${removedVersion}`);
$done({ url: newUrl });
};
handleRequest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment