@constructor-io/constructorio-node 3.8.2 → 3.8.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-node",
3
- "version": "3.8.2",
3
+ "version": "3.8.3",
4
4
  "description": "Constructor.io Node.js client",
5
5
  "main": "src/constructorio.js",
6
6
  "scripts": {
@@ -118,11 +118,11 @@ function createBrowseUrlFromFilter(filterName, filterValue, parameters, userPara
118
118
  return `${serviceUrl}/browse/${encodeURIComponent(filterName)}/${encodeURIComponent(filterValue)}?${queryString}`;
119
119
  }
120
120
 
121
- // Create URL from supplied ID's
121
+ // Create URL from supplied IDs and parameters
122
122
  function createBrowseUrlFromIDs(itemIds, parameters, userParameters, options) {
123
123
  const { serviceUrl } = options;
124
124
 
125
- // Validate item ID's are provided
125
+ // Validate item IDs are provided
126
126
  if (!itemIds || !(itemIds instanceof Array) || !itemIds.length) {
127
127
  throw new Error('itemIds is a required parameter of type array');
128
128
  }
@@ -133,7 +133,7 @@ function createBrowseUrlFromIDs(itemIds, parameters, userParameters, options) {
133
133
  return `${serviceUrl}/browse/items?${queryString}`;
134
134
  }
135
135
 
136
- // Create URL from supplied ID's
136
+ // Create URL from supplied parameters
137
137
  function createBrowseUrlForFacets(parameters, userParameters, options) {
138
138
  const { serviceUrl } = options;
139
139
  const queryParams = { ...createQueryParams(parameters, userParameters, options) };
@@ -141,9 +141,28 @@ function createBrowseUrlForFacets(parameters, userParameters, options) {
141
141
  delete queryParams._dt;
142
142
 
143
143
  const queryString = qs.stringify(queryParams, { indices: false });
144
+
144
145
  return `${serviceUrl}/browse/facets?${queryString}`;
145
146
  }
146
147
 
148
+ // Create URL from supplied facet name and parameters
149
+ function createBrowseUrlForFacetOptions(facetName, parameters, userParameters, options) {
150
+ const { serviceUrl } = options;
151
+
152
+ // Validate facet name is provided
153
+ if (!facetName || typeof facetName !== 'string') {
154
+ throw new Error('facetName is a required parameter of type string');
155
+ }
156
+
157
+ const queryParams = { ...createQueryParams(parameters, userParameters, options) };
158
+
159
+ delete queryParams._dt;
160
+
161
+ const queryString = qs.stringify(queryParams, { indices: false });
162
+
163
+ return `${serviceUrl}/browse/facet_options?facet_name=${facetName}&${queryString}`;
164
+ }
165
+
147
166
  // Create request headers using supplied options and user parameters
148
167
  function createHeaders(options, userParameters) {
149
168
  const headers = {};
@@ -257,10 +276,10 @@ class Browse {
257
276
  }
258
277
 
259
278
  /**
260
- * Retrieve browse results from API using item ID's
279
+ * Retrieve browse results from API using item IDs
261
280
  *
262
281
  * @function getBrowseResultsForItemIds
263
- * @param {string[]} itemIds - Item ID's of results to fetch
282
+ * @param {string[]} itemIds - Item IDs of results to fetch
264
283
  * @param {object} [parameters] - Additional parameters to refine result set
265
284
  * @param {number} [parameters.page] - The page number of the results
266
285
  * @param {number} [parameters.resultsPerPage] - The number of results per page to return
@@ -434,6 +453,59 @@ class Browse {
434
453
  return helpers.throwHttpErrorFromResponse(new Error(), response);
435
454
  });
436
455
  }
456
+
457
+ /**
458
+ * Retrieve facet options from API
459
+ *
460
+ * @function getBrowseFacetOptions
461
+ * @param {string} facetName - Name of the facet whose options to return
462
+ * @param {object} [parameters] - Additional parameters to refine result set
463
+ * @param {object} [parameters.fmtOptions] - The format options used to refine result groups
464
+ * @param {boolean} [parameters.fmtOptions.show_hidden_facets] - Include facets configured as hidden
465
+ * @param {boolean} [parameters.fmtOptions.show_protected_facets] - Include facets configured as protected
466
+ * @param {object} [userParameters] - Parameters relevant to the user request
467
+ * @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
468
+ * @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
469
+ * @param {string} [userParameters.userId] - User ID, utilized to personalize results
470
+ * @param {string} [userParameters.segments] - User segments
471
+ * @param {string} [userParameters.testCells] - User test cells
472
+ * @param {string} [userParameters.userIp] - Origin user IP, from client
473
+ * @param {string} [userParameters.userAgent] - Origin user agent, from client
474
+ * @param {object} [networkParameters] - Parameters relevant to the network request
475
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
476
+ * @returns {Promise}
477
+ * @see https://docs.constructor.io/rest_api/browse/facet_options
478
+ * @example
479
+ * constructorio.browse.getBrowseFacetOptions('price', {
480
+ * fmtOptions: { ... },
481
+ * });
482
+ */
483
+ getBrowseFacetOptions(facetName, parameters = {}, userParameters = {}, networkParameters = {}) {
484
+ let requestUrl;
485
+ const fetch = (this.options && this.options.fetch) || nodeFetch;
486
+ const controller = new AbortController();
487
+ const { signal } = controller;
488
+
489
+ try {
490
+ requestUrl = createBrowseUrlForFacetOptions(facetName, parameters, userParameters, this.options);
491
+ } catch (e) {
492
+ return Promise.reject(e);
493
+ }
494
+
495
+ // Handle network timeout if specified
496
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
497
+
498
+ return fetch(requestUrl, {
499
+ headers: helpers.createAuthHeader(this.options),
500
+ signal,
501
+ }).then((response) => {
502
+ if (response.ok) {
503
+ return response.json();
504
+ }
505
+
506
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
507
+ });
508
+ }
437
509
  }
438
510
 
439
511
  module.exports = Browse;
@@ -141,7 +141,7 @@ class Catalog {
141
141
  * @param {string} [parameters.url] - A URL to directly send the user after selecting the item
142
142
  * @param {string} [parameters.image_url] - A URL that points to an image you'd like displayed next to some item (only applicable when URL is supplied)
143
143
  * @param {string} [parameters.description] - A description for some item (only applicable when URL is supplied)
144
- * @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own ID's of the items to more easily access them in other API calls
144
+ * @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own IDs of the items to more easily access them in other API calls
145
145
  * @param {object} [parameters.facets] - Key/value pairs that can be associated with an item and used to filter them during a search. You can associate multiple values with the same key, by making values a list. Facets can be used as filters in search, autosuggest, and browse requests
146
146
  * @param {object} [parameters.metadata] - You can associate schema-less data with items by passing in an object of keys and values. To configure search and display of this data reach out to support@constructor.io
147
147
  * @param {string[]} [parameters.group_ids] - You can associate each item with one or more groups (i.e. categories). To set up a group hierarchy please contact support@constructor.io. group_ids can be used as filters in search, autosuggest, and browse requests
@@ -195,7 +195,7 @@ class Catalog {
195
195
  * @param {string} [parameters.url] - A URL to directly send the user after selecting the item
196
196
  * @param {string} [parameters.image_url] - A URL that points to an image you'd like displayed next to some item (only applicable when URL is supplied)
197
197
  * @param {string} [parameters.description] - A description for some item (only applicable when URL is supplied)
198
- * @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own ID's of the items to more easily access them in other API calls
198
+ * @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own IDs of the items to more easily access them in other API calls
199
199
  * @param {object} [parameters.facets] - Key/value pairs that can be associated with an item and used to filter them during a search. You can associate multiple values with the same key, by making values a list. Facets can be used as filters in search, autosuggest, and browse requests
200
200
  * @param {object} [parameters.metadata] - You can associate schema-less data with items by passing in an object of keys and values. To configure search and display of this data reach out to support@constructor.io
201
201
  * @param {string[]} [parameters.group_ids] - You can associate each item with one or more groups (i.e. categories). To set up a group hierarchy please contact support@constructor.io. group_ids can be used as filters in search, autosuggest, and browse requests
@@ -295,7 +295,7 @@ class Catalog {
295
295
  * @param {string} [parameters.url] - A URL to directly send the user after selecting the item
296
296
  * @param {string} [parameters.image_url] - A URL that points to an image you'd like displayed next to some item (only applicable when URL is supplied)
297
297
  * @param {string} [parameters.description] - A description for some item (only applicable when URL is supplied)
298
- * @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own ID's of the items to more easily access them in other API calls
298
+ * @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own IDs of the items to more easily access them in other API calls
299
299
  * @param {object} [parameters.facets] - Key/value pairs that can be associated with an item and used to filter them during a search. You can associate multiple values with the same key, by making values a list. Facets can be used as filters in search, autosuggest, and browse requests
300
300
  * @param {object} [parameters.metadata] - You can associate schema-less data with items by passing in an object of keys and values. To configure search and display of this data reach out to support@constructor.io
301
301
  * @param {string[]} [parameters.group_ids] - You can associate each item with one or more groups (i.e. categories). To set up a group hierarchy please contact support@constructor.io. group_ids can be used as filters in search, autosuggest, and browse requests