@takeal/cusfront-sdk 0.2.8 → 0.3.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.
- package/CHANGELOG.md +76 -0
- package/README.md +367 -5
- package/dist/devices-C3JHi2Hi.d.cts +165 -0
- package/dist/devices-DP_hxwmW.d.ts +165 -0
- package/dist/{http-BkCfOCR0.d.cts → http-B-OQwMYn.d.cts} +9 -1
- package/dist/{http-BkCfOCR0.d.ts → http-B-OQwMYn.d.ts} +9 -1
- package/dist/index.cjs +280 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +65 -9
- package/dist/index.d.ts +65 -9
- package/dist/index.js +277 -21
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +133 -6
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +62 -3
- package/dist/react/index.d.ts +62 -3
- package/dist/react/index.js +131 -8
- package/dist/react/index.js.map +1 -1
- package/dist/resources/auth.cjs +36 -11
- package/dist/resources/auth.cjs.map +1 -1
- package/dist/resources/auth.d.cts +57 -5
- package/dist/resources/auth.d.ts +57 -5
- package/dist/resources/auth.js +36 -11
- package/dist/resources/auth.js.map +1 -1
- package/dist/resources/balance.d.cts +1 -1
- package/dist/resources/balance.d.ts +1 -1
- package/dist/resources/blog.d.cts +1 -1
- package/dist/resources/blog.d.ts +1 -1
- package/dist/resources/branding.d.cts +1 -1
- package/dist/resources/branding.d.ts +1 -1
- package/dist/resources/cards.cjs +33 -1
- package/dist/resources/cards.cjs.map +1 -1
- package/dist/resources/cards.d.cts +105 -4
- package/dist/resources/cards.d.ts +105 -4
- package/dist/resources/cards.js +33 -1
- package/dist/resources/cards.js.map +1 -1
- package/dist/resources/deposits.d.cts +1 -1
- package/dist/resources/deposits.d.ts +1 -1
- package/dist/resources/devices.cjs +92 -0
- package/dist/resources/devices.cjs.map +1 -0
- package/dist/resources/devices.d.cts +3 -0
- package/dist/resources/devices.d.ts +3 -0
- package/dist/resources/devices.js +89 -0
- package/dist/resources/devices.js.map +1 -0
- package/dist/resources/me.cjs +32 -0
- package/dist/resources/me.cjs.map +1 -0
- package/dist/resources/me.d.cts +40 -0
- package/dist/resources/me.d.ts +40 -0
- package/dist/resources/me.js +30 -0
- package/dist/resources/me.js.map +1 -0
- package/dist/resources/notifications.cjs +53 -0
- package/dist/resources/notifications.cjs.map +1 -0
- package/dist/resources/notifications.d.cts +109 -0
- package/dist/resources/notifications.d.ts +109 -0
- package/dist/resources/notifications.js +51 -0
- package/dist/resources/notifications.js.map +1 -0
- package/dist/resources/push.cjs +8 -4
- package/dist/resources/push.cjs.map +1 -1
- package/dist/resources/push.d.cts +14 -17
- package/dist/resources/push.d.ts +14 -17
- package/dist/resources/push.js +8 -4
- package/dist/resources/push.js.map +1 -1
- package/dist/resources/sessions.d.cts +1 -1
- package/dist/resources/sessions.d.ts +1 -1
- package/dist/resources/subscriptions.cjs.map +1 -1
- package/dist/resources/subscriptions.d.cts +8 -1
- package/dist/resources/subscriptions.d.ts +8 -1
- package/dist/resources/subscriptions.js.map +1 -1
- package/dist/resources/wallet.d.cts +1 -1
- package/dist/resources/wallet.d.ts +1 -1
- package/dist/telegram.cjs +321 -23
- package/dist/telegram.cjs.map +1 -1
- package/dist/telegram.d.cts +62 -7
- package/dist/telegram.d.ts +62 -7
- package/dist/telegram.js +320 -24
- package/dist/telegram.js.map +1 -1
- package/package.json +17 -2
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { H as HttpClient } from './http-B-OQwMYn.js';
|
|
2
|
+
import { PushCapableRegistration } from './resources/push.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Which kind of app is talking to the API. Sent on every request as
|
|
6
|
+
* `X-Takeal-Client: <platform>; <version>`. The server keeps it on the
|
|
7
|
+
* session: it decides whether a sign-in counts as a new device, and which
|
|
8
|
+
* channel a notification goes out on (a Mini App gets Telegram messages, a
|
|
9
|
+
* PWA gets Web Push, and so on).
|
|
10
|
+
*/
|
|
11
|
+
type ClientPlatform = "pwa" | "web" | "telegram-miniapp" | "ios" | "android";
|
|
12
|
+
/** The `client` option of `createClient`. */
|
|
13
|
+
interface ClientDescriptor {
|
|
14
|
+
/** What the app is. Detected at runtime when omitted; see
|
|
15
|
+
* {@link detectClientPlatform}. */
|
|
16
|
+
platform: ClientPlatform;
|
|
17
|
+
/** The app's own version. Defaults to the SDK version. */
|
|
18
|
+
version?: string;
|
|
19
|
+
}
|
|
20
|
+
/** A descriptor with every field filled in. `client.client` on a
|
|
21
|
+
* {@link CusfrontClient} has this shape. */
|
|
22
|
+
interface ResolvedClientDescriptor {
|
|
23
|
+
platform: ClientPlatform;
|
|
24
|
+
version: string;
|
|
25
|
+
}
|
|
26
|
+
/** Header name carrying the client descriptor. */
|
|
27
|
+
declare const CLIENT_HEADER = "X-Takeal-Client";
|
|
28
|
+
/**
|
|
29
|
+
* Best-effort platform detection for browser runtimes:
|
|
30
|
+
*
|
|
31
|
+
* - `telegram-miniapp` when `window.Telegram.WebApp.initData` is non-empty
|
|
32
|
+
* (the app was launched from Telegram);
|
|
33
|
+
* - `pwa` when the page runs installed: iOS Safari's `navigator.standalone`
|
|
34
|
+
* or a `display-mode: standalone` media query;
|
|
35
|
+
* - `web` otherwise, and always outside a browser (Node, workers without a
|
|
36
|
+
* `navigator`).
|
|
37
|
+
*
|
|
38
|
+
* Native wrappers (Capacitor, React Native) should pass `ios` / `android`
|
|
39
|
+
* explicitly; there is no reliable way to tell them apart from a browser.
|
|
40
|
+
*/
|
|
41
|
+
declare function detectClientPlatform(): ClientPlatform;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Devices resource — `client.devices.*`.
|
|
45
|
+
*
|
|
46
|
+
* A user has many devices: each browser with a Web Push subscription, each
|
|
47
|
+
* phone with an FCM / APNs token, and a marker for "uses the Telegram Mini
|
|
48
|
+
* App". The server picks the notification channel from them: a mobile app
|
|
49
|
+
* first, otherwise the device seen most recently.
|
|
50
|
+
*
|
|
51
|
+
* The usual browser flow, once notification permission is granted:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* const reg = await navigator.serviceWorker.ready;
|
|
55
|
+
* const device = await client.devices.registerWebPush(reg, { label: "Chrome on Mac" });
|
|
56
|
+
* // later, from a settings screen:
|
|
57
|
+
* await client.devices.unregisterWebPush(reg);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
type DeviceKind = "webpush" | "fcm" | "apns" | "telegram_miniapp";
|
|
61
|
+
type DevicePlatform = "pwa" | "web" | "ios" | "android" | "telegram";
|
|
62
|
+
interface Device {
|
|
63
|
+
id: string;
|
|
64
|
+
kind: DeviceKind;
|
|
65
|
+
platform: DevicePlatform;
|
|
66
|
+
/** Free-form name the app gave it ("Chrome on Mac"), or `null`. */
|
|
67
|
+
label: string | null;
|
|
68
|
+
locale: string | null;
|
|
69
|
+
app_version: string | null;
|
|
70
|
+
created_at: string;
|
|
71
|
+
last_seen_at: string;
|
|
72
|
+
/** Set when the device no longer receives anything (the push endpoint is
|
|
73
|
+
* gone, or someone switched it off). */
|
|
74
|
+
disabled_at: string | null;
|
|
75
|
+
/** `true` when it was registered from the session this client holds. */
|
|
76
|
+
is_current: boolean;
|
|
77
|
+
}
|
|
78
|
+
/** `PushSubscription.toJSON()` minus the parts the server doesn't need. */
|
|
79
|
+
interface WebPushCredential {
|
|
80
|
+
/** `PushSubscription.endpoint`; must be https. */
|
|
81
|
+
endpoint: string;
|
|
82
|
+
keys: {
|
|
83
|
+
/** URL-safe base64. */
|
|
84
|
+
p256dh: string;
|
|
85
|
+
/** URL-safe base64. */
|
|
86
|
+
auth: string;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** FCM registration token or APNs device token. */
|
|
90
|
+
interface MobilePushCredential {
|
|
91
|
+
token: string;
|
|
92
|
+
}
|
|
93
|
+
type RegisterDeviceInput = {
|
|
94
|
+
kind: "webpush";
|
|
95
|
+
platform: DevicePlatform;
|
|
96
|
+
credential: WebPushCredential;
|
|
97
|
+
label?: string;
|
|
98
|
+
locale?: string;
|
|
99
|
+
app_version?: string;
|
|
100
|
+
} | {
|
|
101
|
+
kind: "fcm" | "apns";
|
|
102
|
+
platform: DevicePlatform;
|
|
103
|
+
credential: MobilePushCredential;
|
|
104
|
+
label?: string;
|
|
105
|
+
locale?: string;
|
|
106
|
+
app_version?: string;
|
|
107
|
+
} | {
|
|
108
|
+
kind: "telegram_miniapp";
|
|
109
|
+
platform: DevicePlatform;
|
|
110
|
+
/** Nothing to send: the device is a marker. */
|
|
111
|
+
credential: Record<string, never>;
|
|
112
|
+
label?: string;
|
|
113
|
+
locale?: string;
|
|
114
|
+
app_version?: string;
|
|
115
|
+
};
|
|
116
|
+
interface RegisteredDevice {
|
|
117
|
+
id: string;
|
|
118
|
+
kind: DeviceKind;
|
|
119
|
+
platform: DevicePlatform;
|
|
120
|
+
/** VAPID public key (URL-safe base64) for `pushManager.subscribe`;
|
|
121
|
+
* `null` when push is not set up on this deployment. */
|
|
122
|
+
vapid_public_key: string | null;
|
|
123
|
+
}
|
|
124
|
+
interface RegisterWebPushOptions {
|
|
125
|
+
/** A name to show in the user's device list ("Chrome on Mac"). */
|
|
126
|
+
label?: string;
|
|
127
|
+
/** Skip the key lookup when you already hold the deployment's VAPID
|
|
128
|
+
* public key (from an earlier `register` result, for example). */
|
|
129
|
+
vapidPublicKey?: string;
|
|
130
|
+
}
|
|
131
|
+
interface UnregisterWebPushOptions {
|
|
132
|
+
/** The device to delete, from an earlier `registerWebPush` result. When
|
|
133
|
+
* omitted, every Web Push device registered from this session goes. */
|
|
134
|
+
deviceId?: string;
|
|
135
|
+
}
|
|
136
|
+
/** Client platform → the platform the device list uses. */
|
|
137
|
+
declare function devicePlatformOf(platform: ClientPlatform): DevicePlatform;
|
|
138
|
+
declare class DevicesResource {
|
|
139
|
+
private readonly http;
|
|
140
|
+
private readonly client;
|
|
141
|
+
constructor(http: HttpClient, client: ResolvedClientDescriptor);
|
|
142
|
+
/** Every device the user has, disabled ones included. */
|
|
143
|
+
list(): Promise<Device[]>;
|
|
144
|
+
/** Register a device, or refresh it when the same credential is already
|
|
145
|
+
* known (then `last_seen_at`, `label` and `app_version` update and a
|
|
146
|
+
* disabled device comes back). `app_version` defaults to the client
|
|
147
|
+
* version sent in `X-Takeal-Client`.
|
|
148
|
+
*
|
|
149
|
+
* Errors: 400 `validation`; 409 `push_disabled` for `webpush` when the
|
|
150
|
+
* deployment has no VAPID keys. */
|
|
151
|
+
register(input: RegisterDeviceInput): Promise<RegisteredDevice>;
|
|
152
|
+
/** Delete one device. 404 when it isn't the user's. */
|
|
153
|
+
remove(id: string): Promise<void>;
|
|
154
|
+
/** Subscribe this browser to Web Push and register it as a device. Ask
|
|
155
|
+
* for notification permission before calling. Throws when push isn't
|
|
156
|
+
* set up on the deployment. */
|
|
157
|
+
registerWebPush(registration: PushCapableRegistration, opts?: RegisterWebPushOptions): Promise<RegisteredDevice>;
|
|
158
|
+
/** Unsubscribe this browser from Web Push and delete the matching device
|
|
159
|
+
* (see {@link UnregisterWebPushOptions}). Safe when nothing is
|
|
160
|
+
* registered. */
|
|
161
|
+
unregisterWebPush(registration: PushCapableRegistration, opts?: UnregisterWebPushOptions): Promise<void>;
|
|
162
|
+
private vapidPublicKey;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export { type ClientDescriptor as C, DevicesResource as D, type MobilePushCredential as M, type ResolvedClientDescriptor as R, type UnregisterWebPushOptions as U, type WebPushCredential as W, CLIENT_HEADER as a, type ClientPlatform as b, type Device as c, type DeviceKind as d, type DevicePlatform as e, type RegisterDeviceInput as f, type RegisterWebPushOptions as g, type RegisteredDevice as h, detectClientPlatform as i, devicePlatformOf as j };
|
|
@@ -36,6 +36,8 @@ declare function inMemoryStore(): TokenStore;
|
|
|
36
36
|
* - Set `Content-Type: application/json` on bodies.
|
|
37
37
|
* - Parse JSON responses + map non-2xx to `ApiError`.
|
|
38
38
|
* - Forward `Idempotency-Key` when the caller passes one.
|
|
39
|
+
* - Send the constructor's `defaultHeaders` (the `X-Takeal-Client`
|
|
40
|
+
* descriptor) on every request.
|
|
39
41
|
* - Surface network failures (DNS, abort, broken pipe) as
|
|
40
42
|
* `NetworkError`.
|
|
41
43
|
*
|
|
@@ -62,7 +64,13 @@ declare class HttpClient {
|
|
|
62
64
|
private readonly baseUrl;
|
|
63
65
|
private readonly tokens;
|
|
64
66
|
private readonly fetchImpl;
|
|
65
|
-
|
|
67
|
+
/** Headers sent on every request, below the per-call `headers`
|
|
68
|
+
* (a caller can still override one). */
|
|
69
|
+
private readonly defaultHeaders;
|
|
70
|
+
constructor(baseUrl: string, tokens: TokenStore, fetchImpl: FetchLike,
|
|
71
|
+
/** Headers sent on every request, below the per-call `headers`
|
|
72
|
+
* (a caller can still override one). */
|
|
73
|
+
defaultHeaders?: Record<string, string>);
|
|
66
74
|
get<T>(path: string, opts?: HttpOptions): Promise<T>;
|
|
67
75
|
post<T>(path: string, opts?: HttpOptions): Promise<T>;
|
|
68
76
|
put<T>(path: string, opts?: HttpOptions): Promise<T>;
|
|
@@ -36,6 +36,8 @@ declare function inMemoryStore(): TokenStore;
|
|
|
36
36
|
* - Set `Content-Type: application/json` on bodies.
|
|
37
37
|
* - Parse JSON responses + map non-2xx to `ApiError`.
|
|
38
38
|
* - Forward `Idempotency-Key` when the caller passes one.
|
|
39
|
+
* - Send the constructor's `defaultHeaders` (the `X-Takeal-Client`
|
|
40
|
+
* descriptor) on every request.
|
|
39
41
|
* - Surface network failures (DNS, abort, broken pipe) as
|
|
40
42
|
* `NetworkError`.
|
|
41
43
|
*
|
|
@@ -62,7 +64,13 @@ declare class HttpClient {
|
|
|
62
64
|
private readonly baseUrl;
|
|
63
65
|
private readonly tokens;
|
|
64
66
|
private readonly fetchImpl;
|
|
65
|
-
|
|
67
|
+
/** Headers sent on every request, below the per-call `headers`
|
|
68
|
+
* (a caller can still override one). */
|
|
69
|
+
private readonly defaultHeaders;
|
|
70
|
+
constructor(baseUrl: string, tokens: TokenStore, fetchImpl: FetchLike,
|
|
71
|
+
/** Headers sent on every request, below the per-call `headers`
|
|
72
|
+
* (a caller can still override one). */
|
|
73
|
+
defaultHeaders?: Record<string, string>);
|
|
66
74
|
get<T>(path: string, opts?: HttpOptions): Promise<T>;
|
|
67
75
|
post<T>(path: string, opts?: HttpOptions): Promise<T>;
|
|
68
76
|
put<T>(path: string, opts?: HttpOptions): Promise<T>;
|
package/dist/index.cjs
CHANGED
|
@@ -37,10 +37,11 @@ function versionedPath(path) {
|
|
|
37
37
|
return `${API_VERSION}${p}`;
|
|
38
38
|
}
|
|
39
39
|
var HttpClient = class {
|
|
40
|
-
constructor(baseUrl, tokens, fetchImpl) {
|
|
40
|
+
constructor(baseUrl, tokens, fetchImpl, defaultHeaders = {}) {
|
|
41
41
|
this.baseUrl = baseUrl;
|
|
42
42
|
this.tokens = tokens;
|
|
43
43
|
this.fetchImpl = fetchImpl;
|
|
44
|
+
this.defaultHeaders = defaultHeaders;
|
|
44
45
|
if (!baseUrl) throw new Error("HttpClient: baseUrl is required");
|
|
45
46
|
}
|
|
46
47
|
async get(path, opts = {}) {
|
|
@@ -61,7 +62,8 @@ var HttpClient = class {
|
|
|
61
62
|
async request(method, path, opts) {
|
|
62
63
|
const url = this.absolute(path);
|
|
63
64
|
const headers = {
|
|
64
|
-
Accept: "application/json"
|
|
65
|
+
Accept: "application/json",
|
|
66
|
+
...this.defaultHeaders
|
|
65
67
|
};
|
|
66
68
|
if (opts.body !== void 0) headers["Content-Type"] = "application/json";
|
|
67
69
|
if (opts.idempotencyKey) headers["Idempotency-Key"] = opts.idempotencyKey;
|
|
@@ -199,18 +201,38 @@ var AuthResource = class {
|
|
|
199
201
|
skipAuth: true
|
|
200
202
|
});
|
|
201
203
|
}
|
|
202
|
-
/** Exchange a signed Telegram Mini App `initData` payload for a session
|
|
203
|
-
* JWT. The server validates the initData HMAC against the deployment's bot
|
|
204
|
-
* token, then find-or-creates the end-user. First-time Telegram users are
|
|
205
|
-
* auto-provisioned with `user.email_pending === true` — prompt them for a
|
|
206
|
-
* real email and call {@link linkEmail}. Stores the JWT on success.
|
|
207
|
-
*
|
|
208
|
-
* Most consumers use the higher-level `fromInitData` / `fromTelegramWebApp`
|
|
209
|
-
* helpers in `@takeal/cusfront-sdk/telegram`; reach for this directly when
|
|
210
|
-
* you already hold a configured client. */
|
|
211
204
|
async exchangeTelegram(input) {
|
|
212
|
-
const
|
|
213
|
-
|
|
205
|
+
const body = { init_data: input.initData };
|
|
206
|
+
if (input.provision !== void 0) body.provision = input.provision;
|
|
207
|
+
let resp;
|
|
208
|
+
try {
|
|
209
|
+
resp = await this.http.post("/auth/telegram/exchange", {
|
|
210
|
+
body,
|
|
211
|
+
skipAuth: true
|
|
212
|
+
});
|
|
213
|
+
} catch (e) {
|
|
214
|
+
if (isApiError(e) && e.status === 404 && e.code === "telegram_unknown") {
|
|
215
|
+
return { stage: "unknown" };
|
|
216
|
+
}
|
|
217
|
+
throw e;
|
|
218
|
+
}
|
|
219
|
+
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
220
|
+
return resp;
|
|
221
|
+
}
|
|
222
|
+
/** Attach the Telegram account behind `initData` to an existing email +
|
|
223
|
+
* password account and sign in as that account. No bearer needed; the
|
|
224
|
+
* password is checked the same way `login` checks it. The JWT is stored.
|
|
225
|
+
*
|
|
226
|
+
* Errors: 401 `unauthenticated` (wrong email or password, as with
|
|
227
|
+
* `login`) / `invalid_init_data`; 403
|
|
228
|
+
* `user_disabled` / `second_factor_required` (accounts that sign in with
|
|
229
|
+
* a second factor can't be linked from a Mini App); 409
|
|
230
|
+
* `telegram_already_linked_other` (this account already has a different
|
|
231
|
+
* Telegram; unlink it first) / `telegram_in_use` (this Telegram belongs to
|
|
232
|
+
* another account); 503 `telegram_not_configured`. */
|
|
233
|
+
async linkTelegram(input) {
|
|
234
|
+
const resp = await this.http.post("/auth/telegram/link", {
|
|
235
|
+
body: { init_data: input.initData, email: input.email, password: input.password },
|
|
214
236
|
skipAuth: true
|
|
215
237
|
});
|
|
216
238
|
await Promise.resolve(this.tokens.set(resp.access_token));
|
|
@@ -294,12 +316,44 @@ var CardsResource = class {
|
|
|
294
316
|
* `failure_reason` on the result.
|
|
295
317
|
*/
|
|
296
318
|
async create(input) {
|
|
297
|
-
const { idempotencyKey, ...
|
|
319
|
+
const { idempotencyKey, productId, name, designId, ...rest } = input;
|
|
320
|
+
const body = { ...rest };
|
|
321
|
+
if (productId !== void 0) body["product_id"] = productId;
|
|
322
|
+
if (name !== void 0) body["name"] = name;
|
|
323
|
+
if (designId !== void 0) body["design_id"] = designId;
|
|
298
324
|
return this.http.post("/me/cards", {
|
|
299
325
|
body,
|
|
300
326
|
idempotencyKey
|
|
301
327
|
});
|
|
302
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* The cards the caller can order, in the order the deployment offers them
|
|
331
|
+
* (the default product first). Each item carries its name, description,
|
|
332
|
+
* artwork, the issuance price after the caller's subscription discount,
|
|
333
|
+
* and the currency. Pass an item's `id` as `productId` to {@link create}.
|
|
334
|
+
*/
|
|
335
|
+
async listProducts() {
|
|
336
|
+
return this.http.get("/me/card-products");
|
|
337
|
+
}
|
|
338
|
+
/** One product from the catalog. 404 when unknown, disabled or hidden. */
|
|
339
|
+
async getProduct(id) {
|
|
340
|
+
return this.http.get(
|
|
341
|
+
`/me/card-products/${encodeURIComponent(id)}`
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Rename the caller's own card or change its design. Resolves to the
|
|
346
|
+
* updated card. 404 if the card isn't the caller's; 400 `invalid_name` /
|
|
347
|
+
* `design_not_allowed` on a bad value.
|
|
348
|
+
*/
|
|
349
|
+
async update(id, input) {
|
|
350
|
+
const body = {};
|
|
351
|
+
if (input.name !== void 0) body["name"] = input.name;
|
|
352
|
+
if (input.designId !== void 0) body["design_id"] = input.designId;
|
|
353
|
+
return this.http.patch(`/me/cards/${encodeURIComponent(id)}`, {
|
|
354
|
+
body
|
|
355
|
+
});
|
|
356
|
+
}
|
|
303
357
|
/** Fetch one card by id. */
|
|
304
358
|
async get(id) {
|
|
305
359
|
return this.http.get(`/me/cards/${encodeURIComponent(id)}`);
|
|
@@ -499,17 +553,20 @@ var PushResource = class {
|
|
|
499
553
|
return this.http.get("/me/push-subscription");
|
|
500
554
|
}
|
|
501
555
|
/** Store a subscription you created yourself.
|
|
502
|
-
* 409 `push_disabled` when the deployment has no VAPID keys.
|
|
556
|
+
* 409 `push_disabled` when the deployment has no VAPID keys.
|
|
557
|
+
* @deprecated Use `client.devices.register({ kind: "webpush", ... })`. */
|
|
503
558
|
async save(input) {
|
|
504
559
|
return this.http.put("/me/push-subscription", { body: input });
|
|
505
560
|
}
|
|
506
|
-
/** Delete the stored subscription. Safe when nothing is stored.
|
|
561
|
+
/** Delete the stored subscription. Safe when nothing is stored.
|
|
562
|
+
* @deprecated Use `client.devices.remove(id)` or `client.devices.unregisterWebPush(reg)`. */
|
|
507
563
|
async remove() {
|
|
508
564
|
await this.http.delete("/me/push-subscription");
|
|
509
565
|
}
|
|
510
566
|
/** Subscribe this browser and store the subscription. Ask for
|
|
511
567
|
* notification permission before calling. Throws when push isn't set up
|
|
512
|
-
* on the deployment.
|
|
568
|
+
* on the deployment.
|
|
569
|
+
* @deprecated Use `client.devices.registerWebPush(registration)`. */
|
|
513
570
|
async enable(registration) {
|
|
514
571
|
const { vapid_public_key } = await this.status();
|
|
515
572
|
if (!vapid_public_key) {
|
|
@@ -528,7 +585,8 @@ var PushResource = class {
|
|
|
528
585
|
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
529
586
|
});
|
|
530
587
|
}
|
|
531
|
-
/** Unsubscribe this browser and delete the stored subscription.
|
|
588
|
+
/** Unsubscribe this browser and delete the stored subscription.
|
|
589
|
+
* @deprecated Use `client.devices.unregisterWebPush(registration)`. */
|
|
532
590
|
async disable(registration) {
|
|
533
591
|
const sub = await registration.pushManager.getSubscription();
|
|
534
592
|
if (sub) await sub.unsubscribe();
|
|
@@ -544,6 +602,185 @@ function base64UrlToBytes(input) {
|
|
|
544
602
|
return out;
|
|
545
603
|
}
|
|
546
604
|
|
|
605
|
+
// src/resources/me.ts
|
|
606
|
+
var MeResource = class {
|
|
607
|
+
constructor(http) {
|
|
608
|
+
this.http = http;
|
|
609
|
+
}
|
|
610
|
+
/** Attach the Telegram account behind `initData` to the signed-in user.
|
|
611
|
+
* Returns the updated user (`telegram` is set). Repeating the call with
|
|
612
|
+
* the same Telegram is fine.
|
|
613
|
+
*
|
|
614
|
+
* Errors: 401 `invalid_init_data`; 403 `user_disabled` /
|
|
615
|
+
* `second_factor_required`; 409 `telegram_already_linked_other` (the
|
|
616
|
+
* account already has a different Telegram; `unlinkTelegram` first) /
|
|
617
|
+
* `telegram_in_use` (that Telegram belongs to another account); 503
|
|
618
|
+
* `telegram_not_configured`. */
|
|
619
|
+
async linkTelegram(initData) {
|
|
620
|
+
return this.http.post("/me/telegram", { body: { init_data: initData } });
|
|
621
|
+
}
|
|
622
|
+
/** Detach the Telegram account from the signed-in user. Returns the
|
|
623
|
+
* updated user (`telegram` is `null`).
|
|
624
|
+
*
|
|
625
|
+
* Errors: 404 `telegram_not_linked`; 409 `last_credential` when the
|
|
626
|
+
* account has no password, so Telegram is its only way to sign in. */
|
|
627
|
+
async unlinkTelegram() {
|
|
628
|
+
return this.http.delete("/me/telegram");
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
// src/resources/notifications.ts
|
|
633
|
+
var NotificationsResource = class {
|
|
634
|
+
constructor(http) {
|
|
635
|
+
this.http = http;
|
|
636
|
+
}
|
|
637
|
+
/** One page of the inbox, newest first. */
|
|
638
|
+
async list(params = {}) {
|
|
639
|
+
const q = new URLSearchParams();
|
|
640
|
+
if (params.cursor) q.set("cursor", params.cursor);
|
|
641
|
+
if (params.limit !== void 0) q.set("limit", String(params.limit));
|
|
642
|
+
if (params.unreadOnly) q.set("unread", "true");
|
|
643
|
+
if (params.category) q.set("category", params.category);
|
|
644
|
+
const qs = q.toString();
|
|
645
|
+
return this.http.get(`/me/notifications${qs ? `?${qs}` : ""}`);
|
|
646
|
+
}
|
|
647
|
+
/** How many items are unread. Cheap; fine to poll for a badge. */
|
|
648
|
+
async unreadCount() {
|
|
649
|
+
const { unread } = await this.http.get("/me/notifications/unread-count");
|
|
650
|
+
return unread;
|
|
651
|
+
}
|
|
652
|
+
/** Mark one item read. Returns the item; calling it again is harmless. */
|
|
653
|
+
async markRead(id) {
|
|
654
|
+
return this.http.post(`/me/notifications/${encodeURIComponent(id)}/read`);
|
|
655
|
+
}
|
|
656
|
+
/** Mark every unread item read, or only those created before `before`
|
|
657
|
+
* (ISO timestamp). Returns how many changed. */
|
|
658
|
+
async markAllRead(before) {
|
|
659
|
+
return this.http.post("/me/notifications/read-all", {
|
|
660
|
+
body: before ? { before } : {}
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
/** Delete one item. 404 when it isn't the user's. */
|
|
664
|
+
async remove(id) {
|
|
665
|
+
await this.http.delete(`/me/notifications/${encodeURIComponent(id)}`);
|
|
666
|
+
}
|
|
667
|
+
/** The user's channel and language settings. */
|
|
668
|
+
async getPreferences() {
|
|
669
|
+
return this.http.get("/me/notification-preferences");
|
|
670
|
+
}
|
|
671
|
+
/** Change some settings; the rest stay. Returns the stored view.
|
|
672
|
+
* 400 `validation` for an unknown category, channel or locale. */
|
|
673
|
+
async setPreferences(input) {
|
|
674
|
+
return this.http.put("/me/notification-preferences", {
|
|
675
|
+
body: input
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
// src/resources/devices.ts
|
|
681
|
+
function devicePlatformOf(platform) {
|
|
682
|
+
return platform === "telegram-miniapp" ? "telegram" : platform;
|
|
683
|
+
}
|
|
684
|
+
var DevicesResource = class {
|
|
685
|
+
constructor(http, client) {
|
|
686
|
+
this.http = http;
|
|
687
|
+
this.client = client;
|
|
688
|
+
}
|
|
689
|
+
/** Every device the user has, disabled ones included. */
|
|
690
|
+
async list() {
|
|
691
|
+
return this.http.get("/me/devices");
|
|
692
|
+
}
|
|
693
|
+
/** Register a device, or refresh it when the same credential is already
|
|
694
|
+
* known (then `last_seen_at`, `label` and `app_version` update and a
|
|
695
|
+
* disabled device comes back). `app_version` defaults to the client
|
|
696
|
+
* version sent in `X-Takeal-Client`.
|
|
697
|
+
*
|
|
698
|
+
* Errors: 400 `validation`; 409 `push_disabled` for `webpush` when the
|
|
699
|
+
* deployment has no VAPID keys. */
|
|
700
|
+
async register(input) {
|
|
701
|
+
return this.http.put("/me/devices", {
|
|
702
|
+
body: { app_version: this.client.version, ...input }
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
/** Delete one device. 404 when it isn't the user's. */
|
|
706
|
+
async remove(id) {
|
|
707
|
+
await this.http.delete(`/me/devices/${encodeURIComponent(id)}`);
|
|
708
|
+
}
|
|
709
|
+
/** Subscribe this browser to Web Push and register it as a device. Ask
|
|
710
|
+
* for notification permission before calling. Throws when push isn't
|
|
711
|
+
* set up on the deployment. */
|
|
712
|
+
async registerWebPush(registration, opts = {}) {
|
|
713
|
+
const key = opts.vapidPublicKey ?? await this.vapidPublicKey();
|
|
714
|
+
if (!key) {
|
|
715
|
+
throw new Error("@takeal/cusfront-sdk: push notifications are not enabled on this deployment");
|
|
716
|
+
}
|
|
717
|
+
const sub = await registration.pushManager.subscribe({
|
|
718
|
+
userVisibleOnly: true,
|
|
719
|
+
applicationServerKey: base64UrlToBytes(key)
|
|
720
|
+
});
|
|
721
|
+
const json = sub.toJSON();
|
|
722
|
+
if (!json.endpoint || !json.keys?.p256dh || !json.keys.auth) {
|
|
723
|
+
throw new Error("@takeal/cusfront-sdk: the browser returned an incomplete push subscription");
|
|
724
|
+
}
|
|
725
|
+
return this.register({
|
|
726
|
+
kind: "webpush",
|
|
727
|
+
platform: devicePlatformOf(this.client.platform),
|
|
728
|
+
credential: {
|
|
729
|
+
endpoint: json.endpoint,
|
|
730
|
+
keys: { p256dh: json.keys.p256dh, auth: json.keys.auth }
|
|
731
|
+
},
|
|
732
|
+
...opts.label ? { label: opts.label } : {}
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
/** Unsubscribe this browser from Web Push and delete the matching device
|
|
736
|
+
* (see {@link UnregisterWebPushOptions}). Safe when nothing is
|
|
737
|
+
* registered. */
|
|
738
|
+
async unregisterWebPush(registration, opts = {}) {
|
|
739
|
+
const sub = await registration.pushManager.getSubscription();
|
|
740
|
+
if (sub) await sub.unsubscribe();
|
|
741
|
+
if (opts.deviceId) {
|
|
742
|
+
await this.remove(opts.deviceId);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const mine = (await this.list()).filter((d) => d.kind === "webpush" && d.is_current);
|
|
746
|
+
for (const d of mine) await this.remove(d.id);
|
|
747
|
+
}
|
|
748
|
+
async vapidPublicKey() {
|
|
749
|
+
const { vapid_public_key } = await this.http.get(
|
|
750
|
+
"/me/push-subscription"
|
|
751
|
+
);
|
|
752
|
+
return vapid_public_key ?? null;
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
// src/version.ts
|
|
757
|
+
var SDK_VERSION = "0.3.0";
|
|
758
|
+
|
|
759
|
+
// src/client-platform.ts
|
|
760
|
+
var CLIENT_HEADER = "X-Takeal-Client";
|
|
761
|
+
function detectClientPlatform() {
|
|
762
|
+
const g = globalThis;
|
|
763
|
+
const initData = g.Telegram?.WebApp?.initData;
|
|
764
|
+
if (typeof initData === "string" && initData.length > 0) return "telegram-miniapp";
|
|
765
|
+
if (g.navigator?.standalone === true) return "pwa";
|
|
766
|
+
if (typeof g.matchMedia === "function") {
|
|
767
|
+
try {
|
|
768
|
+
if (g.matchMedia("(display-mode: standalone)").matches) return "pwa";
|
|
769
|
+
} catch {
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return "web";
|
|
773
|
+
}
|
|
774
|
+
function resolveClientDescriptor(given) {
|
|
775
|
+
return {
|
|
776
|
+
platform: given?.platform ?? detectClientPlatform(),
|
|
777
|
+
version: given?.version || SDK_VERSION
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
function clientHeaderValue(desc) {
|
|
781
|
+
return `${desc.platform}; ${desc.version}`;
|
|
782
|
+
}
|
|
783
|
+
|
|
547
784
|
// src/token-store.ts
|
|
548
785
|
function defaultBrowserStore(key = "takeal_jwt") {
|
|
549
786
|
const fallback = inMemoryStore();
|
|
@@ -580,14 +817,30 @@ function inMemoryStore() {
|
|
|
580
817
|
};
|
|
581
818
|
}
|
|
582
819
|
|
|
820
|
+
// src/assets.ts
|
|
821
|
+
function resolveAssetUrl(baseUrl, url) {
|
|
822
|
+
if (isAbsolute(url)) return url;
|
|
823
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
824
|
+
const path = url.startsWith("/") ? url : `/${url}`;
|
|
825
|
+
return `${base}${path}`;
|
|
826
|
+
}
|
|
827
|
+
function isAbsolute(url) {
|
|
828
|
+
return /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(url);
|
|
829
|
+
}
|
|
830
|
+
|
|
583
831
|
// src/index.ts
|
|
584
832
|
function createClient(opts) {
|
|
585
833
|
const tokens = opts.tokenStore ?? pickDefaultStore();
|
|
586
834
|
const fetchImpl = opts.fetch ?? defaultFetch();
|
|
587
|
-
const
|
|
835
|
+
const client = resolveClientDescriptor(opts.client);
|
|
836
|
+
const http = new HttpClient(opts.baseUrl, tokens, fetchImpl, {
|
|
837
|
+
[CLIENT_HEADER]: clientHeaderValue(client)
|
|
838
|
+
});
|
|
588
839
|
return {
|
|
589
840
|
brand: opts.brand,
|
|
841
|
+
client,
|
|
590
842
|
tokens,
|
|
843
|
+
assetUrl: (url) => resolveAssetUrl(opts.baseUrl, url),
|
|
591
844
|
auth: new AuthResource(http, tokens),
|
|
592
845
|
deposits: new DepositsResource(http),
|
|
593
846
|
cards: new CardsResource(http),
|
|
@@ -597,7 +850,10 @@ function createClient(opts) {
|
|
|
597
850
|
blog: new BlogResource(http),
|
|
598
851
|
subscriptions: new SubscriptionsResource(http),
|
|
599
852
|
sessions: new SessionsResource(http),
|
|
600
|
-
push: new PushResource(http)
|
|
853
|
+
push: new PushResource(http),
|
|
854
|
+
me: new MeResource(http),
|
|
855
|
+
notifications: new NotificationsResource(http),
|
|
856
|
+
devices: new DevicesResource(http, client)
|
|
601
857
|
};
|
|
602
858
|
}
|
|
603
859
|
function pickDefaultStore() {
|
|
@@ -616,11 +872,15 @@ function defaultFetch() {
|
|
|
616
872
|
}
|
|
617
873
|
|
|
618
874
|
exports.ApiError = ApiError;
|
|
875
|
+
exports.CLIENT_HEADER = CLIENT_HEADER;
|
|
619
876
|
exports.NetworkError = NetworkError;
|
|
877
|
+
exports.SDK_VERSION = SDK_VERSION;
|
|
620
878
|
exports.createClient = createClient;
|
|
621
879
|
exports.defaultBrowserStore = defaultBrowserStore;
|
|
880
|
+
exports.detectClientPlatform = detectClientPlatform;
|
|
622
881
|
exports.inMemoryStore = inMemoryStore;
|
|
623
882
|
exports.isApiError = isApiError;
|
|
624
883
|
exports.isNetworkError = isNetworkError;
|
|
884
|
+
exports.resolveAssetUrl = resolveAssetUrl;
|
|
625
885
|
//# sourceMappingURL=index.cjs.map
|
|
626
886
|
//# sourceMappingURL=index.cjs.map
|