@tickwise/sdk 0.0.0-stage → 0.1.0

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 (72) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +185 -2
  3. package/dist/cjs/api-promise.d.ts +22 -0
  4. package/dist/cjs/api-promise.js +43 -0
  5. package/dist/cjs/client.d.ts +64 -0
  6. package/dist/cjs/client.js +118 -0
  7. package/dist/cjs/csv.d.ts +37 -0
  8. package/dist/cjs/csv.js +69 -0
  9. package/dist/cjs/errors.d.ts +85 -0
  10. package/dist/cjs/errors.js +181 -0
  11. package/dist/cjs/generated/openapi.d.ts +1821 -0
  12. package/dist/cjs/generated/openapi.js +6 -0
  13. package/dist/cjs/index.d.ts +11 -0
  14. package/dist/cjs/index.js +30 -0
  15. package/dist/cjs/package.json +1 -0
  16. package/dist/cjs/resources/base.d.ts +21 -0
  17. package/dist/cjs/resources/base.js +81 -0
  18. package/dist/cjs/resources/hooks.d.ts +26 -0
  19. package/dist/cjs/resources/hooks.js +46 -0
  20. package/dist/cjs/resources/meta.d.ts +23 -0
  21. package/dist/cjs/resources/meta.js +35 -0
  22. package/dist/cjs/resources/pools.d.ts +21 -0
  23. package/dist/cjs/resources/pools.js +40 -0
  24. package/dist/cjs/resources/positions.d.ts +45 -0
  25. package/dist/cjs/resources/positions.js +133 -0
  26. package/dist/cjs/resources/tokens.d.ts +13 -0
  27. package/dist/cjs/resources/tokens.js +20 -0
  28. package/dist/cjs/resources/wallets.d.ts +11 -0
  29. package/dist/cjs/resources/wallets.js +14 -0
  30. package/dist/cjs/routes.d.ts +80 -0
  31. package/dist/cjs/routes.js +40 -0
  32. package/dist/cjs/transport.d.ts +91 -0
  33. package/dist/cjs/transport.js +310 -0
  34. package/dist/cjs/types.d.ts +99 -0
  35. package/dist/cjs/types.js +2 -0
  36. package/dist/cjs/version.d.ts +2 -0
  37. package/dist/cjs/version.js +5 -0
  38. package/dist/esm/api-promise.d.ts +22 -0
  39. package/dist/esm/api-promise.js +39 -0
  40. package/dist/esm/client.d.ts +64 -0
  41. package/dist/esm/client.js +114 -0
  42. package/dist/esm/csv.d.ts +37 -0
  43. package/dist/esm/csv.js +64 -0
  44. package/dist/esm/errors.d.ts +85 -0
  45. package/dist/esm/errors.js +163 -0
  46. package/dist/esm/generated/openapi.d.ts +1821 -0
  47. package/dist/esm/generated/openapi.js +5 -0
  48. package/dist/esm/index.d.ts +11 -0
  49. package/dist/esm/index.js +8 -0
  50. package/dist/esm/resources/base.d.ts +21 -0
  51. package/dist/esm/resources/base.js +73 -0
  52. package/dist/esm/resources/hooks.d.ts +26 -0
  53. package/dist/esm/resources/hooks.js +42 -0
  54. package/dist/esm/resources/meta.d.ts +23 -0
  55. package/dist/esm/resources/meta.js +30 -0
  56. package/dist/esm/resources/pools.d.ts +21 -0
  57. package/dist/esm/resources/pools.js +36 -0
  58. package/dist/esm/resources/positions.d.ts +45 -0
  59. package/dist/esm/resources/positions.js +129 -0
  60. package/dist/esm/resources/tokens.d.ts +13 -0
  61. package/dist/esm/resources/tokens.js +16 -0
  62. package/dist/esm/resources/wallets.d.ts +11 -0
  63. package/dist/esm/resources/wallets.js +10 -0
  64. package/dist/esm/routes.d.ts +80 -0
  65. package/dist/esm/routes.js +36 -0
  66. package/dist/esm/transport.d.ts +91 -0
  67. package/dist/esm/transport.js +299 -0
  68. package/dist/esm/types.d.ts +99 -0
  69. package/dist/esm/types.js +1 -0
  70. package/dist/esm/version.d.ts +2 -0
  71. package/dist/esm/version.js +2 -0
  72. package/package.json +64 -4
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ /**
3
+ * Every error the SDK throws is a `TickwiseError`. Branch on `instanceof` or on `kind`, never on `message`: the API's
4
+ * error bodies are `{ error: string }` with no machine code, and their text can change. `kind` is derived from the
5
+ * status code and, for a 429, from the `X-RateLimit-Reason` header.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TimeoutError = exports.ConnectionError = exports.ServiceUnavailableError = exports.QuotaExceededError = exports.RateLimitError = exports.TooManyRequestsError = exports.NotFoundError = exports.PermissionDeniedError = exports.AuthenticationError = exports.BadRequestError = exports.APIError = exports.TickwiseError = void 0;
9
+ exports.errorMessage = errorMessage;
10
+ exports.retryAfterOf = retryAfterOf;
11
+ exports.errorFromResponse = errorFromResponse;
12
+ class TickwiseError extends Error {
13
+ kind;
14
+ /** HTTP status, when the API answered. */
15
+ status;
16
+ /** The parsed JSON body (or the text, when it was not JSON). */
17
+ body;
18
+ headers;
19
+ /** Seconds the API asked to wait (`Retry-After`), on a 429 or 503. */
20
+ retryAfterSec;
21
+ /** The request URL (it never contains the key). */
22
+ url;
23
+ constructor(kind, message, init = {}) {
24
+ super(message, init.cause === undefined ? undefined : { cause: init.cause });
25
+ this.name = 'TickwiseError';
26
+ this.kind = kind;
27
+ this.status = init.status;
28
+ this.body = init.body;
29
+ this.headers = init.headers;
30
+ this.retryAfterSec = init.retryAfterSec;
31
+ this.url = init.url;
32
+ }
33
+ }
34
+ exports.TickwiseError = TickwiseError;
35
+ /** The API answered with a status the SDK does not treat as success. */
36
+ class APIError extends TickwiseError {
37
+ constructor(kind, message, init = {}) {
38
+ super(kind, message, init);
39
+ this.name = 'APIError';
40
+ }
41
+ }
42
+ exports.APIError = APIError;
43
+ /** 400: a parameter is malformed or unknown. Costs no units. */
44
+ class BadRequestError extends APIError {
45
+ constructor(message, init = {}) {
46
+ super('bad_request', message, init);
47
+ this.name = 'BadRequestError';
48
+ }
49
+ }
50
+ exports.BadRequestError = BadRequestError;
51
+ /** 401: the key is missing, unknown or revoked, or the account is suspended. */
52
+ class AuthenticationError extends APIError {
53
+ constructor(message, init = {}) {
54
+ super('authentication', message, init);
55
+ this.name = 'AuthenticationError';
56
+ }
57
+ }
58
+ exports.AuthenticationError = AuthenticationError;
59
+ /** 403: a server key sent from a browser, or a browser key from an origin that is not registered. */
60
+ class PermissionDeniedError extends APIError {
61
+ constructor(message, init = {}) {
62
+ super('permission_denied', message, init);
63
+ this.name = 'PermissionDeniedError';
64
+ }
65
+ }
66
+ exports.PermissionDeniedError = PermissionDeniedError;
67
+ /** 404: no such position, pool or token on that chain and protocol. */
68
+ class NotFoundError extends APIError {
69
+ constructor(message, init = {}) {
70
+ super('not_found', message, init);
71
+ this.name = 'NotFoundError';
72
+ }
73
+ }
74
+ exports.NotFoundError = NotFoundError;
75
+ /** 429, either limit. Catch this to handle both; the subclasses say which. */
76
+ class TooManyRequestsError extends APIError {
77
+ constructor(kind, message, init = {}) {
78
+ super(kind, message, init);
79
+ this.name = 'TooManyRequestsError';
80
+ }
81
+ }
82
+ exports.TooManyRequestsError = TooManyRequestsError;
83
+ /** 429 from the 60-second burst limit. Retried automatically; wait `retryAfterSec`. */
84
+ class RateLimitError extends TooManyRequestsError {
85
+ constructor(message, init = {}) {
86
+ super('rate_limited', message, init);
87
+ this.name = 'RateLimitError';
88
+ }
89
+ }
90
+ exports.RateLimitError = RateLimitError;
91
+ /** 429 from the monthly unit quota. Never retried: nothing changes until the quota resets or the plan is upgraded. */
92
+ class QuotaExceededError extends TooManyRequestsError {
93
+ constructor(message, init = {}) {
94
+ super('quota_exceeded', message, init);
95
+ this.name = 'QuotaExceededError';
96
+ }
97
+ }
98
+ exports.QuotaExceededError = QuotaExceededError;
99
+ /** 503: a read did not finish in time. Retried automatically after `Retry-After`. Costs no units. */
100
+ class ServiceUnavailableError extends APIError {
101
+ constructor(message, init = {}) {
102
+ super('unavailable', message, init);
103
+ this.name = 'ServiceUnavailableError';
104
+ }
105
+ }
106
+ exports.ServiceUnavailableError = ServiceUnavailableError;
107
+ /** No answer: DNS, refused or reset connection, TLS. */
108
+ class ConnectionError extends TickwiseError {
109
+ /**
110
+ * True when the request certainly never reached the API (the name did not resolve, or the connection was
111
+ * refused or could not be opened). Only then is a metered call retried: a connection lost after the request was
112
+ * sent may have been served, and charged.
113
+ */
114
+ connectFailed;
115
+ constructor(message, init = {}) {
116
+ super('connection', message, init);
117
+ this.name = 'ConnectionError';
118
+ this.connectFailed = init.connectFailed ?? false;
119
+ }
120
+ }
121
+ exports.ConnectionError = ConnectionError;
122
+ /** The attempt took longer than `timeoutMs`. */
123
+ class TimeoutError extends TickwiseError {
124
+ constructor(message, init = {}) {
125
+ super('timeout', message, init);
126
+ this.name = 'TimeoutError';
127
+ }
128
+ }
129
+ exports.TimeoutError = TimeoutError;
130
+ /** The most useful text an error body carries: Nest validation detail first, then `error`. */
131
+ function errorMessage(body, status) {
132
+ if (typeof body === 'string' && body.trim())
133
+ return body.trim().slice(0, 500);
134
+ if (body && typeof body === 'object') {
135
+ const o = body;
136
+ const m = o.message;
137
+ if (Array.isArray(m) && m.length > 0)
138
+ return m.map(String).join('; ');
139
+ if (typeof m === 'string' && m)
140
+ return m;
141
+ if (typeof o.error === 'string' && o.error) {
142
+ // Some 400s name the offending value next to a generic error (an unknown `fields` path, a bad poolId).
143
+ const where = typeof o.path === 'string' ? o.path : typeof o.poolId === 'string' ? o.poolId : undefined;
144
+ return where ? `${o.error}: ${where}` : o.error;
145
+ }
146
+ }
147
+ return `HTTP ${status}`;
148
+ }
149
+ /** `Retry-After` in whole seconds (the API sends seconds), else the 429 body's `retryAfterSec`. */
150
+ function retryAfterOf(headers, body) {
151
+ const h = headers.get('retry-after')?.trim();
152
+ if (h && /^\d+$/.test(h))
153
+ return Number(h);
154
+ const b = body && typeof body === 'object' ? body.retryAfterSec : undefined;
155
+ return typeof b === 'number' && Number.isFinite(b) && b >= 0 ? b : undefined;
156
+ }
157
+ /** The error for a non-2xx answer. */
158
+ function errorFromResponse(status, headers, body, url) {
159
+ const message = errorMessage(body, status);
160
+ const init = { status, body, headers, url, retryAfterSec: retryAfterOf(headers, body) };
161
+ switch (status) {
162
+ case 400:
163
+ return new BadRequestError(message, init);
164
+ case 401:
165
+ return new AuthenticationError(message, init);
166
+ case 403:
167
+ return new PermissionDeniedError(message, init);
168
+ case 404:
169
+ return new NotFoundError(message, init);
170
+ case 429: {
171
+ const reason = headers.get('x-ratelimit-reason')?.trim().toLowerCase();
172
+ // The header is the contract; the body text is a fallback for a client that cannot read it.
173
+ const quota = reason === 'quota' || (!reason && /quota/i.test(message));
174
+ return quota ? new QuotaExceededError(message, init) : new RateLimitError(message, init);
175
+ }
176
+ case 503:
177
+ return new ServiceUnavailableError(message, init);
178
+ default:
179
+ return new APIError('api_error', message, init);
180
+ }
181
+ }