refactor(*): 稳定期治理 - 收敛核心架构、错误语义并修复跨项目数据竞态
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { createClientId } from "@/lib/client-id";
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import { buildApiUrl, isSystemProxyBaseUrl } from "@/stores/use-config-store";
|
||||
import { createChannelTransport } from "@/services/api/channel-transport";
|
||||
import { buildApiUrl } from "@/stores/use-config-store";
|
||||
|
||||
import type { RequestOptions, ResolvedAiConfig } from "./video-contracts";
|
||||
|
||||
@@ -19,36 +16,13 @@ export type VideoTransport = {
|
||||
* 统一视频 Provider 的 HTTP 边界。Provider 只负责协议和 payload,不再重复拼接中转请求。
|
||||
*/
|
||||
export function createVideoTransport(config: ResolvedAiConfig): VideoTransport {
|
||||
const headers = (contentType?: string) => ({
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
...(contentType ? { "Content-Type": contentType } : {}),
|
||||
...(isSystemProxyBaseUrl(config.baseUrl) ? { "X-Canvas-Scene": "video", "X-Idempotency-Key": createClientId() } : {}),
|
||||
});
|
||||
const requestOptions = (options?: RequestOptions) => ({
|
||||
withCredentials: false,
|
||||
signal: options?.signal,
|
||||
});
|
||||
|
||||
const transport = createChannelTransport(config, "video");
|
||||
return {
|
||||
apiUrl: (path) => buildApiUrl(config.baseUrl, path),
|
||||
post: async <T>(upstreamUrl: string, body: unknown, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl, headers("application/json"));
|
||||
return (await axios.post<T>(request.url, body, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include" })).data;
|
||||
},
|
||||
postForm: async <T>(upstreamUrl: string, body: FormData, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl, headers());
|
||||
return (await axios.post<T>(request.url, body, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include" })).data;
|
||||
},
|
||||
get: async <T>(upstreamUrl: string, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl);
|
||||
return (await axios.get<T>(request.url, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include" })).data;
|
||||
},
|
||||
getBlob: async (upstreamUrl: string, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl);
|
||||
return (await axios.get<Blob>(request.url, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include", responseType: "blob" })).data;
|
||||
},
|
||||
getExternalBlob: async (url: string, externalHeaders: Record<string, string>, options?: RequestOptions) => {
|
||||
return (await axios.get<Blob>(url, { headers: externalHeaders, responseType: "blob", signal: options?.signal })).data;
|
||||
},
|
||||
post: (upstreamUrl, body, options) => transport.postJson(upstreamUrl, body, options),
|
||||
postForm: (upstreamUrl, body, options) => transport.postForm(upstreamUrl, body, options),
|
||||
get: (upstreamUrl, options) => transport.get(upstreamUrl, options),
|
||||
getBlob: (upstreamUrl, options) => transport.getBlob(upstreamUrl, options),
|
||||
getExternalBlob: (url, headers, options) => transport.getExternalBlob(url, headers, options),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user