@tmsfe/tms-core 0.0.198 → 0.0.199

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/request.js +36 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.198",
3
+ "version": "0.0.199",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
package/src/request.js CHANGED
@@ -315,12 +315,15 @@ export default class Request {
315
315
  * 发送post方式的请求(返回 wx.request 的 requestTask,用于监听流式数据)
316
316
  * @memberof Request
317
317
  * @param {string} path 请求接口路径
318
- * @param {object} param 业务参数
319
- * @param {object} header 自定义请求头
318
+ * @param {object} params 参数
319
+ * @param {object} params.param 业务参数
320
+ * @param {string} params.method 请求方法
321
+ * @param {object} params.header 自定义请求头
322
+ * @param {object} params.callBacks 监听流式数据的回调函数
320
323
  * @returns {Promise} Promise<WechatMiniprogram.RequestTask>
321
324
  */
322
- execPostTask(path, param, header) {
323
- return this.createStreamRequestTask(path, param, 'POST', header);
325
+ execPostTask(path, params) {
326
+ return this.createStreamRequestTask(path, params);
324
327
  }
325
328
 
326
329
  /**
@@ -556,20 +559,28 @@ export default class Request {
556
559
  }
557
560
 
558
561
  /**
559
- * 创建发送请求任务(返回 requestTask,用于流式场景)
560
- * 不封装为 Promise,直接返回 wx.request 的 requestTask,便于调用方绑定 onChunkReceived
561
- * @memberof Request
562
- * @param {string} path 请求接口路径
563
- * @param {object} param 业务参数
564
- * @param {string} method 请求方法 get/post
565
- * @param {object} header 自定义的请求头
566
- * @returns {Promise} 返回 wx.request 的 requestTask
567
- */
568
- async createStreamRequestTask(path, param = {}, method = 'POST', header = {}) {
562
+ * 创建发送请求任务(返回 requestTask,用于流式场景)
563
+ * 支持流式输出,同时提供 success、fail 回调
564
+ * @memberof Request
565
+ * @param {string} path 请求接口路径
566
+ * @param {object} params 参数对象
567
+ * @param {object} params.param 业务参数
568
+ * @param {string} params.method 请求方法 get/post
569
+ * @param {object} params.header 自定义的请求头
570
+ * @param {object} params.callbacks 回调函数对象
571
+ * @param {function} data.callbacks.onSuccess 请求成功回调
572
+ * @param {function} data.callbacks.onFail 请求失败回调
573
+ * @param {function} data.callbacks.onComplete 请求完成回调
574
+ * @returns {Promise} 返回 wx.request 的 requestTask
575
+ */
576
+ async createStreamRequestTask(path, params = {}) {
577
+ const { param = {}, method = 'POST', header = {}, callbacks = {} } = params;
569
578
  const requestParam = await composeParam(param, this.withAuth, this.baseParam);
570
579
  const data = sign(requestParam, this.secretKey);
571
580
 
572
- // 返回原始 requestTask,供外部绑定 onChunkReceived
581
+ const { onSuccess, onFail, onComplete } = callbacks;
582
+
583
+ // 创建 requestTask,支持流式输出 提供onChunkReceived监听、abort中断请求方法
573
584
  const requestTask = wx.request({
574
585
  url: this.makeUrl(path),
575
586
  header,
@@ -578,11 +589,20 @@ export default class Request {
578
589
  enableHttp2: this.enableHttp2,
579
590
  enableChunked: this.enableChunked,
580
591
  responseType: this.responseType,
592
+ success: (res) => {
593
+ onSuccess?.(res);
594
+ },
595
+ fail: (err) => {
596
+ onFail?.(err);
597
+ },
598
+ complete: (res) => {
599
+ onComplete?.(res);
600
+ },
581
601
  });
602
+
582
603
  return requestTask;
583
604
  }
584
605
  }
585
-
586
606
  /**
587
607
  * 判断指定路径是否需要token失效重试
588
608
  * @param {string} path 请求路径