@takeal/cusfront-sdk 0.6.8 → 0.6.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +112 -2
  2. package/README.md +26 -9
  3. package/dist/analytics.cjs +44 -6
  4. package/dist/analytics.cjs.map +1 -1
  5. package/dist/analytics.js +44 -6
  6. package/dist/analytics.js.map +1 -1
  7. package/dist/{cards-BgY00UpU.d.ts → cards-DPlgx76p.d.ts} +86 -18
  8. package/dist/{cards-DGz4Rqu1.d.cts → cards-D_krTWzr.d.cts} +86 -18
  9. package/dist/index.cjs +225 -93
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +5 -5
  12. package/dist/index.d.ts +5 -5
  13. package/dist/index.js +221 -94
  14. package/dist/index.js.map +1 -1
  15. package/dist/react/index.d.cts +1 -1
  16. package/dist/react/index.d.ts +1 -1
  17. package/dist/resources/attribution.cjs +99 -4
  18. package/dist/resources/attribution.cjs.map +1 -1
  19. package/dist/resources/attribution.d.cts +58 -1
  20. package/dist/resources/attribution.d.ts +58 -1
  21. package/dist/resources/attribution.js +96 -5
  22. package/dist/resources/attribution.js.map +1 -1
  23. package/dist/resources/auth.cjs +64 -3
  24. package/dist/resources/auth.cjs.map +1 -1
  25. package/dist/resources/auth.d.cts +1 -1
  26. package/dist/resources/auth.d.ts +1 -1
  27. package/dist/resources/auth.js +64 -3
  28. package/dist/resources/auth.js.map +1 -1
  29. package/dist/resources/cards.cjs +69 -7
  30. package/dist/resources/cards.cjs.map +1 -1
  31. package/dist/resources/cards.d.cts +2 -1
  32. package/dist/resources/cards.d.ts +2 -1
  33. package/dist/resources/cards.js +69 -8
  34. package/dist/resources/cards.js.map +1 -1
  35. package/dist/resources/codes.cjs.map +1 -1
  36. package/dist/resources/codes.d.cts +21 -1
  37. package/dist/resources/codes.d.ts +21 -1
  38. package/dist/resources/codes.js.map +1 -1
  39. package/dist/resources/consents.cjs +15 -0
  40. package/dist/resources/consents.cjs.map +1 -1
  41. package/dist/resources/consents.js +15 -0
  42. package/dist/resources/consents.js.map +1 -1
  43. package/dist/resources/deposits.cjs +50 -0
  44. package/dist/resources/deposits.cjs.map +1 -1
  45. package/dist/resources/deposits.d.cts +2 -1
  46. package/dist/resources/deposits.d.ts +2 -1
  47. package/dist/resources/deposits.js +50 -0
  48. package/dist/resources/deposits.js.map +1 -1
  49. package/dist/resources/funding-options.d.cts +2 -1
  50. package/dist/resources/funding-options.d.ts +2 -1
  51. package/dist/resources/me.d.cts +1 -1
  52. package/dist/resources/me.d.ts +1 -1
  53. package/dist/telegram.cjs +224 -73
  54. package/dist/telegram.cjs.map +1 -1
  55. package/dist/telegram.d.cts +34 -3
  56. package/dist/telegram.d.ts +34 -3
  57. package/dist/telegram.js +224 -74
  58. package/dist/telegram.js.map +1 -1
  59. package/package.json +4 -4
package/dist/telegram.cjs CHANGED
@@ -144,6 +144,150 @@ async function parseResponse(resp) {
144
144
  return body;
145
145
  }
146
146
 
