import {z} from 'zod' export type Translation = {city_name: string, address_line: string, language_code: string, property_name: string}; export type PositionPatch = {latitude?: number | undefined, longitude?: number | undefined}; export type TimeRangeUpdate = {from?: string | undefined, until?: string | undefined}; export type AddressPatch = {city_name?: string | undefined, country_code?: string | undefined, postal_code?: string | undefined, address_line?: string | undefined, display_address?: boolean | undefined}; export type PropertyInfoUpdate = {translations?: Array | undefined, position?: PositionPatch | undefined, check_in?: TimeRangeUpdate | undefined, check_out?: TimeRangeUpdate | undefined, property_name?: string | undefined, property_category?: number | undefined, booking_property_category?: number | undefined, primary_language?: string | undefined, languages_spoken?: Array | undefined, room_count?: number | undefined, floor_count?: number | undefined, provider_property_id?: string | undefined, currency_code?: string | undefined, stars?: (string | null) | undefined, physical_address?: AddressPatch | undefined}; export type PatchApiPropertiesPropertyIdArgs = {'Accept-Version'?: string | undefined, propertyId: number, body: PropertyInfoUpdate}; export type PatchApiPropertiesPropertyIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyId = z.object({property_id: z.number().int().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 apiResponsePropertyId = z.object({data: propertyId.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 }; /** * Partial update property info * * PATCH /properties/{propertyId} */ export const patchApiPropertiesPropertyId = async (args: PatchApiPropertiesPropertyIdArgs, options?: PatchApiPropertiesPropertyIdOptions) => { 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 (args['Accept-Version'] != null) headers.set("Accept-Version", String(args['Accept-Version'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/properties/${encodeURIComponent(String(args.propertyId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(apiResponsePropertyId, await res.json()) };