79 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-08-31 20:51:14 -07:00
import { getApiRoot, ApiVersion } from 'wherehows-web/utils/api/shared';
2018-09-11 11:11:36 -07:00
import buildUrl from 'wherehows-web/utils/build-url';
2019-08-31 20:51:14 -07:00
import { getJSON } from '@datahub/utils/api/fetcher';
import { toRestli, fromRestli } from 'restliparams';
2018-09-17 23:03:32 -07:00
import {
IFacetsSelectionsMap,
IFacetsSelectionsArray,
IFacetSelections
2019-08-31 20:51:14 -07:00
} from '@datahub/data-models/types/entity/facets';
import { ISearchEntityApiParams, IEntitySearchResult } from 'wherehows-web/typings/api/search/entity';
2018-09-14 16:40:45 -07:00
/**
* Converts IFacetsSelectionsMap into IFacetsSelectionsArray
* @param selections
*/
export const toFacetSelectionsArray = (selections: IFacetsSelectionsMap): IFacetsSelectionsArray =>
2019-08-31 20:51:14 -07:00
Object.keys(selections).reduce((newSelections: IFacetsSelectionsArray, key): IFacetsSelectionsArray => {
const selectionsArray = Object.keys(selections[key]).reduce((arr, selKey): Array<string> => {
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 =>
2019-08-31 20:51:14 -07:00
Object.keys(selections).reduce((newSelections: IFacetsSelectionsMap, key): IFacetsSelectionsMap => {
newSelections[key] = selections[key].reduce((obj: IFacetSelections, selKey: string): IFacetSelections => {
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
*/
2019-08-31 20:51:14 -07:00
export const facetToParamUrl = (selections: IFacetsSelectionsMap): string => {
return toRestli(toFacetSelectionsArray(selections));
};
2018-09-14 16:40:45 -07:00
/**
* Transform (source:List(hive, hdfs)) into IFacetsSelectionsMap
* @param selections
*/
2019-08-31 20:51:14 -07:00
export const facetFromParamUrl = (value: string = ''): IFacetsSelectionsMap => {
return toFacetSelectionsMap(fromRestli(value));
};
2018-09-14 16:40:45 -07:00
/**
* Build search url
*/
2019-08-31 20:51:14 -07:00
export const searchUrl = (
{ facets, ...params }: ISearchEntityApiParams,
version: ApiVersion = ApiVersion.v2
): string => {
return buildUrl(`${getApiRoot(version)}/search`, {
...params,
...facets
});
};
2018-09-14 16:40:45 -07:00
/**
2019-08-31 20:51:14 -07:00
* Queries the search endpoint using the search params
* @param params
* @returns
2018-09-14 16:40:45 -07:00
*/
2019-08-31 20:51:14 -07:00
export const readSearchV2 = <T>(params: ISearchEntityApiParams): Promise<IEntitySearchResult<T>> =>
getJSON<IEntitySearchResult<T>>({ url: searchUrl(params, ApiVersion.v2) });