UNPKG

cluedin-widget

Version:

This project contains all the pages needed for browsing entities and searching them. The aim is to replace the CluedIn.Webapp project with this one when all the pages ( including the Admin page ) will be ported to REACT.

108 lines (80 loc) 2.26 kB
/* eslint no-underscore-dangle: ["error", { "allow": ["__token"] }]*/ import { apiRequest } from '../helpers/request'; import entityAPI from '../../iso/entity'; export function search(q, options) { const { sortDirection, filters, pageNumber, aggregation, type } = options; let url = 'api/search?q='; let filterUrl = ''; if (aggregation) { url = `api/v1/entitysearch?id=${aggregation}`; if (type) { url += `&type=${type}`; } url += '&q='; } if (filters && filters.length > 0) { filters.forEach((f) => { if (filterUrl) { filterUrl += ' AND '; } filterUrl += `${f.filterType}:${(f.facet.term || f.facet.time)}`; }); filterUrl = filterUrl.replace('entitytype', 'entityType'); } url += q; if (filterUrl) { url += `&type=${filterUrl}`; if (type) { url += ` ${encodeURIComponent(type)}`; } } else { if (type) { url += `&type=${encodeURIComponent(type)}`; } } if (pageNumber) { url += `&page=${pageNumber}`; } if (sortDirection && sortDirection !== 'relevance') { let direction = ''; if (sortDirection === 'new') { direction = 'desc'; } if (sortDirection === 'old') { direction = 'asc'; } url += `&dateSort=true&direction=${direction}`; } return apiRequest('GET', url).then((resp) => { const result = resp.body; if (result.Hits) { result.Hits = result.Hits.map((entity) => ( entityAPI.toVM(entity, false, resp.__token) )); } return result; }); } export function suggestedSearch(q) { let url = 'api/search/suggest?q='; url += q; return apiRequest('GET', url).then((resp) => (resp.body)); } export function getFacetsSearch(q, page = 0, take = 10) { let url = `api/v2/joinsearch?q=${q}`; url += `&page=${page}`; url += `&take=${take}`; return apiRequest('GET', url).then((resp) => (resp.body)); } export function aggregationSearch(id) { let url = `api/v1/entitysearch?id=${id}`; return apiRequest('GET', url).then((resp) => { const result = resp.body; if (result.Hits) { result.Hits = result.Hits.map((entity) => ( entityAPI.toVM(entity, false, resp.__token) )); } return result; }); }