entity-server-client 1.0.1 → 1.0.2
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/dist/EntityServerClient.d.ts +85 -0
- package/dist/client/base.d.ts +15 -1
- package/dist/client/request.d.ts +4 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/dist/mixins/auth.d.ts +15 -0
- package/dist/mixins/entity.d.ts +14 -0
- package/dist/mixins/file.d.ts +14 -0
- package/dist/mixins/push.d.ts +14 -0
- package/dist/mixins/smtp.d.ts +14 -0
- package/dist/mixins/utils.d.ts +14 -0
- package/dist/react.js +1 -1
- package/dist/react.js.map +3 -3
- package/dist/types.d.ts +12 -0
- package/package.json +34 -3
- package/dist/mixins/alimtalk.d.ts +0 -56
- package/dist/mixins/email.d.ts +0 -51
- package/dist/mixins/identity.d.ts +0 -52
- package/dist/mixins/llm.d.ts +0 -94
- package/dist/mixins/pg.d.ts +0 -63
- package/dist/mixins/sms.d.ts +0 -55
|
@@ -32,6 +32,11 @@ declare const EntityServerClient_base: {
|
|
|
32
32
|
apiKey: string;
|
|
33
33
|
hmacSecret: string;
|
|
34
34
|
encryptRequests: boolean;
|
|
35
|
+
csrfEnabled: boolean;
|
|
36
|
+
csrfToken: string;
|
|
37
|
+
csrfHeaderName: string;
|
|
38
|
+
csrfRefreshPath: string;
|
|
39
|
+
csrfRefreshBuffer: number;
|
|
35
40
|
activeTxId: string | null;
|
|
36
41
|
keepSession: boolean;
|
|
37
42
|
refreshBuffer: number;
|
|
@@ -39,18 +44,27 @@ declare const EntityServerClient_base: {
|
|
|
39
44
|
onSessionExpired?: (error: Error) => void;
|
|
40
45
|
_sessionRefreshToken: string | null;
|
|
41
46
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
47
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
48
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
42
49
|
configure(options: Partial<import("./types").EntityServerClientOptions>): void;
|
|
43
50
|
setToken(token: string): void;
|
|
44
51
|
setAnonymousPacketToken(token: string): void;
|
|
45
52
|
setApiKey(apiKey: string): void;
|
|
46
53
|
setHmacSecret(secret: string): void;
|
|
47
54
|
setEncryptRequests(value: boolean): void;
|
|
55
|
+
setCsrfToken(token: string): void;
|
|
56
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
48
57
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
49
58
|
access_token: string;
|
|
50
59
|
expires_in: number;
|
|
51
60
|
}>): void;
|
|
52
61
|
_clearRefreshTimer(): void;
|
|
53
62
|
stopKeepSession(): void;
|
|
63
|
+
_clearCsrfRefreshTimer(): void;
|
|
64
|
+
stopCsrfRefresh(): void;
|
|
65
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
66
|
+
refreshCsrfToken(): Promise<string>;
|
|
67
|
+
_applyCsrfHealth(csrf?: import("./types").EntityServerClientHealthCsrf | null): void;
|
|
54
68
|
readRequestBody<T = Record<string, unknown>>(body: ArrayBuffer | Uint8Array | string | T | null | undefined, contentType?: string, requireEncrypted?: boolean): T;
|
|
55
69
|
get _reqOpts(): import("./client/request").RequestOptions;
|
|
56
70
|
requestJson<T>(method: string, path: string, body?: unknown, withAuth?: boolean, extraHeaders?: Record<string, string>): Promise<T>;
|
|
@@ -99,6 +113,11 @@ declare const EntityServerClient_base: {
|
|
|
99
113
|
apiKey: string;
|
|
100
114
|
hmacSecret: string;
|
|
101
115
|
encryptRequests: boolean;
|
|
116
|
+
csrfEnabled: boolean;
|
|
117
|
+
csrfToken: string;
|
|
118
|
+
csrfHeaderName: string;
|
|
119
|
+
csrfRefreshPath: string;
|
|
120
|
+
csrfRefreshBuffer: number;
|
|
102
121
|
activeTxId: string | null;
|
|
103
122
|
keepSession: boolean;
|
|
104
123
|
refreshBuffer: number;
|
|
@@ -106,18 +125,27 @@ declare const EntityServerClient_base: {
|
|
|
106
125
|
onSessionExpired?: (error: Error) => void;
|
|
107
126
|
_sessionRefreshToken: string | null;
|
|
108
127
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
128
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
129
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
109
130
|
configure(options: Partial<import("./types").EntityServerClientOptions>): void;
|
|
110
131
|
setToken(token: string): void;
|
|
111
132
|
setAnonymousPacketToken(token: string): void;
|
|
112
133
|
setApiKey(apiKey: string): void;
|
|
113
134
|
setHmacSecret(secret: string): void;
|
|
114
135
|
setEncryptRequests(value: boolean): void;
|
|
136
|
+
setCsrfToken(token: string): void;
|
|
137
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
115
138
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
116
139
|
access_token: string;
|
|
117
140
|
expires_in: number;
|
|
118
141
|
}>): void;
|
|
119
142
|
_clearRefreshTimer(): void;
|
|
120
143
|
stopKeepSession(): void;
|
|
144
|
+
_clearCsrfRefreshTimer(): void;
|
|
145
|
+
stopCsrfRefresh(): void;
|
|
146
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
147
|
+
refreshCsrfToken(): Promise<string>;
|
|
148
|
+
_applyCsrfHealth(csrf?: import("./types").EntityServerClientHealthCsrf | null): void;
|
|
121
149
|
readRequestBody<T = Record<string, unknown>>(body: ArrayBuffer | Uint8Array | string | T | null | undefined, contentType?: string, requireEncrypted?: boolean): T;
|
|
122
150
|
get _reqOpts(): import("./client/request").RequestOptions;
|
|
123
151
|
requestJson<T>(method: string, path: string, body?: unknown, withAuth?: boolean, extraHeaders?: Record<string, string>): Promise<T>;
|
|
@@ -145,6 +173,11 @@ declare const EntityServerClient_base: {
|
|
|
145
173
|
apiKey: string;
|
|
146
174
|
hmacSecret: string;
|
|
147
175
|
encryptRequests: boolean;
|
|
176
|
+
csrfEnabled: boolean;
|
|
177
|
+
csrfToken: string;
|
|
178
|
+
csrfHeaderName: string;
|
|
179
|
+
csrfRefreshPath: string;
|
|
180
|
+
csrfRefreshBuffer: number;
|
|
148
181
|
activeTxId: string | null;
|
|
149
182
|
keepSession: boolean;
|
|
150
183
|
refreshBuffer: number;
|
|
@@ -152,18 +185,27 @@ declare const EntityServerClient_base: {
|
|
|
152
185
|
onSessionExpired?: (error: Error) => void;
|
|
153
186
|
_sessionRefreshToken: string | null;
|
|
154
187
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
188
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
189
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
155
190
|
configure(options: Partial<import("./types").EntityServerClientOptions>): void;
|
|
156
191
|
setToken(token: string): void;
|
|
157
192
|
setAnonymousPacketToken(token: string): void;
|
|
158
193
|
setApiKey(apiKey: string): void;
|
|
159
194
|
setHmacSecret(secret: string): void;
|
|
160
195
|
setEncryptRequests(value: boolean): void;
|
|
196
|
+
setCsrfToken(token: string): void;
|
|
197
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
161
198
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
162
199
|
access_token: string;
|
|
163
200
|
expires_in: number;
|
|
164
201
|
}>): void;
|
|
165
202
|
_clearRefreshTimer(): void;
|
|
166
203
|
stopKeepSession(): void;
|
|
204
|
+
_clearCsrfRefreshTimer(): void;
|
|
205
|
+
stopCsrfRefresh(): void;
|
|
206
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
207
|
+
refreshCsrfToken(): Promise<string>;
|
|
208
|
+
_applyCsrfHealth(csrf?: import("./types").EntityServerClientHealthCsrf | null): void;
|
|
167
209
|
readRequestBody<T = Record<string, unknown>>(body: ArrayBuffer | Uint8Array | string | T | null | undefined, contentType?: string, requireEncrypted?: boolean): T;
|
|
168
210
|
get _reqOpts(): import("./client/request").RequestOptions;
|
|
169
211
|
requestJson<T>(method: string, path: string, body?: unknown, withAuth?: boolean, extraHeaders?: Record<string, string>): Promise<T>;
|
|
@@ -210,6 +252,11 @@ declare const EntityServerClient_base: {
|
|
|
210
252
|
apiKey: string;
|
|
211
253
|
hmacSecret: string;
|
|
212
254
|
encryptRequests: boolean;
|
|
255
|
+
csrfEnabled: boolean;
|
|
256
|
+
csrfToken: string;
|
|
257
|
+
csrfHeaderName: string;
|
|
258
|
+
csrfRefreshPath: string;
|
|
259
|
+
csrfRefreshBuffer: number;
|
|
213
260
|
activeTxId: string | null;
|
|
214
261
|
keepSession: boolean;
|
|
215
262
|
refreshBuffer: number;
|
|
@@ -217,18 +264,27 @@ declare const EntityServerClient_base: {
|
|
|
217
264
|
onSessionExpired?: (error: Error) => void;
|
|
218
265
|
_sessionRefreshToken: string | null;
|
|
219
266
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
267
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
268
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
220
269
|
configure(options: Partial<import("./types").EntityServerClientOptions>): void;
|
|
221
270
|
setToken(token: string): void;
|
|
222
271
|
setAnonymousPacketToken(token: string): void;
|
|
223
272
|
setApiKey(apiKey: string): void;
|
|
224
273
|
setHmacSecret(secret: string): void;
|
|
225
274
|
setEncryptRequests(value: boolean): void;
|
|
275
|
+
setCsrfToken(token: string): void;
|
|
276
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
226
277
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
227
278
|
access_token: string;
|
|
228
279
|
expires_in: number;
|
|
229
280
|
}>): void;
|
|
230
281
|
_clearRefreshTimer(): void;
|
|
231
282
|
stopKeepSession(): void;
|
|
283
|
+
_clearCsrfRefreshTimer(): void;
|
|
284
|
+
stopCsrfRefresh(): void;
|
|
285
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
286
|
+
refreshCsrfToken(): Promise<string>;
|
|
287
|
+
_applyCsrfHealth(csrf?: import("./types").EntityServerClientHealthCsrf | null): void;
|
|
232
288
|
readRequestBody<T = Record<string, unknown>>(body: ArrayBuffer | Uint8Array | string | T | null | undefined, contentType?: string, requireEncrypted?: boolean): T;
|
|
233
289
|
get _reqOpts(): import("./client/request").RequestOptions;
|
|
234
290
|
requestJson<T>(method: string, path: string, body?: unknown, withAuth?: boolean, extraHeaders?: Record<string, string>): Promise<T>;
|
|
@@ -320,6 +376,11 @@ declare const EntityServerClient_base: {
|
|
|
320
376
|
apiKey: string;
|
|
321
377
|
hmacSecret: string;
|
|
322
378
|
encryptRequests: boolean;
|
|
379
|
+
csrfEnabled: boolean;
|
|
380
|
+
csrfToken: string;
|
|
381
|
+
csrfHeaderName: string;
|
|
382
|
+
csrfRefreshPath: string;
|
|
383
|
+
csrfRefreshBuffer: number;
|
|
323
384
|
activeTxId: string | null;
|
|
324
385
|
keepSession: boolean;
|
|
325
386
|
refreshBuffer: number;
|
|
@@ -327,18 +388,27 @@ declare const EntityServerClient_base: {
|
|
|
327
388
|
onSessionExpired?: (error: Error) => void;
|
|
328
389
|
_sessionRefreshToken: string | null;
|
|
329
390
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
391
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
392
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
330
393
|
configure(options: Partial<import("./types").EntityServerClientOptions>): void;
|
|
331
394
|
setToken(token: string): void;
|
|
332
395
|
setAnonymousPacketToken(token: string): void;
|
|
333
396
|
setApiKey(apiKey: string): void;
|
|
334
397
|
setHmacSecret(secret: string): void;
|
|
335
398
|
setEncryptRequests(value: boolean): void;
|
|
399
|
+
setCsrfToken(token: string): void;
|
|
400
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
336
401
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
337
402
|
access_token: string;
|
|
338
403
|
expires_in: number;
|
|
339
404
|
}>): void;
|
|
340
405
|
_clearRefreshTimer(): void;
|
|
341
406
|
stopKeepSession(): void;
|
|
407
|
+
_clearCsrfRefreshTimer(): void;
|
|
408
|
+
stopCsrfRefresh(): void;
|
|
409
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
410
|
+
refreshCsrfToken(): Promise<string>;
|
|
411
|
+
_applyCsrfHealth(csrf?: import("./types").EntityServerClientHealthCsrf | null): void;
|
|
342
412
|
readRequestBody<T = Record<string, unknown>>(body: ArrayBuffer | Uint8Array | string | T | null | undefined, contentType?: string, requireEncrypted?: boolean): T;
|
|
343
413
|
get _reqOpts(): import("./client/request").RequestOptions;
|
|
344
414
|
requestJson<T>(method: string, path: string, body?: unknown, withAuth?: boolean, extraHeaders?: Record<string, string>): Promise<T>;
|
|
@@ -357,6 +427,7 @@ declare const EntityServerClient_base: {
|
|
|
357
427
|
packet_encryption?: boolean;
|
|
358
428
|
packet_mode?: string;
|
|
359
429
|
packet_token?: string;
|
|
430
|
+
csrf?: import("./types").EntityServerClientHealthCsrf;
|
|
360
431
|
}>;
|
|
361
432
|
login(email: string, password: string): Promise<{
|
|
362
433
|
access_token: string;
|
|
@@ -396,6 +467,11 @@ declare const EntityServerClient_base: {
|
|
|
396
467
|
apiKey: string;
|
|
397
468
|
hmacSecret: string;
|
|
398
469
|
encryptRequests: boolean;
|
|
470
|
+
csrfEnabled: boolean;
|
|
471
|
+
csrfToken: string;
|
|
472
|
+
csrfHeaderName: string;
|
|
473
|
+
csrfRefreshPath: string;
|
|
474
|
+
csrfRefreshBuffer: number;
|
|
399
475
|
activeTxId: string | null;
|
|
400
476
|
keepSession: boolean;
|
|
401
477
|
refreshBuffer: number;
|
|
@@ -403,18 +479,27 @@ declare const EntityServerClient_base: {
|
|
|
403
479
|
onSessionExpired?: (error: Error) => void;
|
|
404
480
|
_sessionRefreshToken: string | null;
|
|
405
481
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
482
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
483
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
406
484
|
configure(options: Partial<import("./types").EntityServerClientOptions>): void;
|
|
407
485
|
setToken(token: string): void;
|
|
408
486
|
setAnonymousPacketToken(token: string): void;
|
|
409
487
|
setApiKey(apiKey: string): void;
|
|
410
488
|
setHmacSecret(secret: string): void;
|
|
411
489
|
setEncryptRequests(value: boolean): void;
|
|
490
|
+
setCsrfToken(token: string): void;
|
|
491
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
412
492
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
413
493
|
access_token: string;
|
|
414
494
|
expires_in: number;
|
|
415
495
|
}>): void;
|
|
416
496
|
_clearRefreshTimer(): void;
|
|
417
497
|
stopKeepSession(): void;
|
|
498
|
+
_clearCsrfRefreshTimer(): void;
|
|
499
|
+
stopCsrfRefresh(): void;
|
|
500
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
501
|
+
refreshCsrfToken(): Promise<string>;
|
|
502
|
+
_applyCsrfHealth(csrf?: import("./types").EntityServerClientHealthCsrf | null): void;
|
|
418
503
|
readRequestBody<T = Record<string, unknown>>(body: ArrayBuffer | Uint8Array | string | T | null | undefined, contentType?: string, requireEncrypted?: boolean): T;
|
|
419
504
|
get _reqOpts(): import("./client/request").RequestOptions;
|
|
420
505
|
requestJson<T>(method: string, path: string, body?: unknown, withAuth?: boolean, extraHeaders?: Record<string, string>): Promise<T>;
|
package/dist/client/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EntityServerClientOptions } from "../types";
|
|
1
|
+
import type { EntityServerClientHealthCsrf, EntityServerClientOptions } from "../types";
|
|
2
2
|
import { type RequestOptions } from "./request";
|
|
3
3
|
export type GConstructor<T = object> = new (...args: any[]) => T;
|
|
4
4
|
export declare class EntityServerClientBase {
|
|
@@ -8,6 +8,11 @@ export declare class EntityServerClientBase {
|
|
|
8
8
|
apiKey: string;
|
|
9
9
|
hmacSecret: string;
|
|
10
10
|
encryptRequests: boolean;
|
|
11
|
+
csrfEnabled: boolean;
|
|
12
|
+
csrfToken: string;
|
|
13
|
+
csrfHeaderName: string;
|
|
14
|
+
csrfRefreshPath: string;
|
|
15
|
+
csrfRefreshBuffer: number;
|
|
11
16
|
activeTxId: string | null;
|
|
12
17
|
keepSession: boolean;
|
|
13
18
|
refreshBuffer: number;
|
|
@@ -15,6 +20,8 @@ export declare class EntityServerClientBase {
|
|
|
15
20
|
onSessionExpired?: (error: Error) => void;
|
|
16
21
|
_sessionRefreshToken: string | null;
|
|
17
22
|
_refreshTimer: ReturnType<typeof setTimeout> | null;
|
|
23
|
+
_csrfRefreshTimer: ReturnType<typeof setTimeout> | null;
|
|
24
|
+
_csrfRefreshPromise: Promise<string> | null;
|
|
18
25
|
/**
|
|
19
26
|
* EntityServerClient 인스턴스를 생성합니다.
|
|
20
27
|
*
|
|
@@ -34,6 +41,8 @@ export declare class EntityServerClientBase {
|
|
|
34
41
|
setHmacSecret(secret: string): void;
|
|
35
42
|
/** 암호화 요청 활성화 여부를 설정합니다. */
|
|
36
43
|
setEncryptRequests(value: boolean): void;
|
|
44
|
+
setCsrfToken(token: string): void;
|
|
45
|
+
setCsrfEnabled(enabled: boolean): void;
|
|
37
46
|
/** @internal 자동 토큰 갱신 타이머를 시작합니다. */
|
|
38
47
|
_scheduleKeepSession(refreshToken: string, expiresIn: number, refreshFn: (rt: string) => Promise<{
|
|
39
48
|
access_token: string;
|
|
@@ -46,6 +55,11 @@ export declare class EntityServerClientBase {
|
|
|
46
55
|
* `logout()` 호출 시 자동으로 중지되며, 직접 호출이 필요한 경우는 드뭅니다.
|
|
47
56
|
*/
|
|
48
57
|
stopKeepSession(): void;
|
|
58
|
+
_clearCsrfRefreshTimer(): void;
|
|
59
|
+
stopCsrfRefresh(): void;
|
|
60
|
+
_scheduleCsrfRefresh(expiresIn: number): void;
|
|
61
|
+
refreshCsrfToken(): Promise<string>;
|
|
62
|
+
_applyCsrfHealth(csrf?: EntityServerClientHealthCsrf | null): void;
|
|
49
63
|
/**
|
|
50
64
|
* 요청 바디를 파싱합니다.
|
|
51
65
|
* `application/octet-stream`이면 XChaCha20-Poly1305 복호화, 그 외는 JSON 파싱합니다.
|
package/dist/client/request.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ export interface RequestOptions {
|
|
|
5
5
|
apiKey: string;
|
|
6
6
|
hmacSecret: string;
|
|
7
7
|
encryptRequests: boolean;
|
|
8
|
+
csrfEnabled: boolean;
|
|
9
|
+
csrfToken: string;
|
|
10
|
+
csrfHeaderName: string;
|
|
11
|
+
refreshCsrfToken: (() => Promise<string>) | null;
|
|
8
12
|
}
|
|
9
13
|
/**
|
|
10
14
|
* Entity Server에 HTTP 요청을 보냅니다.
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function A(i){let r=import.meta;if(r?.env?.[i]!=null)return r.env[i];let e=globalThis.process;if(e?.env?.[i]!=null)return e.env[i]}function q(i){return Object.entries(i).filter(([,r])=>r!=null).map(([r,e])=>`${encodeURIComponent(r==="orderBy"?"order_by":r)}=${encodeURIComponent(String(e))}`).join("&")}import{xchacha20poly1305 as C}from"@noble/ciphers/chacha";import{sha256 as J}from"@noble/hashes/sha2";import{hkdf as z}from"@noble/hashes/hkdf";var v=32,E=2,w=14,m=24,V=16,Z="entity-server:hkdf:v1",W="entity-server:packet-encryption";function g(i){return i instanceof Uint8Array?i:new Uint8Array(i)}function O(i,r=W){return z(J,new TextEncoder().encode(i),new TextEncoder().encode(Z),new TextEncoder().encode(r),v)}function U(i,r=E,e=w){let t=g(i);return t.length<v?r:r+t[v-1]%e}function $(i,r,e=E,t=w){let n=g(i),s=g(r),o=U(s,e,t),a=crypto.getRandomValues(new Uint8Array(o)),c=crypto.getRandomValues(new Uint8Array(m)),l=C(s,c).encrypt(n),p=new Uint8Array(o+m+l.length);return p.set(a,0),p.set(c,o),p.set(l,o+m),p}function I(i,r,e=E,t=w){let n=g(i),s=g(r),o=U(s,e,t);if(n.length<o+m+V)throw new Error("Encrypted packet too short");let a=n.slice(o,o+m),c=n.slice(o+m);return C(s,a).decrypt(c)}function k(i,r){return O(i||r)}function K(i,r){return $(i,r)}function b(i,r){let e=I(i,r);return JSON.parse(new TextDecoder().decode(e))}function H(i,r,e,t){let n=r.toLowerCase().includes("application/octet-stream");if(e&&!n)throw new Error("Encrypted request required: Content-Type must be application/octet-stream");if(n){if(i==null)throw new Error("Encrypted request body is empty");if(i instanceof ArrayBuffer)return b(i,t);if(i instanceof Uint8Array){let s=i.buffer.slice(i.byteOffset,i.byteOffset+i.byteLength);return b(s,t)}throw new Error("Encrypted request body must be ArrayBuffer or Uint8Array")}return i==null||i===""?{}:typeof i=="string"?JSON.parse(i):i}import{sha256 as Y}from"@noble/hashes/sha2";import{hmac as ee}from"@noble/hashes/hmac";function M(i,r,e,t,n){let s=String(Math.floor(Date.now()/1e3)),o=crypto.randomUUID(),a=new TextEncoder().encode(`${i}|${r}|${s}|${o}|`),c=new Uint8Array(a.length+e.length);c.set(a,0),c.set(e,a.length);let l=[...ee(Y,new TextEncoder().encode(n),c)].map(p=>p.toString(16).padStart(2,"0")).join("");return{"X-API-Key":t,"X-Timestamp":s,"X-Nonce":o,"X-Signature":l}}function te(i){return i.hmacSecret||i.token||i.anonymousPacketToken}async function re(i){if((i.headers.get("Content-Type")??"").includes("application/json")){let t=await i.json().catch(()=>null);if(t?.error)return t.error;if(t?.message)return t.message}return await i.text().catch(()=>"")||`HTTP ${i.status}`}async function R(i,r,e,t,n=!0,s={},o=!0){let{baseUrl:a,token:c,apiKey:u,hmacSecret:l,encryptRequests:p,anonymousPacketToken:T}=i,S=n&&!!(u&&l),X=te(i),h={"Content-Type":"application/json",...s};!S&&n&&c&&(h.Authorization=`Bearer ${c}`),!c&&!S&&T&&(h["X-Packet-Token"]=T);let y=null;if(t!=null)if(p&&!!X&&r!=="GET"&&r!=="HEAD"){let Q=k(l,c||T);y=K(new TextEncoder().encode(JSON.stringify(t)),Q),h["Content-Type"]="application/octet-stream"}else y=JSON.stringify(t);if(S){let f=y instanceof Uint8Array?y:typeof y=="string"?new TextEncoder().encode(y):new Uint8Array(0);Object.assign(h,M(r,e,f,u,l))}let d=await fetch(a+e,{method:r,headers:h,...y!=null?{body:y}:{},credentials:"include"});if(!d.ok){let f=new Error(await re(d));throw f.status=d.status,f}let B=d.headers.get("Content-Type")??"";if(B.includes("application/octet-stream")){let f=k(l,c||T);return b(await d.arrayBuffer(),f)}if(!B.includes("application/json"))return await d.text();let x=await d.json();if(o&&!x.ok){let f=new Error(x.message??`EntityServer error (HTTP ${d.status})`);throw f.status=d.status,f}return x}var P=class{baseUrl;token;anonymousPacketToken;apiKey;hmacSecret;encryptRequests;activeTxId=null;keepSession;refreshBuffer;onTokenRefreshed;onSessionExpired;_sessionRefreshToken=null;_refreshTimer=null;constructor(r={}){let e=A("VITE_ENTITY_SERVER_URL");this.baseUrl=(r.baseUrl??e??"").replace(/\/$/,""),this.token=r.token??"",this.anonymousPacketToken=r.anonymousPacketToken??"",this.apiKey=r.apiKey??"",this.hmacSecret=r.hmacSecret??"",this.encryptRequests=r.encryptRequests??!1,this.keepSession=r.keepSession??!1,this.refreshBuffer=r.refreshBuffer??60,this.onTokenRefreshed=r.onTokenRefreshed,this.onSessionExpired=r.onSessionExpired}configure(r){typeof r.baseUrl=="string"&&(this.baseUrl=r.baseUrl.replace(/\/$/,"")),typeof r.token=="string"&&(this.token=r.token),typeof r.anonymousPacketToken=="string"&&(this.anonymousPacketToken=r.anonymousPacketToken),typeof r.encryptRequests=="boolean"&&(this.encryptRequests=r.encryptRequests),typeof r.apiKey=="string"&&(this.apiKey=r.apiKey),typeof r.hmacSecret=="string"&&(this.hmacSecret=r.hmacSecret),typeof r.keepSession=="boolean"&&(this.keepSession=r.keepSession),typeof r.refreshBuffer=="number"&&(this.refreshBuffer=r.refreshBuffer),r.onTokenRefreshed&&(this.onTokenRefreshed=r.onTokenRefreshed),r.onSessionExpired&&(this.onSessionExpired=r.onSessionExpired)}setToken(r){this.token=r}setAnonymousPacketToken(r){this.anonymousPacketToken=r}setApiKey(r){this.apiKey=r}setHmacSecret(r){this.hmacSecret=r}setEncryptRequests(r){this.encryptRequests=r}_scheduleKeepSession(r,e,t){this._clearRefreshTimer(),this._sessionRefreshToken=r;let n=Math.max((e-this.refreshBuffer)*1e3,0);this._refreshTimer=setTimeout(async()=>{if(this._sessionRefreshToken)try{let s=await t(this._sessionRefreshToken);this.onTokenRefreshed?.(s.access_token,s.expires_in),this._scheduleKeepSession(this._sessionRefreshToken,s.expires_in,t)}catch(s){this._clearRefreshTimer(),this.onSessionExpired?.(s instanceof Error?s:new Error(String(s)))}},n)}_clearRefreshTimer(){this._refreshTimer!==null&&(clearTimeout(this._refreshTimer),this._refreshTimer=null)}stopKeepSession(){this._clearRefreshTimer(),this._sessionRefreshToken=null}readRequestBody(r,e="application/json",t=!1){let n=k(this.hmacSecret,this.token||this.anonymousPacketToken);return H(r,e,t,n)}get _reqOpts(){return{baseUrl:this.baseUrl,token:this.token,anonymousPacketToken:this.anonymousPacketToken,apiKey:this.apiKey,hmacSecret:this.hmacSecret,encryptRequests:this.encryptRequests}}requestJson(r,e,t,n=!0,s){return R(this._reqOpts,r,e,t,n,s,!1)}requestBinary(r,e,t,n=!0){return this._requestBinary(r,e,t,n)}requestForm(r,e,t,n=!0){return this._requestForm(r,e,t,n)}requestFormBinary(r,e,t,n=!0){return this._requestFormBinary(r,e,t,n)}_request(r,e,t,n=!0,s){return R(this._reqOpts,r,e,t,n,s,!0)}async _requestBinary(r,e,t,n=!0){let s={"Content-Type":"application/json"};n&&this.token&&(s.Authorization=`Bearer ${this.token}`),this.apiKey&&(s["X-API-Key"]=this.apiKey);let o=await fetch(this.baseUrl+e,{method:r,headers:s,...t!=null?{body:JSON.stringify(t)}:{},credentials:"include"});if(!o.ok){let a=await o.text(),c=new Error(`HTTP ${o.status}: ${a}`);throw c.status=o.status,c}return o.arrayBuffer()}async _requestForm(r,e,t,n=!0){let s={};n&&this.token&&(s.Authorization=`Bearer ${this.token}`),this.apiKey&&(s["X-API-Key"]=this.apiKey);let o=await fetch(this.baseUrl+e,{method:r,headers:s,body:t,credentials:"include"}),a=await o.json();if(!a.ok){let c=new Error(a.message??`EntityServer error (HTTP ${o.status})`);throw c.status=o.status,c}return a}async _requestFormBinary(r,e,t,n=!0){let s={};n&&this.token&&(s.Authorization=`Bearer ${this.token}`),this.apiKey&&(s["X-API-Key"]=this.apiKey);let o=await fetch(this.baseUrl+e,{method:r,headers:s,body:t,credentials:"include"});if(!o.ok){let a=await o.text(),c=new Error(`HTTP ${o.status}: ${a}`);throw c.status=o.status,c}return o.arrayBuffer()}};function F(i){return class extends i{async checkHealth(){let t=await(await fetch(`${this.baseUrl}/v1/health`,{signal:AbortSignal.timeout(3e3),credentials:"include"})).json();return t.packet_encryption&&(this.encryptRequests=!0),typeof t.packet_token=="string"&&(this.anonymousPacketToken=t.packet_token),t}async login(e,t){let n=await this._request("POST","/v1/auth/login",{email:e,passwd:t},!1);return this.token=n.data.access_token,this.keepSession&&this._scheduleKeepSession(n.data.refresh_token,n.data.expires_in,s=>this.refreshToken(s)),n.data}async refreshToken(e){let t=await this._request("POST","/v1/auth/refresh",{refresh_token:e},!1);return this.token=t.data.access_token,this.keepSession&&this._scheduleKeepSession(e,t.data.expires_in,n=>this.refreshToken(n)),t.data}async logout(e){this.stopKeepSession();let t=await this._request("POST","/v1/auth/logout",{refresh_token:e},!1);return this.token="",t}me(){return this._request("GET","/v1/auth/me")}withdraw(e){return this._request("POST","/v1/auth/withdraw",e?{passwd:e}:{})}reactivate(e){return this._request("POST","/v1/auth/reactivate",e,!1)}}}function L(i){return class extends i{async transStart(){let e=await this._request("POST","/v1/transaction/start",void 0,!1);return this.activeTxId=e.transaction_id,this.activeTxId}transRollback(e){let t=e??this.activeTxId;return t?(this.activeTxId=null,this._request("POST",`/v1/transaction/rollback/${t}`)):Promise.reject(new Error("No active transaction. Call transStart() first."))}transCommit(e){let t=e??this.activeTxId;return t?(this.activeTxId=null,this._request("POST",`/v1/transaction/commit/${t}`)):Promise.reject(new Error("No active transaction. Call transStart() first."))}get(e,t,n={}){let s=n.skipHooks?"?skipHooks=true":"";return this._request("GET",`/v1/entity/${e}/${t}${s}`)}find(e,t,n={}){let s=n.skipHooks?"?skipHooks=true":"";return this._request("POST",`/v1/entity/${e}/find${s}`,t??{})}list(e,t={}){let{conditions:n,fields:s,orderDir:o,orderBy:a,...c}=t,u={page:1,limit:20,...c};return a&&(u.orderBy=o==="DESC"?`-${a}`:a),s?.length&&(u.fields=s.join(",")),this._request("POST",`/v1/entity/${e}/list?${q(u)}`,n??{})}count(e,t){return this._request("POST",`/v1/entity/${e}/count`,t??{})}query(e,t){return this._request("POST",`/v1/entity/${e}/query`,t)}submit(e,t,n={}){let s=n.transactionId??this.activeTxId,o=s?{"X-Transaction-ID":s}:void 0,a=n.skipHooks?"?skipHooks=true":"";return this._request("POST",`/v1/entity/${e}/submit${a}`,t,!0,o)}delete(e,t,n={}){let s=new URLSearchParams;n.hard&&s.set("hard","true"),n.skipHooks&&s.set("skipHooks","true");let o=s.size?`?${s}`:"",a=n.transactionId??this.activeTxId,c=a?{"X-Transaction-ID":a}:void 0;return this._request("POST",`/v1/entity/${e}/delete/${t}${o}`,void 0,!0,c)}history(e,t,n={}){return this._request("GET",`/v1/entity/${e}/history/${t}?${q({page:1,limit:50,...n})}`)}rollback(e,t){return this._request("POST",`/v1/entity/${e}/rollback/${t}`)}}}function D(i){return class extends i{push(e,t,n={}){return this.submit(e,t,n)}pushLogList(e={}){return this.list("push_log",e)}registerPushDevice(e,t,n,s={}){let{platform:o,deviceType:a,browser:c,browserVersion:u,pushEnabled:l=!0,transactionId:p}=s;return this.submit("account_device",{id:t,account_seq:e,push_token:n,push_enabled:l,...o?{platform:o}:{},...a?{device_type:a}:{},...c?{browser:c}:{},...u?{browser_version:u}:{}},{transactionId:p})}updatePushDeviceToken(e,t,n={}){let{pushEnabled:s=!0,transactionId:o}=n;return this.submit("account_device",{seq:e,push_token:t,push_enabled:s},{transactionId:o})}disablePushDevice(e,t={}){return this.submit("account_device",{seq:e,push_enabled:!1},{transactionId:t.transactionId})}}}function G(i){return class extends i{smtpSend(e){return this._request("POST","/v1/smtp/send",e)}smtpStatus(e){return this._request("POST",`/v1/smtp/status/${e}`,{})}}}function j(i){return class extends i{async fileUpload(e,t,n={}){let s=new FormData;return s.append("file",t,t instanceof File?t.name:"upload"),n.refSeq!=null&&s.append("ref_seq",String(n.refSeq)),n.isPublic!=null&&s.append("is_public",n.isPublic?"true":"false"),this._requestForm("POST",`/v1/files/${e}/upload`,s)}fileDownload(e,t){return this._requestBinary("POST",`/v1/files/${e}/download/${t}`,{})}fileDelete(e,t){return this._request("POST",`/v1/files/${e}/delete/${t}`,{})}fileList(e,t={}){return this._request("POST",`/v1/files/${e}/list`,t.refSeq?{ref_seq:t.refSeq}:{})}fileMeta(e,t){return this._request("POST",`/v1/files/${e}/meta/${t}`,{})}fileToken(e){return this._request("POST",`/v1/files/token/${e}`,{})}fileUrl(e){return`${this.baseUrl}/v1/files/${e}`}}}function N(i){return class extends i{qrcode(e,t={}){return this._requestBinary("POST","/v1/utils/qrcode",{content:e,...t})}qrcodeBase64(e,t={}){return this._request("POST","/v1/utils/qrcode/base64",{content:e,...t})}qrcodeText(e,t={}){return this._request("POST","/v1/utils/qrcode/text",{content:e,...t})}barcode(e,t={}){return this._requestBinary("POST","/v1/utils/barcode",{content:e,...t})}pdf2png(e,t={}){let n=new FormData;n.append("file",new Blob([e],{type:"application/pdf"}),"document.pdf");let s=new URLSearchParams;t.dpi!=null&&s.set("dpi",String(t.dpi)),t.firstPage!=null&&s.set("first_page",String(t.firstPage)),t.lastPage!=null&&s.set("last_page",String(t.lastPage));let o=s.toString(),a="/v1/utils/pdf2png"+(o?`?${o}`:"");return this._requestFormBinary("POST",a,n)}}}var _=class extends N(j(G(D(L(F(P)))))){};var De=new _;export{_ as EntityServerClient,De as entityServer};
|
|
1
|
+
function H(s){let e=import.meta;if(e?.env?.[s]!=null)return e.env[s];let t=globalThis.process;if(t?.env?.[s]!=null)return t.env[s]}function w(s){return Object.entries(s).filter(([,e])=>e!=null).map(([e,t])=>`${encodeURIComponent(e==="orderBy"?"order_by":e)}=${encodeURIComponent(String(t))}`).join("&")}import{xchacha20poly1305 as I}from"@noble/ciphers/chacha";import{sha256 as ne}from"@noble/hashes/sha2";import{hkdf as ie}from"@noble/hashes/hkdf";var q=32,C=2,B=14,y=24,oe=16,ae="entity-server:hkdf:v1",ce="entity-server:packet-encryption";function g(s){return s instanceof Uint8Array?s:new Uint8Array(s)}function K(s,e=ce){return ie(ne,new TextEncoder().encode(s),new TextEncoder().encode(ae),new TextEncoder().encode(e),q)}function M(s,e=C,t=B){let r=g(s);return r.length<q?e:e+r[q-1]%t}function F(s,e,t=C,r=B){let n=g(s),i=g(e),o=M(i,t,r),a=crypto.getRandomValues(new Uint8Array(o)),c=crypto.getRandomValues(new Uint8Array(y)),h=I(i,c).encrypt(n),d=new Uint8Array(o+y+h.length);return d.set(a,0),d.set(c,o),d.set(h,o+y),d}function N(s,e,t=C,r=B){let n=g(s),i=g(e),o=M(i,t,r);if(n.length<o+y+oe)throw new Error("Encrypted packet too short");let a=n.slice(o,o+y),c=n.slice(o+y);return I(i,a).decrypt(c)}function T(s,e){return K(s||e)}function L(s,e){return F(s,e)}function S(s,e){let t=N(s,e);return JSON.parse(new TextDecoder().decode(t))}function D(s,e,t,r){let n=e.toLowerCase().includes("application/octet-stream");if(t&&!n)throw new Error("Encrypted request required: Content-Type must be application/octet-stream");if(n){if(s==null)throw new Error("Encrypted request body is empty");if(s instanceof ArrayBuffer)return S(s,r);if(s instanceof Uint8Array){let i=s.buffer.slice(s.byteOffset,s.byteOffset+s.byteLength);return S(i,r)}throw new Error("Encrypted request body must be ArrayBuffer or Uint8Array")}return s==null||s===""?{}:typeof s=="string"?JSON.parse(s):s}import{sha256 as ue}from"@noble/hashes/sha2";import{hmac as fe}from"@noble/hashes/hmac";function G(s,e,t,r,n){let i=String(Math.floor(Date.now()/1e3)),o=crypto.randomUUID(),a=new TextEncoder().encode(`${s}|${e}|${i}|${o}|`),c=new Uint8Array(a.length+t.length);c.set(a,0),c.set(t,a.length);let h=[...fe(ue,new TextEncoder().encode(n),c)].map(d=>d.toString(16).padStart(2,"0")).join("");return{"X-API-Key":r,"X-Timestamp":i,"X-Nonce":o,"X-Signature":h}}function le(s){return s.hmacSecret||s.token||s.anonymousPacketToken}function he(s){return s!=="GET"&&s!=="HEAD"&&s!=="OPTIONS"}function de(s,e){return s===403&&/csrf/i.test(e)?!0:/csrf/i.test(e)&&/expired|token validation failed/i.test(e)}async function j(s){if((s.headers.get("Content-Type")??"").includes("application/json")){let r=await s.json().catch(()=>null);if(r?.error)return r.error;if(r?.message)return r.message}return await s.text().catch(()=>"")||`HTTP ${s.status}`}async function A(s,e,t,r,n=!0,i={},o=!0){let{baseUrl:a,token:c,apiKey:l,hmacSecret:h,encryptRequests:d,anonymousPacketToken:b,csrfEnabled:W,csrfHeaderName:Y,refreshCsrfToken:P}=s,_=n&&!!(l&&h),ee=le(s),R=W&&he(e)&&!_,k=s.csrfToken,O="application/json",te=!c&&!_&&!!b,m=null;if(r!=null)if(d&&!!ee&&e!=="GET"&&e!=="HEAD"){let p=T(h,c||b);m=L(new TextEncoder().encode(JSON.stringify(r)),p),O="application/octet-stream"}else m=JSON.stringify(r);let re=u=>{let p={"Content-Type":O,...i};if(!_&&n&&c&&(p.Authorization=`Bearer ${c}`),te&&(p["X-Packet-Token"]=b),R&&u&&(p[Y]=u),_){let se=m instanceof Uint8Array?m:typeof m=="string"?new TextEncoder().encode(m):new Uint8Array(0);Object.assign(p,G(e,t,se,l,h))}return p};R&&!k&&P&&(k=await P());let U=u=>fetch(a+t,{method:e,headers:re(u),...m!=null?{body:m}:{},credentials:"include"}),f=await U(k);if(!f.ok){let u=await j(f.clone());if(R&&P&&de(f.status,u))k=await P(),f=await U(k);else{let p=new Error(u);throw p.status=f.status,p}}if(!f.ok){let u=new Error(await j(f));throw u.status=f.status,u}let $=f.headers.get("Content-Type")??"";if($.includes("application/octet-stream")){let u=T(h,c||b);return S(await f.arrayBuffer(),u)}if(!$.includes("application/json"))return await f.text();let v=await f.json();if(o&&!v.ok){let u=new Error(v.message??`EntityServer error (HTTP ${f.status})`);throw u.status=f.status,u}return v}var x=class{baseUrl;token;anonymousPacketToken;apiKey;hmacSecret;encryptRequests;csrfEnabled;csrfToken;csrfHeaderName;csrfRefreshPath;csrfRefreshBuffer;activeTxId=null;keepSession;refreshBuffer;onTokenRefreshed;onSessionExpired;_sessionRefreshToken=null;_refreshTimer=null;_csrfRefreshTimer=null;_csrfRefreshPromise=null;constructor(e={}){let t=H("VITE_ENTITY_SERVER_URL");this.baseUrl=(e.baseUrl??t??"").replace(/\/$/,""),this.token=e.token??"",this.anonymousPacketToken=e.anonymousPacketToken??"",this.apiKey=e.apiKey??"",this.hmacSecret=e.hmacSecret??"",this.encryptRequests=e.encryptRequests??!1,this.csrfEnabled=e.csrfEnabled??!1,this.csrfToken=e.csrfToken??"",this.csrfHeaderName=e.csrfHeaderName??"x-csrf-token",this.csrfRefreshPath=e.csrfRefreshPath??"/v1/csrf-token",this.csrfRefreshBuffer=e.csrfRefreshBuffer??60,this.keepSession=e.keepSession??!1,this.refreshBuffer=e.refreshBuffer??60,this.onTokenRefreshed=e.onTokenRefreshed,this.onSessionExpired=e.onSessionExpired}configure(e){typeof e.baseUrl=="string"&&(this.baseUrl=e.baseUrl.replace(/\/$/,"")),typeof e.token=="string"&&(this.token=e.token),typeof e.anonymousPacketToken=="string"&&(this.anonymousPacketToken=e.anonymousPacketToken),typeof e.encryptRequests=="boolean"&&(this.encryptRequests=e.encryptRequests),typeof e.csrfEnabled=="boolean"&&(this.csrfEnabled=e.csrfEnabled,e.csrfEnabled||(this.csrfToken="",this.stopCsrfRefresh())),typeof e.csrfToken=="string"&&(this.csrfToken=e.csrfToken),typeof e.csrfHeaderName=="string"&&(this.csrfHeaderName=e.csrfHeaderName),typeof e.csrfRefreshPath=="string"&&(this.csrfRefreshPath=e.csrfRefreshPath),typeof e.csrfRefreshBuffer=="number"&&(this.csrfRefreshBuffer=e.csrfRefreshBuffer),typeof e.apiKey=="string"&&(this.apiKey=e.apiKey),typeof e.hmacSecret=="string"&&(this.hmacSecret=e.hmacSecret),typeof e.keepSession=="boolean"&&(this.keepSession=e.keepSession),typeof e.refreshBuffer=="number"&&(this.refreshBuffer=e.refreshBuffer),e.onTokenRefreshed&&(this.onTokenRefreshed=e.onTokenRefreshed),e.onSessionExpired&&(this.onSessionExpired=e.onSessionExpired)}setToken(e){this.token=e}setAnonymousPacketToken(e){this.anonymousPacketToken=e}setApiKey(e){this.apiKey=e}setHmacSecret(e){this.hmacSecret=e}setEncryptRequests(e){this.encryptRequests=e}setCsrfToken(e){this.csrfToken=e}setCsrfEnabled(e){this.csrfEnabled=e,e||(this.csrfToken="",this.stopCsrfRefresh())}_scheduleKeepSession(e,t,r){this._clearRefreshTimer(),this._sessionRefreshToken=e;let n=Math.max((t-this.refreshBuffer)*1e3,0);this._refreshTimer=setTimeout(async()=>{if(this._sessionRefreshToken)try{let i=await r(this._sessionRefreshToken);this.onTokenRefreshed?.(i.access_token,i.expires_in),this._scheduleKeepSession(this._sessionRefreshToken,i.expires_in,r)}catch(i){this._clearRefreshTimer(),this.onSessionExpired?.(i instanceof Error?i:new Error(String(i)))}},n)}_clearRefreshTimer(){this._refreshTimer!==null&&(clearTimeout(this._refreshTimer),this._refreshTimer=null)}stopKeepSession(){this._clearRefreshTimer(),this._sessionRefreshToken=null}_clearCsrfRefreshTimer(){this._csrfRefreshTimer!==null&&(clearTimeout(this._csrfRefreshTimer),this._csrfRefreshTimer=null)}stopCsrfRefresh(){this._clearCsrfRefreshTimer(),this._csrfRefreshPromise=null}_scheduleCsrfRefresh(e){if(this._clearCsrfRefreshTimer(),!this.csrfEnabled||!this.csrfRefreshPath)return;let t=Math.max((e-this.csrfRefreshBuffer)*1e3,0);this._csrfRefreshTimer=setTimeout(()=>{this.refreshCsrfToken().catch(()=>{this._clearCsrfRefreshTimer()})},t)}async refreshCsrfToken(){return!this.csrfEnabled||!this.csrfRefreshPath?"":(this._csrfRefreshPromise||(this._csrfRefreshPromise=(async()=>{let e=await fetch(`${this.baseUrl}${this.csrfRefreshPath}`,{method:"GET",credentials:"include"});if(!e.ok){let n=await e.text().catch(()=>""),i=new Error(n||`HTTP ${e.status}`);throw i.status=e.status,i}let t=await e.json().catch(()=>null),r=t&&typeof t=="object"&&"data"in t?t.data??null:t;if(!r?.enabled||typeof r.token!="string")throw new Error("CSRF token refresh failed");return this._applyCsrfHealth(r),this.csrfToken})().finally(()=>{this._csrfRefreshPromise=null})),this._csrfRefreshPromise)}_applyCsrfHealth(e){if(!e?.enabled){this.setCsrfEnabled(!1);return}this.csrfEnabled=!0,typeof e.token=="string"&&(this.csrfToken=e.token),typeof e.headerName=="string"&&(this.csrfHeaderName=e.headerName),typeof e.refreshPath=="string"&&(this.csrfRefreshPath=e.refreshPath),typeof e.expiresIn=="number"&&e.expiresIn>0&&this._scheduleCsrfRefresh(e.expiresIn)}readRequestBody(e,t="application/json",r=!1){let n=T(this.hmacSecret,this.token||this.anonymousPacketToken);return D(e,t,r,n)}get _reqOpts(){return{baseUrl:this.baseUrl,token:this.token,anonymousPacketToken:this.anonymousPacketToken,apiKey:this.apiKey,hmacSecret:this.hmacSecret,encryptRequests:this.encryptRequests,csrfEnabled:this.csrfEnabled,csrfToken:this.csrfToken,csrfHeaderName:this.csrfHeaderName,refreshCsrfToken:this.csrfEnabled?()=>this.refreshCsrfToken():null}}requestJson(e,t,r,n=!0,i){return A(this._reqOpts,e,t,r,n,i,!1)}requestBinary(e,t,r,n=!0){return this._requestBinary(e,t,r,n)}requestForm(e,t,r,n=!0){return this._requestForm(e,t,r,n)}requestFormBinary(e,t,r,n=!0){return this._requestFormBinary(e,t,r,n)}_request(e,t,r,n=!0,i){return A(this._reqOpts,e,t,r,n,i,!0)}async _requestBinary(e,t,r,n=!0){let i={"Content-Type":"application/json"};n&&this.token&&(i.Authorization=`Bearer ${this.token}`),this.apiKey&&(i["X-API-Key"]=this.apiKey);let o=await fetch(this.baseUrl+t,{method:e,headers:i,...r!=null?{body:JSON.stringify(r)}:{},credentials:"include"});if(!o.ok){let a=await o.text(),c=new Error(`HTTP ${o.status}: ${a}`);throw c.status=o.status,c}return o.arrayBuffer()}async _requestForm(e,t,r,n=!0){let i={};n&&this.token&&(i.Authorization=`Bearer ${this.token}`),this.apiKey&&(i["X-API-Key"]=this.apiKey);let o=await fetch(this.baseUrl+t,{method:e,headers:i,body:r,credentials:"include"}),a=await o.json();if(!a.ok){let c=new Error(a.message??`EntityServer error (HTTP ${o.status})`);throw c.status=o.status,c}return a}async _requestFormBinary(e,t,r,n=!0){let i={};n&&this.token&&(i.Authorization=`Bearer ${this.token}`),this.apiKey&&(i["X-API-Key"]=this.apiKey);let o=await fetch(this.baseUrl+t,{method:e,headers:i,body:r,credentials:"include"});if(!o.ok){let a=await o.text(),c=new Error(`HTTP ${o.status}: ${a}`);throw c.status=o.status,c}return o.arrayBuffer()}};function X(s){return class extends s{async checkHealth(){let r=await(await fetch(`${this.baseUrl}/v1/health`,{signal:AbortSignal.timeout(3e3),credentials:"include"})).json();return r.packet_encryption&&(this.encryptRequests=!0),typeof r.packet_token=="string"&&(this.anonymousPacketToken=r.packet_token),this._applyCsrfHealth(r.csrf),r}async login(t,r){let n=await this._request("POST","/v1/auth/login",{email:t,passwd:r},!1);return this.token=n.data.access_token,this.keepSession&&this._scheduleKeepSession(n.data.refresh_token,n.data.expires_in,i=>this.refreshToken(i)),n.data}async refreshToken(t){let r=await this._request("POST","/v1/auth/refresh",{refresh_token:t},!1);return this.token=r.data.access_token,this.keepSession&&this._scheduleKeepSession(t,r.data.expires_in,n=>this.refreshToken(n)),r.data}async logout(t){this.stopKeepSession();let r=await this._request("POST","/v1/auth/logout",{refresh_token:t},!1);return this.token="",r}me(){return this._request("GET","/v1/auth/me")}withdraw(t){return this._request("POST","/v1/auth/withdraw",t?{passwd:t}:{})}reactivate(t){return this._request("POST","/v1/auth/reactivate",t,!1)}}}function Q(s){return class extends s{async transStart(){let t=await this._request("POST","/v1/transaction/start",void 0,!1);return this.activeTxId=t.transaction_id,this.activeTxId}transRollback(t){let r=t??this.activeTxId;return r?(this.activeTxId=null,this._request("POST",`/v1/transaction/rollback/${r}`)):Promise.reject(new Error("No active transaction. Call transStart() first."))}transCommit(t){let r=t??this.activeTxId;return r?(this.activeTxId=null,this._request("POST",`/v1/transaction/commit/${r}`)):Promise.reject(new Error("No active transaction. Call transStart() first."))}get(t,r,n={}){let i=n.skipHooks?"?skipHooks=true":"";return this._request("GET",`/v1/entity/${t}/${r}${i}`)}find(t,r,n={}){let i=n.skipHooks?"?skipHooks=true":"";return this._request("POST",`/v1/entity/${t}/find${i}`,r??{})}list(t,r={}){let{conditions:n,fields:i,orderDir:o,orderBy:a,...c}=r,l={page:1,limit:20,...c};return a&&(l.orderBy=o==="DESC"?`-${a}`:a),i?.length&&(l.fields=i.join(",")),this._request("POST",`/v1/entity/${t}/list?${w(l)}`,n??{})}count(t,r){return this._request("POST",`/v1/entity/${t}/count`,r??{})}query(t,r){return this._request("POST",`/v1/entity/${t}/query`,r)}submit(t,r,n={}){let i=n.transactionId??this.activeTxId,o=i?{"X-Transaction-ID":i}:void 0,a=n.skipHooks?"?skipHooks=true":"";return this._request("POST",`/v1/entity/${t}/submit${a}`,r,!0,o)}delete(t,r,n={}){let i=new URLSearchParams;n.hard&&i.set("hard","true"),n.skipHooks&&i.set("skipHooks","true");let o=i.size?`?${i}`:"",a=n.transactionId??this.activeTxId,c=a?{"X-Transaction-ID":a}:void 0;return this._request("POST",`/v1/entity/${t}/delete/${r}${o}`,void 0,!0,c)}history(t,r,n={}){return this._request("GET",`/v1/entity/${t}/history/${r}?${w({page:1,limit:50,...n})}`)}rollback(t,r){return this._request("POST",`/v1/entity/${t}/rollback/${r}`)}}}function J(s){return class extends s{push(t,r,n={}){return this.submit(t,r,n)}pushLogList(t={}){return this.list("push_log",t)}registerPushDevice(t,r,n,i={}){let{platform:o,deviceType:a,browser:c,browserVersion:l,pushEnabled:h=!0,transactionId:d}=i;return this.submit("account_device",{id:r,account_seq:t,push_token:n,push_enabled:h,...o?{platform:o}:{},...a?{device_type:a}:{},...c?{browser:c}:{},...l?{browser_version:l}:{}},{transactionId:d})}updatePushDeviceToken(t,r,n={}){let{pushEnabled:i=!0,transactionId:o}=n;return this.submit("account_device",{seq:t,push_token:r,push_enabled:i},{transactionId:o})}disablePushDevice(t,r={}){return this.submit("account_device",{seq:t,push_enabled:!1},{transactionId:r.transactionId})}}}function z(s){return class extends s{smtpSend(t){return this._request("POST","/v1/smtp/send",t)}smtpStatus(t){return this._request("POST",`/v1/smtp/status/${t}`,{})}}}function V(s){return class extends s{async fileUpload(t,r,n={}){let i=new FormData;return i.append("file",r,r instanceof File?r.name:"upload"),n.refSeq!=null&&i.append("ref_seq",String(n.refSeq)),n.isPublic!=null&&i.append("is_public",n.isPublic?"true":"false"),this._requestForm("POST",`/v1/files/${t}/upload`,i)}fileDownload(t,r){return this._requestBinary("POST",`/v1/files/${t}/download/${r}`,{})}fileDelete(t,r){return this._request("POST",`/v1/files/${t}/delete/${r}`,{})}fileList(t,r={}){return this._request("POST",`/v1/files/${t}/list`,r.refSeq?{ref_seq:r.refSeq}:{})}fileMeta(t,r){return this._request("POST",`/v1/files/${t}/meta/${r}`,{})}fileToken(t){return this._request("POST",`/v1/files/token/${t}`,{})}fileUrl(t){return`${this.baseUrl}/v1/files/${t}`}}}function Z(s){return class extends s{qrcode(t,r={}){return this._requestBinary("POST","/v1/utils/qrcode",{content:t,...r})}qrcodeBase64(t,r={}){return this._request("POST","/v1/utils/qrcode/base64",{content:t,...r})}qrcodeText(t,r={}){return this._request("POST","/v1/utils/qrcode/text",{content:t,...r})}barcode(t,r={}){return this._requestBinary("POST","/v1/utils/barcode",{content:t,...r})}pdf2png(t,r={}){let n=new FormData;n.append("file",new Blob([t],{type:"application/pdf"}),"document.pdf");let i=new URLSearchParams;r.dpi!=null&&i.set("dpi",String(r.dpi)),r.firstPage!=null&&i.set("first_page",String(r.firstPage)),r.lastPage!=null&&i.set("last_page",String(r.lastPage));let o=i.toString(),a="/v1/utils/pdf2png"+(o?`?${o}`:"");return this._requestFormBinary("POST",a,n)}}}var E=class extends Z(V(z(J(Q(X(x)))))){};var Ye=new E;export{E as EntityServerClient,Ye as entityServer};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|