Skip to content

Commit 9ea7994

Browse files
committed
feat: add doOriginalCall method for wx.request
1 parent b045bb7 commit 9ea7994

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/interceptor/fetch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ export default class FetchInterceptor extends Base{
134134
});
135135
}
136136

137+
/**
138+
* Get original response
139+
* @param {string} requestUrl
140+
* @param {FetchRequest | AnyObject} params
141+
*/
137142
private async getOriginalResponse(requestUrl: string, params: FetchRequest | AnyObject): Promise<OriginalResponse> {
138143
let status = null;
139144
const headers: Record<string, string | string[]> = {};

src/interceptor/wx-request.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Bypass from '../common/bypass';
33
import { isObject, sleep, tryToParseJson } from '../common/utils';
44
import MockItem from '../mocker/mock-item';
55
import Mocker from '../mocker/mocker';
6-
import { HttpVerb, RemoteResponse, RequestInfo, WxRequestOpts, WxRequestTask, WxResponse } from '../types';
6+
import { HttpVerb, OriginalResponse, RemoteResponse, RequestInfo, WxRequestOpts, WxRequestTask, WxResponse } from '../types';
77
import Base from './base';
88

99
export default class WxRequestInterceptor extends Base {
@@ -61,6 +61,12 @@ export default class WxRequestInterceptor extends Base {
6161
requestInfo.body = wxRequestOpts.data;
6262
}
6363

64+
requestInfo.doOriginalCall = async (): Promise<OriginalResponse> => {
65+
const res = this.getOriginalResponse(wxRequestOpts);
66+
requestInfo.doOriginalCall = undefined;
67+
return res;
68+
};
69+
6470
if (mockItem) {
6571
this.doMockRequest(mockItem, requestInfo, wxRequestOpts).then(isBypassed => {
6672
if (isBypassed) {
@@ -112,6 +118,45 @@ export default class WxRequestInterceptor extends Base {
112118
return this.getRequstTask();
113119
}
114120

121+
/**
122+
* Get original response
123+
* @param {WxRequestOpts} wxRequestOpts
124+
*/
125+
private getOriginalResponse(wxRequestOpts: WxRequestOpts): Promise<OriginalResponse> {
126+
return new Promise((resolve) => {
127+
this.wxRequest({
128+
...wxRequestOpts,
129+
success(wxResponse: WxResponse) {
130+
const { data } = wxResponse;
131+
resolve({
132+
status: wxResponse.statusCode,
133+
headers: wxResponse.header,
134+
responseText: typeof data === 'string' ? data : JSON.stringify(data),
135+
responseJson: typeof data === 'string' ? tryToParseJson(data) : data,
136+
responseBuffer: typeof ArrayBuffer === 'function' && (data instanceof ArrayBuffer)
137+
? (data as ArrayBuffer)
138+
: null,
139+
// https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
140+
// wx.request does not support Blob response data
141+
responseBlob: null,
142+
error: null,
143+
});
144+
},
145+
fail(err: { errMsg: string }) {
146+
resolve({
147+
status: 0,
148+
headers: {},
149+
responseText: null,
150+
responseJson: null,
151+
responseBuffer: null,
152+
responseBlob: null,
153+
error: new Error(`request error: ${err.errMsg}`),
154+
});
155+
}
156+
});
157+
});
158+
}
159+
115160
/**
116161
* Make mock request.
117162
* @param {MockItem} mockItem

src/interceptor/xml-http-request.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ export default class XMLHttpRequestInterceptor extends Base {
174174
return xhr;
175175
}
176176

177+
/**
178+
* Get original response
179+
* @param {XMLHttpRequestInstance} xhr
180+
*/
177181
private getOriginalResponse(xhr: XMLHttpRequestInstance): Promise<OriginalResponse> {
178182
const [ method, requestUrl, async, user, password ] = xhr.requestArgs;
179183
const { requestInfo } = xhr;

0 commit comments

Comments
 (0)