import {z} from 'zod' export type GetApiV3RentableSegmentsArgs = {'page[number]'?: number | undefined, 'page[size]'?: number | undefined, sort?: string | undefined, 'fields[rentable_segment]'?: string | undefined, 'filter[id]'?: string | undefined, 'filter[name]'?: string | undefined, 'filter[semantic_segment]'?: string | undefined, 'filter[created_at]'?: string | undefined, 'filter[updated_at]'?: string | undefined, 'Accept-Language'?: string | undefined}; export type GetApiV3RentableSegmentsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rentableSegment = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({name: z.object({cs: z.string().optional(), "da-DK": z.string().optional(), de: z.string().optional(), "de-AT": z.string().optional(), "de-CH": z.string().optional(), "en-US": z.string().optional(), "en-GB": z.string().optional(), es: z.string().optional(), fr: z.string().optional(), "fr-BE": z.string().optional(), "fr-LU": z.string().optional(), it: z.string().optional(), nb: z.string().optional(), nl: z.string().optional(), "nl-BE": z.string().optional(), pl: z.string().optional(), pt: z.string().optional(), tr: z.string().optional(), en: z.string().optional(), da: z.string().optional()}).optional(), semantic_segment: z.enum(["house", "pitch", "room", "berth", "apartment", "other", "year_site", "b_and_b", "bungalow", "cabin", "campervan_pitch", "caravan_pitch", "chalet", "cottage", "glamping_tent", "hotel_room", "houseboat", "mobile_home", "owners_managers_residence", "parking_place", "private_bathroom", "recreational_vehicle", "tent_pitch", "tiny_house", "treehouse", "villa"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), meta: z.object({temp_id: z.string().optional()}).optional()}); export const getApiV3RentableSegmentsResponse = z.object({data: z.array(rentableSegment).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 rentable_segments * * GET /v3/rentable_segments * * Also accepts auth: OAuth2 */ export const getApiV3RentableSegments = async (args: GetApiV3RentableSegmentsArgs, options?: GetApiV3RentableSegmentsOptions) => { 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[rentable_segment]'] != null) query.append("fields[rentable_segment]", String(args['fields[rentable_segment]'])) 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[semantic_segment]'] != null) query.append("filter[semantic_segment]", String(args['filter[semantic_segment]'])) if (args['filter[created_at]'] != null) query.append("filter[created_at]", String(args['filter[created_at]'])) if (args['filter[updated_at]'] != null) query.append("filter[updated_at]", String(args['filter[updated_at]'])) 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/rentable_segments${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(getApiV3RentableSegmentsResponse, await res.json()) };