uniapp-request-sdk 1.6.1 → 1.7.1

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.
package/dist/index.d.ts CHANGED
@@ -15,6 +15,8 @@ interface IParams {
15
15
  timeout?: number;
16
16
  /** 上传文件的请求超时时间,ms为单位,默认5s */
17
17
  uploadTimeout?: number;
18
+ /** 下载文件的请求超时时间,ms为单位,默认30s */
19
+ downloadTimeout?: number;
18
20
  /** 请求尝试延迟时间默认3s,ms为单位 */
19
21
  retryDelay?: number;
20
22
  /** token,APP初始化给到小程序的token */
@@ -30,40 +32,102 @@ interface IParams {
30
32
  /** 用户名,用于日志排查使用 */
31
33
  username?: string;
32
34
  }
35
+ /**
36
+ * uni.uploadFile 的选项扩展类型,支持进度回调
37
+ * @description 在 UploadFileOption 基础上扩展了进度更新回调
38
+ */
33
39
  type UploadFileWithProgressOption = UploadFileOption & {
40
+ /** 上传进度更新回调函数 */
34
41
  onProgressUpdateCallback?: (result: OnProgressUpdateResult) => void;
35
42
  };
36
- declare function requestPromise(options?: RequestOptions & {
37
- timeout: number;
38
- }): Promise<RequestSuccessCallbackResult>;
43
+ /**
44
+ * uni.downloadFile 的选项扩展类型,支持进度回调
45
+ * @description 在 DownloadFileOption 基础上扩展了进度更新回调
46
+ */
47
+ type DownloadFileWithProgressOption = DownloadFileOption & {
48
+ /** 下载进度更新回调函数 */
49
+ onProgressUpdateCallback?: (result: any) => void;
50
+ };
51
+ /**
52
+ * UniRequest 网络请求管理类
53
+ * @description 统一封装 uni-app 的网络请求(request、uploadFile、downloadFile),
54
+ * 提供 Promise 风格的 API 和高级特性,如自动重试、token 管理、请求头预处理等
55
+ * @example
56
+ * const uniRequest = new UniRequest({
57
+ * baseUrl: 'https://api.example.com',
58
+ * timeout: 10000,
59
+ * uploadTimeout: 5000
60
+ * });
61
+ *
62
+ * // 普通请求
63
+ * const data = await uniRequest.post('/path', { key: 'value' });
64
+ *
65
+ * // 上传文件
66
+ * const result = await uniRequest.uploadFile('/upload', filePath, formData);
67
+ *
68
+ * // 下载文件
69
+ * const filePath = await uniRequest.downloadFile('/download/file.pdf');
70
+ */
39
71
  declare class UniRequest {
40
- /** 基准路径 */
72
+ /** 基准路径,用于构建完整的请求 URL */
41
73
  private baseUrl;
42
- /** 用户名 */
74
+ /** 用户名,用于日志记录和问题排查 */
43
75
  private username;
44
- /** 错误处理函数 */
76
+ /** 错误处理函数,当请求失败时会被调用 */
45
77
  private onErrorHandler;
46
- /** 默认请求头 */
78
+ /** 默认请求头,会被合并到所有请求中 */
47
79
  private header?;
48
- /** 请求头预处理函数 */
80
+ /** 请求头预处理函数,用于动态修改请求头(在 token 设置之后执行) */
49
81
  private headerProcessor?;
82
+ /** 请求失败时的最大重试次数,默认 3 次 */
50
83
  private maxRetryCount;
51
- /** 超时时间 */
84
+ /** 普通请求的超时时间,单位毫秒,默认 10 秒 */
52
85
  private timeout;
53
- /** 上传文件的超时时间 */
86
+ /** 上传文件的超时时间,单位毫秒,默认 5 秒 */
54
87
  private uploadTimeout;
88
+ /** 下载文件的超时时间,单位毫秒,默认 30 秒 */
89
+ private downloadTimeout;
90
+ /** 请求失败后的重试延迟时间,单位毫秒,默认 3 秒 */
55
91
  private retryDelay;
92
+ /** 当前的 token,用于授权请求 */
56
93
  private token?;
94
+ /** 获取 token 的事件名称,默认为 'getToken' */
57
95
  private tokenEventName;
58
- /** token的header名称,默认Authorization */
96
+ /** token 所在的请求头字段名称,默认为 'Authorization' */
59
97
  private tokenHeader;
60
- /** 传入的token前缀,默认Bearer ,注意Bearer有个空字符串*/
98
+ /** token 的前缀,默认为 'Bearer '(注意末尾有空格) */
61
99
  private tokenPrefix;
62
- /** 自定义得到token的函数 */
100
+ /** 自定义的获取 token 函数,若未设置则使用 getToken() */
63
101
  private getTokenFun?;
102
+ /**
103
+ * 构造函数,初始化 UniRequest 实例
104
+ * @param params - 初始化参数,详见 IParams 接口
105
+ */
64
106
  constructor(params?: IParams);
107
+ /**
108
+ * 内部错误处理函数
109
+ * @description 触发 reject 并调用全局错误处理函数
110
+ * @param rej - Promise 的 reject 函数
111
+ * @param data - 错误数据
112
+ */
65
113
  private rejectHandler;
114
+ /**
115
+ * 动态设置配置参数
116
+ * @description 可以在实例创建后动态修改配置,用于变更超时时间、token、请求头等
117
+ * @param params - 要设置的参数,详见 IParams 接口
118
+ * @example
119
+ * uniRequest.setParams({ token: 'new_token', timeout: 15000 });
120
+ */
66
121
  setParams(params?: IParams): void;
122
+ /**
123
+ * 底层通用请求方法
124
+ * @description 所有具体的请求方法(post、get 等)都调用此方法来执行请求。
125
+ * 支持自动重试、token 管理、请求头处理等高级功能
126
+ * @param params - 请求参数,包含 url、method、data、header、timeout 等
127
+ * @param callbackPromise - 回调函数,可以是 requestPromise、uploadFilePromise 或 downloadFilePromise
128
+ * @returns Promise<T> 返回解析后的响应数据
129
+ * @internal 此方法不建议直接调用,应使用 post、get、uploadFile 等高层方法
130
+ */
67
131
  request<T>(params: {
68
132
  url: string;
69
133
  method: RequestOptions['method'];
@@ -71,29 +135,91 @@ declare class UniRequest {
71
135
  header?: RequestOptions['header'];
72
136
  /** 超时时间 */
73
137
  timeout?: number;
74
- } & RequestOptions & UploadFileWithProgressOption, callbackPromise?: typeof requestPromise): Promise<T>;
138
+ } & RequestOptions & UploadFileWithProgressOption & DownloadFileWithProgressOption, callbackPromise?: any): Promise<T>;
75
139
  /**
76
- * post请求
77
- * @param url
78
- * @param data 可传可不传
79
- * @param header
80
- * @returns
140
+ * 发送 POST 请求
141
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
142
+ * @param data - 请求数据,可选;如不传递则默认为空对象
143
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
144
+ * @returns Promise<T> 返回解析后的响应数据
145
+ * @example
146
+ * const result = await uniRequest.post('/users', { name: 'John', age: 30 });
147
+ * const result = await uniRequest.post('https://api.example.com/users', data);
81
148
  */
82
149
  post<T>(url: string, data?: {}, header?: Record<string, string>): Promise<T>;
150
+ /**
151
+ * 发送 GET 请求
152
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
153
+ * @param data - 查询参数,可选;如不传递则默认为空对象
154
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
155
+ * @returns Promise<T> 返回解析后的响应数据
156
+ * @example
157
+ * const result = await uniRequest.get('/users', { page: 1, limit: 10 });
158
+ */
83
159
  get<T>(url: string, data?: {}, header?: Record<string, string>): Promise<T>;
160
+ /**
161
+ * 发送 DELETE 请求
162
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
163
+ * @param data - 请求数据,可选;如不传递则默认为空对象
164
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
165
+ * @returns Promise<T> 返回解析后的响应数据
166
+ * @example
167
+ * const result = await uniRequest.delete('/users/123');
168
+ */
84
169
  delete<T>(url: string, data?: {}, header?: Record<string, string>): Promise<T>;
170
+ /**
171
+ * 发送 PUT 请求
172
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
173
+ * @param data - 请求数据,可选;如不传递则默认为空对象
174
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
175
+ * @returns Promise<T> 返回解析后的响应数据
176
+ * @example
177
+ * const result = await uniRequest.put('/users/123', { name: 'Jane', age: 25 });
178
+ */
85
179
  put<T>(url: string, data?: {}, header?: Record<string, string>): Promise<T>;
86
180
  /**
87
181
  * 文件上传
88
- * @param url 上传接口地址;支持相对路径和完整的 http/https 地址
89
- * @param filePath 待上传文件的本地临时路径
90
- * @param formData 额外的表单字段,会与文件一起作为 multipart/form-data 提交
91
- * @param name 文件字段名;服务端通过该字段读取文件内容,默认值为 file
92
- * @param header 当前上传请求的自定义请求头;会与实例级公共请求头合并
93
- * @param onProgressUpdateCallback 上传进度回调;传入后会绑定到 uni.uploadFile 返回的 UploadTask.onProgressUpdate
94
- * @returns 返回一个 Promise;成功时解析服务端响应中的 data 字段,失败时抛出错误信息
182
+ * @param url - 上传接口地址;支持相对路径和完整的 http/https 地址
183
+ * @param filePath - 待上传文件的本地临时路径
184
+ * @param formData - 额外的表单字段,会与文件一起作为 multipart/form-data 提交
185
+ * @param name - 文件字段名;服务端通过该字段读取文件内容,默认值为 file
186
+ * @param header - 当前上传请求的自定义请求头;会与实例级公共请求头合并
187
+ * @param onProgressUpdateCallback - 上传进度回调;传入后会绑定到 uni.uploadFile 返回的 UploadTask.onProgressUpdate
188
+ * @returns Promise<T> 返回一个 Promise;成功时解析服务端响应中的 data 字段,失败时抛出错误信息
189
+ * @example
190
+ * const result = await uniRequest.uploadFile('/upload', filePath);
191
+ *
192
+ * // 带上传进度
193
+ * const result = await uniRequest.uploadFile(
194
+ * '/upload',
195
+ * filePath,
196
+ * { category: 'profile' },
197
+ * 'file',
198
+ * { 'Authorization': 'Bearer token' },
199
+ * (progress) => console.log(`上传进度: ${progress.progress}%`)
200
+ * );
95
201
  */
96
202
  uploadFile<T>(url: string, filePath: string, formData?: Record<string, any>, name?: string, header?: Record<string, string>, onProgressUpdateCallback?: (result: OnProgressUpdateResult) => void): Promise<T>;
203
+ /**
204
+ * 文件下载
205
+ * @param url - 下载资源的 url;支持相对路径和完整的 http/https 地址
206
+ * @param filePath - 指定文件下载后存储的路径(本地路径),仅小程序端支持;如不指定则保存到临时目录
207
+ * @param header - 当前下载请求的自定义请求头;会与实例级公共请求头合并
208
+ * @param onProgressUpdateCallback - 下载进度回调;传入后会绑定到 uni.downloadFile 返回的 DownloadTask.onProgressUpdate
209
+ * @returns Promise<T> 返回一个 Promise;成功时解析得到的文件临时路径(tempFilePath),失败时抛出错误信息
210
+ * @example
211
+ * // 简单下载
212
+ * const filePath = await uniRequest.downloadFile('/files/document.pdf');
213
+ *
214
+ * // 带自定义保存路径和进度回调
215
+ * const filePath = await uniRequest.downloadFile(
216
+ * '/files/large-file.zip',
217
+ * 'local/storage/path',
218
+ * { 'Authorization': 'Bearer token' },
219
+ * (progress) => console.log(`下载进度: ${progress.progress}%`)
220
+ * );
221
+ */
222
+ downloadFile<T extends string = string>(url: string, filePath?: string, header?: Record<string, string>, onProgressUpdateCallback?: (result: any) => void): Promise<T>;
97
223
  }
