@takeal/cusfront-sdk 0.6.1 → 0.6.3
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.
- package/CHANGELOG.md +43 -0
- package/README.md +87 -0
- package/dist/{cards-CF3-He2x.d.ts → cards-BrqeBqLV.d.ts} +442 -1
- package/dist/{cards-qfHz6xZY.d.cts → cards-ofoGRU9v.d.cts} +442 -1
- package/dist/index.cjs +329 -205
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -8
- package/dist/index.d.ts +14 -8
- package/dist/index.js +329 -205
- package/dist/index.js.map +1 -1
- package/dist/react/index.d.cts +3 -2
- package/dist/react/index.d.ts +3 -2
- package/dist/resources/auth.cjs +48 -5
- package/dist/resources/auth.cjs.map +1 -1
- package/dist/resources/auth.d.cts +53 -7
- package/dist/resources/auth.d.ts +53 -7
- package/dist/resources/auth.js +48 -5
- package/dist/resources/auth.js.map +1 -1
- package/dist/resources/cards.cjs +36 -0
- package/dist/resources/cards.cjs.map +1 -1
- package/dist/resources/cards.d.cts +2 -1
- package/dist/resources/cards.d.ts +2 -1
- package/dist/resources/cards.js +36 -1
- package/dist/resources/cards.js.map +1 -1
- package/dist/resources/consents.cjs +91 -0
- package/dist/resources/consents.cjs.map +1 -0
- package/dist/resources/consents.d.cts +83 -0
- package/dist/resources/consents.d.ts +83 -0
- package/dist/resources/consents.js +89 -0
- package/dist/resources/consents.js.map +1 -0
- package/dist/resources/deposits.cjs +2 -0
- package/dist/resources/deposits.cjs.map +1 -1
- package/dist/resources/deposits.d.cts +2 -2
- package/dist/resources/deposits.d.ts +2 -2
- package/dist/resources/deposits.js +2 -0
- package/dist/resources/deposits.js.map +1 -1
- package/dist/resources/funding-options.d.cts +2 -2
- package/dist/resources/funding-options.d.ts +2 -2
- package/dist/resources/me.d.cts +2 -0
- package/dist/resources/me.d.ts +2 -0
- package/dist/resources/receipts.cjs +22 -0
- package/dist/resources/receipts.cjs.map +1 -0
- package/dist/resources/receipts.d.cts +51 -0
- package/dist/resources/receipts.d.ts +51 -0
- package/dist/resources/receipts.js +20 -0
- package/dist/resources/receipts.js.map +1 -0
- package/dist/telegram.cjs +367 -205
- package/dist/telegram.cjs.map +1 -1
- package/dist/telegram.d.cts +3 -2
- package/dist/telegram.d.ts +3 -2
- package/dist/telegram.js +367 -205
- package/dist/telegram.js.map +1 -1
- package/package.json +11 -1
- package/dist/deposits-B2LBe2k1.d.ts +0 -373
- package/dist/deposits-ZjT_btgi.d.cts +0 -373
package/dist/telegram.cjs
CHANGED
|
@@ -134,208 +134,6 @@ async function parseResponse(resp) {
|
|
|
134
134
|
return body;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
// src/resources/auth.ts
|
|
138
|
-
function withRemember(input) {
|
|
139
|
-
const { remember, ...rest } = input;
|
|
140
|
-
return remember ? { ...rest, remember_me: true } : rest;
|
|
141
|
-
}
|
|
142
|
-
var AuthResource = class {
|
|
143
|
-
constructor(http, tokens, remembered) {
|
|
144
|
-
this.http = http;
|
|
145
|
-
this.tokens = tokens;
|
|
146
|
-
this.remembered = remembered;
|
|
147
|
-
if (remembered) http.setRefresher(() => this.renew().then((r) => r.kind !== "gone"));
|
|
148
|
-
}
|
|
149
|
-
/** Store what a finished sign-in returned. */
|
|
150
|
-
async keep(resp) {
|
|
151
|
-
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
152
|
-
if (resp.remember_token && this.remembered) {
|
|
153
|
-
await Promise.resolve(this.remembered.set(resp.remember_token));
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
/** Renew a remembered sign-in: a new session and a new remember token,
|
|
157
|
-
* both stored. Resolves `null` (and forgets the remember token) when there
|
|
158
|
-
* is none or it no longer works; the app then shows the sign-in form.
|
|
159
|
-
* The SDK does this by itself on a 401; call it on app start to resume a
|
|
160
|
-
* session without a request failing first. */
|
|
161
|
-
async refresh() {
|
|
162
|
-
const r = await this.renew();
|
|
163
|
-
if (r.kind === "renewed") return r.resp;
|
|
164
|
-
if (r.kind === "taken_over") {
|
|
165
|
-
const access = await Promise.resolve(this.tokens.get());
|
|
166
|
-
const user = await this.me();
|
|
167
|
-
return { stage: "jwt", access_token: access ?? "", expires_at: "", user };
|
|
168
|
-
}
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
/** The renewal itself, for the 401 path: it must not call the API again
|
|
172
|
-
* through the client, or a second 401 would wait on this very renewal. */
|
|
173
|
-
async renew() {
|
|
174
|
-
const token = this.remembered ? await Promise.resolve(this.remembered.get()) : null;
|
|
175
|
-
if (!token) return { kind: "gone" };
|
|
176
|
-
try {
|
|
177
|
-
const resp = await this.http.post("/auth/refresh", {
|
|
178
|
-
body: { remember_token: token },
|
|
179
|
-
skipAuth: true
|
|
180
|
-
});
|
|
181
|
-
await this.keep(resp);
|
|
182
|
-
return { kind: "renewed", resp };
|
|
183
|
-
} catch (e) {
|
|
184
|
-
if (isApiError(e) && e.status === 401) {
|
|
185
|
-
const now = await Promise.resolve(this.remembered.get());
|
|
186
|
-
if (now && now !== token && await Promise.resolve(this.tokens.get())) {
|
|
187
|
-
return { kind: "taken_over" };
|
|
188
|
-
}
|
|
189
|
-
await Promise.resolve(this.remembered.clear());
|
|
190
|
-
await Promise.resolve(this.tokens.clear());
|
|
191
|
-
return { kind: "gone" };
|
|
192
|
-
}
|
|
193
|
-
throw e;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/** Password-auth entry. May resolve to a JWT, or to a step-up
|
|
197
|
-
* challenge that needs `verifyTotp` / `verifyOtp` next. */
|
|
198
|
-
async login(input) {
|
|
199
|
-
const resp = await this.http.post("/auth/login", {
|
|
200
|
-
body: withRemember(input),
|
|
201
|
-
skipAuth: true
|
|
202
|
-
});
|
|
203
|
-
if (resp.stage === "jwt") {
|
|
204
|
-
await this.keep(resp);
|
|
205
|
-
}
|
|
206
|
-
return resp;
|
|
207
|
-
}
|
|
208
|
-
/** Verify a TOTP code (or XXXX-XXXX backup code) against a
|
|
209
|
-
* `totp_required` challenge. On success the JWT is stored. */
|
|
210
|
-
async verifyTotp(input) {
|
|
211
|
-
const resp = await this.http.post("/auth/login/totp", {
|
|
212
|
-
body: withRemember(input),
|
|
213
|
-
skipAuth: true
|
|
214
|
-
});
|
|
215
|
-
await this.keep(resp);
|
|
216
|
-
return resp;
|
|
217
|
-
}
|
|
218
|
-
/** Verify a 6-digit OTP code (Telegram / email) against an
|
|
219
|
-
* `otp_sent` challenge. */
|
|
220
|
-
async verifyOtp(input) {
|
|
221
|
-
const resp = await this.http.post("/auth/login/otp/verify", {
|
|
222
|
-
body: withRemember(input),
|
|
223
|
-
skipAuth: true
|
|
224
|
-
});
|
|
225
|
-
await this.keep(resp);
|
|
226
|
-
return resp;
|
|
227
|
-
}
|
|
228
|
-
/** Create an end-user account and sign it in. The JWT is stored.
|
|
229
|
-
* `wallet_currency` must be one the deployment offers; omit it to take
|
|
230
|
-
* the deployment default (it can be switched later via `client.wallet`
|
|
231
|
-
* while the wallet is empty).
|
|
232
|
-
* Errors: 400 `validation` / `currency_not_allowed`, 409 email taken. */
|
|
233
|
-
async register(input) {
|
|
234
|
-
const resp = await this.http.post("/auth/register", {
|
|
235
|
-
body: withRemember(input),
|
|
236
|
-
skipAuth: true
|
|
237
|
-
});
|
|
238
|
-
await this.keep(resp);
|
|
239
|
-
return resp;
|
|
240
|
-
}
|
|
241
|
-
/** Finish a `totp_setup_required` login: send the first code from the
|
|
242
|
-
* authenticator app. The JWT is stored. The response carries ten
|
|
243
|
-
* one-time `backup_codes` that are never returned again, so show them to
|
|
244
|
-
* the user right away and don't keep them on the device. */
|
|
245
|
-
async completeTotpSetup(input) {
|
|
246
|
-
const resp = await this.http.post("/auth/login/totp-setup", {
|
|
247
|
-
body: withRemember(input),
|
|
248
|
-
skipAuth: true
|
|
249
|
-
});
|
|
250
|
-
await this.keep(resp);
|
|
251
|
-
return resp;
|
|
252
|
-
}
|
|
253
|
-
/** Change the signed-in user's password. Every other session of the
|
|
254
|
-
* account is signed out; this one stays valid.
|
|
255
|
-
* Errors: 400 when the new password fails the policy, 401 when the
|
|
256
|
-
* current one is wrong. */
|
|
257
|
-
async changePassword(input) {
|
|
258
|
-
return this.http.post("/auth/password", {
|
|
259
|
-
body: input
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
/** Swap the 2FA method mid-flow. Resolves to `otp_sent` for
|
|
263
|
-
* telegram/email; for TOTP it short-circuits back to the challenge. */
|
|
264
|
-
async switchMethod(input) {
|
|
265
|
-
return this.http.post("/auth/login/2fa/switch", {
|
|
266
|
-
body: input,
|
|
267
|
-
skipAuth: true
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
async exchangeTelegram(input) {
|
|
271
|
-
const body = {
|
|
272
|
-
init_data: input.initData
|
|
273
|
-
};
|
|
274
|
-
if (input.provision !== void 0) body.provision = input.provision;
|
|
275
|
-
if (input.attribution !== void 0) body.attribution = input.attribution;
|
|
276
|
-
if (input.code !== void 0) body.code = input.code;
|
|
277
|
-
let resp;
|
|
278
|
-
try {
|
|
279
|
-
resp = await this.http.post("/auth/telegram/exchange", {
|
|
280
|
-
body,
|
|
281
|
-
skipAuth: true
|
|
282
|
-
});
|
|
283
|
-
} catch (e) {
|
|
284
|
-
if (isApiError(e) && e.status === 404 && e.code === "telegram_unknown") {
|
|
285
|
-
return { stage: "unknown" };
|
|
286
|
-
}
|
|
287
|
-
throw e;
|
|
288
|
-
}
|
|
289
|
-
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
290
|
-
return resp;
|
|
291
|
-
}
|
|
292
|
-
/** Attach the Telegram account behind `initData` to an existing email +
|
|
293
|
-
* password account and sign in as that account. No bearer needed; the
|
|
294
|
-
* password is checked the same way `login` checks it. The JWT is stored.
|
|
295
|
-
*
|
|
296
|
-
* Errors: 401 `unauthenticated` (wrong email or password, as with
|
|
297
|
-
* `login`) / `invalid_init_data`; 403
|
|
298
|
-
* `user_disabled` / `second_factor_required` (accounts that sign in with
|
|
299
|
-
* a second factor can't be linked from a Mini App); 409
|
|
300
|
-
* `telegram_already_linked_other` (this account already has a different
|
|
301
|
-
* Telegram; unlink it first) / `telegram_in_use` (this Telegram belongs to
|
|
302
|
-
* another account); 503 `telegram_not_configured`. */
|
|
303
|
-
async linkTelegram(input) {
|
|
304
|
-
const resp = await this.http.post("/auth/telegram/link", {
|
|
305
|
-
body: { init_data: input.initData, email: input.email, password: input.password },
|
|
306
|
-
skipAuth: true
|
|
307
|
-
});
|
|
308
|
-
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
309
|
-
return resp;
|
|
310
|
-
}
|
|
311
|
-
/** Attach a real email to the current user (and clear `email_pending`).
|
|
312
|
-
* Used after a Telegram exchange to satisfy the email requirement; also
|
|
313
|
-
* works for a password user changing their address. 409 if taken. */
|
|
314
|
-
async linkEmail(input) {
|
|
315
|
-
return this.http.post("/me/email", { body: { email: input.email } });
|
|
316
|
-
}
|
|
317
|
-
/** Current user — fails 401 when the stored JWT is expired or
|
|
318
|
-
* missing. Useful as a "do I have a session" probe on app boot. */
|
|
319
|
-
async me() {
|
|
320
|
-
return this.http.get("/auth/me");
|
|
321
|
-
}
|
|
322
|
-
/** Sign out: ends this session and the remembered sign-in on the
|
|
323
|
-
* server (best effort: a network failure still signs out locally), then
|
|
324
|
-
* forgets both tokens. */
|
|
325
|
-
async signOut() {
|
|
326
|
-
const remember = this.remembered ? await Promise.resolve(this.remembered.get()) : null;
|
|
327
|
-
try {
|
|
328
|
-
await this.http.post("/auth/logout", {
|
|
329
|
-
body: remember ? { remember_token: remember } : {},
|
|
330
|
-
retried: true
|
|
331
|
-
});
|
|
332
|
-
} catch {
|
|
333
|
-
}
|
|
334
|
-
await Promise.resolve(this.tokens.clear());
|
|
335
|
-
if (this.remembered) await Promise.resolve(this.remembered.clear());
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
|
|
339
137
|
// src/resources/cards.ts
|
|
340
138
|
function cardRequestBody(input) {
|
|
341
139
|
const {
|
|
@@ -345,6 +143,7 @@ function cardRequestBody(input) {
|
|
|
345
143
|
useBonus,
|
|
346
144
|
bonusAmount,
|
|
347
145
|
promoCode,
|
|
146
|
+
consentChannel,
|
|
348
147
|
initial_amount: _ignored,
|
|
349
148
|
...rest
|
|
350
149
|
} = input;
|
|
@@ -355,6 +154,24 @@ function cardRequestBody(input) {
|
|
|
355
154
|
if (useBonus !== void 0) body["use_bonus"] = useBonus;
|
|
356
155
|
if (bonusAmount !== void 0) body["bonus_amount"] = bonusAmount;
|
|
357
156
|
if (promoCode !== void 0) body["promo_code"] = promoCode;
|
|
157
|
+
if (consentChannel !== void 0) body["consent_channel"] = consentChannel;
|
|
158
|
+
return body;
|
|
159
|
+
}
|
|
160
|
+
function cardOrderBody(input) {
|
|
161
|
+
const body = {
|
|
162
|
+
product_id: input.productId,
|
|
163
|
+
method: input.method
|
|
164
|
+
};
|
|
165
|
+
if (input.designId !== void 0) body["design_id"] = input.designId;
|
|
166
|
+
if (input.name !== void 0) body["name"] = input.name;
|
|
167
|
+
if (input.funderSlug !== void 0) body["funder_slug"] = input.funderSlug;
|
|
168
|
+
if (input.currency !== void 0) body["currency"] = input.currency;
|
|
169
|
+
if (input.extraLoad !== void 0) body["extra_load"] = input.extraLoad;
|
|
170
|
+
if (input.promoCode !== void 0) body["promo_code"] = input.promoCode;
|
|
171
|
+
if (input.useBonus !== void 0) body["use_bonus"] = input.useBonus;
|
|
172
|
+
if (input.consents !== void 0) body["consents"] = input.consents;
|
|
173
|
+
if (input.consentChannel !== void 0) body["consent_channel"] = input.consentChannel;
|
|
174
|
+
if (input.metadata !== void 0) body["metadata"] = input.metadata;
|
|
358
175
|
return body;
|
|
359
176
|
}
|
|
360
177
|
var CardsResource = class {
|
|
@@ -374,6 +191,22 @@ var CardsResource = class {
|
|
|
374
191
|
idempotencyKey
|
|
375
192
|
});
|
|
376
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Buy a card in one step: the API prices the product in the payment's
|
|
196
|
+
* currency, locks the rate and opens the top-up that issues the card.
|
|
197
|
+
* Send the user to `order.deposit.redirect_url`; the card shows as
|
|
198
|
+
* `pending_payment` until the payment is confirmed. The same
|
|
199
|
+
* `idempotencyKey` returns the same order.
|
|
200
|
+
* Errors: 400 `unknown_product` / `method_not_available` / a limit of the
|
|
201
|
+
* way of paying, 503 no funding connector or no exchange rate.
|
|
202
|
+
*/
|
|
203
|
+
async order(input) {
|
|
204
|
+
const { idempotencyKey, ...order } = input;
|
|
205
|
+
return this.http.post("/me/card-orders", {
|
|
206
|
+
body: cardOrderBody(order),
|
|
207
|
+
idempotencyKey
|
|
208
|
+
});
|
|
209
|
+
}
|
|
377
210
|
/**
|
|
378
211
|
* The cards the caller can order, in the order the storefront shows them
|
|
379
212
|
* (`is_default` marks the one issued when none is chosen). Each item
|
|
@@ -536,6 +369,232 @@ var CardsResource = class {
|
|
|
536
369
|
}
|
|
537
370
|
};
|
|
538
371
|
|
|
372
|
+
// src/resources/auth.ts
|
|
373
|
+
function withRemember(input) {
|
|
374
|
+
const { remember, ...rest } = input;
|
|
375
|
+
return remember ? { ...rest, remember_me: true } : rest;
|
|
376
|
+
}
|
|
377
|
+
var AuthResource = class {
|
|
378
|
+
constructor(http, tokens, remembered) {
|
|
379
|
+
this.http = http;
|
|
380
|
+
this.tokens = tokens;
|
|
381
|
+
this.remembered = remembered;
|
|
382
|
+
if (remembered) http.setRefresher(() => this.renew().then((r) => r.kind !== "gone"));
|
|
383
|
+
}
|
|
384
|
+
/** Store what a finished sign-in returned. */
|
|
385
|
+
async keep(resp) {
|
|
386
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
387
|
+
if (resp.remember_token && this.remembered) {
|
|
388
|
+
await Promise.resolve(this.remembered.set(resp.remember_token));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/** Renew a remembered sign-in: a new session and a new remember token,
|
|
392
|
+
* both stored. Resolves `null` (and forgets the remember token) when there
|
|
393
|
+
* is none or it no longer works; the app then shows the sign-in form.
|
|
394
|
+
* The SDK does this by itself on a 401; call it on app start to resume a
|
|
395
|
+
* session without a request failing first. */
|
|
396
|
+
async refresh() {
|
|
397
|
+
const r = await this.renew();
|
|
398
|
+
if (r.kind === "renewed") return r.resp;
|
|
399
|
+
if (r.kind === "taken_over") {
|
|
400
|
+
const access = await Promise.resolve(this.tokens.get());
|
|
401
|
+
const user = await this.me();
|
|
402
|
+
return { stage: "jwt", access_token: access ?? "", expires_at: "", user };
|
|
403
|
+
}
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
/** The renewal itself, for the 401 path: it must not call the API again
|
|
407
|
+
* through the client, or a second 401 would wait on this very renewal. */
|
|
408
|
+
async renew() {
|
|
409
|
+
const token = this.remembered ? await Promise.resolve(this.remembered.get()) : null;
|
|
410
|
+
if (!token) return { kind: "gone" };
|
|
411
|
+
try {
|
|
412
|
+
const resp = await this.http.post("/auth/refresh", {
|
|
413
|
+
body: { remember_token: token },
|
|
414
|
+
skipAuth: true
|
|
415
|
+
});
|
|
416
|
+
await this.keep(resp);
|
|
417
|
+
return { kind: "renewed", resp };
|
|
418
|
+
} catch (e) {
|
|
419
|
+
if (isApiError(e) && e.status === 401) {
|
|
420
|
+
const now = await Promise.resolve(this.remembered.get());
|
|
421
|
+
if (now && now !== token && await Promise.resolve(this.tokens.get())) {
|
|
422
|
+
return { kind: "taken_over" };
|
|
423
|
+
}
|
|
424
|
+
await Promise.resolve(this.remembered.clear());
|
|
425
|
+
await Promise.resolve(this.tokens.clear());
|
|
426
|
+
return { kind: "gone" };
|
|
427
|
+
}
|
|
428
|
+
throw e;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
/** Password-auth entry. May resolve to a JWT, or to a step-up
|
|
432
|
+
* challenge that needs `verifyTotp` / `verifyOtp` next. */
|
|
433
|
+
async login(input) {
|
|
434
|
+
const resp = await this.http.post("/auth/login", {
|
|
435
|
+
body: withRemember(input),
|
|
436
|
+
skipAuth: true
|
|
437
|
+
});
|
|
438
|
+
if (resp.stage === "jwt") {
|
|
439
|
+
await this.keep(resp);
|
|
440
|
+
}
|
|
441
|
+
return resp;
|
|
442
|
+
}
|
|
443
|
+
/** Verify a TOTP code (or XXXX-XXXX backup code) against a
|
|
444
|
+
* `totp_required` challenge. On success the JWT is stored. */
|
|
445
|
+
async verifyTotp(input) {
|
|
446
|
+
const resp = await this.http.post("/auth/login/totp", {
|
|
447
|
+
body: withRemember(input),
|
|
448
|
+
skipAuth: true
|
|
449
|
+
});
|
|
450
|
+
await this.keep(resp);
|
|
451
|
+
return resp;
|
|
452
|
+
}
|
|
453
|
+
/** Verify a 6-digit OTP code (Telegram / email) against an
|
|
454
|
+
* `otp_sent` challenge. */
|
|
455
|
+
async verifyOtp(input) {
|
|
456
|
+
const resp = await this.http.post("/auth/login/otp/verify", {
|
|
457
|
+
body: withRemember(input),
|
|
458
|
+
skipAuth: true
|
|
459
|
+
});
|
|
460
|
+
await this.keep(resp);
|
|
461
|
+
return resp;
|
|
462
|
+
}
|
|
463
|
+
/** Create an end-user account and sign it in. The JWT is stored.
|
|
464
|
+
* `wallet_currency` must be one the deployment offers; omit it to take
|
|
465
|
+
* the deployment default (it can be switched later via `client.wallet`
|
|
466
|
+
* while the wallet is empty).
|
|
467
|
+
* Errors: 400 `validation` / `currency_not_allowed` / `consents_required`
|
|
468
|
+
* / `unknown_consent`, 409 email taken. */
|
|
469
|
+
async register(input) {
|
|
470
|
+
const { cardOrder, ...rest } = input;
|
|
471
|
+
const body = withRemember(rest);
|
|
472
|
+
if (cardOrder !== void 0) body["card_order"] = cardOrderBody(cardOrder);
|
|
473
|
+
const resp = await this.http.post("/auth/register", {
|
|
474
|
+
body,
|
|
475
|
+
skipAuth: true
|
|
476
|
+
});
|
|
477
|
+
await this.keep(resp);
|
|
478
|
+
return resp;
|
|
479
|
+
}
|
|
480
|
+
/** Finish a `totp_setup_required` login: send the first code from the
|
|
481
|
+
* authenticator app. The JWT is stored. The response carries ten
|
|
482
|
+
* one-time `backup_codes` that are never returned again, so show them to
|
|
483
|
+
* the user right away and don't keep them on the device. */
|
|
484
|
+
async completeTotpSetup(input) {
|
|
485
|
+
const resp = await this.http.post("/auth/login/totp-setup", {
|
|
486
|
+
body: withRemember(input),
|
|
487
|
+
skipAuth: true
|
|
488
|
+
});
|
|
489
|
+
await this.keep(resp);
|
|
490
|
+
return resp;
|
|
491
|
+
}
|
|
492
|
+
/** Change the signed-in user's password. Every other session of the
|
|
493
|
+
* account is signed out; this one stays valid.
|
|
494
|
+
* Errors: 400 when the new password fails the policy, 401 when the
|
|
495
|
+
* current one is wrong. */
|
|
496
|
+
async changePassword(input) {
|
|
497
|
+
return this.http.post("/auth/password", {
|
|
498
|
+
body: input
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
/** Swap the 2FA method mid-flow. Resolves to `otp_sent` for
|
|
502
|
+
* telegram/email; for TOTP it short-circuits back to the challenge. */
|
|
503
|
+
async switchMethod(input) {
|
|
504
|
+
return this.http.post("/auth/login/2fa/switch", {
|
|
505
|
+
body: input,
|
|
506
|
+
skipAuth: true
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
async exchangeTelegram(input) {
|
|
510
|
+
const body = {
|
|
511
|
+
init_data: input.initData
|
|
512
|
+
};
|
|
513
|
+
if (input.provision !== void 0) body.provision = input.provision;
|
|
514
|
+
if (input.attribution !== void 0) body.attribution = input.attribution;
|
|
515
|
+
if (input.code !== void 0) body.code = input.code;
|
|
516
|
+
if (input.consents !== void 0) body.consents = input.consents;
|
|
517
|
+
if (input.consentChannel !== void 0) body.consent_channel = input.consentChannel;
|
|
518
|
+
let resp;
|
|
519
|
+
try {
|
|
520
|
+
resp = await this.http.post("/auth/telegram/exchange", {
|
|
521
|
+
body,
|
|
522
|
+
skipAuth: true
|
|
523
|
+
});
|
|
524
|
+
} catch (e) {
|
|
525
|
+
if (isApiError(e) && e.status === 404 && e.code === "telegram_unknown") {
|
|
526
|
+
return { stage: "unknown" };
|
|
527
|
+
}
|
|
528
|
+
throw e;
|
|
529
|
+
}
|
|
530
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
531
|
+
return resp;
|
|
532
|
+
}
|
|
533
|
+
/** Attach the Telegram account behind `initData` to an existing email +
|
|
534
|
+
* password account and sign in as that account. No bearer needed; the
|
|
535
|
+
* password is checked the same way `login` checks it. The JWT is stored.
|
|
536
|
+
*
|
|
537
|
+
* Errors: 401 `unauthenticated` (wrong email or password, as with
|
|
538
|
+
* `login`) / `invalid_init_data`; 403
|
|
539
|
+
* `user_disabled` / `second_factor_required` (accounts that sign in with
|
|
540
|
+
* a second factor can't be linked from a Mini App); 409
|
|
541
|
+
* `telegram_already_linked_other` (this account already has a different
|
|
542
|
+
* Telegram; unlink it first) / `telegram_in_use` (this Telegram belongs to
|
|
543
|
+
* another account); 503 `telegram_not_configured`. */
|
|
544
|
+
async linkTelegram(input) {
|
|
545
|
+
const resp = await this.http.post("/auth/telegram/link", {
|
|
546
|
+
body: { init_data: input.initData, email: input.email, password: input.password },
|
|
547
|
+
skipAuth: true
|
|
548
|
+
});
|
|
549
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
550
|
+
return resp;
|
|
551
|
+
}
|
|
552
|
+
/** Attach the first real email to a Telegram-made account (and clear
|
|
553
|
+
* `email_pending`). 409 if taken. An account that already has a real
|
|
554
|
+
* address gets 409 `email_change_needs_code`: change it with
|
|
555
|
+
* {@link requestEmailChange} and {@link confirmEmailChange}. */
|
|
556
|
+
async linkEmail(input) {
|
|
557
|
+
return this.http.post("/me/email", { body: { email: input.email } });
|
|
558
|
+
}
|
|
559
|
+
/** Step one of an email change: a six-digit code goes to the new address
|
|
560
|
+
* and works for 5 minutes. Nothing changes yet.
|
|
561
|
+
* Errors: 400 `same_email`, 409 address taken, 429 `too_many_requests`
|
|
562
|
+
* (five codes an hour). */
|
|
563
|
+
async requestEmailChange(input) {
|
|
564
|
+
return this.http.post("/me/email/change", {
|
|
565
|
+
body: { email: input.email }
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
/** Step two: the code from the e-mail changes the address; the old one
|
|
569
|
+
* gets a notice. Errors: 401 `invalid_code` (after five wrong codes a new
|
|
570
|
+
* request is needed), 409 the address was taken meanwhile. */
|
|
571
|
+
async confirmEmailChange(input) {
|
|
572
|
+
return this.http.post("/me/email/change/confirm", {
|
|
573
|
+
body: { challenge_id: input.challengeId, code: input.code }
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
/** Current user — fails 401 when the stored JWT is expired or
|
|
577
|
+
* missing. Useful as a "do I have a session" probe on app boot. */
|
|
578
|
+
async me() {
|
|
579
|
+
return this.http.get("/auth/me");
|
|
580
|
+
}
|
|
581
|
+
/** Sign out: ends this session and the remembered sign-in on the
|
|
582
|
+
* server (best effort: a network failure still signs out locally), then
|
|
583
|
+
* forgets both tokens. */
|
|
584
|
+
async signOut() {
|
|
585
|
+
const remember = this.remembered ? await Promise.resolve(this.remembered.get()) : null;
|
|
586
|
+
try {
|
|
587
|
+
await this.http.post("/auth/logout", {
|
|
588
|
+
body: remember ? { remember_token: remember } : {},
|
|
589
|
+
retried: true
|
|
590
|
+
});
|
|
591
|
+
} catch {
|
|
592
|
+
}
|
|
593
|
+
await Promise.resolve(this.tokens.clear());
|
|
594
|
+
if (this.remembered) await Promise.resolve(this.remembered.clear());
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
|
|
539
598
|
// src/resources/deposits.ts
|
|
540
599
|
var DepositsResource = class {
|
|
541
600
|
constructor(http) {
|
|
@@ -1075,6 +1134,44 @@ var AttributionResource = class {
|
|
|
1075
1134
|
return this.http.put("/me/attribution/survey", { body });
|
|
1076
1135
|
}
|
|
1077
1136
|
};
|
|
1137
|
+
function tgWebApp() {
|
|
1138
|
+
return globalThis.Telegram?.WebApp?.initDataUnsafe;
|
|
1139
|
+
}
|
|
1140
|
+
function readCookie(name) {
|
|
1141
|
+
const doc = globalThis.document;
|
|
1142
|
+
if (!doc?.cookie) return void 0;
|
|
1143
|
+
for (const part of doc.cookie.split(";")) {
|
|
1144
|
+
const [k, ...v] = part.trim().split("=");
|
|
1145
|
+
if (k === name) return decodeURIComponent(v.join("="));
|
|
1146
|
+
}
|
|
1147
|
+
return void 0;
|
|
1148
|
+
}
|
|
1149
|
+
var KEY_SHAPE = /^[A-Za-z0-9_-]{8,64}$/;
|
|
1150
|
+
function anonymousKey(opts = {}) {
|
|
1151
|
+
const tgId = tgWebApp()?.user?.id;
|
|
1152
|
+
if (tgId) return `tg:${tgId}`;
|
|
1153
|
+
const g = globalThis;
|
|
1154
|
+
if (!g.document) return void 0;
|
|
1155
|
+
const name = opts.name ?? "takeal_anon";
|
|
1156
|
+
let stored;
|
|
1157
|
+
try {
|
|
1158
|
+
stored = g.localStorage?.getItem(name) ?? void 0;
|
|
1159
|
+
} catch {
|
|
1160
|
+
stored = void 0;
|
|
1161
|
+
}
|
|
1162
|
+
const fromCookie = readCookie(name);
|
|
1163
|
+
let key = [fromCookie, stored].find((k) => !!k && KEY_SHAPE.test(k));
|
|
1164
|
+
if (!key) {
|
|
1165
|
+
key = g.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
|
|
1166
|
+
}
|
|
1167
|
+
try {
|
|
1168
|
+
g.localStorage?.setItem(name, key);
|
|
1169
|
+
} catch {
|
|
1170
|
+
}
|
|
1171
|
+
const domain = opts.cookieDomain ? `; domain=${opts.cookieDomain}` : "";
|
|
1172
|
+
g.document.cookie = `${name}=${encodeURIComponent(key)}; max-age=31536000; path=/; SameSite=Lax${domain}`;
|
|
1173
|
+
return key;
|
|
1174
|
+
}
|
|
1078
1175
|
|
|
1079
1176
|
// src/resources/history.ts
|
|
1080
1177
|
var HistoryResource = class {
|
|
@@ -1104,6 +1201,69 @@ var HistoryResource = class {
|
|
|
1104
1201
|
}
|
|
1105
1202
|
};
|
|
1106
1203
|
|
|
1204
|
+
// src/resources/consents.ts
|
|
1205
|
+
var ConsentsResource = class {
|
|
1206
|
+
constructor(http) {
|
|
1207
|
+
this.http = http;
|
|
1208
|
+
}
|
|
1209
|
+
/** Where each consent stands. */
|
|
1210
|
+
async get() {
|
|
1211
|
+
return this.http.get("/me/consents");
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Give (`true`) or withdraw (`false`) an optional consent: `marketing` or
|
|
1215
|
+
* `cookies_analytics`. Withdrawing `marketing` turns advertising off in
|
|
1216
|
+
* every channel at once; giving it turns on advertising by e-mail. A call
|
|
1217
|
+
* that changes nothing records nothing.
|
|
1218
|
+
*
|
|
1219
|
+
* Errors: 400 `consent_required` for `terms` or `personal_data`,
|
|
1220
|
+
* 400 `unknown_consent`.
|
|
1221
|
+
*/
|
|
1222
|
+
async set(kind, given, channel) {
|
|
1223
|
+
const body = { kind, given };
|
|
1224
|
+
if (channel !== void 0) body.channel = channel;
|
|
1225
|
+
return this.http.post("/me/consents", { body });
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Record what the cookie banner or the cookie settings say about
|
|
1229
|
+
* analytics. Works signed in or not: before sign-in the choice is kept
|
|
1230
|
+
* under the anonymous key and moves to the account at sign-up (send the
|
|
1231
|
+
* same key in `register({ attribution })`). Declining deletes the
|
|
1232
|
+
* visitor's recorded visits and analytics ids and stops their conversions
|
|
1233
|
+
* going to Metrica. Call it on every change, not on every page load; the
|
|
1234
|
+
* same answer twice records nothing (`recorded: false`).
|
|
1235
|
+
*
|
|
1236
|
+
* Errors: 400 `invalid_anonymous_key` when not signed in and no key could
|
|
1237
|
+
* be found.
|
|
1238
|
+
*/
|
|
1239
|
+
async setCookies(input) {
|
|
1240
|
+
const body = {
|
|
1241
|
+
analytics: input.analytics
|
|
1242
|
+
};
|
|
1243
|
+
const key = input.anonymousKey ?? anonymousKey();
|
|
1244
|
+
if (key !== void 0) body.anonymous_key = key;
|
|
1245
|
+
if (input.channel !== void 0) body.channel = input.channel;
|
|
1246
|
+
return this.http.post("/consents/cookies", { body });
|
|
1247
|
+
}
|
|
1248
|
+
};
|
|
1249
|
+
|
|
1250
|
+
// src/resources/receipts.ts
|
|
1251
|
+
var ReceiptsResource = class {
|
|
1252
|
+
constructor(http) {
|
|
1253
|
+
this.http = http;
|
|
1254
|
+
}
|
|
1255
|
+
async list(params = {}) {
|
|
1256
|
+
const q = new URLSearchParams();
|
|
1257
|
+
if (params.refId !== void 0) q.set("ref_id", params.refId);
|
|
1258
|
+
if (params.limit !== void 0) q.set("limit", String(params.limit));
|
|
1259
|
+
const qs = q.toString();
|
|
1260
|
+
return this.http.get(`/me/receipts${qs ? `?${qs}` : ""}`);
|
|
1261
|
+
}
|
|
1262
|
+
async get(id) {
|
|
1263
|
+
return this.http.get(`/me/receipts/${encodeURIComponent(id)}`);
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
|
|
1107
1267
|
// src/resources/funding-options.ts
|
|
1108
1268
|
var FundingOptionsResource = class {
|
|
1109
1269
|
constructor(http) {
|
|
@@ -1116,7 +1276,7 @@ var FundingOptionsResource = class {
|
|
|
1116
1276
|
};
|
|
1117
1277
|
|
|
1118
1278
|
// src/version.ts
|
|
1119
|
-
var SDK_VERSION = "0.6.
|
|
1279
|
+
var SDK_VERSION = "0.6.3";
|
|
1120
1280
|
|
|
1121
1281
|
// src/client-platform.ts
|
|
1122
1282
|
var CLIENT_HEADER = "X-Takeal-Client";
|
|
@@ -1191,7 +1351,7 @@ function isAbsolute(url) {
|
|
|
1191
1351
|
}
|
|
1192
1352
|
|
|
1193
1353
|
// src/compat.ts
|
|
1194
|
-
var SUPPORTED_MEISTER = ">=0.6.
|
|
1354
|
+
var SUPPORTED_MEISTER = ">=0.6.8, <0.7.0";
|
|
1195
1355
|
var SEMVER = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
1196
1356
|
function parse(v) {
|
|
1197
1357
|
const m = SEMVER.exec(v.trim());
|
|
@@ -1300,7 +1460,9 @@ function createClient(opts) {
|
|
|
1300
1460
|
notifications: new NotificationsResource(http),
|
|
1301
1461
|
devices: new DevicesResource(http, client),
|
|
1302
1462
|
attribution: new AttributionResource(http),
|
|
1303
|
-
history: new HistoryResource(http)
|
|
1463
|
+
history: new HistoryResource(http),
|
|
1464
|
+
consents: new ConsentsResource(http),
|
|
1465
|
+
receipts: new ReceiptsResource(http)
|
|
1304
1466
|
};
|
|
1305
1467
|
}
|
|
1306
1468
|
function pickDefaultStore(key) {
|