@takeal/cusfront-sdk 0.1.0 → 0.3.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.
- package/CHANGELOG.md +104 -2
- package/README.md +393 -14
- package/dist/devices-C3JHi2Hi.d.cts +165 -0
- package/dist/devices-DP_hxwmW.d.ts +165 -0
- package/dist/{http-BkZZZI8K.d.cts → http-B-OQwMYn.d.cts} +13 -2
- package/dist/{http-BkZZZI8K.d.ts → http-B-OQwMYn.d.ts} +13 -2
- package/dist/index.cjs +425 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +77 -8
- package/dist/index.d.ts +77 -8
- package/dist/index.js +422 -26
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +133 -6
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +65 -3
- package/dist/react/index.d.ts +65 -3
- package/dist/react/index.js +131 -8
- package/dist/react/index.js.map +1 -1
- package/dist/resources/auth.cjs +73 -15
- package/dist/resources/auth.cjs.map +1 -1
- package/dist/resources/auth.d.cts +100 -9
- package/dist/resources/auth.d.ts +100 -9
- package/dist/resources/auth.js +73 -15
- package/dist/resources/auth.js.map +1 -1
- package/dist/resources/balance.cjs +4 -3
- package/dist/resources/balance.cjs.map +1 -1
- package/dist/resources/balance.d.cts +8 -9
- package/dist/resources/balance.d.ts +8 -9
- package/dist/resources/balance.js +4 -3
- package/dist/resources/balance.js.map +1 -1
- package/dist/resources/blog.d.cts +1 -1
- package/dist/resources/blog.d.ts +1 -1
- package/dist/resources/branding.cjs.map +1 -1
- package/dist/resources/branding.d.cts +8 -2
- package/dist/resources/branding.d.ts +8 -2
- package/dist/resources/branding.js.map +1 -1
- package/dist/resources/cards.cjs +33 -1
- package/dist/resources/cards.cjs.map +1 -1
- package/dist/resources/cards.d.cts +105 -4
- package/dist/resources/cards.d.ts +105 -4
- package/dist/resources/cards.js +33 -1
- package/dist/resources/cards.js.map +1 -1
- package/dist/resources/deposits.d.cts +1 -1
- package/dist/resources/deposits.d.ts +1 -1
- package/dist/resources/devices.cjs +92 -0
- package/dist/resources/devices.cjs.map +1 -0
- package/dist/resources/devices.d.cts +3 -0
- package/dist/resources/devices.d.ts +3 -0
- package/dist/resources/devices.js +89 -0
- package/dist/resources/devices.js.map +1 -0
- package/dist/resources/me.cjs +32 -0
- package/dist/resources/me.cjs.map +1 -0
- package/dist/resources/me.d.cts +40 -0
- package/dist/resources/me.d.ts +40 -0
- package/dist/resources/me.js +30 -0
- package/dist/resources/me.js.map +1 -0
- package/dist/resources/notifications.cjs +53 -0
- package/dist/resources/notifications.cjs.map +1 -0
- package/dist/resources/notifications.d.cts +109 -0
- package/dist/resources/notifications.d.ts +109 -0
- package/dist/resources/notifications.js +51 -0
- package/dist/resources/notifications.js.map +1 -0
- package/dist/resources/push.cjs +65 -0
- package/dist/resources/push.cjs.map +1 -0
- package/dist/resources/push.d.cts +76 -0
- package/dist/resources/push.d.ts +76 -0
- package/dist/resources/push.js +62 -0
- package/dist/resources/push.js.map +1 -0
- package/dist/resources/sessions.cjs +26 -0
- package/dist/resources/sessions.cjs.map +1 -0
- package/dist/resources/sessions.d.cts +43 -0
- package/dist/resources/sessions.d.ts +43 -0
- package/dist/resources/sessions.js +24 -0
- package/dist/resources/sessions.js.map +1 -0
- package/dist/resources/subscriptions.cjs.map +1 -1
- package/dist/resources/subscriptions.d.cts +8 -1
- package/dist/resources/subscriptions.d.ts +8 -1
- package/dist/resources/subscriptions.js.map +1 -1
- package/dist/resources/wallet.cjs +24 -0
- package/dist/resources/wallet.cjs.map +1 -0
- package/dist/resources/wallet.d.cts +39 -0
- package/dist/resources/wallet.d.ts +39 -0
- package/dist/resources/wallet.js +22 -0
- package/dist/resources/wallet.js.map +1 -0
- package/dist/telegram.cjs +466 -28
- package/dist/telegram.cjs.map +1 -1
- package/dist/telegram.d.cts +65 -7
- package/dist/telegram.d.ts +65 -7
- package/dist/telegram.js +465 -29
- package/dist/telegram.js.map +1 -1
- package/package.json +37 -3
package/dist/index.js
CHANGED
|
@@ -25,11 +25,21 @@ function isNetworkError(e) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
// src/http.ts
|
|
28
|
+
var API_VERSION = "/v1";
|
|
29
|
+
var UNVERSIONED = /* @__PURE__ */ new Set(["/health", "/ready", "/branding", "/favicon.ico"]);
|
|
30
|
+
function versionedPath(path) {
|
|
31
|
+
const p = path.startsWith("/") ? path : `/${path}`;
|
|
32
|
+
const bare = p.split("?")[0];
|
|
33
|
+
if (UNVERSIONED.has(bare)) return p;
|
|
34
|
+
if (bare === API_VERSION || bare.startsWith(`${API_VERSION}/`)) return p;
|
|
35
|
+
return `${API_VERSION}${p}`;
|
|
36
|
+
}
|
|
28
37
|
var HttpClient = class {
|
|
29
|
-
constructor(baseUrl, tokens, fetchImpl) {
|
|
38
|
+
constructor(baseUrl, tokens, fetchImpl, defaultHeaders = {}) {
|
|
30
39
|
this.baseUrl = baseUrl;
|
|
31
40
|
this.tokens = tokens;
|
|
32
41
|
this.fetchImpl = fetchImpl;
|
|
42
|
+
this.defaultHeaders = defaultHeaders;
|
|
33
43
|
if (!baseUrl) throw new Error("HttpClient: baseUrl is required");
|
|
34
44
|
}
|
|
35
45
|
async get(path, opts = {}) {
|
|
@@ -50,7 +60,8 @@ var HttpClient = class {
|
|
|
50
60
|
async request(method, path, opts) {
|
|
51
61
|
const url = this.absolute(path);
|
|
52
62
|
const headers = {
|
|
53
|
-
Accept: "application/json"
|
|
63
|
+
Accept: "application/json",
|
|
64
|
+
...this.defaultHeaders
|
|
54
65
|
};
|
|
55
66
|
if (opts.body !== void 0) headers["Content-Type"] = "application/json";
|
|
56
67
|
if (opts.idempotencyKey) headers["Idempotency-Key"] = opts.idempotencyKey;
|
|
@@ -81,8 +92,7 @@ var HttpClient = class {
|
|
|
81
92
|
absolute(path) {
|
|
82
93
|
if (path.startsWith("http://") || path.startsWith("https://")) return path;
|
|
83
94
|
const base = this.baseUrl.replace(/\/+$/, "");
|
|
84
|
-
|
|
85
|
-
return `${base}${tail}`;
|
|
95
|
+
return `${base}${versionedPath(path)}`;
|
|
86
96
|
}
|
|
87
97
|
};
|
|
88
98
|
async function parseResponse(resp) {
|
|
@@ -147,6 +157,40 @@ var AuthResource = class {
|
|
|
147
157
|
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
148
158
|
return resp;
|
|
149
159
|
}
|
|
160
|
+
/** Create an end-user account and sign it in. The JWT is stored.
|
|
161
|
+
* `wallet_currency` must be one the deployment offers; omit it to take
|
|
162
|
+
* the deployment default (it can be switched later via `client.wallet`
|
|
163
|
+
* while the wallet is empty).
|
|
164
|
+
* Errors: 400 `validation` / `currency_not_allowed`, 409 email taken. */
|
|
165
|
+
async register(input) {
|
|
166
|
+
const resp = await this.http.post("/auth/register", {
|
|
167
|
+
body: input,
|
|
168
|
+
skipAuth: true
|
|
169
|
+
});
|
|
170
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
171
|
+
return resp;
|
|
172
|
+
}
|
|
173
|
+
/** Finish a `totp_setup_required` login: send the first code from the
|
|
174
|
+
* authenticator app. The JWT is stored. The response carries ten
|
|
175
|
+
* one-time `backup_codes` that are never returned again, so show them to
|
|
176
|
+
* the user right away and don't keep them on the device. */
|
|
177
|
+
async completeTotpSetup(input) {
|
|
178
|
+
const resp = await this.http.post("/auth/login/totp-setup", {
|
|
179
|
+
body: input,
|
|
180
|
+
skipAuth: true
|
|
181
|
+
});
|
|
182
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
183
|
+
return resp;
|
|
184
|
+
}
|
|
185
|
+
/** Change the signed-in user's password. Every other session of the
|
|
186
|
+
* account is signed out; this one stays valid.
|
|
187
|
+
* Errors: 400 when the new password fails the policy, 401 when the
|
|
188
|
+
* current one is wrong. */
|
|
189
|
+
async changePassword(input) {
|
|
190
|
+
return this.http.post("/auth/password", {
|
|
191
|
+
body: input
|
|
192
|
+
});
|
|
193
|
+
}
|
|
150
194
|
/** Swap the 2FA method mid-flow. Resolves to `otp_sent` for
|
|
151
195
|
* telegram/email; for TOTP it short-circuits back to the challenge. */
|
|
152
196
|
async switchMethod(input) {
|
|
@@ -155,18 +199,38 @@ var AuthResource = class {
|
|
|
155
199
|
skipAuth: true
|
|
156
200
|
});
|
|
157
201
|
}
|
|
158
|
-
/** Exchange a signed Telegram Mini App `initData` payload for a session
|
|
159
|
-
* JWT. The server validates the initData HMAC against the deployment's bot
|
|
160
|
-
* token, then find-or-creates the end-user. First-time Telegram users are
|
|
161
|
-
* auto-provisioned with `user.email_pending === true` — prompt them for a
|
|
162
|
-
* real email and call {@link linkEmail}. Stores the JWT on success.
|
|
163
|
-
*
|
|
164
|
-
* Most consumers use the higher-level `fromInitData` / `fromTelegramWebApp`
|
|
165
|
-
* helpers in `@takeal/cusfront-sdk/telegram`; reach for this directly when
|
|
166
|
-
* you already hold a configured client. */
|
|
167
202
|
async exchangeTelegram(input) {
|
|
168
|
-
const
|
|
169
|
-
|
|
203
|
+
const body = { init_data: input.initData };
|
|
204
|
+
if (input.provision !== void 0) body.provision = input.provision;
|
|
205
|
+
let resp;
|
|
206
|
+
try {
|
|
207
|
+
resp = await this.http.post("/auth/telegram/exchange", {
|
|
208
|
+
body,
|
|
209
|
+
skipAuth: true
|
|
210
|
+
});
|
|
211
|
+
} catch (e) {
|
|
212
|
+
if (isApiError(e) && e.status === 404 && e.code === "telegram_unknown") {
|
|
213
|
+
return { stage: "unknown" };
|
|
214
|
+
}
|
|
215
|
+
throw e;
|
|
216
|
+
}
|
|
217
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
218
|
+
return resp;
|
|
219
|
+
}
|
|
220
|
+
/** Attach the Telegram account behind `initData` to an existing email +
|
|
221
|
+
* password account and sign in as that account. No bearer needed; the
|
|
222
|
+
* password is checked the same way `login` checks it. The JWT is stored.
|
|
223
|
+
*
|
|
224
|
+
* Errors: 401 `unauthenticated` (wrong email or password, as with
|
|
225
|
+
* `login`) / `invalid_init_data`; 403
|
|
226
|
+
* `user_disabled` / `second_factor_required` (accounts that sign in with
|
|
227
|
+
* a second factor can't be linked from a Mini App); 409
|
|
228
|
+
* `telegram_already_linked_other` (this account already has a different
|
|
229
|
+
* Telegram; unlink it first) / `telegram_in_use` (this Telegram belongs to
|
|
230
|
+
* another account); 503 `telegram_not_configured`. */
|
|
231
|
+
async linkTelegram(input) {
|
|
232
|
+
const resp = await this.http.post("/auth/telegram/link", {
|
|
233
|
+
body: { init_data: input.initData, email: input.email, password: input.password },
|
|
170
234
|
skipAuth: true
|
|
171
235
|
});
|
|
172
236
|
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
@@ -183,10 +247,9 @@ var AuthResource = class {
|
|
|
183
247
|
async me() {
|
|
184
248
|
return this.http.get("/auth/me");
|
|
185
249
|
}
|
|
186
|
-
/** Local sign-out.
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* with a future session table. */
|
|
250
|
+
/** Local sign-out: forgets the stored JWT. To end the session on the
|
|
251
|
+
* server as well, revoke it first with `client.sessions.revoke(jti)`
|
|
252
|
+
* (the current one is marked `current: true` in `client.sessions.list()`). */
|
|
190
253
|
async signOut() {
|
|
191
254
|
await Promise.resolve(this.tokens.clear());
|
|
192
255
|
}
|
|
@@ -251,12 +314,44 @@ var CardsResource = class {
|
|
|
251
314
|
* `failure_reason` on the result.
|
|
252
315
|
*/
|
|
253
316
|
async create(input) {
|
|
254
|
-
const { idempotencyKey, ...
|
|
317
|
+
const { idempotencyKey, productId, name, designId, ...rest } = input;
|
|
318
|
+
const body = { ...rest };
|
|
319
|
+
if (productId !== void 0) body["product_id"] = productId;
|
|
320
|
+
if (name !== void 0) body["name"] = name;
|
|
321
|
+
if (designId !== void 0) body["design_id"] = designId;
|
|
255
322
|
return this.http.post("/me/cards", {
|
|
256
323
|
body,
|
|
257
324
|
idempotencyKey
|
|
258
325
|
});
|
|
259
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* The cards the caller can order, in the order the deployment offers them
|
|
329
|
+
* (the default product first). Each item carries its name, description,
|
|
330
|
+
* artwork, the issuance price after the caller's subscription discount,
|
|
331
|
+
* and the currency. Pass an item's `id` as `productId` to {@link create}.
|
|
332
|
+
*/
|
|
333
|
+
async listProducts() {
|
|
334
|
+
return this.http.get("/me/card-products");
|
|
335
|
+
}
|
|
336
|
+
/** One product from the catalog. 404 when unknown, disabled or hidden. */
|
|
337
|
+
async getProduct(id) {
|
|
338
|
+
return this.http.get(
|
|
339
|
+
`/me/card-products/${encodeURIComponent(id)}`
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Rename the caller's own card or change its design. Resolves to the
|
|
344
|
+
* updated card. 404 if the card isn't the caller's; 400 `invalid_name` /
|
|
345
|
+
* `design_not_allowed` on a bad value.
|
|
346
|
+
*/
|
|
347
|
+
async update(id, input) {
|
|
348
|
+
const body = {};
|
|
349
|
+
if (input.name !== void 0) body["name"] = input.name;
|
|
350
|
+
if (input.designId !== void 0) body["design_id"] = input.designId;
|
|
351
|
+
return this.http.patch(`/me/cards/${encodeURIComponent(id)}`, {
|
|
352
|
+
body
|
|
353
|
+
});
|
|
354
|
+
}
|
|
260
355
|
/** Fetch one card by id. */
|
|
261
356
|
async get(id) {
|
|
262
357
|
return this.http.get(`/me/cards/${encodeURIComponent(id)}`);
|
|
@@ -331,11 +426,12 @@ var BalanceResource = class {
|
|
|
331
426
|
this.http = http;
|
|
332
427
|
}
|
|
333
428
|
/**
|
|
334
|
-
* Fetch the user's wallet balance
|
|
335
|
-
*
|
|
336
|
-
*
|
|
429
|
+
* Fetch the user's wallet balance. Omit `currency` for the wallet's own
|
|
430
|
+
* currency; pass an ISO 4217 code (e.g. `"USD"`) to read another one —
|
|
431
|
+
* the ledger holds a separate balance per currency.
|
|
337
432
|
*/
|
|
338
433
|
async get(currency) {
|
|
434
|
+
if (!currency) return this.http.get("/me/balance");
|
|
339
435
|
const q = new URLSearchParams({ currency }).toString();
|
|
340
436
|
return this.http.get(`/me/balance?${q}`);
|
|
341
437
|
}
|
|
@@ -352,6 +448,25 @@ var BrandingResource = class {
|
|
|
352
448
|
}
|
|
353
449
|
};
|
|
354
450
|
|
|
451
|
+
// src/resources/wallet.ts
|
|
452
|
+
var WalletResource = class {
|
|
453
|
+
constructor(http) {
|
|
454
|
+
this.http = http;
|
|
455
|
+
}
|
|
456
|
+
/** Current wallet currency + the currencies the user may switch to. */
|
|
457
|
+
async get(opts = {}) {
|
|
458
|
+
return this.http.get("/me/wallet", { signal: opts.signal });
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Switch the wallet currency. Fails with `409 wallet_not_empty` while any
|
|
462
|
+
* balance is non-zero, and `400 currency_not_allowed` for a currency the
|
|
463
|
+
* deployment does not offer.
|
|
464
|
+
*/
|
|
465
|
+
async set(currency, opts = {}) {
|
|
466
|
+
return this.http.put("/me/wallet", { body: { currency }, signal: opts.signal });
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
|
|
355
470
|
// src/resources/blog.ts
|
|
356
471
|
var BlogResource = class {
|
|
357
472
|
constructor(http) {
|
|
@@ -405,6 +520,265 @@ var SubscriptionsResource = class {
|
|
|
405
520
|
}
|
|
406
521
|
};
|
|
407
522
|
|
|
523
|
+
// src/resources/sessions.ts
|
|
524
|
+
var SessionsResource = class {
|
|
525
|
+
constructor(http) {
|
|
526
|
+
this.http = http;
|
|
527
|
+
}
|
|
528
|
+
/** All live sessions of the account, this one included. */
|
|
529
|
+
async list() {
|
|
530
|
+
return this.http.get("/me/sessions");
|
|
531
|
+
}
|
|
532
|
+
/** Sign out one session. Works on the current one too; the stored JWT
|
|
533
|
+
* stops working, so follow with `client.auth.signOut()`.
|
|
534
|
+
* 404 when the session is unknown or already signed out. */
|
|
535
|
+
async revoke(jti) {
|
|
536
|
+
await this.http.delete(`/me/sessions/${encodeURIComponent(jti)}`);
|
|
537
|
+
}
|
|
538
|
+
/** Sign out every session except this one. */
|
|
539
|
+
async revokeOthers() {
|
|
540
|
+
return this.http.post("/me/sessions/terminate-others");
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// src/resources/push.ts
|
|
545
|
+
var PushResource = class {
|
|
546
|
+
constructor(http) {
|
|
547
|
+
this.http = http;
|
|
548
|
+
}
|
|
549
|
+
/** Whether the user is subscribed, plus the VAPID key to subscribe with. */
|
|
550
|
+
async status() {
|
|
551
|
+
return this.http.get("/me/push-subscription");
|
|
552
|
+
}
|
|
553
|
+
/** Store a subscription you created yourself.
|
|
554
|
+
* 409 `push_disabled` when the deployment has no VAPID keys.
|
|
555
|
+
* @deprecated Use `client.devices.register({ kind: "webpush", ... })`. */
|
|
556
|
+
async save(input) {
|
|
557
|
+
return this.http.put("/me/push-subscription", { body: input });
|
|
558
|
+
}
|
|
559
|
+
/** Delete the stored subscription. Safe when nothing is stored.
|
|
560
|
+
* @deprecated Use `client.devices.remove(id)` or `client.devices.unregisterWebPush(reg)`. */
|
|
561
|
+
async remove() {
|
|
562
|
+
await this.http.delete("/me/push-subscription");
|
|
563
|
+
}
|
|
564
|
+
/** Subscribe this browser and store the subscription. Ask for
|
|
565
|
+
* notification permission before calling. Throws when push isn't set up
|
|
566
|
+
* on the deployment.
|
|
567
|
+
* @deprecated Use `client.devices.registerWebPush(registration)`. */
|
|
568
|
+
async enable(registration) {
|
|
569
|
+
const { vapid_public_key } = await this.status();
|
|
570
|
+
if (!vapid_public_key) {
|
|
571
|
+
throw new Error("@takeal/cusfront-sdk: push notifications are not enabled on this deployment");
|
|
572
|
+
}
|
|
573
|
+
const sub = await registration.pushManager.subscribe({
|
|
574
|
+
userVisibleOnly: true,
|
|
575
|
+
applicationServerKey: base64UrlToBytes(vapid_public_key)
|
|
576
|
+
});
|
|
577
|
+
const json = sub.toJSON();
|
|
578
|
+
if (!json.endpoint || !json.keys?.p256dh || !json.keys.auth) {
|
|
579
|
+
throw new Error("@takeal/cusfront-sdk: the browser returned an incomplete push subscription");
|
|
580
|
+
}
|
|
581
|
+
return this.save({
|
|
582
|
+
endpoint: json.endpoint,
|
|
583
|
+
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
/** Unsubscribe this browser and delete the stored subscription.
|
|
587
|
+
* @deprecated Use `client.devices.unregisterWebPush(registration)`. */
|
|
588
|
+
async disable(registration) {
|
|
589
|
+
const sub = await registration.pushManager.getSubscription();
|
|
590
|
+
if (sub) await sub.unsubscribe();
|
|
591
|
+
await this.remove();
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
function base64UrlToBytes(input) {
|
|
595
|
+
const b64 = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
596
|
+
const padded = b64 + "=".repeat((4 - b64.length % 4) % 4);
|
|
597
|
+
const bin = atob(padded);
|
|
598
|
+
const out = new Uint8Array(bin.length);
|
|
599
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
600
|
+
return out;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// src/resources/me.ts
|
|
604
|
+
var MeResource = class {
|
|
605
|
+
constructor(http) {
|
|
606
|
+
this.http = http;
|
|
607
|
+
}
|
|
608
|
+
/** Attach the Telegram account behind `initData` to the signed-in user.
|
|
609
|
+
* Returns the updated user (`telegram` is set). Repeating the call with
|
|
610
|
+
* the same Telegram is fine.
|
|
611
|
+
*
|
|
612
|
+
* Errors: 401 `invalid_init_data`; 403 `user_disabled` /
|
|
613
|
+
* `second_factor_required`; 409 `telegram_already_linked_other` (the
|
|
614
|
+
* account already has a different Telegram; `unlinkTelegram` first) /
|
|
615
|
+
* `telegram_in_use` (that Telegram belongs to another account); 503
|
|
616
|
+
* `telegram_not_configured`. */
|
|
617
|
+
async linkTelegram(initData) {
|
|
618
|
+
return this.http.post("/me/telegram", { body: { init_data: initData } });
|
|
619
|
+
}
|
|
620
|
+
/** Detach the Telegram account from the signed-in user. Returns the
|
|
621
|
+
* updated user (`telegram` is `null`).
|
|
622
|
+
*
|
|
623
|
+
* Errors: 404 `telegram_not_linked`; 409 `last_credential` when the
|
|
624
|
+
* account has no password, so Telegram is its only way to sign in. */
|
|
625
|
+
async unlinkTelegram() {
|
|
626
|
+
return this.http.delete("/me/telegram");
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// src/resources/notifications.ts
|
|
631
|
+
var NotificationsResource = class {
|
|
632
|
+
constructor(http) {
|
|
633
|
+
this.http = http;
|
|
634
|
+
}
|
|
635
|
+
/** One page of the inbox, newest first. */
|
|
636
|
+
async list(params = {}) {
|
|
637
|
+
const q = new URLSearchParams();
|
|
638
|
+
if (params.cursor) q.set("cursor", params.cursor);
|
|
639
|
+
if (params.limit !== void 0) q.set("limit", String(params.limit));
|
|
640
|
+
if (params.unreadOnly) q.set("unread", "true");
|
|
641
|
+
if (params.category) q.set("category", params.category);
|
|
642
|
+
const qs = q.toString();
|
|
643
|
+
return this.http.get(`/me/notifications${qs ? `?${qs}` : ""}`);
|
|
644
|
+
}
|
|
645
|
+
/** How many items are unread. Cheap; fine to poll for a badge. */
|
|
646
|
+
async unreadCount() {
|
|
647
|
+
const { unread } = await this.http.get("/me/notifications/unread-count");
|
|
648
|
+
return unread;
|
|
649
|
+
}
|
|
650
|
+
/** Mark one item read. Returns the item; calling it again is harmless. */
|
|
651
|
+
async markRead(id) {
|
|
652
|
+
return this.http.post(`/me/notifications/${encodeURIComponent(id)}/read`);
|
|
653
|
+
}
|
|
654
|
+
/** Mark every unread item read, or only those created before `before`
|
|
655
|
+
* (ISO timestamp). Returns how many changed. */
|
|
656
|
+
async markAllRead(before) {
|
|
657
|
+
return this.http.post("/me/notifications/read-all", {
|
|
658
|
+
body: before ? { before } : {}
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
/** Delete one item. 404 when it isn't the user's. */
|
|
662
|
+
async remove(id) {
|
|
663
|
+
await this.http.delete(`/me/notifications/${encodeURIComponent(id)}`);
|
|
664
|
+
}
|
|
665
|
+
/** The user's channel and language settings. */
|
|
666
|
+
async getPreferences() {
|
|
667
|
+
return this.http.get("/me/notification-preferences");
|
|
668
|
+
}
|
|
669
|
+
/** Change some settings; the rest stay. Returns the stored view.
|
|
670
|
+
* 400 `validation` for an unknown category, channel or locale. */
|
|
671
|
+
async setPreferences(input) {
|
|
672
|
+
return this.http.put("/me/notification-preferences", {
|
|
673
|
+
body: input
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// src/resources/devices.ts
|
|
679
|
+
function devicePlatformOf(platform) {
|
|
680
|
+
return platform === "telegram-miniapp" ? "telegram" : platform;
|
|
681
|
+
}
|
|
682
|
+
var DevicesResource = class {
|
|
683
|
+
constructor(http, client) {
|
|
684
|
+
this.http = http;
|
|
685
|
+
this.client = client;
|
|
686
|
+
}
|
|
687
|
+
/** Every device the user has, disabled ones included. */
|
|
688
|
+
async list() {
|
|
689
|
+
return this.http.get("/me/devices");
|
|
690
|
+
}
|
|
691
|
+
/** Register a device, or refresh it when the same credential is already
|
|
692
|
+
* known (then `last_seen_at`, `label` and `app_version` update and a
|
|
693
|
+
* disabled device comes back). `app_version` defaults to the client
|
|
694
|
+
* version sent in `X-Takeal-Client`.
|
|
695
|
+
*
|
|
696
|
+
* Errors: 400 `validation`; 409 `push_disabled` for `webpush` when the
|
|
697
|
+
* deployment has no VAPID keys. */
|
|
698
|
+
async register(input) {
|
|
699
|
+
return this.http.put("/me/devices", {
|
|
700
|
+
body: { app_version: this.client.version, ...input }
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
/** Delete one device. 404 when it isn't the user's. */
|
|
704
|
+
async remove(id) {
|
|
705
|
+
await this.http.delete(`/me/devices/${encodeURIComponent(id)}`);
|
|
706
|
+
}
|
|
707
|
+
/** Subscribe this browser to Web Push and register it as a device. Ask
|
|
708
|
+
* for notification permission before calling. Throws when push isn't
|
|
709
|
+
* set up on the deployment. */
|
|
710
|
+
async registerWebPush(registration, opts = {}) {
|
|
711
|
+
const key = opts.vapidPublicKey ?? await this.vapidPublicKey();
|
|
712
|
+
if (!key) {
|
|
713
|
+
throw new Error("@takeal/cusfront-sdk: push notifications are not enabled on this deployment");
|
|
714
|
+
}
|
|
715
|
+
const sub = await registration.pushManager.subscribe({
|
|
716
|
+
userVisibleOnly: true,
|
|
717
|
+
applicationServerKey: base64UrlToBytes(key)
|
|
718
|
+
});
|
|
719
|
+
const json = sub.toJSON();
|
|
720
|
+
if (!json.endpoint || !json.keys?.p256dh || !json.keys.auth) {
|
|
721
|
+
throw new Error("@takeal/cusfront-sdk: the browser returned an incomplete push subscription");
|
|
722
|
+
}
|
|
723
|
+
return this.register({
|
|
724
|
+
kind: "webpush",
|
|
725
|
+
platform: devicePlatformOf(this.client.platform),
|
|
726
|
+
credential: {
|
|
727
|
+
endpoint: json.endpoint,
|
|
728
|
+
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
729
|
+
},
|
|
730
|
+
...opts.label ? { label: opts.label } : {}
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
/** Unsubscribe this browser from Web Push and delete the matching device
|
|
734
|
+
* (see {@link UnregisterWebPushOptions}). Safe when nothing is
|
|
735
|
+
* registered. */
|
|
736
|
+
async unregisterWebPush(registration, opts = {}) {
|
|
737
|
+
const sub = await registration.pushManager.getSubscription();
|
|
738
|
+
if (sub) await sub.unsubscribe();
|
|
739
|
+
if (opts.deviceId) {
|
|
740
|
+
await this.remove(opts.deviceId);
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
const mine = (await this.list()).filter((d) => d.kind === "webpush" && d.is_current);
|
|
744
|
+
for (const d of mine) await this.remove(d.id);
|
|
745
|
+
}
|
|
746
|
+
async vapidPublicKey() {
|
|
747
|
+
const { vapid_public_key } = await this.http.get(
|
|
748
|
+
"/me/push-subscription"
|
|
749
|
+
);
|
|
750
|
+
return vapid_public_key ?? null;
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
// src/version.ts
|
|
755
|
+
var SDK_VERSION = "0.3.0";
|
|
756
|
+
|
|
757
|
+
// src/client-platform.ts
|
|
758
|
+
var CLIENT_HEADER = "X-Takeal-Client";
|
|
759
|
+
function detectClientPlatform() {
|
|
760
|
+
const g = globalThis;
|
|
761
|
+
const initData = g.Telegram?.WebApp?.initData;
|
|
762
|
+
if (typeof initData === "string" && initData.length > 0) return "telegram-miniapp";
|
|
763
|
+
if (g.navigator?.standalone === true) return "pwa";
|
|
764
|
+
if (typeof g.matchMedia === "function") {
|
|
765
|
+
try {
|
|
766
|
+
if (g.matchMedia("(display-mode: standalone)").matches) return "pwa";
|
|
767
|
+
} catch {
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return "web";
|
|
771
|
+
}
|
|
772
|
+
function resolveClientDescriptor(given) {
|
|
773
|
+
return {
|
|
774
|
+
platform: given?.platform ?? detectClientPlatform(),
|
|
775
|
+
version: given?.version || SDK_VERSION
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
function clientHeaderValue(desc) {
|
|
779
|
+
return `${desc.platform}; ${desc.version}`;
|
|
780
|
+
}
|
|
781
|
+
|
|
408
782
|
// src/token-store.ts
|
|
409
783
|
function defaultBrowserStore(key = "takeal_jwt") {
|
|
410
784
|
const fallback = inMemoryStore();
|
|
@@ -441,21 +815,43 @@ function inMemoryStore() {
|
|
|
441
815
|
};
|
|
442
816
|
}
|
|
443
817
|
|
|
818
|
+
// src/assets.ts
|
|
819
|
+
function resolveAssetUrl(baseUrl, url) {
|
|
820
|
+
if (isAbsolute(url)) return url;
|
|
821
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
822
|
+
const path = url.startsWith("/") ? url : `/${url}`;
|
|
823
|
+
return `${base}${path}`;
|
|
824
|
+
}
|
|
825
|
+
function isAbsolute(url) {
|
|
826
|
+
return /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(url);
|
|
827
|
+
}
|
|
828
|
+
|
|
444
829
|
// src/index.ts
|
|
445
830
|
function createClient(opts) {
|
|
446
831
|
const tokens = opts.tokenStore ?? pickDefaultStore();
|
|
447
832
|
const fetchImpl = opts.fetch ?? defaultFetch();
|
|
448
|
-
const
|
|
833
|
+
const client = resolveClientDescriptor(opts.client);
|
|
834
|
+
const http = new HttpClient(opts.baseUrl, tokens, fetchImpl, {
|
|
835
|
+
[CLIENT_HEADER]: clientHeaderValue(client)
|
|
836
|
+
});
|
|
449
837
|
return {
|
|
450
838
|
brand: opts.brand,
|
|
839
|
+
client,
|
|
451
840
|
tokens,
|
|
841
|
+
assetUrl: (url) => resolveAssetUrl(opts.baseUrl, url),
|
|
452
842
|
auth: new AuthResource(http, tokens),
|
|
453
843
|
deposits: new DepositsResource(http),
|
|
454
844
|
cards: new CardsResource(http),
|
|
455
845
|
balance: new BalanceResource(http),
|
|
846
|
+
wallet: new WalletResource(http),
|
|
456
847
|
branding: new BrandingResource(http),
|
|
457
848
|
blog: new BlogResource(http),
|
|
458
|
-
subscriptions: new SubscriptionsResource(http)
|
|
849
|
+
subscriptions: new SubscriptionsResource(http),
|
|
850
|
+
sessions: new SessionsResource(http),
|
|
851
|
+
push: new PushResource(http),
|
|
852
|
+
me: new MeResource(http),
|
|
853
|
+
notifications: new NotificationsResource(http),
|
|
854
|
+
devices: new DevicesResource(http, client)
|
|
459
855
|
};
|
|
460
856
|
}
|
|
461
857
|
function pickDefaultStore() {
|
|
@@ -473,6 +869,6 @@ function defaultFetch() {
|
|
|
473
869
|
);
|
|
474
870
|
}
|
|
475
871
|
|
|
476
|
-
export { ApiError, NetworkError, createClient, defaultBrowserStore, inMemoryStore, isApiError, isNetworkError };
|
|
872
|
+
export { ApiError, CLIENT_HEADER, NetworkError, SDK_VERSION, createClient, defaultBrowserStore, detectClientPlatform, inMemoryStore, isApiError, isNetworkError, resolveAssetUrl };
|
|
477
873
|
//# sourceMappingURL=index.js.map
|
|
478
874
|
//# sourceMappingURL=index.js.map
|