98
224
 
99
225
  export { IParams, UniRequest as default };
package/dist/index.esm.js CHANGED
@@ -398,6 +398,12 @@ var EVENT_NAME = {
398
398
  /** 通知APP退出到登录页面 */
399
399
  LOGOUT: 'logout'
400
400
  };
401
+ /**
402
+ * uni.request 的 Promise 包装函数
403
+ * @description 将回调式的 uni.request 转换为 Promise 风格,便于与 async/await 配合使用
404
+ * @param options - uni.request 的选项参数,必须包含 timeout 字段
405
+ * @returns Promise<RequestSuccessCallbackResult> 请求成功的响应数据
406
+ */
401
407
  function requestPromise(options) {
402
408
  return new Promise(function (res, rej) {
403
409
  uni.request(Object.assign(Object.assign({}, options), {
@@ -406,6 +412,12 @@ function requestPromise(options) {
406
412
  }));
407
413
  });
408
414
  }
415
+ /**
416
+ * uni.uploadFile 的 Promise 包装函数
417
+ * @description 将回调式的 uni.uploadFile 转换为 Promise 风格,并自动处理进度回调的绑定
418
+ * @param options - uni.uploadFile 的选项参数,支持 onProgressUpdateCallback 扩展字段
419
+ * @returns Promise<UploadFileSuccessCallbackResult> 上传成功的响应数据
420
+ */
409
421
  function uploadFilePromise(options) {
410
422
  return new Promise(function (res, rej) {
411
423
  var _a = options || {},
@@ -420,7 +432,32 @@ function uploadFilePromise(options) {
420
432
  }
421
433
  });
422
434
  }
423
- /** 得到小程序的token */
435
+ /**
436
+ * uni.downloadFile 的 Promise 包装函数
437
+ * @description 将回调式的 uni.downloadFile 转换为 Promise 风格,并自动处理进度回调的绑定
438
+ * @param options - uni.downloadFile 的选项参数,支持 onProgressUpdateCallback 扩展字段
439
+ * @returns Promise<any> 下载成功的响应数据(包含 tempFilePath 等)
440
+ */
441
+ function downloadFilePromise(options) {
442
+ return new Promise(function (res, rej) {
443
+ var _a = options || {},
444
+ onProgressUpdateCallback = _a.onProgressUpdateCallback,
445
+ downloadOptions = __rest(_a, ["onProgressUpdateCallback"]);
446
+ var downloadTask = uni.downloadFile(Object.assign(Object.assign({}, downloadOptions), {
447
+ success: res,
448
+ fail: rej
449
+ }));
450
+ if (onProgressUpdateCallback) {
451
+ downloadTask.onProgressUpdate(onProgressUpdateCallback);
452
+ }
453
+ });
454
+ }
455
+ /**
456
+ * 从客户端获取 token
457
+ * @description 通过 uni.sendNativeEvent 向原生应用发送事件请求 token,超时时间为 5 秒
458
+ * @param eventName - 事件名称,用于向原生应用指示要获取 token
459
+ * @returns Promise<string> 返回获取到的 token 字符串
460
+ */
424
461
  function getToken(eventName) {
425
462
  return new Promise(function (res, rej) {
426
463
  var timeout = setTimeout(rej, 5000);
@@ -430,28 +467,57 @@ function getToken(eventName) {
430
467
  });
431
468
  });
432
469
  }
