fix: wrap fetch for injecting headers
This commit is contained in:
@@ -3,48 +3,6 @@ import qs from 'query-string';
|
||||
|
||||
const extractStreamRegx = /(data|error):\s*({.*?})(?=\n|$)/g;
|
||||
|
||||
const readJsonNumber = (
|
||||
storage: Storage | null,
|
||||
key: string
|
||||
): number | null => {
|
||||
if (storage == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const raw = storage.getItem(key);
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
const parsed = JSON.parse(raw);
|
||||
return typeof parsed === 'number' ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Read the active Org id the same way the umi request interceptor in
|
||||
* ``request.extensions.ts`` does, then translate it to the header the
|
||||
* backend's tenant resolver expects. Lets non-umi ``fetch()`` paths —
|
||||
* streaming Playground completions, raw image / TTS POSTs — pin
|
||||
* tenant context with the same precedence rules as everywhere else
|
||||
* (createScope override first, current org second). Returns an empty
|
||||
* object when no active org context is set.
|
||||
*/
|
||||
export const tenantHeaders = (): Record<string, string> => {
|
||||
if (typeof window === 'undefined') {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
const orgId =
|
||||
readJsonNumber(window.sessionStorage, 'createScopeOrgOverride') ??
|
||||
readJsonNumber(window.localStorage, 'currentOrganizationId');
|
||||
return orgId == null ? {} : { 'X-Organization-Id': String(orgId) };
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
const extractJSON = (
|
||||
dataStr: string
|
||||
): { results: any[]; remaining: string } => {
|
||||
@@ -113,7 +71,6 @@ export const fetchChunkedData = async (params: {
|
||||
signal: params.signal,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...tenantHeaders(),
|
||||
...(params.headers || {})
|
||||
}
|
||||
});
|
||||
@@ -167,7 +124,6 @@ export const fetchChunkedDataPostFormData = async (params: {
|
||||
body: createFormData(params.data),
|
||||
signal: params.signal,
|
||||
headers: {
|
||||
...tenantHeaders(),
|
||||
...(params.headers || {})
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { getTenantHeaders } from '@/request.extensions';
|
||||
|
||||
const isSameOrigin = (url: string): boolean => {
|
||||
try {
|
||||
// Relative URLs resolve against the current origin → same-origin.
|
||||
return new URL(url, window.location.href).origin === window.location.origin;
|
||||
} catch {
|
||||
// Unparseable input — treat as a relative same-origin path.
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const urlOf = (input: RequestInfo | URL): string => {
|
||||
if (typeof input === 'string') return input;
|
||||
if (input instanceof URL) return input.href;
|
||||
return input.url;
|
||||
};
|
||||
|
||||
const methodOf = (input: RequestInfo | URL, init?: RequestInit): string => {
|
||||
if (init?.method) return init.method;
|
||||
if (input instanceof Request) return input.method;
|
||||
return 'GET';
|
||||
};
|
||||
|
||||
const mergeHeaders = (
|
||||
base: HeadersInit | undefined,
|
||||
extra: Record<string, string>
|
||||
): Headers => {
|
||||
const merged = new Headers(base ?? undefined);
|
||||
Object.entries(extra).forEach(([key, value]) => merged.set(key, value));
|
||||
return merged;
|
||||
};
|
||||
|
||||
let installed = false;
|
||||
|
||||
export const installTenantFetch = (): void => {
|
||||
if (installed || typeof window === 'undefined' || !window.fetch) {
|
||||
return;
|
||||
}
|
||||
installed = true;
|
||||
|
||||
const nativeFetch = window.fetch.bind(window);
|
||||
|
||||
window.fetch = (
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit
|
||||
): Promise<Response> => {
|
||||
const headers = getTenantHeaders(methodOf(input, init));
|
||||
if (Object.keys(headers).length === 0 || !isSameOrigin(urlOf(input))) {
|
||||
return nativeFetch(input, init);
|
||||
}
|
||||
|
||||
// For a Request object the existing headers live on the object, not
|
||||
// in `init`; fold them in so they survive the override.
|
||||
if (input instanceof Request && init?.headers == null) {
|
||||
return nativeFetch(
|
||||
new Request(input, { headers: mergeHeaders(input.headers, headers) }),
|
||||
init
|
||||
);
|
||||
}
|
||||
|
||||
return nativeFetch(input, {
|
||||
...init,
|
||||
headers: mergeHeaders(init?.headers, headers)
|
||||
});
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user