import {z} from 'zod' export type GetApiApiV11SortingsetArgs = {'X-HawkSearch-ApiKey': string}; export type GetApiApiV11SortingsetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const sortingSetFieldDataModel = z.object({Direction: z.string(), FieldId: z.number().int().optional(), IsCustom: z.boolean().optional(), NumberOfDigits: z.number().int().optional(), NullValue: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), MoveNoValueToEnd: z.boolean().optional()}); export const sortingSetFieldModel = z.object({SortingSetFieldId: z.number().int().optional(), IsDefault: z.boolean().optional(), IsPinSortIncluded: z.boolean().optional(), ParamName: z.string(), Label: z.string(), FieldsConfig: z.array(sortingSetFieldDataModel).optional()}); export const sortingSetModel = z.object({SortingSetId: z.number().int().optional(), SyncGuid: z.string().optional(), Name: z.string(), Code: z.string(), RuleXml: z.string().optional(), SortOrder: z.number().int().optional(), Fields: z.array(sortingSetFieldModel).optional(), CreateDate: z.string().optional(), ModifyDate: z.string().optional()}); export const getApiApiV11SortingsetResponse = z.array(sortingSetModel); 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 }; /** * Get sortingsets * * GET /api/v11/sortingset */ export const getApiApiV11Sortingset = async (args: GetApiApiV11SortingsetArgs, options?: GetApiApiV11SortingsetOptions) => { const {baseUrl = "https://indexing-dev.hawksearch.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-HawkSearch-ApiKey'] != null) headers.set("X-HawkSearch-ApiKey", String(args['X-HawkSearch-ApiKey'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v11/sortingset${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(getApiApiV11SortingsetResponse, await res.json()) };