@noah-libjs/request 0.0.23 → 0.0.43
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/README.md +1 -1
- package/dist/Request.d.ts +44 -0
- package/dist/constant.d.ts +17 -0
- package/dist/helper.d.ts +9 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +375 -0
- package/dist/types.d.ts +26 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
+
import { IRequest_ResponseError, IRequest_SpawnConfig, TRequest_PreReq, TRequest_SimpleReq } from './types';
|
|
3
|
+
import { EventEmitter, MyLog } from '@noah-libjs/utils';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export declare class Request extends EventEmitter<{
|
|
6
|
+
request: any;
|
|
7
|
+
response: any;
|
|
8
|
+
error: any;
|
|
9
|
+
message: [boolean, string, any];
|
|
10
|
+
}> {
|
|
11
|
+
ins: AxiosInstance;
|
|
12
|
+
token?: string;
|
|
13
|
+
constructor(config?: IRequest_SpawnConfig);
|
|
14
|
+
static CONFIG: {
|
|
15
|
+
successCode: number[];
|
|
16
|
+
};
|
|
17
|
+
static logger: MyLog;
|
|
18
|
+
static checkRTCode(res?: AxiosResponse<any>): boolean;
|
|
19
|
+
static genErrMsg(res?: AxiosResponse<any>, error?: AxiosError): string;
|
|
20
|
+
static createErr(res?: AxiosResponse<any>, error?: AxiosError): IRequest_ResponseError;
|
|
21
|
+
static getMsg(res?: AxiosResponse<{
|
|
22
|
+
data?: any;
|
|
23
|
+
code?: number;
|
|
24
|
+
msg?: string;
|
|
25
|
+
message?: string;
|
|
26
|
+
title?: string;
|
|
27
|
+
}>): any;
|
|
28
|
+
static getRemoteMessage(res?: AxiosResponse<any>): any;
|
|
29
|
+
static getHeaderMessage(res?: AxiosResponse<any>): string | undefined;
|
|
30
|
+
displayMsg(res?: AxiosResponse<{
|
|
31
|
+
data?: any;
|
|
32
|
+
code?: number;
|
|
33
|
+
msg?: string;
|
|
34
|
+
}>): void;
|
|
35
|
+
config: IRequest_SpawnConfig;
|
|
36
|
+
spawn(spawnConfig?: IRequest_SpawnConfig): AxiosInstance;
|
|
37
|
+
gen(spawnConfig?: IRequest_SpawnConfig): AxiosInstance;
|
|
38
|
+
get: TRequest_SimpleReq;
|
|
39
|
+
delete: TRequest_SimpleReq;
|
|
40
|
+
head: TRequest_SimpleReq;
|
|
41
|
+
post: TRequest_PreReq;
|
|
42
|
+
put: TRequest_PreReq;
|
|
43
|
+
}
|
|
44
|
+
export declare const request: Request;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const codeMessage: {
|
|
2
|
+
200: string;
|
|
3
|
+
201: string;
|
|
4
|
+
202: string;
|
|
5
|
+
204: string;
|
|
6
|
+
400: string;
|
|
7
|
+
401: string;
|
|
8
|
+
403: string;
|
|
9
|
+
404: string;
|
|
10
|
+
406: string;
|
|
11
|
+
410: string;
|
|
12
|
+
422: string;
|
|
13
|
+
500: string;
|
|
14
|
+
502: string;
|
|
15
|
+
503: string;
|
|
16
|
+
504: string;
|
|
17
|
+
};
|
package/dist/helper.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ICommonOption } from "@noah-libjs/utils";
|
|
2
|
+
import { FormInstance } from "antd";
|
|
3
|
+
export type T_FETCH_OPTIONS = ((f?: FormInstance) => (Promise<ICommonOption[]> | ICommonOption[])) | {
|
|
4
|
+
url: string | (() => string);
|
|
5
|
+
labelKey?: string;
|
|
6
|
+
valueKey?: string;
|
|
7
|
+
method?: 'get' | 'post';
|
|
8
|
+
};
|
|
9
|
+
export declare function safe_fetch_options(cb: T_FETCH_OPTIONS, f?: FormInstance): Promise<ICommonOption[]>;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
import { Request, request } from './Request';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export * from './helper';
|
|
5
|
+
export { codeMessage } from './constant';
|
|
6
|
+
declare function createInstance(defaultConfig?: {}): AxiosInstance;
|
|
7
|
+
export { Request, createInstance, request };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
4
|
+
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
|
5
|
+
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
|
6
|
+
import _inherits from "@babel/runtime/helpers/inherits";
|
|
7
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
8
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
9
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
10
|
+
import axios, { Axios } from 'axios';
|
|
11
|
+
import utils from 'axios/lib/utils';
|
|
12
|
+
import { MyLog, EventEmitter, getSearchParamsAll, isFunction, safe_async_call, expect_array, isString, cache_fetch, get } from '@noah-libjs/utils';
|
|
13
|
+
import bind from 'axios/lib/helpers/bind';
|
|
14
|
+
var codeMessage = {
|
|
15
|
+
200: '服务器成功返回请求的数据。',
|
|
16
|
+
201: '新建或修改数据成功。',
|
|
17
|
+
202: '一个请求已经进入后台排队(异步任务)。',
|
|
18
|
+
204: '删除数据成功。',
|
|
19
|
+
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
|
|
20
|
+
401: '用户没有权限(令牌、用户名、密码错误)。',
|
|
21
|
+
403: '用户得到授权,但是访问是被禁止的。',
|
|
22
|
+
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
|
|
23
|
+
406: '请求的格式不可得。',
|
|
24
|
+
410: '请求的资源被永久删除,且不会再得到的。',
|
|
25
|
+
422: '当创建一个对象时,发生一个验证错误。',
|
|
26
|
+
500: '服务器发生错误,请检查服务器。',
|
|
27
|
+
502: '网关错误。',
|
|
28
|
+
503: '服务不可用,服务器暂时过载或维护。',
|
|
29
|
+
504: '网关超时。'
|
|
30
|
+
};
|
|
31
|
+
var Request = /*#__PURE__*/function (_EventEmitter) {
|
|
32
|
+
function Request() {
|
|
33
|
+
var _this;
|
|
34
|
+
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
35
|
+
_classCallCheck(this, Request);
|
|
36
|
+
_this = _callSuper(this, Request);
|
|
37
|
+
Object.defineProperty(_this, "ins", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: void 0
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(_this, "token", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: ''
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(_this, "config", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: {}
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(_this, "get", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: void 0
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(_this, "delete", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(_this, "head", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: void 0
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(_this, "post", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(_this, "put", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: void 0
|
|
84
|
+
});
|
|
85
|
+
_this.spawn(config);
|
|
86
|
+
return _this;
|
|
87
|
+
}
|
|
88
|
+
_inherits(Request, _EventEmitter);
|
|
89
|
+
return _createClass(Request, [{
|
|
90
|
+
key: "displayMsg",
|
|
91
|
+
value: function displayMsg(res) {
|
|
92
|
+
var isSuccessful = Request.checkRTCode(res);
|
|
93
|
+
var msg = Request.getMsg(res);
|
|
94
|
+
if (!msg) return;
|
|
95
|
+
this.emit('message', isSuccessful, msg, res);
|
|
96
|
+
// if (isSuccessful) {
|
|
97
|
+
// message.success(msg)
|
|
98
|
+
// } else {
|
|
99
|
+
// message.warning(msg)
|
|
100
|
+
// }
|
|
101
|
+
}
|
|
102
|
+
}, {
|
|
103
|
+
key: "spawn",
|
|
104
|
+
value: function spawn() {
|
|
105
|
+
var spawnConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
106
|
+
var ins = this.gen(spawnConfig);
|
|
107
|
+
this.ins = ins;
|
|
108
|
+
this.config = spawnConfig;
|
|
109
|
+
utils.extend(this, Axios.prototype, this.ins);
|
|
110
|
+
return this.ins;
|
|
111
|
+
}
|
|
112
|
+
}, {
|
|
113
|
+
key: "gen",
|
|
114
|
+
value: function gen() {
|
|
115
|
+
var _this2 = this;
|
|
116
|
+
var spawnConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
117
|
+
var _spawnConfig$config = spawnConfig.config,
|
|
118
|
+
config = _spawnConfig$config === void 0 ? {} : _spawnConfig$config,
|
|
119
|
+
_spawnConfig$onReques = spawnConfig.onRequest,
|
|
120
|
+
onRequest = _spawnConfig$onReques === void 0 ? function (value) {
|
|
121
|
+
return value;
|
|
122
|
+
} : _spawnConfig$onReques,
|
|
123
|
+
_spawnConfig$onRespon = spawnConfig.onResponse,
|
|
124
|
+
onResponse = _spawnConfig$onRespon === void 0 ? function (value) {
|
|
125
|
+
return value;
|
|
126
|
+
} : _spawnConfig$onRespon,
|
|
127
|
+
_spawnConfig$onReject = spawnConfig.onRejected,
|
|
128
|
+
onRejected = _spawnConfig$onReject === void 0 ? function (error) {
|
|
129
|
+
return Promise.reject(error);
|
|
130
|
+
} : _spawnConfig$onReject;
|
|
131
|
+
var ins = axios.create(config);
|
|
132
|
+
var _onErr = function _onErr(e) {
|
|
133
|
+
_this2.emit('error', e);
|
|
134
|
+
config.logger && Request.logger.error(e);
|
|
135
|
+
return onRejected(e);
|
|
136
|
+
};
|
|
137
|
+
ins.interceptors.request.use(function (config) {
|
|
138
|
+
var _a;
|
|
139
|
+
var args = getSearchParamsAll();
|
|
140
|
+
var headers = (_a = config.headers) !== null && _a !== void 0 ? _a : config.headers = {};
|
|
141
|
+
Object.entries(args).forEach(function (_ref) {
|
|
142
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
143
|
+
k = _ref2[0],
|
|
144
|
+
v = _ref2[1];
|
|
145
|
+
headers["X-Arg-".concat(k)] = encodeURI(v);
|
|
146
|
+
});
|
|
147
|
+
_this2.emit('request', config);
|
|
148
|
+
// TODO: remove
|
|
149
|
+
headers.Authorization = headers.Authorization || _this2.token || '';
|
|
150
|
+
// params.pathname = location.pathname
|
|
151
|
+
return onRequest(config);
|
|
152
|
+
});
|
|
153
|
+
ins.interceptors.response.use(function (response) {
|
|
154
|
+
var axiosConfig = response.config,
|
|
155
|
+
headers = response.headers;
|
|
156
|
+
var config = axiosConfig;
|
|
157
|
+
var url = config.url,
|
|
158
|
+
data = config.data,
|
|
159
|
+
params = config.params,
|
|
160
|
+
method = config.method,
|
|
161
|
+
logger = config.logger,
|
|
162
|
+
unboxing = config.unboxing;
|
|
163
|
+
_this2.displayMsg(response);
|
|
164
|
+
if (Request.checkRTCode(response)) {
|
|
165
|
+
logger && Request.logger.log({
|
|
166
|
+
url: url,
|
|
167
|
+
data: data,
|
|
168
|
+
params: params,
|
|
169
|
+
method: method
|
|
170
|
+
}, response.data);
|
|
171
|
+
_this2.emit('response', response);
|
|
172
|
+
var isSameOrigin = (url === null || url === void 0 ? void 0 : url.startsWith('http')) ? new URL(url).origin === location.origin : true;
|
|
173
|
+
if (isSameOrigin) {
|
|
174
|
+
var token = headers['Authorization'] || headers['authorization'];
|
|
175
|
+
_this2.token = token;
|
|
176
|
+
}
|
|
177
|
+
if (unboxing) {
|
|
178
|
+
response.data = doUnboxing(response.data);
|
|
179
|
+
}
|
|
180
|
+
return onResponse(response);
|
|
181
|
+
}
|
|
182
|
+
var e = Request.createErr(response);
|
|
183
|
+
return _onErr(e);
|
|
184
|
+
}, function (error) {
|
|
185
|
+
Request.logger.error('请求出错:', {
|
|
186
|
+
error: error
|
|
187
|
+
});
|
|
188
|
+
_this2.displayMsg(error.response);
|
|
189
|
+
var response = error === null || error === void 0 ? void 0 : error.response;
|
|
190
|
+
var e = Request.createErr(response, error);
|
|
191
|
+
return _onErr(e);
|
|
192
|
+
});
|
|
193
|
+
return ins;
|
|
194
|
+
}
|
|
195
|
+
}], [{
|
|
196
|
+
key: "checkRTCode",
|
|
197
|
+
value: function checkRTCode(res) {
|
|
198
|
+
var _a, _b, _c, _d;
|
|
199
|
+
if (!res) return false;
|
|
200
|
+
var headerCode = res.headers['lm-code'];
|
|
201
|
+
var rawCode = ((_a = res.data) === null || _a === void 0 ? void 0 : _a.id) ? 200 : (_b = res.data) === null || _b === void 0 ? void 0 : _b.code;
|
|
202
|
+
// 某些数据返回 status
|
|
203
|
+
var rawStatus = ((_c = res.data) === null || _c === void 0 ? void 0 : _c.id) ? 200 : (_d = res.data) === null || _d === void 0 ? void 0 : _d.status;
|
|
204
|
+
var _code = typeof rawCode === 'number' ? rawCode : rawStatus !== null && rawStatus !== void 0 ? rawStatus : 1;
|
|
205
|
+
var code = headerCode ? Number(headerCode) : _code;
|
|
206
|
+
// console.log('isSuccessful', { headerCode, _code,h:res.headers})
|
|
207
|
+
return Request.CONFIG.successCode.includes(code);
|
|
208
|
+
}
|
|
209
|
+
}, {
|
|
210
|
+
key: "genErrMsg",
|
|
211
|
+
value: function genErrMsg(res, error) {
|
|
212
|
+
var _a, _b, _c;
|
|
213
|
+
var status = (res === null || res === void 0 ? void 0 : res.status) || -1;
|
|
214
|
+
var rawMessage = (error === null || error === void 0 ? void 0 : error.message) === 'Network Error' ? '网络连接异常 ' : '';
|
|
215
|
+
var msg = (_c = (_b = (_a = Request.getMsg(res)) !== null && _a !== void 0 ? _a : codeMessage[status]) !== null && _b !== void 0 ? _b : rawMessage) !== null && _c !== void 0 ? _c : '未知错误';
|
|
216
|
+
return msg;
|
|
217
|
+
}
|
|
218
|
+
}, {
|
|
219
|
+
key: "createErr",
|
|
220
|
+
value: function createErr(res, error) {
|
|
221
|
+
var _a;
|
|
222
|
+
return {
|
|
223
|
+
error: error,
|
|
224
|
+
msg: Request.genErrMsg(res, error),
|
|
225
|
+
url: (_a = res === null || res === void 0 ? void 0 : res.config) === null || _a === void 0 ? void 0 : _a.url,
|
|
226
|
+
data: res === null || res === void 0 ? void 0 : res.data,
|
|
227
|
+
status: res === null || res === void 0 ? void 0 : res.status
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}, {
|
|
231
|
+
key: "getMsg",
|
|
232
|
+
value: function getMsg(res) {
|
|
233
|
+
if (!res) return '';
|
|
234
|
+
var _ref3 = res === null || res === void 0 ? void 0 : res.config,
|
|
235
|
+
_ref3$showMsg = _ref3.showMsg,
|
|
236
|
+
showMsg = _ref3$showMsg === void 0 ? true : _ref3$showMsg,
|
|
237
|
+
successText = _ref3.successText,
|
|
238
|
+
errText = _ref3.errText;
|
|
239
|
+
var remoteMsg = showMsg ? Request.getRemoteMessage(res) : '';
|
|
240
|
+
var isSuccessful = Request.checkRTCode(res);
|
|
241
|
+
var configMsg = isSuccessful ? successText : errText;
|
|
242
|
+
return configMsg !== null && configMsg !== void 0 ? configMsg : remoteMsg;
|
|
243
|
+
}
|
|
244
|
+
}, {
|
|
245
|
+
key: "getRemoteMessage",
|
|
246
|
+
value: function getRemoteMessage(res) {
|
|
247
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
248
|
+
var headerMessage = Request.getHeaderMessage(res);
|
|
249
|
+
var remoteMsg = (_f = (_d = (_b = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.msg) !== null && _b !== void 0 ? _b : (_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.detail) !== null && _d !== void 0 ? _d : (_e = res === null || res === void 0 ? void 0 : res.data) === null || _e === void 0 ? void 0 : _e.message) !== null && _f !== void 0 ? _f : (_g = res === null || res === void 0 ? void 0 : res.data) === null || _g === void 0 ? void 0 : _g.title;
|
|
250
|
+
return headerMessage !== null && headerMessage !== void 0 ? headerMessage : remoteMsg;
|
|
251
|
+
}
|
|
252
|
+
}, {
|
|
253
|
+
key: "getHeaderMessage",
|
|
254
|
+
value: function getHeaderMessage(res) {
|
|
255
|
+
if (!res) return;
|
|
256
|
+
var headerMessageRaw = res.headers['lm-message'];
|
|
257
|
+
var headerMessage = headerMessageRaw ? decodeURIComponent(headerMessageRaw) : undefined;
|
|
258
|
+
return headerMessage;
|
|
259
|
+
}
|
|
260
|
+
}]);
|
|
261
|
+
}(EventEmitter);
|
|
262
|
+
Object.defineProperty(Request, "CONFIG", {
|
|
263
|
+
enumerable: true,
|
|
264
|
+
configurable: true,
|
|
265
|
+
writable: true,
|
|
266
|
+
value: {
|
|
267
|
+
successCode: [200, 1, 0]
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
Object.defineProperty(Request, "logger", {
|
|
271
|
+
enumerable: true,
|
|
272
|
+
configurable: true,
|
|
273
|
+
writable: true,
|
|
274
|
+
value: new MyLog('Request')
|
|
275
|
+
});
|
|
276
|
+
var request = new Request();
|
|
277
|
+
function doUnboxing(res) {
|
|
278
|
+
var isBoxing = typeof (res === null || res === void 0 ? void 0 : res.code) === 'number' && !!res.data;
|
|
279
|
+
if (isBoxing) {
|
|
280
|
+
return res.data;
|
|
281
|
+
}
|
|
282
|
+
return res;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/******************************************************************************
|
|
286
|
+
Copyright (c) Microsoft Corporation.
|
|
287
|
+
|
|
288
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
289
|
+
purpose with or without fee is hereby granted.
|
|
290
|
+
|
|
291
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
292
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
293
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
294
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
295
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
296
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
297
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
298
|
+
***************************************************************************** */
|
|
299
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
300
|
+
function adopt(value) {
|
|
301
|
+
return value instanceof P ? value : new P(function (resolve) {
|
|
302
|
+
resolve(value);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
306
|
+
function fulfilled(value) {
|
|
307
|
+
try {
|
|
308
|
+
step(generator.next(value));
|
|
309
|
+
} catch (e) {
|
|
310
|
+
reject(e);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function rejected(value) {
|
|
314
|
+
try {
|
|
315
|
+
step(generator["throw"](value));
|
|
316
|
+
} catch (e) {
|
|
317
|
+
reject(e);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function step(result) {
|
|
321
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
322
|
+
}
|
|
323
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
327
|
+
var e = new Error(message);
|
|
328
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
329
|
+
};
|
|
330
|
+
function safe_fetch_options(cb, f) {
|
|
331
|
+
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
332
|
+
var arr, _cb$method, method, _cb$valueKey, valueKey, _cb$labelKey, labelKey, url, _url;
|
|
333
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
334
|
+
while (1) switch (_context.prev = _context.next) {
|
|
335
|
+
case 0:
|
|
336
|
+
if (!isFunction(cb)) {
|
|
337
|
+
_context.next = 7;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
_context.next = 3;
|
|
341
|
+
return safe_async_call(cb, f);
|
|
342
|
+
case 3:
|
|
343
|
+
arr = _context.sent;
|
|
344
|
+
return _context.abrupt("return", expect_array(arr));
|
|
345
|
+
case 7:
|
|
346
|
+
_cb$method = cb.method, method = _cb$method === void 0 ? 'get' : _cb$method, _cb$valueKey = cb.valueKey, valueKey = _cb$valueKey === void 0 ? 'value' : _cb$valueKey, _cb$labelKey = cb.labelKey, labelKey = _cb$labelKey === void 0 ? 'label' : _cb$labelKey, url = cb.url;
|
|
347
|
+
_url = isString(url) ? url : url();
|
|
348
|
+
return _context.abrupt("return", cache_fetch(_url, function () {
|
|
349
|
+
return request.ins({
|
|
350
|
+
method: method,
|
|
351
|
+
url: _url
|
|
352
|
+
});
|
|
353
|
+
}).then(function (res) {
|
|
354
|
+
return expect_array(res.data).map(function (_) {
|
|
355
|
+
return {
|
|
356
|
+
label: get(_, labelKey),
|
|
357
|
+
value: get(_, valueKey)
|
|
358
|
+
};
|
|
359
|
+
});
|
|
360
|
+
}));
|
|
361
|
+
case 10:
|
|
362
|
+
case "end":
|
|
363
|
+
return _context.stop();
|
|
364
|
+
}
|
|
365
|
+
}, _callee);
|
|
366
|
+
}));
|
|
367
|
+
}
|
|
368
|
+
function createInstance() {
|
|
369
|
+
var defaultConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
370
|
+
var instance = bind(Axios.prototype.request, request.ins);
|
|
371
|
+
utils.extend(instance, Axios.prototype, request.ins);
|
|
372
|
+
utils.extend(instance, request.ins);
|
|
373
|
+
return instance;
|
|
374
|
+
}
|
|
375
|
+
export { Request, codeMessage, createInstance, request, safe_fetch_options };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
export type { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
|
|
3
|
+
export interface IRequest_AxiosRequestConfig<D = any> extends AxiosRequestConfig<D> {
|
|
4
|
+
logger?: boolean;
|
|
5
|
+
unboxing?: boolean;
|
|
6
|
+
showMsg?: boolean;
|
|
7
|
+
successText?: string;
|
|
8
|
+
errText?: string;
|
|
9
|
+
ignore_usr?: boolean;
|
|
10
|
+
ignore_env?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface IRequest_ResponseError<D = any> {
|
|
13
|
+
msg: string;
|
|
14
|
+
data?: D;
|
|
15
|
+
status?: number;
|
|
16
|
+
url?: string;
|
|
17
|
+
error?: AxiosError<any, any>;
|
|
18
|
+
}
|
|
19
|
+
export interface IRequest_SpawnConfig {
|
|
20
|
+
config?: IRequest_AxiosRequestConfig;
|
|
21
|
+
onRequest?(value: IRequest_AxiosRequestConfig): IRequest_AxiosRequestConfig;
|
|
22
|
+
onResponse?(value: AxiosResponse): any;
|
|
23
|
+
onRejected?(err: IRequest_ResponseError): Promise<any>;
|
|
24
|
+
}
|
|
25
|
+
export type TRequest_SimpleReq = <T = any, R = AxiosResponse<T>, D = any>(url: string, config?: IRequest_AxiosRequestConfig<D>) => Promise<R>;
|
|
26
|
+
export type TRequest_PreReq = <T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: IRequest_AxiosRequestConfig<D>) => Promise<R>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noah-libjs/request",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.43",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@noah-libjs/utils": "^0.0.
|
|
24
|
+
"@noah-libjs/utils": "^0.0.43",
|
|
25
25
|
"axios": "^0.26.1",
|
|
26
26
|
"store": "2.x"
|
|
27
27
|
},
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"keywords": [],
|
|
33
33
|
"author": "",
|
|
34
34
|
"license": "MIT",
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "5dced859dd68c336e8837474c80a39eacb4456dd"
|
|
36
36
|
}
|