@constructor-io/constructorio-client-javascript 2.31.0 → 2.33.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/lib/modules/quizzes.js +16 -16
- package/lib/modules/recommendations.js +1 -2
- package/lib/modules/search.js +1 -0
- package/lib/types/autocomplete.d.ts +67 -0
- package/lib/types/browse.d.ts +133 -0
- package/lib/types/constructorio.d.ts +29 -0
- package/lib/types/event-dispatcher.d.ts +13 -0
- package/lib/types/recommendations.d.ts +77 -0
- package/lib/types/search.d.ts +109 -0
- package/lib/types/tracker.d.ts +162 -0
- package/lib/types/types.d.ts +126 -0
- package/lib/utils/helpers.js +1 -1
- package/package.json +13 -3
package/lib/modules/quizzes.js
CHANGED
|
@@ -48,13 +48,13 @@ function createQuizUrl(quizId, parameters, options, path) {
|
|
|
48
48
|
throw new Error('quizId is a required parameter of type string');
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
if (path === 'finalize' && ((0, _typeof2["default"])(parameters.
|
|
52
|
-
throw new Error('
|
|
51
|
+
if (path === 'finalize' && ((0, _typeof2["default"])(parameters.answers) !== 'object' || !Array.isArray(parameters.answers) || parameters.answers.length === 0)) {
|
|
52
|
+
throw new Error('answers is a required parameter of type array');
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (parameters) {
|
|
56
56
|
var section = parameters.section,
|
|
57
|
-
|
|
57
|
+
answers = parameters.answers,
|
|
58
58
|
versionId = parameters.versionId; // Pull section from parameters
|
|
59
59
|
|
|
60
60
|
if (section) {
|
|
@@ -64,11 +64,11 @@ function createQuizUrl(quizId, parameters, options, path) {
|
|
|
64
64
|
|
|
65
65
|
if (versionId) {
|
|
66
66
|
queryParams.version_id = versionId;
|
|
67
|
-
} // Pull
|
|
67
|
+
} // Pull answers from parameters.answers and transform
|
|
68
68
|
|
|
69
69
|
|
|
70
|
-
if (
|
|
71
|
-
|
|
70
|
+
if (answers) {
|
|
71
|
+
answers.forEach(function (ans) {
|
|
72
72
|
answersParamString += "&".concat(qs.stringify({
|
|
73
73
|
a: ans
|
|
74
74
|
}, {
|
|
@@ -103,20 +103,20 @@ var Quizzes = /*#__PURE__*/function () {
|
|
|
103
103
|
/**
|
|
104
104
|
* Retrieve next question from API
|
|
105
105
|
*
|
|
106
|
-
* @function
|
|
106
|
+
* @function getQuizNextQuestion
|
|
107
107
|
* @description Retrieve next question from Constructor.io API
|
|
108
108
|
* @param {string} id - The identifier of the quiz
|
|
109
109
|
* @param {string} [parameters] - Additional parameters to refine result set
|
|
110
110
|
* @param {string} [parameters.section] - Product catalog section
|
|
111
|
-
* @param {array} [parameters.
|
|
111
|
+
* @param {array} [parameters.answers] - An array of answers in the format [[1,2],[1]]
|
|
112
112
|
* @param {string} [parameters.versionId] - Version identifier for the quiz
|
|
113
113
|
* @param {object} [networkParameters] - Parameters relevant to the network request
|
|
114
114
|
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
|
|
115
115
|
* @returns {Promise}
|
|
116
116
|
* @see https://docs.constructor.io/rest_api/quiz/using_quizzes/#answering-a-quiz
|
|
117
117
|
* @example
|
|
118
|
-
* constructorio.quizzes.
|
|
119
|
-
*
|
|
118
|
+
* constructorio.quizzes.getQuizNextQuestion('quizId', {
|
|
119
|
+
* answers: [[1,2],[1]],
|
|
120
120
|
* section: '123',
|
|
121
121
|
* versionId: '123'
|
|
122
122
|
* });
|
|
@@ -124,8 +124,8 @@ var Quizzes = /*#__PURE__*/function () {
|
|
|
124
124
|
|
|
125
125
|
|
|
126
126
|
(0, _createClass2["default"])(Quizzes, [{
|
|
127
|
-
key: "
|
|
128
|
-
value: function
|
|
127
|
+
key: "getQuizNextQuestion",
|
|
128
|
+
value: function getQuizNextQuestion(quizId, parameters) {
|
|
129
129
|
var _this = this;
|
|
130
130
|
|
|
131
131
|
var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
@@ -154,12 +154,12 @@ var Quizzes = /*#__PURE__*/function () {
|
|
|
154
154
|
return helpers.throwHttpErrorFromResponse(new Error(), response);
|
|
155
155
|
}).then(function (json) {
|
|
156
156
|
if (json.version_id) {
|
|
157
|
-
_this.eventDispatcher.queue('quizzes.
|
|
157
|
+
_this.eventDispatcher.queue('quizzes.getQuizNextQuestion.completed', json);
|
|
158
158
|
|
|
159
159
|
return json;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
throw new Error('
|
|
162
|
+
throw new Error('getQuizNextQuestion response data is malformed');
|
|
163
163
|
});
|
|
164
164
|
}
|
|
165
165
|
/**
|
|
@@ -170,7 +170,7 @@ var Quizzes = /*#__PURE__*/function () {
|
|
|
170
170
|
* @param {string} id - The identifier of the quiz
|
|
171
171
|
* @param {string} [parameters] - Additional parameters to refine result set
|
|
172
172
|
* @param {string} [parameters.section] - Product catalog section
|
|
173
|
-
* @param {array} [parameters.
|
|
173
|
+
* @param {array} [parameters.answers] - An array of answers in the format [[1,2],[1]]
|
|
174
174
|
* @param {string} [parameters.versionId] - Specific version identifier for the quiz
|
|
175
175
|
* @param {object} [networkParameters] - Parameters relevant to the network request
|
|
176
176
|
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
|
|
@@ -178,7 +178,7 @@ var Quizzes = /*#__PURE__*/function () {
|
|
|
178
178
|
* @see https://docs.constructor.io/rest_api/quiz/using_quizzes/#completing-the-quiz
|
|
179
179
|
* @example
|
|
180
180
|
* constructorio.quizzes.getQuizResults('quizId', {
|
|
181
|
-
*
|
|
181
|
+
* answers: [[1,2],[1]],
|
|
182
182
|
* section: '123',
|
|
183
183
|
* versionId: '123'
|
|
184
184
|
* });
|
|
@@ -149,8 +149,6 @@ var Recommendations = /*#__PURE__*/function () {
|
|
|
149
149
|
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
parameters = parameters || {};
|
|
153
|
-
|
|
154
152
|
try {
|
|
155
153
|
requestUrl = createRecommendationsUrl(podId, parameters, this.options);
|
|
156
154
|
} catch (e) {
|
|
@@ -170,6 +168,7 @@ var Recommendations = /*#__PURE__*/function () {
|
|
|
170
168
|
if (json.result_id) {
|
|
171
169
|
// Append `result_id` to each result item
|
|
172
170
|
json.response.results.forEach(function (result) {
|
|
171
|
+
// eslint-disable-next-line no-param-reassign
|
|
173
172
|
result.result_id = json.result_id;
|
|
174
173
|
});
|
|
175
174
|
}
|
package/lib/modules/search.js
CHANGED
|
@@ -242,6 +242,7 @@ var Search = /*#__PURE__*/function () {
|
|
|
242
242
|
// Search results
|
|
243
243
|
if (json.response && json.response.results) {
|
|
244
244
|
if (json.result_id) {
|
|
245
|
+
// Append `result_id` to each result item
|
|
245
246
|
json.response.results.forEach(function (result) {
|
|
246
247
|
// eslint-disable-next-line no-param-reassign
|
|
247
248
|
result.result_id = json.result_id;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConstructorClientOptions,
|
|
3
|
+
RequestFeature,
|
|
4
|
+
RequestFeatureVariant,
|
|
5
|
+
} from "./types";
|
|
6
|
+
import EventDispatcher from "./event-dispatcher";
|
|
7
|
+
|
|
8
|
+
/***************
|
|
9
|
+
*
|
|
10
|
+
* Autocomplete
|
|
11
|
+
*
|
|
12
|
+
***************/
|
|
13
|
+
export = Autocomplete;
|
|
14
|
+
|
|
15
|
+
interface IAutocompleteParameters {
|
|
16
|
+
numResults: number;
|
|
17
|
+
filters: Record<string, any>;
|
|
18
|
+
resultsPerSection: Record<string, number>;
|
|
19
|
+
hiddenFields: string[];
|
|
20
|
+
variationsMap: Record<string, any>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare class Autocomplete {
|
|
24
|
+
constructor(options: ConstructorClientOptions);
|
|
25
|
+
options: ConstructorClientOptions;
|
|
26
|
+
eventDispatcher: EventDispatcher;
|
|
27
|
+
|
|
28
|
+
getAutocompleteResults(
|
|
29
|
+
query: string,
|
|
30
|
+
parameters?: IAutocompleteParameters,
|
|
31
|
+
networkParameters?: {
|
|
32
|
+
timeout?: number;
|
|
33
|
+
}
|
|
34
|
+
): Promise<Autocomplete.AutocompleteResponse>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/***********
|
|
38
|
+
*
|
|
39
|
+
* Autocomplete results returned from server
|
|
40
|
+
*
|
|
41
|
+
***********/
|
|
42
|
+
declare namespace Autocomplete {
|
|
43
|
+
export interface AutocompleteResponse extends Record<string, any> {
|
|
44
|
+
request: Partial<Request>;
|
|
45
|
+
sections: Record<string, Section>;
|
|
46
|
+
result_id: string;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface Request extends Record<string, any> {
|
|
51
|
+
num_results: number;
|
|
52
|
+
term: string;
|
|
53
|
+
query: string;
|
|
54
|
+
features: Partial<RequestFeature>;
|
|
55
|
+
feature_variants: Partial<RequestFeatureVariant>;
|
|
56
|
+
searchandized_items: Record<string, any>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type Section = Partial<SectionItem>[]
|
|
60
|
+
|
|
61
|
+
interface SectionItem extends Record<string, any> {
|
|
62
|
+
data: Record<string, any>;
|
|
63
|
+
is_slotted: boolean;
|
|
64
|
+
labels: Record<string, any>;
|
|
65
|
+
matched_terms: string[];
|
|
66
|
+
value: string;
|
|
67
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Collection, ConstructorClientOptions, ErrorData, Facet, Feature, Group, RequestFeature, RequestFeatureVariant, ResultSources, SortOption } from "./types";
|
|
2
|
+
import EventDispatcher from "./event-dispatcher";
|
|
3
|
+
|
|
4
|
+
/*********
|
|
5
|
+
*
|
|
6
|
+
* Browse
|
|
7
|
+
*
|
|
8
|
+
*********/
|
|
9
|
+
export = Browse;
|
|
10
|
+
|
|
11
|
+
interface IBrowseParameters {
|
|
12
|
+
page?: number;
|
|
13
|
+
offset?: number;
|
|
14
|
+
resultsPerPage?: number;
|
|
15
|
+
filters?: Record<string, any>;
|
|
16
|
+
sortBy?: string;
|
|
17
|
+
sortOrder?: string;
|
|
18
|
+
section?: string;
|
|
19
|
+
fmtOptions?: object;
|
|
20
|
+
preFilterExpression: Record<string, any>;
|
|
21
|
+
hiddenFields?: string[];
|
|
22
|
+
hiddenFacets?: string[];
|
|
23
|
+
variationsMap?: Record<string, any>;
|
|
24
|
+
qs?: Record<string, any>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare class Browse {
|
|
28
|
+
constructor(options: ConstructorClientOptions);
|
|
29
|
+
options: ConstructorClientOptions;
|
|
30
|
+
eventDispatcher: EventDispatcher;
|
|
31
|
+
|
|
32
|
+
getBrowseResults(
|
|
33
|
+
filterName: string,
|
|
34
|
+
filterValue: string,
|
|
35
|
+
parameters?: IBrowseParameters,
|
|
36
|
+
networkParameters?: {
|
|
37
|
+
timeout?: number;
|
|
38
|
+
}
|
|
39
|
+
): Promise<Browse.GetBrowseResultsResponse>;
|
|
40
|
+
|
|
41
|
+
getBrowseResultsForItemIds(
|
|
42
|
+
itemIds: string[],
|
|
43
|
+
parameters?: Omit<IBrowseParameters, "preFilterExpression" | "preFilterExpression">,
|
|
44
|
+
networkParameters?: {
|
|
45
|
+
timeout?: number;
|
|
46
|
+
}
|
|
47
|
+
): Promise<Browse.GetBrowseResultsForItemIdsResponse>;
|
|
48
|
+
|
|
49
|
+
getBrowseGroups(
|
|
50
|
+
parameters: Pick<IBrowseParameters, "filters" | "section" | "fmtOptions">,
|
|
51
|
+
networkParameters?: {
|
|
52
|
+
timeout?: number;
|
|
53
|
+
}
|
|
54
|
+
): Promise<Browse.GetBrowseGroupsResponse>;
|
|
55
|
+
|
|
56
|
+
getBrowseFacets(
|
|
57
|
+
parameters?: Pick<IBrowseParameters, "page" | "offset" | "section" | "fmtOptions"| "resultsPerPage">,
|
|
58
|
+
networkParameters?: {
|
|
59
|
+
timeout?: number;
|
|
60
|
+
}
|
|
61
|
+
): Promise<Browse.GetBrowseFacetsResponse>;
|
|
62
|
+
|
|
63
|
+
getBrowseFacetOptions(
|
|
64
|
+
facetName: string,
|
|
65
|
+
parameters?: Pick<IBrowseParameters, "section" | "fmtOptions">,
|
|
66
|
+
networkParameters?: {
|
|
67
|
+
timeout?: number;
|
|
68
|
+
}
|
|
69
|
+
): Promise<Browse.GetBrowseFacetOptionsResponse>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/***********
|
|
73
|
+
*
|
|
74
|
+
* Browse results returned from server
|
|
75
|
+
*
|
|
76
|
+
***********/
|
|
77
|
+
declare namespace Browse {
|
|
78
|
+
export type GetBrowseResultsResponse = BrowseResponse<GetBrowseResultsResponseData>;
|
|
79
|
+
export type GetBrowseResultsForItemIdsResponse = BrowseResponse<GetBrowseResultsResponseData>;
|
|
80
|
+
export type GetBrowseGroupsResponse = BrowseResponse<Pick<GetBrowseResultsResponseData, "result_sources" | "groups" | "refined_content">>;
|
|
81
|
+
export type GetBrowseFacetsResponse = BrowseResponse<Pick<GetBrowseResultsResponseData, "facets" | "total_num_results">>;
|
|
82
|
+
export type GetBrowseFacetOptionsResponse = BrowseResponse<Pick<GetBrowseResultsResponseData, "facets">>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface BrowseResponse<ResponseType> extends Record<string, any> {
|
|
86
|
+
request?: Partial<Request>;
|
|
87
|
+
response?: Partial<ResponseType>;
|
|
88
|
+
result_id?: string;
|
|
89
|
+
ad_based?: boolean;
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface GetBrowseResultsResponseData extends Record<string, any> {
|
|
94
|
+
result_sources: Partial<ResultSources>;
|
|
95
|
+
facets: Partial<Facet>[];
|
|
96
|
+
groups: Partial<Group>[];
|
|
97
|
+
results: Partial<BrowseResultData>[];
|
|
98
|
+
sort_options: Partial<SortOption>[];
|
|
99
|
+
refined_content: Record<string, any>[];
|
|
100
|
+
total_num_results: number;
|
|
101
|
+
features: Partial<Feature>[];
|
|
102
|
+
collection: Partial<Collection>
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface BrowseResultData extends Record<string, any> {
|
|
106
|
+
matched_terms: string[];
|
|
107
|
+
data: {
|
|
108
|
+
id: string;
|
|
109
|
+
[key: string]: any;
|
|
110
|
+
};
|
|
111
|
+
value: string;
|
|
112
|
+
is_slotted: false;
|
|
113
|
+
labels: Record<string, any>;
|
|
114
|
+
variations: Record<string, any>[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface Request extends Record<string, any> {
|
|
118
|
+
browse_filter_name: string;
|
|
119
|
+
browse_filter_value: string;
|
|
120
|
+
filter_match_types: Record<string, any>,
|
|
121
|
+
filters: Record<string, any>,
|
|
122
|
+
fmt_options: Record<string, any>,
|
|
123
|
+
num_results_per_page: number;
|
|
124
|
+
page: number,
|
|
125
|
+
section: string,
|
|
126
|
+
sort_by: string,
|
|
127
|
+
sort_order: string,
|
|
128
|
+
term: string;
|
|
129
|
+
query: string;
|
|
130
|
+
features: Partial<RequestFeature>;
|
|
131
|
+
feature_variants: Partial<RequestFeatureVariant>;
|
|
132
|
+
searchandized_items: Record<string, any>;
|
|
133
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import Search from "./search";
|
|
2
|
+
import Browse from "./browse";
|
|
3
|
+
import Autocomplete from "./autocomplete";
|
|
4
|
+
import Recommendations from "./recommendations";
|
|
5
|
+
import Tracker from "./tracker";
|
|
6
|
+
import { ConstructorClientOptions } from "./types";
|
|
7
|
+
|
|
8
|
+
export = ConstructorIO;
|
|
9
|
+
|
|
10
|
+
/*********************
|
|
11
|
+
*
|
|
12
|
+
* Constructor client
|
|
13
|
+
*
|
|
14
|
+
*********************/
|
|
15
|
+
declare class ConstructorIO {
|
|
16
|
+
constructor(options: ConstructorClientOptions);
|
|
17
|
+
private options: ConstructorClientOptions;
|
|
18
|
+
search: Search;
|
|
19
|
+
browse: Browse;
|
|
20
|
+
autocomplete: Autocomplete;
|
|
21
|
+
recommendations: Recommendations;
|
|
22
|
+
tracker: Tracker;
|
|
23
|
+
|
|
24
|
+
setClientOptions(options: ConstructorClientOptions): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare namespace ConstructorIO {
|
|
28
|
+
export { Tracker }
|
|
29
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ConstructorClientOptions } from "./types";
|
|
2
|
+
|
|
3
|
+
export = EventDispatcher;
|
|
4
|
+
|
|
5
|
+
declare class EventDispatcher {
|
|
6
|
+
constructor(options: ConstructorClientOptions);
|
|
7
|
+
events: { name: string; data: Record<string, any> }[];
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
waitForBeacon: boolean;
|
|
10
|
+
active: boolean;
|
|
11
|
+
queue(name: string, data: Record<string, any>): void;
|
|
12
|
+
dispatchEvents(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { ConstructorClientOptions, ErrorData } from "./types";
|
|
2
|
+
import EventDispatcher from "./event-dispatcher";
|
|
3
|
+
|
|
4
|
+
/******************
|
|
5
|
+
*
|
|
6
|
+
* Recommendations
|
|
7
|
+
*
|
|
8
|
+
*****************/
|
|
9
|
+
export = Recommendations;
|
|
10
|
+
|
|
11
|
+
interface RecommendationsParameters {
|
|
12
|
+
itemIds?: string | string[];
|
|
13
|
+
numResults?: number;
|
|
14
|
+
section?: string;
|
|
15
|
+
term?: string;
|
|
16
|
+
filters?: Record<string, any>;
|
|
17
|
+
variationsMap?: Record<string, any>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare class Recommendations {
|
|
21
|
+
constructor(options: ConstructorClientOptions);
|
|
22
|
+
options: ConstructorClientOptions;
|
|
23
|
+
eventDispatcher: EventDispatcher;
|
|
24
|
+
|
|
25
|
+
getRecommendations(
|
|
26
|
+
podId: string,
|
|
27
|
+
parameters?: RecommendationsParameters,
|
|
28
|
+
networkParameters?: {
|
|
29
|
+
timeout?: number;
|
|
30
|
+
}
|
|
31
|
+
): Promise<Recommendations.RecommendationsResponse>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/***********
|
|
35
|
+
*
|
|
36
|
+
* Recommendations results returned from server
|
|
37
|
+
*
|
|
38
|
+
***********/
|
|
39
|
+
declare namespace Recommendations {
|
|
40
|
+
export interface RecommendationsResponse extends Record<string, any> {
|
|
41
|
+
request: Partial<Request>;
|
|
42
|
+
response: Partial<Response>;
|
|
43
|
+
result_id: string;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface Request extends Record<string, any> {
|
|
48
|
+
num_results: number;
|
|
49
|
+
item_id: string;
|
|
50
|
+
filters: {
|
|
51
|
+
group_id: string;
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
};
|
|
54
|
+
pod_id: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface Response extends Record<string, any> {
|
|
58
|
+
results: Partial<Result>;
|
|
59
|
+
total_num_results: number;
|
|
60
|
+
pod: {
|
|
61
|
+
id:string;
|
|
62
|
+
display_name: string;
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface Result extends Record<string, any> {
|
|
68
|
+
matched_terms: string[];
|
|
69
|
+
data: Record<string, any>;
|
|
70
|
+
value: string;
|
|
71
|
+
is_slotted: boolean;
|
|
72
|
+
labels: Record<string, any>;
|
|
73
|
+
strategy: {
|
|
74
|
+
id: string;
|
|
75
|
+
[key: string]: any;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConstructorClientOptions,
|
|
3
|
+
ErrorData,
|
|
4
|
+
Facet,
|
|
5
|
+
Feature,
|
|
6
|
+
FmtOption,
|
|
7
|
+
Group,
|
|
8
|
+
RequestFeature,
|
|
9
|
+
RequestFeatureVariant,
|
|
10
|
+
ResultSources,
|
|
11
|
+
SortOption,
|
|
12
|
+
} from "./types";
|
|
13
|
+
import EventDispatcher from "./event-dispatcher";
|
|
14
|
+
|
|
15
|
+
/***********
|
|
16
|
+
*
|
|
17
|
+
* SEARCH
|
|
18
|
+
*
|
|
19
|
+
***********/
|
|
20
|
+
|
|
21
|
+
export = Search;
|
|
22
|
+
|
|
23
|
+
interface SearchParameters {
|
|
24
|
+
page?: number;
|
|
25
|
+
resultsPerPage?: number;
|
|
26
|
+
filters?: Record<string, any>;
|
|
27
|
+
sortBy?: string;
|
|
28
|
+
sortOrder?: string;
|
|
29
|
+
section?: string;
|
|
30
|
+
fmtOptions?: Record<string, any>;
|
|
31
|
+
hiddenFields?: string[];
|
|
32
|
+
hiddenFacets?: string[];
|
|
33
|
+
variationsMap?: Record<string, any>;
|
|
34
|
+
}
|
|
35
|
+
declare class Search {
|
|
36
|
+
constructor(options: ConstructorClientOptions);
|
|
37
|
+
options: ConstructorClientOptions;
|
|
38
|
+
eventDispatcher: EventDispatcher;
|
|
39
|
+
getSearchResults(
|
|
40
|
+
query: string,
|
|
41
|
+
parameters?: SearchParameters,
|
|
42
|
+
networkParameters?: {
|
|
43
|
+
timeout?: number;
|
|
44
|
+
}
|
|
45
|
+
): Promise<Search.SearchResponse>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/***********
|
|
49
|
+
*
|
|
50
|
+
* search results returned from server
|
|
51
|
+
*
|
|
52
|
+
***********/
|
|
53
|
+
interface Response extends Record<string, any> {
|
|
54
|
+
result_sources: Partial<ResultSources>;
|
|
55
|
+
facets: Partial<Facet>[];
|
|
56
|
+
groups: Partial<Group>[];
|
|
57
|
+
results: Partial<Result>[];
|
|
58
|
+
sort_options: Partial<SortOption>[];
|
|
59
|
+
refined_content: Record<string, any>[];
|
|
60
|
+
total_num_results: number;
|
|
61
|
+
features: Partial<Feature>[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface Request extends Record<string, any> {
|
|
65
|
+
page: number;
|
|
66
|
+
num_results_per_page: number;
|
|
67
|
+
section: string;
|
|
68
|
+
blacklist_rules: boolean;
|
|
69
|
+
term: string;
|
|
70
|
+
fmt_options: Partial<FmtOption>;
|
|
71
|
+
sort_by: string;
|
|
72
|
+
sort_order: string;
|
|
73
|
+
features: Partial<RequestFeature>;
|
|
74
|
+
feature_variants: Partial<RequestFeatureVariant>;
|
|
75
|
+
searchandized_items: Record<string, any>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface Result extends Record<string, any> {
|
|
79
|
+
matched_terms: string[];
|
|
80
|
+
data: {
|
|
81
|
+
id: string;
|
|
82
|
+
[key: string]: any;
|
|
83
|
+
};
|
|
84
|
+
value: string;
|
|
85
|
+
is_slotted: false;
|
|
86
|
+
labels: Record<string, any>;
|
|
87
|
+
variations: Record<string, any>[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
declare namespace Search {
|
|
91
|
+
export interface SearchResponse {
|
|
92
|
+
request: Partial<Request>;
|
|
93
|
+
response: Partial<Response | Redirect>;
|
|
94
|
+
result_id: string;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface Redirect extends Record<string, any> {
|
|
99
|
+
redirect: {
|
|
100
|
+
data: {
|
|
101
|
+
match_id: number;
|
|
102
|
+
rule_id: number;
|
|
103
|
+
url: string;
|
|
104
|
+
[key: string]: any;
|
|
105
|
+
};
|
|
106
|
+
matched_terms: string[];
|
|
107
|
+
matched_user_segments: string[];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { ConstructorClientOptions } from "./types";
|
|
2
|
+
import EventEmitter = require("events");
|
|
3
|
+
import RequestQueue = require("../utils/request-queue");
|
|
4
|
+
|
|
5
|
+
export = Tracker;
|
|
6
|
+
|
|
7
|
+
/******************
|
|
8
|
+
*
|
|
9
|
+
* Tracker
|
|
10
|
+
*
|
|
11
|
+
*****************/
|
|
12
|
+
declare class Tracker {
|
|
13
|
+
constructor(options: ConstructorClientOptions);
|
|
14
|
+
private options: ConstructorClientOptions;
|
|
15
|
+
private eventemitter: EventEmitter;
|
|
16
|
+
private requests: RequestQueue;
|
|
17
|
+
|
|
18
|
+
trackSessionStart(networkParameters?: {
|
|
19
|
+
timeout?: number;
|
|
20
|
+
}): (true | Error);
|
|
21
|
+
|
|
22
|
+
trackInputFocus(networkParameters?: {
|
|
23
|
+
timeout?: number;
|
|
24
|
+
}): (true | Error);
|
|
25
|
+
|
|
26
|
+
trackItemDetailLoad(parameters: {
|
|
27
|
+
item_name: string;
|
|
28
|
+
item_id: string;
|
|
29
|
+
variation_id?: string;
|
|
30
|
+
}, networkParameters?: {
|
|
31
|
+
timeout?: number;
|
|
32
|
+
}): (true | Error);
|
|
33
|
+
|
|
34
|
+
trackAutocompleteSelect(term: string, parameters: {
|
|
35
|
+
original_query: string;
|
|
36
|
+
section: string;
|
|
37
|
+
tr?: string;
|
|
38
|
+
group_id?: string;
|
|
39
|
+
display_name?: string;
|
|
40
|
+
}, networkParameters?: {
|
|
41
|
+
timeout?: number;
|
|
42
|
+
}): (true | Error);
|
|
43
|
+
|
|
44
|
+
trackSearchSubmit(term: string, parameters: {
|
|
45
|
+
original_query: string;
|
|
46
|
+
group_id?: string;
|
|
47
|
+
display_name?: string;
|
|
48
|
+
}, networkParameters?: {
|
|
49
|
+
timeout?: number;
|
|
50
|
+
}): (true | Error);
|
|
51
|
+
|
|
52
|
+
trackSearchResultsLoaded(term: string, parameters: {
|
|
53
|
+
num_results: number;
|
|
54
|
+
item_ids?: string[];
|
|
55
|
+
}, networkParameters?: {
|
|
56
|
+
timeout?: number;
|
|
57
|
+
}): (true | Error);
|
|
58
|
+
|
|
59
|
+
trackSearchResultClick(term: string, parameters: {
|
|
60
|
+
item_name: string;
|
|
61
|
+
item_id: string;
|
|
62
|
+
variation_id?: string;
|
|
63
|
+
result_id?: string;
|
|
64
|
+
item_is_convertible?: string;
|
|
65
|
+
}, networkParameters?: {
|
|
66
|
+
timeout?: number;
|
|
67
|
+
}): (true | Error);
|
|
68
|
+
|
|
69
|
+
trackConversion(term?: string, parameters: {
|
|
70
|
+
item_id: string;
|
|
71
|
+
revenue?: number;
|
|
72
|
+
item_name?: string;
|
|
73
|
+
variation_id?: string;
|
|
74
|
+
type?: string;
|
|
75
|
+
is_custom_type?: boolean;
|
|
76
|
+
display_name?: string;
|
|
77
|
+
result_id?: string;
|
|
78
|
+
section?: string;
|
|
79
|
+
}, networkParameters?: {
|
|
80
|
+
timeout?: number;
|
|
81
|
+
}): (true | Error);
|
|
82
|
+
|
|
83
|
+
trackPurchase(parameters: {
|
|
84
|
+
items: object[];
|
|
85
|
+
revenue: number;
|
|
86
|
+
order_id?: string;
|
|
87
|
+
section?: string;
|
|
88
|
+
}, networkParameters?: {
|
|
89
|
+
timeout?: number;
|
|
90
|
+
}): (true | Error);
|
|
91
|
+
|
|
92
|
+
trackRecommendationView(parameters: {
|
|
93
|
+
url: string;
|
|
94
|
+
pod_id: string;
|
|
95
|
+
num_results_viewed: number;
|
|
96
|
+
items?: object[];
|
|
97
|
+
result_count?: number;
|
|
98
|
+
result_page?: number;
|
|
99
|
+
result_id?: string;
|
|
100
|
+
section?: string;
|
|
101
|
+
}, networkParameters?: {
|
|
102
|
+
timeout?: number;
|
|
103
|
+
}): (true | Error);
|
|
104
|
+
|
|
105
|
+
trackRecommendationClick(parameters: {
|
|
106
|
+
pod_id: string;
|
|
107
|
+
strategy_id: string;
|
|
108
|
+
item_id: string;
|
|
109
|
+
variation_id?: string;
|
|
110
|
+
section?: string;
|
|
111
|
+
result_id?: string;
|
|
112
|
+
result_count?: number;
|
|
113
|
+
result_page?: number;
|
|
114
|
+
result_position_on_page?: number;
|
|
115
|
+
num_results_per_page?: number;
|
|
116
|
+
}, networkParameters?: {
|
|
117
|
+
timeout?: number;
|
|
118
|
+
}): (true | Error);
|
|
119
|
+
|
|
120
|
+
trackBrowseResultsLoaded(parameters: {
|
|
121
|
+
url: string;
|
|
122
|
+
filter_name: string;
|
|
123
|
+
filter_value: string;
|
|
124
|
+
section?: string;
|
|
125
|
+
result_count?: number;
|
|
126
|
+
result_page?: number;
|
|
127
|
+
result_id?: string;
|
|
128
|
+
selected_filters?: object;
|
|
129
|
+
sort_order?: string;
|
|
130
|
+
sort_by?: string;
|
|
131
|
+
items?: object[];
|
|
132
|
+
}, networkParameters?: {
|
|
133
|
+
timeout?: number;
|
|
134
|
+
}): (true | Error);
|
|
135
|
+
|
|
136
|
+
trackBrowseResultClick(parameters: {
|
|
137
|
+
filter_name: string;
|
|
138
|
+
filter_value: string;
|
|
139
|
+
item_id: string;
|
|
140
|
+
section?: string;
|
|
141
|
+
variation_id?: string;
|
|
142
|
+
result_id?: string;
|
|
143
|
+
result_count?: number;
|
|
144
|
+
result_page?: number;
|
|
145
|
+
result_position_on_page?: number;
|
|
146
|
+
num_results_per_page?: number;
|
|
147
|
+
selected_filters?: object;
|
|
148
|
+
}, networkParameters?: {
|
|
149
|
+
timeout?: number;
|
|
150
|
+
}): (true | Error);
|
|
151
|
+
|
|
152
|
+
trackGenericResultClick(parameters: {
|
|
153
|
+
item_id: string;
|
|
154
|
+
item_name?: string;
|
|
155
|
+
variation_id?: string;
|
|
156
|
+
section?: string;
|
|
157
|
+
}, networkParameters?: {
|
|
158
|
+
timeout?: number;
|
|
159
|
+
}): (true | Error);
|
|
160
|
+
|
|
161
|
+
on(messageType: string, callback: Function): (true | Error);
|
|
162
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import EventDispatcher from './event-dispatcher';
|
|
2
|
+
|
|
3
|
+
export interface ConstructorClientOptions {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
serviceUrl?: string;
|
|
7
|
+
// session id is of type string in jsdocs but of type number in code usage
|
|
8
|
+
sessionId?: string;
|
|
9
|
+
clientId?: string;
|
|
10
|
+
userId?: string;
|
|
11
|
+
segments?: string[];
|
|
12
|
+
testCells?: Record<string, string>;
|
|
13
|
+
fetch?: any;
|
|
14
|
+
trackingSendDelay?: number;
|
|
15
|
+
sendTrackingEvents?: boolean;
|
|
16
|
+
sendReferrerWithTrackingEvents?: boolean;
|
|
17
|
+
eventDispatcher?: EventDispatcher;
|
|
18
|
+
beaconMode?: boolean;
|
|
19
|
+
networkParameters?: {
|
|
20
|
+
timeout: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RequestFeature extends Record<string, any> {
|
|
25
|
+
query_items: boolean;
|
|
26
|
+
auto_generated_refined_query_rules: boolean;
|
|
27
|
+
manual_searchandizing: boolean;
|
|
28
|
+
personalization: boolean;
|
|
29
|
+
filter_items: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RequestFeatureVariant extends Record<string, any> {
|
|
33
|
+
query_items: string;
|
|
34
|
+
auto_generated_refined_query_rules: string;
|
|
35
|
+
manual_searchandizing: string | null;
|
|
36
|
+
personalization: string;
|
|
37
|
+
filter_items: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type ErrorData = {
|
|
41
|
+
message: string;
|
|
42
|
+
[key: string]: any;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export interface ResultSources extends Record<string, any> {
|
|
46
|
+
token_match: { count: number; [key: string]: any };
|
|
47
|
+
embeddings_match: { count: number; [key: string]: any };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SortOption extends Record<string, any> {
|
|
51
|
+
sort_by: string;
|
|
52
|
+
display_name: string;
|
|
53
|
+
sort_order: string;
|
|
54
|
+
status: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Feature extends Record<string, any> {
|
|
58
|
+
feature_name: string;
|
|
59
|
+
display_name: string;
|
|
60
|
+
enabled: boolean;
|
|
61
|
+
variant: {
|
|
62
|
+
name: string;
|
|
63
|
+
display_name: string;
|
|
64
|
+
[key: string]: any;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface FmtOption extends Record<string, any> {
|
|
69
|
+
groups_start: string;
|
|
70
|
+
groups_max_depth: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type Facet = RangeFacet | OptionFacet;
|
|
74
|
+
|
|
75
|
+
export interface BaseFacet extends Record<string, any> {
|
|
76
|
+
data: Record<string, any>;
|
|
77
|
+
status: Record<string, any>;
|
|
78
|
+
display_name: string;
|
|
79
|
+
name: string;
|
|
80
|
+
hidden: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface RangeFacet extends BaseFacet, Record<string, any> {
|
|
84
|
+
max: number;
|
|
85
|
+
min: number;
|
|
86
|
+
type: "range";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface OptionFacet extends BaseFacet, Record<string, any> {
|
|
90
|
+
options: FacetOption[];
|
|
91
|
+
type: "multiple" | "single" | "hierarchical";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface FacetOption extends Record<string, any> {
|
|
95
|
+
count: number;
|
|
96
|
+
display_name: string;
|
|
97
|
+
value: string;
|
|
98
|
+
options?: FacetOption[];
|
|
99
|
+
range?: ["-inf" | number, "inf" | number];
|
|
100
|
+
status: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface Group extends BaseGroup, Record<string, any> {
|
|
104
|
+
count: number;
|
|
105
|
+
data: Record<string, any>;
|
|
106
|
+
parents: BaseGroup[];
|
|
107
|
+
children: Group[];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface Collection extends Record<string, any> {
|
|
111
|
+
collection_id: string,
|
|
112
|
+
display_name: string,
|
|
113
|
+
data: Record<string, any>
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface BaseGroup extends Record<string, any> {
|
|
117
|
+
display_name: string;
|
|
118
|
+
group_id: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface FmtOptions extends Record<string, any> {
|
|
122
|
+
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type Nullable<T> = T | null;
|
|
126
|
+
|
package/lib/utils/helpers.js
CHANGED
|
@@ -143,7 +143,7 @@ var utils = {
|
|
|
143
143
|
var controller = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
144
144
|
var optionsTimeout = options && options.networkParameters && options.networkParameters.timeout;
|
|
145
145
|
var networkParametersTimeout = networkParameters && networkParameters.timeout;
|
|
146
|
-
var timeout =
|
|
146
|
+
var timeout = networkParametersTimeout || optionsTimeout;
|
|
147
147
|
|
|
148
148
|
if (typeof timeout === 'number' && controller) {
|
|
149
149
|
setTimeout(function () {
|
package/package.json
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructor-io/constructorio-client-javascript",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.33.0",
|
|
4
4
|
"description": "Constructor.io JavaScript client",
|
|
5
5
|
"main": "lib/constructorio.js",
|
|
6
|
+
"types": "lib/types/constructorio.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"clean": "sudo rm -rf node_modules package-lock.json",
|
|
8
9
|
"version": "chmod +x ./scripts/verify-node-version.sh && ./scripts/verify-node-version.sh && npm run docs && git add ./docs/* && npm run bundle && git add -A ./dist",
|
|
9
10
|
"check-lisc": "license-checker --production --onlyAllow 'Apache-2.0;BSD-3-Clause;MIT'",
|
|
10
11
|
"lint": "eslint 'src/**/*.js' 'spec/**/*.js'",
|
|
11
12
|
"test": "npm run compile && mkdir -p test && cp -rf lib/* test && mocha ./spec/*",
|
|
13
|
+
"test:types": "tsd .",
|
|
14
|
+
"test:parallel": "npm run compile && mkdir -p test && cp -rf lib/* test && mocha --parallel ./spec/*",
|
|
12
15
|
"test:src": "mkdir -p test && cp -rf src/* test && mocha ./spec/*",
|
|
16
|
+
"test:src:parallel": "mkdir -p test && cp -rf src/* test && mocha --parallel ./spec/*",
|
|
13
17
|
"test:bundled": "npm run bundle && BUNDLED=true PACKAGE_VERSION=$(echo $npm_package_version) mocha ./spec/*",
|
|
18
|
+
"test:bundled:parallel": "npm run bundle && BUNDLED=true PACKAGE_VERSION=$(echo $npm_package_version) mocha --parallel ./spec/*",
|
|
14
19
|
"test:all": "npm run test && npm run test:bundled",
|
|
20
|
+
"test:all:parallel": "npm run test:parallel && npm run test:bundled:parallel",
|
|
15
21
|
"precoverage": "rm -rf ./coverage && rm -rf ./.nyc_output",
|
|
16
22
|
"coverage": "nyc --all --reporter=html npm run test:src",
|
|
17
23
|
"postcoverage": "open coverage/index.html && rm -rf test",
|
|
18
24
|
"docs": "jsdoc --configure ./.jsdoc.json ./README.md --recurse ./src --destination ./docs",
|
|
19
|
-
"compile": "babel src/ -d lib/",
|
|
25
|
+
"compile": "babel src/ -d lib/ --copy-files && rm -rf lib/types/tests",
|
|
20
26
|
"prepublish": "npm run compile",
|
|
21
27
|
"bundle": "rm -rf ./dist/* && npm run compile && node bundle.js",
|
|
22
28
|
"prepare": "husky install"
|
|
@@ -57,7 +63,8 @@
|
|
|
57
63
|
"mocha": "^9.1.3",
|
|
58
64
|
"nyc": "^15.1.0",
|
|
59
65
|
"sinon": "^7.5.0",
|
|
60
|
-
"sinon-chai": "^3.7.0"
|
|
66
|
+
"sinon-chai": "^3.7.0",
|
|
67
|
+
"tsd": "^0.24.1"
|
|
61
68
|
},
|
|
62
69
|
"dependencies": {
|
|
63
70
|
"@constructor-io/constructorio-id": "^2.4.10",
|
|
@@ -69,5 +76,8 @@
|
|
|
69
76
|
},
|
|
70
77
|
"peerDependencies": {
|
|
71
78
|
"@babel/runtime": "^7.19.0"
|
|
79
|
+
},
|
|
80
|
+
"tsd": {
|
|
81
|
+
"directory": "src/types/tests"
|
|
72
82
|
}
|
|
73
83
|
}
|