import {z} from 'zod' export type Translation = {city_name: string, address_line: string, language_code: string, property_name: string}; export type Position = {latitude: number, longitude: number}; export type TimeRangeCreate = {from?: string | undefined, until?: string | undefined}; export type Address = {city_name: string, country_code: string, postal_code?: string | undefined, address_line: string, display_address?: boolean | undefined}; export type PropertyInfoCreate = {translations?: Array | undefined, position: Position, check_in: TimeRangeCreate, check_out?: TimeRangeCreate | undefined, property_name: string, property_category?: number | undefined, booking_property_category?: number | undefined, primary_language?: string | undefined, languages_spoken?: Array | undefined, room_count: number, floor_count?: number | undefined, provider_property_id?: string | undefined, currency_code?: string | undefined, stars?: string | undefined, target: 'test' | 'production', physical_address: Address, legal_entity_id: number}; export type CreateApiPropertiesArgs = {'Accept-Version'?: string | undefined, body: PropertyInfoCreate}; export type CreateApiPropertiesOptions = {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 }; /** * Create property * * POST /properties */ export const createApiProperties = async (args: CreateApiPropertiesArgs, options?: CreateApiPropertiesOptions) => { 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${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(apiResponsePropertyId, await res.json()) };