import {z} from 'zod' export type GetApiAvailabilityArgs = {rental_id?: number | undefined, owner_id?: number | undefined, campground_id?: number | undefined, from?: string | undefined, to?: string | undefined, override_protections?: boolean | undefined, include_children?: boolean | undefined, booking_id?: number | undefined, IncludeMarketplaceOnlyBlocks?: boolean | undefined}; export type GetApiAvailabilityOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const availabilityInfo = z.object({campground_id: z.number().int().optional(), from: z.string().optional(), marketplace_only: z.boolean().optional(), next_available: z.string().optional(), rental_id: z.number().int().optional(), to: z.string().optional(), turnaround_days: z.number().int().optional()}); export const getApiAvailabilityResponse = z.array(availabilityInfo); 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 }; /** * Returns list of available rentals for a given date range. * * GET /availability * * Also accepts auth: API-Key */ export const getApiAvailability = async (args: GetApiAvailabilityArgs, options: GetApiAvailabilityOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.rental_id != null) query.append("rental_id", String(args.rental_id)) if (args.owner_id != null) query.append("owner_id", String(args.owner_id)) if (args.campground_id != null) query.append("campground_id", String(args.campground_id)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.override_protections != null) query.append("override_protections", String(args.override_protections)) if (args.include_children != null) query.append("include_children", String(args.include_children)) if (args.booking_id != null) query.append("booking_id", String(args.booking_id)) if (args.IncludeMarketplaceOnlyBlocks != null) query.append("IncludeMarketplaceOnlyBlocks", String(args.IncludeMarketplaceOnlyBlocks)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiAvailabilityResponse, await res.json()) };