import {z} from 'zod' export type GetApiV3SuggestedSearchTermsArgs = {pageToken?: string | undefined, pageSize?: unknown | undefined, catalogId: string, productCode?: string | undefined, searchTermTypes?: (Array<'SEARCH_TERM_TYPE_ORGANIC' | 'SEARCH_TERM_TYPE_CROSS_SELL' | 'SEARCH_TERM_TYPE_SUBSTITUTE'>) | undefined}; export type GetApiV3SuggestedSearchTermsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignv3SearchTermType = z.enum(["SEARCH_TERM_TYPE_ORGANIC", "SEARCH_TERM_TYPE_CROSS_SELL", "SEARCH_TERM_TYPE_SUBSTITUTE"]); export const campaignv3SuggestedSearchTerm = z.object({id: z.string().optional(), catalogId: z.string().optional(), productCode: z.string().optional(), phrase: z.string().optional(), rank: z.string().optional(), collectionName: z.string().optional(), searchTermType: campaignv3SearchTermType.optional()}); export const campaignv3ListSuggestedSearchTermsResponse = z.object({suggestedSearchTerms: z.array(campaignv3SuggestedSearchTerm).optional(), nextPageToken: z.string().optional(), totalCount: z.string().optional()}); 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 }; /** * Retrieve suggested search terms * * GET /v3/suggested-search-terms */ export const getApiV3SuggestedSearchTerms = async (args: GetApiV3SuggestedSearchTermsArgs, options?: GetApiV3SuggestedSearchTermsOptions) => { const {baseUrl = "https://auth..citrusad.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.catalogId != null) query.append("catalogId", String(args.catalogId)) if (args.productCode != null) query.append("productCode", String(args.productCode)) for (const item of args.searchTermTypes ?? []) query.append("searchTermTypes", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/suggested-search-terms${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(campaignv3ListSuggestedSearchTermsResponse, await res.json()) };