import {z} from 'zod' export type GetApiPropertiesPropertyIdArgs = {'Accept-Version'?: string | undefined, propertyId: number}; export type GetApiPropertiesPropertyIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const translation = z.object({city_name: z.string(), address_line: z.string().max(255), language_code: z.string(), property_name: z.string()}); export const position = z.object({latitude: z.number().gte(-90).lte(90), longitude: z.number().gte(-180).lte(180)}); export const timeRange = z.object({from: z.string().optional(), until: z.string().optional()}); export const address = z.object({city_name: z.string(), country_code: z.string(), postal_code: z.string().optional(), address_line: z.string().min(1).max(255), display_address: z.boolean().optional()}); export const propertyInfoRead = z.object({translations: z.array(translation).optional(), position: position, check_in: timeRange, check_out: timeRange.optional(), property_name: z.string().min(3).max(255), property_category: z.number().int().optional(), booking_property_category: z.number().int().optional(), primary_language: z.string().optional(), languages_spoken: z.array(z.string()).optional(), room_count: z.number().int().gte(0), floor_count: z.number().int().gte(0).lte(200).optional(), provider_property_id: z.string().max(16).optional(), currency_code: z.string().max(3).optional(), stars: z.string().optional(), target: z.enum(["test", "production"]), property_id: z.number().int().optional(), status: z.string().optional(), url: z.string().optional(), physical_address: address, legal_entity_id: z.number().int()}); 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 apiResponsePropertyInfoRead = z.object({data: propertyInfoRead.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 }; /** * Read property * * GET /properties/{propertyId} */ export const getApiPropertiesPropertyId = async (args: GetApiPropertiesPropertyIdArgs, options?: GetApiPropertiesPropertyIdOptions) => { const {baseUrl = "https://supply-xml.booking.com/property-api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(apiResponsePropertyInfoRead, await res.json()) };