@etsoo/appscript 1.2.84 → 1.2.87

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,535 @@
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
+ * Is screen size down 'sm'
157
+ */
158
+ smDown?: boolean;
159
+
160
+ /**
161
+ * Is screen size up 'md'
162
+ */
163
+ mdUp?: boolean;
164
+
165
+ /**
166
+ * Alert action result
167
+ * @param result Action result
168
+ * @param callback Callback
169
+ */
170
+ alertResult(
171
+ result: IActionResult,
172
+ callback?: NotificationReturn<void>
173
+ ): void;
174
+
175
+ /**
176
+ * Authorize
177
+ * @param token New token
178
+ * @param refreshToken Refresh token
179
+ */
180
+ authorize(token?: string, refreshToken?: string): void;
181
+
182
+ /**
183
+ * Change country or region
184
+ * @param region New country or region
185
+ */
186
+ changeRegion(region: string | AddressRegion): void;
187
+
188
+ /**
189
+ * Change culture
190
+ * @param culture New culture definition
191
+ */
192
+ changeCulture(culture: DataTypes.CultureDefinition): void;
193
+
194
+ /**
195
+ * Check the action result is about device invalid
196
+ * @param result Action result
197
+ * @returns true means device is invalid
198
+ */
199
+ checkDeviceResult(result: IActionResult): boolean;
200
+
201
+ /**
202
+ * Clear cache data
203
+ */
204
+ clearCacheData(): void;
205
+
206
+ /**
207
+ * Clear cached token
208
+ */
209
+ clearCacheToken(): void;
210
+
211
+ /**
212
+ * Decrypt message
213
+ * @param messageEncrypted Encrypted message
214
+ * @param passphrase Secret passphrase
215
+ * @returns Pure text
216
+ */
217
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
218
+
219
+ /**
220
+ * Enhanced decrypt message
221
+ * @param messageEncrypted Encrypted message
222
+ * @param passphrase Secret passphrase
223
+ * @param durationSeconds Duration seconds, <= 12 will be considered as month
224
+ * @returns Pure text
225
+ */
226
+ decryptEnhanced(
227
+ messageEncrypted: string,
228
+ passphrase?: string,
229
+ durationSeconds?: number
230
+ ): string | undefined;
231
+
232
+ /**
233
+ * Detect IP data, call only one time
234
+ * @param callback Callback will be called when the IP is ready
235
+ */
236
+ detectIP(callback?: IDetectIPCallback): void;
237
+
238
+ /**
239
+ * Encrypt message
240
+ * @param message Message
241
+ * @param passphrase Secret passphrase
242
+ * @param iterations Iterations, 1000 times, 1 - 99
243
+ * @returns Result
244
+ */
245
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
246
+
247
+ /**
248
+ * Enhanced encrypt message
249
+ * @param message Message
250
+ * @param passphrase Secret passphrase
251
+ * @param iterations Iterations, 1000 times, 1 - 99
252
+ * @returns Result
253
+ */
254
+ encryptEnhanced(
255
+ message: string,
256
+ passphrase?: string,
257
+ iterations?: number
258
+ ): string;
259
+
260
+ /**
261
+ * Format date to string
262
+ * @param input Input date
263
+ * @param options Options
264
+ * @param timeZone Time zone
265
+ * @returns string
266
+ */
267
+ formatDate(
268
+ input?: Date | string,
269
+ options?: DateUtils.FormatOptions,
270
+ timeZone?: string
271
+ ): string | undefined;
272
+
273
+ /**
274
+ * Format error
275
+ * @param error Error
276
+ * @returns Error message
277
+ */
278
+ formatError(error: ApiDataError): string;
279
+
280
+ /**
281
+ * Format money number
282
+ * @param input Input money number
283
+ * @param isInteger Is integer
284
+ * @param options Options
285
+ * @returns Result
286
+ */
287
+ formatMoney(
288
+ input?: number | bigint,
289
+ isInteger?: boolean,
290
+ options?: Intl.NumberFormatOptions
291
+ ): string | undefined;
292
+
293
+ /**
294
+ * Format number
295
+ * @param input Input number
296
+ * @param options Options
297
+ * @returns Result
298
+ */
299
+ formatNumber(
300
+ input?: number | bigint,
301
+ options?: Intl.NumberFormatOptions
302
+ ): string | undefined;
303
+
304
+ /**
305
+ * Do refresh token result
306
+ * @param result Result
307
+ * @param initCallCallback InitCall callback
308
+ * @param silent Silent without any popups
309
+ */
310
+ doRefreshTokenResult(
311
+ result: RefreshTokenResult,
312
+ initCallCallback?: (result: boolean) => void,
313
+ silent?: boolean
314
+ ): void;
315
+
316
+ /**
317
+ * Format refresh token result
318
+ * @param result Refresh token result
319
+ * @returns Message
320
+ */
321
+ formatRefreshTokenResult(result: RefreshTokenResult): string | undefined;
322
+
323
+ /**
324
+ * Format result text
325
+ * @param result Action result
326
+ * @param forceToLocal Force to local labels
327
+ * @returns Message
328
+ */
329
+ formatResult(result: IActionResult, forceToLocal?: boolean): string;
330
+
331
+ /**
332
+ * Fresh countdown UI
333
+ * @param callback Callback
334
+ */
335
+ freshCountdownUI(callback?: () => PromiseLike<unknown>): void;
336
+
337
+ /**
338
+ * Get culture resource
339
+ * @param key key
340
+ * @returns Resource
341
+ */
342
+ get<T = string>(key: string): T | undefined;
343
+
344
+ /**
345
+ * Get multiple culture labels
346
+ * @param keys Keys
347
+ */
348
+ getLabels<T extends string>(...keys: T[]): { [K in T]: string };
349
+
350
+ /**
351
+ * Get cached token
352
+ * @returns Cached token
353
+ */
354
+ getCacheToken(): string | undefined;
355
+
356
+ /**
357
+ * Get all regions
358
+ * @returns Regions
359
+ */
360
+ getRegions(): AddressRegion[];
361
+
362
+ /**
363
+ * Get roles
364
+ * @param role Combination role value
365
+ */
366
+ getRoles(role: number): ListType[];
367
+
368
+ /**
369
+ * Get status label
370
+ * @param status Status value
371
+ */
372
+ getStatusLabel(status: number | null | undefined): string;
373
+
374
+ /**
375
+ * Get status list
376
+ * @returns list
377
+ */
378
+ getStatusList(): ListType[];
379
+
380
+ /**
381
+ * Get refresh token from response headers
382
+ * @param rawResponse Raw response from API call
383
+ * @returns response refresh token
384
+ */
385
+ getResponseToken(rawResponse: any): string | null;
386
+
387
+ /**
388
+ * Get time zone
389
+ * @returns Time zone
390
+ */
391
+ getTimeZone(): string | undefined;
392
+
393
+ /**
394
+ * Get product unit and repeat option label
395
+ * @param unit Product unit or repeat option
396
+ * @param isJoined Add the join label like 'per Kg' for Kg
397
+ */
398
+ getUnitLabel(unit?: ProductUnit, isJoined?: boolean): string;
399
+
400
+ /**
401
+ * Hash message, SHA3 or HmacSHA512, 512 as Base64
402
+ * https://cryptojs.gitbook.io/docs/
403
+ * @param message Message
404
+ * @param passphrase Secret passphrase
405
+ */
406
+ hash(message: string, passphrase?: string): string;
407
+
408
+ /**
409
+ * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
410
+ * https://cryptojs.gitbook.io/docs/
411
+ * @param message Message
412
+ * @param passphrase Secret passphrase
413
+ */
414
+ hashHex(message: string, passphrase?: string): string;
415
+
416
+ /**
417
+ * Check use has the specific role permission or not
418
+ * @param roles Roles to check
419
+ * @returns Result
420
+ */
421
+ hasPermission(roles: number | UserRole | number[] | UserRole[]): boolean;
422
+
423
+ /**
424
+ * Init call
425
+ * @param callback Callback
426
+ * @param resetKeys Reset all keys first
427
+ * @returns Result
428
+ */
429
+ initCall(
430
+ callback?: (result: boolean) => void,
431
+ resetKeys?: boolean
432
+ ): Promise<void>;
433
+
434
+ /**
435
+ * Is valid password, override to implement custom check
436
+ * @param password Input password
437
+ */
438
+ isValidPassword(password: string): boolean;
439
+
440
+ /**
441
+ * Callback where exit a page
442
+ */
443
+ pageExit(): void;
444
+
445
+ /**
446
+ * Refresh token
447
+ * @param props Props
448
+ */
449
+ refreshToken<D extends object = {}>(
450
+ props?: RefreshTokenProps<D>
451
+ ): Promise<boolean>;
452
+
453
+ /**
454
+ * Signout
455
+ */
456
+ signout(): Promise<void>;
457
+
458
+ /**
459
+ * Get organization list
460
+ * @param items Max items
461
+ * @param serviceId Service id
462
+ * @returns Result
463
+ */
464
+ orgList(
465
+ items?: number,
466
+ serviceId?: number
467
+ ): Promise<ListType[] | undefined>;
468
+
469
+ /**
470
+ * Persist settings to source when application exit
471
+ */
472
+ persist(): void;
473
+
474
+ /**
475
+ * Redirect to the Url
476
+ * @param url Url
477
+ */
478
+ redirectTo(url: string): void;
479
+
480
+ /**
481
+ * Switch organization
482
+ * @param id Organization id
483
+ * @param serviceId Service id
484
+ */
485
+ switchOrg(id: number, serviceId?: number): Promise<boolean | undefined>;
486
+
487
+ /**
488
+ * Go to the login page
489
+ * @param tryLogin Try to login again
490
+ */
491
+ toLoginPage(tryLogin?: boolean): void;
492
+
493
+ /**
494
+ * Transform URL
495
+ * @param url URL
496
+ * @returns Transformed url
497
+ */
498
+ transformUrl(url: string): string;
499
+
500
+ /**
501
+ * Try login, returning false means is loading
502
+ * UI get involved while refreshToken not intended
503
+ * @param data Additional request data
504
+ */
505
+ tryLogin<D extends object = {}>(data?: D): Promise<boolean>;
506
+
507
+ /**
508
+ * User login
509
+ * @param user User data
510
+ * @param refreshToken Refresh token
511
+ * @param keep Keep login or not
512
+ */
513
+ userLogin(user: IUser, refreshToken: string, keep?: boolean): void;
514
+
515
+ /**
516
+ * User logout
517
+ * @param clearToken Clear refresh token or not
518
+ */
519
+ userLogout(clearToken: boolean): void;
520
+
521
+ /**
522
+ * User unauthorized
523
+ */
524
+ userUnauthorized(): void;
525
+
526
+ /**
527
+ * Show warning message
528
+ * @param message Message
529
+ * @param align Align, default as TopRight
530
+ */
531
+ warning(
532
+ message: NotificationContent<unknown>,
533
+ align?: NotificationAlign
534
+ ): void;
535
+ }