import {z} from 'zod' export type GetApiV3AgeGroupsArgs = {'page[number]'?: number | undefined, 'page[size]'?: number | undefined, sort?: string | undefined, 'fields[age_group]'?: string | undefined, 'filter[id]'?: string | undefined, 'filter[is_active]'?: string | undefined, 'filter[guest_type]'?: string | undefined, 'filter[min_age]'?: string | undefined, 'filter[max_age]'?: string | undefined, 'Accept-Language'?: string | undefined}; export type GetApiV3AgeGroupsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ageGroup = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({is_active: z.boolean().optional(), guest_type: z.enum(["babies", "children", "adolescents", "adults", "seniors"]).optional(), min_age: z.number().int().optional(), max_age: z.number().int().optional()}).optional(), meta: z.object({temp_id: z.string().optional()}).optional()}); export const getApiV3AgeGroupsResponse = z.object({data: z.array(ageGroup).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 age_groups * * GET /v3/age_groups * * Also accepts auth: OAuth2 */ export const getApiV3AgeGroups = async (args: GetApiV3AgeGroupsArgs, options?: GetApiV3AgeGroupsOptions) => { 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[age_group]'] != null) query.append("fields[age_group]", String(args['fields[age_group]'])) if (args['filter[id]'] != null) query.append("filter[id]", String(args['filter[id]'])) if (args['filter[is_active]'] != null) query.append("filter[is_active]", String(args['filter[is_active]'])) if (args['filter[guest_type]'] != null) query.append("filter[guest_type]", String(args['filter[guest_type]'])) if (args['filter[min_age]'] != null) query.append("filter[min_age]", String(args['filter[min_age]'])) if (args['filter[max_age]'] != null) query.append("filter[max_age]", String(args['filter[max_age]'])) 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/age_groups${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(getApiV3AgeGroupsResponse, await res.json()) };