143 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-09-11 11:11:36 -07:00
import { getApiRoot, ApiStatus } from 'wherehows-web/utils/api/shared';
import buildUrl from 'wherehows-web/utils/build-url';
import { getJSON } from 'wherehows-web/utils/api/fetcher';
import { toRestli, fromRestli } from 'restliparams';
2018-09-11 11:11:36 -07:00
2018-09-14 16:40:45 -07:00
/**
* Backend search expected parameters
*/
2018-09-11 11:11:36 -07:00
export interface ISearchApiParams {
keyword: string;
category: string;
page: number;
facets: string;
2018-09-11 11:11:36 -07:00
[key: string]: any;
}
2018-09-14 16:40:45 -07:00
/**
* Backend search expected response
*/
2018-09-11 11:11:36 -07:00
export interface ISearchResponse {
status: ApiStatus;
result: {
keywords: string;
data: Array<any>;
2018-09-14 16:40:45 -07:00
[key: string]: any;
2018-09-11 11:11:36 -07:00
};
}
2018-09-14 16:40:45 -07:00
/**
* Dynamic facet selections
*/
export interface IFacetSelections {
[key: string]: boolean;
}
2018-09-14 16:40:45 -07:00
/**
* Dynamic facets:
* {
* source: {
* hdfs: true,
* hive: true
* }
* }
*/
export interface IFacetsSelectionsMap {
[key: string]: IFacetSelections;
}
2018-09-14 16:40:45 -07:00
/**
* Compressed version of selection to put it in a url
* {
* source: ['hdfs', 'hive]
* }
*/
export interface IFacetsSelectionsArray {
[key: string]: Array<string>;
}
2018-09-14 16:40:45 -07:00
/**
* Dynamic counts facet option
*/
export interface IFacetCounts {
[key: string]: number;
}
/**
* Dynamic counts facet similar to selections
*/
export interface IFacetsCounts {
[key: string]: IFacetCounts;
}
/**
* Convert backend static structure into a dynamic facet count structure
*/
export const facetToDynamicCounts = (result: ISearchResponse['result']): IFacetsCounts => {
return Object.keys(result).reduce((counts: IFacetsCounts, key) => {
if (key.indexOf('groupby') === 0) {
counts[key.replace('groupby', '')] = result[key];
}
return counts;
}, {});
};
/**
* Converts IFacetsSelectionsMap into IFacetsSelectionsArray
* @param selections
*/
export const toFacetSelectionsArray = (selections: IFacetsSelectionsMap): IFacetsSelectionsArray =>
2018-09-14 16:42:13 -07:00
Object.keys(selections).reduce((newSelections: IFacetsSelectionsArray, key) => {
const selectionsArray = Object.keys(selections[key]).reduce((arr, selKey) => {
if (selections[key][selKey] && selKey) {
return [...arr, selKey];
}
return arr;
}, []);
if (selectionsArray.length > 0) {
newSelections[key] = selectionsArray;
}
return newSelections;
}, {});
2018-09-14 16:40:45 -07:00
/**
* Converts IFacetsSelectionsArray into IFacetsSelectionsMap
* @param selections
*/
export const toFacetSelectionsMap = (selections: IFacetsSelectionsArray): IFacetsSelectionsMap =>
2018-09-14 16:42:13 -07:00
Object.keys(selections).reduce((newSelections: IFacetsSelectionsMap, key) => {
newSelections[key] = selections[key].reduce((obj: IFacetSelections, selKey: string) => {
obj[selKey] = true;
return obj;
}, {});
return newSelections;
}, {});
2018-09-14 16:40:45 -07:00
/**
* Transform IFacetsSelectionsMap into this string: (source:List(hive, hdfs))
* @param selections
*/
export const facetToParamUrl = (selections: IFacetsSelectionsMap) => {
return toRestli(toFacetSelectionsArray(selections));
};
2018-09-14 16:40:45 -07:00
/**
* Transform (source:List(hive, hdfs)) into IFacetsSelectionsMap
* @param selections
*/
export const facetFromParamUrl = (value: string = '') => {
return toFacetSelectionsMap(fromRestli(value));
};
2018-09-14 16:40:45 -07:00
/**
* Build search url
*/
export const searchUrl = ({ facets, ...params }: ISearchApiParams): string => {
return buildUrl(`${getApiRoot()}/search`, { ...params, ...fromRestli(facets || '') });
};
2018-09-11 11:11:36 -07:00
2018-09-14 16:40:45 -07:00
/**
* Fetch Search from API
*/
2018-09-11 11:11:36 -07:00
export const readSearch = (params: ISearchApiParams) => getJSON<ISearchResponse>({ url: searchUrl(params) });