@takeal/cusfront-sdk 0.2.8 → 0.3.7
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 +100 -0
- package/README.md +367 -5
- package/dist/devices-C3JHi2Hi.d.cts +165 -0
- package/dist/devices-DP_hxwmW.d.ts +165 -0
- package/dist/{http-BkCfOCR0.d.cts → http-B-OQwMYn.d.cts} +9 -1
- package/dist/{http-BkCfOCR0.d.ts → http-B-OQwMYn.d.ts} +9 -1
- package/dist/index.cjs +284 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +65 -9
- package/dist/index.d.ts +65 -9
- package/dist/index.js +280 -21
- 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 +62 -3
- package/dist/react/index.d.ts +62 -3
- package/dist/react/index.js +131 -8
- package/dist/react/index.js.map +1 -1
- package/dist/resources/auth.cjs +36 -11
- package/dist/resources/auth.cjs.map +1 -1
- package/dist/resources/auth.d.cts +57 -5
- package/dist/resources/auth.d.ts +57 -5
- package/dist/resources/auth.js +36 -11
- package/dist/resources/auth.js.map +1 -1
- package/dist/resources/balance.d.cts +1 -1
- package/dist/resources/balance.d.ts +1 -1
- package/dist/resources/blog.d.cts +1 -1
- package/dist/resources/blog.d.ts +1 -1
- package/dist/resources/branding.d.cts +1 -1
- package/dist/resources/branding.d.ts +1 -1
- package/dist/resources/cards.cjs +33 -1
- package/dist/resources/cards.cjs.map +1 -1
- package/dist/resources/cards.d.cts +111 -4
- package/dist/resources/cards.d.ts +111 -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 +57 -0
- package/dist/resources/notifications.cjs.map +1 -0
- package/dist/resources/notifications.d.cts +133 -0
- package/dist/resources/notifications.d.ts +133 -0
- package/dist/resources/notifications.js +54 -0
- package/dist/resources/notifications.js.map +1 -0
- package/dist/resources/push.cjs +8 -4
- package/dist/resources/push.cjs.map +1 -1
- package/dist/resources/push.d.cts +14 -17
- package/dist/resources/push.d.ts +14 -17
- package/dist/resources/push.js +8 -4
- package/dist/resources/push.js.map +1 -1
- package/dist/resources/sessions.d.cts +1 -1
- package/dist/resources/sessions.d.ts +1 -1
- 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.d.cts +1 -1
- package/dist/resources/wallet.d.ts +1 -1
- package/dist/telegram.cjs +321 -23
- package/dist/telegram.cjs.map +1 -1
- package/dist/telegram.d.cts +62 -7
- package/dist/telegram.d.ts +62 -7
- package/dist/telegram.js +320 -24
- package/dist/telegram.js.map +1 -1
- package/package.json +23 -4
package/dist/telegram.cjs
CHANGED
|
@@ -19,6 +19,9 @@ var NetworkError = class extends Error {
|
|
|
19
19
|
this.name = "NetworkError";
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
+
function isApiError(e) {
|
|
23
|
+
return typeof e === "object" && e !== null && e.isApiError === true;
|
|
24
|
+
}
|
|
22
25
|
|
|
23
26
|
// src/http.ts
|
|
24
27
|
var API_VERSION = "/v1";
|
|
@@ -31,10 +34,11 @@ function versionedPath(path) {
|
|
|
31
34
|
return `${API_VERSION}${p}`;
|
|
32
35
|
}
|
|
33
36
|
var HttpClient = class {
|
|
34
|
-
constructor(baseUrl, tokens, fetchImpl) {
|
|
37
|
+
constructor(baseUrl, tokens, fetchImpl, defaultHeaders = {}) {
|
|
35
38
|
this.baseUrl = baseUrl;
|
|
36
39
|
this.tokens = tokens;
|
|
37
40
|
this.fetchImpl = fetchImpl;
|
|
41
|
+
this.defaultHeaders = defaultHeaders;
|
|
38
42
|
if (!baseUrl) throw new Error("HttpClient: baseUrl is required");
|
|
39
43
|
}
|
|
40
44
|
async get(path, opts = {}) {
|
|
@@ -55,7 +59,8 @@ var HttpClient = class {
|
|
|
55
59
|
async request(method, path, opts) {
|
|
56
60
|
const url = this.absolute(path);
|
|
57
61
|
const headers = {
|
|
58
|
-
Accept: "application/json"
|
|
62
|
+
Accept: "application/json",
|
|
63
|
+
...this.defaultHeaders
|
|
59
64
|
};
|
|
60
65
|
if (opts.body !== void 0) headers["Content-Type"] = "application/json";
|
|
61
66
|
if (opts.idempotencyKey) headers["Idempotency-Key"] = opts.idempotencyKey;
|
|
@@ -193,18 +198,38 @@ var AuthResource = class {
|
|
|
193
198
|
skipAuth: true
|
|
194
199
|
});
|
|
195
200
|
}
|
|
196
|
-
/** Exchange a signed Telegram Mini App `initData` payload for a session
|
|
197
|
-
* JWT. The server validates the initData HMAC against the deployment's bot
|
|
198
|
-
* token, then find-or-creates the end-user. First-time Telegram users are
|
|
199
|
-
* auto-provisioned with `user.email_pending === true` — prompt them for a
|
|
200
|
-
* real email and call {@link linkEmail}. Stores the JWT on success.
|
|
201
|
-
*
|
|
202
|
-
* Most consumers use the higher-level `fromInitData` / `fromTelegramWebApp`
|
|
203
|
-
* helpers in `@takeal/cusfront-sdk/telegram`; reach for this directly when
|
|
204
|
-
* you already hold a configured client. */
|
|
205
201
|
async exchangeTelegram(input) {
|
|
206
|
-
const
|
|
207
|
-
|
|
202
|
+
const body = { init_data: input.initData };
|
|
203
|
+
if (input.provision !== void 0) body.provision = input.provision;
|
|
204
|
+
let resp;
|
|
205
|
+
try {
|
|
206
|
+
resp = await this.http.post("/auth/telegram/exchange", {
|
|
207
|
+
body,
|
|
208
|
+
skipAuth: true
|
|
209
|
+
});
|
|
210
|
+
} catch (e) {
|
|
211
|
+
if (isApiError(e) && e.status === 404 && e.code === "telegram_unknown") {
|
|
212
|
+
return { stage: "unknown" };
|
|
213
|
+
}
|
|
214
|
+
throw e;
|
|
215
|
+
}
|
|
216
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
217
|
+
return resp;
|
|
218
|
+
}
|
|
219
|
+
/** Attach the Telegram account behind `initData` to an existing email +
|
|
220
|
+
* password account and sign in as that account. No bearer needed; the
|
|
221
|
+
* password is checked the same way `login` checks it. The JWT is stored.
|
|
222
|
+
*
|
|
223
|
+
* Errors: 401 `unauthenticated` (wrong email or password, as with
|
|
224
|
+
* `login`) / `invalid_init_data`; 403
|
|
225
|
+
* `user_disabled` / `second_factor_required` (accounts that sign in with
|
|
226
|
+
* a second factor can't be linked from a Mini App); 409
|
|
227
|
+
* `telegram_already_linked_other` (this account already has a different
|
|
228
|
+
* Telegram; unlink it first) / `telegram_in_use` (this Telegram belongs to
|
|
229
|
+
* another account); 503 `telegram_not_configured`. */
|
|
230
|
+
async linkTelegram(input) {
|
|
231
|
+
const resp = await this.http.post("/auth/telegram/link", {
|
|
232
|
+
body: { init_data: input.initData, email: input.email, password: input.password },
|
|
208
233
|
skipAuth: true
|
|
209
234
|
});
|
|
210
235
|
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
@@ -288,12 +313,44 @@ var CardsResource = class {
|
|
|
288
313
|
* `failure_reason` on the result.
|
|
289
314
|
*/
|
|
290
315
|
async create(input) {
|
|
291
|
-
const { idempotencyKey, ...
|
|
316
|
+
const { idempotencyKey, productId, name, designId, ...rest } = input;
|
|
317
|
+
const body = { ...rest };
|
|
318
|
+
if (productId !== void 0) body["product_id"] = productId;
|
|
319
|
+
if (name !== void 0) body["name"] = name;
|
|
320
|
+
if (designId !== void 0) body["design_id"] = designId;
|
|
292
321
|
return this.http.post("/me/cards", {
|
|
293
322
|
body,
|
|
294
323
|
idempotencyKey
|
|
295
324
|
});
|
|
296
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* The cards the caller can order, in the order the deployment offers them
|
|
328
|
+
* (the default product first). Each item carries its name, description,
|
|
329
|
+
* artwork, the issuance price after the caller's subscription discount,
|
|
330
|
+
* and the currency. Pass an item's `id` as `productId` to {@link create}.
|
|
331
|
+
*/
|
|
332
|
+
async listProducts() {
|
|
333
|
+
return this.http.get("/me/card-products");
|
|
334
|
+
}
|
|
335
|
+
/** One product from the catalog. 404 when unknown, disabled or hidden. */
|
|
336
|
+
async getProduct(id) {
|
|
337
|
+
return this.http.get(
|
|
338
|
+
`/me/card-products/${encodeURIComponent(id)}`
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Rename the caller's own card or change its design. Resolves to the
|
|
343
|
+
* updated card. 404 if the card isn't the caller's; 400 `invalid_name` /
|
|
344
|
+
* `design_not_allowed` on a bad value.
|
|
345
|
+
*/
|
|
346
|
+
async update(id, input) {
|
|
347
|
+
const body = {};
|
|
348
|
+
if (input.name !== void 0) body["name"] = input.name;
|
|
349
|
+
if (input.designId !== void 0) body["design_id"] = input.designId;
|
|
350
|
+
return this.http.patch(`/me/cards/${encodeURIComponent(id)}`, {
|
|
351
|
+
body
|
|
352
|
+
});
|
|
353
|
+
}
|
|
297
354
|
/** Fetch one card by id. */
|
|
298
355
|
async get(id) {
|
|
299
356
|
return this.http.get(`/me/cards/${encodeURIComponent(id)}`);
|
|
@@ -493,17 +550,20 @@ var PushResource = class {
|
|
|
493
550
|
return this.http.get("/me/push-subscription");
|
|
494
551
|
}
|
|
495
552
|
/** Store a subscription you created yourself.
|
|
496
|
-
* 409 `push_disabled` when the deployment has no VAPID keys.
|
|
553
|
+
* 409 `push_disabled` when the deployment has no VAPID keys.
|
|
554
|
+
* @deprecated Use `client.devices.register({ kind: "webpush", ... })`. */
|
|
497
555
|
async save(input) {
|
|
498
556
|
return this.http.put("/me/push-subscription", { body: input });
|
|
499
557
|
}
|
|
500
|
-
/** Delete the stored subscription. Safe when nothing is stored.
|
|
558
|
+
/** Delete the stored subscription. Safe when nothing is stored.
|
|
559
|
+
* @deprecated Use `client.devices.remove(id)` or `client.devices.unregisterWebPush(reg)`. */
|
|
501
560
|
async remove() {
|
|
502
561
|
await this.http.delete("/me/push-subscription");
|
|
503
562
|
}
|
|
504
563
|
/** Subscribe this browser and store the subscription. Ask for
|
|
505
564
|
* notification permission before calling. Throws when push isn't set up
|
|
506
|
-
* on the deployment.
|
|
565
|
+
* on the deployment.
|
|
566
|
+
* @deprecated Use `client.devices.registerWebPush(registration)`. */
|
|
507
567
|
async enable(registration) {
|
|
508
568
|
const { vapid_public_key } = await this.status();
|
|
509
569
|
if (!vapid_public_key) {
|
|
@@ -522,7 +582,8 @@ var PushResource = class {
|
|
|
522
582
|
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
523
583
|
});
|
|
524
584
|
}
|
|
525
|
-
/** Unsubscribe this browser and delete the stored subscription.
|
|
585
|
+
/** Unsubscribe this browser and delete the stored subscription.
|
|
586
|
+
* @deprecated Use `client.devices.unregisterWebPush(registration)`. */
|
|
526
587
|
async disable(registration) {
|
|
527
588
|
const sub = await registration.pushManager.getSubscription();
|
|
528
589
|
if (sub) await sub.unsubscribe();
|
|
@@ -538,6 +599,185 @@ function base64UrlToBytes(input) {
|
|
|
538
599
|
return out;
|
|
539
600
|
}
|
|
540
601
|
|
|
602
|
+
// src/resources/me.ts
|
|
603
|
+
var MeResource = class {
|
|
604
|
+
constructor(http) {
|
|
605
|
+
this.http = http;
|
|
606
|
+
}
|
|
607
|
+
/** Attach the Telegram account behind `initData` to the signed-in user.
|
|
608
|
+
* Returns the updated user (`telegram` is set). Repeating the call with
|
|
609
|
+
* the same Telegram is fine.
|
|
610
|
+
*
|
|
611
|
+
* Errors: 401 `invalid_init_data`; 403 `user_disabled` /
|
|
612
|
+
* `second_factor_required`; 409 `telegram_already_linked_other` (the
|
|
613
|
+
* account already has a different Telegram; `unlinkTelegram` first) /
|
|
614
|
+
* `telegram_in_use` (that Telegram belongs to another account); 503
|
|
615
|
+
* `telegram_not_configured`. */
|
|
616
|
+
async linkTelegram(initData) {
|
|
617
|
+
return this.http.post("/me/telegram", { body: { init_data: initData } });
|
|
618
|
+
}
|
|
619
|
+
/** Detach the Telegram account from the signed-in user. Returns the
|
|
620
|
+
* updated user (`telegram` is `null`).
|
|
621
|
+
*
|
|
622
|
+
* Errors: 404 `telegram_not_linked`; 409 `last_credential` when the
|
|
623
|
+
* account has no password, so Telegram is its only way to sign in. */
|
|
624
|
+
async unlinkTelegram() {
|
|
625
|
+
return this.http.delete("/me/telegram");
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
// src/resources/notifications.ts
|
|
630
|
+
var NotificationsResource = class {
|
|
631
|
+
constructor(http) {
|
|
632
|
+
this.http = http;
|
|
633
|
+
}
|
|
634
|
+
/** One page of the inbox, newest first. */
|
|
635
|
+
async list(params = {}) {
|
|
636
|
+
const q = new URLSearchParams();
|
|
637
|
+
if (params.cursor) q.set("cursor", params.cursor);
|
|
638
|
+
if (params.limit !== void 0) q.set("limit", String(params.limit));
|
|
639
|
+
if (params.unreadOnly) q.set("unread", "true");
|
|
640
|
+
if (params.category) q.set("category", params.category);
|
|
641
|
+
const qs = q.toString();
|
|
642
|
+
return this.http.get(`/me/notifications${qs ? `?${qs}` : ""}`);
|
|
643
|
+
}
|
|
644
|
+
/** How many items are unread. Cheap; fine to poll for a badge. */
|
|
645
|
+
async unreadCount() {
|
|
646
|
+
const { unread } = await this.http.get("/me/notifications/unread-count");
|
|
647
|
+
return unread;
|
|
648
|
+
}
|
|
649
|
+
/** Mark one item read. Returns the item; calling it again is harmless. */
|
|
650
|
+
async markRead(id) {
|
|
651
|
+
return this.http.post(`/me/notifications/${encodeURIComponent(id)}/read`);
|
|
652
|
+
}
|
|
653
|
+
/** Mark every unread item read, or only those created before `before`
|
|
654
|
+
* (ISO timestamp). Returns how many changed. */
|
|
655
|
+
async markAllRead(before) {
|
|
656
|
+
return this.http.post("/me/notifications/read-all", {
|
|
657
|
+
body: before ? { before } : {}
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
/** Delete one item. 404 when it isn't the user's. */
|
|
661
|
+
async remove(id) {
|
|
662
|
+
await this.http.delete(`/me/notifications/${encodeURIComponent(id)}`);
|
|
663
|
+
}
|
|
664
|
+
/** The user's channel and language settings. */
|
|
665
|
+
async getPreferences() {
|
|
666
|
+
return this.http.get("/me/notification-preferences");
|
|
667
|
+
}
|
|
668
|
+
/** Change some settings; the rest stay. Returns the stored view.
|
|
669
|
+
* 400 `validation` for an unknown category, channel or locale. */
|
|
670
|
+
async setPreferences(input) {
|
|
671
|
+
return this.http.put("/me/notification-preferences", {
|
|
672
|
+
body: input
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
// src/resources/devices.ts
|
|
678
|
+
function devicePlatformOf(platform) {
|
|
679
|
+
return platform === "telegram-miniapp" ? "telegram" : platform;
|
|
680
|
+
}
|
|
681
|
+
var DevicesResource = class {
|
|
682
|
+
constructor(http, client) {
|
|
683
|
+
this.http = http;
|
|
684
|
+
this.client = client;
|
|
685
|
+
}
|
|
686
|
+
/** Every device the user has, disabled ones included. */
|
|
687
|
+
async list() {
|
|
688
|
+
return this.http.get("/me/devices");
|
|
689
|
+
}
|
|
690
|
+
/** Register a device, or refresh it when the same credential is already
|
|
691
|
+
* known (then `last_seen_at`, `label` and `app_version` update and a
|
|
692
|
+
* disabled device comes back). `app_version` defaults to the client
|
|
693
|
+
* version sent in `X-Takeal-Client`.
|
|
694
|
+
*
|
|
695
|
+
* Errors: 400 `validation`; 409 `push_disabled` for `webpush` when the
|
|
696
|
+
* deployment has no VAPID keys. */
|
|
697
|
+
async register(input) {
|
|
698
|
+
return this.http.put("/me/devices", {
|
|
699
|
+
body: { app_version: this.client.version, ...input }
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
/** Delete one device. 404 when it isn't the user's. */
|
|
703
|
+
async remove(id) {
|
|
704
|
+
await this.http.delete(`/me/devices/${encodeURIComponent(id)}`);
|
|
705
|
+
}
|
|
706
|
+
/** Subscribe this browser to Web Push and register it as a device. Ask
|
|
707
|
+
* for notification permission before calling. Throws when push isn't
|
|
708
|
+
* set up on the deployment. */
|
|
709
|
+
async registerWebPush(registration, opts = {}) {
|
|
710
|
+
const key = opts.vapidPublicKey ?? await this.vapidPublicKey();
|
|
711
|
+
if (!key) {
|
|
712
|
+
throw new Error("@takeal/cusfront-sdk: push notifications are not enabled on this deployment");
|
|
713
|
+
}
|
|
714
|
+
const sub = await registration.pushManager.subscribe({
|
|
715
|
+
userVisibleOnly: true,
|
|
716
|
+
applicationServerKey: base64UrlToBytes(key)
|
|
717
|
+
});
|
|
718
|
+
const json = sub.toJSON();
|
|
719
|
+
if (!json.endpoint || !json.keys?.p256dh || !json.keys.auth) {
|
|
720
|
+
throw new Error("@takeal/cusfront-sdk: the browser returned an incomplete push subscription");
|
|
721
|
+
}
|
|
722
|
+
return this.register({
|
|
723
|
+
kind: "webpush",
|
|
724
|
+
platform: devicePlatformOf(this.client.platform),
|
|
725
|
+
credential: {
|
|
726
|
+
endpoint: json.endpoint,
|
|
727
|
+
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
728
|
+
},
|
|
729
|
+
...opts.label ? { label: opts.label } : {}
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
/** Unsubscribe this browser from Web Push and delete the matching device
|
|
733
|
+
* (see {@link UnregisterWebPushOptions}). Safe when nothing is
|
|
734
|
+
* registered. */
|
|
735
|
+
async unregisterWebPush(registration, opts = {}) {
|
|
736
|
+
const sub = await registration.pushManager.getSubscription();
|
|
737
|
+
if (sub) await sub.unsubscribe();
|
|
738
|
+
if (opts.deviceId) {
|
|
739
|
+
await this.remove(opts.deviceId);
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
const mine = (await this.list()).filter((d) => d.kind === "webpush" && d.is_current);
|
|
743
|
+
for (const d of mine) await this.remove(d.id);
|
|
744
|
+
}
|
|
745
|
+
async vapidPublicKey() {
|
|
746
|
+
const { vapid_public_key } = await this.http.get(
|
|
747
|
+
"/me/push-subscription"
|
|
748
|
+
);
|
|
749
|
+
return vapid_public_key ?? null;
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
// src/version.ts
|
|
754
|
+
var SDK_VERSION = "0.3.7";
|
|
755
|
+
|
|
756
|
+
// src/client-platform.ts
|
|
757
|
+
var CLIENT_HEADER = "X-Takeal-Client";
|
|
758
|
+
function detectClientPlatform() {
|
|
759
|
+
const g = globalThis;
|
|
760
|
+
const initData = g.Telegram?.WebApp?.initData;
|
|
761
|
+
if (typeof initData === "string" && initData.length > 0) return "telegram-miniapp";
|
|
762
|
+
if (g.navigator?.standalone === true) return "pwa";
|
|
763
|
+
if (typeof g.matchMedia === "function") {
|
|
764
|
+
try {
|
|
765
|
+
if (g.matchMedia("(display-mode: standalone)").matches) return "pwa";
|
|
766
|
+
} catch {
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return "web";
|
|
770
|
+
}
|
|
771
|
+
function resolveClientDescriptor(given) {
|
|
772
|
+
return {
|
|
773
|
+
platform: given?.platform ?? detectClientPlatform(),
|
|
774
|
+
version: given?.version || SDK_VERSION
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
function clientHeaderValue(desc) {
|
|
778
|
+
return `${desc.platform}; ${desc.version}`;
|
|
779
|
+
}
|
|
780
|
+
|
|
541
781
|
// src/token-store.ts
|
|
542
782
|
function defaultBrowserStore(key = "takeal_jwt") {
|
|
543
783
|
const fallback = inMemoryStore();
|
|
@@ -574,14 +814,30 @@ function inMemoryStore() {
|
|
|
574
814
|
};
|
|
575
815
|
}
|
|
576
816
|
|
|
817
|
+
// src/assets.ts
|
|
818
|
+
function resolveAssetUrl(baseUrl, url) {
|
|
819
|
+
if (isAbsolute(url)) return url;
|
|
820
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
821
|
+
const path = url.startsWith("/") ? url : `/${url}`;
|
|
822
|
+
return `${base}${path}`;
|
|
823
|
+
}
|
|
824
|
+
function isAbsolute(url) {
|
|
825
|
+
return /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(url);
|
|
826
|
+
}
|
|
827
|
+
|
|
577
828
|
// src/index.ts
|
|
578
829
|
function createClient(opts) {
|
|
579
830
|
const tokens = opts.tokenStore ?? pickDefaultStore();
|
|
580
831
|
const fetchImpl = opts.fetch ?? defaultFetch();
|
|
581
|
-
const
|
|
832
|
+
const client = resolveClientDescriptor(opts.client);
|
|
833
|
+
const http = new HttpClient(opts.baseUrl, tokens, fetchImpl, {
|
|
834
|
+
[CLIENT_HEADER]: clientHeaderValue(client)
|
|
835
|
+
});
|
|
582
836
|
return {
|
|
583
837
|
brand: opts.brand,
|
|
838
|
+
client,
|
|
584
839
|
tokens,
|
|
840
|
+
assetUrl: (url) => resolveAssetUrl(opts.baseUrl, url),
|
|
585
841
|
auth: new AuthResource(http, tokens),
|
|
586
842
|
deposits: new DepositsResource(http),
|
|
587
843
|
cards: new CardsResource(http),
|
|
@@ -591,7 +847,10 @@ function createClient(opts) {
|
|
|
591
847
|
blog: new BlogResource(http),
|
|
592
848
|
subscriptions: new SubscriptionsResource(http),
|
|
593
849
|
sessions: new SessionsResource(http),
|
|
594
|
-
push: new PushResource(http)
|
|
850
|
+
push: new PushResource(http),
|
|
851
|
+
me: new MeResource(http),
|
|
852
|
+
notifications: new NotificationsResource(http),
|
|
853
|
+
devices: new DevicesResource(http, client)
|
|
595
854
|
};
|
|
596
855
|
}
|
|
597
856
|
function pickDefaultStore() {
|
|
@@ -666,10 +925,47 @@ function assertWellFormed(initData, opts = {}) {
|
|
|
666
925
|
}
|
|
667
926
|
return parsed;
|
|
668
927
|
}
|
|
669
|
-
|
|
928
|
+
var TelegramUnknownError = class extends Error {
|
|
929
|
+
constructor(initData) {
|
|
930
|
+
super("this Telegram account is not linked to any user (provision: false)");
|
|
931
|
+
this.initData = initData;
|
|
932
|
+
this.name = "TelegramUnknownError";
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
async function exchangeInitData(initData, config, opts = {}) {
|
|
670
936
|
if (!opts.skipWellFormedCheck) assertWellFormed(initData, opts);
|
|
671
|
-
const client = createClient(
|
|
672
|
-
|
|
937
|
+
const client = createClient({
|
|
938
|
+
...config,
|
|
939
|
+
client: { ...config.client, platform: "telegram-miniapp" }
|
|
940
|
+
});
|
|
941
|
+
const session = await client.auth.exchangeTelegram({
|
|
942
|
+
initData,
|
|
943
|
+
...opts.provision !== void 0 ? { provision: opts.provision } : {}
|
|
944
|
+
});
|
|
945
|
+
if (session.stage === "jwt" && opts.registerDevice !== false) {
|
|
946
|
+
await registerMiniAppDevice(client, initData);
|
|
947
|
+
}
|
|
948
|
+
return { client, session };
|
|
949
|
+
}
|
|
950
|
+
async function registerMiniAppDevice(client, initData) {
|
|
951
|
+
const locale = parseInitData(initData).user?.language_code;
|
|
952
|
+
try {
|
|
953
|
+
await client.devices.register({
|
|
954
|
+
kind: "telegram_miniapp",
|
|
955
|
+
platform: "telegram",
|
|
956
|
+
credential: {},
|
|
957
|
+
...locale ? { locale } : {}
|
|
958
|
+
});
|
|
959
|
+
} catch (e) {
|
|
960
|
+
console.warn(
|
|
961
|
+
"@takeal/cusfront-sdk/telegram: could not register the Mini App as a device; notifications may not reach this user in Telegram",
|
|
962
|
+
e
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
async function fromInitData(initData, config, opts = {}) {
|
|
967
|
+
const { client, session } = await exchangeInitData(initData, config, opts);
|
|
968
|
+
if (session.stage === "unknown") throw new TelegramUnknownError(initData);
|
|
673
969
|
return client;
|
|
674
970
|
}
|
|
675
971
|
function readWebAppInitData() {
|
|
@@ -688,7 +984,9 @@ async function fromTelegramWebApp(config, opts = {}) {
|
|
|
688
984
|
}
|
|
689
985
|
|
|
690
986
|
exports.InitDataError = InitDataError;
|
|
987
|
+
exports.TelegramUnknownError = TelegramUnknownError;
|
|
691
988
|
exports.assertWellFormed = assertWellFormed;
|
|
989
|
+
exports.exchangeInitData = exchangeInitData;
|
|
692
990
|
exports.fromInitData = fromInitData;
|
|
693
991
|
exports.fromTelegramWebApp = fromTelegramWebApp;
|
|
694
992
|
exports.parseInitData = parseInitData;
|