import {z} from 'zod' export type VariantOptions = {CountFacetHitOnChild?: boolean | undefined, PageNo?: number | undefined, MaxPerPage?: number | undefined, SortCode?: string | undefined, SortBy?: string | undefined}; export type BoostQueries = {Query?: string | undefined, Boost?: number | undefined}; export type HierarchyLevelConfig = {StartLevel?: number | undefined, EndLevel?: number | undefined}; export type SmartBar = {BoostAndBury?: boolean | undefined, VisibilityRules?: boolean | undefined, PersonalizedBoost?: boolean | undefined, PopularityBoost?: boolean | undefined, ItemPin?: boolean | undefined, PopularitySalesBoost?: boolean | undefined, PopularityAdd2CartBoost?: boolean | undefined, PopularityLandingPageBoost?: boolean | undefined, KeywordReplacement?: boolean | undefined, PreviewDate?: string | undefined}; export type Coordinate = {Latitude?: number | undefined, Longitude?: number | undefined}; export type ClientData = {VisitorId?: string | undefined, VisitId?: string | undefined, Custom?: {[key: string]: string} | undefined, ExtendedCustom?: {[key: string]: Array} | undefined, PreviewBuckets?: Array | undefined, Source?: string | undefined, Origin?: Coordinate | undefined, ZipCode?: string | undefined}; export type SearchRequest = {IndexName?: string | undefined, Query?: string | undefined, Variant?: VariantOptions | undefined, BoostQueries?: Array | undefined, DistanceUnitType?: (1 | 2 | 3) | undefined, RequestType?: (0 | 1 | 2 | 3 | 4 | 5 | 6) | undefined, ImageData?: string | undefined, ImageText?: string | undefined, kValue?: number | undefined, FacetHierarchyLevelOverride?: {[key: string]: HierarchyLevelConfig} | undefined, NestedId?: string | undefined, ClientGuid?: string | undefined, Keyword?: string | undefined, PageId?: number | undefined, PageNo?: number | undefined, MaxPerPage?: number | undefined, SearchWithin?: string | undefined, SortBy?: string | undefined, SortingSetCode?: string | undefined, PaginationSetCode?: string | undefined, FacetSelections?: {[key: string]: Array>} | undefined, CustomUrl?: string | undefined, IsInPreview?: boolean | undefined, Is100CoverageTurnedOn?: boolean | undefined, ExplainDocId?: string | undefined, FacetOverride?: Array | undefined, FieldOverride?: Array | undefined, SmartBar?: SmartBar | undefined, ClientData?: ClientData | undefined, SearchType?: string | undefined, IgnoreSpellcheck?: boolean | undefined}; export type CreateApiApiV2SearchArgs = {body: SearchRequest}; export type CreateApiApiV2SearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV2SearchResponse = z.object({}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Search * * POST /api/v2/search */ export const createApiApiV2Search = async (args: CreateApiApiV2SearchArgs, options?: CreateApiApiV2SearchOptions) => { const {baseUrl = "https://indexing-dev.hawksearch.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiV2SearchResponse, await res.json()) };