147
+ // src/resources/attribution.ts
148
+ var AttributionResource = class {
149
+ constructor(http) {
150
+ this.http = http;
151
+ }
152
+ /** Record a visit. No session needed; safe to call on every page load. */
153
+ async touch(input) {
154
+ return this.http.post("/attribution/touches", { body: input, skipAuth: true });
155
+ }
156
+ /** A one-day token that carries the visitor's analytics ids into the
157
+ * Telegram Mini App, whose sign-in reads it back. Most callers want
158
+ * {@link AttributionResource.telegramStartParam}. Errors: 400 when none of
159
+ * the ids is valid. */
160
+ async handoff(ids) {
161
+ return this.http.post("/attribution/handoff", { body: ids, skipAuth: true });
162
+ }
163
+ /**
164
+ * The `startapp` value for a link from the site to the Mini App: the code
165
+ * as it is, followed by `__<token>` when the visitor has analytics ids, so
166
+ * a payment made in Telegram is still credited to the visit it came from.
167
+ * Without ids, or when the token can't be had, just the code.
168
+ *
169
+ * ```ts
170
+ * const start = await client.attribution.telegramStartParam("SPRING");
171
+ * location.href = `https://t.me/our_bot/app?startapp=${start}`;
172
+ * ```
173
+ */
174
+ async telegramStartParam(code = "") {
175
+ const ids = analyticsIds();
176
+ if (!ids) return code;
177
+ try {
178
+ const { token } = await this.handoff(ids);
179
+ return `${code}__${token}`;
180
+ } catch {
181
+ return code;
182
+ }
183
+ }
184
+ /** Send the visitor's analytics ids for the signed-in account, e.g. right
185
+ * after they agreed to analytics cookies in the cabinet. Without an
186
+ * argument the ids this browser holds are sent; nothing is sent when it
187
+ * holds none. */
188
+ async sendAnalyticsIds(ids = analyticsIds()) {
189
+ if (!ids) return;
190
+ await this.http.post("/me/analytics/ids", { body: ids });
191
+ }
192
+ /** Whether to ask the signed-in user how they heard about us. */
193
+ async status() {
194
+ return this.http.get("/me/attribution");
195
+ }
196
+ /** Answer the question once. Send `"skipped"` when the user closes it.
197
+ * `otherText` is kept only with `"other"`, up to 500 characters.
198
+ * Errors: 400 `validation`, 409 `survey_answered`. */
199
+ async answerSurvey(answer, otherText) {
200
+ const body = { answer };
201
+ if (otherText !== void 0) body.other_text = otherText;
202
+ return this.http.put("/me/attribution/survey", { body });
203
+ }
204
+ };
205
+ function tgWebApp() {
206
+ return globalThis.Telegram?.WebApp?.initDataUnsafe;
207
+ }
208
+ function readCookie(name) {
209
+ const doc = globalThis.document;
210
+ if (!doc?.cookie) return void 0;
211
+ for (const part of doc.cookie.split(";")) {
212
+ const [k, ...v] = part.trim().split("=");
213
+ if (k === name) return decodeURIComponent(v.join("="));
214
+ }
215
+ return void 0;
216
+ }
217
+ var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
218
+ function anonymousKey(opts = {}) {
219
+ const tgId = tgWebApp()?.user?.id;
220
+ if (tgId) return `tg:${tgId}`;
221
+ const g = globalThis;
222
+ if (!g.document) return void 0;
223
+ const name = opts.name ?? "takeal_anon";
224
+ let stored2;
225
+ try {
226
+ stored2 = g.localStorage?.getItem(name) ?? void 0;
227
+ } catch {
228
+ stored2 = void 0;
229
+ }
230
+ const fromCookie = readCookie(name);
231
+ let key = [fromCookie, stored2].find((k) => !!k && KEY_SHAPE.test(k));
232
+ if (!key) {
233
+ key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
234
+ }
235
+ try {
236
+ g.localStorage?.setItem(name, key);
237
+ } catch {
238
+ }
239
+ const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
240
+ g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
241
+ return key;
242
+ }
243
+ var IDS_KEY = "takeal_analytics_ids";
244
+ function storage() {
245
+ try {
246
+ return globalThis.localStorage;
247
+ } catch {
248
+ return void 0;
249
+ }
250
+ }
251
+ function stored() {
252
+ try {
253
+ const raw = storage()?.getItem(IDS_KEY);
254
+ const v = raw ? JSON.parse(raw) : {};
255
+ return typeof v === "object" && v !== null ? v : {};
256
+ } catch {
257
+ return {};
258
+ }
259
+ }
260
+ function clean(ids) {
261
+ const out = {};
262
+ const ym = ids.metrika_client_id?.trim();
263
+ if (ym && /^\d{1,32}$/.test(ym)) out.metrika_client_id = ym;
264
+ const ga = ids.ga_client_id?.trim();
265
+ if (ga && /^\d+\.\d+$/.test(ga) && ga.length <= 64) out.ga_client_id = ga;
266
+ return out.metrika_client_id || out.ga_client_id ? out : void 0;
267
+ }
268
+ function forgetAnalyticsIds() {
269
+ try {
270
+ storage()?.removeItem(IDS_KEY);
271
+ } catch {
272
+ }
273
+ }
274
+ function analyticsIds() {
275
+ const kept = stored();
276
+ return clean({
277
+ metrika_client_id: kept.metrika_client_id ?? readCookie("_ym_uid"),
278
+ ga_client_id: kept.ga_client_id ?? readCookie("_ga")?.split(".").slice(-2).join(".")
279
+ });
280
+ }
281
+ function withAnalyticsIds(attribution) {
282
+ const ids = analyticsIds();
283
+ if (!ids) return attribution;
284
+ return {
285
+ ...attribution,
286
+ metrika_client_id: attribution?.metrika_client_id ?? ids.metrika_client_id,
287
+ ga_client_id: attribution?.ga_client_id ?? ids.ga_client_id
288
+ };
289
+ }
290
+
147
291
  // src/resources/cards.ts
