modules/browse.js

/* eslint-disable object-curly-newline, no-underscore-dangle, max-len, complexity */
const EventDispatcher = require('../utils/event-dispatcher');
const helpers = require('../utils/helpers');

// Create query params from parameters and options
function createQueryParams(parameters, options) {
  const {
    apiKey,
    version,
    sessionId,
    clientId,
    userId,
    segments,
    testCells,
  } = options;
  let queryParams = { c: version };

  queryParams.key = apiKey;
  queryParams.i = clientId;
  queryParams.s = sessionId;

  // Pull test cells from options
  if (testCells) {
    Object.keys(testCells).forEach((testCellKey) => {
      queryParams[`ef-${testCellKey}`] = testCells[testCellKey];
    });
  }

  // Pull user segments from options
  if (segments && segments.length) {
    queryParams.us = segments;
  }

  // Pull user id from options and ensure string
  if (userId) {
    queryParams.ui = String(userId);
  }

  if (parameters) {
    const {
      page,
      offset,
      resultsPerPage,
      filters,
      sortBy,
      sortOrder,
      section,
      fmtOptions,
      hiddenFields,
      hiddenFacets,
      variationsMap,
      qsParam,
      preFilterExpression,
      filterMatchTypes,
    } = parameters;

    // Pull page from parameters
    if (!helpers.isNil(page)) {
      queryParams.page = page;
    }

    // Pull offset from parameters
    if (!helpers.isNil(offset)) {
      queryParams.offset = offset;
    }

    // Pull results per page from parameters
    if (!helpers.isNil(resultsPerPage)) {
      queryParams.num_results_per_page = resultsPerPage;
    }

    if (filters) {
      queryParams.filters = filters;
    }

    if (filterMatchTypes) {
      queryParams.filter_match_types = filterMatchTypes;
    }

    // Pull sort by from parameters
    if (sortBy) {
      queryParams.sort_by = sortBy;
    }

    // Pull sort order from parameters
    if (sortOrder) {
      queryParams.sort_order = sortOrder;
    }

    // Pull section from parameters
    if (section) {
      queryParams.section = section;
    }

    // Pull format options from parameters
    if (fmtOptions) {
      queryParams.fmt_options = fmtOptions;
    }

    // Pull hidden fields from parameters
    if (hiddenFields) {
      if (queryParams.fmt_options) {
        queryParams.fmt_options.hidden_fields = hiddenFields;
      } else {
        queryParams.fmt_options = { hidden_fields: hiddenFields };
      }
    }

    // Pull hidden facets from parameters
    if (hiddenFacets) {
      if (queryParams.fmt_options) {
        queryParams.fmt_options.hidden_facets = hiddenFacets;
      } else {
        queryParams.fmt_options = { hidden_facets: hiddenFacets };
      }
    }

    // Pull variations map from parameters
    if (variationsMap) {
      queryParams.variations_map = JSON.stringify(variationsMap);
    }

    // Pull pre_filter_expression from parameters
    if (preFilterExpression) {
      queryParams.pre_filter_expression = JSON.stringify(preFilterExpression);
    }

    // pull qs param from parameters
    if (qsParam) {
      queryParams.qs = JSON.stringify(qsParam);
    }
  }

  queryParams._dt = Date.now();
  queryParams = helpers.cleanParams(queryParams);

  return queryParams;
}

// Create URL from supplied filter name, value and parameters
function createBrowseUrlFromFilter(filterName, filterValue, parameters, options) {
  const { serviceUrl } = options;

  // Validate filter name is provided
  if (!filterName || typeof filterName !== 'string') {
    throw new Error('filterName is a required parameter of type string');
  }

  // Validate filter value is provided
  if (!filterValue || typeof filterValue !== 'string') {
    throw new Error('filterValue is a required parameter of type string');
  }

  const queryParams = createQueryParams(parameters, options);
  const queryString = helpers.stringify(queryParams);

  return `${serviceUrl}/browse/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(filterName))}/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(filterValue))}?${queryString}`;
}

