@injectivelabs/utils 1.20.62 → 1.20.63
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/cjs/HttpRestClient-BIcHe0ig.cjs +490 -0
- package/dist/cjs/HttpRestClient-CtRnRmrM.d.cts +163 -0
- package/dist/cjs/{LocalStorage-DRuofBv_.cjs → LocalStorage-BwUKa0QV.cjs} +1 -1
- package/dist/cjs/{Status-C1EkdFOa.cjs → Status-DfWX_f_H.cjs} +1 -1
- package/dist/cjs/http.cjs +2 -2
- package/dist/cjs/http.d.cts +1 -1
- package/dist/cjs/index.cjs +11 -4
- package/dist/cjs/index.d.cts +2 -2
- package/dist/cjs/light.cjs +1 -1
- package/dist/cjs/status.cjs +2 -2
- package/dist/cjs/storage.cjs +2 -2
- package/dist/esm/HttpRestClient-00SidMhA.d.ts +163 -0
- package/dist/esm/HttpRestClient-CWyiACSA.js +435 -0
- package/dist/esm/{LocalStorage-Cpe5wG8z.js → LocalStorage-CVCXt4aX.js} +1 -1
- package/dist/esm/{Status-eHtiFREA.js → Status-BhM2hdSA.js} +1 -1
- package/dist/esm/http.d.ts +1 -1
- package/dist/esm/http.js +2 -2
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +5 -5
- package/dist/esm/light.js +1 -1
- package/dist/esm/status.js +2 -2
- package/dist/esm/storage.js +2 -2
- package/package.json +4 -4
- package/dist/cjs/HttpRestClient-C0p7w6ht.d.cts +0 -29
- package/dist/cjs/HttpRestClient-C761J3nA.cjs +0 -138
- package/dist/esm/HttpRestClient-B-UvB9KB.d.ts +0 -29
- package/dist/esm/HttpRestClient-CsKkn8lo.js +0 -125
- /package/dist/cjs/{defineProperty-CImP5zIM.cjs → defineProperty-DAtJs5JQ.cjs} +0 -0
- /package/dist/esm/{defineProperty-Zp25b8Cm.js → defineProperty-DiY4Kzl4.js} +0 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
require('./
|
|
2
|
-
|
|
1
|
+
const require_HttpRestClient = require('./HttpRestClient-BIcHe0ig.cjs');
|
|
2
|
+
require('./defineProperty-DAtJs5JQ.cjs');
|
|
3
3
|
const require_BigNumber = require('./BigNumber-DbDA4_xA.cjs');
|
|
4
4
|
const require_time = require('./time-CPSnQ8xf.cjs');
|
|
5
5
|
const require_constants = require('./constants-BdaegCnI.cjs');
|
|
6
6
|
const require_helpers = require('./helpers-BW4oKV4e.cjs');
|
|
7
7
|
const require_numbers = require('./numbers-BRXzNJ1I.cjs');
|
|
8
8
|
const require_formatters = require('./formatters-CBhRviK1.cjs');
|
|
9
|
-
const require_Status = require('./Status-
|
|
10
|
-
const require_LocalStorage = require('./LocalStorage-
|
|
9
|
+
const require_Status = require('./Status-DfWX_f_H.cjs');
|
|
10
|
+
const require_LocalStorage = require('./LocalStorage-BwUKa0QV.cjs');
|
|
11
11
|
const require_BigNumberInBase = require('./BigNumberInBase-CVKbAXw6.cjs');
|
|
12
12
|
|
|
13
|
+
exports.AbortError = require_HttpRestClient.AbortError;
|
|
13
14
|
exports.BigNumber = require_BigNumber.BigNumber_default;
|
|
14
15
|
exports.BigNumberInBase = require_BigNumberInBase.BigNumberInBase;
|
|
15
16
|
exports.BigNumberInWei = require_BigNumberInBase.BigNumberInWei;
|
|
@@ -36,10 +37,13 @@ exports.LocalStorage = require_LocalStorage.LocalStorage;
|
|
|
36
37
|
exports.Status = require_Status.Status;
|
|
37
38
|
exports.StatusType = require_Status.StatusType;
|
|
38
39
|
exports.ZERO_ADDRESS = require_constants.ZERO_ADDRESS;
|
|
40
|
+
exports.abortableSleep = require_HttpRestClient.abortableSleep;
|
|
39
41
|
exports.awaitAll = require_helpers.awaitAll;
|
|
40
42
|
exports.awaitForAll = require_helpers.awaitForAll;
|
|
41
43
|
exports.capitalize = require_formatters.capitalize;
|
|
44
|
+
exports.combineAbortSignals = require_HttpRestClient.combineAbortSignals;
|
|
42
45
|
exports.convertTimestampToMilliseconds = require_time.convertTimestampToMilliseconds;
|
|
46
|
+
exports.createAbortError = require_HttpRestClient.createAbortError;
|
|
43
47
|
exports.formatWalletAddress = require_formatters.formatWalletAddress;
|
|
44
48
|
exports.getBigNumberWithRounding = require_numbers.getBigNumberWithRounding;
|
|
45
49
|
exports.getDefaultStdFee = require_constants.getDefaultStdFee;
|
|
@@ -52,12 +56,15 @@ exports.getStdFeeForToken = require_helpers.getStdFeeForToken;
|
|
|
52
56
|
exports.getStdFeeFromObject = require_helpers.getStdFeeFromObject;
|
|
53
57
|
exports.getStdFeeFromString = require_helpers.getStdFeeFromString;
|
|
54
58
|
exports.getUTCDateFromTimestamp = require_time.getUTCDateFromTimestamp;
|
|
59
|
+
exports.isAbortError = require_HttpRestClient.isAbortError;
|
|
55
60
|
exports.past24Hours = require_time.past24Hours;
|
|
56
61
|
exports.pastDays = require_time.pastDays;
|
|
62
|
+
exports.raceWithAbort = require_HttpRestClient.raceWithAbort;
|
|
57
63
|
exports.sleep = require_helpers.sleep;
|
|
58
64
|
exports.snakeToPascal = require_formatters.snakeToPascal;
|
|
59
65
|
exports.splitArrayToChunks = require_helpers.splitArrayToChunks;
|
|
60
66
|
exports.splitArrayToChunksThrow = require_helpers.splitArrayToChunksThrow;
|
|
67
|
+
exports.throwIfAborted = require_HttpRestClient.throwIfAborted;
|
|
61
68
|
exports.toBigNumber = require_BigNumber.toBigNumber;
|
|
62
69
|
exports.toChainFormat = require_BigNumber.toChainFormat;
|
|
63
70
|
exports.toHumanReadable = require_BigNumber.toHumanReadable;
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -3,9 +3,9 @@ import { n as BigNumberInBase, t as BigNumberInWei } from "./BigNumberInWei-BMQp
|
|
|
3
3
|
import { _ as INJ_DENOM, a as DEFAULT_BRIDGE_FEE_PRICE, c as DEFAULT_GAS_LIMIT, d as DEFAULT_STD_FEE_BY_DENOM, f as DEFAULT_TIMESTAMP_TIMEOUT_MS, g as INJECTIVE_DENOM, h as DEFAULT_TX_POLL_INTERVAL_MS, i as DEFAULT_BRIDGE_FEE_DENOM, l as DEFAULT_GAS_PRICE, m as DEFAULT_TX_POLL_CALL_TIMEOUT_MS, n as DEFAULT_BLOCK_TIME_IN_SECONDS, o as DEFAULT_EXCHANGE_LIMIT, p as DEFAULT_TX_BLOCK_INCLUSION_TIMEOUT_IN_MS, r as DEFAULT_BRIDGE_FEE_AMOUNT, s as DEFAULT_FEE_DENOM, t as DEFAULT_BLOCK_TIMEOUT_HEIGHT, u as DEFAULT_IBC_GAS_LIMIT, v as ZERO_ADDRESS, y as getDefaultStdFee } from "./constants-BRub-WIk.cjs";
|
|
4
4
|
import { i as toPascalCase, n as formatWalletAddress, r as snakeToPascal, t as capitalize } from "./formatters-lY6KvxzC.cjs";
|
|
5
5
|
import { a as getStdFeeFromObject, c as splitArrayToChunks, i as getStdFeeForToken, l as splitArrayToChunksThrow, n as awaitForAll, o as getStdFeeFromString, r as getStdFee, s as sleep, t as awaitAll } from "./helpers-BmH3TkaY.cjs";
|
|
6
|
-
import { n as HttpClient, t as HttpRestClient } from "./HttpRestClient-
|
|
6
|
+
import { a as AbortErrorLike, c as combineAbortSignals, d as raceWithAbort, f as throwIfAborted, i as AbortError, l as createAbortError, n as RetryRequestConfig, o as RequestOptions, p as HttpClient, r as RetryOptions, s as abortableSleep, t as HttpRestClient, u as isAbortError } from "./HttpRestClient-CtRnRmrM.cjs";
|
|
7
7
|
import { a as past24Hours, c as tomorrow, i as getUTCDateFromTimestamp, n as getEndDateStringFromTimeInSeconds, o as pastDays, r as getSecondsInDay, s as todayInSeconds, t as convertTimestampToMilliseconds } from "./time-DQwWSv4e.cjs";
|
|
8
8
|
import { n as getExactDecimalsFromNumber, r as getSignificantDecimalsFromNumber, t as getBigNumberWithRounding } from "./numbers-CVJM893V.cjs";
|
|
9
9
|
import { n as StatusType, t as Status } from "./Status-lj0Wyw_3.cjs";
|
|
10
10
|
import { t as LocalStorage } from "./LocalStorage-Dqm913-z.cjs";
|
|
11
|
-
export { BigNumber, BigNumberInBase, BigNumberInWei, DEFAULT_BLOCK_TIMEOUT_HEIGHT, DEFAULT_BLOCK_TIME_IN_SECONDS, DEFAULT_BRIDGE_FEE_AMOUNT, DEFAULT_BRIDGE_FEE_DENOM, DEFAULT_BRIDGE_FEE_PRICE, DEFAULT_EXCHANGE_LIMIT, DEFAULT_FEE_DENOM, DEFAULT_GAS_LIMIT, DEFAULT_GAS_PRICE, DEFAULT_IBC_GAS_LIMIT, DEFAULT_STD_FEE_BY_DENOM, DEFAULT_TIMESTAMP_TIMEOUT_MS, DEFAULT_TX_BLOCK_INCLUSION_TIMEOUT_IN_MS, DEFAULT_TX_POLL_CALL_TIMEOUT_MS, DEFAULT_TX_POLL_INTERVAL_MS, HttpClient, HttpRestClient, INJECTIVE_DENOM, INJ_DENOM, LocalStorage, Status, StatusType, ZERO_ADDRESS, awaitAll, awaitForAll, capitalize, convertTimestampToMilliseconds, formatWalletAddress, getBigNumberWithRounding, getDefaultStdFee, getEndDateStringFromTimeInSeconds, getExactDecimalsFromNumber, getSecondsInDay, getSignificantDecimalsFromNumber, getStdFee, getStdFeeForToken, getStdFeeFromObject, getStdFeeFromString, getUTCDateFromTimestamp, past24Hours, pastDays, sleep, snakeToPascal, splitArrayToChunks, splitArrayToChunksThrow, toBigNumber, toChainFormat, toHumanReadable, toPascalCase, todayInSeconds, tomorrow };
|
|
11
|
+
export { AbortError, AbortErrorLike, BigNumber, BigNumberInBase, BigNumberInWei, DEFAULT_BLOCK_TIMEOUT_HEIGHT, DEFAULT_BLOCK_TIME_IN_SECONDS, DEFAULT_BRIDGE_FEE_AMOUNT, DEFAULT_BRIDGE_FEE_DENOM, DEFAULT_BRIDGE_FEE_PRICE, DEFAULT_EXCHANGE_LIMIT, DEFAULT_FEE_DENOM, DEFAULT_GAS_LIMIT, DEFAULT_GAS_PRICE, DEFAULT_IBC_GAS_LIMIT, DEFAULT_STD_FEE_BY_DENOM, DEFAULT_TIMESTAMP_TIMEOUT_MS, DEFAULT_TX_BLOCK_INCLUSION_TIMEOUT_IN_MS, DEFAULT_TX_POLL_CALL_TIMEOUT_MS, DEFAULT_TX_POLL_INTERVAL_MS, HttpClient, HttpRestClient, INJECTIVE_DENOM, INJ_DENOM, LocalStorage, RequestOptions, RetryOptions, type RetryRequestConfig, Status, StatusType, ZERO_ADDRESS, abortableSleep, awaitAll, awaitForAll, capitalize, combineAbortSignals, convertTimestampToMilliseconds, createAbortError, formatWalletAddress, getBigNumberWithRounding, getDefaultStdFee, getEndDateStringFromTimeInSeconds, getExactDecimalsFromNumber, getSecondsInDay, getSignificantDecimalsFromNumber, getStdFee, getStdFeeForToken, getStdFeeFromObject, getStdFeeFromString, getUTCDateFromTimestamp, isAbortError, past24Hours, pastDays, raceWithAbort, sleep, snakeToPascal, splitArrayToChunks, splitArrayToChunksThrow, throwIfAborted, toBigNumber, toChainFormat, toHumanReadable, toPascalCase, todayInSeconds, tomorrow };
|
package/dist/cjs/light.cjs
CHANGED
package/dist/cjs/status.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require('./defineProperty-
|
|
2
|
-
const require_Status = require('./Status-
|
|
1
|
+
require('./defineProperty-DAtJs5JQ.cjs');
|
|
2
|
+
const require_Status = require('./Status-DfWX_f_H.cjs');
|
|
3
3
|
|
|
4
4
|
exports.Status = require_Status.Status;
|
|
5
5
|
exports.StatusType = require_Status.StatusType;
|
package/dist/cjs/storage.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('./defineProperty-
|
|
2
|
-
const require_LocalStorage = require('./LocalStorage-
|
|
1
|
+
require('./defineProperty-DAtJs5JQ.cjs');
|
|
2
|
+
const require_LocalStorage = require('./LocalStorage-BwUKa0QV.cjs');
|
|
3
3
|
|
|
4
4
|
exports.LocalStorage = require_LocalStorage.LocalStorage;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from "axios";
|
|
2
|
+
|
|
3
|
+
//#region src/classes/HttpClient.d.ts
|
|
4
|
+
declare class HttpClient {
|
|
5
|
+
private readonly client;
|
|
6
|
+
private readonly abortSignal?;
|
|
7
|
+
private config;
|
|
8
|
+
constructor(endpoint: string, options?: Record<string, any>);
|
|
9
|
+
setConfig(config: AxiosRequestConfig): HttpClient;
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*
|
|
13
|
+
* Current constructor/config cancellation constraint, optionally combined
|
|
14
|
+
* with an operation-level signal. Callers must invoke `dispose` after the
|
|
15
|
+
* operation settles.
|
|
16
|
+
*/
|
|
17
|
+
getAbortSignal(signal?: AbortSignal, config?: AxiosRequestConfig): {
|
|
18
|
+
signal: AbortSignal | undefined;
|
|
19
|
+
dispose(): void;
|
|
20
|
+
};
|
|
21
|
+
private prepareRequest;
|
|
22
|
+
private executeRequest;
|
|
23
|
+
get<T, P>(endpoint: string, params?: T, config?: AxiosRequestConfig): Promise<P>;
|
|
24
|
+
post<T, P>(endpoint: string, data?: T, config?: AxiosRequestConfig): Promise<P>;
|
|
25
|
+
put<T, P>(endpoint: string, data?: T, config?: AxiosRequestConfig): Promise<P>;
|
|
26
|
+
delete<T, P>(endpoint: string, params?: T, config?: AxiosRequestConfig): Promise<P>;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/abort.d.ts
|
|
30
|
+
/**
|
|
31
|
+
* Optional cancellation options accepted by read/query APIs.
|
|
32
|
+
*/
|
|
33
|
+
interface RequestOptions {
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Local cancellation error shared by every cancellable SDK operation.
|
|
38
|
+
*
|
|
39
|
+
* The message is deliberately stable and generic so that callers can rely on
|
|
40
|
+
* `name`/`code`/`reason` instead of parsing messages, and so that a user
|
|
41
|
+
* provided reason can never be mistaken for an existing string-based error
|
|
42
|
+
* (for example a "not found" response).
|
|
43
|
+
*/
|
|
44
|
+
declare class AbortError extends Error {
|
|
45
|
+
readonly name: 'AbortError';
|
|
46
|
+
readonly code: 'ERR_CANCELED';
|
|
47
|
+
readonly reason: unknown;
|
|
48
|
+
constructor(reason?: unknown);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates an abort error, preserving the signal's abort reason when present.
|
|
52
|
+
*/
|
|
53
|
+
declare const createAbortError: (signal?: AbortSignal) => AbortError;
|
|
54
|
+
/** Structural because native and cross-package cancellation may be plain objects. */
|
|
55
|
+
type AbortErrorLike = ({
|
|
56
|
+
name: 'AbortError';
|
|
57
|
+
code?: unknown;
|
|
58
|
+
} | {
|
|
59
|
+
name?: unknown;
|
|
60
|
+
code: 'ERR_CANCELED';
|
|
61
|
+
}) & {
|
|
62
|
+
reason?: unknown;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Identifies local/SDK/documented abort-shaped errors.
|
|
66
|
+
*
|
|
67
|
+
* This intentionally does not treat gRPC status codes (`CANCELLED`,
|
|
68
|
+
* `ABORTED`, `DEADLINE_EXCEEDED`) or arbitrary server errors as cancellation.
|
|
69
|
+
*/
|
|
70
|
+
declare const isAbortError: (error: unknown) => error is AbortErrorLike;
|
|
71
|
+
/**
|
|
72
|
+
* Throws an {@link AbortError} when the signal has already been aborted.
|
|
73
|
+
*/
|
|
74
|
+
declare const throwIfAborted: (signal?: AbortSignal) => void;
|
|
75
|
+
/**
|
|
76
|
+
* Combines several optional signals into a single cancellation constraint.
|
|
77
|
+
*
|
|
78
|
+
* - No signal: returns `undefined` without creating controllers/listeners.
|
|
79
|
+
* - One signal: returns it as-is without adding listeners.
|
|
80
|
+
* - Multiple signals: returns a new controller signal that aborts with the
|
|
81
|
+
* first observed reason. Caller controllers are never aborted.
|
|
82
|
+
*
|
|
83
|
+
* `dispose` removes all listeners and is safe to call more than once.
|
|
84
|
+
*/
|
|
85
|
+
declare function combineAbortSignals(signals: readonly [AbortSignal, ...(AbortSignal | undefined)[]] | readonly [...(AbortSignal | undefined)[], AbortSignal]): {
|
|
86
|
+
signal: AbortSignal;
|
|
87
|
+
dispose(): void;
|
|
88
|
+
};
|
|
89
|
+
declare function combineAbortSignals(signals: readonly (AbortSignal | undefined)[]): {
|
|
90
|
+
signal: AbortSignal | undefined;
|
|
91
|
+
dispose(): void;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Sleeps for `ms`, rejecting with {@link AbortError} as soon as the signal is
|
|
95
|
+
* aborted. Timers and listeners are always cleaned up.
|
|
96
|
+
*/
|
|
97
|
+
declare const abortableSleep: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Waits for `promise`, but rejects promptly when `signal` is aborted.
|
|
100
|
+
*
|
|
101
|
+
* The eventual promise settlement is always observed, so a late rejection can
|
|
102
|
+
* never become unhandled. Late success/rejection cannot override an abort, and
|
|
103
|
+
* listeners are removed on every settlement path.
|
|
104
|
+
*/
|
|
105
|
+
declare const raceWithAbort: <T>(promise: PromiseLike<T>, signal?: AbortSignal) => Promise<T>;
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/retry.d.ts
|
|
108
|
+
/** Options shared by HTTP and gRPC retry operations. */
|
|
109
|
+
interface RetryOptions extends RequestOptions {
|
|
110
|
+
/** Maximum number of attempts, including the initial call. Defaults to 3. */
|
|
111
|
+
retries?: number;
|
|
112
|
+
/** Base backoff delay in milliseconds, multiplied by the attempt. Defaults to 1000. */
|
|
113
|
+
delay?: number;
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/classes/RetryRequestConfig.d.ts
|
|
117
|
+
declare const RETRY_REQUEST_CONFIG_BRAND: unique symbol;
|
|
118
|
+
/**
|
|
119
|
+
* Request config passed to callbacks in the options-object retry overload.
|
|
120
|
+
*
|
|
121
|
+
* It is a regular Axios request config that carries an internal cancellation
|
|
122
|
+
* scope. Forward it directly to HttpClient methods:
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* client.retry(
|
|
126
|
+
* (requestConfig) =>
|
|
127
|
+
* client.get(endpoint, params, requestConfig),
|
|
128
|
+
* options ?? {},
|
|
129
|
+
* )
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* The scoped signal pins cancellation for the whole retry operation. Later
|
|
133
|
+
* `setConfig` changes still apply to headers/auth/adapter/timeouts/params but
|
|
134
|
+
* cannot retarget the operation's cancellation.
|
|
135
|
+
*/
|
|
136
|
+
interface RetryRequestConfig extends AxiosRequestConfig {
|
|
137
|
+
readonly [RETRY_REQUEST_CONFIG_BRAND]?: {
|
|
138
|
+
readonly signal?: AbortSignal;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/classes/HttpRestClient.d.ts
|
|
143
|
+
/**
|
|
144
|
+
* @hidden
|
|
145
|
+
*/
|
|
146
|
+
declare class HttpRestClient {
|
|
147
|
+
protected client: HttpClient;
|
|
148
|
+
protected endpoint: string;
|
|
149
|
+
constructor(endpoint: string, options?: Record<string, any>);
|
|
150
|
+
setConfig(config: AxiosRequestConfig): HttpRestClient;
|
|
151
|
+
get<T>(endpoint: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<T>;
|
|
152
|
+
retry<TResponse>(httpCall: (requestConfig: RetryRequestConfig) => TResponse | PromiseLike<TResponse>, options: RetryOptions): Promise<TResponse>;
|
|
153
|
+
/** @deprecated Pass a RetryOptions object instead of positional retry settings. */
|
|
154
|
+
retry<TResponse>(httpCall: () => TResponse | PromiseLike<TResponse>, retries?: number, delay?: number, options?: RequestOptions): Promise<TResponse>;
|
|
155
|
+
/**
|
|
156
|
+
* @deprecated Retained for callers typed as Function by the legacy API.
|
|
157
|
+
* The options-object overload requires a typed request-config callback.
|
|
158
|
+
*/
|
|
159
|
+
retry<TResponse>(httpCall: Function, retries?: number, delay?: number, options?: RequestOptions): Promise<TResponse>;
|
|
160
|
+
post<T>(endpoint: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<T>;
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
163
|
+
export { AbortErrorLike as a, combineAbortSignals as c, raceWithAbort as d, throwIfAborted as f, AbortError as i, createAbortError as l, RetryRequestConfig as n, RequestOptions as o, HttpClient as p, RetryOptions as r, abortableSleep as s, HttpRestClient as t, isAbortError as u };
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { t as _defineProperty } from "./defineProperty-DiY4Kzl4.js";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { StatusCodes } from "http-status-codes";
|
|
4
|
+
import { HttpRequestException, HttpRequestMethod, UnspecifiedErrorCode } from "@injectivelabs/exceptions";
|
|
5
|
+
|
|
6
|
+
//#region src/classes/RetryRequestConfig.ts
|
|
7
|
+
const RETRY_REQUEST_CONFIG_BRAND = Symbol("@injectivelabs/utils/RetryRequestConfig");
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
const createRetryRequestConfig = (signal) => {
|
|
12
|
+
const config = { signal };
|
|
13
|
+
Object.defineProperty(config, RETRY_REQUEST_CONFIG_BRAND, {
|
|
14
|
+
configurable: false,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
value: Object.freeze({ signal }),
|
|
17
|
+
writable: false
|
|
18
|
+
});
|
|
19
|
+
return config;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*
|
|
24
|
+
* Returns undefined for ordinary request configs. A branded config with no
|
|
25
|
+
* signal intentionally means "do not compose the current config signal".
|
|
26
|
+
*/
|
|
27
|
+
const getRetryRequestCancellation = (config) => {
|
|
28
|
+
if (!config || typeof config !== "object") return;
|
|
29
|
+
return config[RETRY_REQUEST_CONFIG_BRAND];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*
|
|
34
|
+
* The brand is only meaningful to HttpClient. Remove it from the final Axios
|
|
35
|
+
* config so it is never forwarded to the transport.
|
|
36
|
+
*/
|
|
37
|
+
const clearRetryRequestConfigBrand = (config) => {
|
|
38
|
+
if (!config || typeof config !== "object") return;
|
|
39
|
+
Reflect.deleteProperty(config, RETRY_REQUEST_CONFIG_BRAND);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/abort.ts
|
|
44
|
+
const ABORT_ERROR_MESSAGE = "The operation was aborted";
|
|
45
|
+
/**
|
|
46
|
+
* Local cancellation error shared by every cancellable SDK operation.
|
|
47
|
+
*
|
|
48
|
+
* The message is deliberately stable and generic so that callers can rely on
|
|
49
|
+
* `name`/`code`/`reason` instead of parsing messages, and so that a user
|
|
50
|
+
* provided reason can never be mistaken for an existing string-based error
|
|
51
|
+
* (for example a "not found" response).
|
|
52
|
+
*/
|
|
53
|
+
var AbortError = class extends Error {
|
|
54
|
+
constructor(reason) {
|
|
55
|
+
super(ABORT_ERROR_MESSAGE);
|
|
56
|
+
_defineProperty(this, "name", "AbortError");
|
|
57
|
+
_defineProperty(this, "code", "ERR_CANCELED");
|
|
58
|
+
_defineProperty(this, "reason", void 0);
|
|
59
|
+
this.reason = reason;
|
|
60
|
+
if (reason !== void 0) Object.defineProperty(this, "cause", {
|
|
61
|
+
configurable: true,
|
|
62
|
+
enumerable: false,
|
|
63
|
+
value: reason,
|
|
64
|
+
writable: true
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Creates an abort error, preserving the signal's abort reason when present.
|
|
70
|
+
*/
|
|
71
|
+
const createAbortError = (signal) => {
|
|
72
|
+
return new AbortError((signal === null || signal === void 0 ? void 0 : signal.aborted) ? signal.reason : void 0);
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Identifies local/SDK/documented abort-shaped errors.
|
|
76
|
+
*
|
|
77
|
+
* This intentionally does not treat gRPC status codes (`CANCELLED`,
|
|
78
|
+
* `ABORTED`, `DEADLINE_EXCEEDED`) or arbitrary server errors as cancellation.
|
|
79
|
+
*/
|
|
80
|
+
const isAbortError = (error) => {
|
|
81
|
+
if (error instanceof AbortError) return true;
|
|
82
|
+
if (!error || typeof error !== "object") return false;
|
|
83
|
+
const candidate = error;
|
|
84
|
+
if (candidate.name === "AbortError") return true;
|
|
85
|
+
if (candidate.code === "ERR_CANCELED") return true;
|
|
86
|
+
return false;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Throws an {@link AbortError} when the signal has already been aborted.
|
|
90
|
+
*/
|
|
91
|
+
const throwIfAborted = (signal) => {
|
|
92
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) throw createAbortError(signal);
|
|
93
|
+
};
|
|
94
|
+
function combineAbortSignals(signals) {
|
|
95
|
+
const uniqueSignals = Array.from(new Set(signals.filter((signal) => Boolean(signal))));
|
|
96
|
+
const noop = () => {};
|
|
97
|
+
if (uniqueSignals.length === 0) return {
|
|
98
|
+
signal: void 0,
|
|
99
|
+
dispose: noop
|
|
100
|
+
};
|
|
101
|
+
if (uniqueSignals.length === 1) return {
|
|
102
|
+
signal: uniqueSignals[0],
|
|
103
|
+
dispose: noop
|
|
104
|
+
};
|
|
105
|
+
const controller = new AbortController();
|
|
106
|
+
const alreadyAborted = uniqueSignals.find((signal) => signal.aborted);
|
|
107
|
+
if (alreadyAborted) {
|
|
108
|
+
controller.abort(alreadyAborted.reason);
|
|
109
|
+
return {
|
|
110
|
+
signal: controller.signal,
|
|
111
|
+
dispose: noop
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
let disposed = false;
|
|
115
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
116
|
+
const dispose = () => {
|
|
117
|
+
if (disposed) return;
|
|
118
|
+
disposed = true;
|
|
119
|
+
listeners.forEach((listener, signal) => {
|
|
120
|
+
signal.removeEventListener("abort", listener);
|
|
121
|
+
});
|
|
122
|
+
listeners.clear();
|
|
123
|
+
};
|
|
124
|
+
uniqueSignals.forEach((sourceSignal) => {
|
|
125
|
+
const listener = () => {
|
|
126
|
+
if (controller.signal.aborted) return;
|
|
127
|
+
dispose();
|
|
128
|
+
controller.abort(sourceSignal.reason);
|
|
129
|
+
};
|
|
130
|
+
listeners.set(sourceSignal, listener);
|
|
131
|
+
sourceSignal.addEventListener("abort", listener, { once: true });
|
|
132
|
+
});
|
|
133
|
+
const abortedDuringSetup = uniqueSignals.find((signal) => signal.aborted);
|
|
134
|
+
if (abortedDuringSetup && !controller.signal.aborted) {
|
|
135
|
+
dispose();
|
|
136
|
+
controller.abort(abortedDuringSetup.reason);
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
signal: controller.signal,
|
|
140
|
+
dispose
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Sleeps for `ms`, rejecting with {@link AbortError} as soon as the signal is
|
|
145
|
+
* aborted. Timers and listeners are always cleaned up.
|
|
146
|
+
*/
|
|
147
|
+
const abortableSleep = (ms, signal) => {
|
|
148
|
+
if (!signal) return new Promise((resolve) => {
|
|
149
|
+
setTimeout(resolve, ms);
|
|
150
|
+
});
|
|
151
|
+
if (signal.aborted) return Promise.reject(createAbortError(signal));
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
let timer;
|
|
154
|
+
let settled = false;
|
|
155
|
+
const onAbort = () => {
|
|
156
|
+
if (settled) return;
|
|
157
|
+
settled = true;
|
|
158
|
+
if (timer !== void 0) {
|
|
159
|
+
clearTimeout(timer);
|
|
160
|
+
timer = void 0;
|
|
161
|
+
}
|
|
162
|
+
signal.removeEventListener("abort", onAbort);
|
|
163
|
+
reject(createAbortError(signal));
|
|
164
|
+
};
|
|
165
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
166
|
+
if (signal.aborted) {
|
|
167
|
+
onAbort();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
timer = setTimeout(() => {
|
|
171
|
+
if (settled) return;
|
|
172
|
+
settled = true;
|
|
173
|
+
signal.removeEventListener("abort", onAbort);
|
|
174
|
+
resolve();
|
|
175
|
+
}, ms);
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Waits for `promise`, but rejects promptly when `signal` is aborted.
|
|
180
|
+
*
|
|
181
|
+
* The eventual promise settlement is always observed, so a late rejection can
|
|
182
|
+
* never become unhandled. Late success/rejection cannot override an abort, and
|
|
183
|
+
* listeners are removed on every settlement path.
|
|
184
|
+
*/
|
|
185
|
+
const raceWithAbort = (promise, signal) => {
|
|
186
|
+
const observedPromise = Promise.resolve(promise);
|
|
187
|
+
if (!signal) return observedPromise;
|
|
188
|
+
if (signal.aborted) {
|
|
189
|
+
observedPromise.then(() => {}, () => {});
|
|
190
|
+
return Promise.reject(createAbortError(signal));
|
|
191
|
+
}
|
|
192
|
+
return new Promise((resolve, reject) => {
|
|
193
|
+
let settled = false;
|
|
194
|
+
const onAbort = () => {
|
|
195
|
+
if (settled) return;
|
|
196
|
+
settled = true;
|
|
197
|
+
signal.removeEventListener("abort", onAbort);
|
|
198
|
+
reject(createAbortError(signal));
|
|
199
|
+
};
|
|
200
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
201
|
+
observedPromise.then((value) => {
|
|
202
|
+
if (settled) return;
|
|
203
|
+
settled = true;
|
|
204
|
+
signal.removeEventListener("abort", onAbort);
|
|
205
|
+
resolve(value);
|
|
206
|
+
}, (error) => {
|
|
207
|
+
if (settled) return;
|
|
208
|
+
settled = true;
|
|
209
|
+
signal.removeEventListener("abort", onAbort);
|
|
210
|
+
reject(error);
|
|
211
|
+
});
|
|
212
|
+
if (signal.aborted) onAbort();
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/classes/HttpClient.ts
|
|
218
|
+
const getAbortReason = (error) => {
|
|
219
|
+
if (!error || typeof error !== "object") return;
|
|
220
|
+
if ("reason" in error) return error.reason;
|
|
221
|
+
if ("cause" in error) return error.cause;
|
|
222
|
+
};
|
|
223
|
+
var HttpClient = class {
|
|
224
|
+
constructor(endpoint, options = { headers: { "Content-Type": "application/json" } }) {
|
|
225
|
+
_defineProperty(this, "client", void 0);
|
|
226
|
+
_defineProperty(this, "abortSignal", void 0);
|
|
227
|
+
_defineProperty(this, "config", {});
|
|
228
|
+
const { signal, ...axiosOptions } = options || {};
|
|
229
|
+
this.client = axios.create({
|
|
230
|
+
baseURL: endpoint,
|
|
231
|
+
timeout: 15e3,
|
|
232
|
+
...axiosOptions
|
|
233
|
+
});
|
|
234
|
+
this.abortSignal = signal;
|
|
235
|
+
this.config = {};
|
|
236
|
+
}
|
|
237
|
+
setConfig(config) {
|
|
238
|
+
this.config = config || {};
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* @internal
|
|
243
|
+
*
|
|
244
|
+
* Current constructor/config cancellation constraint, optionally combined
|
|
245
|
+
* with an operation-level signal. Callers must invoke `dispose` after the
|
|
246
|
+
* operation settles.
|
|
247
|
+
*/
|
|
248
|
+
getAbortSignal(signal, config = this.config) {
|
|
249
|
+
return combineAbortSignals([
|
|
250
|
+
this.abortSignal,
|
|
251
|
+
config === null || config === void 0 ? void 0 : config.signal,
|
|
252
|
+
signal
|
|
253
|
+
]);
|
|
254
|
+
}
|
|
255
|
+
prepareRequest(baseConfig = {}, perCallConfig = {}) {
|
|
256
|
+
const base = baseConfig || {};
|
|
257
|
+
const perCall = perCallConfig || {};
|
|
258
|
+
const retryCancellation = getRetryRequestCancellation(perCall);
|
|
259
|
+
const config = {
|
|
260
|
+
...base,
|
|
261
|
+
...perCall
|
|
262
|
+
};
|
|
263
|
+
if (base.headers && perCall.headers) config.headers = {
|
|
264
|
+
...base.headers,
|
|
265
|
+
...perCall.headers
|
|
266
|
+
};
|
|
267
|
+
const { signal, dispose } = combineAbortSignals([
|
|
268
|
+
this.abortSignal,
|
|
269
|
+
retryCancellation ? retryCancellation.signal : base.signal,
|
|
270
|
+
perCall.signal
|
|
271
|
+
]);
|
|
272
|
+
if (signal) config.signal = signal;
|
|
273
|
+
else delete config.signal;
|
|
274
|
+
clearRetryRequestConfigBrand(config);
|
|
275
|
+
return {
|
|
276
|
+
config,
|
|
277
|
+
dispose
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
async executeRequest(request, baseConfig, perCallConfig) {
|
|
281
|
+
const { config, dispose } = this.prepareRequest(baseConfig, perCallConfig);
|
|
282
|
+
const signal = config.signal;
|
|
283
|
+
try {
|
|
284
|
+
throwIfAborted(signal);
|
|
285
|
+
return await raceWithAbort(request(config), signal);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) throw createAbortError(signal);
|
|
288
|
+
if (isAbortError(error)) throw error instanceof AbortError ? error : new AbortError(getAbortReason(error));
|
|
289
|
+
throw error;
|
|
290
|
+
} finally {
|
|
291
|
+
dispose();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
get(endpoint, params = {}, config = {}) {
|
|
295
|
+
return this.executeRequest((requestConfig) => this.client.get(endpoint, requestConfig), {
|
|
296
|
+
params,
|
|
297
|
+
...this.config
|
|
298
|
+
}, config);
|
|
299
|
+
}
|
|
300
|
+
post(endpoint, data = {}, config = {}) {
|
|
301
|
+
return this.executeRequest((requestConfig) => this.client.post(endpoint, data, requestConfig), this.config, config);
|
|
302
|
+
}
|
|
303
|
+
put(endpoint, data = {}, config = {}) {
|
|
304
|
+
return this.executeRequest((requestConfig) => this.client.put(endpoint, data, requestConfig), this.config, config);
|
|
305
|
+
}
|
|
306
|
+
delete(endpoint, params = {}, config = {}) {
|
|
307
|
+
return this.executeRequest((requestConfig) => this.client.delete(endpoint, requestConfig), {
|
|
308
|
+
params,
|
|
309
|
+
...this.config
|
|
310
|
+
}, config);
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/classes/HttpRestClient.ts
|
|
316
|
+
const getErrorMessage = (error, endpoint) => {
|
|
317
|
+
if (!error.response) return `The request to ${endpoint} has failed.`;
|
|
318
|
+
return error.response.data ? error.response.data.message || error.response.data.statusMessage || error.response.data : error.response.statusText;
|
|
319
|
+
};
|
|
320
|
+
const getLocalAbortError = (error, signal) => {
|
|
321
|
+
var _error$config;
|
|
322
|
+
const axiosSignal = axios.isAxiosError(error) ? (_error$config = error.config) === null || _error$config === void 0 ? void 0 : _error$config.signal : void 0;
|
|
323
|
+
if (isAbortError(error)) {
|
|
324
|
+
if (error instanceof AbortError) return error;
|
|
325
|
+
return new AbortError((signal === null || signal === void 0 ? void 0 : signal.aborted) ? signal.reason : (axiosSignal === null || axiosSignal === void 0 ? void 0 : axiosSignal.aborted) ? axiosSignal.reason : error.reason);
|
|
326
|
+
}
|
|
327
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) return createAbortError(signal);
|
|
328
|
+
if (axiosSignal === null || axiosSignal === void 0 ? void 0 : axiosSignal.aborted) return createAbortError(axiosSignal);
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* @hidden
|
|
332
|
+
*/
|
|
333
|
+
var HttpRestClient = class {
|
|
334
|
+
constructor(endpoint, options = {}) {
|
|
335
|
+
_defineProperty(this, "client", void 0);
|
|
336
|
+
_defineProperty(this, "endpoint", void 0);
|
|
337
|
+
this.client = new HttpClient(endpoint, options);
|
|
338
|
+
this.endpoint = endpoint;
|
|
339
|
+
}
|
|
340
|
+
setConfig(config) {
|
|
341
|
+
this.client.setConfig(config);
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
async get(endpoint, params = {}, config = {}) {
|
|
345
|
+
const requestConfig = config || {};
|
|
346
|
+
try {
|
|
347
|
+
return await this.client.get(endpoint, params, requestConfig);
|
|
348
|
+
} catch (e) {
|
|
349
|
+
const error = e;
|
|
350
|
+
const abortError = getLocalAbortError(error, requestConfig.signal);
|
|
351
|
+
if (abortError) throw abortError;
|
|
352
|
+
if (axios.isAxiosError(error)) {
|
|
353
|
+
if (error.code === "ECONNABORTED") throw new HttpRequestException(new Error(error.message), {
|
|
354
|
+
code: StatusCodes.REQUEST_TOO_LONG,
|
|
355
|
+
context: endpoint
|
|
356
|
+
});
|
|
357
|
+
const message = getErrorMessage(error, endpoint);
|
|
358
|
+
throw new HttpRequestException(new Error(message), {
|
|
359
|
+
context: endpoint,
|
|
360
|
+
code: error.response ? error.response.status : StatusCodes.BAD_REQUEST
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
throw new HttpRequestException(new Error(error.message), {
|
|
364
|
+
code: UnspecifiedErrorCode,
|
|
365
|
+
context: endpoint
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async retry(httpCall, retriesOrOptions, legacyDelay, options) {
|
|
370
|
+
var _retriesOrOptions$del;
|
|
371
|
+
const withRequestConfig = typeof retriesOrOptions === "object" && retriesOrOptions !== null;
|
|
372
|
+
const { retries = 3, delay = 1e3, signal: requestSignal } = !withRequestConfig ? {
|
|
373
|
+
...options,
|
|
374
|
+
retries: retriesOrOptions,
|
|
375
|
+
delay: legacyDelay
|
|
376
|
+
} : {
|
|
377
|
+
...options,
|
|
378
|
+
...retriesOrOptions,
|
|
379
|
+
delay: (_retriesOrOptions$del = retriesOrOptions.delay) !== null && _retriesOrOptions$del !== void 0 ? _retriesOrOptions$del : legacyDelay
|
|
380
|
+
};
|
|
381
|
+
const { signal, dispose } = this.client.getAbortSignal(requestSignal);
|
|
382
|
+
const retryHttpCall = async (attempt = 1) => {
|
|
383
|
+
throwIfAborted(signal);
|
|
384
|
+
try {
|
|
385
|
+
const responsePromise = withRequestConfig ? httpCall(createRetryRequestConfig(signal)) : httpCall();
|
|
386
|
+
return await raceWithAbort(Promise.resolve(responsePromise), signal);
|
|
387
|
+
} catch (e) {
|
|
388
|
+
const abortError = getLocalAbortError(e, signal);
|
|
389
|
+
if (abortError) throw abortError;
|
|
390
|
+
throwIfAborted(signal);
|
|
391
|
+
if (e instanceof HttpRequestException) {
|
|
392
|
+
if (e.code === StatusCodes.REQUEST_TOO_LONG) throw e;
|
|
393
|
+
}
|
|
394
|
+
if (attempt >= retries) throw e;
|
|
395
|
+
await abortableSleep(delay * attempt, signal);
|
|
396
|
+
return retryHttpCall(attempt + 1);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
try {
|
|
400
|
+
return await retryHttpCall();
|
|
401
|
+
} finally {
|
|
402
|
+
dispose();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
async post(endpoint, params = {}, config = {}) {
|
|
406
|
+
const requestConfig = config || {};
|
|
407
|
+
try {
|
|
408
|
+
return await this.client.post(endpoint, params, requestConfig);
|
|
409
|
+
} catch (e) {
|
|
410
|
+
const error = e;
|
|
411
|
+
const abortError = getLocalAbortError(error, requestConfig.signal);
|
|
412
|
+
if (abortError) throw abortError;
|
|
413
|
+
if (axios.isAxiosError(error)) {
|
|
414
|
+
if (error.code === "ECONNABORTED") throw new HttpRequestException(new Error(error.message), {
|
|
415
|
+
code: StatusCodes.REQUEST_TOO_LONG,
|
|
416
|
+
method: HttpRequestMethod.Post
|
|
417
|
+
});
|
|
418
|
+
const message = getErrorMessage(error, endpoint);
|
|
419
|
+
throw new HttpRequestException(new Error(message), {
|
|
420
|
+
code: error.response ? error.response.status : StatusCodes.BAD_REQUEST,
|
|
421
|
+
context: endpoint,
|
|
422
|
+
contextModule: HttpRequestMethod.Post
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
throw new HttpRequestException(new Error(error.message), {
|
|
426
|
+
code: UnspecifiedErrorCode,
|
|
427
|
+
context: endpoint,
|
|
428
|
+
contextModule: HttpRequestMethod.Post
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
//#endregion
|
|
435
|
+
export { combineAbortSignals as a, raceWithAbort as c, abortableSleep as i, throwIfAborted as l, HttpClient as n, createAbortError as o, AbortError as r, isAbortError as s, HttpRestClient as t };
|