All files / core Search.js

71.88% Statements 23/32
35.71% Branches 5/14
60% Functions 3/5
70.97% Lines 22/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58    2x         2x 2x     2x 2x     2x 2x 2x 2x 2x 2x   2x         2x 2x     12x 2x 2x 2x   2x         2x                 2x       2x      
import fetch from 'fetch-jsonp';
 
require('es6-promise').polyfill();
 
 
class SearchLibrary {
  constructor() {
    this.apiKey = '';
    this.siteKey = '';
  }
  fetchData(params) {
    const callback = `UB_${Math.floor(Math.random() * 10000000000)}`;
    const defaultParams = {
      q: '*', start: 0, rows: 20, version: 'V2', format: 'json', 'json.wrf': callback,
    };
    let facetfilter = [];
    let variants = {};
    const paramsProvided = { ...params };
    Eif (typeof paramsProvided.filter !== 'undefined') {
      facetfilter = paramsProvided.filter;
      delete paramsProvided.filter;
    }
    Iif (typeof paramsProvided.variants !== 'undefined') {
      ({ variants } = paramsProvided);
      delete paramsProvided.variants;
    }
 
    const searchParams = Object.assign({}, defaultParams, paramsProvided);
    Iif (searchParams.q === '') {
      searchParams.q = '*';
    }
    let urlParams = Object.entries(searchParams).map(([key, val]) => `${key}=${val}`).join('&');
    urlParams += '&facet.multiselect=true';
    Eif (facetfilter.length > 0) {
      urlParams += `&filter=${facetfilter.join('&filter=')}`;
    }
    Iif (Object.keys(variants).length > 0) {
      urlParams += '&variants=true';
      urlParams += typeof variants.count !== 'undefined' ? `&variants.count=${variants.count}` : '';
      urlParams += typeof variants.groupby !== 'undefined' ? `&variants.groupby=${variants.groupby}` : '';
    }
    const results = fetch(`https://search.unbxd.io/${this.apiKey}/${this.siteKey}/search?${urlParams}`, {
      timeout: 5000,
      jsonpCallbackFunction: callback,
    })
      .then(response => response.json())
      .catch((err) => {
        console.error(err);
        return null;
      });
    return results;
  }
}
 
const Search = new SearchLibrary();
 
export default Search;