// Create URL from supplied id's
function createBrowseUrlFromIDs(ids, parameters, options) {
  const { serviceUrl } = options;

  // Validate id's are provided
  if (!ids || !(Array.isArray(ids)) || !ids.length) {
    throw new Error('ids is a required parameter of type array');
  }

  const queryParams = { ...createQueryParams(parameters, options), ids };
  const queryString = helpers.stringify(queryParams);

  return `${serviceUrl}/browse/items?${queryString}`;
}

// Create URL for facets
function createBrowseUrlForFacets(parameters, options) {
  const { serviceUrl } = options;
  const queryParams = { ...createQueryParams(parameters, options) };

  // Endpoint does not accept _dt
  delete queryParams._dt;

  const queryString = helpers.stringify(queryParams);

  return `${serviceUrl}/browse/facets?${queryString}`;
}

// Create URL from supplied facet name and parameters
function createBrowseUrlForFacetOptions(facetName, parameters, options) {
  const { serviceUrl } = options;

  // Validate facet name is provided
  if (!facetName || typeof facetName !== 'string') {
    throw new Error('facetName is a required parameter of type string');
  }

  const queryParams = { ...createQueryParams(parameters, options) };

  queryParams.facet_name = facetName;

  // Endpoint does not accept _dt
  delete queryParams._dt;

  const queryString = helpers.stringify(queryParams);

  return `${serviceUrl}/browse/facet_options?${queryString}`;
}

/**
 * Interface to browse related API calls
 *
 * @module browse
 * @inner
 * @returns {object}
 */
class Browse {
  constructor(options) {
    this.options = options || {};
    this.eventDispatcher = new EventDispatcher(options.eventDispatcher);
  }

  /**
   * Retrieve browse results from API
   *
   * @function getBrowseResults
   * @description Retrieve browse results from Constructor.io API
   * @param {string} filterName - Filter name to display results from
   * @param {string} filterValue - Filter value to display results from
   * @param {object} [parameters] - Additional parameters to refine result set
   * @param {number} [parameters.page] - The page number of the results (Can't be used together with offset)
   * @param {number} [parameters.offset] - The number of results to skip from the beginning (Can't be used together with page)
   * @param {number} [parameters.resultsPerPage] - The number of results per page to return
   * @param {object} [parameters.filters] - Key / value mapping (dictionary) of filters used to refine results
   * @param {string} [parameters.sortBy='relevance'] - The sort method for results
   * @param {string} [parameters.sortOrder='descending'] - The sort order for results
   * @param {string} [parameters.section='Products'] - The section name for results
   * @param {object} [parameters.fmtOptions] - The format options used to refine result groups. Please refer to https://docs.constructor.com/reference/browse-browse-results for details
   * @param {object} [parameters.preFilterExpression] - Faceting expression to scope browse results. Please refer to https://docs.constructor.com/reference/configuration-collections
   * @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
   * @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
   * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.com/reference/shared-variations-mapping for details
   * @param {object} [parameters.qsParam] - Parameters listed above can be serialized into a JSON object and parsed through this parameter. Please refer to https://docs.constructor.com/reference/browse-browse-results
   * @param {object} [parameters.filterMatchTypes] - An object specifying whether results must match `all`, `any` or `none` of a given filter. Please refer to https://docs.constructor.com/reference/v1-browse-get-browse-results
   * @param {object} [networkParameters] - Parameters relevant to the network request
   * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
   * @returns {Promise}
   * @see https://docs.constructor.com/reference/shared-results-response-structure
   * @example
   * constructorio.browse.getBrowseResults('group_id', 't-shirts', {
   *     resultsPerPage: 40,
   *     filters: {
   *         size: 'medium'
   *     },
   *     filterMatchTypes: {
   *        size: 'all'
   *     }
   * });
   */
  getBrowseResults(filterName, filterValue, parameters, networkParameters = {}) {
    let requestUrl;
    const { fetch } = this.options;
    let signal;

    if (typeof AbortController === 'function') {
      const controller = new AbortController();

      signal = controller && controller.signal;

      // Handle network timeout if specified
      helpers.applyNetworkTimeout(this.options, networkParameters, controller);
    }

    try {
      requestUrl = createBrowseUrlFromFilter(filterName, filterValue, parameters, this.options);
    } catch (e) {
      return Promise.reject(e);
    }

    return fetch(requestUrl, { signal })
      .then(helpers.convertResponseToJson)
      .then((json) => {
        if (json.response && json.response.results) {
          if (json.result_id) {
            // Append `result_id` to each result item
            json.response.results.forEach((result) => {
              // eslint-disable-next-line no-param-reassign
              result.result_id = json.result_id;
            });
          }

          this.eventDispatcher.queue('browse.getBrowseResults.completed', json);

          return json;
        }

        throw new Error('getBrowseResults response data is malformed');
      });
  }

