import {z} from 'zod' export type GetApiRmRatePlansExtRatePlansRatePlanIdArgs = {ratePlanId: string}; export type GetApiRmRatePlansExtRatePlansRatePlanIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRmRatePlansExtRatePlansRatePlanIdResponse = z.object({ratePlan: 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()}); 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 a Rate Plan by ID * * GET /rm-rate-plans-ext/rate-plans/{ratePlanId} */ export const getApiRmRatePlansExtRatePlansRatePlanId = async (args: GetApiRmRatePlansExtRatePlansRatePlanIdArgs, options?: GetApiRmRatePlansExtRatePlansRatePlanIdOptions) => { 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 (bearerAuth !== undefined) headers.set("authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rm-rate-plans-ext/rate-plans/${encodeURIComponent(String(args.ratePlanId))}${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(getApiRmRatePlansExtRatePlansRatePlanIdResponse, await res.json()) };