import {z} from 'zod' export type GetApiRmRatePlansExtRatePlansListingListingIdArgs = {listingId: string, skip: number, limit: number, sort: string, channelId: 'bookingCom' | 'manual_reservations' | 'booking_engine'}; export type GetApiRmRatePlansExtRatePlansListingListingIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRmRatePlansExtRatePlansListingListingIdResponse = z.object({listingId: z.string(), ratePlans: z.array(z.object({_id: z.string(), accountId: z.string(), type: z.literal("independent"), name: z.string(), description: z.string().optional(), cancellationPolicy: z.enum(["strict", "strict_60", "strict_90", "super_strict", "moderate", "semi_moderate", "semi_flexible", "firm", "flex", "free"]).nullable().optional(), cancellationFee: z.string().nullable().optional(), mealPlans: z.array(z.number()).optional(), availabilityRules: z.object({advanceNotice: z.number().optional(), bookingWindow: z.number().optional(), dateRange: z.object({to: z.string(), from: z.string(), repeatedDays: z.object({"0": z.object({available: z.boolean()}), "1": z.object({available: z.boolean()}), "2": z.object({available: z.boolean()}), "3": z.object({available: z.boolean()}), "4": z.object({available: z.boolean()}), "5": z.object({available: z.boolean()}), "6": z.object({available: z.boolean()})})}).optional(), restrictedDates: z.array(z.string()).optional()}).optional(), pricingModel: z.literal("nightly_rate"), channelSync: z.array(z.enum(["bookingCom", "manual_reservations", "booking_engine"])).optional(), active: z.boolean(), createdAt: z.string(), updatedAt: z.string()})), requestId: z.string(), count: z.number(), limit: z.number(), skip: z.number()}); 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 Rate Plans by Property * * GET /rm-rate-plans-ext/rate-plans/listing/{listingId} */ export const getApiRmRatePlansExtRatePlansListingListingId = async (args: GetApiRmRatePlansExtRatePlansListingListingIdArgs, options?: GetApiRmRatePlansExtRatePlansListingListingIdOptions) => { const {baseUrl = "https://open-api.guesty.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.skip != null) query.append("skip", String(args.skip)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.channelId != null) query.append("channelId", String(args.channelId)) if (bearerAuth !== undefined) headers.set("authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rm-rate-plans-ext/rate-plans/listing/${encodeURIComponent(String(args.listingId))}${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(getApiRmRatePlansExtRatePlansListingListingIdResponse, await res.json()) };