import {z} from 'zod' export type Reason = {type?: ('OTHER' | 'SEASON' | 'RENOVATION' | 'PROPERTY_NOT_AVAILABLE' | 'FULLY_BOOKED' | 'ISSUES_GUEST' | 'ISSUES_BOOKING' | 'STRATEGY') | undefined, description?: string | undefined}; export type PropertyStatusInput = {operation: 'CHECK' | 'OPEN' | 'CLOSE', reason?: Reason | undefined}; export type CreateApiPropertiesPropertyIdStatusArgs = {propertyId: number, body: PropertyStatusInput}; export type CreateApiPropertiesPropertyIdStatusOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyStatusOutput = z.object({status: z.string().optional()}); export const objectNode = z.object({}); export const apiErrorOTA = z.object({message: z.string().optional(), code: z.number().int().optional(), details: objectNode.optional(), description: z.string().optional()}); export const responseMeta = z.object({ruid: z.string().optional()}); export const apiResponsePropertyStatusOutput = z.object({data: propertyStatusOutput.optional(), warnings: z.array(apiErrorOTA).optional(), errors: z.array(apiErrorOTA).optional(), meta: responseMeta.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 }; /** * Check property status * * POST /properties/{propertyId}/status */ export const createApiPropertiesPropertyIdStatus = async (args: CreateApiPropertiesPropertyIdStatusArgs, options?: CreateApiPropertiesPropertyIdStatusOptions) => { const {baseUrl = "https://supply-xml.booking.com/property-api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/properties/${encodeURIComponent(String(args.propertyId))}/status${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(apiResponsePropertyStatusOutput, await res.json()) };