All files / request/search search.request.ts

100% Statements 7/7
95.83% Branches 23/24
100% Functions 1/1
100% Lines 7/7

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                  32x                                                 21x     21x 21x 2x 2x     21x                      
import { convertFacetsObjectIntoString, customIdentity, customPickBy } from '../../utils';
import { GlobalRequestOptions } from '../main/main.request.type';
import { buildSearchRequestSchema } from './search.schema';
import { SearchRequestParams, SearchRequestResult, SelectedFacets } from './search.request.type';
 
 
export async function buildSearchRequest(this: GlobalRequestOptions, params: SearchRequestParams): Promise<SearchRequestResult> {
 
 
    const validatedParams = await buildSearchRequestSchema
        .validate(
            customPickBy({
                ...params,
                indexName: params.indexName || this?.indexName || process.env.GLOBAL_DEFAULT_INDEX_NAME,
                platformName: params.platformName || this?.platformName,
                searchHandler: params.searchHandler || this?.searchHandler || process.env.GLOBAL_DEFAULT_SEARCH_HANDLER,
                APIKey: params.APIKey || this?.APIKey,
                userCountry: params.userCountry || this?.userCountry,
                userId: params.userId || this?.userId,
                userIp: params.userIp || this?.userIp,
                sessionId: params.sessionId || this?.sessionId,
                sessionIdGenerator: params.sessionIdGenerator || this?.sessionIdGenerator,
            }, customIdentity)
        );
 
 
    const {
        indexName,
        platformName,
        searchHandler,
        APIKey,
        query,
        facets,
        ...restOfOptions
    } = validatedParams;
 
 
    let facetsString = '';
    if (params.facets) {
        const facetsStringResult = convertFacetsObjectIntoString(params.facets);
        facetsString = facetsStringResult ? `?${facetsStringResult}` : '';
    }
 
    return {
        method: 'GET',
        url: `${process.env.API_BASE_URL}/projects/${platformName}/indices/${indexName}/search/${searchHandler}${facetsString}`,
        params: {
            apikey: APIKey,
            q: query,
            ...restOfOptions
        },
        headers: {},
    }
}