@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.
Files changed (65) hide show
  1. package/.claude/settings.local.json +14 -0
  2. package/CLAUDE.md +77 -0
  3. package/README.md +600 -0
  4. package/es/api/accounts/index.d.ts +20 -0
  5. package/es/api/accounts/types.d.ts +9 -0
  6. package/es/api/chain/index.d.ts +12 -0
  7. package/es/api/chain/types.d.ts +3 -0
  8. package/es/api/checkpoints/index.d.ts +26 -0
  9. package/es/api/checkpoints/types.d.ts +33 -0
  10. package/es/api/constants.d.ts +9 -0
  11. package/es/api/index.d.ts +31 -0
  12. package/es/api/index.js +593 -0
  13. package/es/api/state/index.d.ts +12 -0
  14. package/es/api/state/types.d.ts +7 -0
  15. package/es/api/tokens/index.d.ts +62 -0
  16. package/es/api/tokens/types.d.ts +130 -0
  17. package/es/api/transactions/index.d.ts +35 -0
  18. package/es/api/transactions/types.d.ts +25 -0
  19. package/es/api/types.d.ts +18 -0
  20. package/es/client/core.d.ts +109 -0
  21. package/es/client/index.d.ts +21 -0
  22. package/es/client/index.js +373 -0
  23. package/es/index.d.ts +20 -0
  24. package/es/index.js +1490 -0
  25. package/es/utils/address.d.ts +15 -0
  26. package/es/utils/index.d.ts +6 -0
  27. package/es/utils/index.js +841 -0
  28. package/es/utils/interface.d.ts +7 -0
  29. package/es/utils/safePromise.d.ts +4 -0
  30. package/es/utils/sign.d.ts +15 -0
  31. package/es/utils/txHash.d.ts +2 -0
  32. package/es/utils/typeof.d.ts +2 -0
  33. package/lib/api/accounts/index.d.ts +20 -0
  34. package/lib/api/accounts/types.d.ts +9 -0
  35. package/lib/api/chain/index.d.ts +12 -0
  36. package/lib/api/chain/types.d.ts +3 -0
  37. package/lib/api/checkpoints/index.d.ts +26 -0
  38. package/lib/api/checkpoints/types.d.ts +33 -0
  39. package/lib/api/constants.d.ts +9 -0
  40. package/lib/api/index.d.ts +31 -0
  41. package/lib/api/index.js +692 -0
  42. package/lib/api/state/index.d.ts +12 -0
  43. package/lib/api/state/types.d.ts +7 -0
  44. package/lib/api/tokens/index.d.ts +62 -0
  45. package/lib/api/tokens/types.d.ts +130 -0
  46. package/lib/api/transactions/index.d.ts +35 -0
  47. package/lib/api/transactions/types.d.ts +25 -0
  48. package/lib/api/types.d.ts +18 -0
  49. package/lib/client/core.d.ts +109 -0
  50. package/lib/client/index.d.ts +21 -0
  51. package/lib/client/index.js +434 -0
  52. package/lib/index.d.ts +20 -0
  53. package/lib/index.js +1591 -0
  54. package/lib/utils/address.d.ts +15 -0
  55. package/lib/utils/index.d.ts +6 -0
  56. package/lib/utils/index.js +937 -0
  57. package/lib/utils/interface.d.ts +7 -0
  58. package/lib/utils/safePromise.d.ts +4 -0
  59. package/lib/utils/sign.d.ts +15 -0
  60. package/lib/utils/txHash.d.ts +2 -0
  61. package/lib/utils/typeof.d.ts +2 -0
  62. package/package.json +111 -0
  63. package/public/favicon.ico +0 -0
  64. package/public/logo.png +0 -0
  65. package/umd/1money-protocol-ts-sdk.min.js +2 -0
