@1money/protocol-ts-sdk 1.0.14
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/.claude/settings.local.json +14 -0
- package/CLAUDE.md +77 -0
- package/README.md +600 -0
- package/es/api/accounts/index.d.ts +20 -0
- package/es/api/accounts/types.d.ts +9 -0
- package/es/api/chain/index.d.ts +12 -0
- package/es/api/chain/types.d.ts +3 -0
- package/es/api/checkpoints/index.d.ts +26 -0
- package/es/api/checkpoints/types.d.ts +33 -0
- package/es/api/constants.d.ts +9 -0
- package/es/api/index.d.ts +31 -0
- package/es/api/index.js +593 -0
- package/es/api/state/index.d.ts +12 -0
- package/es/api/state/types.d.ts +7 -0
- package/es/api/tokens/index.d.ts +62 -0
- package/es/api/tokens/types.d.ts +130 -0
- package/es/api/transactions/index.d.ts +35 -0
- package/es/api/transactions/types.d.ts +25 -0
- package/es/api/types.d.ts +18 -0
- package/es/client/core.d.ts +109 -0
- package/es/client/index.d.ts +21 -0
- package/es/client/index.js +373 -0
- package/es/index.d.ts +20 -0
- package/es/index.js +1490 -0
- package/es/utils/address.d.ts +15 -0
- package/es/utils/index.d.ts +6 -0
- package/es/utils/index.js +841 -0
- package/es/utils/interface.d.ts +7 -0
- package/es/utils/safePromise.d.ts +4 -0
- package/es/utils/sign.d.ts +15 -0
- package/es/utils/txHash.d.ts +2 -0
- package/es/utils/typeof.d.ts +2 -0
- package/lib/api/accounts/index.d.ts +20 -0
- package/lib/api/accounts/types.d.ts +9 -0
- package/lib/api/chain/index.d.ts +12 -0
- package/lib/api/chain/types.d.ts +3 -0
- package/lib/api/checkpoints/index.d.ts +26 -0
- package/lib/api/checkpoints/types.d.ts +33 -0
- package/lib/api/constants.d.ts +9 -0
- package/lib/api/index.d.ts +31 -0
- package/lib/api/index.js +692 -0
- package/lib/api/state/index.d.ts +12 -0
- package/lib/api/state/types.d.ts +7 -0
- package/lib/api/tokens/index.d.ts +62 -0
- package/lib/api/tokens/types.d.ts +130 -0
- package/lib/api/transactions/index.d.ts +35 -0
- package/lib/api/transactions/types.d.ts +25 -0
- package/lib/api/types.d.ts +18 -0
- package/lib/client/core.d.ts +109 -0
- package/lib/client/index.d.ts +21 -0
- package/lib/client/index.js +434 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.js +1591 -0
- package/lib/utils/address.d.ts +15 -0
- package/lib/utils/index.d.ts +6 -0
- package/lib/utils/index.js +937 -0
- package/lib/utils/interface.d.ts +7 -0
- package/lib/utils/safePromise.d.ts +4 -0
- package/lib/utils/sign.d.ts +15 -0
- package/lib/utils/txHash.d.ts +2 -0
- package/lib/utils/typeof.d.ts +2 -0
- package/package.json +111 -0
- package/public/favicon.ico +0 -0
- package/public/logo.png +0 -0
- package/umd/1money-protocol-ts-sdk.min.js +2 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var axios$1=require('axios');require('viem'),require('@ethereumjs/rlp');/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
var __assign = function() {
|
|
19
|
+
__assign = Object.assign || function __assign(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function __rest(s, e) {
|
|
30
|
+
var t = {};
|
|
31
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
32
|
+
t[p] = s[p];
|
|
33
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
34
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
35
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
36
|
+
t[p[i]] = s[p[i]];
|
|
37
|
+
}
|
|
38
|
+
return t;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
42
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
43
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
44
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
45
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
46
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
47
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function __generator(thisArg, body) {
|
|
52
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
53
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
54
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
55
|
+
function step(op) {
|
|
56
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
57
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
58
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
59
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
60
|
+
switch (op[0]) {
|
|
61
|
+
case 0: case 1: t = op; break;
|
|
62
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
63
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
64
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
65
|
+
default:
|
|
66
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
67
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
68
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
69
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
70
|
+
if (t[2]) _.ops.pop();
|
|
71
|
+
_.trys.pop(); continue;
|
|
72
|
+
}
|
|
73
|
+
op = body.call(thisArg, _);
|
|
74
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
75
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
80
|
+
var e = new Error(message);
|
|
81
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
82
|
+
};function _typeof(ele) {
|
|
83
|
+
if (typeof ele !== 'object')
|
|
84
|
+
return (typeof ele).toLowerCase();
|
|
85
|
+
var typeStr = Object.prototype.toString.call(ele);
|
|
86
|
+
return typeStr.slice(8, typeStr.length - 1).toLowerCase();
|
|
87
|
+
}var ResponsePromise = /** @class */ (function () {
|
|
88
|
+
function ResponsePromise() {
|
|
89
|
+
var _this = this;
|
|
90
|
+
this.promiseWrapper = new Promise(function (resolve, reject) {
|
|
91
|
+
_this._resolve = resolve;
|
|
92
|
+
_this._reject = reject;
|
|
93
|
+
});
|
|
94
|
+
this._restScope = ['success', 'failure', 'error', 'login', 'timeout'];
|
|
95
|
+
var _loop_1 = function (i) {
|
|
96
|
+
var method = this_1._restScope[i];
|
|
97
|
+
if (!method)
|
|
98
|
+
return "continue";
|
|
99
|
+
this_1.promiseWrapper[method] = function (_callback) {
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
if (typeof _callback === 'function')
|
|
102
|
+
_this["_".concat(method)] = _callback;
|
|
103
|
+
var wrapper = _this.promiseWrapper;
|
|
104
|
+
delete wrapper[method];
|
|
105
|
+
var methods = _this._restScope.slice();
|
|
106
|
+
methods.splice(methods.indexOf(method), 1);
|
|
107
|
+
if (methods.every(function (_m) { return !!_m && !wrapper[_m]; }))
|
|
108
|
+
delete wrapper.rest;
|
|
109
|
+
return wrapper;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
var this_1 = this;
|
|
113
|
+
for (var i = 0; i < this._restScope.length; i++) {
|
|
114
|
+
_loop_1(i);
|
|
115
|
+
}
|
|
116
|
+
this.promiseWrapper.rest = function (onRest, scope) {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
if (typeof onRest === 'function')
|
|
119
|
+
_this._rest = onRest;
|
|
120
|
+
var wrapper = _this.promiseWrapper;
|
|
121
|
+
delete wrapper.rest;
|
|
122
|
+
_this._restScope = scope || _this._restScope;
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
if (_this._restScope.length === 0) {
|
|
125
|
+
console.warn('[1Money client]: The ".rest(cb, scope)" scope is empty and will never be triggered!');
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
var deletedCounter_1 = 0;
|
|
129
|
+
_this._restScope.forEach(function (method) {
|
|
130
|
+
if (!method)
|
|
131
|
+
return;
|
|
132
|
+
if (wrapper[method]) {
|
|
133
|
+
delete wrapper[method];
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
deletedCounter_1++;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
if (deletedCounter_1 === _this._restScope.length) {
|
|
140
|
+
console.warn("[1Money client]: The \"".concat(_this._restScope.join(', '), "\" had been called and the \"rest\" will never be triggered!"));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return wrapper;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return ResponsePromise;
|
|
147
|
+
}());
|
|
148
|
+
var Request = /** @class */ (function () {
|
|
149
|
+
function Request(config) {
|
|
150
|
+
this._config = config || {};
|
|
151
|
+
this.axios = axios$1;
|
|
152
|
+
this.parseError = this.parseError.bind(this);
|
|
153
|
+
this.setting = this.setting.bind(this);
|
|
154
|
+
this.request = this.request.bind(this);
|
|
155
|
+
}
|
|
156
|
+
Request.prototype.parseError = function (err) {
|
|
157
|
+
var _a, _b, _c, _d, _f, _g, _h, _j, _k;
|
|
158
|
+
if (typeof err === 'string')
|
|
159
|
+
err = new Error(err);
|
|
160
|
+
// Ensure we have a valid error object
|
|
161
|
+
if (!err || (_typeof(err) !== 'object' && _typeof(err) !== 'error')) {
|
|
162
|
+
err = new Error('Unknown error occurred');
|
|
163
|
+
}
|
|
164
|
+
var name = (_a = err === null || err === void 0 ? void 0 : err.name) !== null && _a !== void 0 ? _a : 'Error';
|
|
165
|
+
var message = (_d = (_b = err === null || err === void 0 ? void 0 : err.message) !== null && _b !== void 0 ? _b : (_c = err === null || err === void 0 ? void 0 : err.toString) === null || _c === void 0 ? void 0 : _c.call(err)) !== null && _d !== void 0 ? _d : 'Unknown error';
|
|
166
|
+
var stack = (_f = err === null || err === void 0 ? void 0 : err.stack) !== null && _f !== void 0 ? _f : '';
|
|
167
|
+
var status = (_h = (_g = err === null || err === void 0 ? void 0 : err.response) === null || _g === void 0 ? void 0 : _g.status) !== null && _h !== void 0 ? _h : 500;
|
|
168
|
+
var data = (_k = (_j = err === null || err === void 0 ? void 0 : err.response) === null || _j === void 0 ? void 0 : _j.data) !== null && _k !== void 0 ? _k : undefined;
|
|
169
|
+
// Validate status code
|
|
170
|
+
if (typeof status !== 'number' || status < 100 || status > 599) {
|
|
171
|
+
return {
|
|
172
|
+
name: 'InvalidStatusError',
|
|
173
|
+
message: 'Invalid HTTP status code',
|
|
174
|
+
stack: stack,
|
|
175
|
+
status: 500,
|
|
176
|
+
data: data
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
name: name,
|
|
181
|
+
message: message,
|
|
182
|
+
stack: stack,
|
|
183
|
+
status: status,
|
|
184
|
+
data: data
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
Request.prototype.setting = function (config) {
|
|
188
|
+
if (!config)
|
|
189
|
+
return console.warn('[1Money client]: setting method required correct parameters!');
|
|
190
|
+
this._config = __assign(__assign({}, this._config), config);
|
|
191
|
+
};
|
|
192
|
+
Request.prototype.request = function (options) {
|
|
193
|
+
var _this = this;
|
|
194
|
+
// Set default security headers
|
|
195
|
+
options.withCredentials = typeof options.withCredentials === 'boolean' ? options.withCredentials : true;
|
|
196
|
+
options.headers = options.headers || {};
|
|
197
|
+
options.headers['Accept'] = options.headers['Accept'] || '*/*';
|
|
198
|
+
options.headers['X-Requested-With'] = options.headers['X-Requested-With'] || 'XMLHttpRequest';
|
|
199
|
+
options.headers['X-Content-Type-Options'] = options.headers['X-Content-Type-Options'] || 'nosniff';
|
|
200
|
+
var _a = this._config, initOnSuccess = _a.onSuccess, initOnFailure = _a.onFailure, initOnLogin = _a.onLogin, initOnError = _a.onError, initOnTimeout = _a.onTimeout, initIsSuccess = _a.isSuccess, initIsLogin = _a.isLogin, initTimeout = _a.timeout;
|
|
201
|
+
var onSuccess = options.onSuccess, onFailure = options.onFailure, onLogin = options.onLogin, onError = options.onError, onTimeout = options.onTimeout, isSuccess = options.isSuccess, isLogin = options.isLogin, timeout = options.timeout;
|
|
202
|
+
var rules = {
|
|
203
|
+
success: isSuccess !== null && isSuccess !== void 0 ? isSuccess : initIsSuccess,
|
|
204
|
+
login: isLogin !== null && isLogin !== void 0 ? isLogin : initIsLogin,
|
|
205
|
+
};
|
|
206
|
+
var ResPromise = new ResponsePromise();
|
|
207
|
+
Promise.resolve().then(function () {
|
|
208
|
+
var _a, _b, _c, _d, _f, _g, _h, _j, _k, _l, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
209
|
+
var callbacks = {
|
|
210
|
+
success: (_d = (_c = (_b = (_a = ResPromise._success) !== null && _a !== void 0 ? _a : (~ResPromise._restScope.indexOf('success') ? ResPromise._rest : undefined)) !== null && _b !== void 0 ? _b : onSuccess) !== null && _c !== void 0 ? _c : initOnSuccess) !== null && _d !== void 0 ? _d : (function (r, h) { return r; }),
|
|
211
|
+
failure: (_j = (_h = (_g = (_f = ResPromise._failure) !== null && _f !== void 0 ? _f : (~ResPromise._restScope.indexOf('failure') ? ResPromise._rest : undefined)) !== null && _g !== void 0 ? _g : onFailure) !== null && _h !== void 0 ? _h : initOnFailure) !== null && _j !== void 0 ? _j : (function (v, h) { return v; }),
|
|
212
|
+
error: (_p = (_o = (_l = (_k = ResPromise._error) !== null && _k !== void 0 ? _k : (~ResPromise._restScope.indexOf('error') ? ResPromise._rest : undefined)) !== null && _l !== void 0 ? _l : onError) !== null && _o !== void 0 ? _o : initOnError) !== null && _p !== void 0 ? _p : (function (v, h) { return v; }),
|
|
213
|
+
login: (_t = (_s = (_r = (_q = ResPromise._login) !== null && _q !== void 0 ? _q : (~ResPromise._restScope.indexOf('login') ? ResPromise._rest : undefined)) !== null && _r !== void 0 ? _r : onLogin) !== null && _s !== void 0 ? _s : initOnLogin) !== null && _t !== void 0 ? _t : (function (v, h) { return v; }),
|
|
214
|
+
timeout: (_x = (_w = (_v = (_u = ResPromise._timeout) !== null && _u !== void 0 ? _u : (~ResPromise._restScope.indexOf('timeout') ? ResPromise._rest : undefined)) !== null && _v !== void 0 ? _v : onTimeout) !== null && _w !== void 0 ? _w : initOnTimeout) !== null && _x !== void 0 ? _x : (function (v, h) { return v; }),
|
|
215
|
+
};
|
|
216
|
+
var existedHandler = {
|
|
217
|
+
success: !!(ResPromise._success ||
|
|
218
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('success')) ||
|
|
219
|
+
onSuccess ||
|
|
220
|
+
initOnSuccess),
|
|
221
|
+
failure: !!(ResPromise._failure ||
|
|
222
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('failure')) ||
|
|
223
|
+
onFailure ||
|
|
224
|
+
initOnFailure),
|
|
225
|
+
login: !!(ResPromise._login ||
|
|
226
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('login')) ||
|
|
227
|
+
onLogin ||
|
|
228
|
+
initOnLogin),
|
|
229
|
+
error: !!(ResPromise._error ||
|
|
230
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('error')) ||
|
|
231
|
+
onError ||
|
|
232
|
+
initOnError),
|
|
233
|
+
timeout: !!(ResPromise._timeout ||
|
|
234
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('timeout')) ||
|
|
235
|
+
onTimeout ||
|
|
236
|
+
initOnTimeout),
|
|
237
|
+
};
|
|
238
|
+
var existedChainHandler = {
|
|
239
|
+
success: !!(ResPromise._success ||
|
|
240
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('success'))),
|
|
241
|
+
failure: !!(ResPromise._failure ||
|
|
242
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('failure'))),
|
|
243
|
+
login: !!(ResPromise._login ||
|
|
244
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('login'))),
|
|
245
|
+
error: !!(ResPromise._error ||
|
|
246
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('error'))),
|
|
247
|
+
timeout: !!(ResPromise._timeout ||
|
|
248
|
+
(ResPromise._rest && ~ResPromise._restScope.indexOf('timeout'))),
|
|
249
|
+
};
|
|
250
|
+
var errorHandler = function (e, headers) { return __awaiter(_this, void 0, void 0, function () {
|
|
251
|
+
var err, result, _e_1;
|
|
252
|
+
return __generator(this, function (_a) {
|
|
253
|
+
switch (_a.label) {
|
|
254
|
+
case 0:
|
|
255
|
+
_a.trys.push([0, 2, , 3]);
|
|
256
|
+
err = this.parseError(e);
|
|
257
|
+
return [4 /*yield*/, Promise.resolve(callbacks.error(err, headers))];
|
|
258
|
+
case 1:
|
|
259
|
+
result = _a.sent();
|
|
260
|
+
if (existedChainHandler.error)
|
|
261
|
+
err = result;
|
|
262
|
+
existedHandler.error ? ResPromise._resolve(err) : ResPromise._reject(err);
|
|
263
|
+
return [3 /*break*/, 3];
|
|
264
|
+
case 2:
|
|
265
|
+
_e_1 = _a.sent();
|
|
266
|
+
ResPromise._reject(this.parseError(_e_1));
|
|
267
|
+
return [3 /*break*/, 3];
|
|
268
|
+
case 3: return [2 /*return*/];
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}); };
|
|
272
|
+
var timer = null;
|
|
273
|
+
var isTimeout = false;
|
|
274
|
+
var _timeout = timeout !== null && timeout !== void 0 ? timeout : initTimeout;
|
|
275
|
+
// Cleanup function for timeout
|
|
276
|
+
var cleanup = function () {
|
|
277
|
+
if (timer !== null) {
|
|
278
|
+
clearTimeout(timer);
|
|
279
|
+
timer = null;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
if (_timeout) {
|
|
283
|
+
timer = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
284
|
+
var err, res, e_1;
|
|
285
|
+
var _a, _b;
|
|
286
|
+
return __generator(this, function (_c) {
|
|
287
|
+
switch (_c.label) {
|
|
288
|
+
case 0:
|
|
289
|
+
_c.trys.push([0, 2, , 3]);
|
|
290
|
+
isTimeout = true;
|
|
291
|
+
cleanup();
|
|
292
|
+
err = this.parseError('timeout');
|
|
293
|
+
return [4 /*yield*/, Promise.resolve(callbacks.timeout(err, (_a = options.headers) !== null && _a !== void 0 ? _a : {}))];
|
|
294
|
+
case 1:
|
|
295
|
+
res = _c.sent();
|
|
296
|
+
if (existedChainHandler.timeout)
|
|
297
|
+
err = res;
|
|
298
|
+
existedHandler.timeout ? ResPromise._resolve(err) : ResPromise._reject(err);
|
|
299
|
+
return [3 /*break*/, 3];
|
|
300
|
+
case 2:
|
|
301
|
+
e_1 = _c.sent();
|
|
302
|
+
errorHandler(e_1, (_b = options.headers) !== null && _b !== void 0 ? _b : {});
|
|
303
|
+
return [3 /*break*/, 3];
|
|
304
|
+
case 3: return [2 /*return*/];
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}); }, _timeout);
|
|
308
|
+
}
|
|
309
|
+
_this.axios(options).then(function (response) { return __awaiter(_this, void 0, void 0, function () {
|
|
310
|
+
var status, data, headers, doSuccess, doLogin, res, result, result, result, e_2;
|
|
311
|
+
var _a, _b;
|
|
312
|
+
return __generator(this, function (_c) {
|
|
313
|
+
switch (_c.label) {
|
|
314
|
+
case 0:
|
|
315
|
+
if (isTimeout)
|
|
316
|
+
return [2 /*return*/];
|
|
317
|
+
cleanup();
|
|
318
|
+
status = response.status, data = response.data, headers = response.headers;
|
|
319
|
+
_c.label = 1;
|
|
320
|
+
case 1:
|
|
321
|
+
_c.trys.push([1, 8, , 9]);
|
|
322
|
+
doSuccess = (_a = rules.success) === null || _a === void 0 ? void 0 : _a.call(rules, data, status, headers);
|
|
323
|
+
doLogin = (_b = rules.login) === null || _b === void 0 ? void 0 : _b.call(rules, data, status, headers);
|
|
324
|
+
res = data;
|
|
325
|
+
if (!doLogin) return [3 /*break*/, 3];
|
|
326
|
+
return [4 /*yield*/, Promise.resolve(callbacks.login(data, headers))];
|
|
327
|
+
case 2:
|
|
328
|
+
result = _c.sent();
|
|
329
|
+
if (existedChainHandler.login)
|
|
330
|
+
res = result;
|
|
331
|
+
return [3 /*break*/, 7];
|
|
332
|
+
case 3:
|
|
333
|
+
if (!doSuccess) return [3 /*break*/, 5];
|
|
334
|
+
return [4 /*yield*/, Promise.resolve(callbacks.success(data, headers))];
|
|
335
|
+
case 4:
|
|
336
|
+
result = _c.sent();
|
|
337
|
+
if (existedChainHandler.success)
|
|
338
|
+
res = result;
|
|
339
|
+
return [3 /*break*/, 7];
|
|
340
|
+
case 5: return [4 /*yield*/, Promise.resolve(callbacks.failure(data, headers))];
|
|
341
|
+
case 6:
|
|
342
|
+
result = _c.sent();
|
|
343
|
+
if (existedChainHandler.failure)
|
|
344
|
+
res = result;
|
|
345
|
+
_c.label = 7;
|
|
346
|
+
case 7:
|
|
347
|
+
ResPromise._resolve(res);
|
|
348
|
+
return [3 /*break*/, 9];
|
|
349
|
+
case 8:
|
|
350
|
+
e_2 = _c.sent();
|
|
351
|
+
errorHandler(e_2, headers);
|
|
352
|
+
return [3 /*break*/, 9];
|
|
353
|
+
case 9: return [2 /*return*/];
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}); }).catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
357
|
+
var status, data, headers, res, doLogin, e_3;
|
|
358
|
+
var _a, _b, _c, _d, _f, _g, _h, _j, _k, _l, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
359
|
+
return __generator(this, function (_w) {
|
|
360
|
+
switch (_w.label) {
|
|
361
|
+
case 0:
|
|
362
|
+
if (isTimeout)
|
|
363
|
+
return [2 /*return*/];
|
|
364
|
+
cleanup();
|
|
365
|
+
console.error("[1Money client]: Error(".concat((_a = err.status) !== null && _a !== void 0 ? _a : 500, ", ").concat((_b = err.code) !== null && _b !== void 0 ? _b : 'UNKNOWN', "), Message: ").concat(err.message, ", Config: ").concat((_c = err.config) === null || _c === void 0 ? void 0 : _c.method, ", ").concat((_f = (_d = err.config) === null || _d === void 0 ? void 0 : _d.baseURL) !== null && _f !== void 0 ? _f : '').concat((_h = (_g = err.config) === null || _g === void 0 ? void 0 : _g.url) !== null && _h !== void 0 ? _h : '', ", ").concat(JSON.stringify((_k = (_j = err.config) === null || _j === void 0 ? void 0 : _j.headers) !== null && _k !== void 0 ? _k : {}), ", Request: ").concat(JSON.stringify((_o = (_l = err.config) === null || _l === void 0 ? void 0 : _l.data) !== null && _o !== void 0 ? _o : {}), ";"));
|
|
366
|
+
status = (_q = (_p = err.response) === null || _p === void 0 ? void 0 : _p.status) !== null && _q !== void 0 ? _q : 500;
|
|
367
|
+
data = (_s = (_r = err.response) === null || _r === void 0 ? void 0 : _r.data) !== null && _s !== void 0 ? _s : {};
|
|
368
|
+
headers = (_u = (_t = err.response) === null || _t === void 0 ? void 0 : _t.headers) !== null && _u !== void 0 ? _u : {};
|
|
369
|
+
_w.label = 1;
|
|
370
|
+
case 1:
|
|
371
|
+
_w.trys.push([1, 5, , 6]);
|
|
372
|
+
res = data;
|
|
373
|
+
doLogin = (_v = rules.login) === null || _v === void 0 ? void 0 : _v.call(rules, data, status, headers);
|
|
374
|
+
if (!doLogin) return [3 /*break*/, 3];
|
|
375
|
+
return [4 /*yield*/, Promise.resolve(callbacks.login(res, headers))];
|
|
376
|
+
case 2:
|
|
377
|
+
res = _w.sent();
|
|
378
|
+
ResPromise._resolve(res);
|
|
379
|
+
return [3 /*break*/, 4];
|
|
380
|
+
case 3:
|
|
381
|
+
errorHandler(err, headers);
|
|
382
|
+
_w.label = 4;
|
|
383
|
+
case 4: return [3 /*break*/, 6];
|
|
384
|
+
case 5:
|
|
385
|
+
e_3 = _w.sent();
|
|
386
|
+
errorHandler(e_3, headers);
|
|
387
|
+
return [3 /*break*/, 6];
|
|
388
|
+
case 6: return [2 /*return*/];
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}); });
|
|
392
|
+
});
|
|
393
|
+
return ResPromise.promiseWrapper;
|
|
394
|
+
};
|
|
395
|
+
return Request;
|
|
396
|
+
}());var _a = new Request({
|
|
397
|
+
isSuccess: function (res, status) { return status === 200 && res.code == 0; },
|
|
398
|
+
isLogin: function (res, status) { return status === 401 || res.code == 401; },
|
|
399
|
+
timeout: 10000
|
|
400
|
+
}), request = _a.request, setting = _a.setting, axios = _a.axios;
|
|
401
|
+
function get(url, options) {
|
|
402
|
+
return request(__assign(__assign({}, options), { method: 'get', url: url }));
|
|
403
|
+
}
|
|
404
|
+
function post(url, data, options) {
|
|
405
|
+
return request(__assign(__assign({}, options), { method: 'post', url: url, data: data, headers: __assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));
|
|
406
|
+
}
|
|
407
|
+
function postForm(url, data, options) {
|
|
408
|
+
return request(__assign(__assign({}, options), { method: 'post', url: url, data: data, headers: __assign({ 'Content-Type': 'multipart/form-data' }, options === null || options === void 0 ? void 0 : options.headers) }));
|
|
409
|
+
}
|
|
410
|
+
function put(url, data, options) {
|
|
411
|
+
return request(__assign(__assign({}, options), { method: 'put', url: url, data: data, headers: __assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));
|
|
412
|
+
}
|
|
413
|
+
function patch(url, data, options) {
|
|
414
|
+
return request(__assign(__assign({}, options), { method: 'patch', url: url, data: data, headers: __assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));
|
|
415
|
+
}
|
|
416
|
+
function del(url, data, options) {
|
|
417
|
+
return request(__assign(__assign({}, options), { method: 'delete', url: url, data: data, headers: __assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));
|
|
418
|
+
}
|
|
419
|
+
function setInitConfig(config) {
|
|
420
|
+
var baseURL = config.baseURL, rest = __rest(config, ["baseURL"]);
|
|
421
|
+
axios.defaults.baseURL = baseURL || (typeof window !== 'undefined' ? location.origin : void 0);
|
|
422
|
+
setting(rest);
|
|
423
|
+
}
|
|
424
|
+
var axiosStatic = axios;
|
|
425
|
+
var index = {
|
|
426
|
+
get: get,
|
|
427
|
+
post: post,
|
|
428
|
+
postForm: postForm,
|
|
429
|
+
del: del,
|
|
430
|
+
put: put,
|
|
431
|
+
patch: patch,
|
|
432
|
+
setInitConfig: setInitConfig,
|
|
433
|
+
axiosStatic: axiosStatic
|
|
434
|
+
};exports.axiosStatic=axiosStatic;exports.default=index;exports.del=del;exports.get=get;exports.patch=patch;exports.post=post;exports.postForm=postForm;exports.put=put;exports.setInitConfig=setInitConfig;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { api } from './api/index.js';
|
|
2
|
+
export { api } from './api/index.js';
|
|
3
|
+
export { default as client } from './client/index.js';
|
|
4
|
+
export * from './utils/index.js';
|
|
5
|
+
export type { AxiosStatic } from 'axios';
|
|
6
|
+
export type { InitConfig, Options, ParsedError, PromiseWrapper } from './client/core';
|
|
7
|
+
declare const _default: {
|
|
8
|
+
api: typeof api;
|
|
9
|
+
client: {
|
|
10
|
+
get: typeof import("./client/index.js").get;
|
|
11
|
+
post: typeof import("./client/index.js").post;
|
|
12
|
+
postForm: typeof import("./client/index.js").postForm;
|
|
13
|
+
del: typeof import("./client/index.js").del;
|
|
14
|
+
put: typeof import("./client/index.js").put;
|
|
15
|
+
patch: typeof import("./client/index.js").patch;
|
|
16
|
+
setInitConfig: typeof import("./client/index.js").setInitConfig;
|
|
17
|
+
axiosStatic: import("axios").AxiosStatic;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export default _default;
|