/** * @description 描述 fetch 层支持的响应读取方式。 */ export type FetchResponseType = "json" | "text" | "blob" | "arrayBuffer" | "stream" /** * @description 描述一次请求在输入侧允许携带的 query 与 body 结构。 */ export interface FetchInput { query?: unknown | undefined body?: unknown | undefined } /** * @description 描述一次请求在输出侧暴露的响应类型与数据。 */ export interface FetchOutput { type: FetchResponseType data: unknown } /** * @description 从输出声明中提取 JSON 响应数据类型。 */ export type FetchOutputJsonData = Output extends { type: "json" data: infer T } ? T : never /** * @description 从输出声明中提取文本响应数据类型。 */ export type FetchOutputTextData = Output extends { type: "text" data: infer T } ? T : never /** * @description 从输出声明中提取 Blob 响应数据类型。 */ export type FetchOutputBlobData = Output extends { type: "blob" data: infer T } ? T : never /** * @description 从输出声明中提取 ArrayBuffer 响应数据类型。 */ export type FetchOutputArrayBufferData = Output extends { type: "arrayBuffer" data: infer T } ? T : never /** * @description 从输出声明中提取流式响应数据类型。 */ export type FetchOutputStreamData = Output extends { type: "stream" data: infer T } ? T : never /** * @description 描述构造 fetch 执行器时使用的标准化选项。 * * 这些选项负责把资源契约中的 URL、方法、输入、期望响应类型以及中止控制 * 规整到统一结构里,便于不同运行时的执行器共享同一套输入语义。 */ export interface BaseFetchOptions< Input extends FetchInput = FetchInput, Output extends FetchOutput = FetchOutput, > { baseUrl: string path: string method: string headers?: Record | undefined query?: Input["query"] | undefined body?: Input["body"] | undefined responseType?: Output["type"] | undefined timeout?: number | undefined abortSignal?: AbortSignal | undefined } /** * @description 表示一个可按多种响应类型读取结果的抽象 fetch 执行器。 */ export abstract class BaseFetch< Input extends FetchInput = FetchInput, Output extends FetchOutput = FetchOutput, > { protected options: BaseFetchOptions constructor(options: BaseFetchOptions) { this.options = options } /** * @description 以 JSON 形式读取响应数据。 */ abstract getJson(): Promise> /** * @description 以文本形式读取响应数据。 */ abstract getText(): Promise> /** * @description 以 Blob 形式读取响应数据。 */ abstract getBlob(): Promise> /** * @description 以 ArrayBuffer 形式读取响应数据。 */ abstract getArrayBuffer(): Promise> /** * @description 以流形式读取响应数据。 */ abstract getStream(): Promise> } /** * @description 根据标准化请求选项创建具体 fetch 执行器的工厂函数类型。 */ export type FetchFactory< Input extends FetchInput = FetchInput, Output extends FetchOutput = FetchOutput, > = (options: BaseFetchOptions) => BaseFetch