  /**
   * Retrieve browse results from API using item id's
   *
   * @function getBrowseResultsForItemIds
   * @param {string[]} itemIds - Item id's of results to fetch
   * @param {object} [parameters] - Additional parameters to refine result set
   * @param {number} [parameters.page] - The page number of the results (Can't be used together with offset)
   * @param {number} [parameters.offset] - The number of results to skip from the beginning (Can't be used together with page)
   * @param {number} [parameters.resultsPerPage] - The number of results per page to return
   * @param {object} [parameters.filters] - Filters used to refine results
   * @param {string} [parameters.sortBy='relevance'] - The sort method for results
   * @param {string} [parameters.sortOrder='descending'] - The sort order for results
   * @param {string} [parameters.section='Products'] - The section name for results
   * @param {object} [parameters.fmtOptions] - The format options used to refine result groups. Please refer to https://docs.constructor.com/reference/browse-browse-results for details
   * @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
   * @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
   * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.com/reference/shared-variations-mapping for details
   * @param {object} [parameters.filterMatchTypes] - An object specifying whether results must match `all`, `any` or `none` of a given filter. Please refer to https://docs.constructor.com/reference/v1-browse-get-browse-items-results
   * @param {object} [parameters.preFilterExpression] - Faceting expression to scope browse results. Please refer to https://docs.constructor.com/reference/configuration-collections
   * @param {object} [networkParameters] - Parameters relevant to the network request
   * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
   * @returns {Promise}
   * @see https://docs.constructor.com/reference/v1-browse-get-browse-items-results
   * @example
   * constructorio.browse.getBrowseResultsForItemIds(['shirt-123', 'shirt-456', 'shirt-789'], {
   *     filters: {
   *         size: 'medium'
   *     },
   *     filterMatchTypes: {
   *        size: 'all'
   *     }
   * });
   */
  getBrowseResultsForItemIds(itemIds, parameters, networkParameters = {}) {
    let requestUrl;
    const { fetch } = this.options;
    let signal;

    if (typeof AbortController === 'function') {
      const controller = new AbortController();

      signal = controller && controller.signal;

      // Handle network timeout if specified
      helpers.applyNetworkTimeout(this.options, networkParameters, controller);
    }

    try {
      requestUrl = createBrowseUrlFromIDs(itemIds, parameters, this.options);
    } catch (e) {
      return Promise.reject(e);
    }

    return fetch(requestUrl, { signal })
      .then(helpers.convertResponseToJson)
      .then((json) => {
        if (json.response && json.response.results) {
          if (json.result_id) {
            // Append `result_id` to each result item
            json.response.results.forEach((result) => {
              // eslint-disable-next-line no-param-reassign
              result.result_id = json.result_id;
            });
          }

          this.eventDispatcher.queue('browse.getBrowseResultsForItemIds.completed', json);

          return json;
        }

        throw new Error('getBrowseResultsForItemIds response data is malformed');
      });
  }

  /**
   * Retrieve browse groups from API
   *
   * @function getBrowseGroups
   * @param {object} [parameters.filters] - Filters used to refine results
   * @param {string} [parameters.section='Products'] - The section name for results
   * @param {object} [parameters.fmtOptions] - The format options used to refine result groups. Please refer to https://docs.constructor.com/reference/v1-browse-get-browse-groups/ for details
   * @param {object} [networkParameters] - Parameters relevant to the network request
   * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
   * @returns {Promise}
   * @see https://docs.constructor.com/reference/v1-browse-get-browse-groups
   * @example
   * constructorio.browse.getBrowseGroups({
   *     filters: {
   *         group_id: 'drill_collection'
   *     },
   *     fmtOptions: {
   *         groups_max_depth: 2
   *     }
   * });
   */
  getBrowseGroups(parameters, networkParameters = {}) {
    const { fetch } = this.options;
    const { serviceUrl } = this.options;
    const queryParams = createQueryParams(parameters, this.options);
    let signal;

    if (typeof AbortController === 'function') {
      const controller = new AbortController();

      signal = controller && controller.signal;

      // Handle network timeout if specified
      helpers.applyNetworkTimeout(this.options, networkParameters, controller);
    }

    delete queryParams._dt;

    const queryString = helpers.stringify(queryParams);
    const requestUrl = `${serviceUrl}/browse/groups?${queryString}`;

    return fetch(requestUrl, { signal })
      .then(helpers.convertResponseToJson)
      .then((json) => {
        if (json.response && json.response.groups) {
          this.eventDispatcher.queue('browse.getBrowseGroups.completed', json);

          return json;
        }

        throw new Error('getBrowseGroups response data is malformed');
      });
  }

