orion-design 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export declare function helloworld(): void;
1
+ export { default as OrionError } from './error/OrionError';
package/dist/index.js CHANGED
@@ -1,3 +1 @@
1
- function helloworld() { }
2
-
3
- export { helloworld };
1
+ export { default as OrionError } from './error/OrionError.js';
@@ -1,5 +1,4 @@
1
- import { d as dayjs } from '../../dayjs.min-CYqA_arp.js';
2
- import '../../_commonjsHelpers-BFTU3MAI.js';
1
+ import dayjs from 'dayjs';
3
2
 
4
3
  class DateSerializer {
5
4
  format;
@@ -10,7 +10,7 @@ class DivisionResponseParser {
10
10
  return chain.doParse(response, chain);
11
11
  }
12
12
  // data为string时,兼容ie
13
- const { success, errorcode, data, errortext } = window.ActiveXObject || 'ActiveXObject' in window ? (typeof response.data === 'string' ? JSON.parse(response.data) : response.data) : response.data;
13
+ const { success, errorcode, data, errortext } = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
14
14
  if (success === true) {
15
15
  return chain.doParse(data, chain);
16
16
  }
@@ -1,28 +1,476 @@
1
- export { default } from './request.js';
2
- import '../../_commonjsHelpers-BFTU3MAI.js';
3
- import 'util';
4
- import 'stream';
5
- import 'path';
6
- import 'http';
7
- import 'https';
8
- import 'url';
9
- import 'fs';
10
- import 'assert';
11
- import 'tty';
12
- import 'os';
13
- import 'zlib';
14
- import 'events';
15
- import '../../dayjs.min-CYqA_arp.js';
1
+ import axios from 'axios';
2
+ import 'dayjs';
16
3
  import '../../utils/md5.js';
17
4
  import '../../bignumber-upqAL281.js';
18
- import '../ResponseParserChain.js';
19
- import '../ErrorHandlerChain.js';
20
- import '../RequestFilterChain.js';
21
- import './DateSerializer.js';
22
- import './DivisionResponseParser.js';
23
- import './DivisionErrorHandler.js';
24
- import '../error/ResponseError.js';
5
+ import ResponseParserChain from '../ResponseParserChain.js';
6
+ import ErrorHandlerChain from '../ErrorHandlerChain.js';
7
+ import RequestFilterChain from '../RequestFilterChain.js';
8
+ import DateSerializer from './DateSerializer.js';
9
+ import DivisionResponseParser from './DivisionResponseParser.js';
10
+ import DivisionErrorHandler from './DivisionErrorHandler.js';
11
+ import ResponseError from '../error/ResponseError.js';
12
+ import cloneDeep from '../../utils/cloneDeep.js';
25
13
  import '../error/ExceptionResponseError.js';
26
14
  import '../error/BizExceptionResponseError.js';
27
15
  import '../error/SessionExceptionResponseError.js';
28
- import '../../utils/cloneDeep.js';
16
+
17
+ /**
18
+ * base64.ts
19
+ *
20
+ * Licensed under the BSD 3-Clause License.
21
+ * http://opensource.org/licenses/BSD-3-Clause
22
+ *
23
+ * References:
24
+ * http://en.wikipedia.org/wiki/Base64
25
+ *
26
+ * @author Dan Kogai (https://github.com/dankogai)
27
+ */
28
+ const version = '3.7.7';
29
+ /**
30
+ * @deprecated use lowercase `version`.
31
+ */
32
+ const VERSION = version;
33
+ const _hasBuffer = typeof Buffer === 'function';
34
+ const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
35
+ const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
36
+ const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
37
+ const b64chs = Array.prototype.slice.call(b64ch);
38
+ const b64tab = ((a) => {
39
+ let tab = {};
40
+ a.forEach((c, i) => tab[c] = i);
41
+ return tab;
42
+ })(b64chs);
43
+ const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
44
+ const _fromCC = String.fromCharCode.bind(String);
45
+ const _U8Afrom = typeof Uint8Array.from === 'function'
46
+ ? Uint8Array.from.bind(Uint8Array)
47
+ : (it) => new Uint8Array(Array.prototype.slice.call(it, 0));
48
+ const _mkUriSafe = (src) => src
49
+ .replace(/=/g, '').replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_');
50
+ const _tidyB64 = (s) => s.replace(/[^A-Za-z0-9\+\/]/g, '');
51
+ /**
52
+ * polyfill version of `btoa`
53
+ */
54
+ const btoaPolyfill = (bin) => {
55
+ // console.log('polyfilled');
56
+ let u32, c0, c1, c2, asc = '';
57
+ const pad = bin.length % 3;
58
+ for (let i = 0; i < bin.length;) {
59
+ if ((c0 = bin.charCodeAt(i++)) > 255 ||
60
+ (c1 = bin.charCodeAt(i++)) > 255 ||
61
+ (c2 = bin.charCodeAt(i++)) > 255)
62
+ throw new TypeError('invalid character found');
63
+ u32 = (c0 << 16) | (c1 << 8) | c2;
64
+ asc += b64chs[u32 >> 18 & 63]
65
+ + b64chs[u32 >> 12 & 63]
66
+ + b64chs[u32 >> 6 & 63]
67
+ + b64chs[u32 & 63];
68
+ }
69
+ return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
70
+ };
71
+ /**
72
+ * does what `window.btoa` of web browsers do.
73
+ * @param {String} bin binary string
74
+ * @returns {string} Base64-encoded string
75
+ */
76
+ const _btoa = typeof btoa === 'function' ? (bin) => btoa(bin)
77
+ : _hasBuffer ? (bin) => Buffer.from(bin, 'binary').toString('base64')
78
+ : btoaPolyfill;
79
+ const _fromUint8Array = _hasBuffer
80
+ ? (u8a) => Buffer.from(u8a).toString('base64')
81
+ : (u8a) => {
82
+ // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
83
+ const maxargs = 0x1000;
84
+ let strs = [];
85
+ for (let i = 0, l = u8a.length; i < l; i += maxargs) {
86
+ strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
87
+ }
88
+ return _btoa(strs.join(''));
89
+ };
90
+ /**
91
+ * converts a Uint8Array to a Base64 string.
92
+ * @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
93
+ * @returns {string} Base64 string
94
+ */
95
+ const fromUint8Array = (u8a, urlsafe = false) => urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
96
+ // This trick is found broken https://github.com/dankogai/js-base64/issues/130
97
+ // const utob = (src: string) => unescape(encodeURIComponent(src));
98
+ // reverting good old fationed regexp
99
+ const cb_utob = (c) => {
100
+ if (c.length < 2) {
101
+ var cc = c.charCodeAt(0);
102
+ return cc < 0x80 ? c
103
+ : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
104
+ + _fromCC(0x80 | (cc & 0x3f)))
105
+ : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
106
+ + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
107
+ + _fromCC(0x80 | (cc & 0x3f)));
108
+ }
109
+ else {
110
+ var cc = 0x10000
111
+ + (c.charCodeAt(0) - 0xD800) * 0x400
112
+ + (c.charCodeAt(1) - 0xDC00);
113
+ return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
114
+ + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
115
+ + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
116
+ + _fromCC(0x80 | (cc & 0x3f)));
117
+ }
118
+ };
119
+ const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
120
+ /**
121
+ * @deprecated should have been internal use only.
122
+ * @param {string} src UTF-8 string
123
+ * @returns {string} UTF-16 string
124
+ */
125
+ const utob = (u) => u.replace(re_utob, cb_utob);
126
+ //
127
+ const _encode = _hasBuffer
128
+ ? (s) => Buffer.from(s, 'utf8').toString('base64')
129
+ : _TE
130
+ ? (s) => _fromUint8Array(_TE.encode(s))
131
+ : (s) => _btoa(utob(s));
132
+ /**
133
+ * converts a UTF-8-encoded string to a Base64 string.
134
+ * @param {boolean} [urlsafe] if `true` make the result URL-safe
135
+ * @returns {string} Base64 string
136
+ */
137
+ const encode = (src, urlsafe = false) => urlsafe
138
+ ? _mkUriSafe(_encode(src))
139
+ : _encode(src);
140
+ /**
141
+ * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
142
+ * @returns {string} Base64 string
143
+ */
144
+ const encodeURI = (src) => encode(src, true);
145
+ // This trick is found broken https://github.com/dankogai/js-base64/issues/130
146
+ // const btou = (src: string) => decodeURIComponent(escape(src));
147
+ // reverting good old fationed regexp
148
+ const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
149
+ const cb_btou = (cccc) => {
150
+ switch (cccc.length) {
151
+ case 4:
152
+ var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
153
+ | ((0x3f & cccc.charCodeAt(1)) << 12)
154
+ | ((0x3f & cccc.charCodeAt(2)) << 6)
155
+ | (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
156
+ return (_fromCC((offset >>> 10) + 0xD800)
157
+ + _fromCC((offset & 0x3FF) + 0xDC00));
158
+ case 3:
159
+ return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
160
+ | ((0x3f & cccc.charCodeAt(1)) << 6)
161
+ | (0x3f & cccc.charCodeAt(2)));
162
+ default:
163
+ return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
164
+ | (0x3f & cccc.charCodeAt(1)));
165
+ }
166
+ };
167
+ /**
168
+ * @deprecated should have been internal use only.
169
+ * @param {string} src UTF-16 string
170
+ * @returns {string} UTF-8 string
171
+ */
172
+ const btou = (b) => b.replace(re_btou, cb_btou);
173
+ /**
174
+ * polyfill version of `atob`
175
+ */
176
+ const atobPolyfill = (asc) => {
177
+ // console.log('polyfilled');
178
+ asc = asc.replace(/\s+/g, '');
179
+ if (!b64re.test(asc))
180
+ throw new TypeError('malformed base64.');
181
+ asc += '=='.slice(2 - (asc.length & 3));
182
+ let u24, bin = '', r1, r2;
183
+ for (let i = 0; i < asc.length;) {
184
+ u24 = b64tab[asc.charAt(i++)] << 18
185
+ | b64tab[asc.charAt(i++)] << 12
186
+ | (r1 = b64tab[asc.charAt(i++)]) << 6
187
+ | (r2 = b64tab[asc.charAt(i++)]);
188
+ bin += r1 === 64 ? _fromCC(u24 >> 16 & 255)
189
+ : r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255)
190
+ : _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255);
191
+ }
192
+ return bin;
193
+ };
194
+ /**
195
+ * does what `window.atob` of web browsers do.
196
+ * @param {String} asc Base64-encoded string
197
+ * @returns {string} binary string
198
+ */
199
+ const _atob = typeof atob === 'function' ? (asc) => atob(_tidyB64(asc))
200
+ : _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
201
+ : atobPolyfill;
202
+ //
203
+ const _toUint8Array = _hasBuffer
204
+ ? (a) => _U8Afrom(Buffer.from(a, 'base64'))
205
+ : (a) => _U8Afrom(_atob(a).split('').map(c => c.charCodeAt(0)));
206
+ /**
207
+ * converts a Base64 string to a Uint8Array.
208
+ */
209
+ const toUint8Array = (a) => _toUint8Array(_unURI(a));
210
+ //
211
+ const _decode = _hasBuffer
212
+ ? (a) => Buffer.from(a, 'base64').toString('utf8')
213
+ : _TD
214
+ ? (a) => _TD.decode(_toUint8Array(a))
215
+ : (a) => btou(_atob(a));
216
+ const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
217
+ /**
218
+ * converts a Base64 string to a UTF-8 string.
219
+ * @param {String} src Base64 string. Both normal and URL-safe are supported
220
+ * @returns {string} UTF-8 string
221
+ */
222
+ const decode = (src) => _decode(_unURI(src));
223
+ /**
224
+ * check if a value is a valid Base64 string
225
+ * @param {String} src a value to check
226
+ */
227
+ const isValid = (src) => {
228
+ if (typeof src !== 'string')
229
+ return false;
230
+ const s = src.replace(/\s+/g, '').replace(/={0,2}$/, '');
231
+ return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
232
+ };
233
+ //
234
+ const _noEnum = (v) => {
235
+ return {
236
+ value: v, enumerable: false, writable: true, configurable: true
237
+ };
238
+ };
239
+ /**
240
+ * extend String.prototype with relevant methods
241
+ */
242
+ const extendString = function () {
243
+ const _add = (name, body) => Object.defineProperty(String.prototype, name, _noEnum(body));
244
+ _add('fromBase64', function () { return decode(this); });
245
+ _add('toBase64', function (urlsafe) { return encode(this, urlsafe); });
246
+ _add('toBase64URI', function () { return encode(this, true); });
247
+ _add('toBase64URL', function () { return encode(this, true); });
248
+ _add('toUint8Array', function () { return toUint8Array(this); });
249
+ };
250
+ /**
251
+ * extend Uint8Array.prototype with relevant methods
252
+ */
253
+ const extendUint8Array = function () {
254
+ const _add = (name, body) => Object.defineProperty(Uint8Array.prototype, name, _noEnum(body));
255
+ _add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe); });
256
+ _add('toBase64URI', function () { return fromUint8Array(this, true); });
257
+ _add('toBase64URL', function () { return fromUint8Array(this, true); });
258
+ };
259
+ /**
260
+ * extend Builtin prototypes with relevant methods
261
+ */
262
+ const extendBuiltins = () => {
263
+ extendString();
264
+ extendUint8Array();
265
+ };
266
+ const gBase64 = {
267
+ version: version,
268
+ VERSION: VERSION,
269
+ atob: _atob,
270
+ atobPolyfill: atobPolyfill,
271
+ btoa: _btoa,
272
+ btoaPolyfill: btoaPolyfill,
273
+ fromBase64: decode,
274
+ toBase64: encode,
275
+ encode: encode,
276
+ encodeURI: encodeURI,
277
+ encodeURL: encodeURI,
278
+ utob: utob,
279
+ btou: btou,
280
+ decode: decode,
281
+ isValid: isValid,
282
+ fromUint8Array: fromUint8Array,
283
+ toUint8Array: toUint8Array,
284
+ extendString: extendString,
285
+ extendUint8Array: extendUint8Array,
286
+ extendBuiltins: extendBuiltins
287
+ };
288
+
289
+ //////////////////////////////////// Chain
290
+ let gRequestHeaders = () => ({});
291
+ let gResponseParsers = [new DivisionResponseParser()];
292
+ let gErrorHandlers = [new DivisionErrorHandler()];
293
+ let gRequestFilters = [
294
+ {
295
+ doFilter: (request, chain) => {
296
+ const headers = gRequestHeaders();
297
+ Object.keys(headers).forEach(function (key) {
298
+ request.headers[key] = headers[key];
299
+ });
300
+ chain.doFilter(request, chain);
301
+ },
302
+ },
303
+ {
304
+ doFilter: (request) => {
305
+ new DateSerializer('YYYYMMDDHHmmss').serialize(request.data);
306
+ },
307
+ },
308
+ ];
309
+ //////////////////////////////////// GLOBAL VARS
310
+ let BASE_URL = '/';
311
+ let REQUEST_TIMEOUT = 1000 * 30;
312
+ //////////////////////////////////// AXIOS
313
+ const g_AxiosInstance = axios.create();
314
+ g_AxiosInstance.interceptors.request.use(function (config) {
315
+ const chain = new RequestFilterChain(gRequestFilters);
316
+ chain.doFilter(config, chain);
317
+ return config;
318
+ });
319
+ g_AxiosInstance.interceptors.response.use(function (response) {
320
+ try {
321
+ const chain = new ResponseParserChain(gResponseParsers);
322
+ return chain.doParse(response, chain);
323
+ }
324
+ catch (e) {
325
+ throw new ResponseError(e.message, { cause: e }, response);
326
+ }
327
+ });
328
+ function request(url, data = {}, options = { loading: true, mask: false, timeout: REQUEST_TIMEOUT }) {
329
+ let newData = data;
330
+ if (!(data instanceof FormData)) {
331
+ newData = cloneDeep(data);
332
+ }
333
+ if (options.loading === true) ;
334
+ return g_AxiosInstance
335
+ .request({
336
+ baseURL: BASE_URL,
337
+ method: 'POST',
338
+ timeout: REQUEST_TIMEOUT,
339
+ withCredentials: true,
340
+ url,
341
+ data: newData,
342
+ responseType: 'json',
343
+ ...options,
344
+ })
345
+ .then((data) => {
346
+ return data;
347
+ })
348
+ .catch(function (error) {
349
+ const chain = new ErrorHandlerChain(gErrorHandlers);
350
+ return chain.handle(error, chain);
351
+ });
352
+ }
353
+ function download(url, data = {}, options = { loading: true, mask: false, timeout: REQUEST_TIMEOUT }) {
354
+ if (options.loading === true) ;
355
+ return g_AxiosInstance
356
+ .request({
357
+ baseURL: BASE_URL,
358
+ method: 'POST',
359
+ timeout: REQUEST_TIMEOUT,
360
+ withCredentials: true,
361
+ url,
362
+ data,
363
+ responseType: 'blob',
364
+ ...options,
365
+ })
366
+ .then((data) => {
367
+ return data;
368
+ })
369
+ .then((response) => {
370
+ const contentDisposition = response.headers['content-disposition'];
371
+ const blob = response.data;
372
+ const base64FileName = contentDisposition.match(/attachment; filename=\"(.*?)\"/)[1];
373
+ const decodedFileName = gBase64.decode(base64FileName);
374
+ // for IE
375
+ //@ts-ignore
376
+ if (window.navigator && window.navigator.msSaveOrOpenBlob) {
377
+ //@ts-ignore
378
+ window.navigator.msSaveOrOpenBlob(blob, decodedFileName);
379
+ }
380
+ else {
381
+ const a = document.createElement('a');
382
+ const url = URL.createObjectURL(blob);
383
+ a.href = url;
384
+ a.download = decodedFileName;
385
+ document.body.appendChild(a);
386
+ a.click();
387
+ document.body.removeChild(a);
388
+ URL.revokeObjectURL(url);
389
+ }
390
+ })
391
+ .catch(function (error) {
392
+ const chain = new ErrorHandlerChain(gErrorHandlers);
393
+ return chain.handle(error, chain);
394
+ });
395
+ }
396
+ function upload(url, params, options) {
397
+ const formdata = new FormData();
398
+ if (params) {
399
+ const keys = Object.keys(params);
400
+ const normalParams = {};
401
+ for (let i = 0; i < keys.length; i++) {
402
+ const key = keys[i];
403
+ const itemValue = params[key];
404
+ if (itemValue instanceof File) {
405
+ formdata.append(key, itemValue);
406
+ }
407
+ else if (itemValue instanceof Array) {
408
+ let isFile = false;
409
+ for (let j = 0; j < itemValue.length; j++) {
410
+ if (itemValue[j] instanceof File) {
411
+ isFile = true;
412
+ break;
413
+ }
414
+ }
415
+ if (isFile) {
416
+ for (let j = 0; j < itemValue.length; j++) {
417
+ formdata.append(key, itemValue[j]);
418
+ }
419
+ }
420
+ else {
421
+ // formdata.append(key, JSON.stringify(itemValue));
422
+ normalParams[key] = itemValue;
423
+ }
424
+ }
425
+ else {
426
+ // formdata.append(key, itemValue);
427
+ normalParams[key] = itemValue;
428
+ }
429
+ }
430
+ formdata.append('ibayJson', JSON.stringify(normalParams));
431
+ }
432
+ return request(url, formdata, options);
433
+ }
434
+ //////////////////////////////////// Exports
435
+ // api
436
+ request.download = download;
437
+ request.upload = upload;
438
+ // setters
439
+ request.setBaseUrl = (pBaseUrl) => {
440
+ if (!pBaseUrl) {
441
+ throw new Error(`pBaseUrl is null!`);
442
+ }
443
+ BASE_URL = pBaseUrl;
444
+ };
445
+ request.setRequestTimeout = (pTimeout) => {
446
+ REQUEST_TIMEOUT = pTimeout;
447
+ };
448
+ request.setRequestHeaders = (pRequestHeaders) => {
449
+ if (!pRequestHeaders) {
450
+ throw new Error(`pRequestHeaders is null!`);
451
+ }
452
+ if (!(pRequestHeaders instanceof Function)) {
453
+ throw new Error(`pRequestHeaders is not a function!`);
454
+ }
455
+ gRequestHeaders = pRequestHeaders;
456
+ };
457
+ request.setRequestFilters = (...filters) => {
458
+ if (filters == null) {
459
+ throw new Error(`filters is null!`);
460
+ }
461
+ gRequestFilters = filters;
462
+ };
463
+ request.setResponseParsers = (...parsers) => {
464
+ if (parsers == null) {
465
+ throw new Error(`parsers is null!`);
466
+ }
467
+ gResponseParsers = parsers;
468
+ };
469
+ request.setErrorHandlers = (...handlers) => {
470
+ if (handlers == null) {
471
+ throw new Error(`handlers should not null!`);
472
+ }
473
+ gErrorHandlers = handlers;
474
+ };
475
+
476
+ export { request as default };