@takeal/cusfront-sdk 0.3.20 → 0.5.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.
Files changed (121) hide show
  1. package/CHANGELOG.md +127 -0
  2. package/README.md +407 -8
  3. package/dist/analytics.cjs +417 -0
  4. package/dist/analytics.cjs.map +1 -0
  5. package/dist/analytics.d.cts +129 -0
  6. package/dist/analytics.d.ts +129 -0
  7. package/dist/analytics.js +403 -0
  8. package/dist/analytics.js.map +1 -0
  9. package/dist/cards-B_aNY35J.d.cts +514 -0
  10. package/dist/cards-Ba1wZttb.d.ts +514 -0
  11. package/dist/deposits-BEc8U80M.d.cts +310 -0
  12. package/dist/deposits-DScyT_D9.d.ts +310 -0
  13. package/dist/{devices-DP_hxwmW.d.ts → devices-B_YveTl9.d.ts} +1 -1
  14. package/dist/{devices-C3JHi2Hi.d.cts → devices-a5Z45Mx_.d.cts} +1 -1
  15. package/dist/{http-B-OQwMYn.d.cts → http-DeANAoTs.d.cts} +11 -3
  16. package/dist/{http-B-OQwMYn.d.ts → http-DeANAoTs.d.ts} +11 -3
  17. package/dist/index.cjs +939 -77
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.cts +101 -13
  20. package/dist/index.d.ts +101 -13
  21. package/dist/index.js +924 -78
  22. package/dist/index.js.map +1 -1
  23. package/dist/react/index.d.cts +12 -5
  24. package/dist/react/index.d.ts +12 -5
  25. package/dist/resources/attribution.cjs +82 -0
  26. package/dist/resources/attribution.cjs.map +1 -0
  27. package/dist/resources/attribution.d.cts +100 -0
  28. package/dist/resources/attribution.d.ts +100 -0
  29. package/dist/resources/attribution.js +78 -0
  30. package/dist/resources/attribution.js.map +1 -0
  31. package/dist/resources/auth.cjs +81 -15
  32. package/dist/resources/auth.cjs.map +1 -1
  33. package/dist/resources/auth.d.cts +53 -6
  34. package/dist/resources/auth.d.ts +53 -6
  35. package/dist/resources/auth.js +81 -15
  36. package/dist/resources/auth.js.map +1 -1
  37. package/dist/resources/balance.d.cts +1 -1
  38. package/dist/resources/balance.d.ts +1 -1
  39. package/dist/resources/blog.cjs.map +1 -1
  40. package/dist/resources/blog.d.cts +8 -1
  41. package/dist/resources/blog.d.ts +8 -1
  42. package/dist/resources/blog.js.map +1 -1
  43. package/dist/resources/bonus.cjs +28 -0
  44. package/dist/resources/bonus.cjs.map +1 -0
  45. package/dist/resources/bonus.d.cts +121 -0
  46. package/dist/resources/bonus.d.ts +121 -0
  47. package/dist/resources/bonus.js +26 -0
  48. package/dist/resources/bonus.js.map +1 -0
  49. package/dist/resources/branding.d.cts +1 -1
  50. package/dist/resources/branding.d.ts +1 -1
  51. package/dist/resources/cards.cjs +48 -10
  52. package/dist/resources/cards.cjs.map +1 -1
  53. package/dist/resources/cards.d.cts +2 -347
  54. package/dist/resources/cards.d.ts +2 -347
  55. package/dist/resources/cards.js +48 -11
  56. package/dist/resources/cards.js.map +1 -1
  57. package/dist/resources/codes.cjs +20 -0
  58. package/dist/resources/codes.cjs.map +1 -0
  59. package/dist/resources/codes.d.cts +100 -0
  60. package/dist/resources/codes.d.ts +100 -0
  61. package/dist/resources/codes.js +18 -0
  62. package/dist/resources/codes.js.map +1 -0
  63. package/dist/resources/deposits.cjs +28 -3
  64. package/dist/resources/deposits.cjs.map +1 -1
  65. package/dist/resources/deposits.d.cts +3 -176
  66. package/dist/resources/deposits.d.ts +3 -176
  67. package/dist/resources/deposits.js +28 -3
  68. package/dist/resources/deposits.js.map +1 -1
  69. package/dist/resources/devices.d.cts +2 -2
  70. package/dist/resources/devices.d.ts +2 -2
  71. package/dist/resources/documents.cjs +64 -0
  72. package/dist/resources/documents.cjs.map +1 -0
  73. package/dist/resources/documents.d.cts +212 -0
  74. package/dist/resources/documents.d.ts +212 -0
  75. package/dist/resources/documents.js +62 -0
  76. package/dist/resources/documents.js.map +1 -0
  77. package/dist/resources/funding-options.cjs +16 -0
  78. package/dist/resources/funding-options.cjs.map +1 -0
  79. package/dist/resources/funding-options.d.cts +3 -0
  80. package/dist/resources/funding-options.d.ts +3 -0
  81. package/dist/resources/funding-options.js +14 -0
  82. package/dist/resources/funding-options.js.map +1 -0
  83. package/dist/resources/history.cjs +31 -0
  84. package/dist/resources/history.cjs.map +1 -0
  85. package/dist/resources/history.d.cts +95 -0
  86. package/dist/resources/history.d.ts +95 -0
  87. package/dist/resources/history.js +29 -0
  88. package/dist/resources/history.js.map +1 -0
  89. package/dist/resources/me.d.cts +3 -1
  90. package/dist/resources/me.d.ts +3 -1
  91. package/dist/resources/notifications.d.cts +1 -1
  92. package/dist/resources/notifications.d.ts +1 -1
  93. package/dist/resources/push.d.cts +1 -1
  94. package/dist/resources/push.d.ts +1 -1
  95. package/dist/resources/referral.cjs +16 -0
  96. package/dist/resources/referral.cjs.map +1 -0
  97. package/dist/resources/referral.d.cts +58 -0
  98. package/dist/resources/referral.d.ts +58 -0
  99. package/dist/resources/referral.js +14 -0
  100. package/dist/resources/referral.js.map +1 -0
  101. package/dist/resources/sessions.d.cts +1 -1
  102. package/dist/resources/sessions.d.ts +1 -1
  103. package/dist/resources/subscription.cjs +24 -0
  104. package/dist/resources/subscription.cjs.map +1 -0
  105. package/dist/resources/subscription.d.cts +48 -0
  106. package/dist/resources/subscription.d.ts +48 -0
  107. package/dist/resources/subscription.js +22 -0
  108. package/dist/resources/subscription.js.map +1 -0
  109. package/dist/resources/subscriptions.d.cts +1 -1
  110. package/dist/resources/subscriptions.d.ts +1 -1
  111. package/dist/resources/wallet.d.cts +1 -1
  112. package/dist/resources/wallet.d.ts +1 -1
  113. package/dist/telegram.cjs +474 -77
  114. package/dist/telegram.cjs.map +1 -1
  115. package/dist/telegram.d.cts +12 -5
  116. package/dist/telegram.d.ts +12 -5
  117. package/dist/telegram.js +474 -77
  118. package/dist/telegram.js.map +1 -1
  119. package/package.json +46 -1
  120. package/dist/customer-CoxPwe5o.d.cts +0 -32
  121. package/dist/customer-CoxPwe5o.d.ts +0 -32
