@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/index.cjs CHANGED
@@ -147,6 +147,172 @@ async function parseResponse(resp) {
147
147
  return body;
148
148
  }
149
149
 
150
+ // src/resources/attribution.ts
151
+ var AttributionResource = class {
152
+ constructor(http) {
153
+ this.http = http;
154
+ }
155
+ /** Record a visit. No session needed; safe to call on every page load. */
156
+ async touch(input) {
157
+ return this.http.post("/attribution/touches", { body: input, skipAuth: true });
158
+ }
159
+ /** A one-day token that carries the visitor's analytics ids into the
160
+ * Telegram Mini App, whose sign-in reads it back. Most callers want
161
+ * {@link AttributionResource.telegramStartParam}. Errors: 400 when none of
162
+ * the ids is valid. */
163
+ async handoff(ids) {
164
+ return this.http.post("/attribution/handoff", { body: ids, skipAuth: true });
165
+ }
166
+ /**
167
+ * The `startapp` value for a link from the site to the Mini App: the code
168
+ * as it is, followed by `__<token>` when the visitor has analytics ids, so
169
+ * a payment made in Telegram is still credited to the visit it came from.
170
+ * Without ids, or when the token can't be had, just the code.
171
+ *
172
+ * ```ts
173
+ * const start = await client.attribution.telegramStartParam("SPRING");
174
+ * location.href = `https://t.me/our_bot/app?startapp=${start}`;
175
+ * ```
176
+ */
177
+ async telegramStartParam(code = "") {
178
+ const ids = analyticsIds();
179
+ if (!ids) return code;
180
+ try {
181
+ const { token } = await this.handoff(ids);
182
+ return `${code}__${token}`;
183
+ } catch {
184
+ return code;
185
+ }
186
+ }
187
+ /** Send the visitor's analytics ids for the signed-in account, e.g. right
188
+ * after they agreed to analytics cookies in the cabinet. Without an
189
+ * argument the ids this browser holds are sent; nothing is sent when it
190
+ * holds none. */
191
+ async sendAnalyticsIds(ids = analyticsIds()) {
192
+ if (!ids) return;
193
+ await this.http.post("/me/analytics/ids", { body: ids });
194
+ }
195
+ /** Whether to ask the signed-in user how they heard about us. */
196
+ async status() {
197
+ return this.http.get("/me/attribution");
198
+ }
199
+ /** Answer the question once. Send `"skipped"` when the user closes it.
200
+ * `otherText` is kept only with `"other"`, up to 500 characters.
201
+ * Errors: 400 `validation`, 409 `survey_answered`. */
202
+ async answerSurvey(answer, otherText) {
203
+ const body = { answer };
204
+ if (otherText !== void 0) body.other_text = otherText;
205
+ return this.http.put("/me/attribution/survey", { body });
206
+ }
207
+ };
208
+ function tgWebApp() {
209
+ return globalThis.Telegram?.WebApp?.initDataUnsafe;
210
+ }
211
+ function readCookie(name) {
212
+ const doc = globalThis.document;
213
+ if (!doc?.cookie) return void 0;
214
+ for (const part of doc.cookie.split(";")) {
215
+ const [k, ...v] = part.trim().split("=");
216
+ if (k === name) return decodeURIComponent(v.join("="));
217
+ }
218
+ return void 0;
219
+ }
220
+ var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
221
+ function anonymousKey(opts = {}) {
222
+ const tgId = tgWebApp()?.user?.id;
223
+ if (tgId) return `tg:${tgId}`;
224
+ const g = globalThis;
225
+ if (!g.document) return void 0;
226
+ const name = opts.name ?? "takeal_anon";
227
+ let stored2;
228
+ try {
229
+ stored2 = g.localStorage?.getItem(name) ?? void 0;
230
+ } catch {
231
+ stored2 = void 0;
232
+ }
233
+ const fromCookie = readCookie(name);
234
+ let key = [fromCookie, stored2].find((k) => !!k && KEY_SHAPE.test(k));
235
+ if (!key) {
236
+ key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
237
+ }
238
+ try {
239
+ g.localStorage?.setItem(name, key);
240
+ } catch {
241
+ }
242
+ const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
243
+ g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
244
+ return key;
245
+ }
246
+ function currentTouch() {
247
+ const g = globalThis;
248
+ const tg = tgWebApp();
249
+ const out = {};
250
+ if (g.location?.href) out.url = g.location.href;
251
+ if (g.document?.referrer) out.referrer = g.document.referrer;
252
+ if (tg?.start_param) out.campaign_code = tg.start_param;
253
+ if (tg?.user?.id) out.platform = "miniapp";
254
+ const ym = readCookie("_ym_uid");
255
+ if (ym) out.metrika_client_id = ym;
256
+ const ga = readCookie("_ga")?.split(".").slice(-2).join(".");
257
+ if (ga) out.ga_client_id = ga;
258
+ return out;
259
+ }
260
+ var IDS_KEY = "takeal_analytics_ids";
261
+ function storage() {
262
+ try {
263
+ return globalThis.localStorage;
264
+ } catch {
265
+ return void 0;
266
+ }
267
+ }
268
+ function stored() {
269
+ try {
270
+ const raw = storage()?.getItem(IDS_KEY);
271
+ const v = raw ? JSON.parse(raw) : {};
272
+ return typeof v === "object" && v !== null ? v : {};
273
+ } catch {
274
+ return {};
275
+ }
276
+ }
277
+ function clean(ids) {
278
+ const out = {};
279
+ const ym = ids.metrika_client_id?.trim();
280
+ if (ym && /^\d{1,32}$/.test(ym)) out.metrika_client_id = ym;
281
+ const ga = ids.ga_client_id?.trim();
282
+ if (ga && /^\d+\.\d+$/.test(ga) && ga.length <= 64) out.ga_client_id = ga;
283
+ return out.metrika_client_id || out.ga_client_id ? out : void 0;
284
+ }
285
+ function rememberAnalyticsIds(ids) {
286
+ const merged = clean({ ...stored(), ...ids });
287
+ if (!merged) return;
288
+ try {
289
+ storage()?.setItem(IDS_KEY, JSON.stringify(merged));
290
+ } catch {
291
+ }
292
+ }
293
+ function forgetAnalyticsIds() {
294
+ try {
295
+ storage()?.removeItem(IDS_KEY);
296
+ } catch {
297
+ }
298
+ }
299
+ function analyticsIds() {
300
+ const kept = stored();
301
+ return clean({
302
+ metrika_client_id: kept.metrika_client_id ?? readCookie("_ym_uid"),
303
+ ga_client_id: kept.ga_client_id ?? readCookie("_ga")?.split(".").slice(-2).join(".")
304
+ });
305
+ }
306
+ function withAnalyticsIds(attribution) {
307
+ const ids = analyticsIds();
308
+ if (!ids) return attribution;
309
+ return {
310
+ ...attribution,
311
+ metrika_client_id: attribution?.metrika_client_id ?? ids.metrika_client_id,
312
+ ga_client_id: attribution?.ga_client_id ?? ids.ga_client_id
313
+ };
314
+ }
315
+
150
316
  // src/resources/cards.ts
151
317
  function cardRequestBody(input) {
152
318
  const {
@@ -185,6 +351,8 @@ function cardOrderBody(input) {
185
351
  if (input.consents !== void 0) body["consents"] = input.consents;
186
352
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
187
353
  if (input.metadata !== void 0) body["metadata"] = input.metadata;
354
+ const ids = input.analyticsIds ?? analyticsIds();
355
+ if (ids !== void 0) body["analytics_ids"] = ids;
188
356
  return body;
189
357
  }
190
358
  function revealState(r) {
@@ -219,8 +387,10 @@ var CardsResource = class {
219
387
  * `idempotencyKey` returns the same order.
220
388
  * Errors: 400 `unknown_product` / `method_not_available` / a limit of the
221
389
  * way of paying, 409 `holder_data_consent_required` (the product has
222
- * `holder_data_required` and the user has not agreed to it), 503 no
223
- * funding connector or no exchange rate.
390
+ * `holder_data_required` and the user has not agreed to it), 409
391
+ * `payments_on_hold` (a payment on this account was not accepted and new
392
+ * ones wait until support sorts it out: show the message and point the
393
+ * user to support), 503 no funding connector or no exchange rate.
224
394
  */
225
395
  async order(input) {
226
396
  const { idempotencyKey, ...order } = input;
@@ -234,7 +404,9 @@ var CardsResource = class {
234
404
  * (`is_default` marks the one issued when none is chosen). Each item
235
405
  * carries its product card (name, tagline, benefits, tile picture),
236
406
  * artwork, and the issuance price after the caller's subscription
237
- * discount, in the caller's wallet currency. Pass an item's `id` as
407
+ * discount, in the currency the operator set it in (`price.from_wallet`
408
+ * is the same price from the wallet, when that holds another currency).
409
+ * Pass an item's `id` as
238
410
  * `productId` to {@link create}.
239
411
  */
240
412
  async listProducts() {
@@ -479,8 +651,11 @@ var AuthResource = class {
479
651
  /** Password-auth entry. May resolve to a JWT, or to a step-up
480
652
  * challenge that needs `verifyTotp` / `verifyOtp` next. */
481
653
  async login(input) {
654
+ const body = withRemember(input);
655
+ const attribution = withAnalyticsIds(void 0);
656
+ if (attribution !== void 0) body["attribution"] = attribution;
482
657
  const resp = await this.http.post("/auth/login", {
483
- body: withRemember(input),
658
+ body,
484
659
  skipAuth: true
485
660
  });
486
661
  if (resp.stage === "jwt") {
@@ -517,6 +692,8 @@ var AuthResource = class {
517
692
  async register(input) {
518
693
  const { cardOrder, ...rest } = input;
519
694
  const body = withRemember(rest);
695
+ const attribution = withAnalyticsIds(rest.attribution);
696
+ if (attribution !== void 0) body["attribution"] = attribution;
520
697
  if (cardOrder !== void 0) body["card_order"] = cardOrderBody(cardOrder);
521
698
  const resp = await this.http.post("/auth/register", {
522
699
  body,
@@ -552,7 +729,8 @@ var AuthResource = class {
552
729
  };
553
730
  if (input.remember) body["remember_me"] = true;
554
731
  if (input.walletCurrency !== void 0) body["wallet_currency"] = input.walletCurrency;
555
- if (input.attribution !== void 0) body["attribution"] = input.attribution;
732
+ const attribution = withAnalyticsIds(input.attribution);
733
+ if (attribution !== void 0) body["attribution"] = attribution;
556
734
  if (input.promoCode !== void 0) body["promo_code"] = input.promoCode;
557
735
  if (input.consents !== void 0) body["consents"] = input.consents;
558
736
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
@@ -598,7 +776,8 @@ var AuthResource = class {
598
776
  init_data: input.initData
599
777
  };
600
778
  if (input.provision !== void 0) body.provision = input.provision;
601
- if (input.attribution !== void 0) body.attribution = input.attribution;
779
+ const attribution = withAnalyticsIds(input.attribution);
780
+ if (attribution !== void 0) body.attribution = attribution;
602
781
  if (input.code !== void 0) body.code = input.code;
603
782
  if (input.consents !== void 0) body.consents = input.consents;
604
783
  if (input.consentChannel !== void 0) body.consent_channel = input.consentChannel;
@@ -704,10 +883,17 @@ var DepositsResource = class {
704
883
  if (connectorSlug !== void 0) body["connector_slug"] = connectorSlug;
705
884
  return this.http.post("/me/deposits/quotes", { body });
706
885
  }
886
+ /**
887
+ * Start a top-up. 409 `payments_on_hold`: a payment on this account was
888
+ * not accepted and new ones wait until support sorts it out. Show the
889
+ * message as it comes and point the user to support.
890
+ */
707
891
  async initiate(input) {
708
892
  const { idempotencyKey, issue_card, ...rest } = input;
709
893
  const body = { ...rest };
710
894
  if (issue_card !== void 0) body["issue_card"] = cardRequestBody(issue_card);
895
+ const ids = input.analytics_ids ?? analyticsIds();
896
+ if (ids !== void 0) body["analytics_ids"] = ids;
711
897
  return this.http.post("/me/deposits", {
712
898
  body,
713
899
  idempotencyKey
@@ -1312,81 +1498,6 @@ var DevicesResource = class {
1312
1498
  }
1313
1499
  };
1314
1500
 
1315
- // src/resources/attribution.ts
1316
- var AttributionResource = class {
1317
- constructor(http) {
1318
- this.http = http;
1319
- }
1320
- /** Record a visit. No session needed; safe to call on every page load. */
1321
- async touch(input) {
1322
- return this.http.post("/attribution/touches", { body: input, skipAuth: true });
1323
- }
1324
- /** Whether to ask the signed-in user how they heard about us. */
1325
- async status() {
1326
- return this.http.get("/me/attribution");
1327
- }
1328
- /** Answer the question once. Send `"skipped"` when the user closes it.
1329
- * `otherText` is kept only with `"other"`, up to 500 characters.
1330
- * Errors: 400 `validation`, 409 `survey_answered`. */
1331
- async answerSurvey(answer, otherText) {
1332
- const body = { answer };
1333
- if (otherText !== void 0) body.other_text = otherText;
1334
- return this.http.put("/me/attribution/survey", { body });
1335
- }
1336
- };
1337
- function tgWebApp() {
1338
- return globalThis.Telegram?.WebApp?.initDataUnsafe;
1339
- }
1340
- function readCookie(name) {
1341
- const doc = globalThis.document;
1342
- if (!doc?.cookie) return void 0;
1343
- for (const part of doc.cookie.split(";")) {
1344
- const [k, ...v] = part.trim().split("=");
1345
- if (k === name) return decodeURIComponent(v.join("="));
1346
- }
1347
- return void 0;
1348
- }
1349
- var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
1350
- function anonymousKey(opts = {}) {
1351
- const tgId = tgWebApp()?.user?.id;
1352
- if (tgId) return `tg:${tgId}`;
1353
- const g = globalThis;
1354
- if (!g.document) return void 0;
1355
- const name = opts.name ?? "takeal_anon";
1356
- let stored;
1357
- try {
1358
- stored = g.localStorage?.getItem(name) ?? void 0;
1359
- } catch {
1360
- stored = void 0;
1361
- }
1362
- const fromCookie = readCookie(name);
1363
- let key = [fromCookie, stored].find((k) => !!k && KEY_SHAPE.test(k));
1364
- if (!key) {
1365
- key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
1366
- }
1367
- try {
1368
- g.localStorage?.setItem(name, key);
1369
- } catch {
1370
- }
1371
- const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
1372
- g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
1373
- return key;
1374
- }
1375
- function currentTouch() {
1376
- const g = globalThis;
1377
- const tg = tgWebApp();
1378
- const out = {};
1379
- if (g.location?.href) out.url = g.location.href;
1380
- if (g.document?.referrer) out.referrer = g.document.referrer;
1381
- if (tg?.start_param) out.campaign_code = tg.start_param;
1382
- if (tg?.user?.id) out.platform = "miniapp";
1383
- const ym = readCookie("_ym_uid");
1384
- if (ym) out.metrika_client_id = ym;
1385
- const ga = readCookie("_ga")?.split(".").slice(-2).join(".");
1386
- if (ga) out.ga_client_id = ga;
1387
- return out;
1388
- }
1389
-
1390
1501
  // src/resources/history.ts
1391
1502
  var HistoryResource = class {
1392
1503
  constructor(http) {
@@ -1490,6 +1601,7 @@ var ConsentsResource = class {
1490
1601
  const key = input.anonymousKey ?? anonymousKey();
1491
1602
  if (key !== void 0) body.anonymous_key = key;
1492
1603
  if (input.channel !== void 0) body.channel = input.channel;
1604
+ if (!input.analytics) forgetAnalyticsIds();
1493
1605
  return this.http.post("/consents/cookies", { body });
1494
1606
  }
1495
1607
  };
@@ -1587,7 +1699,7 @@ var FundingOptionsResource = class {
1587
1699
  };
1588
1700
 
1589
1701
  // src/version.ts
1590
- var SDK_VERSION = "0.6.11";
1702
+ var SDK_VERSION = "0.6.14";
1591
1703
 
1592
1704
  // src/client-platform.ts
1593
1705
  var CLIENT_HEADER = "X-Takeal-Client";
@@ -2061,6 +2173,9 @@ var MetrikaSink = class {
2061
2173
  trackLinks: false,
2062
2174
  accurateTrackBounce: true
2063
2175
  });
2176
+ this.ym("getClientID", (id) => {
2177
+ if (id !== void 0 && id !== null) rememberAnalyticsIds({ metrika_client_id: String(id) });
2178
+ });
2064
2179
  }
2065
2180
  ym(...args) {
2066
2181
  if (this.live) win().ym?.(this.id, ...args);
@@ -2112,19 +2227,19 @@ function setAnalyticsZone(zone) {
2112
2227
  function track(event, props = {}) {
2113
2228
  const s = state();
2114
2229
  if (!s || !ANALYTICS_EVENTS.includes(event)) return;
2115
- const clean = sanitizeProperties(props);
2116
- for (const sink of sinks(s)) sink.capture(event, clean);
2230
+ const clean2 = sanitizeProperties(props);
2231
+ for (const sink of sinks(s)) sink.capture(event, clean2);
2117
2232
  }
2118
2233
  function page(url) {
2119
2234
  const s = state();
2120
2235
  const w = win();
2121
2236
  const raw = url ?? w.location?.href;
2122
2237
  if (!s || !raw) return;
2123
- const clean = sanitizeUrl(raw);
2124
- if (clean === s.lastPage) return;
2238
+ const clean2 = sanitizeUrl(raw);
2239
+ if (clean2 === s.lastPage) return;
2125
2240
  const referrer = s.lastPage ?? (w.document?.referrer ? sanitizeUrl(w.document.referrer) : void 0);
2126
- s.lastPage = clean;
2127
- for (const sink of sinks(s)) sink.page(clean, referrer);
2241
+ s.lastPage = clean2;
2242
+ for (const sink of sinks(s)) sink.page(clean2, referrer);
2128
2243
  }
2129
2244
  function identify(userId) {
2130
2245
  const s = state();
@@ -2223,11 +2338,13 @@ exports.NO_CAPTURE_CLASS = NO_CAPTURE_CLASS;
2223
2338
  exports.NetworkError = NetworkError;
2224
2339
  exports.SDK_VERSION = SDK_VERSION;
2225
2340
  exports.SUPPORTED_MEISTER = SUPPORTED_MEISTER;
2341
+ exports.analyticsIds = analyticsIds;
2226
2342
  exports.anonymousKey = anonymousKey;
2227
2343
  exports.createClient = createClient;
2228
2344
  exports.currentTouch = currentTouch;
2229
2345
  exports.defaultBrowserStore = defaultBrowserStore;
2230
2346
  exports.detectClientPlatform = detectClientPlatform;
2347
+ exports.forgetAnalyticsIds = forgetAnalyticsIds;
2231
2348
  exports.identify = identify;
2232
2349
  exports.inMemoryStore = inMemoryStore;
2233
2350
  exports.initAnalytics = initAnalytics;
@@ -2236,6 +2353,7 @@ exports.isCardOtp = isCardOtp;
2236
2353
  exports.isNetworkError = isNetworkError;
2237
2354
  exports.page = page;
2238
2355
  exports.posthogDistinctId = posthogDistinctId;
2356
+ exports.rememberAnalyticsIds = rememberAnalyticsIds;
2239
2357
  exports.resetAnalytics = resetAnalytics;
2240
2358
  exports.resolveAssetUrl = resolveAssetUrl;
2241
2359
  exports.revealState = revealState;
@@ -2244,5 +2362,6 @@ exports.sanitizeUrl = sanitizeUrl;
2244
2362
  exports.satisfies = satisfies;
2245
2363
  exports.setAnalyticsZone = setAnalyticsZone;
2246
2364
  exports.track = track;
2365
+ exports.withAnalyticsIds = withAnalyticsIds;
2247
2366
  //# sourceMappingURL=index.cjs.map
2248
2367
  //# sourceMappingURL=index.cjs.map