@oiyo/framework 0.5.0 → 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,362 +0,0 @@
1
- /**
2
- * @oiyo/framework v0.5.0
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/utils/emitter.d.ts
8
- type EmitterListener = (...arg: unknown[]) => void;
9
- declare abstract class Emitter {
10
- private listeners;
11
- constructor();
12
- on(id: string, listener: EmitterListener): void;
13
- off(id: string, listener: EmitterListener): void;
14
- protected emit(id: string, ...args: unknown[]): void;
15
- remove(id: string): void;
16
- }
17
- //#endregion
18
- //#region src/oiyo/http/abort.d.ts
19
- type OnabortListener = () => void;
20
- declare class HttpAbortSignal extends Emitter {
21
- protected _aborted: boolean;
22
- protected _reason: unknown;
23
- protected _onabortListener: OnabortListener | null;
24
- get aborted(): boolean;
25
- get reason(): unknown;
26
- protected _abort(reason: unknown): void;
27
- throwIfAborted(): void;
28
- }
29
- declare class HttpAborter {
30
- private readonly _signal;
31
- constructor();
32
- get signal(): HttpAbortSignal;
33
- abort(reason?: unknown): void;
34
- }
35
- /**
36
- * 创建一个中断信标。
37
- *
38
- * @example
39
- * const aborter = createHttpAborter()
40
- * http.request('/pet/1', { signal: aborter.signal })
41
- * aborter.abort()
42
- */
43
- declare function createHttpAborter(): HttpAborter;
44
- //#endregion
45
- //#region src/oiyo/http/types/utils.d.ts
46
- type Prettify<T> = { [K in keyof T]: T[K]; } & {};
47
- type MaybePromise<T> = T | Promise<T>;
48
- //#endregion
49
- //#region src/oiyo/http/types/body.d.ts
50
- interface ResponseBodyTypeMap {
51
- text: string;
52
- arrayBuffer: ArrayBuffer;
53
- }
54
- type ResponseBodyType = Prettify<keyof ResponseBodyTypeMap | 'json'>;
55
- type MappedResponseBodyType<TRBType extends ResponseBodyType, TRBJsonType = any> = TRBType extends keyof ResponseBodyTypeMap ? ResponseBodyTypeMap[TRBType] : TRBJsonType;
56
- //#endregion
57
- //#region src/oiyo/http/types/uni.d.ts
58
- interface UniAppOnHeadersData {
59
- header: Record<string, string>;
60
- }
61
- type UniAppRequestOptions = Omit<UniNamespace.RequestOptions, 'url' | 'data' | 'header' | 'method' | 'timeout' | 'responseType' | 'success' | 'fail' | 'complete'>;
62
- interface UniAppRequestChunkData {
63
- data: ArrayBuffer;
64
- }
65
- type UniAppRequestResponse<TRBody = any> = Prettify<Omit<UniNamespace.RequestSuccessCallbackResult, 'data'> & {
66
- data: TRBody;
67
- }>;
68
- type UniAppUploadOptions = Omit<UniNamespace.UploadFileOption, 'url' | 'header' | 'formData' | 'timeout' | 'success' | 'fail' | 'complete'>;
69
- interface UniAppUploadOnProgressData extends UniNamespace.OnProgressUpdateResult {}
70
- type UniAppUploadResponse<TRBody = any> = Prettify<Omit<UniNamespace.UploadFileSuccessCallbackResult, 'data'> & {
71
- data: TRBody;
72
- }>;
73
- type UniAppDownloadBodyType = 'json';
74
- type UniAppDownloadBody = Prettify<Pick<UniNamespace.DownloadSuccessData, 'tempFilePath'>>;
75
- type UniAppDownloadOptions = Omit<UniNamespace.DownloadFileOption, 'url' | 'header' | 'timeout' | 'success' | 'fail' | 'complete'>;
76
- interface UniAppDownloadOnProgressData extends UniNamespace.OnProgressDownloadResult {}
77
- type UniAppDownloadResponse<TRBody = any> = Prettify<Omit<UniNamespace.DownloadSuccessData, keyof UniAppDownloadBody> & {
78
- data: TRBody;
79
- }>;
80
- //#endregion
81
- //#region src/oiyo/http/types/response.d.ts
82
- type HttpRequestResult<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = MappedResponseBodyType<TRBType, TRBody>;
83
- type HttpUploadResult<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = MappedResponseBodyType<TRBType, TRBody>;
84
- type HttpDownloadResult<TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = 'json'> = MappedResponseBodyType<TRBType, TRBody>;
85
- type HttpRequestResponse<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = UniAppRequestResponse<MappedResponseBodyType<TRBType, TRBody>>;
86
- type HttpUploadResponse<TRBody = any, TRBType extends ResponseBodyType = ResponseBodyType> = UniAppUploadResponse<MappedResponseBodyType<TRBType, TRBody>>;
87
- type HttpDownloadResponse<TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = 'json'> = UniAppDownloadResponse<MappedResponseBodyType<TRBType, TRBody>>;
88
- type HttpResponse<T = any> = HttpRequestResponse<T> | HttpUploadResponse<T> | HttpDownloadResponse;
89
- //#endregion
90
- //#region src/oiyo/http/types/options.d.ts
91
- /**
92
- * 所有传输方式共享的基础选项。
93
- */
94
- interface HttpCommonOptions<TRBType extends ResponseBodyType = ResponseBodyType> {
95
- /**
96
- * 传输动作
97
- * @description 常用于钩子中
98
- * @protected
99
- */
100
- _action?: 'request' | 'upload' | 'download';
101
- /** 与相对 url 进行拼接的基础地址 */
102
- baseURL?: string;
103
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
104
- /** 传输过程中所需的请求头 */
105
- headers?: Record<string, string>;
106
- /** 与完整传输地址进行拼接的查询参数 */
107
- query?: Record<string, any>;
108
- body?: any;
109
- timeout?: number;
110
- /**
111
- * 为 true 时返回完整响应对象(statusCode / header / data ...),
112
- * 否则只返回精炼后的 data。
113
- */
114
- raw?: boolean;
115
- responseType?: TRBType;
116
- parseResponse?: (responseText: string) => any;
117
- /**
118
- * 重试的响应状态码
119
- * @default [408, 409, 425, 429, 500, 502, 503, 504]
120
- */
121
- retryStatusCodes?: number[];
122
- /**
123
- * 是否基于重试响应状态码进行重试
124
- * @default 1
125
- */
126
- retry?: number | false;
127
- /**
128
- * 重试延迟时间,单位 ms
129
- * @default 0
130
- */
131
- retryDelay?: number;
132
- /**
133
- * 判定响应是否走 `onResponseSuccess`;未通过则走 `onResponseError` 并 reject。
134
- * @default (status) => status >= 200 && status < 300
135
- */
136
- validateStatus?: (status: number) => boolean;
137
- /**
138
- * 中断信号,由 `createHttpAborter()` 创建。
139
- * 信号触发后传输会被中断;已中断的信号会让传输立即中断。
140
- */
141
- signal?: HttpAbortSignal;
142
- /**
143
- * 监听响应头(对应 task.onHeadersReceived)。
144
- */
145
- onHeaders?: (data: UniAppOnHeadersData, off: () => void) => void;
146
- }
147
- interface HttpRequestOptions<TRBType extends ResponseBodyType = ResponseBodyType> extends HttpCommonOptions<TRBType>, UniAppRequestOptions {
148
- _action?: 'request';
149
- /**
150
- * 传输方法
151
- * @default - 'GET'
152
- */
153
- method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
154
- /**
155
- * 监听流式数据,对应原生 task.onChunkReceived
156
- * @description 调用 `off()` 将关闭流式数据监听
157
- */
158
- onChunk?: (data: UniAppRequestChunkData, off: () => void) => void;
159
- }
160
- interface HttpUploadOptions<TRBType extends ResponseBodyType = ResponseBodyType> extends HttpCommonOptions<TRBType>, UniAppUploadOptions {
161
- _action?: 'upload';
162
- /**
163
- * 传输方法,强制 POST
164
- * @default - 'POST'
165
- */
166
- method?: 'POST';
167
- /**
168
- * 监听上传进度,对应原生 task.onProgressUpdate
169
- * @description 调用 `off()` 将关闭上传进度监听
170
- */
171
- onProgress?: (data: UniAppUploadOnProgressData, off: () => void) => void;
172
- }
173
- interface HttpDownloadOptions<TRBType extends UniAppDownloadBodyType = 'json'> extends HttpCommonOptions<TRBType>, UniAppDownloadOptions {
174
- _action?: 'download';
175
- /**
176
- * 传输方法,强制 GET
177
- * @default - 'GET'
178
- */
179
- method?: 'GET';
180
- /**
181
- * 监听下载进度,对应原生 task.onProgressUpdate
182
- * @description 调用 `off()` 将关闭下载进度监听
183
- */
184
- onProgress?: (data: UniAppDownloadOnProgressData, off: () => void) => void;
185
- }
186
- /**
187
- * 传输地址
188
- */
189
- type HttpURL = string;
190
- /**
191
- * 传输属性
192
- */
193
- type HttpOptions = HttpRequestOptions | HttpUploadOptions | HttpDownloadOptions;
194
- /**
195
- * 传输拦截器
196
- */
197
- interface HttpHooks<TOptions, TResponse> {
198
- /**
199
- * 请求前置拦截器
200
- * - 传输状态: 发起失败
201
- * - 触发条件: 网络发出前
202
- * - 接受返回值: options、void、Error
203
- */
204
- onBeforeRequest?: (context: {
205
- url: HttpURL;
206
- options: TOptions;
207
- }) => MaybePromise<TOptions | void>;
208
- /**
209
- * 请求错误拦截器
210
- * - 传输状态: 发起失败
211
- * - 触发条件: 发前抛错、传输 fail、主动 abort 等
212
- * - Error类: HttpError、Error...
213
- * - 接受返回值: any (抛出的值将由调用方所接受)
214
- */
215
- onRequestError?: (context: {
216
- error: Error;
217
- url: HttpURL;
218
- options: TOptions;
219
- }) => MaybePromise<unknown>;
220
- /**
221
- * 响应成功拦截器
222
- * - 传输状态: 已响应
223
- * - 触发条件: `validateStatus` 通过(默认 2xx)
224
- * - 接受返回值: 原响应值结构 (HttpResponse)、void
225
- */
226
- onResponseSuccess?: (context: {
227
- response: TResponse;
228
- url: HttpURL;
229
- options: TOptions;
230
- }) => MaybePromise<TResponse | void>;
231
- /**
232
- * 响应错误拦截器
233
- * - 传输状态: 已响应
234
- * - 触发条件: `validateStatus` 未通过
235
- * - Error类: HttpError、Error
236
- * - 接受返回值: any (抛出的值将由调用方所接受)
237
- */
238
- onResponseError?: (context: {
239
- error: Error;
240
- response: TResponse;
241
- url: HttpURL;
242
- options: TOptions;
243
- }) => MaybePromise<unknown>;
244
- }
245
- interface HttpConfig extends HttpCommonOptions, HttpHooks<HttpOptions, HttpResponse> {}
246
- //#endregion
247
- //#region src/oiyo/http/types/core.d.ts
248
- interface HttpRequest {
249
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(url: HttpURL, options: HttpRequestOptions<TRBType> & {
250
- raw: true;
251
- }): Promise<HttpRequestResponse<TRBody, TRBType>>;
252
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(url: HttpURL, options?: HttpRequestOptions<TRBType>): Promise<HttpRequestResult<TRBody, TRBType>>;
253
- }
254
- interface HttpUpload {
255
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(url: HttpURL, options: HttpUploadOptions<TRBType> & {
256
- raw: true;
257
- }): Promise<HttpUploadResponse<TRBody, TRBType>>;
258
- <TRBody = any, TRBType extends ResponseBodyType = 'json'>(url: HttpURL, options?: HttpUploadOptions<TRBType>): Promise<HttpUploadResult<TRBody, TRBType>>;
259
- }
260
- interface HttpDownload {
261
- <TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = UniAppDownloadBodyType>(url: HttpURL, options: HttpDownloadOptions<TRBType> & {
262
- raw: true;
263
- }): Promise<HttpDownloadResponse<TRBody, TRBType>>;
264
- <TRBody extends UniAppDownloadBody = UniAppDownloadBody, TRBType extends UniAppDownloadBodyType = UniAppDownloadBodyType>(url: HttpURL, options?: HttpDownloadOptions<TRBType>): Promise<HttpDownloadResult<TRBody, TRBType>>;
265
- }
266
- //#endregion
267
- //#region src/oiyo/http/core.d.ts
268
- interface Http {
269
- /**
270
- * 发起请求
271
- *
272
- * @example
273
- * const data = await http.request<IUserInfoRes>('/user/info')
274
- * // data 是精炼后的数据,同 `IUserInfoRes` 结构的值
275
- *
276
- * const response = await http.request<IUserInfoRes>('/user/info', { raw: true })
277
- * // response 是源响应数据,你必须通过 response.data 才能拿到同 `IUserInfoRes` 结构的值
278
- */
279
- request: HttpRequest;
280
- /**
281
- * 文件上传
282
- *
283
- * @example
284
- * const data = await http.upload<IUploadRes>('/upload', { name: 'file', filePath, body: { category: 'avatar' } })
285
- * // data 是精炼后的数据,同 `IUploadRes` 结构的值
286
- *
287
- * const response = await http.upload<IUploadRes>('/upload', { raw: true, name: 'file', filePath, body: { category: 'avatar' } })
288
- * // response 是源响应数据,你必须通过 response.data 才能拿到同 `IUploadRes` 结构的值
289
- */
290
- upload: HttpUpload;
291
- /**
292
- * 文件下载
293
- *
294
- * @example
295
- * const data = await http.download('/file/report.pdf')
296
- * // data.tempFilePath 可以直接获取到临时文件路径
297
- *
298
- * const response = await http.download('/file/report.pdf', { raw: true })
299
- * // response 是源响应数据,你必须通过 response.data.tempFilePath 才能拿到
300
- */
301
- download: HttpDownload;
302
- /**
303
- * 基于当前配置派生一个新实例。
304
- *
305
- * @example
306
- * // 作为实例源
307
- * const client = http.create({ ... })
308
- *
309
- * // 派生鉴权/订单专用实例,它们都会继承与 client 中的配置
310
- * const authClient = client.create({...})
311
- * const orderClient = client.create({...})
312
- */
313
- create: (config: HttpConfig) => Http;
314
- }
315
- declare function createHttp(globalConfig?: HttpConfig): Http;
316
- /**
317
- * 请求层
318
- *
319
- * @docs https://oiyo.js.org/docs/manual/data/http
320
- * @example
321
- * // 发起请求
322
- * const data = await http.request<IUserInfoRes>('/user/info')
323
- *
324
- * // 文件上传
325
- * const data = await http.upload<IUploadRes>('/upload', { name: 'file', filePath, body: { category: 'avatar' } })
326
- *
327
- * // 文件下载
328
- * const { tempFilePath } = await http.download('/file/report.pdf')
329
- *
330
- * // 中断进行中的请求
331
- * const aborter = createHttpAborter()
332
- * http.request('/pet/1', { signal: aborter.signal })
333
- * aborter.abort()
334
- *
335
- * // 派生一个新实例(覆盖超时等配置,公共实例的钩子会一并串联执行)
336
- * const longTimeoutClient = http.create({ timeout: 120000 })
337
- */
338
- declare const http: Http;
339
- //#endregion
340
- //#region src/oiyo/http/error.d.ts
341
- declare class HttpError<TB = any> extends Error {
342
- /** 调用方在传输过程中主动中止 */
343
- static readonly ABORT = "ABORT";
344
- /** 调用方无法发起传输行为:网络错误、跨域、超时等 */
345
- static readonly FAIL = "FAIL";
346
- /** 响应的 HTTP 状态码未通过 `validateStatus`(默认非 2xx) */
347
- static readonly STATUS = "STATUS";
348
- readonly url?: HttpURL;
349
- readonly options?: HttpOptions;
350
- readonly response?: HttpResponse<TB>;
351
- readonly statusCode?: number;
352
- readonly statusMessage?: string;
353
- readonly data?: TB;
354
- constructor(options: {
355
- name: string;
356
- message: string;
357
- cause?: unknown;
358
- });
359
- }
360
- declare function isHttpError(error: unknown): error is HttpError;
361
- //#endregion
362
- export { HttpCommonOptions as a, HttpOptions as c, HttpDownloadResponse as d, HttpRequestResponse as f, createHttpAborter as g, ResponseBodyType as h, http as i, HttpRequestOptions as l, HttpUploadResponse as m, isHttpError as n, HttpConfig as o, HttpResponse as p, createHttp as r, HttpDownloadOptions as s, HttpError as t, HttpUploadOptions as u };