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