import {z} from 'zod' export type GetApiV1AvailabilityArgs = {periodStart?: string | undefined, periodEnd?: string | undefined, CombineRoomTypes?: boolean | undefined, BookingsOnly?: boolean | undefined, IncludeBookingIds?: boolean | undefined, ModifiedSince?: string | undefined}; export type GetApiV1AvailabilityOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const lodgifyApiPublicReservationsModelsV1AvailabilityAvailabilityDto = z.object({property_id: z.number().int().optional(), room_type_id: z.number().int().optional(), period_start: z.string().optional(), period_end: z.string().optional(), available: z.number().int().optional(), total_units: z.number().int().optional(), total_overbooked: z.number().int().optional(), is_available: z.boolean().optional(), booking_ids: z.array(z.number().int()).nullable().optional(), closed_period_id: z.number().int().nullable().optional()}); export const getApiV1AvailabilityResponse = z.array(lodgifyApiPublicReservationsModelsV1AvailabilityAvailabilityDto); 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 }; /** * Retrieves availability information for all properties and room types associated with the authenticated user for a given date range. * * GET /v1/availability */ export const getApiV1Availability = async (args: GetApiV1AvailabilityArgs, options?: GetApiV1AvailabilityOptions) => { const {baseUrl = "https://api.lodgify.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.periodStart != null) query.append("periodStart", String(args.periodStart)) if (args.periodEnd != null) query.append("periodEnd", String(args.periodEnd)) if (args.CombineRoomTypes != null) query.append("CombineRoomTypes", String(args.CombineRoomTypes)) if (args.BookingsOnly != null) query.append("BookingsOnly", String(args.BookingsOnly)) if (args.IncludeBookingIds != null) query.append("IncludeBookingIds", String(args.IncludeBookingIds)) if (args.ModifiedSince != null) query.append("ModifiedSince", String(args.ModifiedSince)) if (ApiKey !== undefined) headers.set("X-ApiKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/availability${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(getApiV1AvailabilityResponse, await res.json()) };