import {z} from 'zod' export type GetApiSearchBookingsArgs = {archived?: boolean | undefined, filter?: string | undefined, offset?: number | undefined, limit?: number | undefined, search?: string | undefined, order_by?: string | undefined, created_gt?: string | undefined, created_lt?: string | undefined, from_gt?: string | undefined, from_lt?: string | undefined, location_ids?: string | undefined, statuses?: string | undefined, new?: boolean | undefined}; export type GetApiSearchBookingsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const searchBooking = z.object({created: z.string().optional(), dealer_minimum_days: z.number().int().optional(), from: z.string().optional(), id: z.number().int().optional(), instant_book: z.boolean().optional(), is_request_less_than_minimum_days: z.boolean().optional(), num_reservations: z.number().int().optional(), owner_approved: z.boolean().optional(), owner_read: z.boolean().optional(), rental_category: z.string().optional(), rental_id: z.number().int().optional(), rental_name: z.string().optional(), renter_approved: z.boolean().optional(), renter_first_name: z.string().optional(), renter_id: z.number().int().optional(), renter_last_name: z.string().optional(), status: z.string().optional(), to: z.string().optional(), total: z.number().int().optional(), undelivered_emails_count: z.number().int().optional(), updated: z.string().optional()}); export const bookingsStatusCount = z.object({approved: z.number().int().optional(), change_requested: z.number().int().optional(), handed_off: z.number().int().optional(), imminent: z.number().int().optional(), owner_cancelled: z.number().int().optional(), renter_cancelled: z.number().int().optional()}); export const searchBookingsMetaCount = z.object({status_count: bookingsStatusCount.optional(), total: z.number().int().optional()}); export const searchBookingsMeta = z.object({abandoned: searchBookingsMetaCount.optional(), cancelled: searchBookingsMetaCount.optional(), expired: searchBookingsMetaCount.optional(), pending: searchBookingsMetaCount.optional(), returned: searchBookingsMetaCount.optional(), upcoming: searchBookingsMetaCount.optional()}); export const searchBookingsResponse = z.object({bookings: z.array(searchBooking).optional(), meta: searchBookingsMeta.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 }; /** * Search Bookings * * GET /search/bookings * * Also accepts auth: API-Key */ export const getApiSearchBookings = async (args: GetApiSearchBookingsArgs, options: GetApiSearchBookingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.archived != null) query.append("archived", String(args.archived)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.search != null) query.append("search", String(args.search)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.created_gt != null) query.append("created_gt", String(args.created_gt)) if (args.created_lt != null) query.append("created_lt", String(args.created_lt)) if (args.from_gt != null) query.append("from_gt", String(args.from_gt)) if (args.from_lt != null) query.append("from_lt", String(args.from_lt)) if (args.location_ids != null) query.append("location_ids", String(args.location_ids)) if (args.statuses != null) query.append("statuses", String(args.statuses)) if (args.new != null) query.append("new", String(args.new)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/search/bookings${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(searchBookingsResponse, await res.json()) };