  /**
   * Retrieve browse facets from API
   *
   * @function getBrowseFacets
   * @param {object} [parameters] - Additional parameters to refine result set
   * @param {number} [parameters.page] - The page number of the results (Can't be used together with offset)
   * @param {number} [parameters.offset] - The number of results to skip from the beginning (Can't be used together with page)
   * @param {string} [parameters.section='Products'] - The section name for results
   * @param {object} [parameters.fmtOptions] - The format options used to refine result groups. Please refer to https://docs.constructor.com/reference/browse-browse-facets/ for details
   * @param {number} [parameters.resultsPerPage] - The number of results per page to return
   * @param {object} [networkParameters] - Parameters relevant to the network request
   * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
   * @returns {Promise}
   * @see https://docs.constructor.com/reference/browse-browse-facets
   * @example
   * constructorio.browse.getBrowseFacets({
   *     page: 1,
   *     resultsPerPage: 10,
   * });
   */
  getBrowseFacets(parameters, networkParameters) {
    let requestUrl;
    const { fetch } = this.options;
    let signal;

    if (typeof AbortController === 'function') {
      const controller = new AbortController();

      signal = controller && controller.signal;

      // Handle network timeout if specified
      helpers.applyNetworkTimeout(this.options, networkParameters, controller);
    }

    try {
      requestUrl = createBrowseUrlForFacets(parameters, this.options);
    } catch (e) {
      return Promise.reject(e);
    }

    return fetch(requestUrl, { signal })
      .then(helpers.convertResponseToJson)
      .then((json) => {
        if (json.response && json.response.facets) {
          this.eventDispatcher.queue('browse.getBrowseFacets.completed', json);

          return json;
        }

        throw new Error('getBrowseFacets response data is malformed');
      });
  }

  /**
   * Retrieve facet options from API
   *
   * @function getBrowseFacetOptions
   * @param {string} facetName - Name of the facet whose options to return
   * @param {object} [networkParameters] - Parameters relevant to the network request
   * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
   * @param {object} [parameters] - Additional parameters to refine result set
   * @param {string} [parameters.section='Products'] - The section name for results
   * @param {object} [parameters.fmtOptions] - The format options used to refine result groups. Please refer to https://docs.constructor.com/reference/v1-browse-get-browse-facet-options for details
   * @param {}
   * @returns {Promise}
   * @see https://docs.constructor.com/reference/v1-browse-get-browse-facet-options
   * @example
   * constructorio.browse.getBrowseFacetOptions('price', {
   * });
   */
  getBrowseFacetOptions(facetName, parameters = {}, networkParameters = {}) {
    let requestUrl;
    const { fetch } = this.options;
    let signal;

    if (typeof AbortController === 'function') {
      const controller = new AbortController();

      signal = controller && controller.signal;

      // Handle network timeout if specified
      helpers.applyNetworkTimeout(this.options, networkParameters, controller);
    }

    try {
      requestUrl = createBrowseUrlForFacetOptions(facetName, parameters, this.options);
    } catch (e) {
      return Promise.reject(e);
    }

    return fetch(requestUrl, { signal })
      .then(helpers.convertResponseToJson)
      .then((json) => {
        if (json.response && json.response.facets) {
          this.eventDispatcher.queue('browse.getBrowseFacetOptions.completed', json);

          return json;
        }

        throw new Error('getBrowseFacetOptions response data is malformed');
      });
  }
}

module.exports = Browse;