@oiyo/framework 0.4.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,322 +0,0 @@
1
- /**
2
- * @oiyo/framework v0.4.9
3
- * Copyright (c) 2026 skiyee. All rights reserved.
4
- * Commercial software. See LICENSE for terms.
5
- * Official site: https://oiyo.js.org
6
- */
7
- //#region src/oiyo/http/abort.d.ts
8
- interface HttpAborter {
9
- readonly signal: HttpAbortSignal;
10
- abort: (reason?: any) => void;
11
- }
12
- /**
13
- * 创建一个中断信标。
14
- *
15
- * @example
16
- * const aborter = createHttpAborter()
17
- * http.request('/pet/1', { signal: aborter.signal })
18
- * aborter.abort()
19
- */
20
- declare function createHttpAborter(): HttpAborter;
21
- type AbortListener = (this: HttpAbortSignal, event: {
22
- type: 'abort';
23
- }) => void;
24
- declare class AbortEmitter {
25
- private listeners;
26
- addEventListener(type: 'abort', listener: AbortListener): void;
27
- removeEventListener(type: 'abort', listener: AbortListener): void;
28
- protected emit(event: {
29
- type: 'abort';
30
- }): void;
31
- }
32
- declare class HttpAbortSignal extends AbortEmitter {
33
- aborted: boolean;
34
- reason: any;
35
- onabort: AbortListener | null;
36
- /**
37
- * 内部使用:标记中断并派发 `abort` 事件(幂等)。
38
- */
39
- _abort(reason?: any): void;
40
- /**
41
- * 已中断则抛出 `reason`,否则什么都不做。
42
- */
43
- throwIfAborted(): void;
44
- }
45
- //#endregion
46
- //#region src/oiyo/http/types/utils.d.ts
47
- type Prettify<T> = { [K in keyof T]: T[K]; } & {};
48
- type MaybePromise<T> = T | Promise<T>;
49
- //#endregion
50
- //#region src/oiyo/http/types/body.d.ts
51
- interface ResponseBodyTypeMap {
52
- text: string;
53
- arrayBuffer: ArrayBuffer;
54
- }
55
- type ResponseBodyType = Prettify<keyof ResponseBodyTypeMap | 'json'>;
56
- type MappedResponseBodyType<TRBType extends ResponseBodyType, TRBJsonType = any> = TRBType extends keyof ResponseBodyTypeMap ? ResponseBodyTypeMap[TRBType] : TRBJsonType;
57
- //#endregion
58
- //#region src/oiyo/http/types/uni.d.ts
59
- type UniAppRequestOptions = Omit<UniNamespace.RequestOptions, 'url' | 'data' | 'header' | 'method' | 'timeout' | 'responseType' | 'success' | 'fail' | 'complete'>;
60
- interface UniAppOnChunkData {
61
- data: ArrayBuffer;
62
- }
63
- type UniAppRequestResponse<TRBody = any> = Prettify<Omit<UniNamespace.RequestSuccessCallbackResult, 'data'> & {
64
- data: TRBody;
65
- }>;
66
- type UniAppUploadOptions = Omit<UniNamespace.UploadFileOption, 'url' | 'header' | 'formData' | 'timeout' | 'success' | 'fail' | 'complete'>;
67
- interface UniAppOnUploadProgressData extends UniNamespace.OnProgressUpdateResult {}
68
- type UniAppUploadResponse<TRBody = any> = Prettify<Omit<UniNamespace.UploadFileSuccessCallbackResult, 'data'> & {
69
- data: TRBody;
70
- }>;
71
- type UniAppDownloadBodyType = 'json';
72
- type UniAppDownloadBody = Prettify<Pick<UniNamespace.DownloadSuccessData, 'tempFilePath'>>;
73
- type UniAppDownloadOptions = Omit<UniNamespace.DownloadFileOption, 'url' | 'header' | 'timeout' | 'success' | 'fail' | 'complete'>;
74
- interface UniAppDownloadProgressData extends UniNamespace.OnProgressDownloadResult {}
75
- type UniAppDownloadResponse<TRBody = any> = Prettify<Omit<UniNamespace.DownloadSuccessData, keyof UniAppDownloadBody> & {
76
- data: TRBody;
77
- }>;
78
- //#endregion
79
- //#region src/oiyo/http/types/options.d.ts
80
- /**
81
- * 响应头监听回调入参。
82
- */
83
- interface HeadersReceived {
84
- header: Record<string, string>;
85
- }
86
- /**
87
- * 所有传输方式共享的基础选项。
88
- */
89
- interface HttpCommonOptions<TRBType extends ResponseBodyType = ResponseBodyType> {
90
- baseURL?: string;
91
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
92
- headers?: Record<string, string>;
93
- query?: Record<string, any>;
94
- body?: any;
95
- timeout?: number;
96
- _action?: 'request' | 'upload' | 'download';
97
- /**
98
- * 为 true 时返回完整响应对象(statusCode / header / data ...),
99
- * 否则只返回精炼后的 data。
100
- */
101
- raw?: boolean;
102
- responseType?: TRBType;
103
- parseResponse?: (responseText: string) => any;
104
- retry?: number | false;
105
- retryDelay?: number;
106
- /**
107
- * 重试的响应状态码
108
- * @default [408, 409, 425, 429, 500, 502, 503, 504]
109
- */
110
- retryStatusCodes?: number[];
111
- ignoreResponseError?: boolean;
112
- /**
113
- * 中断信号,由 `createHttpAborter()` 创建。
114
- * 信号触发后传输会被中断;已中断的信号会让传输立即中断。
115
- */
116
- signal?: HttpAbortSignal;
117
- /**
118
- * 监听响应头(对应 task.onHeadersReceived)。
119
- */
120
- onHeadersReceived?: (result: HeadersReceived) => void;
121
- }
122
- interface HttpRequestOptions<TRBType extends ResponseBodyType = ResponseBodyType> extends HttpCommonOptions<TRBType>, UniAppRequestOptions {
123
- /**
124
- * 传输动作
125
- * @description 常用于钩子中
126
- * @protected
127
- */
128
- _action?: 'request';
129
- /**
130
- * 请求方法
131
- * @default - 'POST'
132
- */
133
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
134
- /**
135
- * 监听流式数据
136
- * @description onChunkReceived
137
- */
138
- onChunk?: (data: UniAppOnChunkData) => void;
139
- }
140
- interface HttpUploadOptions<TRBType extends ResponseBodyType = ResponseBodyType> extends HttpCommonOptions<TRBType>, UniAppUploadOptions {
141
- /**
142
- * 传输动作
143
- * @description 常用于钩子中
144
- * @protected
145
- */
146
- _action?: 'upload';
147
- /**
148
- * 上传方法,强制 POST
149
- * @default - 'POST'
150
- */
151
- method?: 'POST';
152
- /**
153
- * 监听上传进度
154
- * @description 对应 onProgressUpdate
155
- */
156
- onProgress?: (data: UniAppOnUploadProgressData) => void;
157
- }
158
- interface HttpDownloadOptions<TRBType extends UniAppDownloadBodyType = 'json'> extends HttpCommonOptions<TRBType>, UniAppDownloadOptions {
159
- /**
160
- * 传输动作
161
- * @description 常用于钩子中
162
- * @protected
163
- */
164
- _action?: 'download';
165
- /**
166
- * 下载方法,强制 GET
167
- * @default - 'GET'
168
- */
169
- method?: 'GET';
170
- /**
171
- * 监听下载进度
172
- * @description 对应 onProgressUpdate
173
- */
174
- onProgress?: (data: UniAppDownloadProgressData) => void;
175
- }
176
- /**
177
- * 传输地址
178
- */
179
- type HttpURL = string;
180
- /**
181
- * 传输属性
182
- */
183
- interface HttpOptions extends HttpCommonOptions, UniAppRequestOptions, UniAppUploadOptions, UniAppDownloadOptions {}
184
- //#endregion
185
- //#region src/oiyo/http/types/response.d.ts
186
- type HttpRequestResult<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = MappedResponseBodyType<TRBType, TRBody>;
187
- type HttpUploadResult<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = MappedResponseBodyType<TRBType, TRBody>;
188
- type HttpDownloadResult<TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = 'json'> = MappedResponseBodyType<TRBType, TRBody>;
189
- type HttpRequestResponse<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = UniAppRequestResponse<MappedResponseBodyType<TRBType, TRBody>>;
190
- type HttpUploadResponse<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = UniAppUploadResponse<MappedResponseBodyType<TRBType, TRBody>>;
191
- type HttpDownloadResponse<TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = 'json'> = UniAppDownloadResponse<MappedResponseBodyType<TRBType, TRBody>>;
192
- type HttpResponse<T = any> = HttpRequestResponse<T> | HttpUploadResponse<T> | HttpDownloadResponse;
193
- //#endregion
194
- //#region src/oiyo/http/types/core.d.ts
195
- interface HttpRequest {
196
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options: HttpRequestOptions<TRBType> & {
197
- raw: true;
198
- }): Promise<HttpRequestResponse<TRBody, TRBType>>;
199
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options?: HttpRequestOptions<TRBType>): Promise<HttpRequestResult<TRBody, TRBType>>;
200
- }
201
- interface HttpUpload {
202
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options: HttpUploadOptions<TRBType> & {
203
- raw: true;
204
- }): Promise<HttpUploadResponse<TRBody, TRBType>>;
205
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options?: HttpUploadOptions<TRBType>): Promise<HttpUploadResult<TRBody, TRBType>>;
206
- }
207
- interface HttpDownload {
208
- <TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = UniAppDownloadBodyType>(resource: HttpURL, options: HttpDownloadOptions<TRBType> & {
209
- raw: true;
210
- }): Promise<HttpDownloadResponse<TRBody, TRBType>>;
211
- <TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = UniAppDownloadBodyType>(resource: HttpURL, options?: HttpDownloadOptions<TRBType>): Promise<HttpDownloadResult<TRBody, TRBType>>;
212
- }
213
- type HttpHookContextOptions = HttpRequestOptions & {
214
- _action: 'request';
215
- } | HttpUploadOptions & {
216
- _action: 'upload';
217
- } | HttpDownloadOptions & {
218
- _action: 'download';
219
- };
220
- interface HttpHooks<TContextOptions, TResponse> {
221
- onRequest?: (context: {
222
- resource: HttpURL;
223
- options: TContextOptions;
224
- }) => MaybePromise<void>;
225
- onRequestError?: (context: {
226
- resource: HttpURL;
227
- options: TContextOptions;
228
- error: Error;
229
- }) => MaybePromise<void>;
230
- onResponse?: (context: {
231
- resource: HttpURL;
232
- options: TContextOptions;
233
- response: TResponse;
234
- }) => MaybePromise<void>;
235
- onResponseError?: (context: {
236
- resource: HttpURL;
237
- options: TContextOptions;
238
- response: TResponse;
239
- }) => MaybePromise<void>;
240
- }
241
- interface HttpConfig extends HttpCommonOptions, HttpHooks<HttpHookContextOptions, HttpResponse> {}
242
- interface HttpError<T = any> extends Error {
243
- resource?: HttpURL;
244
- options?: HttpOptions;
245
- response?: HttpResponse<T>;
246
- data: T;
247
- }
248
- //#endregion
249
- //#region src/oiyo/http/core.d.ts
250
- interface Http {
251
- /**
252
- * 发起请求
253
- *
254
- * @example
255
- * const data = await http.request<IUserInfoRes>('/user/info')
256
- * // data 是精炼后的数据,同 `IUserInfoRes` 结构的值
257
- *
258
- * const response = await http.request<IUserInfoRes>('/user/info', { raw: true })
259
- * // response 是源响应数据,你必须通过 response.data 才能拿到同 `IUserInfoRes` 结构的值
260
- */
261
- request: HttpRequest;
262
- /**
263
- * 文件上传
264
- *
265
- * @example
266
- * const data = await http.upload<IUploadRes>('/upload', { name: 'file', filePath, body: { category: 'avatar' } })
267
- * // data 是精炼后的数据,同 `IUploadRes` 结构的值
268
- *
269
- * const response = await http.upload<IUploadRes>('/upload', { raw: true, name: 'file', filePath, body: { category: 'avatar' } })
270
- * // response 是源响应数据,你必须通过 response.data 才能拿到同 `IUploadRes` 结构的值
271
- */
272
- upload: HttpUpload;
273
- /**
274
- * 文件下载
275
- *
276
- * @example
277
- * const data = await http.download('/file/report.pdf')
278
- * // data.tempFilePath 可以直接获取到临时文件路径
279
- *
280
- * const response = await http.download('/file/report.pdf', { raw: true })
281
- * // response 是源响应数据,你必须通过 response.data.tempFilePath 才能拿到
282
- */
283
- download: HttpDownload;
284
- /**
285
- * 基于当前配置派生一个新实例。
286
- *
287
- * @example
288
- * // 作为实例源
289
- * const client = http.create({ ... })
290
- *
291
- * // 派生鉴权/订单专用实例,它们都会继承与 client 中的配置
292
- * const authClient = client.create({...})
293
- * const orderClient = client.create({...})
294
- */
295
- create: (config: HttpConfig) => Http;
296
- }
297
- declare function createHttp(globalConfig?: HttpConfig): Http;
298
- /**
299
- * 请求层
300
- *
301
- * @docs https://oiyo.js.org/docs/manual/data/http
302
- * @example
303
- * // 发起请求
304
- * const data = await http.request<IUserInfoRes>('/user/info')
305
- *
306
- * // 文件上传
307
- * const data = await http.upload<IUploadRes>('/upload', { name: 'file', filePath, body: { category: 'avatar' } })
308
- *
309
- * // 文件下载
310
- * const { tempFilePath } = await http.download('/file/report.pdf')
311
- *
312
- * // 中断进行中的请求
313
- * const aborter = createHttpAborter()
314
- * http.request('/pet/1', { signal: aborter.signal })
315
- * aborter.abort()
316
- *
317
- * // 派生一个新实例(覆盖超时等配置,公共实例的钩子会一并串联执行)
318
- * const longTimeoutClient = http.create({ timeout: 120000 })
319
- */
320
- declare const http: Http;
321
- //#endregion
322
- export { HttpDownloadResponse as a, HttpUploadResponse as c, HttpOptions as d, HttpRequestOptions as f, createHttpAborter as h, HttpError as i, HttpCommonOptions as l, ResponseBodyType as m, http as n, HttpRequestResponse as o, HttpUploadOptions as p, HttpConfig as r, HttpResponse as s, createHttp as t, HttpDownloadOptions as u };
@@ -1,322 +0,0 @@
1
- /**
2
- * @oiyo/framework v0.4.9
3
- * Copyright (c) 2026 skiyee. All rights reserved.
4
- * Commercial software. See LICENSE for terms.
5
- * Official site: https://oiyo.js.org
6
- */
7
- //#region src/oiyo/http/abort.d.ts
8
- interface HttpAborter {
9
- readonly signal: HttpAbortSignal;
10
- abort: (reason?: any) => void;
11
- }
12
- /**
13
- * 创建一个中断信标。
14
- *
15
- * @example
16
- * const aborter = createHttpAborter()
17
- * http.request('/pet/1', { signal: aborter.signal })
18
- * aborter.abort()
19
- */
20
- declare function createHttpAborter(): HttpAborter;
21
- type AbortListener = (this: HttpAbortSignal, event: {
22
- type: 'abort';
23
- }) => void;
24
- declare class AbortEmitter {
25
- private listeners;
26
- addEventListener(type: 'abort', listener: AbortListener): void;
27
- removeEventListener(type: 'abort', listener: AbortListener): void;
28
- protected emit(event: {
29
- type: 'abort';
30
- }): void;
31
- }
32
- declare class HttpAbortSignal extends AbortEmitter {
33
- aborted: boolean;
34
- reason: any;
35
- onabort: AbortListener | null;
36
- /**
37
- * 内部使用:标记中断并派发 `abort` 事件(幂等)。
38
- */
39
- _abort(reason?: any): void;
40
- /**
41
- * 已中断则抛出 `reason`,否则什么都不做。
42
- */
43
- throwIfAborted(): void;
44
- }
45
- //#endregion
46
- //#region src/oiyo/http/types/utils.d.ts
47
- type Prettify<T> = { [K in keyof T]: T[K]; } & {};
48
- type MaybePromise<T> = T | Promise<T>;
49
- //#endregion
50
- //#region src/oiyo/http/types/body.d.ts
51
- interface ResponseBodyTypeMap {
52
- text: string;
53
- arrayBuffer: ArrayBuffer;
54
- }
55
- type ResponseBodyType = Prettify<keyof ResponseBodyTypeMap | 'json'>;
56
- type MappedResponseBodyType<TRBType extends ResponseBodyType, TRBJsonType = any> = TRBType extends keyof ResponseBodyTypeMap ? ResponseBodyTypeMap[TRBType] : TRBJsonType;
57
- //#endregion
58
- //#region src/oiyo/http/types/uni.d.ts
59
- type UniAppRequestOptions = Omit<UniNamespace.RequestOptions, 'url' | 'data' | 'header' | 'method' | 'timeout' | 'responseType' | 'success' | 'fail' | 'complete'>;
60
- interface UniAppOnChunkData {
61
- data: ArrayBuffer;
62
- }
63
- type UniAppRequestResponse<TRBody = any> = Prettify<Omit<UniNamespace.RequestSuccessCallbackResult, 'data'> & {
64
- data: TRBody;
65
- }>;
66
- type UniAppUploadOptions = Omit<UniNamespace.UploadFileOption, 'url' | 'header' | 'formData' | 'timeout' | 'success' | 'fail' | 'complete'>;
67
- interface UniAppOnUploadProgressData extends UniNamespace.OnProgressUpdateResult {}
68
- type UniAppUploadResponse<TRBody = any> = Prettify<Omit<UniNamespace.UploadFileSuccessCallbackResult, 'data'> & {
69
- data: TRBody;
70
- }>;
71
- type UniAppDownloadBodyType = 'json';
72
- type UniAppDownloadBody = Prettify<Pick<UniNamespace.DownloadSuccessData, 'tempFilePath'>>;
73
- type UniAppDownloadOptions = Omit<UniNamespace.DownloadFileOption, 'url' | 'header' | 'timeout' | 'success' | 'fail' | 'complete'>;
74
- interface UniAppDownloadProgressData extends UniNamespace.OnProgressDownloadResult {}
75
- type UniAppDownloadResponse<TRBody = any> = Prettify<Omit<UniNamespace.DownloadSuccessData, keyof UniAppDownloadBody> & {
76
- data: TRBody;
77
- }>;
78
- //#endregion
79
- //#region src/oiyo/http/types/options.d.ts
80
- /**
81
- * 响应头监听回调入参。
82
- */
83
- interface HeadersReceived {
84
- header: Record<string, string>;
85
- }
86
- /**
87
- * 所有传输方式共享的基础选项。
88
- */
89
- interface HttpCommonOptions<TRBType extends ResponseBodyType = ResponseBodyType> {
90
- baseURL?: string;
91
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
92
- headers?: Record<string, string>;
93
- query?: Record<string, any>;
94
- body?: any;
95
- timeout?: number;
96
- _action?: 'request' | 'upload' | 'download';
97
- /**
98
- * 为 true 时返回完整响应对象(statusCode / header / data ...),
99
- * 否则只返回精炼后的 data。
100
- */
101
- raw?: boolean;
102
- responseType?: TRBType;
103
- parseResponse?: (responseText: string) => any;
104
- retry?: number | false;
105
- retryDelay?: number;
106
- /**
107
- * 重试的响应状态码
108
- * @default [408, 409, 425, 429, 500, 502, 503, 504]
109
- */
110
- retryStatusCodes?: number[];
111
- ignoreResponseError?: boolean;
112
- /**
113
- * 中断信号,由 `createHttpAborter()` 创建。
114
- * 信号触发后传输会被中断;已中断的信号会让传输立即中断。
115
- */
116
- signal?: HttpAbortSignal;
117
- /**
118
- * 监听响应头(对应 task.onHeadersReceived)。
119
- */
120
- onHeadersReceived?: (result: HeadersReceived) => void;
121
- }
122
- interface HttpRequestOptions<TRBType extends ResponseBodyType = ResponseBodyType> extends HttpCommonOptions<TRBType>, UniAppRequestOptions {
123
- /**
124
- * 传输动作
125
- * @description 常用于钩子中
126
- * @protected
127
- */
128
- _action?: 'request';
129
- /**
130
- * 请求方法
131
- * @default - 'POST'
132
- */
133
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
134
- /**
135
- * 监听流式数据
136
- * @description onChunkReceived
137
- */
138
- onChunk?: (data: UniAppOnChunkData) => void;
139
- }
140
- interface HttpUploadOptions<TRBType extends ResponseBodyType = ResponseBodyType> extends HttpCommonOptions<TRBType>, UniAppUploadOptions {
141
- /**
142
- * 传输动作
143
- * @description 常用于钩子中
144
- * @protected
145
- */
146
- _action?: 'upload';
147
- /**
148
- * 上传方法,强制 POST
149
- * @default - 'POST'
150
- */
151
- method?: 'POST';
152
- /**
153
- * 监听上传进度
154
- * @description 对应 onProgressUpdate
155
- */
156
- onProgress?: (data: UniAppOnUploadProgressData) => void;
157
- }
158
- interface HttpDownloadOptions<TRBType extends UniAppDownloadBodyType = 'json'> extends HttpCommonOptions<TRBType>, UniAppDownloadOptions {
159
- /**
160
- * 传输动作
161
- * @description 常用于钩子中
162
- * @protected
163
- */
164
- _action?: 'download';
165
- /**
166
- * 下载方法,强制 GET
167
- * @default - 'GET'
168
- */
169
- method?: 'GET';
170
- /**
171
- * 监听下载进度
172
- * @description 对应 onProgressUpdate
173
- */
174
- onProgress?: (data: UniAppDownloadProgressData) => void;
175
- }
176
- /**
177
- * 传输地址
178
- */
179
- type HttpURL = string;
180
- /**
181
- * 传输属性
182
- */
183
- interface HttpOptions extends HttpCommonOptions, UniAppRequestOptions, UniAppUploadOptions, UniAppDownloadOptions {}
184
- //#endregion
185
- //#region src/oiyo/http/types/response.d.ts
186
- type HttpRequestResult<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = MappedResponseBodyType<TRBType, TRBody>;
187
- type HttpUploadResult<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = MappedResponseBodyType<TRBType, TRBody>;
188
- type HttpDownloadResult<TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = 'json'> = MappedResponseBodyType<TRBType, TRBody>;
189
- type HttpRequestResponse<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = UniAppRequestResponse<MappedResponseBodyType<TRBType, TRBody>>;
190
- type HttpUploadResponse<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = UniAppUploadResponse<MappedResponseBodyType<TRBType, TRBody>>;
191
- type HttpDownloadResponse<TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = 'json'> = UniAppDownloadResponse<MappedResponseBodyType<TRBType, TRBody>>;
192
- type HttpResponse<T = any> = HttpRequestResponse<T> | HttpUploadResponse<T> | HttpDownloadResponse;
193
- //#endregion
194
- //#region src/oiyo/http/types/core.d.ts
195
- interface HttpRequest {
196
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options: HttpRequestOptions<TRBType> & {
197
- raw: true;
198
- }): Promise<HttpRequestResponse<TRBody, TRBType>>;
199
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options?: HttpRequestOptions<TRBType>): Promise<HttpRequestResult<TRBody, TRBType>>;
200
- }
201
- interface HttpUpload {
202
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options: HttpUploadOptions<TRBType> & {
203
- raw: true;
204
- }): Promise<HttpUploadResponse<TRBody, TRBType>>;
205
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(resource: HttpURL, options?: HttpUploadOptions<TRBType>): Promise<HttpUploadResult<TRBody, TRBType>>;
206
- }
207
- interface HttpDownload {
208
- <TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = UniAppDownloadBodyType>(resource: HttpURL, options: HttpDownloadOptions<TRBType> & {
209
- raw: true;
210
- }): Promise<HttpDownloadResponse<TRBody, TRBType>>;
211
- <TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = UniAppDownloadBodyType>(resource: HttpURL, options?: HttpDownloadOptions<TRBType>): Promise<HttpDownloadResult<TRBody, TRBType>>;
212
- }
213
- type HttpHookContextOptions = HttpRequestOptions & {
214
- _action: 'request';
215
- } | HttpUploadOptions & {
216
- _action: 'upload';
217
- } | HttpDownloadOptions & {
218
- _action: 'download';
219
- };
220
- interface HttpHooks<TContextOptions, TResponse> {
221
- onRequest?: (context: {
222
- resource: HttpURL;
223
- options: TContextOptions;
224
- }) => MaybePromise<void>;
225
- onRequestError?: (context: {
226
- resource: HttpURL;
227
- options: TContextOptions;
228
- error: Error;
229
- }) => MaybePromise<void>;
230
- onResponse?: (context: {
231
- resource: HttpURL;
232
- options: TContextOptions;
233
- response: TResponse;
234
- }) => MaybePromise<void>;
235
- onResponseError?: (context: {
236
- resource: HttpURL;
237
- options: TContextOptions;
238
- response: TResponse;
239
- }) => MaybePromise<void>;
240
- }
241
- interface HttpConfig extends HttpCommonOptions, HttpHooks<HttpHookContextOptions, HttpResponse> {}
242
- interface HttpError<T = any> extends Error {
243
- resource?: HttpURL;
244
- options?: HttpOptions;
245
- response?: HttpResponse<T>;
246
- data: T;
247
- }
248
- //#endregion
249
- //#region src/oiyo/http/core.d.ts
250
- interface Http {
251
- /**
252
- * 发起请求
253
- *
254
- * @example
255
- * const data = await http.request<IUserInfoRes>('/user/info')
256
- * // data 是精炼后的数据,同 `IUserInfoRes` 结构的值
257
- *
258
- * const response = await http.request<IUserInfoRes>('/user/info', { raw: true })
259
- * // response 是源响应数据,你必须通过 response.data 才能拿到同 `IUserInfoRes` 结构的值
260
- */
261
- request: HttpRequest;
262
- /**
263
- * 文件上传
264
- *
265
- * @example
266
- * const data = await http.upload<IUploadRes>('/upload', { name: 'file', filePath, body: { category: 'avatar' } })
267
- * // data 是精炼后的数据,同 `IUploadRes` 结构的值
268
- *
269
- * const response = await http.upload<IUploadRes>('/upload', { raw: true, name: 'file', filePath, body: { category: 'avatar' } })
270
- * // response 是源响应数据,你必须通过 response.data 才能拿到同 `IUploadRes` 结构的值
271
- */
272
- upload: HttpUpload;
273
- /**
274
- * 文件下载
275
- *
276
- * @example
277
- * const data = await http.download('/file/report.pdf')
278
- * // data.tempFilePath 可以直接获取到临时文件路径
279
- *
280
- * const response = await http.download('/file/report.pdf', { raw: true })
281
- * // response 是源响应数据,你必须通过 response.data.tempFilePath 才能拿到
282
- */
283
- download: HttpDownload;
284
- /**
285
- * 基于当前配置派生一个新实例。
286
- *
287
- * @example
288
- * // 作为实例源
289
- * const client = http.create({ ... })
290
- *
291
- * // 派生鉴权/订单专用实例,它们都会继承与 client 中的配置
292
- * const authClient = client.create({...})
293
- * const orderClient = client.create({...})
294
- */
295
- create: (config: HttpConfig) => Http;
296
- }
297
- declare function createHttp(globalConfig?: HttpConfig): Http;
298
- /**
299
- * 请求层
300
- *
301
- * @docs https://oiyo.js.org/docs/manual/data/http
302
- * @example
303
- * // 发起请求
304
- * const data = await http.request<IUserInfoRes>('/user/info')
305
- *
306
- * // 文件上传
307
- * const data = await http.upload<IUploadRes>('/upload', { name: 'file', filePath, body: { category: 'avatar' } })
308
- *
309
- * // 文件下载
310
- * const { tempFilePath } = await http.download('/file/report.pdf')
311
- *
312
- * // 中断进行中的请求
313
- * const aborter = createHttpAborter()
314
- * http.request('/pet/1', { signal: aborter.signal })
315
- * aborter.abort()
316
- *
317
- * // 派生一个新实例(覆盖超时等配置,公共实例的钩子会一并串联执行)
318
- * const longTimeoutClient = http.create({ timeout: 120000 })
319
- */
320
- declare const http: Http;
321
- //#endregion
322
- export { HttpDownloadResponse as a, HttpUploadResponse as c, HttpOptions as d, HttpRequestOptions as f, createHttpAborter as h, HttpError as i, HttpCommonOptions as l, ResponseBodyType as m, http as n, HttpRequestResponse as o, HttpUploadOptions as p, HttpConfig as r, HttpResponse as s, createHttp as t, HttpDownloadOptions as u };