fix: route dashscope calls through dev proxy to avoid CORS
This commit is contained in:
parent
59c4d448f3
commit
66cbfa9629
|
|
@ -2,6 +2,89 @@ import * as React from "react";
|
||||||
|
|
||||||
import { cn } from "@/shared/utils/utils";
|
import { cn } from "@/shared/utils/utils";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
__dashscopeProxyPatched__?: boolean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const DASH_SCOPE_ORIGIN = "https://dashscope.aliyuncs.com";
|
||||||
|
const DASH_SCOPE_PROXY_PREFIX = "/dashscope-proxy";
|
||||||
|
|
||||||
|
// Ensure dashscope API traffic is routed through the local proxy during development to avoid browser CORS blocks.
|
||||||
|
if (typeof window !== "undefined" && import.meta.env.DEV) {
|
||||||
|
const globalWindow = window as Window;
|
||||||
|
|
||||||
|
if (!globalWindow.__dashscopeProxyPatched__) {
|
||||||
|
const originalFetch = window.fetch.bind(window);
|
||||||
|
|
||||||
|
const rewriteUrl = (url: string): string => {
|
||||||
|
if (url.startsWith(DASH_SCOPE_PROXY_PREFIX)) return url;
|
||||||
|
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
const originPrefixed = `${window.location.origin}${DASH_SCOPE_PROXY_PREFIX}`;
|
||||||
|
if (url.startsWith(originPrefixed)) {
|
||||||
|
return url.slice(window.location.origin.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.startsWith(DASH_SCOPE_ORIGIN)) {
|
||||||
|
return url.replace(DASH_SCOPE_ORIGIN, DASH_SCOPE_PROXY_PREFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
};
|
||||||
|
|
||||||
|
const rewriteRequestInfo = (input: RequestInfo | URL): RequestInfo => {
|
||||||
|
if (typeof input === "string") {
|
||||||
|
return rewriteUrl(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input instanceof URL) {
|
||||||
|
return rewriteUrl(input.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
const rewrittenUrl = rewriteUrl(input.url);
|
||||||
|
if (rewrittenUrl === input.url) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cloned = input.clone();
|
||||||
|
const init: RequestInit = {
|
||||||
|
method: cloned.method,
|
||||||
|
headers: cloned.headers,
|
||||||
|
body:
|
||||||
|
cloned.method && ["GET", "HEAD"].includes(cloned.method.toUpperCase())
|
||||||
|
? undefined
|
||||||
|
: cloned.body,
|
||||||
|
cache: cloned.cache,
|
||||||
|
credentials: cloned.credentials,
|
||||||
|
integrity: cloned.integrity,
|
||||||
|
keepalive: cloned.keepalive,
|
||||||
|
mode: cloned.mode,
|
||||||
|
redirect: cloned.redirect,
|
||||||
|
referrer: cloned.referrer,
|
||||||
|
referrerPolicy: cloned.referrerPolicy,
|
||||||
|
signal: cloned.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Request(rewrittenUrl, init);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
|
const proxiedInput = rewriteRequestInfo(input);
|
||||||
|
|
||||||
|
if (proxiedInput instanceof Request) {
|
||||||
|
return originalFetch(proxiedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
return originalFetch(proxiedInput, init);
|
||||||
|
}) as typeof window.fetch;
|
||||||
|
|
||||||
|
globalWindow.__dashscopeProxyPatched__ = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function Card({ className, ...props }: React.ComponentProps<"div">) {
|
function Card({ className, ...props }: React.ComponentProps<"div">) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,30 @@ export default defineConfig({
|
||||||
port: 5173,
|
port: 5173,
|
||||||
host: true,
|
host: true,
|
||||||
open: true,
|
open: true,
|
||||||
|
cors: {
|
||||||
|
origin: true,
|
||||||
|
credentials: true,
|
||||||
|
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
|
||||||
|
allowedHeaders: [
|
||||||
|
"Authorization",
|
||||||
|
"Content-Type",
|
||||||
|
"X-DashScope-SSE",
|
||||||
|
"X-Requested-With",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
proxy: {
|
||||||
|
"/dashscope-proxy": {
|
||||||
|
target: "https://dashscope.aliyuncs.com",
|
||||||
|
changeOrigin: true,
|
||||||
|
secure: true,
|
||||||
|
rewrite: (path) => path.replace(/^\/dashscope-proxy/, ""),
|
||||||
|
configure: (proxy) => {
|
||||||
|
proxy.on("proxyReq", (proxyReq) => {
|
||||||
|
proxyReq.setHeader("origin", "https://dashscope.aliyuncs.com");
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
preview: {
|
preview: {
|
||||||
port: 5173,
|
port: 5173,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue