@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
@@ -1,9 +1,10 @@
1
1
  import { CusfrontClient, CreateClientOptions } from './index.cjs';
2
2
  import { JwtIssued, TelegramUnknown } from './resources/auth.cjs';
3
3
  import './http-CL97TjMO.cjs';
4
- import './cards-nXmDP0ei.cjs';
4
+ import './cards-D_krTWzr.cjs';
5
5
  import './resources/claims.cjs';
6
6
  import './resources/consents.cjs';
7
+ import './resources/attribution.cjs';
7
8
  import './resources/balance.cjs';
8
9
  import './resources/bonus.cjs';
9
10
  import './resources/debt.cjs';
@@ -21,7 +22,6 @@ import './resources/push.cjs';
21
22
  import './resources/me.cjs';
22
23
  import './resources/notifications.cjs';
23
24
  import './devices-CiJrlXum.cjs';
24
- import './resources/attribution.cjs';
25
25
  import './resources/history.cjs';
26
26
  import './resources/receipts.cjs';
27
27
  import './resources/support.cjs';
@@ -89,6 +89,37 @@ interface WellFormedOptions {
89
89
  * `user`, and (optionally) not stale. Throws {@link InitDataError}. Does NOT
90
90
  * verify the HMAC — only the server can. */
91
91
  declare function assertWellFormed(initData: string, opts?: WellFormedOptions): ParsedInitData;
92
+ /** What can be logged about an `initData` value without logging it: no
93
+ * values, no signatures, no user fields. Built by {@link describeInitData}. */
94
+ interface InitDataDiagnostics {
95
+ /** `typeof` of what was received (`"string"` for anything usable). */
96
+ type: string;
97
+ /** Length of the string, 0 for anything else. */
98
+ length: number;
99
+ /** Names of the fields present, sorted. Values are left out. */
100
+ fields: string[];
101
+ hasHash: boolean;
102
+ hasSignature: boolean;
103
+ /** `auth_date` is present and a unix timestamp. */
104
+ hasAuthDate: boolean;
105
+ /** Seconds since `auth_date`, when it parses. Negative means a clock ahead. */
106
+ authDateAgeSeconds?: number;
107
+ /** `user` is present and its `id` is a number. */
108
+ hasUserId: boolean;
109
+ /** Why {@link assertWellFormed} would refuse it, or `null` when it would not.
110
+ * `"not_a_string"` when the value is not a string at all. */
111
+ reason: InitDataError["reason"] | "not_a_string" | null;
112
+ }
113
+ /**
114
+ * Describe an `initData` value for a log line, without the value itself.
115
+ * Use it where the payload is refused, so the log says which check failed:
116
+ *
117
+ * console.warn("telegram sign-in refused", describeInitData(body.initData));
118
+ *
119
+ * The signature is checked only by the server; a payload this reports as
120
+ * well formed can still be refused there, with the reason in the 401.
121
+ */
122
+ declare function describeInitData(initData: unknown, opts?: WellFormedOptions): InitDataDiagnostics;
92
123
  interface FromInitDataOptions extends WellFormedOptions {
93
124
  /** Skip the client-side well-formedness check and let the server be the
94
125
  * sole judge. Default false — the fail-fast check saves a round-trip on
@@ -172,4 +203,4 @@ declare function fromTelegramWebApp(config: CreateClientOptions, opts?: FromInit
172
203
  * when you need the envelope rather than just the client. */
173
204
  type TelegramExchangeSession = JwtIssued;
174
205
 
175
- export { type FromInitDataOptions, InitDataError, type InitDataExchange, type ParsedInitData, type TelegramExchangeSession, TelegramUnknownError, type TelegramUser, type WellFormedOptions, assertWellFormed, exchangeInitData, fromInitData, fromTelegramWebApp, parseInitData, readWebAppInitData };
206
+ export { type FromInitDataOptions, type InitDataDiagnostics, InitDataError, type InitDataExchange, type ParsedInitData, type TelegramExchangeSession, TelegramUnknownError, type TelegramUser, type WellFormedOptions, assertWellFormed, describeInitData, exchangeInitData, fromInitData, fromTelegramWebApp, parseInitData, readWebAppInitData };
@@ -1,9 +1,10 @@
1
1
  import { CusfrontClient, CreateClientOptions } from './index.js';
2
2
  import { JwtIssued, TelegramUnknown } from './resources/auth.js';
3
3
  import './http-CL97TjMO.js';
4
- import './cards-CuUdrQ0H.js';
4
+ import './cards-DPlgx76p.js';
5
5
  import './resources/claims.js';
6
6
  import './resources/consents.js';
7
+ import './resources/attribution.js';
7
8
  import './resources/balance.js';
8
9
  import './resources/bonus.js';
9
10
  import './resources/debt.js';
@@ -21,7 +22,6 @@ import './resources/push.js';
21
22
  import './resources/me.js';
22
23
  import './resources/notifications.js';
23
24
  import './devices-C88c38hm.js';
24
- import './resources/attribution.js';
25
25
  import './resources/history.js';
26
26
  import './resources/receipts.js';
27
27
  import './resources/support.js';
@@ -89,6 +89,37 @@ interface WellFormedOptions {
89
89
  * `user`, and (optionally) not stale. Throws {@link InitDataError}. Does NOT
90
90
  * verify the HMAC — only the server can. */
91
91
  declare function assertWellFormed(initData: string, opts?: WellFormedOptions): ParsedInitData;
92
+ /** What can be logged about an `initData` value without logging it: no
93
+ * values, no signatures, no user fields. Built by {@link describeInitData}. */
94
+ interface InitDataDiagnostics {
95
+ /** `typeof` of what was received (`"string"` for anything usable). */
96
+ type: string;
97
+ /** Length of the string, 0 for anything else. */
98
+ length: number;
99
+ /** Names of the fields present, sorted. Values are left out. */
100
+ fields: string[];
101
+ hasHash: boolean;
102
+ hasSignature: boolean;
103
+ /** `auth_date` is present and a unix timestamp. */
104
+ hasAuthDate: boolean;
105
+ /** Seconds since `auth_date`, when it parses. Negative means a clock ahead. */
106
+ authDateAgeSeconds?: number;
107
+ /** `user` is present and its `id` is a number. */
108
+ hasUserId: boolean;
109
+ /** Why {@link assertWellFormed} would refuse it, or `null` when it would not.
110
+ * `"not_a_string"` when the value is not a string at all. */
111
+ reason: InitDataError["reason"] | "not_a_string" | null;
112
+ }
113
+ /**
114
+ * Describe an `initData` value for a log line, without the value itself.
115
+ * Use it where the payload is refused, so the log says which check failed:
116
+ *
117
+ * console.warn("telegram sign-in refused", describeInitData(body.initData));
118
+ *
119
+ * The signature is checked only by the server; a payload this reports as
120
+ * well formed can still be refused there, with the reason in the 401.
121
+ */
122
+ declare function describeInitData(initData: unknown, opts?: WellFormedOptions): InitDataDiagnostics;
92
123
  interface FromInitDataOptions extends WellFormedOptions {
93
124
  /** Skip the client-side well-formedness check and let the server be the
94
125
  * sole judge. Default false — the fail-fast check saves a round-trip on
@@ -172,4 +203,4 @@ declare function fromTelegramWebApp(config: CreateClientOptions, opts?: FromInit
172
203
  * when you need the envelope rather than just the client. */
173
204
  type TelegramExchangeSession = JwtIssued;
174
205
 
175
- export { type FromInitDataOptions, InitDataError, type InitDataExchange, type ParsedInitData, type TelegramExchangeSession, TelegramUnknownError, type TelegramUser, type WellFormedOptions, assertWellFormed, exchangeInitData, fromInitData, fromTelegramWebApp, parseInitData, readWebAppInitData };
206
+ export { type FromInitDataOptions, type InitDataDiagnostics, InitDataError, type InitDataExchange, type ParsedInitData, type TelegramExchangeSession, TelegramUnknownError, type TelegramUser, type WellFormedOptions, assertWellFormed, describeInitData, exchangeInitData, fromInitData, fromTelegramWebApp, parseInitData, readWebAppInitData };
package/dist/telegram.js CHANGED
@@ -142,6 +142,150 @@ async function parseResponse(resp) {
142
142
  return body;
143
143
  }
144
144
 
145
+ // src/resources/attribution.ts
146
+ var AttributionResource = class {
147
+ constructor(http) {
148
+ this.http = http;
149
+ }
150
+ /** Record a visit. No session needed; safe to call on every page load. */
151
+ async touch(input) {
152
+ return this.http.post("/attribution/touches", { body: input, skipAuth: true });
153
+ }
154
+ /** A one-day token that carries the visitor's analytics ids into the
155
+ * Telegram Mini App, whose sign-in reads it back. Most callers want
156
+ * {@link AttributionResource.telegramStartParam}. Errors: 400 when none of
157
+ * the ids is valid. */
158
+ async handoff(ids) {
159
+ return this.http.post("/attribution/handoff", { body: ids, skipAuth: true });
160
+ }
161
+ /**
162
+ * The `startapp` value for a link from the site to the Mini App: the code
163
+ * as it is, followed by `__<token>` when the visitor has analytics ids, so
164
+ * a payment made in Telegram is still credited to the visit it came from.
165
+ * Without ids, or when the token can't be had, just the code.
166
+ *
167
+ * ```ts
168
+ * const start = await client.attribution.telegramStartParam("SPRING");
169
+ * location.href = `https://t.me/our_bot/app?startapp=${start}`;
170
+ * ```
171
+ */
172
+ async telegramStartParam(code = "") {
173
+ const ids = analyticsIds();
174
+ if (!ids) return code;
175
+ try {
176
+ const { token } = await this.handoff(ids);
177
+ return `${code}__${token}`;
178
+ } catch {
179
+ return code;
180
+ }
181
+ }
182
+ /** Send the visitor's analytics ids for the signed-in account, e.g. right
183
+ * after they agreed to analytics cookies in the cabinet. Without an
184
+ * argument the ids this browser holds are sent; nothing is sent when it
185
+ * holds none. */
186
+ async sendAnalyticsIds(ids = analyticsIds()) {
187
+ if (!ids) return;
188
+ await this.http.post("/me/analytics/ids", { body: ids });
189
+ }
190
+ /** Whether to ask the signed-in user how they heard about us. */
191
+ async status() {
192
+ return this.http.get("/me/attribution");
193
+ }
194
+ /** Answer the question once. Send `"skipped"` when the user closes it.
195
+ * `otherText` is kept only with `"other"`, up to 500 characters.
196
+ * Errors: 400 `validation`, 409 `survey_answered`. */
197
+ async answerSurvey(answer, otherText) {
198
+ const body = { answer };
199
+ if (otherText !== void 0) body.other_text = otherText;
200
+ return this.http.put("/me/attribution/survey", { body });
201
+ }
202
+ };
203
+ function tgWebApp() {
204
+ return globalThis.Telegram?.WebApp?.initDataUnsafe;
205
+ }
206
+ function readCookie(name) {
207
+ const doc = globalThis.document;
208
+ if (!doc?.cookie) return void 0;
209
+ for (const part of doc.cookie.split(";")) {
210
+ const [k, ...v] = part.trim().split("=");
211
+ if (k === name) return decodeURIComponent(v.join("="));
212
+ }
213
+ return void 0;
214
+ }
215
+ var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
216
+ function anonymousKey(opts = {}) {
217
+ const tgId = tgWebApp()?.user?.id;
218
+ if (tgId) return `tg:${tgId}`;
219
+ const g = globalThis;
220
+ if (!g.document) return void 0;
221
+ const name = opts.name ?? "takeal_anon";
222
+ let stored2;
223
+ try {
224
+ stored2 = g.localStorage?.getItem(name) ?? void 0;
225
+ } catch {
226
+ stored2 = void 0;
227
+ }
228
+ const fromCookie = readCookie(name);
229
+ let key = [fromCookie, stored2].find((k) => !!k && KEY_SHAPE.test(k));
230
+ if (!key) {
231
+ key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
232
+ }
233
+ try {
234
+ g.localStorage?.setItem(name, key);
235
+ } catch {
236
+ }
237
+ const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
238
+ g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
239
+ return key;
240
+ }
241
+ var IDS_KEY = "takeal_analytics_ids";
242
+ function storage() {
243
+ try {
244
+ return globalThis.localStorage;
245
+ } catch {
246
+ return void 0;
247
+ }
248
+ }
249
+ function stored() {
250
+ try {
251
+ const raw = storage()?.getItem(IDS_KEY);
252
+ const v = raw ? JSON.parse(raw) : {};
253
+ return typeof v === "object" && v !== null ? v : {};
254
+ } catch {
255
+ return {};
256
+ }
257
+ }
258
+ function clean(ids) {
259
+ const out = {};
260
+ const ym = ids.metrika_client_id?.trim();
261
+ if (ym && /^\d{1,32}$/.test(ym)) out.metrika_client_id = ym;
262
+ const ga = ids.ga_client_id?.trim();
263
+ if (ga && /^\d+\.\d+$/.test(ga) && ga.length <= 64) out.ga_client_id = ga;
264
+ return out.metrika_client_id || out.ga_client_id ? out : void 0;
265
+ }
266
+ function forgetAnalyticsIds() {
267
+ try {
268
+ storage()?.removeItem(IDS_KEY);
269
+ } catch {
270
+ }
271
+ }
272
+ function analyticsIds() {
273
+ const kept = stored();
274
+ return clean({
275
+ metrika_client_id: kept.metrika_client_id ?? readCookie("_ym_uid"),
276
+ ga_client_id: kept.ga_client_id ?? readCookie("_ga")?.split(".").slice(-2).join(".")
277
+ });
278
+ }
279
+ function withAnalyticsIds(attribution) {
280
+ const ids = analyticsIds();
281
+ if (!ids) return attribution;
282
+ return {
283
+ ...attribution,
284
+ metrika_client_id: attribution?.metrika_client_id ?? ids.metrika_client_id,
285
+ ga_client_id: attribution?.ga_client_id ?? ids.ga_client_id
286
+ };
287
+ }
288
+
145
289
  // src/resources/cards.ts
146
290
  function cardRequestBody(input) {
147
291
  const {
@@ -180,6 +324,8 @@ function cardOrderBody(input) {
180
324
  if (input.consents !== void 0) body["consents"] = input.consents;
181
325
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
182
326
  if (input.metadata !== void 0) body["metadata"] = input.metadata;
327
+ const ids = input.analyticsIds ?? analyticsIds();
328
+ if (ids !== void 0) body["analytics_ids"] = ids;
183
329
  return body;
184
330
  }
185
331
  function revealState(r) {
@@ -214,8 +360,10 @@ var CardsResource = class {
214
360
  * `idempotencyKey` returns the same order.
215
361
  * Errors: 400 `unknown_product` / `method_not_available` / a limit of the
216
362
  * way of paying, 409 `holder_data_consent_required` (the product has
217
- * `holder_data_required` and the user has not agreed to it), 503 no
218
- * funding connector or no exchange rate.
363
+ * `holder_data_required` and the user has not agreed to it), 409
364
+ * `payments_on_hold` (a payment on this account was not accepted and new
365
+ * ones wait until support sorts it out: show the message and point the
366
+ * user to support), 503 no funding connector or no exchange rate.
219
367
  */
220
368
  async order(input) {
221
369
  const { idempotencyKey, ...order } = input;
@@ -229,7 +377,9 @@ var CardsResource = class {
229
377
  * (`is_default` marks the one issued when none is chosen). Each item
230
378
  * carries its product card (name, tagline, benefits, tile picture),
231
379
  * artwork, and the issuance price after the caller's subscription
232
- * discount, in the caller's wallet currency. Pass an item's `id` as
380
+ * discount, in the currency the operator set it in (`price.from_wallet`
381
+ * is the same price from the wallet, when that holds another currency).
382
+ * Pass an item's `id` as
233
383
  * `productId` to {@link create}.
234
384
  */
235
385
  async listProducts() {
@@ -474,8 +624,11 @@ var AuthResource = class {
474
624
  /** Password-auth entry. May resolve to a JWT, or to a step-up
475
625
  * challenge that needs `verifyTotp` / `verifyOtp` next. */
476
626
  async login(input) {
627
+ const body = withRemember(input);
628
+ const attribution = withAnalyticsIds(void 0);
629
+ if (attribution !== void 0) body["attribution"] = attribution;
477
630
  const resp = await this.http.post("/auth/login", {
478
- body: withRemember(input),
631
+ body,
479
632
  skipAuth: true
480
633
  });
481
634
  if (resp.stage === "jwt") {
@@ -512,6 +665,8 @@ var AuthResource = class {
512
665
  async register(input) {
513
666
  const { cardOrder, ...rest } = input;
514
667
  const body = withRemember(rest);
668
+ const attribution = withAnalyticsIds(rest.attribution);
669
+ if (attribution !== void 0) body["attribution"] = attribution;
515
670
  if (cardOrder !== void 0) body["card_order"] = cardOrderBody(cardOrder);
516
671
  const resp = await this.http.post("/auth/register", {
517
672
  body,
@@ -547,7 +702,8 @@ var AuthResource = class {
547
702
  };
548
703
  if (input.remember) body["remember_me"] = true;
549
704
  if (input.walletCurrency !== void 0) body["wallet_currency"] = input.walletCurrency;
550
- if (input.attribution !== void 0) body["attribution"] = input.attribution;
705
+ const attribution = withAnalyticsIds(input.attribution);
706
+ if (attribution !== void 0) body["attribution"] = attribution;
551
707
  if (input.promoCode !== void 0) body["promo_code"] = input.promoCode;
552
708
  if (input.consents !== void 0) body["consents"] = input.consents;
553
709
  if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
@@ -593,7 +749,8 @@ var AuthResource = class {
593
749
  init_data: input.initData
594
750
  };
595
751
  if (input.provision !== void 0) body.provision = input.provision;
596
- if (input.attribution !== void 0) body.attribution = input.attribution;
752
+ const attribution = withAnalyticsIds(input.attribution);
753
+ if (attribution !== void 0) body.attribution = attribution;
597
754
  if (input.code !== void 0) body.code = input.code;
598
755
  if (input.consents !== void 0) body.consents = input.consents;
599
756
  if (input.consentChannel !== void 0) body.consent_channel = input.consentChannel;
@@ -699,10 +856,17 @@ var DepositsResource = class {
699
856
  if (connectorSlug !== void 0) body["connector_slug"] = connectorSlug;
700
857
  return this.http.post("/me/deposits/quotes", { body });
701
858
  }
859
+ /**
860
+ * Start a top-up. 409 `payments_on_hold`: a payment on this account was
861
+ * not accepted and new ones wait until support sorts it out. Show the
862
+ * message as it comes and point the user to support.
863
+ */
702
864
  async initiate(input) {
703
865
  const { idempotencyKey, issue_card, ...rest } = input;
704
866
  const body = { ...rest };
705
867
  if (issue_card !== void 0) body["issue_card"] = cardRequestBody(issue_card);
868
+ const ids = input.analytics_ids ?? analyticsIds();
869
+ if (ids !== void 0) body["analytics_ids"] = ids;
706
870
  return this.http.post("/me/deposits", {
707
871
  body,
708
872
  idempotencyKey
@@ -1304,67 +1468,6 @@ var DevicesResource = class {
1304
1468
  }
1305
1469
  };
1306
1470
 
1307
- // src/resources/attribution.ts
1308
- var AttributionResource = class {
1309
- constructor(http) {
1310
- this.http = http;
1311
- }
1312
- /** Record a visit. No session needed; safe to call on every page load. */
1313
- async touch(input) {
1314
- return this.http.post("/attribution/touches", { body: input, skipAuth: true });
1315
- }
1316
- /** Whether to ask the signed-in user how they heard about us. */
1317
- async status() {
1318
- return this.http.get("/me/attribution");
1319
- }
1320
- /** Answer the question once. Send `"skipped"` when the user closes it.
1321
- * `otherText` is kept only with `"other"`, up to 500 characters.
1322
- * Errors: 400 `validation`, 409 `survey_answered`. */
1323
- async answerSurvey(answer, otherText) {
1324
- const body = { answer };
1325
- if (otherText !== void 0) body.other_text = otherText;
1326
- return this.http.put("/me/attribution/survey", { body });
1327
- }
1328
- };
1329
- function tgWebApp() {
1330
- return globalThis.Telegram?.WebApp?.initDataUnsafe;
1331
- }
1332
- function readCookie(name) {
1333
- const doc = globalThis.document;
1334
- if (!doc?.cookie) return void 0;
1335
- for (const part of doc.cookie.split(";")) {
1336
- const [k, ...v] = part.trim().split("=");
1337
- if (k === name) return decodeURIComponent(v.join("="));
1338
- }
1339
- return void 0;
1340
- }
1341
- var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
1342
- function anonymousKey(opts = {}) {
1343
- const tgId = tgWebApp()?.user?.id;
1344
- if (tgId) return `tg:${tgId}`;
1345
- const g = globalThis;
1346
- if (!g.document) return void 0;
1347
- const name = opts.name ?? "takeal_anon";
1348
- let stored;
1349
- try {
1350
- stored = g.localStorage?.getItem(name) ?? void 0;
1351
- } catch {
1352
- stored = void 0;
1353
- }
1354
- const fromCookie = readCookie(name);
1355
- let key = [fromCookie, stored].find((k) => !!k && KEY_SHAPE.test(k));
1356
- if (!key) {
1357
- key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
1358
- }
1359
- try {
1360
- g.localStorage?.setItem(name, key);
1361
- } catch {
1362
- }
1363
- const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
1364
- g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
1365
- return key;
1366
- }
1367
-
1368
1471
  // src/resources/history.ts
1369
1472
  var HistoryResource = class {
1370
1473
  constructor(http) {
@@ -1468,6 +1571,7 @@ var ConsentsResource = class {
1468
1571
  const key = input.anonymousKey ?? anonymousKey();
1469
1572
  if (key !== void 0) body.anonymous_key = key;
1470
1573
  if (input.channel !== void 0) body.channel = input.channel;
1574
+ if (!input.analytics) forgetAnalyticsIds();
1471
1575
  return this.http.post("/consents/cookies", { body });
1472
1576
  }
1473
1577
  };
@@ -1565,7 +1669,7 @@ var FundingOptionsResource = class {
1565
1669
  };
1566
1670
 
1567
1671
  // src/version.ts
1568
- var SDK_VERSION = "0.6.11";
1672
+ var SDK_VERSION = "0.6.14";
1569
1673
 
1570
1674
  // src/client-platform.ts
1571
1675
  var CLIENT_HEADER = "X-Takeal-Client";
@@ -1823,6 +1927,40 @@ function assertWellFormed(initData, opts = {}) {
1823
1927
  }
1824
1928
  return parsed;
1825
1929
  }
1930
+ function describeInitData(initData, opts = {}) {
1931
+ if (typeof initData !== "string") {
1932
+ return {
1933
+ type: typeof initData,
1934
+ length: 0,
1935
+ fields: [],
1936
+ hasHash: false,
1937
+ hasSignature: false,
1938
+ hasAuthDate: false,
1939
+ hasUserId: false,
1940
+ reason: "not_a_string"
1941
+ };
1942
+ }
1943
+ const parsed = parseInitData(initData);
1944
+ const now = opts.nowUnix ?? Math.floor(Date.now() / 1e3);
1945
+ let reason = null;
1946
+ try {
1947
+ assertWellFormed(initData, opts);
1948
+ } catch (e) {
1949
+ if (e instanceof InitDataError) reason = e.reason;
1950
+ else throw e;
1951
+ }
1952
+ return {
1953
+ type: "string",
1954
+ length: initData.length,
1955
+ fields: Object.keys(parsed.params).sort(),
1956
+ hasHash: parsed.hash !== void 0,
1957
+ hasSignature: typeof parsed.params.signature === "string" && parsed.params.signature !== "",
1958
+ hasAuthDate: parsed.auth_date !== void 0,
1959
+ ...parsed.auth_date !== void 0 ? { authDateAgeSeconds: now - parsed.auth_date } : {},
1960
+ hasUserId: typeof parsed.user?.id === "number",
1961
+ reason
1962
+ };
1963
+ }
1826
1964
  var TelegramUnknownError = class extends Error {
1827
1965
  constructor(initData) {
1828
1966
  super("this Telegram account is not linked to any user (provision: false)");
@@ -1881,6 +2019,6 @@ async function fromTelegramWebApp(config, opts = {}) {
1881
2019
  return fromInitData(initData, config, opts);
1882
2020
  }
1883
2021
 
1884
- export { InitDataError, TelegramUnknownError, assertWellFormed, exchangeInitData, fromInitData, fromTelegramWebApp, parseInitData, readWebAppInitData };
2022
+ export { InitDataError, TelegramUnknownError, assertWellFormed, describeInitData, exchangeInitData, fromInitData, fromTelegramWebApp, parseInitData, readWebAppInitData };
1885
2023
  //# sourceMappingURL=telegram.js.map
1886
2024
  //# sourceMappingURL=telegram.js.map