Repository navigation
Expand file tree
/
Copy pathbase-api.ts
More file actions
166 lines (151 loc) · 3.99 KB
/
Copy pathbase-api.ts
File metadata and controls
166 lines (151 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type { BrowsableRequest } from '@empathyco/x-types'
import type { WireMetadata, XEventsTypes } from '../../wiring/index'
import type { XBus } from '../../x-bus'
import type { NormalisedSnippetConfig, SnippetConfig, XAPI } from './api.types'
/**
* Default implementation for {@link XAPI}.
*
* @public
*/
export class BaseXAPI implements XAPI {
/**
* Flag to check if the initialization was already done.
*
* @internal
*/
protected isXInitialized = false
/**
* Bus for emitting and listening events.
*
* @internal
*/
protected bus!: XBus<XEventsTypes, WireMetadata>
/**
* The callback to call from the init method. The logic of initialization is out of this API
* since this API is just a facade.
*
* @internal
*/
protected initCallback!: (config: SnippetConfig) => any
/**
* Getter for the snippet config object.
*
* @returns The {@link NormalisedSnippetConfig | snippetConfig} object.
*
* @public
*/
public getSnippetConfig!: () => SnippetConfig
/**
* Callback that allows to update the snippet config. The logic of initialization is out of this
* API since this API is just a facade.
*
* @internal
*/
protected snippetCallback!: (config: Partial<SnippetConfig>) => void
/**
* Tracks that a product was added to cart from PDP.
*
* @param productId - The product id that was added to cart.
*/
addProductToCart(productId?: string): void {
void this.bus?.emit('UserClickedPDPAddToCart', productId)
}
/**
* Setter for the XBus.
*
* @param bus - The XBus received to emit events.
*
* @internal
*/
setBus(bus: XBus<XEventsTypes, WireMetadata>): void {
this.bus = bus
}
/**
* Setter for the callback to call in the init method.
*
* @param initCallback - The callback to call.
*/
setInitCallback(initCallback: (config: SnippetConfig) => any): void {
this.initCallback = initCallback
}
/**
* Setter for the callback to modify the snippet config.
*
* @param snippetCallback - The callback to call.
*
* @internal
*/
setSnippetConfigCallback(snippetCallback: (config: Partial<SnippetConfig>) => void): void {
this.snippetCallback = snippetCallback
}
/**
* Sets or updates the snippet config getter.
*
* @param snippetConfigGetter - A function that returns the snippet config.
*
* @internal
*/
setSnippetConfigGetter(snippetConfigGetter: () => NormalisedSnippetConfig): void {
this.getSnippetConfig = snippetConfigGetter
}
/**
* Sets or updates the snippet config.
*
* @param config - A part or all the snippet config.
*
* @public
*/
setSnippetConfig(config: Partial<SnippetConfig>): void {
this?.snippetCallback(config)
}
/**
* Searches the query parameter as user query.
*
* @param query - Query to be searched.
*
* @public
*/
search(query?: string): void {
if (query) {
void this.bus?.emit('UserAcceptedAQuery', query)
}
void this.bus?.emit('UserClickedOpenX', undefined)
}
/**
* Browses the browseField and browseValue parameters as a selected category.
*
* @param browseCategory - browseField and browseValue to be browsed.
*
* @public
*/
browse(browseCategory?: BrowsableRequest): void {
if (browseCategory) {
void this.bus?.emit('UserBrowsedToCategory', browseCategory)
}
}
/**
* Initializes the Application passing the {@link SnippetConfig}.
*
* @param config - The config coming from the customer snippet.
*
* @returns A promise that will be resolved once x components are initialized.
*
* @public
*/
async init(config: SnippetConfig): Promise<void> {
if (!this.isXInitialized) {
this.isXInitialized = true
await this?.initCallback(config)
} else {
console.warn('We know X is awesome, but you only need to initialize it once.')
}
}
/**
* Closes the Application.
*
* @public
*/
close(): void {
void this.bus?.emit('UserClickedCloseX', undefined)
}
}