@@ -0,0 +1,373 @@
1
+ import axios$1 from'axios';import'viem';import'@ethereumjs/rlp';function _typeof(ele) {
2
+ if (typeof ele !== 'object')
3
+ return (typeof ele).toLowerCase();
4
+ const typeStr = Object.prototype.toString.call(ele);
5
+ return typeStr.slice(8, typeStr.length - 1).toLowerCase();
6
+ }class ResponsePromise {
7
+ _success;
8
+ _failure;
9
+ _error;
10
+ _login;
11
+ _timeout;
12
+ _rest;
13
+ _resolve;
14
+ _reject;
15
+ promiseWrapper;
16
+ _restScope;
17
+ constructor() {
18
+ this.promiseWrapper = new Promise((resolve, reject) => {
19
+ this._resolve = resolve;
20
+ this._reject = reject;
21
+ });
22
+ this._restScope = ['success', 'failure', 'error', 'login', 'timeout'];
23
+ for (let i = 0; i < this._restScope.length; i++) {
24
+ const method = this._restScope[i];
25
+ if (!method)
26
+ continue;
27
+ this.promiseWrapper[method] = _callback => {
28
+ // @ts-ignore
29
+ if (typeof _callback === 'function')
30
+ this[`_${method}`] = _callback;
31
+ const wrapper = this.promiseWrapper;
32
+ delete wrapper[method];
33
+ const methods = this._restScope.slice();
34
+ methods.splice(methods.indexOf(method), 1);
35
+ if (methods.every(_m => !!_m && !wrapper[_m]))
36
+ delete wrapper.rest;
37
+ return wrapper;
38
+ };
39
+ }
40
+ this.promiseWrapper.rest = (onRest, scope) => {
41
+ // @ts-ignore
42
+ if (typeof onRest === 'function')
43
+ this._rest = onRest;
44
+ const wrapper = this.promiseWrapper;
45
+ delete wrapper.rest;
46
+ this._restScope = scope || this._restScope;
47
+ // @ts-ignore
48
+ if (this._restScope.length === 0) {
49
+ console.warn('[1Money client]: The ".rest(cb, scope)" scope is empty and will never be triggered!');
50
+ }
51
+ else {
52
+ let deletedCounter = 0;
53
+ this._restScope.forEach(method => {
54
+ if (!method)
55
+ return;
56
+ if (wrapper[method]) {
57
+ delete wrapper[method];
58
+ }
59
+ else {
60
+ deletedCounter++;
61
+ }
62
+ });
63
+ if (deletedCounter === this._restScope.length) {
64
+ console.warn(`[1Money client]: The "${this._restScope.join(', ')}" had been called and the "rest" will never be triggered!`);
65
+ }
66
+ }
67
+ return wrapper;
68
+ };
69
+ }
70
+ }
71
+ class Request {
72
+ _config;
73
+ axios;
74
+ constructor(config) {
75
+ this._config = config || {};
76
+ this.axios = axios$1;
77
+ this.parseError = this.parseError.bind(this);
78
+ this.setting = this.setting.bind(this);
79
+ this.request = this.request.bind(this);
80
+ }
81
+ parseError(err) {
82
+ if (typeof err === 'string')
83
+ err = new Error(err);
84
+ // Ensure we have a valid error object
85
+ if (!err || (_typeof(err) !== 'object' && _typeof(err) !== 'error')) {
86
+ err = new Error('Unknown error occurred');
87
+ }
88
+ const name = err?.name ?? 'Error';
89
+ const message = err?.message ?? err?.toString?.() ?? 'Unknown error';
90
+ const stack = err?.stack ?? '';
91
+ const status = err?.response?.status ?? 500;
92
+ const data = err?.response?.data ?? undefined;
93
+ // Validate status code
94
+ if (typeof status !== 'number' || status < 100 || status > 599) {
95
+ return {
96
+ name: 'InvalidStatusError',
97
+ message: 'Invalid HTTP status code',
98
+ stack,
99
+ status: 500,
100
+ data
101
+ };
102
+ }
103
+ return {
104
+ name,
105
+ message,
106
+ stack,
107
+ status,
108
+ data
109
+ };
110
+ }
111
+ setting(config) {
112
+ if (!config)
113
+ return console.warn('[1Money client]: setting method required correct parameters!');
114
+ this._config = { ...this._config, ...config };
115
+ }
116
+ request(options) {
117
+ // Set default security headers
118
+ options.withCredentials = typeof options.withCredentials === 'boolean' ? options.withCredentials : true;
119
+ options.headers = options.headers || {};
120
+ options.headers['Accept'] = options.headers['Accept'] || '*/*';
121
+ options.headers['X-Requested-With'] = options.headers['X-Requested-With'] || 'XMLHttpRequest';
122
+ options.headers['X-Content-Type-Options'] = options.headers['X-Content-Type-Options'] || 'nosniff';
123
+ const { onSuccess: initOnSuccess, onFailure: initOnFailure, onLogin: initOnLogin, onError: initOnError, onTimeout: initOnTimeout, isSuccess: initIsSuccess, isLogin: initIsLogin, timeout: initTimeout, } = this._config;
124
+ const { onSuccess, onFailure, onLogin, onError, onTimeout, isSuccess, isLogin, timeout, } = options;
125
+ const rules = {
126
+ success: isSuccess ?? initIsSuccess,
127
+ login: isLogin ?? initIsLogin,
128
+ };
129
+ const ResPromise = new ResponsePromise();
130
+ Promise.resolve().then(() => {
131
+ const callbacks = {
132
+ success: ResPromise._success ??
133
+ (~ResPromise._restScope.indexOf('success') ? ResPromise._rest : undefined) ??
134
+ onSuccess ??
135
+ initOnSuccess ??
136
+ ((r, h) => r),
137
+ failure: ResPromise._failure ??
138
+ (~ResPromise._restScope.indexOf('failure') ? ResPromise._rest : undefined) ??
139
+ onFailure ??
140
+ initOnFailure ??
141
+ ((v, h) => v),
142
+ error: ResPromise._error ??
143
+ (~ResPromise._restScope.indexOf('error') ? ResPromise._rest : undefined) ??
144
+ onError ??
145
+ initOnError ??
146
+ ((v, h) => v),
147
+ login: ResPromise._login ??
148
+ (~ResPromise._restScope.indexOf('login') ? ResPromise._rest : undefined) ??
149
+ onLogin ??
150
+ initOnLogin ??
151
+ ((v, h) => v),
152
+ timeout: ResPromise._timeout ??
153
+ (~ResPromise._restScope.indexOf('timeout') ? ResPromise._rest : undefined) ??
154
+ onTimeout ??
155
+ initOnTimeout ??
156
+ ((v, h) => v),
157
+ };
158
+ const existedHandler = {
159
+ success: !!(ResPromise._success ||
160
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('success')) ||
161
+ onSuccess ||
162
+ initOnSuccess),
163
+ failure: !!(ResPromise._failure ||
164
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('failure')) ||
165
+ onFailure ||
166
+ initOnFailure),
167
+ login: !!(ResPromise._login ||
168
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('login')) ||
169
+ onLogin ||
170
+ initOnLogin),
171
+ error: !!(ResPromise._error ||
172
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('error')) ||
173
+ onError ||
174
+ initOnError),
175
+ timeout: !!(ResPromise._timeout ||
176
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('timeout')) ||
177
+ onTimeout ||
178
+ initOnTimeout),
179
+ };
180
+ const existedChainHandler = {
181
+ success: !!(ResPromise._success ||
182
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('success'))),
183
+ failure: !!(ResPromise._failure ||
184
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('failure'))),
185
+ login: !!(ResPromise._login ||
186
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('login'))),
187
+ error: !!(ResPromise._error ||
188
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('error'))),
189
+ timeout: !!(ResPromise._timeout ||
190
+ (ResPromise._rest && ~ResPromise._restScope.indexOf('timeout'))),
191
+ };
192
+ const errorHandler = async (e, headers) => {
193
+ try {
194
+ let err = this.parseError(e);
195
+ const result = await Promise.resolve(callbacks.error(err, headers));
196
+ if (existedChainHandler.error)
197
+ err = result;
198
+ existedHandler.error ? ResPromise._resolve(err) : ResPromise._reject(err);
199
+ }
200
+ catch (_e) {
201
+ ResPromise._reject(this.parseError(_e));
202
+ }
203
+ };
204
+ let timer = null;
205
+ let isTimeout = false;
206
+ const _timeout = timeout ?? initTimeout;
207
+ // Cleanup function for timeout
208
+ const cleanup = () => {
209
+ if (timer !== null) {
210
+ clearTimeout(timer);
211
+ timer = null;
212
+ }
213
+ };
214
+ if (_timeout) {
215
+ timer = setTimeout(async () => {
216
+ try {
217
+ isTimeout = true;
218
+ cleanup();
219
+ let err = this.parseError('timeout');
220
+ // @ts-ignore
221
+ const res = await Promise.resolve(callbacks.timeout(err, options.headers ?? {}));
222
+ if (existedChainHandler.timeout)
223
+ err = res;
224
+ existedHandler.timeout ? ResPromise._resolve(err) : ResPromise._reject(err);
225
+ }
226
+ catch (e) {
227
+ errorHandler(e, options.headers ?? {});
228
+ }
229
+ }, _timeout);
230
+ }
231
+ this.axios(options).then(async (response) => {
232
+ if (isTimeout)
233
+ return;
234
+ cleanup();
235
+ const { status, data, headers } = response;
236
+ try {
237
+ const doSuccess = rules.success?.(data, status, headers);
238
+ const doLogin = rules.login?.(data, status, headers);
239
+ let res = data;
240
+ if (doLogin) {
241
+ const result = await Promise.resolve(callbacks.login(data, headers));
242
+ if (existedChainHandler.login)
243
+ res = result;
244
+ }
245
+ else if (doSuccess) {
246
+ const result = await Promise.resolve(callbacks.success(data, headers));
247
+ if (existedChainHandler.success)
248
+ res = result;
249
+ }
250
+ else {
251
+ const result = await Promise.resolve(callbacks.failure(data, headers));
252
+ if (existedChainHandler.failure)
253
+ res = result;
254
+ }
255
+ ResPromise._resolve(res);
256
+ }
257
+ catch (e) {
258
+ errorHandler(e, headers);
259
+ }
260
+ }).catch(async (err) => {
261
+ if (isTimeout)
262
+ return;
263
+ cleanup();
264
+ console.error(`[1Money client]: Error(${err.status ?? 500}, ${err.code ?? 'UNKNOWN'}), Message: ${err.message}, Config: ${err.config?.method}, ${err.config?.baseURL ?? ''}${err.config?.url ?? ''}, ${JSON.stringify(err.config?.headers ?? {})}, Request: ${JSON.stringify(err.config?.data ?? {})};`);
265
+ const status = err.response?.status ?? 500;
266
+ const data = err.response?.data ?? {};
267
+ const headers = err.response?.headers ?? {};
268
+ try {
269
+ let res = data;
270
+ const doLogin = rules.login?.(data, status, headers);
271
+ if (doLogin) {
272
+ res = await Promise.resolve(callbacks.login(res, headers));
273
+ ResPromise._resolve(res);
274
+ }
275
+ else {
276
+ errorHandler(err, headers);
277
+ }
278
+ }
279
+ catch (e) {
280
+ errorHandler(e, headers);
281
+ }
282
+ });
283
+ });
284
+ return ResPromise.promiseWrapper;
285
+ }
286
+ }const { request, setting, axios } = new Request({
287
+ isSuccess: (res, status) => status === 200 && res.code == 0,
288
+ isLogin: (res, status) => status === 401 || res.code == 401,
289
+ timeout: 10000
290
+ });
291
+ function get(url, options) {
292
+ return request({
293
+ ...options,
294
+ method: 'get',
295
+ url
296
+ });
297
+ }
298
+ function post(url, data, options) {
299
+ return request({
300
+ ...options,
301
+ method: 'post',
302
+ url,
303
+ data,
304
+ headers: {
305
+ 'Content-Type': 'application/json',
306
+ ...options?.headers,
307
+ }
308
+ });
309
+ }
310
+ function postForm(url, data, options) {
311
+ return request({
312
+ ...options,
313
+ method: 'post',
314
+ url,
315
+ data,
316
+ headers: {
317
+ 'Content-Type': 'multipart/form-data',
318
+ ...options?.headers,
319
+ }
320
+ });
321
+ }
322
+ function put(url, data, options) {
323
+ return request({
324
+ ...options,
325
+ method: 'put',
326
+ url,
327
+ data,
328
+ headers: {
329
+ 'Content-Type': 'application/json',
330
+ ...options?.headers,
331
+ }
332
+ });
333
+ }
334
+ function patch(url, data, options) {
335
+ return request({
336
+ ...options,
337
+ method: 'patch',
338
+ url,
339
+ data,
340
+ headers: {
341
+ 'Content-Type': 'application/json',
342
+ ...options?.headers,
343
+ }
344
+ });
345
+ }
346
+ function del(url, data, options) {
347
+ return request({
348
+ ...options,
349
+ method: 'delete',
350
+ url,
351
+ data,
352
+ headers: {
353
+ 'Content-Type': 'application/json',
354
+ ...options?.headers,
355
+ }
356
+ });
357
+ }
358
+ function setInitConfig(config) {
359
+ const { baseURL, ...rest } = config;
360
+ axios.defaults.baseURL = baseURL || (typeof window !== 'undefined' ? location.origin : void 0);
361
+ setting(rest);
362
+ }
363
+ const axiosStatic = axios;
364
+ var index = {
365
+ get,
366
+ post,
367
+ postForm,
368
+ del,
369
+ put,
370
+ patch,
371
+ setInitConfig,
372
+ axiosStatic
373
+ };export{axiosStatic,index as default,del,get,patch,post,postForm,put,setInitConfig};
package/es/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;