148
292
  function cardRequestBody(input) {
149
293
  const {
@@ -182,8 +326,15 @@ function cardOrderBody(input) {
182
326
  if (input.consents !== void 0) body["consents"] = input.consents;
183
327
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
184
328
  if (input.metadata !== void 0) body["metadata"] = input.metadata;
329
+ const ids = input.analyticsIds ?? analyticsIds();
330
+ if (ids !== void 0) body["analytics_ids"] = ids;
185
331
  return body;
186
332
  }
333
+ function revealState(r) {
334
+ if (r.details_pending) return "pending";
335
+ if (r.reveal_url) return "page";
336
+ return "numbers";
337
+ }
187
338
  var CardsResource = class {
188
339
  constructor(http) {
189
340
  this.http = http;
@@ -211,8 +362,10 @@ var CardsResource = class {
211
362
  * `idempotencyKey` returns the same order.
212
363
  * Errors: 400 `unknown_product` / `method_not_available` / a limit of the
213
364
  * way of paying, 409 `holder_data_consent_required` (the product has
214
- * `holder_data_required` and the user has not agreed to it), 503 no
215
- * funding connector or no exchange rate.
365
+ * `holder_data_required` and the user has not agreed to it), 409
366
+ * `payments_on_hold` (a payment on this account was not accepted and new
367
+ * ones wait until support sorts it out: show the message and point the
368
+ * user to support), 503 no funding connector or no exchange rate.
216
369
  */
217
370
  async order(input) {
218
371
  const { idempotencyKey, ...order } = input;
@@ -226,7 +379,9 @@ var CardsResource = class {
226
379
  * (`is_default` marks the one issued when none is chosen). Each item
227
380
  * carries its product card (name, tagline, benefits, tile picture),
228
381
  * artwork, and the issuance price after the caller's subscription
229
- * discount, in the caller's wallet currency. Pass an item's `id` as
382
+ * discount, in the currency the operator set it in (`price.from_wallet`
383
+ * is the same price from the wallet, when that holds another currency).
384
+ * Pass an item's `id` as
230
385
  * `productId` to {@link create}.
231
386
  */
232
387
  async listProducts() {
@@ -339,15 +494,22 @@ var CardsResource = class {
339
494
  * keep the result in volatile memory only, never write it to
340
495
  * localStorage / logs, and clear it within `expires_in_seconds`.
341
496
  *
342
- * The result is the numbers, or `reveal_url` with empty `pan` and `cvv`
343
- * when the issuer shows them on a page of its own: open that page in a
344
- * frame right away, it is short-lived.
497
+ * `state` tells the three answers apart: `numbers`; `page`, with
498
+ * `reveal_url` and empty `pan` and `cvv` when the issuer shows them on a
499
+ * page of its own (open that page in a frame right away, it is
500
+ * short-lived); or `pending` while the card's details are still being
501
+ * prepared, which doesn't count against the limit. A card says the same
502
+ * in {@link Card.details_pending}, so there is no need to call this just
503
+ * to find out.
345
504
  *
346
505
  * 403 not your card · 409 `card_locked` · 429 rate-limited · 501 connector
347
506
  * has no sensitive-data endpoint · 503 `provider_unavailable`.
348
507
  */
349
508
  async reveal(id, _input) {
350
- return this.http.post(`/me/cards/${encodeURIComponent(id)}/reveal`);
509
+ const r = await this.http.post(
510
+ `/me/cards/${encodeURIComponent(id)}/reveal`
511
+ );
512
+ return { ...r, state: revealState(r) };
351
513
  }
352
514
  /**
353
515
  * Unfreeze a previously-frozen card. Idempotent on an already-active card.
@@ -464,8 +626,11 @@ var AuthResource = class {
464
626
  /** Password-auth entry. May resolve to a JWT, or to a step-up
465
627
  * challenge that needs `verifyTotp` / `verifyOtp` next. */
466
628
  async login(input) {
629
+ const body = withRemember(input);
630
+ const attribution = withAnalyticsIds(void 0);
631
+ if (attribution !== void 0) body["attribution"] = attribution;
467
632
  const resp = await this.http.post("/auth/login", {
468
- body: withRemember(input),
633
+ body,
469
634
  skipAuth: true
470
635
  });
471
636
  if (resp.stage === "jwt") {
@@ -502,6 +667,8 @@ var AuthResource = class {
502
667
  async register(input) {
503
668
  const { cardOrder, ...rest } = input;
504
669
  const body = withRemember(rest);
670
+ const attribution = withAnalyticsIds(rest.attribution);
671
+ if (attribution !== void 0) body["attribution"] = attribution;
505
672
  if (cardOrder !== void 0) body["card_order"] = cardOrderBody(cardOrder);
506
673
  const resp = await this.http.post("/auth/register", {
507
674
  body,
@@ -537,7 +704,8 @@ var AuthResource = class {
537
704
  };
538
705
  if (input.remember) body["remember_me"] = true;
539
706
  if (input.walletCurrency !== void 0) body["wallet_currency"] = input.walletCurrency;
540
- if (input.attribution !== void 0) body["attribution"] = input.attribution;
707
+ const attribution = withAnalyticsIds(input.attribution);
708
+ if (attribution !== void 0) body["attribution"] = attribution;
541
709
  if (input.promoCode !== void 0) body["promo_code"] = input.promoCode;
542
710
  if (input.consents !== void 0) body["consents"] = input.consents;
543
711
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
@@ -583,7 +751,8 @@ var AuthResource = class {
583
751
  init_data: input.initData
584
752
  };
585
753
  if (input.provision !== void 0) body.provision = input.provision;
586
- if (input.attribution !== void 0) body.attribution = input.attribution;
754
+ const attribution = withAnalyticsIds(input.attribution);
755
+ if (attribution !== void 0) body.attribution = attribution;
587
756
  if (input.code !== void 0) body.code = input.code;
588
757
  if (input.consents !== void 0) body.consents = input.consents;
589
758
  if (input.consentChannel !== void 0) body.consent_channel = input.consentChannel;
@@ -689,10 +858,17 @@ var DepositsResource = class {
689
858
  if (connectorSlug !== void 0) body["connector_slug"] = connectorSlug;
690
859
  return this.http.post("/me/deposits/quotes", { body });
691
860
  }
861
+ /**
862
+ * Start a top-up. 409 `payments_on_hold`: a payment on this account was
863
+ * not accepted and new ones wait until support sorts it out. Show the
864
+ * message as it comes and point the user to support.
865
+ */
692
866
  async initiate(input) {
693
867
  const { idempotencyKey, issue_card, ...rest } = input;
694
868
  const body = { ...rest };
695
869
  if (issue_card !== void 0) body["issue_card"] = cardRequestBody(issue_card);
870
+ const ids = input.analytics_ids ?? analyticsIds();
871
+ if (ids !== void 0) body["analytics_ids"] = ids;
696
872
  return this.http.post("/me/deposits", {
697
873
  body,
698
874
  idempotencyKey
@@ -1294,67 +1470,6 @@ var DevicesResource = class {
1294
1470
  }
1295
1471
  };
1296
1472
 
1297
- // src/resources/attribution.ts
1298
- var AttributionResource = class {
1299
- constructor(http) {
1300
- this.http = http;
1301
- }
1302
- /** Record a visit. No session needed; safe to call on every page load. */
1303
- async touch(input) {
1304
- return this.http.post("/attribution/touches", { body: input, skipAuth: true });
1305
- }
1306
- /** Whether to ask the signed-in user how they heard about us. */
1307
- async status() {
1308
- return this.http.get("/me/attribution");
1309
- }
1310
- /** Answer the question once. Send `"skipped"` when the user closes it.
1311
- * `otherText` is kept only with `"other"`, up to 500 characters.
1312
- * Errors: 400 `validation`, 409 `survey_answered`. */
1313
- async answerSurvey(answer, otherText) {
1314
- const body = { answer };
1315
- if (otherText !== void 0) body.other_text = otherText;
1316
- return this.http.put("/me/attribution/survey", { body });
1317
- }
1318
- };
1319
- function tgWebApp() {
1320
- return globalThis.Telegram?.WebApp?.initDataUnsafe;
1321
- }
1322
- function readCookie(name) {
1323
- const doc = globalThis.document;
1324
- if (!doc?.cookie) return void 0;
1325
- for (const part of doc.cookie.split(";")) {
1326
- const [k, ...v] = part.trim().split("=");
1327
- if (k === name) return decodeURIComponent(v.join("="));
1328
- }
1329
- return void 0;
1330
- }
1331
- var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
1332
- function anonymousKey(opts = {}) {
1333
- const tgId = tgWebApp()?.user?.id;
1334
- if (tgId) return `tg:${tgId}`;
1335
- const g = globalThis;
1336
- if (!g.document) return void 0;
1337
- const name = opts.name ?? "takeal_anon";
1338
- let stored;
1339
- try {
1340
- stored = g.localStorage?.getItem(name) ?? void 0;
1341
- } catch {
1342
- stored = void 0;
1343
- }
1344
- const fromCookie = readCookie(name);
1345
- let key = [fromCookie, stored].find((k) => !!k && KEY_SHAPE.test(k));
1346
- if (!key) {
1347
- key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
1348
- }
1349
- try {
1350
- g.localStorage?.setItem(name, key);
1351
- } catch {
1352
- }
1353
- const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
1354
- g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
1355
- return key;
1356
- }
1357
-
1358
1473
  // src/resources/history.ts
1359
1474
  var HistoryResource = class {
1360
1475
  constructor(http) {
@@ -1458,6 +1573,7 @@ var ConsentsResource = class {
1458
1573
  const key = input.anonymousKey ?? anonymousKey();
1459
1574
  if (key !== void 0) body.anonymous_key = key;
1460
1575
  if (input.channel !== void 0) body.channel = input.channel;
1576
+ if (!input.analytics) forgetAnalyticsIds();
1461
1577
  return this.http.post("/consents/cookies", { body });
1462
1578
  }
1463
1579
  };
@@ -1555,7 +1671,7 @@ var FundingOptionsResource = class {
1555
1671
  };
1556
1672
 
1557
1673
  // src/version.ts
1558
- var SDK_VERSION = "0.6.8";
1674
+ var SDK_VERSION = "0.6.14";
1559
1675
 
1560
1676
  // src/client-platform.ts
1561
1677
  var CLIENT_HEADER = "X-Takeal-Client";
@@ -1630,7 +1746,7 @@ function isAbsolute(url) {
1630
1746
  }
1631
1747
 
1632
1748
  // src/compat.ts
1633
- var SUPPORTED_MEISTER = ">=0.6.13, <1.0.0";
1749
+ var SUPPORTED_MEISTER = ">=0.7.6, <1.0.0";
1634
1750
  var SEMVER = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
1635
1751
  function parse(v) {
1636
1752
  const m = SEMVER.exec(v.trim());
@@ -1813,6 +1929,40 @@ function assertWellFormed(initData, opts = {}) {
1813
1929
  }
1814
1930
  return parsed;
1815
1931
  }
1932
+ function describeInitData(initData, opts = {}) {
1933
+ if (typeof initData !== "string") {
1934
+ return {
1935
+ type: typeof initData,
1936
+ length: 0,
1937
+ fields: [],
1938
+ hasHash: false,
1939
+ hasSignature: false,
1940
+ hasAuthDate: false,
1941
+ hasUserId: false,
1942
+ reason: "not_a_string"
1943
+ };
1944
+ }
1945
+ const parsed = parseInitData(initData);
1946
+ const now = opts.nowUnix ?? Math.floor(Date.now() / 1e3);
1947
+ let reason = null;
1948
+ try {
1949
+ assertWellFormed(initData, opts);
1950
+ } catch (e) {
1951
+ if (e instanceof InitDataError) reason = e.reason;
1952
+ else throw e;
1953
+ }
1954
+ return {
1955
+ type: "string",
1956
+ length: initData.length,
1957
+ fields: Object.keys(parsed.params).sort(),
1958
+ hasHash: parsed.hash !== void 0,
1959
+ hasSignature: typeof parsed.params.signature === "string" && parsed.params.signature !== "",
1960
+ hasAuthDate: parsed.auth_date !== void 0,
1961
+ ...parsed.auth_date !== void 0 ? { authDateAgeSeconds: now - parsed.auth_date } : {},
1962
+ hasUserId: typeof parsed.user?.id === "number",
1963
+ reason
1964
+ };
1965
+ }
1816
1966
  var TelegramUnknownError = class extends Error {
1817
1967
  constructor(initData) {
1818
1968
  super("this Telegram account is not linked to any user (provision: false)");
@@ -1874,6 +2024,7 @@ async function fromTelegramWebApp(config, opts = {}) {
1874
2024
  exports.InitDataError = InitDataError;
1875
2025
  exports.TelegramUnknownError = TelegramUnknownError;
1876
2026
  exports.assertWellFormed = assertWellFormed;
2027
+ exports.describeInitData = describeInitData;
1877
2028
  exports.exchangeInitData = exchangeInitData;
1878
2029
  exports.fromInitData = fromInitData;
1879
2030
  exports.fromTelegramWebApp = fromTelegramWebApp;