470
+ /**
471
+ * UniRequest 网络请求管理类
472
+ * @description 统一封装 uni-app 的网络请求(request、uploadFile、downloadFile),
473
+ * 提供 Promise 风格的 API 和高级特性,如自动重试、token 管理、请求头预处理等
474
+ * @example
475
+ * const uniRequest = new UniRequest({
476
+ * baseUrl: 'https://api.example.com',
477
+ * timeout: 10000,
478
+ * uploadTimeout: 5000
479
+ * });
480
+ *
481
+ * // 普通请求
482
+ * const data = await uniRequest.post('/path', { key: 'value' });
483
+ *
484
+ * // 上传文件
485
+ * const result = await uniRequest.uploadFile('/upload', filePath, formData);
486
+ *
487
+ * // 下载文件
488
+ * const filePath = await uniRequest.downloadFile('/download/file.pdf');
489
+ */
433
490
  var UniRequest = /*#__PURE__*/function () {
491
+ /**
492
+ * 构造函数,初始化 UniRequest 实例
493
+ * @param params - 初始化参数,详见 IParams 接口
494
+ */
434
495
  function UniRequest(params) {
435
496
  var _this = this;
436
497
  _classCallCheck(this, UniRequest);
437
- /** 基准路径 */
498
+ /** 基准路径,用于构建完整的请求 URL */
438
499
  this.baseUrl = '';
439
- /** 用户名 */
500
+ /** 用户名,用于日志记录和问题排查 */
440
501
  this.username = '';
441
- /** 错误处理函数 */
502
+ /** 错误处理函数,当请求失败时会被调用 */
442
503
  this.onErrorHandler = function (error) {
443
504
  console.error(error);
444
505
  };
506
+ /** 请求失败时的最大重试次数,默认 3 次 */
445
507
  this.maxRetryCount = 3;
446
- /** 超时时间 */
508
+ /** 普通请求的超时时间,单位毫秒,默认 10 秒 */
447
509
  this.timeout = 10000;
448
- /** 上传文件的超时时间 */
510
+ /** 上传文件的超时时间,单位毫秒,默认 5 秒 */
449
511
  this.uploadTimeout = 5000;
512
+ /** 下载文件的超时时间,单位毫秒,默认 30 秒 */
513
+ this.downloadTimeout = 30000;
514
+ /** 请求失败后的重试延迟时间,单位毫秒,默认 3 秒 */
450
515
  this.retryDelay = 3000;
516
+ /** 获取 token 的事件名称,默认为 'getToken' */
451
517
  this.tokenEventName = 'getToken';
452
- /** token的header名称,默认Authorization */
518
+ /** token 所在的请求头字段名称,默认为 'Authorization' */
453
519
  this.tokenHeader = 'Authorization';
454
- /** 传入的token前缀,默认Bearer ,注意Bearer有个空字符串*/
520
+ /** token 的前缀,默认为 'Bearer '(注意末尾有空格) */
455
521
  this.tokenPrefix = 'Bearer ';
456
522
  if (params) {
457
523
  Object.keys(params).forEach(function (key) {
@@ -461,6 +527,12 @@ var UniRequest = /*#__PURE__*/function () {
461
527
  });
462
528
  }
463
529
  }
530
+ /**
531
+ * 内部错误处理函数
532
+ * @description 触发 reject 并调用全局错误处理函数
533
+ * @param rej - Promise 的 reject 函数
534
+ * @param data - 错误数据
535
+ */
464
536
  _createClass(UniRequest, [{
465
537
  key: "rejectHandler",
466
538
  value: function rejectHandler(rej, data) {
@@ -468,6 +540,13 @@ var UniRequest = /*#__PURE__*/function () {
468
540
  rej(data);
469
541
  (_a = this.onErrorHandler) === null || _a === void 0 ? void 0 : _a.call(this, data);
470
542
  }
543
+ /**
544
+ * 动态设置配置参数
545
+ * @description 可以在实例创建后动态修改配置,用于变更超时时间、token、请求头等
546
+ * @param params - 要设置的参数,详见 IParams 接口
547
+ * @example
548
+ * uniRequest.setParams({ token: 'new_token', timeout: 15000 });
549
+ */
471
550
  }, {
472
551
  key: "setParams",
473
552
  value: function setParams(params) {
@@ -480,6 +559,15 @@ var UniRequest = /*#__PURE__*/function () {
480
559
  });
481
560
  }
482
561
  }
562
+ /**
563
+ * 底层通用请求方法
564
+ * @description 所有具体的请求方法(post、get 等)都调用此方法来执行请求。
565
+ * 支持自动重试、token 管理、请求头处理等高级功能
566
+ * @param params - 请求参数,包含 url、method、data、header、timeout 等
567
+ * @param callbackPromise - 回调函数,可以是 requestPromise、uploadFilePromise 或 downloadFilePromise
568
+ * @returns Promise<T> 返回解析后的响应数据
569
+ * @internal 此方法不建议直接调用,应使用 post、get、uploadFile 等高层方法
570
+ */
483
571
  }, {
484
572
  key: "request",
485
573
  value: function request(params) {
@@ -500,43 +588,49 @@ var UniRequest = /*#__PURE__*/function () {
500
588
  /** 添加日志上报的小程序username */
501
589
  'X-Request-Username': this.username
502
590
  });
591
+ var finalHeader;
503
592
  var requestedToken = false;
504
593
  var retryCount = 0;
505
594
  return new Promise(function (res, rej) {
506
595
  var retryFucntion = /*#__PURE__*/function () {
507
596
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
508
- var finalHeader, processedHeader;
597
+ var requestHeader, processedHeader;
509
598
  return _regeneratorRuntime().wrap(function _callee$(_context) {
510
599
  while (1) switch (_context.prev = _context.next) {
511
600
  case 0:
601
+ if (finalHeader) {
602
+ _context.next = 17;
603
+ break;
604
+ }
605
+ requestHeader = Object.assign({}, header);
512
606
  /** 因为token需要动态获取,因此放入这里 */
513
- header[_this3.tokenHeader] = "".concat(_this3.tokenPrefix).concat(_this3.token);
607
+ requestHeader[_this3.tokenHeader] = "".concat(_this3.tokenPrefix).concat(_this3.token);
514
608
  /** 针对h5调试的时候,因为塞入cookie是不安全的,因此无需塞入cookie,使用登录页后,后端塞入的cookie */
515
609
  // #ifdef H5
516
610
  if (_this3.tokenHeader === 'cookie') {
517
- delete header[_this3.tokenHeader];
611
+ delete requestHeader[_this3.tokenHeader];
518
612
  }
519
613
  // #endif
520
- /** 执行请求头预处理 */
521
- finalHeader = header;
614
+ /** 单次请求生命周期内只执行一次请求头预处理,重试复用同一份结果 */
615
+ finalHeader = requestHeader;
522
616
  if (!_this3.headerProcessor) {
523
- _context.next = 15;
617
+ _context.next = 17;
524
618
  break;
525
619
  }
526
- _context.prev = 4;
527
- _context.next = 7;
528
- return _this3.headerProcessor(header);
529
- case 7:
620
+ _context.prev = 6;
621
+ _context.next = 9;
622
+ return _this3.headerProcessor(requestHeader);
623
+ case 9:
530
624
  processedHeader = _context.sent;
531
- finalHeader = Object.assign(Object.assign({}, header), processedHeader);
532
- _context.next = 15;
625
+ finalHeader = Object.assign(Object.assign({}, requestHeader), processedHeader);
626
+ _context.next = 17;
533
627
  break;
534
- case 11:
535
- _context.prev = 11;
536
- _context.t0 = _context["catch"](4);
628
+ case 13:
629
+ _context.prev = 13;
630
+ _context.t0 = _context["catch"](6);
537
631
  _this3.rejectHandler(rej, _context.t0);
538
632
  return _context.abrupt("return");
539
- case 15:
633
+ case 17:
540
634
  callbackPromise(Object.assign(Object.assign({}, params), {
541
635
  header: finalHeader,
542
636
  url: url,
@@ -545,7 +639,10 @@ var UniRequest = /*#__PURE__*/function () {
545
639
  var statusCode = resData.statusCode,
546
640
  data = resData.data;
547
641
  if (statusCode === 200) {
548
- if (!data.errno) {
642
+ /** downloadFile 特殊处理:直接返回 tempFilePath */
643
+ if (callbackPromise === downloadFilePromise) {
644
+ res(resData.tempFilePath);
645
+ } else if (!data.errno) {
549
646
  var jsonData = data;
550
647
  /** 如果是上传API的话,返回的结果需要解析成json对象 */
551
648
  if (Object.prototype.toString.call(data) !== '[object Object]' && callbackPromise === uploadFilePromise) {
@@ -566,6 +663,7 @@ var UniRequest = /*#__PURE__*/function () {
566
663
  (_this3.getTokenFun ? _this3.getTokenFun() : getToken(_this3.tokenEventName)).then(function (token) {
567
664
  _this3.token = token;
568
665
  requestedToken = true;
666
+ finalHeader = void 0;
569
667
  /** 获取token应该立马请求,不需要延迟 */
570
668
  retryFucntion();
571
669
  })["catch"](function () {
@@ -599,11 +697,11 @@ var UniRequest = /*#__PURE__*/function () {
599
697
  _this3.rejectHandler(rej, rejData);
600
698
  }
601
699
  });
602
- case 16:
700
+ case 18:
603
701
  case "end":
604
702
  return _context.stop();
605
703
  }
606
- }, _callee, null, [[4, 11]]);
704
+ }, _callee, null, [[6, 13]]);
607
705
  }));
608
706
  return function retryFucntion() {
609
707
  return _ref.apply(this, arguments);
@@ -619,11 +717,14 @@ var UniRequest = /*#__PURE__*/function () {
619
717
  });
620
718
  }
621
719
  /**
622
- * post请求
623
- * @param url
624
- * @param data 可传可不传
625
- * @param header
626
- * @returns
720
+ * 发送 POST 请求
721
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
722
+ * @param data - 请求数据,可选;如不传递则默认为空对象
723
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
724
+ * @returns Promise<T> 返回解析后的响应数据
725
+ * @example
726
+ * const result = await uniRequest.post('/users', { name: 'John', age: 30 });
727
+ * const result = await uniRequest.post('https://api.example.com/users', data);
627
728
  */
628
729
  }, {
629
730
  key: "post",
@@ -637,6 +738,15 @@ var UniRequest = /*#__PURE__*/function () {
637
738
  method: 'POST'
638
739
  });
639
740
  }
741
+ /**
742
+ * 发送 GET 请求
743
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
744
+ * @param data - 查询参数,可选;如不传递则默认为空对象
745
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
746
+ * @returns Promise<T> 返回解析后的响应数据
747
+ * @example
748
+ * const result = await uniRequest.get('/users', { page: 1, limit: 10 });
749
+ */
640
750
  }, {
641
751
  key: "get",
642
752
  value: function get(url) {
@@ -649,6 +759,15 @@ var UniRequest = /*#__PURE__*/function () {
649
759
  method: 'GET'
650
760
  });
651
761
  }
762
+ /**
763
+ * 发送 DELETE 请求
764
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
765
+ * @param data - 请求数据,可选;如不传递则默认为空对象
766
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
767
+ * @returns Promise<T> 返回解析后的响应数据
768
+ * @example
769
+ * const result = await uniRequest.delete('/users/123');
770
+ */
652
771
  }, {
653
772
  key: "delete",
654
773
  value: function _delete(url) {
@@ -661,6 +780,15 @@ var UniRequest = /*#__PURE__*/function () {
661
780
  method: 'DELETE'
662
781
  });
663
782
  }
783
+ /**
784
+ * 发送 PUT 请求
785
+ * @param url - 请求地址;支持相对路径(会拼接 baseUrl)和完整的 http/https 地址
786
+ * @param data - 请求数据,可选;如不传递则默认为空对象
787
+ * @param header - 当前请求的自定义请求头;会与实例级公共请求头合并
788
+ * @returns Promise<T> 返回解析后的响应数据
789
+ * @example
790
+ * const result = await uniRequest.put('/users/123', { name: 'Jane', age: 25 });
791
+ */
664
792
  }, {
665
793
  key: "put",
666
794
  value: function put(url) {
@@ -675,13 +803,25 @@ var UniRequest = /*#__PURE__*/function () {
675
803
  }
676
804
  /**
677
805
  * 文件上传
678
- * @param url 上传接口地址;支持相对路径和完整的 http/https 地址
679
- * @param filePath 待上传文件的本地临时路径
680
- * @param formData 额外的表单字段,会与文件一起作为 multipart/form-data 提交
681
- * @param name 文件字段名;服务端通过该字段读取文件内容,默认值为 file
682
- * @param header 当前上传请求的自定义请求头;会与实例级公共请求头合并
683
- * @param onProgressUpdateCallback 上传进度回调;传入后会绑定到 uni.uploadFile 返回的 UploadTask.onProgressUpdate
684
- * @returns 返回一个 Promise;成功时解析服务端响应中的 data 字段,失败时抛出错误信息
806
+ * @param url - 上传接口地址;支持相对路径和完整的 http/https 地址
807
+ * @param filePath - 待上传文件的本地临时路径
808
+ * @param formData - 额外的表单字段,会与文件一起作为 multipart/form-data 提交
809
+ * @param name - 文件字段名;服务端通过该字段读取文件内容,默认值为 file
810
+ * @param header - 当前上传请求的自定义请求头;会与实例级公共请求头合并
811
+ * @param onProgressUpdateCallback - 上传进度回调;传入后会绑定到 uni.uploadFile 返回的 UploadTask.onProgressUpdate
812
+ * @returns Promise<T> 返回一个 Promise;成功时解析服务端响应中的 data 字段,失败时抛出错误信息
813
+ * @example
814
+ * const result = await uniRequest.uploadFile('/upload', filePath);
815
+ *
816
+ * // 带上传进度
817
+ * const result = await uniRequest.uploadFile(
818
+ * '/upload',
819
+ * filePath,
820
+ * { category: 'profile' },
821
+ * 'file',
822
+ * { 'Authorization': 'Bearer token' },
823
+ * (progress) => console.log(`上传进度: ${progress.progress}%`)
824
+ * );
685
825
  */
686
826
  }, {
687
827
  key: "uploadFile",
@@ -700,6 +840,37 @@ var UniRequest = /*#__PURE__*/function () {
700
840
  onProgressUpdateCallback: onProgressUpdateCallback
701
841
  }, uploadFilePromise);
702
842
  }
843
+ /**
844
+ * 文件下载
845
+ * @param url - 下载资源的 url;支持相对路径和完整的 http/https 地址
846
+ * @param filePath - 指定文件下载后存储的路径(本地路径),仅小程序端支持;如不指定则保存到临时目录
847
+ * @param header - 当前下载请求的自定义请求头;会与实例级公共请求头合并
848
+ * @param onProgressUpdateCallback - 下载进度回调;传入后会绑定到 uni.downloadFile 返回的 DownloadTask.onProgressUpdate
849
+ * @returns Promise<T> 返回一个 Promise;成功时解析得到的文件临时路径(tempFilePath),失败时抛出错误信息
850
+ * @example
851
+ * // 简单下载
852
+ * const filePath = await uniRequest.downloadFile('/files/document.pdf');
853
+ *
854
+ * // 带自定义保存路径和进度回调
855
+ * const filePath = await uniRequest.downloadFile(
856
+ * '/files/large-file.zip',
857
+ * 'local/storage/path',
858
+ * { 'Authorization': 'Bearer token' },
859
+ * (progress) => console.log(`下载进度: ${progress.progress}%`)
860
+ * );
861
+ */
862
+ }, {
863
+ key: "downloadFile",
864
+ value: function downloadFile(url, filePath, header, onProgressUpdateCallback) {
865
+ return this.request({
866
+ url: url,
867
+ filePath: filePath,
868
+ header: header,
869
+ method: 'GET',
870
+ timeout: this.downloadTimeout,
871
+ onProgressUpdateCallback: onProgressUpdateCallback
872
+ }, downloadFilePromise);
873
+ }
703
874
  }]);
704
875
  return UniRequest;
705
876
  }();