@searchspring/snap-preact 0.20.0
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/LICENSE +21 -0
- package/README.md +293 -0
- package/dist/cjs/Instantiators/RecommendationInstantiator.d.ts +49 -0
- package/dist/cjs/Instantiators/RecommendationInstantiator.d.ts.map +1 -0
- package/dist/cjs/Instantiators/RecommendationInstantiator.js +226 -0
- package/dist/cjs/Snap.d.ts +93 -0
- package/dist/cjs/Snap.d.ts.map +1 -0
- package/dist/cjs/Snap.js +549 -0
- package/dist/cjs/components/BranchOverride.d.ts +7 -0
- package/dist/cjs/components/BranchOverride.d.ts.map +1 -0
- package/dist/cjs/components/BranchOverride.js +237 -0
- package/dist/cjs/create/createAutocompleteController.d.ts +5 -0
- package/dist/cjs/create/createAutocompleteController.d.ts.map +1 -0
- package/dist/cjs/create/createAutocompleteController.js +25 -0
- package/dist/cjs/create/createFinderController.d.ts +5 -0
- package/dist/cjs/create/createFinderController.d.ts.map +1 -0
- package/dist/cjs/create/createFinderController.js +25 -0
- package/dist/cjs/create/createRecommendationController.d.ts +5 -0
- package/dist/cjs/create/createRecommendationController.d.ts.map +1 -0
- package/dist/cjs/create/createRecommendationController.js +25 -0
- package/dist/cjs/create/createSearchController.d.ts +5 -0
- package/dist/cjs/create/createSearchController.d.ts.map +1 -0
- package/dist/cjs/create/createSearchController.js +25 -0
- package/dist/cjs/create/index.d.ts +5 -0
- package/dist/cjs/create/index.d.ts.map +1 -0
- package/dist/cjs/create/index.js +14 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/polyfills/polyfills.d.ts +2 -0
- package/dist/cjs/polyfills/polyfills.d.ts.map +1 -0
- package/dist/cjs/polyfills/polyfills.js +31 -0
- package/dist/cjs/types.d.ts +54 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/Instantiators/RecommendationInstantiator.d.ts +49 -0
- package/dist/esm/Instantiators/RecommendationInstantiator.d.ts.map +1 -0
- package/dist/esm/Instantiators/RecommendationInstantiator.js +127 -0
- package/dist/esm/Snap.d.ts +93 -0
- package/dist/esm/Snap.d.ts.map +1 -0
- package/dist/esm/Snap.js +396 -0
- package/dist/esm/components/BranchOverride.d.ts +7 -0
- package/dist/esm/components/BranchOverride.d.ts.map +1 -0
- package/dist/esm/components/BranchOverride.js +172 -0
- package/dist/esm/create/createAutocompleteController.d.ts +5 -0
- package/dist/esm/create/createAutocompleteController.d.ts.map +1 -0
- package/dist/esm/create/createAutocompleteController.js +23 -0
- package/dist/esm/create/createFinderController.d.ts +5 -0
- package/dist/esm/create/createFinderController.d.ts.map +1 -0
- package/dist/esm/create/createFinderController.js +23 -0
- package/dist/esm/create/createRecommendationController.d.ts +5 -0
- package/dist/esm/create/createRecommendationController.d.ts.map +1 -0
- package/dist/esm/create/createRecommendationController.js +23 -0
- package/dist/esm/create/createSearchController.d.ts +5 -0
- package/dist/esm/create/createSearchController.d.ts.map +1 -0
- package/dist/esm/create/createSearchController.js +23 -0
- package/dist/esm/create/index.d.ts +5 -0
- package/dist/esm/create/index.d.ts.map +1 -0
- package/dist/esm/create/index.js +4 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/polyfills/polyfills.d.ts +2 -0
- package/dist/esm/polyfills/polyfills.d.ts.map +1 -0
- package/dist/esm/polyfills/polyfills.js +9 -0
- package/dist/esm/types.d.ts +54 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Searchspring
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# Snap Preact
|
|
2
|
+
|
|
3
|
+
<a href="https://www.npmjs.com/package/@searchspring/snap-preact"><img alt="NPM Status" src="https://img.shields.io/npm/v/@searchspring/snap-preact.svg?style=flat"></a>
|
|
4
|
+
|
|
5
|
+
Snap Preact is an abstraction layer that provides a config based interface for creating a Searchspring integration quickly.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
To install the `snap-preact` package and it's dependencies:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install --save @searchspring/snap-preact
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Instantiation
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { Snap } from '@searchspring/snap-preact';
|
|
20
|
+
|
|
21
|
+
const snap = new Snap(config);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
A configuration object provided to Snap will determin the services that will be created.
|
|
26
|
+
|
|
27
|
+
Full example:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
const config = {
|
|
31
|
+
url: {
|
|
32
|
+
parameters: {
|
|
33
|
+
core: {
|
|
34
|
+
query: { name: 'search' },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
client: {
|
|
39
|
+
globals: {
|
|
40
|
+
siteId: 'xxxxxx',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
controllers: {
|
|
44
|
+
search: [
|
|
45
|
+
{
|
|
46
|
+
config: {
|
|
47
|
+
id: 'search',
|
|
48
|
+
settings: {
|
|
49
|
+
redirects: {
|
|
50
|
+
merchandising: false,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
targets: [
|
|
55
|
+
{
|
|
56
|
+
selector: '#searchspring-content',
|
|
57
|
+
component: () => Content,
|
|
58
|
+
hideTarget: true,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
selector: '#searchspring-sidebar',
|
|
62
|
+
component: () => Sidebar,
|
|
63
|
+
hideTarget: true,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
autocomplete: [
|
|
69
|
+
{
|
|
70
|
+
config: {
|
|
71
|
+
id: 'autocomplete',
|
|
72
|
+
selector: 'input.searchspring-ac',
|
|
73
|
+
settings: {
|
|
74
|
+
trending: {
|
|
75
|
+
limit: 5,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
targets: [
|
|
80
|
+
{
|
|
81
|
+
selector: 'input.searchspring-ac',
|
|
82
|
+
component: () => Autocomplete,
|
|
83
|
+
hideTarget: true,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### config.client
|
|
93
|
+
A single client instance will be created and shared across all services using the provided config.
|
|
94
|
+
|
|
95
|
+
See [@searchspring/snap-client](https://github.com/searchspring/snap/tree/main/packages/snap-client) documentation for full client config options.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const config = {
|
|
99
|
+
client: {
|
|
100
|
+
globals: {
|
|
101
|
+
siteId: 'xxxxxx'
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### config.instantiators
|
|
108
|
+
The `instantiators` object must be defined if any Recommendation controllers have also been defined via `config.controllers.recommendation`
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const config = {
|
|
112
|
+
instantiators: {
|
|
113
|
+
recommendation: {
|
|
114
|
+
components: {
|
|
115
|
+
Standard: () => Standard
|
|
116
|
+
},
|
|
117
|
+
config: {
|
|
118
|
+
branch: BRANCHNAME,
|
|
119
|
+
batched: true
|
|
120
|
+
},
|
|
121
|
+
selector: '',
|
|
122
|
+
services: {}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
controllers: {
|
|
126
|
+
recommendation: []
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`recommendation.components` - required mapping of recommendation components.
|
|
132
|
+
|
|
133
|
+
`recommendation.config.branch` - required current git branch name. Defined via webpack during bundle build:
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
const webpack = require('webpack');
|
|
137
|
+
const childProcess = require('child_process');
|
|
138
|
+
const branchName = childProcess.execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
plugins: [
|
|
142
|
+
new webpack.DefinePlugin({
|
|
143
|
+
BRANCHNAME: `"${branchName}"`,
|
|
144
|
+
}),
|
|
145
|
+
],
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`recommendation.config.batched` - optional boolean (default: `true`) to batch multiple recommendations into a single network request
|
|
150
|
+
|
|
151
|
+
`recommendation.selector` - optional selector to target recommendation instances if using a non-standard installation. Default selector: `script[type="searchspring/recommend"]`
|
|
152
|
+
|
|
153
|
+
`recommendation.services` - optional object of `ControllerServices`
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
### config.url
|
|
158
|
+
The `url` object contains the config provided to each [`UrlTranslator`](https://github.com/searchspring/snap/tree/main/packages/snap-url-manager/src/Translators/Url) created by Snap Preact.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const config = {
|
|
162
|
+
url: {
|
|
163
|
+
parameters: {
|
|
164
|
+
core: {
|
|
165
|
+
query: { name: 'search' },
|
|
166
|
+
page: { name: 'p' }
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### config.controllers
|
|
174
|
+
The `controllers` object contains a list of controllers to create for each controller type.
|
|
175
|
+
|
|
176
|
+
Available controllers:
|
|
177
|
+
|
|
178
|
+
- [SearchController](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Search)
|
|
179
|
+
- [AutocompleteController](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Autocomplete)
|
|
180
|
+
- [FinderController](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Finder)
|
|
181
|
+
- [RecommendationController](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Recommendation)
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
const config = {
|
|
185
|
+
controllers: {
|
|
186
|
+
search: [],
|
|
187
|
+
autocomplete: [],
|
|
188
|
+
finder: [],
|
|
189
|
+
recommendation: [],
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Each array entry contains an object with the following properties:
|
|
195
|
+
|
|
196
|
+
`config` - required controller config for the corresponding controller. See Controller specific documentation for all available configuration options.
|
|
197
|
+
|
|
198
|
+
`targets` - optional array of Target objects. Targets thats have been found will have the corresponding controller provided to the target component `controller` prop and the controller's `search` method invoked.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
type ExtendedTarget = {
|
|
202
|
+
selector: string;
|
|
203
|
+
inject?: {
|
|
204
|
+
action: 'before' | 'after' | 'append' | 'prepend' | 'replace';
|
|
205
|
+
element: Element | ((target: Target, element: Element) => Element);
|
|
206
|
+
};
|
|
207
|
+
hideTarget?: boolean;
|
|
208
|
+
emptyTarget?: boolean;
|
|
209
|
+
name?: string;
|
|
210
|
+
component?: () => Promise<RootComponent> | RootComponent;
|
|
211
|
+
props?: unknown; // additional props to pass to the component
|
|
212
|
+
onTarget?: OnTarget; // additional scripts to execute when target is found
|
|
213
|
+
prefetch?: boolean; // run controller search before finding targets
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
`services` - optional object of `ControllerServices` to be used for this controller in place of the default services
|
|
218
|
+
|
|
219
|
+
`url` - optional `UrlTranslator` config object to be used with the `UrlManager` for this controller
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
An example creating a SearchController:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const config = {
|
|
226
|
+
controllers: {
|
|
227
|
+
search: [
|
|
228
|
+
{
|
|
229
|
+
config: {
|
|
230
|
+
id: 'search',
|
|
231
|
+
},
|
|
232
|
+
targets: [
|
|
233
|
+
{
|
|
234
|
+
selector: '#searchspring-content',
|
|
235
|
+
component: () => Content,
|
|
236
|
+
hideTarget: true,
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
selector: '#searchspring-sidebar',
|
|
240
|
+
component: () => Sidebar,
|
|
241
|
+
hideTarget: true,
|
|
242
|
+
},
|
|
243
|
+
],
|
|
244
|
+
services: {}
|
|
245
|
+
}
|
|
246
|
+
]
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
The controller config `id` will be the name of the controller that you will then interface from the return of creating the `new Snap()` instance via the `controllers` object.
|
|
251
|
+
|
|
252
|
+
For example, if using the `config` example above:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
const snap = new Snap(config);
|
|
256
|
+
const { search } = snap.controllers;
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## properties
|
|
260
|
+
|
|
261
|
+
After instantiating an instance of Snap, the following properties can be accessed.
|
|
262
|
+
|
|
263
|
+
### config
|
|
264
|
+
A reference to the config that was provided.
|
|
265
|
+
|
|
266
|
+
### logger
|
|
267
|
+
A reference to the shared [@searchspring/snap-logger](https://github.com/searchspring/snap/tree/main/packages/snap-logger) instance used by each controller.
|
|
268
|
+
|
|
269
|
+
### client
|
|
270
|
+
A reference to the shared [@searchspring/snap-client](https://github.com/searchspring/snap/tree/main/packages/snap-client) instance used by each controller.
|
|
271
|
+
|
|
272
|
+
### tracker
|
|
273
|
+
A reference to the shared [@searchspring/snap-tracker](https://github.com/searchspring/snap/tree/main/packages/snap-tracker) instance used by each controller.
|
|
274
|
+
|
|
275
|
+
### controllers
|
|
276
|
+
An object containing all controllers that have been created.
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
### recommendations
|
|
280
|
+
A reference to `RecommendationInstantiator` instance if creating recommendation instances.
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
## polyfills
|
|
284
|
+
|
|
285
|
+
Snap Preact provides various polyfills to ensure legacy browser support.
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
import { polyfills } from '@searchspring/snap-preact';
|
|
289
|
+
|
|
290
|
+
polyfills.then(() => {
|
|
291
|
+
import('./index');
|
|
292
|
+
})
|
|
293
|
+
```
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DomTargeter } from '@searchspring/snap-toolbox';
|
|
2
|
+
import type { Logger } from '@searchspring/snap-logger';
|
|
3
|
+
import type { UrlTranslatorConfig } from '@searchspring/snap-url-manager';
|
|
4
|
+
import type { Client } from '@searchspring/snap-client';
|
|
5
|
+
import type { Tracker } from '@searchspring/snap-tracker';
|
|
6
|
+
import type { AbstractController, RecommendationController, Attachments } from '@searchspring/snap-controller';
|
|
7
|
+
import type { Middleware } from '@searchspring/snap-event-manager';
|
|
8
|
+
import type { SnapControllerServices, RootComponent } from '../types';
|
|
9
|
+
export declare type RecommendationInstantiatorConfig = {
|
|
10
|
+
components: {
|
|
11
|
+
[name: string]: () => Promise<RootComponent> | RootComponent;
|
|
12
|
+
};
|
|
13
|
+
config: {
|
|
14
|
+
branch: string;
|
|
15
|
+
realtime?: boolean;
|
|
16
|
+
batched?: boolean;
|
|
17
|
+
} & Attachments;
|
|
18
|
+
selector?: string;
|
|
19
|
+
services?: SnapControllerServices;
|
|
20
|
+
url?: UrlTranslatorConfig;
|
|
21
|
+
};
|
|
22
|
+
export declare type RecommendationInstantiatorServices = {
|
|
23
|
+
client: Client;
|
|
24
|
+
logger: Logger;
|
|
25
|
+
tracker: Tracker;
|
|
26
|
+
};
|
|
27
|
+
export declare class RecommendationInstantiator {
|
|
28
|
+
controllers: {
|
|
29
|
+
[key: string]: RecommendationController;
|
|
30
|
+
};
|
|
31
|
+
client: Client;
|
|
32
|
+
tracker: Tracker;
|
|
33
|
+
logger: Logger;
|
|
34
|
+
config: RecommendationInstantiatorConfig;
|
|
35
|
+
uses: Attachments[];
|
|
36
|
+
plugins: {
|
|
37
|
+
(cntrlr: AbstractController): Promise<void>;
|
|
38
|
+
}[];
|
|
39
|
+
middleware: {
|
|
40
|
+
event: string;
|
|
41
|
+
func: Middleware<unknown>[];
|
|
42
|
+
}[];
|
|
43
|
+
targeter: DomTargeter;
|
|
44
|
+
constructor(config: RecommendationInstantiatorConfig, { client, logger, tracker }: RecommendationInstantiatorServices);
|
|
45
|
+
plugin(func: (cntrlr: AbstractController) => Promise<void>): void;
|
|
46
|
+
on<T>(event: string, ...func: Middleware<T>[]): void;
|
|
47
|
+
use(attachments: Attachments): void;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=RecommendationInstantiator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RecommendationInstantiator.d.ts","sourceRoot":"","sources":["../../../src/Instantiators/RecommendationInstantiator.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAc,MAAM,4BAA4B,CAAC;AAErE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,KAAK,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC/G,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,KAAK,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,oBAAY,gCAAgC,GAAG;IAC9C,UAAU,EAAE;QACX,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,GAAG,aAAa,CAAC;KAC7D,CAAC;IACF,MAAM,EAAE;QACP,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,OAAO,CAAC,EAAE,OAAO,CAAC;KAClB,GAAG,WAAW,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,sBAAsB,CAAC;IAClC,GAAG,CAAC,EAAE,mBAAmB,CAAC;CAC1B,CAAC;AAEF,oBAAY,kCAAkC,GAAG;IAChD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,qBAAa,0BAA0B;IACtC,WAAW,EAAE;QACZ,CAAC,GAAG,EAAE,MAAM,GAAG,wBAAwB,CAAC;KACxC,CAAM;IACP,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,gCAAgC,CAAC;IACzC,IAAI,EAAE,WAAW,EAAE,CAAM;IACzB,OAAO,EAAE;QAAE,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,EAAE,CAAM;IAChE,UAAU,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,CAAA;KAAE,EAAE,CAAM;IAC3D,QAAQ,EAAE,WAAW,CAAC;gBAEjB,MAAM,EAAE,gCAAgC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,kCAAkC;IA8I9G,MAAM,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAIjE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI;IAIpD,GAAG,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI;CAG1C"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
26
|
+
if (mod && mod.__esModule) return mod;
|
|
27
|
+
var result = {};
|
|
28
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
29
|
+
__setModuleDefault(result, mod);
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
33
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
42
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
43
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
44
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
45
|
+
function step(op) {
|
|
46
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
47
|
+
while (_) try {
|
|
48
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
49
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
50
|
+
switch (op[0]) {
|
|
51
|
+
case 0: case 1: t = op; break;
|
|
52
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
53
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
54
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
55
|
+
default:
|
|
56
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
57
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
58
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
59
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
60
|
+
if (t[2]) _.ops.pop();
|
|
61
|
+
_.trys.pop(); continue;
|
|
62
|
+
}
|
|
63
|
+
op = body.call(thisArg, _);
|
|
64
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
65
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
69
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
70
|
+
if (ar || !(i in from)) {
|
|
71
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
72
|
+
ar[i] = from[i];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
76
|
+
};
|
|
77
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
78
|
+
exports.RecommendationInstantiator = void 0;
|
|
79
|
+
var jsx_runtime_1 = require("preact/jsx-runtime");
|
|
80
|
+
var preact_1 = require("preact");
|
|
81
|
+
var snap_toolbox_1 = require("@searchspring/snap-toolbox");
|
|
82
|
+
var RecommendationInstantiator = /** @class */ (function () {
|
|
83
|
+
function RecommendationInstantiator(config, _a) {
|
|
84
|
+
var _this = this;
|
|
85
|
+
var client = _a.client, logger = _a.logger, tracker = _a.tracker;
|
|
86
|
+
var _b;
|
|
87
|
+
this.controllers = {};
|
|
88
|
+
this.uses = [];
|
|
89
|
+
this.plugins = [];
|
|
90
|
+
this.middleware = [];
|
|
91
|
+
this.config = config;
|
|
92
|
+
if (!this.config) {
|
|
93
|
+
throw new Error("Recommendation Instantiator config is required");
|
|
94
|
+
}
|
|
95
|
+
if (!((_b = this.config.config) === null || _b === void 0 ? void 0 : _b.branch)) {
|
|
96
|
+
throw new Error("Recommendation Instantiator config must contain 'branch' property");
|
|
97
|
+
}
|
|
98
|
+
if (!this.config.components || typeof this.config.components != 'object') {
|
|
99
|
+
throw new Error("Recommendation Instantiator config must contain 'components' mapping property");
|
|
100
|
+
}
|
|
101
|
+
this.client = client;
|
|
102
|
+
this.tracker = tracker;
|
|
103
|
+
this.logger = logger;
|
|
104
|
+
var profileCount = {};
|
|
105
|
+
this.targeter = new snap_toolbox_1.DomTargeter([
|
|
106
|
+
{
|
|
107
|
+
selector: "script[type=\"searchspring/recommend\"], script[type=\"searchspring/personalized-recommendations\"]".concat(this.config.selector ? " , ".concat(this.config.selector) : ''),
|
|
108
|
+
inject: {
|
|
109
|
+
action: 'before',
|
|
110
|
+
element: function (target, origElement) {
|
|
111
|
+
var profile = origElement.getAttribute('profile');
|
|
112
|
+
if (profile) {
|
|
113
|
+
var recsContainer = document.createElement('div');
|
|
114
|
+
recsContainer.setAttribute('searchspring-recommend', profile);
|
|
115
|
+
return recsContainer;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
_this.logger.warn("'profile' attribute is missing from <script> tag, skipping this profile", origElement);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
], function (target, injectedElem, elem) { return __awaiter(_this, void 0, void 0, function () {
|
|
124
|
+
var globals, _a, shopper, shopperId, product, seed, options, tag, controllerConfig, createRecommendationController, client, tracker, recs, profileVars, component, RecommendationsComponent, _b;
|
|
125
|
+
var _c, _d, _e, _f;
|
|
126
|
+
return __generator(this, function (_g) {
|
|
127
|
+
switch (_g.label) {
|
|
128
|
+
case 0:
|
|
129
|
+
globals = {};
|
|
130
|
+
_a = (0, snap_toolbox_1.getContext)(['shopperId', 'shopper', 'product', 'seed', 'options'], elem), shopper = _a.shopper, shopperId = _a.shopperId, product = _a.product, seed = _a.seed, options = _a.options;
|
|
131
|
+
/*
|
|
132
|
+
type instantiatorContext = {
|
|
133
|
+
shopper?: string;
|
|
134
|
+
shopperId?: string;
|
|
135
|
+
product?: string;
|
|
136
|
+
seed?: string;
|
|
137
|
+
options?: {
|
|
138
|
+
siteId?: string;
|
|
139
|
+
branch?: string;
|
|
140
|
+
batched?: boolean;
|
|
141
|
+
realtime?: boolean;
|
|
142
|
+
categories?: any;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
*/
|
|
146
|
+
if (shopper || shopperId) {
|
|
147
|
+
globals.shopper = shopper || shopperId;
|
|
148
|
+
}
|
|
149
|
+
if (product || seed) {
|
|
150
|
+
globals.product = product || seed;
|
|
151
|
+
}
|
|
152
|
+
if (options === null || options === void 0 ? void 0 : options.branch) {
|
|
153
|
+
globals.branch = options.branch;
|
|
154
|
+
}
|
|
155
|
+
if (options === null || options === void 0 ? void 0 : options.siteId) {
|
|
156
|
+
globals.siteId = options.siteId;
|
|
157
|
+
}
|
|
158
|
+
if (options === null || options === void 0 ? void 0 : options.categories) {
|
|
159
|
+
globals.categories = options.categories;
|
|
160
|
+
}
|
|
161
|
+
tag = injectedElem.getAttribute('searchspring-recommend');
|
|
162
|
+
profileCount[tag] = profileCount[tag] + 1 || 1;
|
|
163
|
+
controllerConfig = __assign({ id: "recommend_".concat(tag + (profileCount[tag] - 1)), tag: tag, batched: (_c = options === null || options === void 0 ? void 0 : options.batched) !== null && _c !== void 0 ? _c : true, realtime: Boolean(options === null || options === void 0 ? void 0 : options.realtime), globals: globals }, this.config.config);
|
|
164
|
+
return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require('../create/createRecommendationController')); })];
|
|
165
|
+
case 1:
|
|
166
|
+
createRecommendationController = (_g.sent()).default;
|
|
167
|
+
client = ((_d = this.config.services) === null || _d === void 0 ? void 0 : _d.client) || this.client;
|
|
168
|
+
tracker = ((_e = this.config.services) === null || _e === void 0 ? void 0 : _e.tracker) || this.tracker;
|
|
169
|
+
recs = createRecommendationController({
|
|
170
|
+
url: this.config.url || {},
|
|
171
|
+
controller: controllerConfig,
|
|
172
|
+
}, { client: client, tracker: tracker });
|
|
173
|
+
this.uses.forEach(function (attachements) { return recs.use(attachements); });
|
|
174
|
+
this.plugins.forEach(function (plugin) { return recs.plugin(plugin); });
|
|
175
|
+
this.middleware.forEach(function (middleware) { return recs.on.apply(recs, __spreadArray([middleware.event], middleware.func, false)); });
|
|
176
|
+
return [4 /*yield*/, recs.search()];
|
|
177
|
+
case 2:
|
|
178
|
+
_g.sent();
|
|
179
|
+
recs.addTargeter(this.targeter);
|
|
180
|
+
this.controllers[recs.config.id] = recs;
|
|
181
|
+
profileVars = recs.store.profile.display.templateParameters;
|
|
182
|
+
component = (_f = recs.store.profile.display.template) === null || _f === void 0 ? void 0 : _f.component;
|
|
183
|
+
if (!profileVars) {
|
|
184
|
+
recs.log.error("profile failed to load!");
|
|
185
|
+
return [2 /*return*/];
|
|
186
|
+
}
|
|
187
|
+
if (!component) {
|
|
188
|
+
recs.log.error("template does not support components!");
|
|
189
|
+
return [2 /*return*/];
|
|
190
|
+
}
|
|
191
|
+
_b = this.config.components;
|
|
192
|
+
if (!_b) return [3 /*break*/, 4];
|
|
193
|
+
return [4 /*yield*/, this.config.components[component]()];
|
|
194
|
+
case 3:
|
|
195
|
+
_b = (_g.sent());
|
|
196
|
+
_g.label = 4;
|
|
197
|
+
case 4:
|
|
198
|
+
RecommendationsComponent = _b;
|
|
199
|
+
if (!RecommendationsComponent) {
|
|
200
|
+
recs.log.error("component '".concat(profileVars.component, "' not found!"));
|
|
201
|
+
return [2 /*return*/];
|
|
202
|
+
}
|
|
203
|
+
setTimeout(function () {
|
|
204
|
+
(0, preact_1.render)((0, jsx_runtime_1.jsx)(RecommendationsComponent, { controller: recs }, void 0), injectedElem);
|
|
205
|
+
});
|
|
206
|
+
return [2 /*return*/];
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}); });
|
|
210
|
+
}
|
|
211
|
+
RecommendationInstantiator.prototype.plugin = function (func) {
|
|
212
|
+
this.plugins.push(func);
|
|
213
|
+
};
|
|
214
|
+
RecommendationInstantiator.prototype.on = function (event) {
|
|
215
|
+
var func = [];
|
|
216
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
217
|
+
func[_i - 1] = arguments[_i];
|
|
218
|
+
}
|
|
219
|
+
this.middleware.push({ event: event, func: func });
|
|
220
|
+
};
|
|
221
|
+
RecommendationInstantiator.prototype.use = function (attachments) {
|
|
222
|
+
this.uses.push(attachments);
|
|
223
|
+
};
|
|
224
|
+
return RecommendationInstantiator;
|
|
225
|
+
}());
|
|
226
|
+
exports.RecommendationInstantiator = RecommendationInstantiator;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Client } from '@searchspring/snap-client';
|
|
2
|
+
import { Logger } from '@searchspring/snap-logger';
|
|
3
|
+
import { Tracker } from '@searchspring/snap-tracker';
|
|
4
|
+
import type { ClientConfig, ClientGlobals } from '@searchspring/snap-client';
|
|
5
|
+
import type { AbstractController, SearchController, AutocompleteController, FinderController, RecommendationController, SearchControllerConfig, AutocompleteControllerConfig, FinderControllerConfig, RecommendationControllerConfig, ControllerConfigs } from '@searchspring/snap-controller';
|
|
6
|
+
import type { Product } from '@searchspring/snap-tracker';
|
|
7
|
+
import type { Target, OnTarget } from '@searchspring/snap-toolbox';
|
|
8
|
+
import type { UrlTranslatorConfig } from '@searchspring/snap-url-manager';
|
|
9
|
+
import { RecommendationInstantiator, RecommendationInstantiatorConfig } from './Instantiators/RecommendationInstantiator';
|
|
10
|
+
import type { SnapControllerServices, RootComponent } from './types';
|
|
11
|
+
declare type ExtendedTarget = Target & {
|
|
12
|
+
name?: string;
|
|
13
|
+
controller?: AbstractController;
|
|
14
|
+
component?: () => Promise<RootComponent> | RootComponent;
|
|
15
|
+
skeleton?: () => Promise<any>;
|
|
16
|
+
props?: unknown;
|
|
17
|
+
onTarget?: OnTarget;
|
|
18
|
+
prefetch?: boolean;
|
|
19
|
+
};
|
|
20
|
+
declare type ContextVariables = {
|
|
21
|
+
shopper?: {
|
|
22
|
+
id: string;
|
|
23
|
+
cart?: Product[];
|
|
24
|
+
[variable: string]: any;
|
|
25
|
+
};
|
|
26
|
+
[variable: string]: any;
|
|
27
|
+
};
|
|
28
|
+
export declare type SnapConfig = {
|
|
29
|
+
context?: ContextVariables;
|
|
30
|
+
url?: UrlTranslatorConfig;
|
|
31
|
+
client: {
|
|
32
|
+
globals: ClientGlobals;
|
|
33
|
+
config?: ClientConfig;
|
|
34
|
+
};
|
|
35
|
+
instantiators?: {
|
|
36
|
+
recommendation?: RecommendationInstantiatorConfig;
|
|
37
|
+
};
|
|
38
|
+
controllers?: {
|
|
39
|
+
search?: {
|
|
40
|
+
config: SearchControllerConfig;
|
|
41
|
+
targeters?: ExtendedTarget[];
|
|
42
|
+
services?: SnapControllerServices;
|
|
43
|
+
url?: UrlTranslatorConfig;
|
|
44
|
+
}[];
|
|
45
|
+
autocomplete?: {
|
|
46
|
+
config: AutocompleteControllerConfig;
|
|
47
|
+
targeters: ExtendedTarget[];
|
|
48
|
+
services?: SnapControllerServices;
|
|
49
|
+
url?: UrlTranslatorConfig;
|
|
50
|
+
}[];
|
|
51
|
+
finder?: {
|
|
52
|
+
config: FinderControllerConfig;
|
|
53
|
+
targeters?: ExtendedTarget[];
|
|
54
|
+
services?: SnapControllerServices;
|
|
55
|
+
url?: UrlTranslatorConfig;
|
|
56
|
+
}[];
|
|
57
|
+
recommendation?: {
|
|
58
|
+
config: RecommendationControllerConfig;
|
|
59
|
+
targeters?: ExtendedTarget[];
|
|
60
|
+
services?: SnapControllerServices;
|
|
61
|
+
url?: UrlTranslatorConfig;
|
|
62
|
+
}[];
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
declare type ControllerTypes = SearchController | AutocompleteController | FinderController | RecommendationController;
|
|
66
|
+
declare enum DynamicImportNames {
|
|
67
|
+
SEARCH = "searchController",
|
|
68
|
+
AUTOCOMPLETE = "autocompleteController",
|
|
69
|
+
FINDER = "finderController",
|
|
70
|
+
RECOMMENDATION = "recommendationController"
|
|
71
|
+
}
|
|
72
|
+
export declare class Snap {
|
|
73
|
+
config: SnapConfig;
|
|
74
|
+
logger: Logger;
|
|
75
|
+
client: Client;
|
|
76
|
+
tracker: Tracker;
|
|
77
|
+
_controllerPromises: {
|
|
78
|
+
[controllerConfigId: string]: Promise<ControllerTypes>;
|
|
79
|
+
};
|
|
80
|
+
controllers: {
|
|
81
|
+
[controllerConfigId: string]: ControllerTypes;
|
|
82
|
+
};
|
|
83
|
+
_instantiatorPromises: {
|
|
84
|
+
[instantiatorId: string]: Promise<RecommendationInstantiator>;
|
|
85
|
+
};
|
|
86
|
+
getInstantiator: (id: string) => Promise<RecommendationInstantiator>;
|
|
87
|
+
getController: (id: string) => Promise<ControllerTypes>;
|
|
88
|
+
getControllers: (...controllerIds: string[]) => Promise<ControllerTypes[]>;
|
|
89
|
+
createController: (type: DynamicImportNames, config: ControllerConfigs, services: SnapControllerServices, urlConfig: UrlTranslatorConfig, resolve: (value?: ControllerTypes | PromiseLike<ControllerTypes>) => void) => Promise<ControllerTypes>;
|
|
90
|
+
constructor(config: SnapConfig);
|
|
91
|
+
}
|
|
92
|
+
export {};
|
|
93
|
+
//# sourceMappingURL=Snap.d.ts.map
|