uniapp-request-sdk 1.4.4 → 1.4.6
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 +5 -1
- package/dist/index.esm.js +16 -8
- package/dist/index.umd.js +16 -8
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ interface IParams {
|
|
|
11
11
|
maxRetryCount?: number;
|
|
12
12
|
/** 请求超时时间默认10s,ms为单位 */
|
|
13
13
|
timeout?: number;
|
|
14
|
-
/** 上传文件的请求超时时间,ms
|
|
14
|
+
/** 上传文件的请求超时时间,ms为单位,默认5s */
|
|
15
15
|
uploadTimeout?: number;
|
|
16
16
|
/** 请求尝试延迟时间默认3s,ms为单位 */
|
|
17
17
|
retryDelay?: number;
|
|
@@ -25,6 +25,8 @@ interface IParams {
|
|
|
25
25
|
tokenPrefix?: string;
|
|
26
26
|
/** 自定义得到token的函数,默认使用从客户端获取token的方式 */
|
|
27
27
|
getTokenFun?: () => Promise<string>;
|
|
28
|
+
/** 用户名,用于日志排查使用 */
|
|
29
|
+
userName?: string;
|
|
28
30
|
}
|
|
29
31
|
declare function requestPromise(options?: RequestOptions & {
|
|
30
32
|
timeout: number;
|
|
@@ -32,6 +34,8 @@ declare function requestPromise(options?: RequestOptions & {
|
|
|
32
34
|
declare class UniRequest {
|
|
33
35
|
/** 基准路径 */
|
|
34
36
|
private baseUrl;
|
|
37
|
+
/** 用户名 */
|
|
38
|
+
private userName;
|
|
35
39
|
/** 错误处理函数 */
|
|
36
40
|
private onErrorHandler;
|
|
37
41
|
/** 默认请求头 */
|
package/dist/index.esm.js
CHANGED
|
@@ -37,6 +37,7 @@ function _toPropertyKey(arg) {
|
|
|
37
37
|
|
|
38
38
|
/** 客户端交互内置事件名称 */
|
|
39
39
|
var EVENT_NAME = {
|
|
40
|
+
/** 通知APP退出到登录页面 */
|
|
40
41
|
LOGOUT: 'logout'
|
|
41
42
|
};
|
|
42
43
|
function requestPromise(options) {
|
|
@@ -71,6 +72,8 @@ var UniRequest = /*#__PURE__*/function () {
|
|
|
71
72
|
_classCallCheck(this, UniRequest);
|
|
72
73
|
/** 基准路径 */
|
|
73
74
|
this.baseUrl = '';
|
|
75
|
+
/** 用户名 */
|
|
76
|
+
this.userName = '';
|
|
74
77
|
/** 错误处理函数 */
|
|
75
78
|
this.onErrorHandler = function (error) {
|
|
76
79
|
console.error(error);
|
|
@@ -118,6 +121,7 @@ var UniRequest = /*#__PURE__*/function () {
|
|
|
118
121
|
value: function request(params) {
|
|
119
122
|
var _this3 = this;
|
|
120
123
|
var callbackPromise = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : requestPromise;
|
|
124
|
+
var _a;
|
|
121
125
|
var url = params.url;
|
|
122
126
|
/** 如果是http开头的,则不需要加入baseUrl */
|
|
123
127
|
if (!/^https?:\/\//.test(url)) {
|
|
@@ -125,19 +129,23 @@ var UniRequest = /*#__PURE__*/function () {
|
|
|
125
129
|
} else {
|
|
126
130
|
url = "".concat(params.url, "?t=").concat(Date.now());
|
|
127
131
|
}
|
|
128
|
-
var
|
|
132
|
+
var appVersion = (_a = uni.getSystemInfoSync()) === null || _a === void 0 ? void 0 : _a.appVersion;
|
|
133
|
+
var header = Object.assign(Object.assign(Object.assign({}, this.header), params.header), {
|
|
134
|
+
/** 添加日志上报的小程序版本 */
|
|
135
|
+
'X-Request-AppVersion': appVersion,
|
|
136
|
+
/** 添加日志上报的小程序userName */
|
|
137
|
+
'X-Request-UserName': this.userName
|
|
138
|
+
});
|
|
129
139
|
var requestedToken = false;
|
|
130
140
|
var retryCount = 0;
|
|
131
141
|
return new Promise(function (res, rej) {
|
|
132
142
|
var retryFucntion = function retryFucntion() {
|
|
133
143
|
/** 因为token需要动态获取,因此放入这里 */
|
|
134
|
-
// #ifndef H5
|
|
135
144
|
header[_this3.tokenHeader] = "".concat(_this3.tokenPrefix).concat(_this3.token);
|
|
136
|
-
// #endif
|
|
137
145
|
/** 针对h5调试的时候,因为塞入cookie是不安全的,因此无需塞入cookie,使用登录页后,后端塞入的cookie */
|
|
138
146
|
// #ifdef H5
|
|
139
|
-
if (
|
|
140
|
-
header[_this3.tokenHeader]
|
|
147
|
+
if (_this3.tokenHeader === 'cookie') {
|
|
148
|
+
delete header[_this3.tokenHeader];
|
|
141
149
|
}
|
|
142
150
|
// #endif
|
|
143
151
|
callbackPromise(Object.assign(Object.assign({}, params), {
|
|
@@ -162,7 +170,7 @@ var UniRequest = /*#__PURE__*/function () {
|
|
|
162
170
|
/** 获取token应该立马请求,不需要延迟 */
|
|
163
171
|
retryFucntion();
|
|
164
172
|
})["catch"](function () {
|
|
165
|
-
if (_this3.maxRetryCount > retryCount) {
|
|
173
|
+
if (_this3.maxRetryCount > retryCount && !requestedToken) {
|
|
166
174
|
delayRetryFcuntion();
|
|
167
175
|
} else {
|
|
168
176
|
_this3.rejectHandler(rej, resData);
|
|
@@ -179,14 +187,14 @@ var UniRequest = /*#__PURE__*/function () {
|
|
|
179
187
|
console.log('用户无权限,调用logout事件,退出到登录页');
|
|
180
188
|
});
|
|
181
189
|
} else {
|
|
182
|
-
if (_this3.maxRetryCount > retryCount) {
|
|
190
|
+
if (_this3.maxRetryCount > retryCount && !requestedToken) {
|
|
183
191
|
delayRetryFcuntion();
|
|
184
192
|
} else {
|
|
185
193
|
_this3.rejectHandler(rej, resData);
|
|
186
194
|
}
|
|
187
195
|
}
|
|
188
196
|
})["catch"](function (rejData) {
|
|
189
|
-
if (_this3.maxRetryCount > retryCount) {
|
|
197
|
+
if (_this3.maxRetryCount > retryCount && !requestedToken) {
|
|
190
198
|
delayRetryFcuntion();
|
|
191
199
|
} else {
|
|
192
200
|
_this3.rejectHandler(rej, rejData);
|
package/dist/index.umd.js
CHANGED
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
|
|
44
44
|
/** 客户端交互内置事件名称 */
|
|
45
45
|
var EVENT_NAME = {
|
|
46
|
+
/** 通知APP退出到登录页面 */
|
|
46
47
|
LOGOUT: 'logout'
|
|
47
48
|
};
|
|
48
49
|
function requestPromise(options) {
|
|
@@ -77,6 +78,8 @@
|
|
|
77
78
|
_classCallCheck(this, UniRequest);
|
|
78
79
|
/** 基准路径 */
|
|
79
80
|
this.baseUrl = '';
|
|
81
|
+
/** 用户名 */
|
|
82
|
+
this.userName = '';
|
|
80
83
|
/** 错误处理函数 */
|
|
81
84
|
this.onErrorHandler = function (error) {
|
|
82
85
|
console.error(error);
|
|
@@ -124,6 +127,7 @@
|
|
|
124
127
|
value: function request(params) {
|
|
125
128
|
var _this3 = this;
|
|
126
129
|
var callbackPromise = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : requestPromise;
|
|
130
|
+
var _a;
|
|
127
131
|
var url = params.url;
|
|
128
132
|
/** 如果是http开头的,则不需要加入baseUrl */
|
|
129
133
|
if (!/^https?:\/\//.test(url)) {
|
|
@@ -131,19 +135,23 @@
|
|
|
131
135
|
} else {
|
|
132
136
|
url = "".concat(params.url, "?t=").concat(Date.now());
|
|
133
137
|
}
|
|
134
|
-
var
|
|
138
|
+
var appVersion = (_a = uni.getSystemInfoSync()) === null || _a === void 0 ? void 0 : _a.appVersion;
|
|
139
|
+
var header = Object.assign(Object.assign(Object.assign({}, this.header), params.header), {
|
|
140
|
+
/** 添加日志上报的小程序版本 */
|
|
141
|
+
'X-Request-AppVersion': appVersion,
|
|
142
|
+
/** 添加日志上报的小程序userName */
|
|
143
|
+
'X-Request-UserName': this.userName
|
|
144
|
+
});
|
|
135
145
|
var requestedToken = false;
|
|
136
146
|
var retryCount = 0;
|
|
137
147
|
return new Promise(function (res, rej) {
|
|
138
148
|
var retryFucntion = function retryFucntion() {
|
|
139
149
|
/** 因为token需要动态获取,因此放入这里 */
|
|
140
|
-
// #ifndef H5
|
|
141
150
|
header[_this3.tokenHeader] = "".concat(_this3.tokenPrefix).concat(_this3.token);
|
|
142
|
-
// #endif
|
|
143
151
|
/** 针对h5调试的时候,因为塞入cookie是不安全的,因此无需塞入cookie,使用登录页后,后端塞入的cookie */
|
|
144
152
|
// #ifdef H5
|
|
145
|
-
if (
|
|
146
|
-
header[_this3.tokenHeader]
|
|
153
|
+
if (_this3.tokenHeader === 'cookie') {
|
|
154
|
+
delete header[_this3.tokenHeader];
|
|
147
155
|
}
|
|
148
156
|
// #endif
|
|
149
157
|
callbackPromise(Object.assign(Object.assign({}, params), {
|
|
@@ -168,7 +176,7 @@
|
|
|
168
176
|
/** 获取token应该立马请求,不需要延迟 */
|
|
169
177
|
retryFucntion();
|
|
170
178
|
})["catch"](function () {
|
|
171
|
-
if (_this3.maxRetryCount > retryCount) {
|
|
179
|
+
if (_this3.maxRetryCount > retryCount && !requestedToken) {
|
|
172
180
|
delayRetryFcuntion();
|
|
173
181
|
} else {
|
|
174
182
|
_this3.rejectHandler(rej, resData);
|
|
@@ -185,14 +193,14 @@
|
|
|
185
193
|
console.log('用户无权限,调用logout事件,退出到登录页');
|
|
186
194
|
});
|
|
187
195
|
} else {
|
|
188
|
-
if (_this3.maxRetryCount > retryCount) {
|
|
196
|
+
if (_this3.maxRetryCount > retryCount && !requestedToken) {
|
|
189
197
|
delayRetryFcuntion();
|
|
190
198
|
} else {
|
|
191
199
|
_this3.rejectHandler(rej, resData);
|
|
192
200
|
}
|
|
193
201
|
}
|
|
194
202
|
})["catch"](function (rejData) {
|
|
195
|
-
if (_this3.maxRetryCount > retryCount) {
|
|
203
|
+
if (_this3.maxRetryCount > retryCount && !requestedToken) {
|
|
196
204
|
delayRetryFcuntion();
|
|
197
205
|
} else {
|
|
198
206
|
_this3.rejectHandler(rej, rejData);
|