All files / react-app/src/api useSearch.ts

77.27% Statements 17/22
75% Branches 9/12
66.66% Functions 6/9
75% Lines 15/20

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                    72x     139x                   138x     72x 5x   4x 4x       4x             4x 43x                   4x     72x       385x 73x         72x                          
import {
  aggQueryBuilder,
  filterQueryBuilder,
  paginationQueryBuilder,
  sortQueryBuilder,
} from "@/components";
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
import { API } from "aws-amplify";
import type { ReactQueryApiError, opensearch } from "shared-types";
 
type QueryProps<T> = {
  index: opensearch.Index;
  filters: opensearch.QueryState<T>["filters"];
  sort?: opensearch.QueryState<T>["sort"];
  pagination: opensearch.QueryState<T>["pagination"];
  aggs?: opensearch.AggQuery<T>[];
};
 
export const getOsData = async <TProps, TResponse extends opensearch.Response<any>>(
  props: QueryProps<TProps>,
): Promise<TResponse> => {
  const searchData = await API.post("os", `/search/${props.index}`, {
    body: {
      ...filterQueryBuilder(props.filters),
      ...paginationQueryBuilder(props.pagination),
      ...(!!props.sort && sortQueryBuilder(props.sort)),
      ...(!!props.aggs && aggQueryBuilder(props.aggs)),
      track_total_hits: true,
    },
  });
 
  return searchData;
};
 
export const getMainExportData = async (filters?: opensearch.main.Filterable[]) => {
  if (!filters) return [];
 
  const recursiveSearch = async (startPage: number): Promise<opensearch.main.Document[]> => {
    Iif (startPage * 1000 >= 10000) {
      return [];
    }
 
    const searchData = await API.post("os", "/search/main", {
      body: {
        ...filterQueryBuilder(filters),
        ...paginationQueryBuilder({ number: startPage, size: 1000 }),
      },
    });
 
    Eif (searchData?.hits.hits.length < 1000) {
      return searchData.hits.hits.map((hit: any) => ({ ...hit._source })) || [];
    }
 
    return searchData.hits.hits
      .map((hit: any) => ({
        ...hit._source,
      }))
      .concat(await recursiveSearch(startPage + 1));
  };
 
  return await recursiveSearch(0);
};
 
export const useOsSearch = <TProps, TResponse>(
  options?: UseMutationOptions<TResponse, ReactQueryApiError, QueryProps<TProps>>,
) => {
  //@ts-expect-error
  return useMutation<TResponse, ReactQueryApiError, QueryProps<TProps>>(
    (props) => getOsData(props),
    options,
  );
};
 
export const useChangelogSearch = (
  options?: UseMutationOptions<
    opensearch.changelog.Response,
    ReactQueryApiError,
    QueryProps<opensearch.changelog.Field>
  >,
) => {
  return useMutation<
    opensearch.changelog.Response,
    ReactQueryApiError,
    QueryProps<opensearch.changelog.Field>
  >((props) => getOsData(props), options);
};