@worldfirst/worldfirst-js 0.0.1 → 0.0.1775118337-dev.7

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/types.d.ts ADDED
@@ -0,0 +1,469 @@
1
+ import type * as CSS_2 from 'csstype';
2
+
3
+ declare interface Appearance<Props extends Partial<DefaultAppearanceProps> = DefaultAppearanceProps> {
4
+ theme?: Props['theme'];
5
+ variables?: PartialSpeKey<Props['variables'], string>;
6
+ rules?: PartialSpeKey<Props['rules'], CSS_2.Properties<string, number>>;
7
+ displaySetting?: PartialSpeKey<Props['displaySetting'], boolean>;
8
+ layout?: Props['layout'];
9
+ }
10
+
11
+ /**
12
+ * @description 内置基础的桥接事件映射类型
13
+ */
14
+ declare interface BaseBridgeEventMap<AppConfig extends Record<string, any> = Record<string, any>, SubmitReplay extends Record<string, any> = Record<string, any>, SubmitParams extends Record<string, any> = Record<string, any>, AppErrorCodes extends string = string, OpenModalConfig extends BaseOpenModalConfig = BaseOpenModalConfig> {
15
+ error: ErrorPayload<AppErrorCodes>;
16
+ /* Excluded from this release type: "OPENSDK@HANDSHAKE" */
17
+ /* Excluded from this release type: "OPENSDK@UPDATE_CONFIG" */
18
+ /* Excluded from this release type: "OPENSDK@HANDSHAKE_ACK" */
19
+ /* Excluded from this release type: "OPENSDK@SUBMIT" */
20
+ /* Excluded from this release type: "OPENSDK@SUBMIT_REPLY" */
21
+ /* Excluded from this release type: "OPENSDK@OPEN_MODAL" */
22
+ /* Excluded from this release type: "OPENSDK@OPEN_MODAL_ERROR" */
23
+ /* Excluded from this release type: "OPENSDK@CLOSE_MODAL" */
24
+ /* Excluded from this release type: "OPENSDK@REDIRECT" */
25
+ /* Excluded from this release type: "OPENSDK@TRANSFER_MODAL_PORT" */
26
+ /* Excluded from this release type: "OPENSDK@TRANSFER_MODAL_APPEARANCE" */
27
+ /* Excluded from this release type: "OPENSDK@TRANSFER_APP_PORT" */
28
+ /* Excluded from this release type: "OPENSDK@ERROR" */
29
+ /* Excluded from this release type: "OPENSDK@TRANSFER_RECEIVE_PORT" */
30
+ /* Excluded from this release type: "OPENSDK@TRANSFER_SEND_PORT" */
31
+ /* Excluded from this release type: "OPENSDK@MESSAGE" */
32
+ /* Excluded from this release type: "OPENSDK@HEIGHT_UPDATE" */
33
+ }
34
+
35
+ declare interface BaseConfig {
36
+ /**
37
+ * @description 透传参数
38
+ */
39
+ targetOrigin: string;
40
+ /**
41
+ * @description 是否开启debug
42
+ */
43
+ debug?: boolean;
44
+ /**
45
+ * @description 当前的环境
46
+ */
47
+ env: 'sandbox' | 'prod';
48
+ /**
49
+ * @description 多语言
50
+ */
51
+ locale?: string;
52
+ /* Excluded from this release type: version */
53
+ }
54
+
55
+ /**
56
+ * @description 基础组件/页面
57
+ */
58
+ declare abstract class BaseElement<Config extends BaseElementConfig = BaseElementConfig, EventMap extends BaseBridgeEventMap<Config['appConfig']> = BaseBridgeEventMap<Config['appConfig']>> {
59
+ static elementType: string;
60
+ /* Excluded from this release type: name */
61
+ /* Excluded from this release type: _elementMode */
62
+ /* Excluded from this release type: _isMainElement */
63
+ /* Excluded from this release type: config */
64
+ /* Excluded from this release type: bridge */
65
+ /* Excluded from this release type: hooks */
66
+ /* Excluded from this release type: iframe */
67
+ status: 'initialized' | 'mounting' | 'mounted' | 'ready' | 'destroyed' | 'unmounted';
68
+ /* Excluded from this release type: cachedSend */
69
+ constructor(name: string, config: WithAppURL<Config>);
70
+ /* Excluded from this release type: submit */
71
+ /* Excluded from this release type: setup */
72
+ /**
73
+ * @param {string | HTMLElement} containerId 如果给字符串id不要带#号
74
+ */
75
+ mount(containerId: HTMLElement): Promise<void>;
76
+ /* Excluded from this release type: _mount */
77
+ /**
78
+ * 更新webapp配置
79
+ * @param {Partial<BaseElementAppConfig>} newConfig
80
+ */
81
+ updateConfig(newConfig: Partial<Config['appConfig']>): void;
82
+ /**
83
+ * 卸载元素
84
+ */
85
+ umount(): void;
86
+ /* Excluded from this release type: isSingleMode */
87
+ /**
88
+ * 监听消息
89
+ */
90
+ on<T extends keyof EventMap>(event: T, handler: (payload: EventMap[T]) => void): void;
91
+ /* Excluded from this release type: send */
92
+ /* Excluded from this release type: _addPort */
93
+ /* Excluded from this release type: _transferReceivePort */
94
+ /* Excluded from this release type: _transferSendPort */
95
+ /* Excluded from this release type: _submit */
96
+ destroy(): void;
97
+ get isMainElement(): boolean;
98
+ /**
99
+ * 对于独立的input需要实现
100
+ */
101
+ protected abstract focus(): void;
102
+ /**
103
+ * 对于独立的input需要实现
104
+ */
105
+ protected abstract blur(): void;
106
+ /**
107
+ * 对于独立的input需要实现
108
+ */
109
+ protected abstract clear(): void;
110
+ /* Excluded from this release type: _addHook */
111
+ }
112
+
113
+ declare interface BaseElementAppConfig {
114
+ locale?: string;
115
+ /* Excluded from this release type: env */
116
+ /* Excluded from this release type: merchantId */
117
+ [key: string]: any;
118
+ }
119
+
120
+ declare interface BaseElementConfig<T extends BaseElementAppConfig = Record<string, any>> extends BaseConfig {
121
+ appConfig: T;
122
+ elementMode: ElementMode;
123
+ }
124
+
125
+ declare abstract class BaseFactory {
126
+ /* Excluded from this release type: config */
127
+ /* Excluded from this release type: isInit */
128
+ /**
129
+ * @description 当前版本号
130
+ * 用于日志上报
131
+ */
132
+ version: string;
133
+ /* Excluded from this release type: elementMode */
134
+ /* Excluded from this release type: _elements */
135
+ /* Excluded from this release type: globalBridgeForWebApp */
136
+ /* Excluded from this release type: globalBridgeForModal */
137
+ /* Excluded from this release type: openModalHandle */
138
+ /* Excluded from this release type: elementClasses */
139
+ /**
140
+ * @param config 配置
141
+ */
142
+ constructor(config: BaseFactoryConfig);
143
+ protected openModal(payload: BaseBridgeEventMap['OPENSDK@OPEN_MODAL']): Promise<OpenModalHandle>;
144
+ /* Excluded from this release type: canUseElements */
145
+ /**
146
+ * @description 无需手动调用
147
+ */
148
+ init(): Promise<this>;
149
+ /* Excluded from this release type: generateElementKey */
150
+ /**
151
+ * @description 更新工厂配置 locale debug env
152
+ * @param config
153
+ */
154
+ updateConfig(config: Partial<BaseFactoryConfig>): void;
155
+ /**
156
+ * @description 销毁所有元素实例
157
+ */
158
+ destroy(): void;
159
+ /* Excluded from this release type: _generateElementKey */
160
+ /**
161
+ * 创建element实例
162
+ * @param {string} elementType 类型
163
+ * @param {BaseElementConfig} options 选项
164
+ */
165
+ abstract createElement(elementType: string, options?: BaseElementAppConfig): BaseElement;
166
+ /**
167
+ * 获取element实例
168
+ * @param {string} elementType 类型
169
+ * @param {BaseElementConfig} options 选项
170
+ */
171
+ abstract getElement(elementType: string, options?: BaseElementAppConfig): BaseElement;
172
+ /* Excluded from this release type: _createElement */
173
+ /* Excluded from this release type: _addElement */
174
+ /* Excluded from this release type: _removeElement */
175
+ /* Excluded from this release type: _getElement */
176
+ /* Excluded from this release type: _getElementSize */
177
+ }
178
+
179
+ declare interface BaseFactoryConfig extends BaseConfig {
180
+ /* Excluded from this release type: trackerCode */
181
+ }
182
+
183
+ declare interface BaseOpenModalConfig {
184
+ /**
185
+ * 无需手动传递,app的唯一scope
186
+ */
187
+ scope?: string;
188
+ /**
189
+ * @description 是否展示关闭按钮
190
+ */
191
+ showClose?: boolean;
192
+ /**
193
+ * @description 配合showClose使用,在加载完成后是否隐藏按钮
194
+ */
195
+ hideCloseWhenLoaded?: boolean;
196
+ /**
197
+ * @description 是否展示loading
198
+ */
199
+ showLoading?: boolean;
200
+ /**
201
+ * @description 点击关闭按钮时的回调
202
+ */
203
+ onClose?: () => void;
204
+ url: string;
205
+ /**
206
+ * 关闭时,是否需要app页面二次确认,默认为false,即直接关闭
207
+ */
208
+ closeConfirm?: boolean;
209
+ appearance?: Appearance;
210
+ }
211
+
212
+ declare interface DefaultAppearanceProps {
213
+ theme: string;
214
+ variables: string;
215
+ displaySetting: string;
216
+ layout: string;
217
+ rules: string;
218
+ }
219
+
220
+ /**
221
+ * @description 元素列表项类型
222
+ */
223
+ declare type ElementListItemType = iBaseElementClass | {
224
+ ElementClass: iBaseElementClass;
225
+ config?: Partial<BaseElementConfig>;
226
+ };
227
+
228
+ declare type ElementMode = 'SINGLE' | 'GROUP';
229
+
230
+ declare interface ErrorPayload<Code extends string = string> {
231
+ source: ErrorSource;
232
+ code: Code;
233
+ message: string;
234
+ reason: string;
235
+ traceId?: string;
236
+ stack?: string;
237
+ }
238
+
239
+ declare type ErrorSource = ErrorSource_2;
240
+
241
+ declare type ErrorSource_2 = 'SDK_INTERNAL' | 'SDK_USAGE' | 'APP_RUNTIME' | 'SERVER_API' | 'SYSTEM';
242
+
243
+ declare type GetI18nQueryParam = string | {
244
+ /**
245
+ * 多语言文案的 key
246
+ */
247
+ id: string;
248
+ /**
249
+ * 多语言文案的默认值
250
+ */
251
+ dm?: string;
252
+ /**
253
+ * 多语言文案的默认值,defaultMessage 和 dm 只需要一个就行
254
+ * 推荐使用 dm
255
+ * defaultMessage 是为了兼容中后台原来的开发习惯
256
+ */
257
+ defaultMessage?: string;
258
+ };
259
+
260
+ declare type GlobalOptions = {
261
+ i18n?: SimpleI18n;
262
+ i18nPrefix: string;
263
+ getDefaultMessage?: () => string;
264
+ };
265
+
266
+ declare interface iBaseElementClass {
267
+ new (name: string, config: BaseElementConfig): BaseElement;
268
+ elementType: string;
269
+ }
270
+
271
+ /**
272
+ * @description OpenModal的返回值类型
273
+ */
274
+ declare type OpenModalHandle = {
275
+ destroy: () => void;
276
+ onLoadedSuccess: (onLoadSuccess: () => void) => void;
277
+ getContentWindow: () => Window | null;
278
+ };
279
+
280
+ declare class OpenSDKError<Code extends string = string> extends Error {
281
+ code: Code | OpenSDKInternalErrorCodes;
282
+ name: string;
283
+ /* Excluded from this release type: _openSDKErrorFlag */
284
+ source: ErrorSource;
285
+ traceId?: string;
286
+ /**
287
+ * @description 用于存储原始错误信息的,给内部调试
288
+ */
289
+ reason: string;
290
+ static isError(error: any): error is OpenSDKError;
291
+ /**
292
+ * @description 统一转换error为OpenSDKError
293
+ */
294
+ static unificationError(error: unknown, source?: ErrorSource): OpenSDKError;
295
+ static initConfig(config: Partial<GlobalOptions>): void;
296
+ constructor(_message: string | {
297
+ message: string;
298
+ params?: Record<string, any>;
299
+ }, code: Code | OpenSDKInternalErrorCodes, source: ErrorSource, _traceId?: string, serverAPIWithoutI18n?: boolean);
300
+ toJSON(): ErrorPayload<Code | OpenSDKInternalErrorCodes>;
301
+ }
302
+
303
+ declare const OpenSDKInternalErrorCodes: {
304
+ /**
305
+ * @description 未知错误
306
+ */
307
+ readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
308
+ /**
309
+ * @description 无端口错误,通常发生在模态通信中,SDK期望接收一个MessagePort但未收到
310
+ */
311
+ readonly NO_PORTS_ERROR: "NO_PORTS_ERROR";
312
+ /**
313
+ * @description 非法的URL
314
+ */
315
+ readonly INVALID_REDIRECT_URL: "INVALID_REDIRECT_URL";
316
+ };
317
+
318
+ declare type OpenSDKInternalErrorCodes = (typeof OpenSDKInternalErrorCodes)[keyof typeof OpenSDKInternalErrorCodes];
319
+
320
+ declare type PartialSpeKey<T, V> = Partial<T extends infer K extends string ? Record<K, V> : Record<string, V>>;
321
+
322
+ declare interface PreLoadConfig {
323
+ /**
324
+ * @description 预加载参数,可选配置。
325
+ * 传入需要使用的 element 类型列表,SDK 将提前加载对应资源以优化性能。
326
+ * 每个值与 createElement 的 type 参数一致,如 'Onboard'、'Vaulting'。
327
+ */
328
+ elements?: string[];
329
+ }
330
+
331
+ /**
332
+ * @description 用于收集所有其他元素的值
333
+ */
334
+ declare class ReceivePort {
335
+ targetPorts: Array<MessagePort>;
336
+ name: string;
337
+ constructor(name: string);
338
+ addPort(port: readonly MessagePort[]): void;
339
+ removePort(port: MessagePort): void;
340
+ getValues(config?: {
341
+ timeout: number;
342
+ }): Promise<Record<string, any> | undefined>;
343
+ }
344
+
345
+ declare interface RedirectOption {
346
+ url: string;
347
+ mode?: 'redirect' | 'replace';
348
+ /**
349
+ * @description 是否重定向后销毁实例,默认为true
350
+ */
351
+ destory?: boolean;
352
+ }
353
+
354
+ /**
355
+ * @description 其他元素,用于发送value到主元素
356
+ */
357
+ declare class SendPort<T extends Record<string, any> = Record<string, any>> {
358
+ targetPort: MessagePort;
359
+ name: string;
360
+ constructor(name: string, port: MessagePort);
361
+ /**
362
+ * @description 用于监听发送消息节点
363
+ */
364
+ onSendRequest(fn: () => void): void;
365
+ send(values?: T): void;
366
+ }
367
+
368
+ declare type SimpleI18n = {
369
+ get(id: GetI18nQueryParam, variable?: Record<string, any>, localeCode?: string): string;
370
+ };
371
+
372
+ /**
373
+ * @description submit return type
374
+ */
375
+ export declare interface SubmitResult {
376
+ [key: string]: any;
377
+ }
378
+
379
+ declare type WithAppURL<T> = T & {
380
+ appUrl: string;
381
+ isMainElement: boolean;
382
+ };
383
+
384
+ /**
385
+ * @public
386
+ * @description load config type
387
+ */
388
+ export declare interface WorldFirstLoadConfig extends Omit<BaseFactoryConfig, 'trackerCode' | 'targetOrigin' | 'version'>, PreLoadConfig {
389
+ }
390
+
391
+ export declare class WorldFirstSDK extends BaseFactory {
392
+ /**
393
+ * SDK 版本号
394
+ */
395
+ static version: string;
396
+ constructor(config: WorldFirstSDKConfig);
397
+ /**
398
+ * 实现 BaseFactory 的 canUseElements 抽象方法
399
+ * 返回工厂可用的元素类列表
400
+ */
401
+ protected canUseElements(): Array<ElementListItemType>;
402
+ /**
403
+ * 实现 BaseElementsFactory 的 elements 方法
404
+ * 创建并返回元素集合
405
+ * 当前用不上该方法
406
+ */
407
+ /**
408
+ * 实现 BaseElementsFactory 的 createElement 方法
409
+ * 创建单个元素
410
+ */
411
+ createElement(type: string, options?: any): any;
412
+ /**
413
+ * 实现 BaseElementsFactory 的 getElement 方法
414
+ * 获取已创建的元素
415
+ */
416
+ getElement(type: string, options?: any): any;
417
+ }
418
+
419
+ /**
420
+ * @description sdk config type
421
+ */
422
+ export declare interface WorldFirstSDKConfig extends Omit<BaseFactoryConfig, 'version' | 'targetOrigin'>, PreLoadConfig {
423
+ version?: string;
424
+ }
425
+
426
+ /**
427
+ * @description SDK element events map type
428
+ */
429
+ export declare type WorldFirstSDKElementEventsMap<AppConfig extends Record<string, any> = Record<string, any>, SubmitParams extends Record<string, any> = Record<string, any>> = BaseBridgeEventMap<AppConfig, SubmitResult, SubmitParams, string /**, OpenModalParams */>;
430
+
431
+ /**
432
+ * @description SDK Error
433
+ */
434
+ export declare class WorldFirstSDKError extends OpenSDKError<WorldFirstSDKErrorCodes> {
435
+ name: string;
436
+ }
437
+
438
+ /**
439
+ * @description SDK error codes
440
+ */
441
+ declare const WorldFirstSDKErrorCodes: {
442
+ readonly GENERATE_ELEMENT_KEY_FAILED: "GENERATE_ELEMENT_KEY_FAILED";
443
+ readonly ELEMENT_INIT_FAILED: "ELEMENT_INIT_FAILED";
444
+ readonly MOUNT_TIMEOUT: "MOUNT_TIMEOUT";
445
+ readonly MOUNT_MISSING_CONTAINER: "MOUNT_MISSING_CONTAINER";
446
+ readonly MOUNT_IFRAME_LOAD_ERROR: "MOUNT_IFRAME_LOAD_ERROR";
447
+ readonly MOUNT_NOT_MAINELEMENT_WITH_SINGLE_MODE: "MOUNT_NOT_MAINELEMENT_WITH_SINGLE_MODE";
448
+ readonly MOUNT_NOT_SUPPORT: "MOUNT_NOT_SUPPORT";
449
+ readonly MOUNT_INITIAL_FAILED: "MOUNT_INITIAL_FAILED";
450
+ readonly ELEMENT_INSTANCE_DESTROYED: "ELEMENT_INSTANCE_DESTROYED";
451
+ readonly ELEMENT_DEFINE_ERROR: "ELEMENT_DEFINE_ERROR";
452
+ readonly ELEMENT_ALREADY_REGISTERED: "ELEMENT_ALREADY_REGISTERED";
453
+ readonly CANNOT_CREATE_SAME_ELEMENT_TWICE: "CANNOT_CREATE_SAME_ELEMENT_TWICE";
454
+ readonly GROUPS_ONLY_ONE_MAIN_ELEMENT: "GROUPS_ONLY_ONE_MAIN_ELEMENT";
455
+ readonly GROUPS_NOT_FOUND_MAIN_ELEMENT: "GROUPS_NOT_FOUND_MAIN_ELEMENT";
456
+ readonly NOT_SUPPORT_ELEMENT: "NOT_SUPPORT_ELEMENT";
457
+ readonly NOT_SUPPORT_SUBMIT: "NOT_SUPPORT_SUBMIT";
458
+ readonly INVALID_MODAL_URL: "INVALID_MODAL_URL";
459
+ readonly LOAD_DEBUGGER_FAILED: "LOAD_DEBUGGER_FAILED";
460
+ };
461
+
462
+ declare type WorldFirstSDKErrorCodes = (typeof WorldFirstSDKErrorCodes)[keyof typeof WorldFirstSDKErrorCodes];
463
+
464
+ /**
465
+ * @description SDK theme
466
+ */
467
+ export declare type WorldFirstTheme = 'dark' | 'light';
468
+
469
+ export { }