@tickwise/sdk 0.0.0-stage → 0.1.1
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/LICENSE +661 -0
- package/README.md +203 -2
- package/dist/cjs/api-promise.d.ts +22 -0
- package/dist/cjs/api-promise.js +43 -0
- package/dist/cjs/client.d.ts +64 -0
- package/dist/cjs/client.js +123 -0
- package/dist/cjs/csv.d.ts +37 -0
- package/dist/cjs/csv.js +69 -0
- package/dist/cjs/errors.d.ts +133 -0
- package/dist/cjs/errors.js +242 -0
- package/dist/cjs/generated/openapi.d.ts +1949 -0
- package/dist/cjs/generated/openapi.js +6 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.js +33 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/resources/base.d.ts +21 -0
- package/dist/cjs/resources/base.js +81 -0
- package/dist/cjs/resources/hooks.d.ts +26 -0
- package/dist/cjs/resources/hooks.js +46 -0
- package/dist/cjs/resources/meta.d.ts +23 -0
- package/dist/cjs/resources/meta.js +35 -0
- package/dist/cjs/resources/pools.d.ts +21 -0
- package/dist/cjs/resources/pools.js +40 -0
- package/dist/cjs/resources/positions.d.ts +59 -0
- package/dist/cjs/resources/positions.js +187 -0
- package/dist/cjs/resources/tokens.d.ts +19 -0
- package/dist/cjs/resources/tokens.js +28 -0
- package/dist/cjs/resources/wallets.d.ts +11 -0
- package/dist/cjs/resources/wallets.js +14 -0
- package/dist/cjs/routes.d.ts +84 -0
- package/dist/cjs/routes.js +45 -0
- package/dist/cjs/transport.d.ts +92 -0
- package/dist/cjs/transport.js +314 -0
- package/dist/cjs/types.d.ts +112 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/esm/api-promise.d.ts +22 -0
- package/dist/esm/api-promise.js +39 -0
- package/dist/esm/client.d.ts +64 -0
- package/dist/esm/client.js +119 -0
- package/dist/esm/csv.d.ts +37 -0
- package/dist/esm/csv.js +64 -0
- package/dist/esm/errors.d.ts +133 -0
- package/dist/esm/errors.js +221 -0
- package/dist/esm/generated/openapi.d.ts +1949 -0
- package/dist/esm/generated/openapi.js +5 -0
- package/dist/esm/index.d.ts +11 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/resources/base.d.ts +21 -0
- package/dist/esm/resources/base.js +73 -0
- package/dist/esm/resources/hooks.d.ts +26 -0
- package/dist/esm/resources/hooks.js +42 -0
- package/dist/esm/resources/meta.d.ts +23 -0
- package/dist/esm/resources/meta.js +30 -0
- package/dist/esm/resources/pools.d.ts +21 -0
- package/dist/esm/resources/pools.js +36 -0
- package/dist/esm/resources/positions.d.ts +59 -0
- package/dist/esm/resources/positions.js +183 -0
- package/dist/esm/resources/tokens.d.ts +19 -0
- package/dist/esm/resources/tokens.js +24 -0
- package/dist/esm/resources/wallets.d.ts +11 -0
- package/dist/esm/resources/wallets.js +10 -0
- package/dist/esm/routes.d.ts +84 -0
- package/dist/esm/routes.js +41 -0
- package/dist/esm/transport.d.ts +92 -0
- package/dist/esm/transport.js +303 -0
- package/dist/esm/types.d.ts +112 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/package.json +64 -4
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every error from the API or the network is a `TickwiseError`. The exceptions: a bad option or argument is a
|
|
3
|
+
* `TypeError` (thrown, or as the rejection of the call), and a call cancelled through `signal` rejects with the
|
|
4
|
+
* signal's reason (the platform's `AbortError` by default). Branch on `instanceof` or on `kind`, never on `message`: the API's
|
|
5
|
+
* error bodies are `{ error: string }` with no machine code, and their text can change. `kind` is derived from the
|
|
6
|
+
* status code and, for a 429, from the `X-RateLimit-Reason` header.
|
|
7
|
+
*/
|
|
8
|
+
export class TickwiseError extends Error {
|
|
9
|
+
kind;
|
|
10
|
+
/** HTTP status, when the API answered. */
|
|
11
|
+
status;
|
|
12
|
+
/** The parsed JSON body (or the text, when it was not JSON). */
|
|
13
|
+
body;
|
|
14
|
+
headers;
|
|
15
|
+
/** Seconds the API asked to wait (`Retry-After`), on a 429 or 503. */
|
|
16
|
+
retryAfterSec;
|
|
17
|
+
/** The request URL (it never contains the key). */
|
|
18
|
+
url;
|
|
19
|
+
constructor(kind, message, init = {}) {
|
|
20
|
+
super(message, init.cause === undefined ? undefined : { cause: init.cause });
|
|
21
|
+
this.name = 'TickwiseError';
|
|
22
|
+
this.kind = kind;
|
|
23
|
+
this.status = init.status;
|
|
24
|
+
this.body = init.body;
|
|
25
|
+
this.headers = init.headers;
|
|
26
|
+
this.retryAfterSec = init.retryAfterSec;
|
|
27
|
+
this.url = init.url;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The API answered with a status the SDK does not treat as success. */
|
|
31
|
+
export class APIError extends TickwiseError {
|
|
32
|
+
constructor(kind, message, init = {}) {
|
|
33
|
+
super(kind, message, init);
|
|
34
|
+
this.name = 'APIError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** 400: a parameter is malformed or unknown. Costs no units. */
|
|
38
|
+
export class BadRequestError extends APIError {
|
|
39
|
+
constructor(message, init = {}) {
|
|
40
|
+
super('bad_request', message, init);
|
|
41
|
+
this.name = 'BadRequestError';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** 401: the key is missing, unknown or revoked, or the account is suspended. */
|
|
45
|
+
export class AuthenticationError extends APIError {
|
|
46
|
+
constructor(message, init = {}) {
|
|
47
|
+
super('authentication', message, init);
|
|
48
|
+
this.name = 'AuthenticationError';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** 403: a server key sent from a browser, or a browser key from an origin that is not registered. */
|
|
52
|
+
export class PermissionDeniedError extends APIError {
|
|
53
|
+
constructor(message, init = {}) {
|
|
54
|
+
super('permission_denied', message, init);
|
|
55
|
+
this.name = 'PermissionDeniedError';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** 404: no such position, pool or token on that chain and protocol. */
|
|
59
|
+
export class NotFoundError extends APIError {
|
|
60
|
+
constructor(message, init = {}) {
|
|
61
|
+
super('not_found', message, init);
|
|
62
|
+
this.name = 'NotFoundError';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** 429, either limit. Catch this to handle both; the subclasses say which. */
|
|
66
|
+
export class TooManyRequestsError extends APIError {
|
|
67
|
+
constructor(kind, message, init = {}) {
|
|
68
|
+
super(kind, message, init);
|
|
69
|
+
this.name = 'TooManyRequestsError';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** 429 from the 60-second burst limit. Retried automatically; wait `retryAfterSec`. */
|
|
73
|
+
export class RateLimitError extends TooManyRequestsError {
|
|
74
|
+
constructor(message, init = {}) {
|
|
75
|
+
super('rate_limited', message, init);
|
|
76
|
+
this.name = 'RateLimitError';
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** 429 from the monthly unit quota. Never retried: nothing changes until the quota resets or the plan is upgraded. */
|
|
80
|
+
export class QuotaExceededError extends TooManyRequestsError {
|
|
81
|
+
constructor(message, init = {}) {
|
|
82
|
+
super('quota_exceeded', message, init);
|
|
83
|
+
this.name = 'QuotaExceededError';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** 503: a read did not finish in time. Retried automatically after `Retry-After`. Costs no units. */
|
|
87
|
+
export class ServiceUnavailableError extends APIError {
|
|
88
|
+
constructor(message, init = {}) {
|
|
89
|
+
super('unavailable', message, init);
|
|
90
|
+
this.name = 'ServiceUnavailableError';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** No answer: DNS, refused or reset connection, TLS. */
|
|
94
|
+
export class ConnectionError extends TickwiseError {
|
|
95
|
+
/**
|
|
96
|
+
* True when the request certainly never reached the API (the name did not resolve, or the connection was
|
|
97
|
+
* refused or could not be opened). Only then is a metered call retried: a connection lost after the request was
|
|
98
|
+
* sent may have been served, and charged.
|
|
99
|
+
*/
|
|
100
|
+
connectFailed;
|
|
101
|
+
constructor(message, init = {}) {
|
|
102
|
+
super('connection', message, init);
|
|
103
|
+
this.name = 'ConnectionError';
|
|
104
|
+
this.connectFailed = init.connectFailed ?? false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** The attempt took longer than `timeoutMs`. */
|
|
108
|
+
export class TimeoutError extends TickwiseError {
|
|
109
|
+
constructor(message, init = {}) {
|
|
110
|
+
super('timeout', message, init);
|
|
111
|
+
this.name = 'TimeoutError';
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A helper that makes several requests (`positions.batchAll`, `positions.iterateTransactions`) could not get
|
|
116
|
+
* everything. What it did get is on the error; `cause` is the failure that stopped it, when one did.
|
|
117
|
+
*/
|
|
118
|
+
export class IncompleteError extends TickwiseError {
|
|
119
|
+
constructor(message, init = {}) {
|
|
120
|
+
super('incomplete', message, init);
|
|
121
|
+
this.name = 'IncompleteError';
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* `positions.batchAll` stopped on a failed chunk after earlier chunks were served, and charged. The served rows are
|
|
126
|
+
* in `data`; pass `remainingIds` to `batchAll` again to finish without paying for the served ones twice. `cause` is
|
|
127
|
+
* the chunk's error (a `QuotaExceededError`, a `TimeoutError`, the signal's reason after an abort, ...).
|
|
128
|
+
*/
|
|
129
|
+
export class BatchAllIncompleteError extends IncompleteError {
|
|
130
|
+
/** The chunks served before the failure, merged as `batchAll` returns them. */
|
|
131
|
+
data;
|
|
132
|
+
/** Units the served chunks were charged; undefined when a chunk's answer did not say. */
|
|
133
|
+
unitsCharged;
|
|
134
|
+
/** The ids not served: the failed chunk's and every later one, in order. */
|
|
135
|
+
remainingIds;
|
|
136
|
+
constructor(message, init) {
|
|
137
|
+
super(message, init);
|
|
138
|
+
this.name = 'BatchAllIncompleteError';
|
|
139
|
+
this.data = init.data;
|
|
140
|
+
this.unitsCharged = init.unitsCharged;
|
|
141
|
+
this.remainingIds = init.remainingIds;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* `positions.iterateTransactions` reached the oldest transaction the API had, but the last page said the list is not
|
|
146
|
+
* complete: `transactionsPartial` (only the newest transactions were ready; ask again in a few seconds),
|
|
147
|
+
* `transactionsTruncated` (the history is longer than the API reads; the oldest are missing) or
|
|
148
|
+
* `transactionsDegraded` (the list could not be read in full). Every row the API had was yielded before this is
|
|
149
|
+
* thrown.
|
|
150
|
+
*/
|
|
151
|
+
export class TransactionsIncompleteError extends IncompleteError {
|
|
152
|
+
reasons;
|
|
153
|
+
/** How many transactions were yielded. */
|
|
154
|
+
yielded;
|
|
155
|
+
constructor(message, init) {
|
|
156
|
+
super(message, init);
|
|
157
|
+
this.name = 'TransactionsIncompleteError';
|
|
158
|
+
this.reasons = init.reasons;
|
|
159
|
+
this.yielded = init.yielded;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** The most useful text an error body carries: Nest validation detail first, then `error`. */
|
|
163
|
+
export function errorMessage(body, status) {
|
|
164
|
+
if (typeof body === 'string' && body.trim())
|
|
165
|
+
return body.trim().slice(0, 500);
|
|
166
|
+
if (body && typeof body === 'object') {
|
|
167
|
+
const o = body;
|
|
168
|
+
const m = o.message;
|
|
169
|
+
if (Array.isArray(m) && m.length > 0)
|
|
170
|
+
return m.map(String).join('; ');
|
|
171
|
+
if (typeof m === 'string' && m)
|
|
172
|
+
return m;
|
|
173
|
+
if (typeof o.error === 'string' && o.error) {
|
|
174
|
+
// Some 400s name the offending value next to a generic error (an unknown `fields` path, a bad poolId).
|
|
175
|
+
const where = typeof o.path === 'string' ? o.path : typeof o.poolId === 'string' ? o.poolId : undefined;
|
|
176
|
+
return where ? `${o.error}: ${where}` : o.error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return `HTTP ${status}`;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* `Retry-After` in whole seconds: the API sends seconds; a gateway may send an HTTP date, read as the seconds from
|
|
183
|
+
* now (0 when it has passed). Else the 429 body's `retryAfterSec`.
|
|
184
|
+
*/
|
|
185
|
+
export function retryAfterOf(headers, body, now = Date.now()) {
|
|
186
|
+
const h = headers.get('retry-after')?.trim();
|
|
187
|
+
if (h && /^\d+$/.test(h))
|
|
188
|
+
return Number(h);
|
|
189
|
+
if (h && /[a-z]/i.test(h)) {
|
|
190
|
+
const at = Date.parse(h);
|
|
191
|
+
if (Number.isFinite(at))
|
|
192
|
+
return Math.max(0, Math.ceil((at - now) / 1000));
|
|
193
|
+
}
|
|
194
|
+
const b = body && typeof body === 'object' ? body.retryAfterSec : undefined;
|
|
195
|
+
return typeof b === 'number' && Number.isFinite(b) && b >= 0 ? b : undefined;
|
|
196
|
+
}
|
|
197
|
+
/** The error for a non-2xx answer. */
|
|
198
|
+
export function errorFromResponse(status, headers, body, url) {
|
|
199
|
+
const message = errorMessage(body, status);
|
|
200
|
+
const init = { status, body, headers, url, retryAfterSec: retryAfterOf(headers, body) };
|
|
201
|
+
switch (status) {
|
|
202
|
+
case 400:
|
|
203
|
+
return new BadRequestError(message, init);
|
|
204
|
+
case 401:
|
|
205
|
+
return new AuthenticationError(message, init);
|
|
206
|
+
case 403:
|
|
207
|
+
return new PermissionDeniedError(message, init);
|
|
208
|
+
case 404:
|
|
209
|
+
return new NotFoundError(message, init);
|
|
210
|
+
case 429: {
|
|
211
|
+
const reason = headers.get('x-ratelimit-reason')?.trim().toLowerCase();
|
|
212
|
+
// The header is the contract; the body text is a fallback for a client that cannot read it.
|
|
213
|
+
const quota = reason === 'quota' || (!reason && /quota/i.test(message));
|
|
214
|
+
return quota ? new QuotaExceededError(message, init) : new RateLimitError(message, init);
|
|
215
|
+
}
|
|
216
|
+
case 503:
|
|
217
|
+
return new ServiceUnavailableError(message, init);
|
|
218
|
+
default:
|
|
219
|
+
return new APIError('api_error', message, init);
|
|
220
|
+
}
|
|
221
|
+
}
|