uview-pro 0.0.18 → 0.0.20

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,434 +0,0 @@
1
- /**
2
- * 通用 uni-app 网络请求,uni.request
3
- * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
4
- * @author anyup
5
- */
6
- import deepMerge from '../function/deepMerge';
7
-
8
- // 请求内容类型枚举
9
- export enum ContentType {
10
- JSON = 'application/json;charset=UTF-8',
11
- URLENCODED = 'application/x-www-form-urlencoded',
12
- FORMDATA = 'multipart/form-data'
13
- }
14
-
15
- // 请求方法类型
16
- export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'UPLOAD';
17
-
18
- // 请求头类型
19
- export type HttpHeaderType = Record<string, string>;
20
-
21
- // 请求配置类型
22
- export interface HttpRequestConfig {
23
- baseURL?: string;
24
- url?: string | Function;
25
- header?: HttpHeaderType;
26
- data?: unknown;
27
- method?: HttpMethod;
28
- dataType?: string;
29
- responseType?: string;
30
- success?: (res: unknown) => void;
31
- fail?: (err: unknown) => void;
32
- complete?: (res: unknown) => void;
33
- [key: string]: unknown;
34
- }
35
-
36
- // 拦截器类型
37
- export interface Interceptor<T = unknown> {
38
- handler?: (arg: T, P: typeof Promise) => unknown;
39
- onerror?: (arg: T, P: typeof Promise) => unknown;
40
- complete?: (arg: T, P: typeof Promise) => unknown;
41
- use: (...args: any[]) => void;
42
- lock?: () => void;
43
- unlock?: () => void;
44
- clear?: () => void;
45
- p?: Promise<unknown> | null;
46
- }
47
-
48
- /**
49
- * HttpHeader 构造器,自动处理 Content-Type
50
- */
51
- export class HttpHeader {
52
- [key: string]: string;
53
- constructor(params: HttpHeaderType) {
54
- Object.keys(params).forEach(key => {
55
- if (key === 'Content-Type') {
56
- this[key] = ContentType[params[key] as keyof typeof ContentType] || params[key];
57
- } else {
58
- this[key] = params[key];
59
- }
60
- });
61
- }
62
- }
63
-
64
- /**
65
- * Builder 用于批量生成 API 方法
66
- */
67
- export class Builder {
68
- private http: Http;
69
- constructor(http: Http) {
70
- this.http = http;
71
- }
72
- /**
73
- * 根据 url 配置表批量生成 API 方法
74
- */
75
- dispatch(urls: Record<string, any>) {
76
- const obj: Record<string, any> = {};
77
- Object.keys(urls).forEach(name => {
78
- obj[name] = this.use.bind(this, urls[name]);
79
- });
80
- return obj;
81
- }
82
- /**
83
- * 发送请求
84
- */
85
- use<T = unknown>(urlConfig: Record<string, any>, data?: unknown, config: HttpRequestConfig = {}): Promise<T> {
86
- // 请求地址
87
- let url = config?.url ?? urlConfig.url;
88
- // 兼容 restful url,如果是使用url为function,则为restful格式
89
- if (config.url && typeof config.url === 'function') {
90
- url = `${urlConfig.url}${config.url()}`;
91
- }
92
- // 请求类型,get,post,put,delete
93
- const method = config?.method ?? urlConfig?.method ?? 'GET';
94
- const options = { ...deepMerge(urlConfig, config), url, method };
95
- return this.http.request<T>(options);
96
- }
97
- }
98
-
99
- /**
100
- * Http 主类,支持拦截器、链式配置、类型完善
101
- */
102
- export class Http {
103
- static Builder = Builder;
104
- config: HttpRequestConfig;
105
- interceptors: {
106
- response: Interceptor;
107
- request: Interceptor;
108
- };
109
-
110
- constructor(options: HttpRequestConfig = {}) {
111
- this.config = {
112
- baseURL: '',
113
- header: new HttpHeader({ 'Content-Type': 'JSON' }),
114
- data: {},
115
- method: 'GET',
116
- dataType: 'json',
117
- responseType: 'text',
118
- success() {},
119
- fail() {},
120
- complete() {},
121
- ...options
122
- };
123
- this.interceptors = {
124
- response: {
125
- use(handler, onerror, complete) {
126
- this.handler = handler;
127
- this.onerror = onerror;
128
- this.complete = complete;
129
- }
130
- },
131
- request: {
132
- use(handler) {
133
- this.handler = handler;
134
- }
135
- }
136
- };
137
- wrap(this.interceptors.request);
138
- wrap(this.interceptors.response);
139
- }
140
-
141
- /**
142
- * 设置 baseURL
143
- */
144
- setBaseURL(baseURL: string) {
145
- this.config.baseURL = baseURL;
146
- return this;
147
- }
148
-
149
- /**
150
- * 设置 header
151
- */
152
- setHeader(header: HttpHeaderType) {
153
- this.config.header = { ...this.config.header, ...header };
154
- return this;
155
- }
156
-
157
- /**
158
- * 设置 data
159
- */
160
- setData(data: unknown) {
161
- if (isArray(data)) {
162
- this.config.data = data;
163
- } else if (isObject(data) && isObject(this.config.data)) {
164
- this.config.data = { ...(this.config.data as object), ...(data as object) };
165
- } else {
166
- this.config.data = data;
167
- }
168
- return this;
169
- }
170
-
171
- /**
172
- * 设置其他 options
173
- */
174
- setOptions(options: HttpRequestConfig) {
175
- this.config = { ...this.config, ...options };
176
- }
177
-
178
- /**
179
- * 请求主方法
180
- * @param options 实时传递的配置
181
- * @returns Promise
182
- * 说明:配置优先级 实时传递的 options > 公共配置的 config
183
- */
184
- request<T = unknown>(options: HttpRequestConfig = {}): Promise<T> {
185
- if (!options) options = {};
186
- // 请求体
187
- const data = options.data || {};
188
- // 请求baseURL:优先级为:实时传递的 > 公共配置的
189
- options.baseURL = options.baseURL !== undefined ? options.baseURL : this.config.baseURL;
190
- // 请求头:合并公共配置与实时设置的header, 且优先级实时设置会覆盖公共配置的
191
- options.header = { ...this.config.header, ...options.header };
192
- // 请求方式:优先级为:实时传递的 > 公共配置的
193
- options.method = options.method || this.config.method;
194
- // 数据格式:默认json
195
- options.dataType = options.dataType || this.config.dataType;
196
- // 请求体:优先级为:实时传递的 > 公共配置的
197
- if (isArray(data)) {
198
- options.data = data;
199
- } else if (isObject(data) && isObject(this.config.data)) {
200
- options.data = { ...(this.config.data as object), ...(data as object) };
201
- } else {
202
- options.data = data;
203
- }
204
-
205
- // 拦截器处理
206
- const interceptors = this.interceptors;
207
- const requestInterceptor = interceptors.request;
208
- const responseInterceptor = interceptors.response;
209
- const requestInterceptorHandler = requestInterceptor.handler;
210
-
211
- return new Promise((resolve: (value: T) => void, reject: (reason?: unknown) => void) => {
212
- function isPromise(p: unknown): p is Promise<unknown> {
213
- return !!p && typeof (p as any).then === 'function' && typeof (p as any).catch === 'function';
214
- }
215
- function enqueueIfLocked(promise: Promise<unknown> | null | undefined, callback: () => void) {
216
- if (promise) {
217
- promise.then(() => {
218
- callback();
219
- });
220
- } else {
221
- callback();
222
- }
223
- }
224
- function onresult(handler: ((arg: unknown, P: typeof Promise) => unknown) | undefined, response: unknown, type: number) {
225
- enqueueIfLocked(responseInterceptor.p, function () {
226
- if (handler) {
227
- (response as any).request = options;
228
- const ret = handler.call(responseInterceptor, response, Promise);
229
- response = ret === undefined ? response : ret;
230
- }
231
- if (!isPromise(response)) {
232
- response = Promise[type === 0 ? 'resolve' : 'reject'](response);
233
- }
234
- (response as Promise<unknown>)
235
- .then((d: unknown) => {
236
- resolve((d as any).data as T);
237
- })
238
- .catch((e: unknown) => {
239
- reject(e);
240
- });
241
- });
242
- }
243
- (options as any).complete = (response: unknown) => {
244
- const statusCode: number = (response as any).statusCode;
245
- let type = 0;
246
- if ((statusCode >= 200 && statusCode < 300) || statusCode === 304) {
247
- type = 0;
248
- onresult(responseInterceptor.handler, response, type);
249
- } else {
250
- type = -1;
251
- onresult(responseInterceptor.onerror, response, type);
252
- }
253
- onresult(responseInterceptor.complete, response, type);
254
- };
255
- enqueueIfLocked(requestInterceptor.p, () => {
256
- options = Object.assign({}, this.config, options);
257
- (options as any).requestId = new Date().getTime();
258
- let ret: unknown = options;
259
- if (requestInterceptorHandler) {
260
- ret = requestInterceptorHandler.call(requestInterceptor, options, Promise) || options;
261
- }
262
- if (!isPromise(ret)) {
263
- ret = Promise.resolve(ret);
264
- }
265
- (ret as Promise<unknown>).then(
266
- (d: unknown) => {
267
- if (d === options) {
268
- // 这里断言为 any,兼容 baseURL、restURL、url、method 等属性
269
- const req = d as any;
270
- req.url = req.url && req.url.indexOf('http') !== 0 ? req.baseURL + req.url : req.url;
271
- req.url = req.restURL ? req.url + req.restURL : req.url;
272
- req.method = req.method.toUpperCase();
273
- if (req.method === 'UPLOAD') {
274
- uni.uploadFile(req);
275
- } else {
276
- uni.request(req);
277
- }
278
- } else {
279
- resolve(d as T);
280
- }
281
- },
282
- (err: unknown) => {
283
- reject(err);
284
- }
285
- );
286
- });
287
- });
288
- }
289
-
290
- /**
291
- * 并发请求
292
- */
293
- all<T = unknown>(promises: Promise<T>[]): Promise<T[]> {
294
- return Promise.all(promises);
295
- }
296
-
297
- /**
298
- * GET 请求
299
- */
300
- get<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
301
- return this.request<T>(merge({ url, data, method: 'GET' }, option));
302
- }
303
- /**
304
- * POST 请求
305
- */
306
- post<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
307
- return this.request<T>(merge({ url, data, method: 'POST' }, option));
308
- }
309
- /**
310
- * PUT 请求
311
- */
312
- put<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
313
- return this.request<T>(merge({ url, data, method: 'PUT' }, option));
314
- }
315
- /**
316
- * PATCH 请求
317
- */
318
- patch<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
319
- return this.request<T>(merge({ url, data, method: 'PATCH' }, option));
320
- }
321
- /**
322
- * HEAD 请求
323
- */
324
- head<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
325
- return this.request<T>(merge({ url, data, method: 'HEAD' }, option));
326
- }
327
- /**
328
- * DELETE 请求
329
- */
330
- delete<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
331
- return this.request<T>(merge({ url, data, method: 'DELETE' }, option));
332
- }
333
- /**
334
- * UPLOAD 文件上传
335
- */
336
- upload<T = unknown>(url: string, data?: unknown, option?: HttpRequestConfig): Promise<T> {
337
- return this.request<T>(merge({ url, data, method: 'UPLOAD' }, option));
338
- }
339
-
340
- /**
341
- * 拦截器锁定
342
- */
343
- lock(): void {
344
- const fn = this.interceptors.request.lock;
345
- if (typeof fn === 'function') {
346
- fn.call(this.interceptors.request);
347
- }
348
- }
349
- /**
350
- * 拦截器解锁
351
- */
352
- unlock(): void {
353
- const fn = this.interceptors.request.unlock;
354
- if (typeof fn === 'function') {
355
- fn.call(this.interceptors.request);
356
- }
357
- }
358
- /**
359
- * 拦截器清空
360
- */
361
- clear(): void {
362
- const fn = this.interceptors.request.clear;
363
- if (typeof fn === 'function') {
364
- fn.call(this.interceptors.request);
365
- }
366
- }
367
- }
368
-
369
- // 数据合并
370
- function merge(a: any, b: any): any {
371
- for (const key in b) {
372
- if (!a.hasOwnProperty(key)) {
373
- a[key] = b[key];
374
- } else if (isObject(b[key], 1) && isObject(a[key], 1)) {
375
- merge(a[key], b[key]);
376
- }
377
- }
378
- return a;
379
- }
380
-
381
- function isObject(ob: any, real?: number): boolean {
382
- if (real) {
383
- return Object.prototype.toString.call(ob).slice(8, -1).toLowerCase() === 'object';
384
- } else {
385
- return ob && typeof ob === 'object';
386
- }
387
- }
388
-
389
- function isArray(value: any): value is any[] {
390
- return Array.isArray(value);
391
- }
392
-
393
- function wrap(interceptor: Interceptor) {
394
- let resolve: ((value?: unknown) => void) | null;
395
- let reject: ((reason?: any) => void) | null;
396
- function _clear() {
397
- interceptor.p = resolve = reject = null;
398
- }
399
- merge(interceptor, {
400
- lock() {
401
- if (!resolve) {
402
- interceptor.p = new Promise((_resolve, _reject) => {
403
- resolve = _resolve;
404
- reject = _reject;
405
- });
406
- }
407
- },
408
- unlock() {
409
- if (resolve) {
410
- resolve();
411
- _clear();
412
- }
413
- },
414
- clear() {
415
- if (reject) {
416
- reject('cancel');
417
- _clear();
418
- }
419
- }
420
- });
421
- }
422
-
423
- /**
424
- * useHttp hooks 工厂函数,推荐唯一入口
425
- * @param options 可选的全局配置
426
- * @returns http 实例(包含 get/post/put/delete 等方法和拦截器)
427
- */
428
- export function useHttp(options: HttpRequestConfig = {}) {
429
- const http = new Http(options);
430
- return http;
431
- }
432
-
433
- // 只导出 useHttp,不再导出 Http 类实例化方式
434
- export default useHttp;