@takeal/cusfront-sdk 0.6.11 → 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 +73 -0
  2. package/README.md +3 -2
  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-CuUdrQ0H.d.ts → cards-DPlgx76p.d.ts} +38 -11
  8. package/dist/{cards-nXmDP0ei.d.cts → cards-D_krTWzr.d.cts} +38 -11
  9. package/dist/index.cjs +207 -88
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +4 -4
  12. package/dist/index.d.ts +4 -4
  13. package/dist/index.js +204 -89
  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 +52 -3
  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 +52 -3
  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 +207 -68
  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 +207 -69
  58. package/dist/telegram.js.map +1 -1
  59. package/package.json +1 -1
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,6 +326,8 @@ 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
  }
187
333
  function revealState(r) {
@@ -216,8 +362,10 @@ var CardsResource = class {
216
362
  * `idempotencyKey` returns the same order.
217
363
  * Errors: 400 `unknown_product` / `method_not_available` / a limit of the
218
364
  * way of paying, 409 `holder_data_consent_required` (the product has
219
- * `holder_data_required` and the user has not agreed to it), 503 no
220
- * 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.
221
369
  */
222
370
  async order(input) {
223
371
  const { idempotencyKey, ...order } = input;
@@ -231,7 +379,9 @@ var CardsResource = class {
231
379
  * (`is_default` marks the one issued when none is chosen). Each item
232
380
  * carries its product card (name, tagline, benefits, tile picture),
233
381
  * artwork, and the issuance price after the caller's subscription
234
- * 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
235
385
  * `productId` to {@link create}.
236
386
  */
237
387
  async listProducts() {
@@ -476,8 +626,11 @@ var AuthResource = class {
476
626
  /** Password-auth entry. May resolve to a JWT, or to a step-up
477
627
  * challenge that needs `verifyTotp` / `verifyOtp` next. */
478
628
  async login(input) {
629
+ const body = withRemember(input);
630
+ const attribution = withAnalyticsIds(void 0);
631
+ if (attribution !== void 0) body["attribution"] = attribution;
479
632
  const resp = await this.http.post("/auth/login", {
480
- body: withRemember(input),
633
+ body,
481
634
  skipAuth: true
482
635
  });
483
636
  if (resp.stage === "jwt") {
@@ -514,6 +667,8 @@ var AuthResource = class {
514
667
  async register(input) {
515
668
  const { cardOrder, ...rest } = input;
516
669
  const body = withRemember(rest);
670
+ const attribution = withAnalyticsIds(rest.attribution);
671
+ if (attribution !== void 0) body["attribution"] = attribution;
517
672
  if (cardOrder !== void 0) body["card_order"] = cardOrderBody(cardOrder);
518
673
  const resp = await this.http.post("/auth/register", {
519
674
  body,
@@ -549,7 +704,8 @@ var AuthResource = class {
549
704
  };
550
705
  if (input.remember) body["remember_me"] = true;
551
706
  if (input.walletCurrency !== void 0) body["wallet_currency"] = input.walletCurrency;
552
- if (input.attribution !== void 0) body["attribution"] = input.attribution;
707
+ const attribution = withAnalyticsIds(input.attribution);
708
+ if (attribution !== void 0) body["attribution"] = attribution;
553
709
  if (input.promoCode !== void 0) body["promo_code"] = input.promoCode;
554
710
  if (input.consents !== void 0) body["consents"] = input.consents;
555
711
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
@@ -595,7 +751,8 @@ var AuthResource = class {
595
751
  init_data: input.initData
596
752
  };
597
753
  if (input.provision !== void 0) body.provision = input.provision;
598
- if (input.attribution !== void 0) body.attribution = input.attribution;
754
+ const attribution = withAnalyticsIds(input.attribution);
755
+ if (attribution !== void 0) body.attribution = attribution;
599
756
  if (input.code !== void 0) body.code = input.code;
600
757
  if (input.consents !== void 0) body.consents = input.consents;
601
758
  if (input.consentChannel !== void 0) body.consent_channel = input.consentChannel;
@@ -701,10 +858,17 @@ var DepositsResource = class {
701
858
  if (connectorSlug !== void 0) body["connector_slug"] = connectorSlug;
702
859
  return this.http.post("/me/deposits/quotes", { body });
703
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
+ */
704
866
  async initiate(input) {
705
867
  const { idempotencyKey, issue_card, ...rest } = input;
706
868
  const body = { ...rest };
707
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;
708
872
  return this.http.post("/me/deposits", {
709
873
  body,
710
874
  idempotencyKey
@@ -1306,67 +1470,6 @@ var DevicesResource = class {
1306
1470
  }
1307
1471
  };
1308
1472
 
1309
- // src/resources/attribution.ts
1310
- var AttributionResource = class {
1311
- constructor(http) {
1312
- this.http = http;
1313
- }
1314
- /** Record a visit. No session needed; safe to call on every page load. */
1315
- async touch(input) {
1316
- return this.http.post("/attribution/touches", { body: input, skipAuth: true });
1317
- }
1318
- /** Whether to ask the signed-in user how they heard about us. */
1319
- async status() {
1320
- return this.http.get("/me/attribution");
1321
- }
1322
- /** Answer the question once. Send `"skipped"` when the user closes it.
1323
- * `otherText` is kept only with `"other"`, up to 500 characters.
1324
- * Errors: 400 `validation`, 409 `survey_answered`. */
1325
- async answerSurvey(answer, otherText) {
1326
- const body = { answer };
1327
- if (otherText !== void 0) body.other_text = otherText;
1328
- return this.http.put("/me/attribution/survey", { body });
1329
- }
1330
- };
1331
- function tgWebApp() {
1332
- return globalThis.Telegram?.WebApp?.initDataUnsafe;
1333
- }
1334
- function readCookie(name) {
1335
- const doc = globalThis.document;
1336
- if (!doc?.cookie) return void 0;
1337
- for (const part of doc.cookie.split(";")) {
1338
- const [k, ...v] = part.trim().split("=");
1339
- if (k === name) return decodeURIComponent(v.join("="));
1340
- }
1341
- return void 0;
1342
- }
1343
- var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
1344
- function anonymousKey(opts = {}) {
1345
- const tgId = tgWebApp()?.user?.id;
1346
- if (tgId) return `tg:${tgId}`;
1347
- const g = globalThis;
1348
- if (!g.document) return void 0;
1349
- const name = opts.name ?? "takeal_anon";
1350
- let stored;
1351
- try {
1352
- stored = g.localStorage?.getItem(name) ?? void 0;
1353
- } catch {
1354
- stored = void 0;
1355
- }
1356
- const fromCookie = readCookie(name);
1357
- let key = [fromCookie, stored].find((k) => !!k && KEY_SHAPE.test(k));
1358
- if (!key) {
1359
- key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
1360
- }
1361
- try {
1362
- g.localStorage?.setItem(name, key);
1363
- } catch {
1364
- }
1365
- const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
1366
- g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
1367
- return key;
1368
- }
1369
-
1370
1473
  // src/resources/history.ts
1371
1474
  var HistoryResource = class {
1372
1475
  constructor(http) {
@@ -1470,6 +1573,7 @@ var ConsentsResource = class {
1470
1573
  const key = input.anonymousKey ?? anonymousKey();
1471
1574
  if (key !== void 0) body.anonymous_key = key;
1472
1575
  if (input.channel !== void 0) body.channel = input.channel;
1576
+ if (!input.analytics) forgetAnalyticsIds();
1473
1577
  return this.http.post("/consents/cookies", { body });
1474
1578
  }
1475
1579
  };
@@ -1567,7 +1671,7 @@ var FundingOptionsResource = class {
1567
1671
  };
1568
1672
 
1569
1673
  // src/version.ts
1570
- var SDK_VERSION = "0.6.11";
1674
+ var SDK_VERSION = "0.6.14";
1571
1675
 
1572
1676
  // src/client-platform.ts
1573
1677
  var CLIENT_HEADER = "X-Takeal-Client";
@@ -1825,6 +1929,40 @@ function assertWellFormed(initData, opts = {}) {
1825
1929
  }
1826
1930
  return parsed;
1827
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
+ }
1828
1966
  var TelegramUnknownError = class extends Error {
1829
1967
  constructor(initData) {
1830
1968
  super("this Telegram account is not linked to any user (provision: false)");
@@ -1886,6 +2024,7 @@ async function fromTelegramWebApp(config, opts = {}) {
1886
2024
  exports.InitDataError = InitDataError;
1887
2025
  exports.TelegramUnknownError = TelegramUnknownError;
1888
2026
  exports.assertWellFormed = assertWellFormed;
2027
+ exports.describeInitData = describeInitData;
1889
2028
  exports.exchangeInitData = exchangeInitData;
1890
2029
  exports.fromInitData = fromInitData;
1891
2030
  exports.fromTelegramWebApp = fromTelegramWebApp;