@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/telegram.cjs
CHANGED
|
@@ -19,13 +19,26 @@ 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
|
|
27
|
+
var API_VERSION = "/v1";
|
|
28
|
+
var UNVERSIONED = /* @__PURE__ */ new Set(["/health", "/ready", "/branding", "/favicon.ico"]);
|
|
29
|
+
function versionedPath(path) {
|
|
30
|
+
const p = path.startsWith("/") ? path : `/${path}`;
|
|
31
|
+
const bare = p.split("?")[0];
|
|
32
|
+
if (UNVERSIONED.has(bare)) return p;
|
|
33
|
+
if (bare === API_VERSION || bare.startsWith(`${API_VERSION}/`)) return p;
|
|
34
|
+
return `${API_VERSION}${p}`;
|
|
35
|
+
}
|
|
24
36
|
var HttpClient = class {
|
|
25
|
-
constructor(baseUrl, tokens, fetchImpl) {
|
|
37
|
+
constructor(baseUrl, tokens, fetchImpl, defaultHeaders = {}) {
|
|
26
38
|
this.baseUrl = baseUrl;
|
|
27
39
|
this.tokens = tokens;
|
|
28
40
|
this.fetchImpl = fetchImpl;
|
|
41
|
+
this.defaultHeaders = defaultHeaders;
|
|
29
42
|
if (!baseUrl) throw new Error("HttpClient: baseUrl is required");
|
|
30
43
|
}
|
|
31
44
|
async get(path, opts = {}) {
|
|
@@ -46,7 +59,8 @@ var HttpClient = class {
|
|
|
46
59
|
async request(method, path, opts) {
|
|
47
60
|
const url = this.absolute(path);
|
|
48
61
|
const headers = {
|
|
49
|
-
Accept: "application/json"
|
|
62
|
+
Accept: "application/json",
|
|
63
|
+
...this.defaultHeaders
|
|
50
64
|
};
|
|
51
65
|
if (opts.body !== void 0) headers["Content-Type"] = "application/json";
|
|
52
66
|
if (opts.idempotencyKey) headers["Idempotency-Key"] = opts.idempotencyKey;
|
|
@@ -77,8 +91,7 @@ var HttpClient = class {
|
|
|
77
91
|
absolute(path) {
|
|
78
92
|
if (path.startsWith("http://") || path.startsWith("https://")) return path;
|
|
79
93
|
const base = this.baseUrl.replace(/\/+$/, "");
|
|
80
|
-
|
|
81
|
-
return `${base}${tail}`;
|
|
94
|
+
return `${base}${versionedPath(path)}`;
|
|
82
95
|
}
|
|
83
96
|
};
|
|
84
97
|
async function parseResponse(resp) {
|
|
@@ -143,6 +156,40 @@ var AuthResource = class {
|
|
|
143
156
|
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
144
157
|
return resp;
|
|
145
158
|
}
|
|
159
|
+
/** Create an end-user account and sign it in. The JWT is stored.
|
|
160
|
+
* `wallet_currency` must be one the deployment offers; omit it to take
|
|
161
|
+
* the deployment default (it can be switched later via `client.wallet`
|
|
162
|
+
* while the wallet is empty).
|
|
163
|
+
* Errors: 400 `validation` / `currency_not_allowed`, 409 email taken. */
|
|
164
|
+
async register(input) {
|
|
165
|
+
const resp = await this.http.post("/auth/register", {
|
|
166
|
+
body: input,
|
|
167
|
+
skipAuth: true
|
|
168
|
+
});
|
|
169
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
170
|
+
return resp;
|
|
171
|
+
}
|
|
172
|
+
/** Finish a `totp_setup_required` login: send the first code from the
|
|
173
|
+
* authenticator app. The JWT is stored. The response carries ten
|
|
174
|
+
* one-time `backup_codes` that are never returned again, so show them to
|
|
175
|
+
* the user right away and don't keep them on the device. */
|
|
176
|
+
async completeTotpSetup(input) {
|
|
177
|
+
const resp = await this.http.post("/auth/login/totp-setup", {
|
|
178
|
+
body: input,
|
|
179
|
+
skipAuth: true
|
|
180
|
+
});
|
|
181
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
182
|
+
return resp;
|
|
183
|
+
}
|
|
184
|
+
/** Change the signed-in user's password. Every other session of the
|
|
185
|
+
* account is signed out; this one stays valid.
|
|
186
|
+
* Errors: 400 when the new password fails the policy, 401 when the
|
|
187
|
+
* current one is wrong. */
|
|
188
|
+
async changePassword(input) {
|
|
189
|
+
return this.http.post("/auth/password", {
|
|
190
|
+
body: input
|
|
191
|
+
});
|
|
192
|
+
}
|
|
146
193
|
/** Swap the 2FA method mid-flow. Resolves to `otp_sent` for
|
|
147
194
|
* telegram/email; for TOTP it short-circuits back to the challenge. */
|
|
148
195
|
async switchMethod(input) {
|
|
@@ -151,18 +198,38 @@ var AuthResource = class {
|
|
|
151
198
|
skipAuth: true
|
|
152
199
|
});
|
|
153
200
|
}
|
|
154
|
-
/** Exchange a signed Telegram Mini App `initData` payload for a session
|
|
155
|
-
* JWT. The server validates the initData HMAC against the deployment's bot
|
|
156
|
-
* token, then find-or-creates the end-user. First-time Telegram users are
|
|
157
|
-
* auto-provisioned with `user.email_pending === true` — prompt them for a
|
|
158
|
-
* real email and call {@link linkEmail}. Stores the JWT on success.
|
|
159
|
-
*
|
|
160
|
-
* Most consumers use the higher-level `fromInitData` / `fromTelegramWebApp`
|
|
161
|
-
* helpers in `@takeal/cusfront-sdk/telegram`; reach for this directly when
|
|
162
|
-
* you already hold a configured client. */
|
|
163
201
|
async exchangeTelegram(input) {
|
|
164
|
-
const
|
|
165
|
-
|
|
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 },
|
|
166
233
|
skipAuth: true
|
|
167
234
|
});
|
|
168
235
|
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
@@ -179,10 +246,9 @@ var AuthResource = class {
|
|
|
179
246
|
async me() {
|
|
180
247
|
return this.http.get("/auth/me");
|
|
181
248
|
}
|
|
182
|
-
/** Local sign-out.
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* with a future session table. */
|
|
249
|
+
/** Local sign-out: forgets the stored JWT. To end the session on the
|
|
250
|
+
* server as well, revoke it first with `client.sessions.revoke(jti)`
|
|
251
|
+
* (the current one is marked `current: true` in `client.sessions.list()`). */
|
|
186
252
|
async signOut() {
|
|
187
253
|
await Promise.resolve(this.tokens.clear());
|
|
188
254
|
}
|
|
@@ -247,12 +313,44 @@ var CardsResource = class {
|
|
|
247
313
|
* `failure_reason` on the result.
|
|
248
314
|
*/
|
|
249
315
|
async create(input) {
|
|
250
|
-
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;
|
|
251
321
|
return this.http.post("/me/cards", {
|
|
252
322
|
body,
|
|
253
323
|
idempotencyKey
|
|
254
324
|
});
|
|
255
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
|
+
}
|
|
256
354
|
/** Fetch one card by id. */
|
|
257
355
|
async get(id) {
|
|
258
356
|
return this.http.get(`/me/cards/${encodeURIComponent(id)}`);
|
|
@@ -327,11 +425,12 @@ var BalanceResource = class {
|
|
|
327
425
|
this.http = http;
|
|
328
426
|
}
|
|
329
427
|
/**
|
|
330
|
-
* Fetch the user's wallet balance
|
|
331
|
-
*
|
|
332
|
-
*
|
|
428
|
+
* Fetch the user's wallet balance. Omit `currency` for the wallet's own
|
|
429
|
+
* currency; pass an ISO 4217 code (e.g. `"USD"`) to read another one —
|
|
430
|
+
* the ledger holds a separate balance per currency.
|
|
333
431
|
*/
|
|
334
432
|
async get(currency) {
|
|
433
|
+
if (!currency) return this.http.get("/me/balance");
|
|
335
434
|
const q = new URLSearchParams({ currency }).toString();
|
|
336
435
|
return this.http.get(`/me/balance?${q}`);
|
|
337
436
|
}
|
|
@@ -348,6 +447,25 @@ var BrandingResource = class {
|
|
|
348
447
|
}
|
|
349
448
|
};
|
|
350
449
|
|
|
450
|
+
// src/resources/wallet.ts
|
|
451
|
+
var WalletResource = class {
|
|
452
|
+
constructor(http) {
|
|
453
|
+
this.http = http;
|
|
454
|
+
}
|
|
455
|
+
/** Current wallet currency + the currencies the user may switch to. */
|
|
456
|
+
async get(opts = {}) {
|
|
457
|
+
return this.http.get("/me/wallet", { signal: opts.signal });
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Switch the wallet currency. Fails with `409 wallet_not_empty` while any
|
|
461
|
+
* balance is non-zero, and `400 currency_not_allowed` for a currency the
|
|
462
|
+
* deployment does not offer.
|
|
463
|
+
*/
|
|
464
|
+
async set(currency, opts = {}) {
|
|
465
|
+
return this.http.put("/me/wallet", { body: { currency }, signal: opts.signal });
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
|
|
351
469
|
// src/resources/blog.ts
|
|
352
470
|
var BlogResource = class {
|
|
353
471
|
constructor(http) {
|
|
@@ -401,6 +519,265 @@ var SubscriptionsResource = class {
|
|
|
401
519
|
}
|
|
402
520
|
};
|
|
403
521
|
|
|
522
|
+
// src/resources/sessions.ts
|
|
523
|
+
var SessionsResource = class {
|
|
524
|
+
constructor(http) {
|
|
525
|
+
this.http = http;
|
|
526
|
+
}
|
|
527
|
+
/** All live sessions of the account, this one included. */
|
|
528
|
+
async list() {
|
|
529
|
+
return this.http.get("/me/sessions");
|
|
530
|
+
}
|
|
531
|
+
/** Sign out one session. Works on the current one too; the stored JWT
|
|
532
|
+
* stops working, so follow with `client.auth.signOut()`.
|
|
533
|
+
* 404 when the session is unknown or already signed out. */
|
|
534
|
+
async revoke(jti) {
|
|
535
|
+
await this.http.delete(`/me/sessions/${encodeURIComponent(jti)}`);
|
|
536
|
+
}
|
|
537
|
+
/** Sign out every session except this one. */
|
|
538
|
+
async revokeOthers() {
|
|
539
|
+
return this.http.post("/me/sessions/terminate-others");
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
// src/resources/push.ts
|
|
544
|
+
var PushResource = class {
|
|
545
|
+
constructor(http) {
|
|
546
|
+
this.http = http;
|
|
547
|
+
}
|
|
548
|
+
/** Whether the user is subscribed, plus the VAPID key to subscribe with. */
|
|
549
|
+
async status() {
|
|
550
|
+
return this.http.get("/me/push-subscription");
|
|
551
|
+
}
|
|
552
|
+
/** Store a subscription you created yourself.
|
|
553
|
+
* 409 `push_disabled` when the deployment has no VAPID keys.
|
|
554
|
+
* @deprecated Use `client.devices.register({ kind: "webpush", ... })`. */
|
|
555
|
+
async save(input) {
|
|
556
|
+
return this.http.put("/me/push-subscription", { body: input });
|
|
557
|
+
}
|
|
558
|
+
/** Delete the stored subscription. Safe when nothing is stored.
|
|
559
|
+
* @deprecated Use `client.devices.remove(id)` or `client.devices.unregisterWebPush(reg)`. */
|
|
560
|
+
async remove() {
|
|
561
|
+
await this.http.delete("/me/push-subscription");
|
|
562
|
+
}
|
|
563
|
+
/** Subscribe this browser and store the subscription. Ask for
|
|
564
|
+
* notification permission before calling. Throws when push isn't set up
|
|
565
|
+
* on the deployment.
|
|
566
|
+
* @deprecated Use `client.devices.registerWebPush(registration)`. */
|
|
567
|
+
async enable(registration) {
|
|
568
|
+
const { vapid_public_key } = await this.status();
|
|
569
|
+
if (!vapid_public_key) {
|
|
570
|
+
throw new Error("@takeal/cusfront-sdk: push notifications are not enabled on this deployment");
|
|
571
|
+
}
|
|
572
|
+
const sub = await registration.pushManager.subscribe({
|
|
573
|
+
userVisibleOnly: true,
|
|
574
|
+
applicationServerKey: base64UrlToBytes(vapid_public_key)
|
|
575
|
+
});
|
|
576
|
+
const json = sub.toJSON();
|
|
577
|
+
if (!json.endpoint || !json.keys?.p256dh || !json.keys.auth) {
|
|
578
|
+
throw new Error("@takeal/cusfront-sdk: the browser returned an incomplete push subscription");
|
|
579
|
+
}
|
|
580
|
+
return this.save({
|
|
581
|
+
endpoint: json.endpoint,
|
|
582
|
+
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
/** Unsubscribe this browser and delete the stored subscription.
|
|
586
|
+
* @deprecated Use `client.devices.unregisterWebPush(registration)`. */
|
|
587
|
+
async disable(registration) {
|
|
588
|
+
const sub = await registration.pushManager.getSubscription();
|
|
589
|
+
if (sub) await sub.unsubscribe();
|
|
590
|
+
await this.remove();
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
function base64UrlToBytes(input) {
|
|
594
|
+
const b64 = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
595
|
+
const padded = b64 + "=".repeat((4 - b64.length % 4) % 4);
|
|
596
|
+
const bin = atob(padded);
|
|
597
|
+
const out = new Uint8Array(bin.length);
|
|
598
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
599
|
+
return out;
|
|
600
|
+
}
|
|
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.0";
|
|
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
|
+
|
|
404
781
|
// src/token-store.ts
|
|
405
782
|
function defaultBrowserStore(key = "takeal_jwt") {
|
|
406
783
|
const fallback = inMemoryStore();
|
|
@@ -437,21 +814,43 @@ function inMemoryStore() {
|
|
|
437
814
|
};
|
|
438
815
|
}
|
|
439
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
|
+
|
|
440
828
|
// src/index.ts
|
|
441
829
|
function createClient(opts) {
|
|
442
830
|
const tokens = opts.tokenStore ?? pickDefaultStore();
|
|
443
831
|
const fetchImpl = opts.fetch ?? defaultFetch();
|
|
444
|
-
const
|
|
832
|
+
const client = resolveClientDescriptor(opts.client);
|
|
833
|
+
const http = new HttpClient(opts.baseUrl, tokens, fetchImpl, {
|
|
834
|
+
[CLIENT_HEADER]: clientHeaderValue(client)
|
|
835
|
+
});
|
|
445
836
|
return {
|
|
446
837
|
brand: opts.brand,
|
|
838
|
+
client,
|
|
447
839
|
tokens,
|
|
840
|
+
assetUrl: (url) => resolveAssetUrl(opts.baseUrl, url),
|
|
448
841
|
auth: new AuthResource(http, tokens),
|
|
449
842
|
deposits: new DepositsResource(http),
|
|
450
843
|
cards: new CardsResource(http),
|
|
451
844
|
balance: new BalanceResource(http),
|
|
845
|
+
wallet: new WalletResource(http),
|
|
452
846
|
branding: new BrandingResource(http),
|
|
453
847
|
blog: new BlogResource(http),
|
|
454
|
-
subscriptions: new SubscriptionsResource(http)
|
|
848
|
+
subscriptions: new SubscriptionsResource(http),
|
|
849
|
+
sessions: new SessionsResource(http),
|
|
850
|
+
push: new PushResource(http),
|
|
851
|
+
me: new MeResource(http),
|
|
852
|
+
notifications: new NotificationsResource(http),
|
|
853
|
+
devices: new DevicesResource(http, client)
|
|
455
854
|
};
|
|
456
855
|
}
|
|
457
856
|
function pickDefaultStore() {
|
|
@@ -526,10 +925,47 @@ function assertWellFormed(initData, opts = {}) {
|
|
|
526
925
|
}
|
|
527
926
|
return parsed;
|
|
528
927
|
}
|
|
529
|
-
|
|
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 = {}) {
|
|
530
936
|
if (!opts.skipWellFormedCheck) assertWellFormed(initData, opts);
|
|
531
|
-
const client = createClient(
|
|
532
|
-
|
|
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);
|
|
533
969
|
return client;
|
|
534
970
|
}
|
|
535
971
|
function readWebAppInitData() {
|
|
@@ -548,7 +984,9 @@ async function fromTelegramWebApp(config, opts = {}) {
|
|
|
548
984
|
}
|
|
549
985
|
|
|
550
986
|
exports.InitDataError = InitDataError;
|
|
987
|
+
exports.TelegramUnknownError = TelegramUnknownError;
|
|
551
988
|
exports.assertWellFormed = assertWellFormed;
|
|
989
|
+
exports.exchangeInitData = exchangeInitData;
|
|
552
990
|
exports.fromInitData = fromInitData;
|
|
553
991
|
exports.fromTelegramWebApp = fromTelegramWebApp;
|
|
554
992
|
exports.parseInitData = parseInitData;
|