@coinlist-co/react 0.0.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/README.md +7 -0
- package/dist/chunk-5E3P7AMH.js +20 -0
- package/dist/chunk-5E3P7AMH.js.map +1 -0
- package/dist/chunk-F56RBKNS.js +63 -0
- package/dist/chunk-F56RBKNS.js.map +1 -0
- package/dist/chunk-NLJMUHOG.js +253 -0
- package/dist/chunk-NLJMUHOG.js.map +1 -0
- package/dist/chunk-OETRTIQ4.js +274 -0
- package/dist/chunk-OETRTIQ4.js.map +1 -0
- package/dist/chunk-YJQMJFA7.js +16 -0
- package/dist/chunk-YJQMJFA7.js.map +1 -0
- package/dist/client/components/index.cjs +445 -0
- package/dist/client/components/index.cjs.map +1 -0
- package/dist/client/components/index.d.cts +19 -0
- package/dist/client/components/index.d.ts +19 -0
- package/dist/client/components/index.js +399 -0
- package/dist/client/components/index.js.map +1 -0
- package/dist/client/core/index.cjs +566 -0
- package/dist/client/core/index.cjs.map +1 -0
- package/dist/client/core/index.d.cts +22 -0
- package/dist/client/core/index.d.ts +22 -0
- package/dist/client/core/index.js +35 -0
- package/dist/client/core/index.js.map +1 -0
- package/dist/client/hooks/index.cjs +56 -0
- package/dist/client/hooks/index.cjs.map +1 -0
- package/dist/client/hooks/index.d.cts +14 -0
- package/dist/client/hooks/index.d.ts +14 -0
- package/dist/client/hooks/index.js +9 -0
- package/dist/client/hooks/index.js.map +1 -0
- package/dist/client/index.cjs +621 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +18 -0
- package/dist/client/index.d.ts +18 -0
- package/dist/client/index.js +41 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/styles.css +2 -0
- package/dist/coinlist-client-BH_Cn_7u.d.cts +71 -0
- package/dist/coinlist-client-fkB3J-2a.d.ts +71 -0
- package/dist/oauth-session-g0OPJNKn.d.cts +59 -0
- package/dist/oauth-session-g0OPJNKn.d.ts +59 -0
- package/dist/server/index.cjs +405 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +37 -0
- package/dist/server/index.d.ts +37 -0
- package/dist/server/index.js +142 -0
- package/dist/server/index.js.map +1 -0
- package/package.json +122 -0
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
AuthorizationCode: () => AuthorizationCode,
|
|
24
|
+
ClientId: () => ClientId,
|
|
25
|
+
ClientSecret: () => ClientSecret,
|
|
26
|
+
CodeChallenge: () => CodeChallenge,
|
|
27
|
+
CodeVerifier: () => CodeVerifier,
|
|
28
|
+
CoinListProvider: () => CoinListProvider,
|
|
29
|
+
OAuthRefreshToken: () => OAuthRefreshToken,
|
|
30
|
+
OAuthSession: () => OAuthSession,
|
|
31
|
+
RedirectUri: () => RedirectUri,
|
|
32
|
+
User: () => User,
|
|
33
|
+
UserEmail: () => UserEmail,
|
|
34
|
+
UserId: () => UserId,
|
|
35
|
+
createCoinListClient: () => createCoinListClient,
|
|
36
|
+
useCoinList: () => useCoinList
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(client_exports);
|
|
39
|
+
|
|
40
|
+
// src/client/CoinListProvider.tsx
|
|
41
|
+
var import_react = require("react");
|
|
42
|
+
|
|
43
|
+
// src/shared/api/http-attributes.ts
|
|
44
|
+
var empty = {};
|
|
45
|
+
var concat = (left, right) => ({
|
|
46
|
+
...left,
|
|
47
|
+
...right
|
|
48
|
+
});
|
|
49
|
+
var concatAll = (...items) => {
|
|
50
|
+
let result = empty;
|
|
51
|
+
for (const item of items) {
|
|
52
|
+
result = concat(result, item);
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
};
|
|
56
|
+
var protectedRequest = () => ({ protected: true });
|
|
57
|
+
var userAgent = () => ({ userAgent: true });
|
|
58
|
+
var idempotencyKey = () => ({
|
|
59
|
+
idempotencyKey: true
|
|
60
|
+
});
|
|
61
|
+
var retryAttempt = (attempt) => ({
|
|
62
|
+
retryAttempt: attempt
|
|
63
|
+
});
|
|
64
|
+
var renewAttempted = (value) => ({
|
|
65
|
+
renewAttempted: value
|
|
66
|
+
});
|
|
67
|
+
var isProtected = (attrs) => attrs?.protected === true;
|
|
68
|
+
var needUserAgent = (attrs) => attrs?.userAgent === true;
|
|
69
|
+
var isIdempotent = (attrs) => attrs?.idempotencyKey === true;
|
|
70
|
+
var getRetryAttempt = (attrs) => attrs?.retryAttempt ?? 0;
|
|
71
|
+
var wasRenewAttempted = (attrs) => attrs?.renewAttempted === true;
|
|
72
|
+
var Attributes = {
|
|
73
|
+
empty,
|
|
74
|
+
concat,
|
|
75
|
+
concatAll,
|
|
76
|
+
protected: protectedRequest,
|
|
77
|
+
isProtected,
|
|
78
|
+
userAgent,
|
|
79
|
+
needUserAgent,
|
|
80
|
+
idempotencyKey,
|
|
81
|
+
isIdempotent,
|
|
82
|
+
retryAttempt,
|
|
83
|
+
getRetryAttempt,
|
|
84
|
+
renewAttempted,
|
|
85
|
+
wasRenewAttempted
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/client/core/api/middleware/attach-session-middleware.ts
|
|
89
|
+
function attachSessionMiddleware(fetchAccessToken) {
|
|
90
|
+
return async (request) => {
|
|
91
|
+
if (!Attributes.isProtected(request.attributes)) {
|
|
92
|
+
return request;
|
|
93
|
+
}
|
|
94
|
+
const accessToken = await fetchAccessToken(false);
|
|
95
|
+
if (accessToken === null) {
|
|
96
|
+
return request;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
...request,
|
|
100
|
+
headers: {
|
|
101
|
+
...request.headers ?? {},
|
|
102
|
+
Authorization: `Bearer ${accessToken.value}`
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/shared/api/http.ts
|
|
109
|
+
var HttpError = class extends Error {
|
|
110
|
+
constructor(response) {
|
|
111
|
+
super(`Request failed with ${response.status} status`);
|
|
112
|
+
this.name = "HttpError";
|
|
113
|
+
this.response = response;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
async function makeRequest(request) {
|
|
117
|
+
const headers = {
|
|
118
|
+
Accept: "application/json",
|
|
119
|
+
...request.method === "POST" && request.body !== void 0 ? { "Content-Type": "application/json" } : {},
|
|
120
|
+
...request.headers ?? {}
|
|
121
|
+
};
|
|
122
|
+
const init = {
|
|
123
|
+
method: request.method,
|
|
124
|
+
headers,
|
|
125
|
+
...request.redirect !== void 0 ? { redirect: request.redirect } : {}
|
|
126
|
+
};
|
|
127
|
+
if (request.method === "POST" && request.body !== void 0) {
|
|
128
|
+
init.body = JSON.stringify(request.body);
|
|
129
|
+
}
|
|
130
|
+
const response = await fetch(request.url, init);
|
|
131
|
+
const responseHeaders = headersToRecord(response.headers);
|
|
132
|
+
if (response.status === 204 || response.status === 205) {
|
|
133
|
+
return {
|
|
134
|
+
status: response.status,
|
|
135
|
+
body: null,
|
|
136
|
+
headers: responseHeaders
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (response.status >= 300 && response.status < 400) {
|
|
140
|
+
await response.text();
|
|
141
|
+
return {
|
|
142
|
+
status: response.status,
|
|
143
|
+
body: null,
|
|
144
|
+
headers: responseHeaders
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const text = await response.text();
|
|
148
|
+
const body = text ? JSON.parse(text) : null;
|
|
149
|
+
return {
|
|
150
|
+
status: response.status,
|
|
151
|
+
body,
|
|
152
|
+
headers: responseHeaders
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function headersToRecord(headers) {
|
|
156
|
+
const record = {};
|
|
157
|
+
headers.forEach((value, key) => {
|
|
158
|
+
record[key.toLowerCase()] = value;
|
|
159
|
+
});
|
|
160
|
+
return record;
|
|
161
|
+
}
|
|
162
|
+
function concatAttributes(request, attrs) {
|
|
163
|
+
const nextAttributes = Attributes.concat(
|
|
164
|
+
request.attributes ?? Attributes.empty,
|
|
165
|
+
attrs
|
|
166
|
+
);
|
|
167
|
+
const nextRequest = {
|
|
168
|
+
...request,
|
|
169
|
+
attributes: nextAttributes
|
|
170
|
+
};
|
|
171
|
+
return nextRequest;
|
|
172
|
+
}
|
|
173
|
+
var Request = {
|
|
174
|
+
concatAttributes
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// src/client/core/api/middleware/session-renewal.ts
|
|
178
|
+
function renewSessionMiddleware(fetchAccessToken) {
|
|
179
|
+
return async ({ request, response, retry }) => {
|
|
180
|
+
if (response.status !== 401) {
|
|
181
|
+
return response;
|
|
182
|
+
}
|
|
183
|
+
if (!Attributes.isProtected(request.attributes)) {
|
|
184
|
+
return response;
|
|
185
|
+
}
|
|
186
|
+
if (Attributes.wasRenewAttempted(request.attributes)) {
|
|
187
|
+
return response;
|
|
188
|
+
}
|
|
189
|
+
const newToken = await fetchAccessToken(true);
|
|
190
|
+
if (newToken) {
|
|
191
|
+
const nextRequest = Request.concatAttributes(
|
|
192
|
+
request,
|
|
193
|
+
Attributes.renewAttempted(true)
|
|
194
|
+
);
|
|
195
|
+
return retry(nextRequest);
|
|
196
|
+
} else {
|
|
197
|
+
return response;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/shared/utils.ts
|
|
203
|
+
async function sha256(data) {
|
|
204
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
205
|
+
const buffer = bytes.buffer.slice(
|
|
206
|
+
bytes.byteOffset,
|
|
207
|
+
bytes.byteOffset + bytes.byteLength
|
|
208
|
+
);
|
|
209
|
+
return crypto.subtle.digest("SHA-256", buffer);
|
|
210
|
+
}
|
|
211
|
+
function arrayBufferToBase64Url(buffer, padding = true) {
|
|
212
|
+
const bytes = new Uint8Array(buffer);
|
|
213
|
+
let binary = "";
|
|
214
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
215
|
+
binary += String.fromCharCode(bytes[i]);
|
|
216
|
+
}
|
|
217
|
+
let base64 = btoa(binary);
|
|
218
|
+
base64 = base64.replace(/\+/g, "-").replace(/\//g, "_");
|
|
219
|
+
if (!padding) {
|
|
220
|
+
base64 = base64.replace(/=+$/, "");
|
|
221
|
+
}
|
|
222
|
+
return base64;
|
|
223
|
+
}
|
|
224
|
+
function generateSecureRandomBase64Url(byteLength) {
|
|
225
|
+
const bytes = new Uint8Array(byteLength);
|
|
226
|
+
crypto.getRandomValues(bytes);
|
|
227
|
+
const buffer = bytes.buffer;
|
|
228
|
+
return arrayBufferToBase64Url(buffer, false);
|
|
229
|
+
}
|
|
230
|
+
function getUUIDv4() {
|
|
231
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
232
|
+
return crypto.randomUUID();
|
|
233
|
+
}
|
|
234
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
235
|
+
const bytes = new Uint8Array(16);
|
|
236
|
+
crypto.getRandomValues(bytes);
|
|
237
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
238
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
239
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0"));
|
|
240
|
+
return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(6, 8).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10, 16).join("")}`;
|
|
241
|
+
}
|
|
242
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2, 12)}`;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// src/client/core/api/middleware/device-id.ts
|
|
246
|
+
var DEVICE_ID_KEY = "coinlist.device_id";
|
|
247
|
+
function getDeviceId() {
|
|
248
|
+
let deviceId = localStorage.getItem(DEVICE_ID_KEY);
|
|
249
|
+
if (!deviceId) {
|
|
250
|
+
deviceId = getUUIDv4();
|
|
251
|
+
localStorage.setItem(DEVICE_ID_KEY, deviceId);
|
|
252
|
+
}
|
|
253
|
+
return deviceId;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/shared/api/frontline.ts
|
|
257
|
+
var BASE_URL = "https://mobile-api.coinlist.co";
|
|
258
|
+
var HEADER_API_VERSION = "X-API-Version";
|
|
259
|
+
var HEADER_USER_AGENT = "User-Agent";
|
|
260
|
+
var HEADER_IDEMPOTENCY_KEY = "Idempotency-Key";
|
|
261
|
+
var API_VERSION = "2025-10-17";
|
|
262
|
+
var COINLIST_OAUTH_URL = "https://coinlist.co/oauth/authorize";
|
|
263
|
+
|
|
264
|
+
// src/client/core/api/middleware/user-agent.ts
|
|
265
|
+
var userAgentMiddleware = async (request) => {
|
|
266
|
+
if (!Attributes.needUserAgent(request.attributes)) {
|
|
267
|
+
return request;
|
|
268
|
+
}
|
|
269
|
+
const userAgent2 = generateUserAgent();
|
|
270
|
+
return {
|
|
271
|
+
...request,
|
|
272
|
+
headers: {
|
|
273
|
+
...request.headers ?? {},
|
|
274
|
+
[HEADER_USER_AGENT]: userAgent2
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
function generateUserAgent() {
|
|
279
|
+
const appInfo = `CoinList/1.0`;
|
|
280
|
+
const deviceInfo = "Web Browser";
|
|
281
|
+
const devicePlatform = "DevicePlatform web";
|
|
282
|
+
const deviceAppId = "DeviceAppID co.coinlist.sdk";
|
|
283
|
+
const deviceId = getDeviceId();
|
|
284
|
+
const finalDeviceId = `DeviceID ${deviceId}`;
|
|
285
|
+
return `${appInfo} (${deviceInfo}; ${devicePlatform}; ${deviceAppId}; ${finalDeviceId})`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/shared/api/http-client.ts
|
|
289
|
+
var HttpClient = class {
|
|
290
|
+
constructor(config, middleware = {}) {
|
|
291
|
+
this.config = config;
|
|
292
|
+
this.middleware = middleware;
|
|
293
|
+
}
|
|
294
|
+
async send(request) {
|
|
295
|
+
return this.runRequestWithAfterMiddleware(request);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Runs beforeRequest, executeRequest, then the full afterRequest middleware
|
|
299
|
+
* chain. Used by send() and by the retry() callback so that when a
|
|
300
|
+
* middleware calls retry(), the retried response also goes through all
|
|
301
|
+
* afterRequest middleware (e.g. session renewal, retry). Middleware
|
|
302
|
+
* must use request.attributes.retryAttempt (or similar) to avoid infinite
|
|
303
|
+
* recursion when they trigger retries.
|
|
304
|
+
*/
|
|
305
|
+
async runRequestWithAfterMiddleware(request) {
|
|
306
|
+
const preparedRequest = await this.runBeforeRequestMiddleware(
|
|
307
|
+
this.withClientDefaults(request)
|
|
308
|
+
);
|
|
309
|
+
let response = await this.executeRequest(preparedRequest);
|
|
310
|
+
for (const middleware of this.middleware.afterRequest ?? []) {
|
|
311
|
+
response = await middleware({
|
|
312
|
+
request: preparedRequest,
|
|
313
|
+
response,
|
|
314
|
+
retry: (nextRequest = preparedRequest) => this.runRequestWithAfterMiddleware(nextRequest)
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
return response;
|
|
318
|
+
}
|
|
319
|
+
withClientDefaults(request) {
|
|
320
|
+
const url = this.resolveUrl(request.url);
|
|
321
|
+
const headers = {
|
|
322
|
+
...request.headers ?? {},
|
|
323
|
+
[HEADER_API_VERSION]: this.config.xApiVersion
|
|
324
|
+
};
|
|
325
|
+
return {
|
|
326
|
+
...request,
|
|
327
|
+
url,
|
|
328
|
+
headers
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Resolves a request URL against the client's baseUrl.
|
|
333
|
+
*
|
|
334
|
+
* @internal Public only for testing. Do not use in application code; use
|
|
335
|
+
* {@link HttpClient.send} with a path and the client will resolve the URL.
|
|
336
|
+
*/
|
|
337
|
+
resolveUrl(url) {
|
|
338
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
339
|
+
return url;
|
|
340
|
+
}
|
|
341
|
+
const base = this.config.baseUrl.replace(/\/$/, "");
|
|
342
|
+
const path = url.startsWith("/") ? url : `/${url}`;
|
|
343
|
+
return base + path;
|
|
344
|
+
}
|
|
345
|
+
async runBeforeRequestMiddleware(initialRequest) {
|
|
346
|
+
let request = initialRequest;
|
|
347
|
+
for (const middleware of this.middleware.beforeRequest ?? []) {
|
|
348
|
+
request = await middleware(request);
|
|
349
|
+
}
|
|
350
|
+
return request;
|
|
351
|
+
}
|
|
352
|
+
executeRequest(request) {
|
|
353
|
+
return makeRequest(request);
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// src/shared/api/middleware/idempotency-key.ts
|
|
358
|
+
var idempotencyKeyMiddleware = async (request) => {
|
|
359
|
+
if (!Attributes.isIdempotent(request.attributes)) {
|
|
360
|
+
return request;
|
|
361
|
+
}
|
|
362
|
+
const existingHeaders = request.headers ?? {};
|
|
363
|
+
if (existingHeaders[HEADER_IDEMPOTENCY_KEY]) {
|
|
364
|
+
return request;
|
|
365
|
+
}
|
|
366
|
+
return {
|
|
367
|
+
...request,
|
|
368
|
+
headers: {
|
|
369
|
+
...existingHeaders,
|
|
370
|
+
[HEADER_IDEMPOTENCY_KEY]: getUUIDv4()
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// src/shared/api/middleware/request-retry.ts
|
|
376
|
+
var MAX_ATTEMPTS = 3;
|
|
377
|
+
var INITIAL_DELAY_MS = 300;
|
|
378
|
+
var MAX_DELAY_MS = 2e3;
|
|
379
|
+
var RETRYABLE_4XX = /* @__PURE__ */ new Set([408, 409, 429]);
|
|
380
|
+
function isRetryableStatus(status) {
|
|
381
|
+
return status >= 500 && status < 600 || RETRYABLE_4XX.has(status);
|
|
382
|
+
}
|
|
383
|
+
function getRetryDelayMs(attempt) {
|
|
384
|
+
return Math.min(INITIAL_DELAY_MS * 2 ** (attempt - 1), MAX_DELAY_MS);
|
|
385
|
+
}
|
|
386
|
+
function createRequestRetryMiddleware(options = {}) {
|
|
387
|
+
const delayFn = options.delayFn ?? defaultDelay;
|
|
388
|
+
return async ({ request, response, retry }) => {
|
|
389
|
+
if (!isRetryableStatus(response.status)) {
|
|
390
|
+
return response;
|
|
391
|
+
}
|
|
392
|
+
const currentAttempt = Attributes.getRetryAttempt(request.attributes);
|
|
393
|
+
if (currentAttempt >= MAX_ATTEMPTS - 1) {
|
|
394
|
+
return response;
|
|
395
|
+
}
|
|
396
|
+
const nextAttempt = currentAttempt + 1;
|
|
397
|
+
await delayFn(getRetryDelayMs(nextAttempt));
|
|
398
|
+
const nextRequest = Request.concatAttributes(
|
|
399
|
+
request,
|
|
400
|
+
Attributes.retryAttempt(nextAttempt)
|
|
401
|
+
);
|
|
402
|
+
return retry(nextRequest);
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function defaultDelay(ms) {
|
|
406
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
407
|
+
}
|
|
408
|
+
var requestRetryMiddleware = createRequestRetryMiddleware();
|
|
409
|
+
|
|
410
|
+
// src/client/core/api/api-client.ts
|
|
411
|
+
var ApiClient = class {
|
|
412
|
+
constructor(config, fetchAccessToken) {
|
|
413
|
+
this.config = config;
|
|
414
|
+
this.fetchAccessToken = fetchAccessToken;
|
|
415
|
+
this.httpClient = new HttpClient(this.config, {
|
|
416
|
+
beforeRequest: [
|
|
417
|
+
attachSessionMiddleware(this.fetchAccessToken),
|
|
418
|
+
userAgentMiddleware,
|
|
419
|
+
idempotencyKeyMiddleware
|
|
420
|
+
],
|
|
421
|
+
afterRequest: [
|
|
422
|
+
renewSessionMiddleware(this.fetchAccessToken),
|
|
423
|
+
requestRetryMiddleware
|
|
424
|
+
]
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
async send(request) {
|
|
428
|
+
const response = await this.httpClient.send(request);
|
|
429
|
+
if (response.status >= 200 && response.status < 300) {
|
|
430
|
+
return response.body;
|
|
431
|
+
} else {
|
|
432
|
+
throw new HttpError(response);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// src/shared/types/oauth.ts
|
|
438
|
+
var AuthorizationCode = (value) => value;
|
|
439
|
+
var CodeVerifier = (value) => value;
|
|
440
|
+
var CodeChallenge = (value) => value;
|
|
441
|
+
var RedirectUri = (value) => value;
|
|
442
|
+
var ClientId = (value) => value;
|
|
443
|
+
var ClientSecret = (value) => value;
|
|
444
|
+
|
|
445
|
+
// src/client/core/coinlist-client.ts
|
|
446
|
+
var OAUTH_STATE_KEY = "coinlist.oauth_state";
|
|
447
|
+
var OAUTH_CODE_VERIFIER_KEY = "coinlist.oauth_code_verifier";
|
|
448
|
+
var CoinListClientImpl = class {
|
|
449
|
+
constructor(_config) {
|
|
450
|
+
this._config = _config;
|
|
451
|
+
/** Cached access token: undefined = loading, null = no token, AccessToken = logged in */
|
|
452
|
+
this._accessToken = void 0;
|
|
453
|
+
this._api = new ApiClient(
|
|
454
|
+
{
|
|
455
|
+
baseUrl: _config.baseUrl || BASE_URL,
|
|
456
|
+
xApiVersion: API_VERSION
|
|
457
|
+
},
|
|
458
|
+
this.fetchAccessToken.bind(this)
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
async init() {
|
|
462
|
+
await this.fetchAccessToken(true);
|
|
463
|
+
return this.getAuthState();
|
|
464
|
+
}
|
|
465
|
+
async fetchAccessToken(refresh) {
|
|
466
|
+
if (!refresh && this._accessToken !== void 0) return this._accessToken;
|
|
467
|
+
try {
|
|
468
|
+
const accessToken = await this._config.getAccessToken();
|
|
469
|
+
this._accessToken = accessToken;
|
|
470
|
+
return accessToken;
|
|
471
|
+
} catch (_) {
|
|
472
|
+
this._accessToken = null;
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
getAuthState() {
|
|
477
|
+
if (this._accessToken === void 0) {
|
|
478
|
+
return "unknown";
|
|
479
|
+
}
|
|
480
|
+
return this._accessToken !== null ? "logged-in" : "logged-out";
|
|
481
|
+
}
|
|
482
|
+
async startOAuth() {
|
|
483
|
+
const state = generateSecureRandomBase64Url(32);
|
|
484
|
+
const codeVerifier = generateSecureRandomBase64Url(32);
|
|
485
|
+
const codeChallengeRaw = await sha256(codeVerifier);
|
|
486
|
+
const codeChallenge = arrayBufferToBase64Url(codeChallengeRaw, false);
|
|
487
|
+
sessionStorage.setItem(OAUTH_STATE_KEY, state);
|
|
488
|
+
sessionStorage.setItem(OAUTH_CODE_VERIFIER_KEY, codeVerifier);
|
|
489
|
+
const params = new URLSearchParams({
|
|
490
|
+
client_id: this._config.clientId,
|
|
491
|
+
response_type: "code",
|
|
492
|
+
redirect_uri: this._config.redirectUri,
|
|
493
|
+
code_challenge: codeChallenge,
|
|
494
|
+
code_challenge_method: "S256",
|
|
495
|
+
state
|
|
496
|
+
});
|
|
497
|
+
const url = `${COINLIST_OAUTH_URL}?${params.toString()}`;
|
|
498
|
+
window.location.href = url;
|
|
499
|
+
}
|
|
500
|
+
completeOAuth() {
|
|
501
|
+
const params = new URLSearchParams(window.location.search);
|
|
502
|
+
const stateParam = params.get("state");
|
|
503
|
+
if (!stateParam) {
|
|
504
|
+
return { type: "error", reason: "missing_state" };
|
|
505
|
+
}
|
|
506
|
+
const storedState = sessionStorage.getItem(OAUTH_STATE_KEY);
|
|
507
|
+
if (storedState === null || storedState !== stateParam) {
|
|
508
|
+
return { type: "error", reason: "invalid_state" };
|
|
509
|
+
}
|
|
510
|
+
const codeParam = params.get("code");
|
|
511
|
+
if (!codeParam) {
|
|
512
|
+
return { type: "error", reason: "missing_code" };
|
|
513
|
+
}
|
|
514
|
+
const codeVerifierValue = sessionStorage.getItem(OAUTH_CODE_VERIFIER_KEY);
|
|
515
|
+
if (!codeVerifierValue) {
|
|
516
|
+
return { type: "error", reason: "missing_code_verifier" };
|
|
517
|
+
}
|
|
518
|
+
return {
|
|
519
|
+
type: "ok",
|
|
520
|
+
code: AuthorizationCode(codeParam),
|
|
521
|
+
codeVerifier: CodeVerifier(codeVerifierValue)
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
logout() {
|
|
525
|
+
this._accessToken = null;
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
function createCoinListClient(config) {
|
|
529
|
+
return new CoinListClientImpl(config);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// src/client/CoinListProvider.tsx
|
|
533
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
534
|
+
var CoinListContext = (0, import_react.createContext)(null);
|
|
535
|
+
function CoinListProvider({ config, children }) {
|
|
536
|
+
const [isReady, setIsReady] = (0, import_react.useState)(false);
|
|
537
|
+
const coinlist = (0, import_react.useMemo)(() => createCoinListClient(config), [config]);
|
|
538
|
+
(0, import_react.useEffect)(() => {
|
|
539
|
+
let isCancelled = false;
|
|
540
|
+
if (coinlist.getAuthState() === "unknown") {
|
|
541
|
+
coinlist.init().then(() => {
|
|
542
|
+
if (!isCancelled) {
|
|
543
|
+
setIsReady(true);
|
|
544
|
+
}
|
|
545
|
+
}).catch(() => {
|
|
546
|
+
if (!isCancelled) {
|
|
547
|
+
setIsReady(true);
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
} else {
|
|
551
|
+
setIsReady(true);
|
|
552
|
+
}
|
|
553
|
+
return () => {
|
|
554
|
+
isCancelled = true;
|
|
555
|
+
};
|
|
556
|
+
}, [coinlist]);
|
|
557
|
+
const value = (0, import_react.useMemo)(() => ({ coinlist, isReady }), [coinlist, isReady]);
|
|
558
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CoinListContext.Provider, { value, children });
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// src/client/hooks/useCoinList.ts
|
|
562
|
+
var import_react2 = require("react");
|
|
563
|
+
function useCoinList() {
|
|
564
|
+
const context = (0, import_react2.useContext)(CoinListContext);
|
|
565
|
+
if (context === null) {
|
|
566
|
+
throw new CoinListClientInitializationError();
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
isReady: context.isReady,
|
|
570
|
+
coinlist: context.coinlist
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
var CoinListClientInitializationError = class extends Error {
|
|
574
|
+
constructor(message = "useCoinList() must be used within CoinListProvider. Wrap your app with <CoinListProvider config={...}>.") {
|
|
575
|
+
super(message);
|
|
576
|
+
this.name = "CoinListClientInitializationError";
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
// src/shared/types/oauth-session.ts
|
|
581
|
+
var OAuthRefreshToken = (value) => value;
|
|
582
|
+
var OAuthSession = {
|
|
583
|
+
fromDto: (dto) => {
|
|
584
|
+
const expiresAt = new Date(Date.now() + dto.expires_in * 1e3);
|
|
585
|
+
return {
|
|
586
|
+
accessToken: {
|
|
587
|
+
value: dto.access_token,
|
|
588
|
+
expiresAt
|
|
589
|
+
},
|
|
590
|
+
...dto.refresh_token != null && dto.refresh_token !== "" ? { refreshToken: OAuthRefreshToken(dto.refresh_token) } : void 0
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
// src/shared/types/user.ts
|
|
596
|
+
var UserId = (value) => value;
|
|
597
|
+
var UserEmail = (value) => value;
|
|
598
|
+
var User = {
|
|
599
|
+
fromDto: (dto) => ({
|
|
600
|
+
id: UserId(dto.id),
|
|
601
|
+
email: UserEmail(dto.email)
|
|
602
|
+
})
|
|
603
|
+
};
|
|
604
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
605
|
+
0 && (module.exports = {
|
|
606
|
+
AuthorizationCode,
|
|
607
|
+
ClientId,
|
|
608
|
+
ClientSecret,
|
|
609
|
+
CodeChallenge,
|
|
610
|
+
CodeVerifier,
|
|
611
|
+
CoinListProvider,
|
|
612
|
+
OAuthRefreshToken,
|
|
613
|
+
OAuthSession,
|
|
614
|
+
RedirectUri,
|
|
615
|
+
User,
|
|
616
|
+
UserEmail,
|
|
617
|
+
UserId,
|
|
618
|
+
createCoinListClient,
|
|
619
|
+
useCoinList
|
|
620
|
+
});
|
|
621
|
+
//# sourceMappingURL=index.cjs.map
|