package/dist/telegram.js CHANGED
@@ -39,6 +39,20 @@ var HttpClient = class {
39
39
  this.defaultHeaders = defaultHeaders;
40
40
  if (!baseUrl) throw new Error("HttpClient: baseUrl is required");
41
41
  }
42
+ /** Called once on a 401; resolves `true` when a new session was stored. */
43
+ setRefresher(fn) {
44
+ this.refresher = fn;
45
+ }
46
+ /** One refresh at a time, whoever asks. */
47
+ refreshOnce() {
48
+ if (!this.refresher) return Promise.resolve(false);
49
+ if (!this.refreshing) {
50
+ this.refreshing = this.refresher().catch(() => false).finally(() => {
51
+ this.refreshing = void 0;
52
+ });
53
+ }
54
+ return this.refreshing;
55
+ }
42
56
  async get(path, opts = {}) {
43
57
  return this.request("GET", path, opts);
44
58
  }
@@ -84,6 +98,11 @@ var HttpClient = class {
84
98
  e
85
99
  );
86
100
  }
101
+ if (resp.status === 401 && !opts.skipAuth && !opts.retried && this.refresher) {
102
+ if (await this.refreshOnce()) {
103
+ return this.request(method, path, { ...opts, retried: true });
104
+ }
105
+ }
87
106
  return parseResponse(resp);
88
107
  }
