@noah-libjs/request 0.0.7 → 0.0.9

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/Request.d.ts CHANGED
@@ -41,3 +41,4 @@ export declare class Request extends EventEmitter<{
41
41
  post: TRequest_PreReq;
42
42
  put: TRequest_PreReq;
43
43
  }
44
+ export declare const request: Request;
@@ -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 CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AxiosInstance } from 'axios';
2
- import { Request } from './Request';
2
+ import { Request, request } from './Request';
3
3
  export * from './types';
4
+ export * from './helper';
4
5
  export { codeMessage } from './constant';
5
6
  declare function createInstance(defaultConfig?: {}): AxiosInstance;
6
- declare const request: Request;
7
7
  export { Request, createInstance, request };
package/dist/index.js CHANGED
@@ -1,13 +1,15 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
1
2
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
3
  import _createClass from "@babel/runtime/helpers/createClass";
3
4
  import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
4
5
  import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
5
6
  import _inherits from "@babel/runtime/helpers/inherits";
7
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
6
8
  function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
7
9
  function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
8
10
  import axios, { Axios } from 'axios';
9
11
  import utils from 'axios/lib/utils';
10
- import { MyLog, EventEmitter } from '@noah-libjs/utils';
12
+ import { MyLog, EventEmitter, getSearchParamsAll, isFunction, safe_async_call, expect_array, isString, cache_fetch, get } from '@noah-libjs/utils';
11
13
  import bind from 'axios/lib/helpers/bind';
12
14
  var codeMessage = {
13
15
  200: '服务器成功返回请求的数据。',
@@ -133,9 +135,18 @@ var Request = /*#__PURE__*/function (_EventEmitter) {
133
135
  return onRejected(e);
134
136
  };
135
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
+ });
136
147
  _this2.emit('request', config);
137
148
  // TODO: remove
138
- config.headers.Authorization = config.headers.Authorization || _this2.token || '';
149
+ headers.Authorization = headers.Authorization || _this2.token || '';
139
150
  // params.pathname = location.pathname
140
151
  return onRequest(config);
141
152
  });
@@ -220,11 +231,11 @@ var Request = /*#__PURE__*/function (_EventEmitter) {
220
231
  key: "getMsg",
221
232
  value: function getMsg(res) {
222
233
  if (!res) return '';
223
- var _ref = res === null || res === void 0 ? void 0 : res.config,
224
- _ref$showMsg = _ref.showMsg,
225
- showMsg = _ref$showMsg === void 0 ? true : _ref$showMsg,
226
- successText = _ref.successText,
227
- errText = _ref.errText;
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;
228
239
  var remoteMsg = showMsg ? Request.getRemoteMessage(res) : '';
229
240
  var isSuccessful = Request.checkRTCode(res);
230
241
  var configMsg = isSuccessful ? successText : errText;
@@ -262,6 +273,7 @@ Object.defineProperty(Request, "logger", {
262
273
  writable: true,
263
274
  value: new MyLog('Request')
264
275
  });
276
+ var request = new Request();
265
277
  function doUnboxing(res) {
266
278
  var isBoxing = typeof (res === null || res === void 0 ? void 0 : res.code) === 'number' && !!res.data;
267
279
  if (isBoxing) {
@@ -269,6 +281,90 @@ function doUnboxing(res) {
269
281
  }
270
282
  return res;
271
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
+ }
272
368
  function createInstance() {
273
369
  var defaultConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
274
370
  var instance = bind(Axios.prototype.request, request.ins);
@@ -276,5 +372,4 @@ function createInstance() {
276
372
  utils.extend(instance, request.ins);
277
373
  return instance;
278
374
  }
279
- var request = new Request();
280
- export { Request, codeMessage, createInstance, request };
375
+ export { Request, codeMessage, createInstance, request, safe_fetch_options };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noah-libjs/request",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
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.7",
24
+ "@noah-libjs/utils": "0.0.9",
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": "fcd560c4526837addb257776124afb9381853236"
35
+ "gitHead": "bb49d078600e65d77b842887c63be3b254faf999"
36
36
  }