import { isEmpty , isUndefined } from 'lodash' ; import { BasicQuery , SearchFiltersURL , SearchQuery } from '../types' ; export const getURLSearchParams = ( query ) : URLSearchParams => { const q = new URLSearchParams ( ) ; if ( ! isUndefined ( query . filters ) && ! isEmpty ( query . filters ) ) { Object . keys ( query . filters ) . forEach ( ( filterId ) => { return query . filters ! [ filterId ] . forEach ( ( id ) => { q . append ( filterId , id ) ; } ) ; } ) ; } if ( ! isUndefined ( query . tsQueryWeb ) ) { q . set ( 'ts_query_web' , query . tsQueryWeb ) ; } if ( ! isUndefined ( query . name ) ) { q . set ( 'name' , query . name ) ; } if ( query . tsQuery && query . tsQuery . length > 0 ) { q . set ( 'ts_query' , query . tsQuery . join ( ' | ' ) ) ; } if ( ! isUndefined ( query . deprecated ) && query . deprecated ) { q . set ( 'deprecated' , 'true' ) ; } if ( ! isUndefined ( query . operators ) && query . operators ) { q . set ( 'operators' , 'true' ) ; } if ( ! isUndefined ( query . verifiedPublisher ) && query . verifiedPublisher ) { q . set ( 'verified_publisher' , 'true' ) ; } if ( ! isUndefined ( query . official ) && query . official ) { q . set ( 'official' , 'true' ) ; } return q ; } ; export const prepareAPIQueryString = ( query ) : string => { const q = getURLSearchParams ( query ) ; q . set ( 'limit' , query . limit . toString ( ) ) ; q . set ( 'offset' , query . offset . toString ( ) ) ; return `?${ q . toString ( ) }` ; } ; export const prepareQueryString = ( query ) : string => { const q = getURLSearchParams ( query ) ; q . set ( 'sort' , query . sort || 'relevance' ) ; q . set ( 'page' , query . pageNumber . toString ( ) ) ; return `?${ q . toString ( ) }` ; } ;