import {z} from 'zod' export type GetApiV3LabelsContextModelArgs = {context_model: string, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, sort?: string | undefined, 'fields[label]'?: string | undefined, 'filter[id]'?: string | undefined, 'filter[name]'?: string | undefined, 'filter[background]'?: string | undefined, 'filter[foreground]'?: string | undefined, 'Accept-Language'?: string | undefined}; export type GetApiV3LabelsContextModelOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const label = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({name: z.string().optional(), background: z.string().optional(), foreground: z.string().optional()}).optional(), meta: z.object({temp_id: z.string().optional()}).optional()}); export const getApiV3LabelsContextModelResponse = z.object({data: z.array(label).optional(), meta: z.object({pagination: z.object({total_records: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional()}).optional()}).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 }; /** * GET labels * * GET /v3/labels/{context_model} * * Also accepts auth: OAuth2 */ export const getApiV3LabelsContextModel = async (args: GetApiV3LabelsContextModelArgs, options?: GetApiV3LabelsContextModelOptions) => { const {baseUrl = "https://api.bookingexperts.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args['fields[label]'] != null) query.append("fields[label]", String(args['fields[label]'])) if (args['filter[id]'] != null) query.append("filter[id]", String(args['filter[id]'])) if (args['filter[name]'] != null) query.append("filter[name]", String(args['filter[name]'])) if (args['filter[background]'] != null) query.append("filter[background]", String(args['filter[background]'])) if (args['filter[foreground]'] != null) query.append("filter[foreground]", String(args['filter[foreground]'])) if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/labels/${encodeURIComponent(String(args.context_model))}${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(getApiV3LabelsContextModelResponse, await res.json()) };