@etsoo/appscript 1.2.85 → 1.2.86

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.
@@ -0,0 +1,525 @@
1
+ import {
2
+ INotifier,
3
+ NotificationAlign,
4
+ NotificationCallProps,
5
+ NotificationContent,
6
+ NotificationReturn
7
+ } from '@etsoo/notificationbase';
8
+ import { ApiDataError, IApi, IPData } from '@etsoo/restclient';
9
+ import { DataTypes, DateUtils, IStorage, ListType } from '@etsoo/shared';
10
+ import { AddressRegion } from '../address/AddressRegion';
11
+ import { ProductUnit } from '../business/ProductUnit';
12
+ import { IActionResult } from '../result/IActionResult';
13
+ import { IUser } from '../state/User';
14
+ import { IAppSettings } from './AppSettings';
15
+ import { UserRole } from './UserRole';
16
+
17
+ /**
18
+ * Detect IP callback interface
19
+ */
20
+ export interface IDetectIPCallback {
21
+ (): void;
22
+ }
23
+
24
+ /**
25
+ * Refresh token result type
26
+ * true means success, false means failed but no any message
27
+ * other cases means failed with differnet message
28
+ */
29
+ export type RefreshTokenResult =
30
+ | boolean
31
+ | string
32
+ | ApiDataError
33
+ | IActionResult;
34
+
35
+ /**
36
+ * Refresh token props
37
+ */
38
+ export interface RefreshTokenProps<D extends object> {
39
+ /**
40
+ * Callback
41
+ */
42
+ callback?: (result: RefreshTokenResult, successData?: string) => void;
43
+
44
+ /**
45
+ * Data to pass
46
+ */
47
+ data?: D;
48
+
49
+ /**
50
+ * Support relogin or not
51
+ */
52
+ relogin?: boolean;
53
+
54
+ /**
55
+ * Show loading bar or not
56
+ */
57
+ showLoading?: boolean;
58
+ }
59
+
60
+ /**
61
+ * App fields
62
+ */
63
+ export const appFields = [
64
+ 'headerToken',
65
+ 'serversideDeviceId',
66
+ 'deviceId',
67
+ 'devices',
68
+ 'devicePassphrase'
69
+ ] as const;
70
+
71
+ /**
72
+ * Basic type template
73
+ */
74
+ export type IAppFields = { [key in typeof appFields[number]]: string };
75
+
76
+ /**
77
+ * Application interface, for generic version, see ICoreApp
78
+ */
79
+ export interface IApp {
80
+ /**
81
+ * Settings
82
+ */
83
+ readonly settings: IAppSettings;
84
+
85
+ /**
86
+ * Fields
87
+ */
88
+ readonly fields: IAppFields;
89
+
90
+ /**
91
+ * API
92
+ */
93
+ readonly api: IApi;
94
+
95
+ /**
96
+ * Notifier
97
+ */
98
+ readonly notifier: INotifier<unknown, NotificationCallProps>;
99
+
100
+ /**
101
+ * Label delegate
102
+ */
103
+ readonly labelDelegate: <T = string>(key: string) => T | undefined;
104
+
105
+ /**
106
+ * Culture, like zh-CN
107
+ */
108
+ readonly culture: string;
109
+
110
+ /**
111
+ * Currency, like USD for US dollar
112
+ */
113
+ readonly currency: string;
114
+
115
+ /**
116
+ * Device id
117
+ */
118
+ readonly deviceId: string;
119
+
120
+ /**
121
+ * Country or region, like CN
122
+ */
123
+ readonly region: string;
124
+
125
+ /**
126
+ * Storage
127
+ */
128
+ readonly storage: IStorage;
129
+
130
+ /**
131
+ * Is current authorized
132
+ */
133
+ readonly authorized: boolean;
134
+
135
+ /**
136
+ * Application name
137
+ */
138
+ readonly name: string;
139
+
140
+ /**
141
+ * IP data
142
+ */
143
+ ipData?: IPData;
144
+
145
+ /**
146
+ * User data
147
+ */
148
+ userData?: IUser;
149
+
150
+ /**
151
+ * Search input element
152
+ */
153
+ searchInput?: HTMLInputElement;
154
+
155
+ /**
156
+ * Alert action result
157
+ * @param result Action result
158
+ * @param callback Callback
159
+ */
160
+ alertResult(
161
+ result: IActionResult,
162
+ callback?: NotificationReturn<void>
163
+ ): void;
164
+
165
+ /**
166
+ * Authorize
167
+ * @param token New token
168
+ * @param refreshToken Refresh token
169
+ */
170
+ authorize(token?: string, refreshToken?: string): void;
171
+
172
+ /**
173
+ * Change country or region
174
+ * @param region New country or region
175
+ */
176
+ changeRegion(region: string | AddressRegion): void;
177
+
178
+ /**
179
+ * Change culture
180
+ * @param culture New culture definition
181
+ */
182
+ changeCulture(culture: DataTypes.CultureDefinition): void;
183
+
184
+ /**
185
+ * Check the action result is about device invalid
186
+ * @param result Action result
187
+ * @returns true means device is invalid
188
+ */
189
+ checkDeviceResult(result: IActionResult): boolean;
190
+
191
+ /**
192
+ * Clear cache data
193
+ */
194
+ clearCacheData(): void;
195
+
196
+ /**
197
+ * Clear cached token
198
+ */
199
+ clearCacheToken(): void;
200
+
201
+ /**
202
+ * Decrypt message
203
+ * @param messageEncrypted Encrypted message
204
+ * @param passphrase Secret passphrase
205
+ * @returns Pure text
206
+ */
207
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
208
+
209
+ /**
210
+ * Enhanced decrypt message
211
+ * @param messageEncrypted Encrypted message
212
+ * @param passphrase Secret passphrase
213
+ * @param durationSeconds Duration seconds, <= 12 will be considered as month
214
+ * @returns Pure text
215
+ */
216
+ decryptEnhanced(
217
+ messageEncrypted: string,
218
+ passphrase?: string,
219
+ durationSeconds?: number
220
+ ): string | undefined;
221
+
222
+ /**
223
+ * Detect IP data, call only one time
224
+ * @param callback Callback will be called when the IP is ready
225
+ */
226
+ detectIP(callback?: IDetectIPCallback): void;
227
+
228
+ /**
229
+ * Encrypt message
230
+ * @param message Message
231
+ * @param passphrase Secret passphrase
232
+ * @param iterations Iterations, 1000 times, 1 - 99
233
+ * @returns Result
234
+ */
235
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
236
+
237
+ /**
238
+ * Enhanced encrypt message
239
+ * @param message Message
240
+ * @param passphrase Secret passphrase
241
+ * @param iterations Iterations, 1000 times, 1 - 99
242
+ * @returns Result
243
+ */
244
+ encryptEnhanced(
245
+ message: string,
246
+ passphrase?: string,
247
+ iterations?: number
248
+ ): string;
249
+
250
+ /**
251
+ * Format date to string
252
+ * @param input Input date
253
+ * @param options Options
254
+ * @param timeZone Time zone
255
+ * @returns string
256
+ */
257
+ formatDate(
258
+ input?: Date | string,
259
+ options?: DateUtils.FormatOptions,
260
+ timeZone?: string
261
+ ): string | undefined;
262
+
263
+ /**
264
+ * Format error
265
+ * @param error Error
266
+ * @returns Error message
267
+ */
268
+ formatError(error: ApiDataError): string;
269
+
270
+ /**
271
+ * Format money number
272
+ * @param input Input money number
273
+ * @param isInteger Is integer
274
+ * @param options Options
275
+ * @returns Result
276
+ */
277
+ formatMoney(
278
+ input?: number | bigint,
279
+ isInteger?: boolean,
280
+ options?: Intl.NumberFormatOptions
281
+ ): string | undefined;
282
+
283
+ /**
284
+ * Format number
285
+ * @param input Input number
286
+ * @param options Options
287
+ * @returns Result
288
+ */
289
+ formatNumber(
290
+ input?: number | bigint,
291
+ options?: Intl.NumberFormatOptions
292
+ ): string | undefined;
293
+
294
+ /**
295
+ * Do refresh token result
296
+ * @param result Result
297
+ * @param initCallCallback InitCall callback
298
+ * @param silent Silent without any popups
299
+ */
300
+ doRefreshTokenResult(
301
+ result: RefreshTokenResult,
302
+ initCallCallback?: (result: boolean) => void,
303
+ silent?: boolean
304
+ ): void;
305
+
306
+ /**
307
+ * Format refresh token result
308
+ * @param result Refresh token result
309
+ * @returns Message
310
+ */
311
+ formatRefreshTokenResult(result: RefreshTokenResult): string | undefined;
312
+
313
+ /**
314
+ * Format result text
315
+ * @param result Action result
316
+ * @param forceToLocal Force to local labels
317
+ * @returns Message
318
+ */
319
+ formatResult(result: IActionResult, forceToLocal?: boolean): string;
320
+
321
+ /**
322
+ * Fresh countdown UI
323
+ * @param callback Callback
324
+ */
325
+ freshCountdownUI(callback?: () => PromiseLike<unknown>): void;
326
+
327
+ /**
328
+ * Get culture resource
329
+ * @param key key
330
+ * @returns Resource
331
+ */
332
+ get<T = string>(key: string): T | undefined;
333
+
334
+ /**
335
+ * Get multiple culture labels
336
+ * @param keys Keys
337
+ */
338
+ getLabels<T extends string>(...keys: T[]): { [K in T]: string };
339
+
340
+ /**
341
+ * Get cached token
342
+ * @returns Cached token
343
+ */
344
+ getCacheToken(): string | undefined;
345
+
346
+ /**
347
+ * Get all regions
348
+ * @returns Regions
349
+ */
350
+ getRegions(): AddressRegion[];
351
+
352
+ /**
353
+ * Get roles
354
+ * @param role Combination role value
355
+ */
356
+ getRoles(role: number): ListType[];
357
+
358
+ /**
359
+ * Get status label
360
+ * @param status Status value
361
+ */
362
+ getStatusLabel(status: number | null | undefined): string;
363
+
364
+ /**
365
+ * Get status list
366
+ * @returns list
367
+ */
368
+ getStatusList(): ListType[];
369
+
370
+ /**
371
+ * Get refresh token from response headers
372
+ * @param rawResponse Raw response from API call
373
+ * @returns response refresh token
374
+ */
375
+ getResponseToken(rawResponse: any): string | null;
376
+
377
+ /**
378
+ * Get time zone
379
+ * @returns Time zone
380
+ */
381
+ getTimeZone(): string | undefined;
382
+
383
+ /**
384
+ * Get product unit and repeat option label
385
+ * @param unit Product unit or repeat option
386
+ * @param isJoined Add the join label like 'per Kg' for Kg
387
+ */
388
+ getUnitLabel(unit?: ProductUnit, isJoined?: boolean): string;
389
+
390
+ /**
391
+ * Hash message, SHA3 or HmacSHA512, 512 as Base64
392
+ * https://cryptojs.gitbook.io/docs/
393
+ * @param message Message
394
+ * @param passphrase Secret passphrase
395
+ */
396
+ hash(message: string, passphrase?: string): string;
397
+
398
+ /**
399
+ * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
400
+ * https://cryptojs.gitbook.io/docs/
401
+ * @param message Message
402
+ * @param passphrase Secret passphrase
403
+ */
404
+ hashHex(message: string, passphrase?: string): string;
405
+
406
+ /**
407
+ * Check use has the specific role permission or not
408
+ * @param roles Roles to check
409
+ * @returns Result
410
+ */
411
+ hasPermission(roles: number | UserRole | number[] | UserRole[]): boolean;
412
+
413
+ /**
414
+ * Init call
415
+ * @param callback Callback
416
+ * @param resetKeys Reset all keys first
417
+ * @returns Result
418
+ */
419
+ initCall(
420
+ callback?: (result: boolean) => void,
421
+ resetKeys?: boolean
422
+ ): Promise<void>;
423
+
424
+ /**
425
+ * Is valid password, override to implement custom check
426
+ * @param password Input password
427
+ */
428
+ isValidPassword(password: string): boolean;
429
+
430
+ /**
431
+ * Callback where exit a page
432
+ */
433
+ pageExit(): void;
434
+
435
+ /**
436
+ * Refresh token
437
+ * @param props Props
438
+ */
439
+ refreshToken<D extends object = {}>(
440
+ props?: RefreshTokenProps<D>
441
+ ): Promise<boolean>;
442
+
443
+ /**
444
+ * Signout
445
+ */
446
+ signout(): Promise<void>;
447
+
448
+ /**
449
+ * Get organization list
450
+ * @param items Max items
451
+ * @param serviceId Service id
452
+ * @returns Result
453
+ */
454
+ orgList(
455
+ items?: number,
456
+ serviceId?: number
457
+ ): Promise<ListType[] | undefined>;
458
+
459
+ /**
460
+ * Persist settings to source when application exit
461
+ */
462
+ persist(): void;
463
+
464
+ /**
465
+ * Redirect to the Url
466
+ * @param url Url
467
+ */
468
+ redirectTo(url: string): void;
469
+
470
+ /**
471
+ * Switch organization
472
+ * @param id Organization id
473
+ * @param serviceId Service id
474
+ */
475
+ switchOrg(id: number, serviceId?: number): Promise<boolean | undefined>;
476
+
477
+ /**
478
+ * Go to the login page
479
+ * @param tryLogin Try to login again
480
+ */
481
+ toLoginPage(tryLogin?: boolean): void;
482
+
483
+ /**
484
+ * Transform URL
485
+ * @param url URL
486
+ * @returns Transformed url
487
+ */
488
+ transformUrl(url: string): string;
489
+
490
+ /**
491
+ * Try login, returning false means is loading
492
+ * UI get involved while refreshToken not intended
493
+ * @param data Additional request data
494
+ */
495
+ tryLogin<D extends object = {}>(data?: D): Promise<boolean>;
496
+
497
+ /**
498
+ * User login
499
+ * @param user User data
500
+ * @param refreshToken Refresh token
501
+ * @param keep Keep login or not
502
+ */
503
+ userLogin(user: IUser, refreshToken: string, keep?: boolean): void;
504
+
505
+ /**
506
+ * User logout
507
+ * @param clearToken Clear refresh token or not
508
+ */
509
+ userLogout(clearToken: boolean): void;
510
+
511
+ /**
512
+ * User unauthorized
513
+ */
514
+ userUnauthorized(): void;
515
+
516
+ /**
517
+ * Show warning message
518
+ * @param message Message
519
+ * @param align Align, default as TopRight
520
+ */
521
+ warning(
522
+ message: NotificationContent<unknown>,
523
+ align?: NotificationAlign
524
+ ): void;
525
+ }