89
108
  absolute(path) {
@@ -117,20 +136,73 @@ async function parseResponse(resp) {
117
136
  }
118
137
 
119
138
  // src/resources/auth.ts
139
+ function withRemember(input) {
140
+ const { remember, ...rest } = input;
141
+ return remember ? { ...rest, remember_me: true } : rest;
142
+ }
120
143
  var AuthResource = class {
121
- constructor(http, tokens) {
144
+ constructor(http, tokens, remembered) {
122
145
  this.http = http;
123
146
  this.tokens = tokens;
147
+ this.remembered = remembered;
148
+ if (remembered) http.setRefresher(() => this.renew().then((r) => r.kind !== "gone"));
149
+ }
150
+ /** Store what a finished sign-in returned. */
151
+ async keep(resp) {
152
+ await Promise.resolve(this.tokens.set(resp.access_token));
153
+ if (resp.remember_token && this.remembered) {
154
+ await Promise.resolve(this.remembered.set(resp.remember_token));
155
+ }
156
+ }
157
+ /** Renew a remembered sign-in: a new session and a new remember token,
158
+ * both stored. Resolves `null` (and forgets the remember token) when there
159
+ * is none or it no longer works; the app then shows the sign-in form.
160
+ * The SDK does this by itself on a 401; call it on app start to resume a
161
+ * session without a request failing first. */
162
+ async refresh() {
163
+ const r = await this.renew();
164
+ if (r.kind === "renewed") return r.resp;
165
+ if (r.kind === "taken_over") {
166
+ const access = await Promise.resolve(this.tokens.get());
167
+ const user = await this.me();
168
+ return { stage: "jwt", access_token: access ?? "", expires_at: "", user };
169
+ }
170
+ return null;
171
+ }
172
+ /** The renewal itself, for the 401 path: it must not call the API again
173
+ * through the client, or a second 401 would wait on this very renewal. */
174
+ async renew() {
175
+ const token = this.remembered ? await Promise.resolve(this.remembered.get()) : null;
176
+ if (!token) return { kind: "gone" };
177
+ try {
178
+ const resp = await this.http.post("/auth/refresh", {
179
+ body: { remember_token: token },
180
+ skipAuth: true
181
+ });
182
+ await this.keep(resp);
183
+ return { kind: "renewed", resp };
184
+ } catch (e) {
185
+ if (isApiError(e) && e.status === 401) {
186
+ const now = await Promise.resolve(this.remembered.get());
187
+ if (now && now !== token && await Promise.resolve(this.tokens.get())) {
188
+ return { kind: "taken_over" };
189
+ }
190
+ await Promise.resolve(this.remembered.clear());
191
+ await Promise.resolve(this.tokens.clear());
192
+ return { kind: "gone" };
193
+ }
194
+ throw e;
195
+ }
124
196
  }
125
197
  /** Password-auth entry. May resolve to a JWT, or to a step-up
126
198
  * challenge that needs `verifyTotp` / `verifyOtp` next. */
127
199
  async login(input) {
128
200
  const resp = await this.http.post("/auth/login", {
129
- body: input,
201
+ body: withRemember(input),
130
202
  skipAuth: true
131
203
  });
132
204
  if (resp.stage === "jwt") {
133
- await Promise.resolve(this.tokens.set(resp.access_token));
205
+ await this.keep(resp);
134
206
  }
135
207
  return resp;
136
208
  }
@@ -138,20 +210,20 @@ var AuthResource = class {
138
210
  * `totp_required` challenge. On success the JWT is stored. */
139
211
  async verifyTotp(input) {
140
212
  const resp = await this.http.post("/auth/login/totp", {
141
- body: input,
213
+ body: withRemember(input),
142
214
  skipAuth: true
143
215
  });
144
- await Promise.resolve(this.tokens.set(resp.access_token));
216
+ await this.keep(resp);
145
217
  return resp;
146
218
  }
147
219
  /** Verify a 6-digit OTP code (Telegram / email) against an
148
220
  * `otp_sent` challenge. */
149
221
  async verifyOtp(input) {
150
222
  const resp = await this.http.post("/auth/login/otp/verify", {
151
- body: input,
223
+ body: withRemember(input),
152
224
  skipAuth: true
153
225
  });
154
- await Promise.resolve(this.tokens.set(resp.access_token));
226
+ await this.keep(resp);
155
227
  return resp;
156
228
  }
157
229
  /** Create an end-user account and sign it in. The JWT is stored.
@@ -161,10 +233,10 @@ var AuthResource = class {
161
233
  * Errors: 400 `validation` / `currency_not_allowed`, 409 email taken. */
162
234
  async register(input) {
163
235
  const resp = await this.http.post("/auth/register", {
164
- body: input,
236
+ body: withRemember(input),
165
237
  skipAuth: true
166
238
  });
167
- await Promise.resolve(this.tokens.set(resp.access_token));
239
+ await this.keep(resp);
168
240
  return resp;
169
241
  }
170
242
  /** Finish a `totp_setup_required` login: send the first code from the
@@ -173,10 +245,10 @@ var AuthResource = class {
173
245
  * the user right away and don't keep them on the device. */
174
246
  async completeTotpSetup(input) {
175
247
  const resp = await this.http.post("/auth/login/totp-setup", {
176
- body: input,
248
+ body: withRemember(input),
177
249
  skipAuth: true
178
250
  });
179
- await Promise.resolve(this.tokens.set(resp.access_token));
251
+ await this.keep(resp);
180
252
  return resp;
181
253
  }
182
254
  /** Change the signed-in user's password. Every other session of the
@@ -197,8 +269,12 @@ var AuthResource = class {
197
269
  });
198
270
  }
199
271
  async exchangeTelegram(input) {
200
- const body = { init_data: input.initData };
272
+ const body = {
273
+ init_data: input.initData
274
+ };
201
275
  if (input.provision !== void 0) body.provision = input.provision;
276
+ if (input.attribution !== void 0) body.attribution = input.attribution;
277
+ if (input.code !== void 0) body.code = input.code;
202
278
  let resp;
203
279
  try {
204
280
  resp = await this.http.post("/auth/telegram/exchange", {
@@ -244,62 +320,44 @@ var AuthResource = class {
244
320
  async me() {
245
321
  return this.http.get("/auth/me");
246
322
  }
247
- /** Local sign-out: forgets the stored JWT. To end the session on the
248
- * server as well, revoke it first with `client.sessions.revoke(jti)`
249
- * (the current one is marked `current: true` in `client.sessions.list()`). */
323
+ /** Sign out: ends this session and the remembered sign-in on the
324
+ * server (best effort: a network failure still signs out locally), then
325
+ * forgets both tokens. */
250
326
  async signOut() {
327
+ const remember = this.remembered ? await Promise.resolve(this.remembered.get()) : null;
328
+ try {
329
+ await this.http.post("/auth/logout", {
330
+ body: remember ? { remember_token: remember } : {},
331
+ retried: true
332
+ });
333
+ } catch {
334
+ }
251
335
  await Promise.resolve(this.tokens.clear());
252
- }
253
- };
254
-
255
- // src/resources/deposits.ts
256
- var DepositsResource = class {
257
- constructor(http) {
258
- this.http = http;
259
- }
260
- /**
261
- * Initiate a top-up. Resolves to the freshly-created `Deposit`, whose
262
- * `status` may already be `confirmed` (synchronous rails / mock) or
263
- * `redirect_required` (3DS / APM — forward the user to `redirect_url`).
264
- */
265
- /**
266
- * Price a cross-currency deposit BEFORE paying. The rate is locked
267
- * server-side until `expires_at`; deposits in the local currency need no
268
- * quote (the API answers 400 `no_quote_needed`).
269
- */
270
- async quote(input) {
271
- return this.http.post("/me/deposits/quotes", { body: input });
272
- }
273
- async initiate(input) {
274
- const { idempotencyKey, ...body } = input;
275
- return this.http.post("/me/deposits", {
276
- body,
277
- idempotencyKey
278
- });
279
- }
280
- /** Fetch one deposit by id — the poll target while a redirect resolves. */
281
- async get(id) {
282
- return this.http.get(`/me/deposits/${encodeURIComponent(id)}`);
283
- }
284
- /** List the caller's deposits, most recent first. */
285
- async list() {
286
- return this.http.get("/me/deposits");
287
- }
288
- /**
289
- * Refund a previously-confirmed deposit. Synchronous when the
290
- * connector settles inline; otherwise the row stays `pending` and flips
291
- * later via the inbound `deposit.refunded` event.
292
- */
293
- async refund(depositId, input) {
294
- const { idempotencyKey, ...body } = input;
295
- return this.http.post(
296
- `/me/deposits/${encodeURIComponent(depositId)}/refunds`,
297
- { body, idempotencyKey }
298
- );
336
+ if (this.remembered) await Promise.resolve(this.remembered.clear());
299
337
  }
300
338
  };
301
339
 
302
340
  // src/resources/cards.ts
341
+ function cardRequestBody(input) {
342
+ const {
343
+ productId,
344
+ name,
345
+ designId,
346
+ useBonus,
347
+ bonusAmount,
348
+ promoCode,
349
+ initial_amount: _ignored,
350
+ ...rest
351
+ } = input;
352
+ const body = { ...rest };
353
+ if (productId !== void 0) body["product_id"] = productId;
354
+ if (name !== void 0) body["name"] = name;
355
+ if (designId !== void 0) body["design_id"] = designId;
356
+ if (useBonus !== void 0) body["use_bonus"] = useBonus;
357
+ if (bonusAmount !== void 0) body["bonus_amount"] = bonusAmount;
358
+ if (promoCode !== void 0) body["promo_code"] = promoCode;
359
+ return body;
360
+ }
303
361
  var CardsResource = class {
304
362
  constructor(http) {
305
363
  this.http = http;
@@ -311,21 +369,19 @@ var CardsResource = class {
311
369
  * `failure_reason` on the result.
312
370
  */
313
371
  async create(input) {
314
- const { idempotencyKey, productId, name, designId, ...rest } = input;
315
- const body = { ...rest };
316
- if (productId !== void 0) body["product_id"] = productId;
317
- if (name !== void 0) body["name"] = name;
318
- if (designId !== void 0) body["design_id"] = designId;
372
+ const { idempotencyKey, ...card } = input;
319
373
  return this.http.post("/me/cards", {
320
- body,
374
+ body: cardRequestBody(card),
321
375
  idempotencyKey
322
376
  });
323
377
  }
324
378
  /**
325
- * The cards the caller can order, in the order the deployment offers them
326
- * (the default product first). Each item carries its name, description,
327
- * artwork, the issuance price after the caller's subscription discount,
328
- * and the currency. Pass an item's `id` as `productId` to {@link create}.
379
+ * The cards the caller can order, in the order the storefront shows them
380
+ * (`is_default` marks the one issued when none is chosen). Each item
381
+ * carries its product card (name, tagline, benefits, tile picture),
382
+ * artwork, and the issuance price after the caller's subscription
383
+ * discount, in the caller's wallet currency. Pass an item's `id` as
384
+ * `productId` to {@link create}.
329
385
  */
330
386
  async listProducts() {
331
387
  return this.http.get("/me/card-products");
@@ -402,6 +458,25 @@ var CardsResource = class {
402
458
  idempotencyKey
403
459
  });
404
460
  }
461
+ /**
462
+ * Move money from one of the caller's cards to another. Both must be
463
+ * theirs, active, in the same currency and of the same kind. The money
464
+ * travels through the wallet one leg at a time: when the second leg does
465
+ * not go through, the call still resolves, `transfer.status` is
466
+ * `on_wallet` and the money is on the wallet — never between two cards.
467
+ * Errors carry a sentence the user can read: 400 the same card twice,
468
+ * different currencies or different kinds · 402 the card holds less than
469
+ * that · 404 one of the cards isn't theirs · 409 a card is frozen, closed
470
+ * or still being issued, or the key was reused with a different body ·
471
+ * 501/502/503 the provider cannot or will not move it.
472
+ */
473
+ async transfer(fromCardId, input) {
474
+ const { toCardId, amount, idempotencyKey } = input;
475
+ return this.http.post(
476
+ `/me/cards/${encodeURIComponent(fromCardId)}/transfer`,
477
+ { body: { to_card_id: toCardId, amount }, idempotencyKey }
478
+ );
479
+ }
405
480
  /**
406
481
  * Freeze the caller's own card. Reversible via {@link unfreeze}. The optional
407
482
  * `reason` is recorded on the audit trail. Resolves to the updated card.
@@ -449,6 +524,56 @@ var CardsResource = class {
449
524
  }
450
525
  };
451
526
 
527
+ // src/resources/deposits.ts
528
+ var DepositsResource = class {
529
+ constructor(http) {
530
+ this.http = http;
531
+ }
532
+ /**
533
+ * Initiate a top-up. Resolves to the freshly-created `Deposit`, whose
534
+ * `status` may already be `confirmed` (synchronous rails / mock) or
535
+ * `redirect_required` (3DS / APM — forward the user to `redirect_url`).
536
+ */
537
+ /**
538
+ * Price a cross-currency deposit BEFORE paying. The rate is locked
539
+ * server-side until `expires_at`; deposits already in the currency they
540
+ * credit (the wallet's, or the card's with `card_id`) need no quote (the
541
+ * API answers 400 `no_quote_needed`).
542
+ */
543
+ async quote(input) {
544
+ return this.http.post("/me/deposits/quotes", { body: input });
545
+ }
546
+ async initiate(input) {
547
+ const { idempotencyKey, issue_card, ...rest } = input;
548
+ const body = { ...rest };
549
+ if (issue_card !== void 0) body["issue_card"] = cardRequestBody(issue_card);
550
+ return this.http.post("/me/deposits", {
551
+ body,
552
+ idempotencyKey
553
+ });
554
+ }
555
+ /** Fetch one deposit by id — the poll target while a redirect resolves. */
556
+ async get(id) {
557
+ return this.http.get(`/me/deposits/${encodeURIComponent(id)}`);
558
+ }
559
+ /** List the caller's deposits, most recent first. */
560
+ async list() {
561
+ return this.http.get("/me/deposits");
562
+ }
563
+ /**
564
+ * Refund a previously-confirmed deposit. Synchronous when the
565
+ * connector settles inline; otherwise the row stays `pending` and flips
566
+ * later via the inbound `deposit.refunded` event.
567
+ */
568
+ async refund(depositId, input) {
569
+ const { idempotencyKey, ...body } = input;
570
+ return this.http.post(
571
+ `/me/deposits/${encodeURIComponent(depositId)}/refunds`,
572
+ { body, idempotencyKey }
573
+ );
574
+ }
575
+ };
576
+
452
577
  // src/resources/balance.ts
453
578
  var BalanceResource = class {
454
579
  constructor(http) {
@@ -466,6 +591,74 @@ var BalanceResource = class {
466
591
  }
467
592
  };
468
593
 
594
+ // src/resources/bonus.ts
595
+ var BonusResource = class {
596
+ constructor(http) {
597
+ this.http = http;
598
+ }
599
+ /** The user's bonuses: balance, where they can go, credits and history. */
600
+ async get(opts = {}) {
601
+ return this.http.get("/me/bonus", { signal: opts.signal });
602
+ }
603
+ /**
604
+ * Work out how bonuses would change a price, at the rate the payment
605
+ * would use. Nothing is held; the numbers may move by the time the user
606
+ * pays if prices, rates or bonuses change.
607
+ */
608
+ async preview(input) {
609
+ const { productId, bonusAmount, ...rest } = input;
610
+ const body = { ...rest };
611
+ if (productId !== void 0) body["product_id"] = productId;
612
+ if (bonusAmount !== void 0) body["bonus_amount"] = bonusAmount;
613
+ return this.http.post("/me/bonus/preview", { body });
614
+ }
615
+ };
616
+
617
+ // src/resources/codes.ts
618
+ var CodesResource = class {
619
+ constructor(http) {
620
+ this.http = http;
621
+ }
622
+ /**
623
+ * Whether a code can be used on an operation now and what it would do.
624
+ * Nothing is used up. For sign-up there is nothing to check here: send
625
+ * the code with `auth.register` and read `code_result`.
626
+ */
627
+ async validate(input) {
628
+ return this.http.post("/me/codes/validate", { body: input });
629
+ }
630
+ };
631
+
632
+ // src/resources/referral.ts
633
+ var ReferralResource = class {
634
+ constructor(http) {
635
+ this.http = http;
636
+ }
637
+ /** The user's referral code, link, invited friends and rewards. */
638
+ async get(opts = {}) {
639
+ return this.http.get("/me/referral", { signal: opts.signal });
640
+ }
641
+ };
642
+
643
+ // src/resources/subscription.ts
644
+ var SubscriptionResource = class {
645
+ constructor(http) {
646
+ this.http = http;
647
+ }
648
+ /** The user's subscription, or `null` when there is none. */
649
+ async get(opts = {}) {
650
+ return this.http.get("/me/subscription", { signal: opts.signal });
651
+ }
652
+ /**
653
+ * Charge the monthly fee with a promo code, as long as the code allows.
654
+ * `null` removes it. A code that can't be used on the fee fails with 400
655
+ * `promo_code_*`; no paid subscription running → 404.
656
+ */
657
+ async setPromoCode(code) {
658
+ return this.http.put("/me/subscription/promo-code", { body: { code } });
659
+ }
660
+ };
661
+
469
662
  // src/resources/branding.ts
470
663
  var BrandingResource = class {
471
664
  constructor(http) {
@@ -530,6 +723,65 @@ var BlogResource = class {
530
723
  }
531
724
  };
532
725
 
726
+ // src/resources/documents.ts
727
+ var DocumentsResource = class {
728
+ constructor(http) {
729
+ this.http = http;
730
+ }
731
+ /**
732
+ * Published documents you may read, paged. The blog comes newest first,
733
+ * other categories in the order set by the editors.
734
+ */
735
+ async list(input = {}) {
736
+ const q = new URLSearchParams();
737
+ if (input.category) q.set("category", input.category);
738
+ if (input.folderId) q.set("folder_id", input.folderId);
739
+ if (input.tag) q.set("tag", input.tag);
740
+ if (input.locale) q.set("locale", input.locale);
741
+ if (input.q) q.set("q", input.q);
742
+ if (input.page) q.set("page", String(input.page));
743
+ if (input.perPage) q.set("per_page", String(input.perPage));
744
+ const qs = q.toString();
745
+ return this.http.get(`/documents${qs ? `?${qs}` : ""}`);
746
+ }
747
+ /** Folders and documents for a sidebar or a table of contents. */
748
+ async tree(input = {}) {
749
+ const qs = input.category ? `?category=${encodeURIComponent(input.category)}` : "";
750
+ return this.http.get(`/documents/tree${qs}`);
751
+ }
752
+ /** Every tag on a document you may read, most used first. */
753
+ async tags() {
754
+ return this.http.get("/documents/tags");
755
+ }
756
+ /**
757
+ * One document by its address. Without `at`, the text in force now; with
758
+ * `at` (an ISO 8601 moment), the text that was in force then. Unknown
759
+ * addresses and documents above your level are a 404.
760
+ */
761
+ async get(slug, opts = {}) {
762
+ const at = opts.at instanceof Date ? opts.at.toISOString() : opts.at;
763
+ const qs = at ? `?at=${encodeURIComponent(at)}` : "";
764
+ return this.http.get(`/documents/${encodeURIComponent(slug)}${qs}`);
765
+ }
766
+ /** Every saved text of a document, newest first, with the date each took effect. */
767
+ async versions(slug) {
768
+ return this.http.get(`/documents/${encodeURIComponent(slug)}/versions`);
769
+ }
770
+ /** The document as it read in one version. */
771
+ async version(slug, version) {
772
+ return this.http.get(`/documents/${encodeURIComponent(slug)}/versions/${version}`);
773
+ }
774
+ /**
775
+ * Absolute URL for an image path returned inside a document (`cover_url`,
776
+ * or an image block's `url`). Handy when the app renders on a different
777
+ * origin than the API.
778
+ */
779
+ imageUrl(baseUrl, path) {
780
+ if (/^https?:\/\//i.test(path)) return path;
781
+ return `${baseUrl.replace(/\/$/, "")}${path.startsWith("/") ? "" : "/"}${path}`;
782
+ }
783
+ };
784
+
533
785
  // src/resources/subscriptions.ts
534
786
  var SubscriptionsResource = class {
535
787
  constructor(http) {
@@ -780,8 +1032,68 @@ var DevicesResource = class {
780
1032
  }
781
1033
  };
782
1034
 
1035
+ // src/resources/attribution.ts
1036
+ var AttributionResource = class {
1037
+ constructor(http) {
1038
+ this.http = http;
1039
+ }
1040
+ /** Record a visit. No session needed; safe to call on every page load. */
1041
+ async touch(input) {
1042
+ return this.http.post("/attribution/touches", { body: input, skipAuth: true });
1043
+ }
1044
+ /** Whether to ask the signed-in user how they heard about us. */
1045
+ async status() {
1046
+ return this.http.get("/me/attribution");
1047
+ }
1048
+ /** Answer the question once. Send `"skipped"` when the user closes it.
1049
+ * `otherText` is kept only with `"other"`, up to 500 characters.
1050
+ * Errors: 400 `validation`, 409 `survey_answered`. */
1051
+ async answerSurvey(answer, otherText) {
1052
+ const body = { answer };
1053
+ if (otherText !== void 0) body.other_text = otherText;
1054
+ return this.http.put("/me/attribution/survey", { body });
1055
+ }
1056
+ };
1057
+
1058
+ // src/resources/history.ts
1059
+ var HistoryResource = class {
1060
+ constructor(http) {
1061
+ this.http = http;
1062
+ }
1063
+ /** One page of the history. Errors: 400 bad cursor or limit, 404 `cardId` is not the user's card. */
1064
+ async list(params = {}) {
1065
+ const q = new URLSearchParams();
1066
+ if (params.cardId) q.set("card_id", params.cardId);
1067
+ if (params.cursor) q.set("cursor", params.cursor);
1068
+ if (params.limit !== void 0) q.set("limit", String(params.limit));
1069
+ if (params.lang) q.set("lang", params.lang);
1070
+ const qs = q.toString();
1071
+ return this.http.get(`/me/history${qs ? `?${qs}` : ""}`, { signal: params.signal });
1072
+ }
1073
+ /** Every entry, page after page, newest first. */
1074
+ async *all(params = {}) {
1075
+ let cursor;
1076
+ do {
1077
+ const page = await this.list({ ...params, cursor });
1078
+ yield* page.items;
1079
+ cursor = page.next_cursor ?? void 0;
1080
+ } while (cursor);
1081
+ }
1082
+ };
1083
+
1084
+ // src/resources/funding-options.ts
1085
+ var FundingOptionsResource = class {
1086
+ constructor(http) {
1087
+ this.http = http;
1088
+ }
1089
+ /** The ways to top up, preferred first. Empty when none is configured. */
1090
+ async list() {
1091
+ return this.http.get("/me/funding-options");
1092
+ }
1093
+ };
1094
+
783
1095
  // src/version.ts
784
- var SDK_VERSION = "0.3.20";
1096
+ var SDK_VERSION = "0.5.0";
785
1097
 
786
1098
  // src/client-platform.ts
787
1099
  var CLIENT_HEADER = "X-Takeal-Client";
@@ -855,9 +1167,85 @@ function isAbsolute(url) {
855
1167
  return /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(url);
856
1168
  }
857
1169
 
1170
+ // src/compat.ts
1171
+ var SUPPORTED_MEISTER = ">=0.5.0, <0.6.0";
1172
+ var SEMVER = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
1173
+ function parse(v) {
1174
+ const m = SEMVER.exec(v.trim());
1175
+ if (!m) return null;
1176
+ return { core: [Number(m[1]), Number(m[2]), Number(m[3])], pre: m[4] ?? null };
1177
+ }
1178
+ function cmpCore(a, b) {
1179
+ for (let i = 0; i < 3; i++) {
1180
+ if (a.core[i] !== b.core[i]) return a.core[i] < b.core[i] ? -1 : 1;
1181
+ }
1182
+ return 0;
1183
+ }
1184
+ function satisfies(version, range) {
1185
+ const v = parse(version);
1186
+ if (!v) return false;
1187
+ const parts = range.split(",").map((p) => p.trim()).filter(Boolean);
1188
+ if (parts.length === 0) return false;
1189
+ let exactPre = false;
1190
+ for (const part of parts) {
1191
+ const m = /^(>=|<=|>|<|=)?\s*(.+)$/.exec(part);
1192
+ if (!m) return false;
1193
+ const op = m[1] ?? "=";
1194
+ const target = parse(m[2]);
1195
+ if (!target) return false;
1196
+ const c = cmpCore(v, target);
1197
+ if (op === "=") {
1198
+ if (c !== 0 || v.pre !== target.pre) return false;
1199
+ if (v.pre !== null) exactPre = true;
1200
+ continue;
1201
+ }
1202
+ const ok = op === ">=" ? c >= 0 : op === ">" ? c > 0 : op === "<=" ? c <= 0 : c < 0;
1203
+ if (!ok) return false;
1204
+ }
1205
+ return v.pre === null || exactPre;
1206
+ }
1207
+ function readyInfo(raw, healthy) {
1208
+ const reported = raw?.ops?.platform_version;
1209
+ const platformVersion = typeof reported === "string" && reported ? reported : null;
1210
+ const base = {
1211
+ platformVersion,
1212
+ supportedMeister: SUPPORTED_MEISTER,
1213
+ healthy,
1214
+ status: typeof raw?.status === "string" ? raw.status : healthy ? "ready" : "degraded",
1215
+ raw
1216
+ };
1217
+ if (platformVersion === null) {
1218
+ return {
1219
+ ...base,
1220
+ compatible: false,
1221
+ warning: `The platform didn't report its version; this SDK supports ${SUPPORTED_MEISTER}.`
1222
+ };
1223
+ }
1224
+ if (!satisfies(platformVersion, SUPPORTED_MEISTER)) {
1225
+ return {
1226
+ ...base,
1227
+ compatible: false,
1228
+ warning: `This SDK supports platform ${SUPPORTED_MEISTER}, the platform runs ${platformVersion}. Some calls may fail until the SDK is updated.`
1229
+ };
1230
+ }
1231
+ return { ...base, compatible: true };
1232
+ }
1233
+ async function fetchReady(http, opts = {}) {
1234
+ try {
1235
+ const body = await http.get("/ready", { skipAuth: true, signal: opts.signal });
1236
+ return readyInfo(body ?? {}, true);
1237
+ } catch (e) {
1238
+ if (isApiError(e) && e.status === 503 && typeof e.body === "object" && e.body !== null) {
1239
+ return readyInfo(e.body, false);
1240
+ }
1241
+ throw e;
1242
+ }
1243
+ }
1244
+
858
1245
  // src/index.ts
859
1246
  function createClient(opts) {
860
1247
  const tokens = opts.tokenStore ?? pickDefaultStore();
1248
+ const remembered = opts.rememberStore ?? pickDefaultStore("takeal_remember");
861
1249
  const fetchImpl = opts.fetch ?? defaultFetch();
862
1250
  const client = resolveClientDescriptor(opts.client);
863
1251
  const http = new HttpClient(opts.baseUrl, tokens, fetchImpl, {
@@ -868,24 +1256,33 @@ function createClient(opts) {
868
1256
  client,
869
1257
  tokens,
870
1258
  assetUrl: (url) => resolveAssetUrl(opts.baseUrl, url),
871
- auth: new AuthResource(http, tokens),
1259
+ ready: (o) => fetchReady(http, o),
1260
+ auth: new AuthResource(http, tokens, remembered),
872
1261
  deposits: new DepositsResource(http),
1262
+ fundingOptions: new FundingOptionsResource(http),
873
1263
  cards: new CardsResource(http),
874
1264
  balance: new BalanceResource(http),
1265
+ bonus: new BonusResource(http),
1266
+ codes: new CodesResource(http),
1267
+ referral: new ReferralResource(http),
1268
+ subscription: new SubscriptionResource(http),
875
1269
  wallet: new WalletResource(http),
876
1270
  branding: new BrandingResource(http),
1271
+ documents: new DocumentsResource(http),
877
1272
  blog: new BlogResource(http),
878
1273
  subscriptions: new SubscriptionsResource(http),
879
1274
  sessions: new SessionsResource(http),
880
1275
  push: new PushResource(http),
881
1276
  me: new MeResource(http),
882
1277
  notifications: new NotificationsResource(http),
883
- devices: new DevicesResource(http, client)
1278
+ devices: new DevicesResource(http, client),
1279
+ attribution: new AttributionResource(http),
1280
+ history: new HistoryResource(http)
884
1281
  };
885
1282
  }
886
- function pickDefaultStore() {
1283
+ function pickDefaultStore(key) {
887
1284
  if (typeof globalThis !== "undefined" && "localStorage" in globalThis) {
888
- return defaultBrowserStore();
1285
+ return defaultBrowserStore(key);
889
1286
  }
890
1287
  return inMemoryStore();
891
1288
  }