import {z} from 'zod' export type GetApiAuthoringV1ContextualsearchArgs = {filter?: unknown | undefined, position?: string | undefined, distance?: number | undefined, metric?: unknown | undefined, 'accept-language'?: unknown | undefined, 'similar-source-id'?: unknown | undefined, 'similar-source-classification'?: unknown | undefined, df?: string | undefined, defType?: string | undefined, facet?: string | undefined, 'facet.contains'?: string | undefined, 'facet.containsIgnoreCase'?: string | undefined, 'facet.field'?: string | undefined, 'facet.limit'?: string | undefined, 'facet.offset'?: string | undefined, 'facet.prefix'?: string | undefined, 'facet.range'?: string | undefined, 'facet.range.gap'?: string | undefined, 'facet.range.start'?: string | undefined, 'facet.range.end'?: string | undefined, fl?: string | undefined, fq?: string | undefined, 'f..'?: string | undefined, indent?: string | undefined, q: string, qf?: string | undefined, rows?: number | undefined, start?: number | undefined, sort?: string | undefined}; export type GetApiAuthoringV1ContextualsearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiAuthoringV1ContextualsearchResponse = z.object({query: 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 contextual search results from the authoring collection. * * GET /authoring/v1/contextualsearch */ export const getApiAuthoringV1Contextualsearch = async (args: GetApiAuthoringV1ContextualsearchArgs, options: GetApiAuthoringV1ContextualsearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.position != null) query.append("position", String(args.position)) if (args.distance != null) query.append("distance", String(args.distance)) if (args.metric != null) query.append("metric", String(args.metric)) if (args['accept-language'] != null) headers.set("accept-language", String(args['accept-language'])) if (args['similar-source-id'] != null) query.append("similar-source-id", String(args['similar-source-id'])) if (args['similar-source-classification'] != null) query.append("similar-source-classification", String(args['similar-source-classification'])) if (args.df != null) query.append("df", String(args.df)) if (args.defType != null) query.append("defType", String(args.defType)) if (args.facet != null) query.append("facet", String(args.facet)) if (args['facet.contains'] != null) query.append("facet.contains", String(args['facet.contains'])) if (args['facet.containsIgnoreCase'] != null) query.append("facet.containsIgnoreCase", String(args['facet.containsIgnoreCase'])) if (args['facet.field'] != null) query.append("facet.field", String(args['facet.field'])) if (args['facet.limit'] != null) query.append("facet.limit", String(args['facet.limit'])) if (args['facet.offset'] != null) query.append("facet.offset", String(args['facet.offset'])) if (args['facet.prefix'] != null) query.append("facet.prefix", String(args['facet.prefix'])) if (args['facet.range'] != null) query.append("facet.range", String(args['facet.range'])) if (args['facet.range.gap'] != null) query.append("facet.range.gap", String(args['facet.range.gap'])) if (args['facet.range.start'] != null) query.append("facet.range.start", String(args['facet.range.start'])) if (args['facet.range.end'] != null) query.append("facet.range.end", String(args['facet.range.end'])) if (args.fl != null) query.append("fl", String(args.fl)) if (args.fq != null) query.append("fq", String(args.fq)) if (args['f..'] != null) query.append("f..", String(args['f..'])) if (args.indent != null) query.append("indent", String(args.indent)) if (args.q != null) query.append("q", String(args.q)) if (args.qf != null) query.append("qf", String(args.qf)) if (args.rows != null) query.append("rows", String(args.rows)) if (args.start != null) query.append("start", String(args.start)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/authoring/v1/contextualsearch${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(getApiAuthoringV1ContextualsearchResponse, await res.json()) };