@tmsfe/tms-core 0.0.197 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.197",
3
+ "version": "0.0.199",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -61,7 +61,8 @@ function proxyWxRequest(): void {
61
61
  // 如果用户自定义了dataType或者responseType,则不做处理
62
62
  if (dataType || responseType) {
63
63
  util.reportFunc(url, traceparent, '用户自定义了dataType和responseType');
64
- originalRequestApi.call(this, {
64
+ // 出行业务请求流式接口时要求必须传responseType,所以这里返回 wx.request 的 requestTask,用于监听流式数据
65
+ return originalRequestApi.call(this, {
65
66
  ...originalOptions,
66
67
  success: (res) => {
67
68
  encryptUtil.dealEncryptionSwitch(url, traceId, res.header).then((msg) => {
@@ -71,7 +72,6 @@ function proxyWxRequest(): void {
71
72
  },
72
73
  header: { ...header, Traceparent: traceparent },
73
74
  });
74
- return;
75
75
  }
76
76
  // 加密请求数据
77
77
  const { data: formatData, header: formatHeader, cryptoKeyInfo } = util
package/src/request.js CHANGED
@@ -191,6 +191,9 @@ export default class Request {
191
191
  secretKey = '';
192
192
  withAuth = true;
193
193
  baseParam = {};
194
+ enableChunked = false;
195
+ responseType = '';
196
+ enableHttp2 = false;
194
197
 
195
198
  /**
196
199
  * Request 构造函数
@@ -208,6 +211,15 @@ export default class Request {
208
211
  this.withAuth = !!config.withAuth;
209
212
  }
210
213
  this.baseParam = config.baseParam || {};
214
+ if (config.enableChunked) {
215
+ this.enableChunked = !!config.enableChunked;
216
+ }
217
+ if (config.responseType) {
218
+ this.responseType = config.responseType;
219
+ }
220
+ if (config.enableHttp2) {
221
+ this.enableHttp2 = !!config.enableHttp2;
222
+ }
211
223
  }
212
224
 
213
225
  /**
@@ -299,6 +311,21 @@ export default class Request {
299
311
  return this.createRequestTask(path, param, 'POST', header);
300
312
  }
301
313
 
314
+ /**
315
+ * 发送post方式的请求(返回 wx.request 的 requestTask,用于监听流式数据)
316
+ * @memberof Request
317
+ * @param {string} path 请求接口路径
318
+ * @param {object} params 参数
319
+ * @param {object} params.param 业务参数
320
+ * @param {string} params.method 请求方法
321
+ * @param {object} params.header 自定义请求头
322
+ * @param {object} params.callBacks 监听流式数据的回调函数
323
+ * @returns {Promise} Promise<WechatMiniprogram.RequestTask>
324
+ */
325
+ execPostTask(path, params) {
326
+ return this.createStreamRequestTask(path, params);
327
+ }
328
+
302
329
  /**
303
330
  * @memberof Request
304
331
  * @param {String} path 请求接口路径
@@ -530,8 +557,52 @@ export default class Request {
530
557
  });
531
558
  });
532
559
  }
533
- }
534
560
 
561
+ /**
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;
578
+ const requestParam = await composeParam(param, this.withAuth, this.baseParam);
579
+ const data = sign(requestParam, this.secretKey);
580
+
581
+ const { onSuccess, onFail, onComplete } = callbacks;
582
+
583
+ // 创建 requestTask,支持流式输出 提供onChunkReceived监听、abort中断请求方法
584
+ const requestTask = wx.request({
585
+ url: this.makeUrl(path),
586
+ header,
587
+ method,
588
+ data,
589
+ enableHttp2: this.enableHttp2,
590
+ enableChunked: this.enableChunked,
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
+ },
601
+ });
602
+
603
+ return requestTask;
604
+ }
605
+ }
535
606
  /**
536
607
  * 判断指定路径是否需要token失效重试
537
608
  * @param {string} path 请求路径