import {z} from 'zod' export type ListingIdParam = string; export type CurrencyISO = 'afn' | 'all' | 'dzd' | 'usd' | 'eur' | 'aoa' | 'xcd' | 'ars' | 'amd' | 'awg' | 'aud' | 'azn' | 'bsd' | 'bhd' | 'bdt' | 'bbd' | 'byn' | 'bzd' | 'xof' | 'bmd' | 'btn' | 'inr' | 'bob' | 'bov' | 'bam' | 'bwp' | 'nok' | 'brl' | 'bnd' | 'bgn' | 'bif' | 'cve' | 'khr' | 'xaf' | 'cad' | 'kyd' | 'clf' | 'clp' | 'cny' | 'cop' | 'cou' | 'kmf' | 'cdf' | 'nzd' | 'crc' | 'hrk' | 'cuc' | 'cup' | 'ang' | 'czk' | 'dkk' | 'djf' | 'dop' | 'egp' | 'svc' | 'ern' | 'etb' | 'fkp' | 'fjd' | 'xpf' | 'gmd' | 'gel' | 'ghs' | 'gip' | 'gtq' | 'gbp' | 'gnf' | 'gyd' | 'htg' | 'hnl' | 'hkd' | 'huf' | 'isk' | 'idr' | 'xdr' | 'irr' | 'iqd' | 'ils' | 'jmd' | 'jpy' | 'jod' | 'kzt' | 'kes' | 'kpw' | 'krw' | 'kwd' | 'kgs' | 'lak' | 'lbp' | 'lsl' | 'zar' | 'lrd' | 'lyd' | 'chf' | 'mop' | 'mga' | 'mwk' | 'myr' | 'mvr' | 'mru' | 'mur' | 'xua' | 'mxn' | 'mxv' | 'mdl' | 'mnt' | 'mad' | 'mzn' | 'mmk' | 'nad' | 'npr' | 'nio' | 'ngn' | 'omr' | 'pkr' | 'pab' | 'pgk' | 'pyg' | 'pen' | 'php' | 'pln' | 'qar' | 'mkd' | 'ron' | 'rub' | 'rwf' | 'shp' | 'wst' | 'stn' | 'sar' | 'rsd' | 'scr' | 'sle' | 'sgd' | 'xsu' | 'sbd' | 'sos' | 'ssp' | 'lkr' | 'sdg' | 'srd' | 'szl' | 'sek' | 'che' | 'chw' | 'syp' | 'twd' | 'tjs' | 'tzs' | 'thb' | 'top' | 'ttd' | 'tnd' | 'try' | 'tmt' | 'ugx' | 'uah' | 'aed' | 'usn' | 'uyi' | 'uyu' | 'uzs' | 'vuv' | 'vef' | 'ved' | 'vnd' | 'yer' | 'zmw' | 'zwl'; export type CreateApiListingBulkFetchArgs = {body: {listing_ids: Array, currency?: CurrencyISO | undefined}}; export type CreateApiListingBulkFetchOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const priceTier = z.enum(["budget", "economy", "midscale", "upscale", "luxury"]); export const propertyType = z.enum(["apartment", "barn", "bed_and_breakfast", "boat", "building", "bungalow", "bus", "cabin", "camper_rv", "campsite", "caravan", "casa_particular", "castle", "cave", "chalet", "condominium", "corporate_apartment", "cottage", "country_house", "cycladic_house", "dammuso", "dome_house", "earth_house", "estate", "farm_stay", "farmhouse", "guest_house", "guest_suite", "holiday_park", "house", "house_boat", "hut", "igloo", "island", "kezhan", "lighthouse", "loft", "mas", "mobile_home", "nature_lodge", "other", "place", "ranch", "riad", "serviced_apartment", "shepherds_hut", "shipping_container", "studio", "tent", "tiny_house", "tipi", "tower", "townhouse", "train", "treehouse", "vacation_home", "villa", "windmill", "yacht", "yurt"]).nullable(); export const listingType = z.enum(["entire_place", "private_room", "shared_room"]); export const realEstateType = z.enum(["apartment", "bed_and_breakfast", "house", "unique"]); export const cancellationPolicy = z.enum(["flexible", "moderate", "strict", "super_strict", "no refund"]).nullable(); export const hostSize = z.enum(["1", "2-5", "6-20", "21+"]); export const listingDetails = z.object({title: z.string(), market_name: z.string(), accommodates: z.number().int(), bedrooms: z.number().int(), bathrooms: z.number(), reviews: z.number().int(), rating: z.number().lte(5).nullable(), images: z.array(z.string()), price_tier: priceTier, property_type: propertyType, listing_type: listingType, real_estate_type: realEstateType, instant_book: z.boolean(), superhost: z.boolean(), cancellation_policy: cancellationPolicy, host_size: hostSize, minimum_stay: z.number().int().gte(1), created_date: z.string(), license: z.string().nullable(), cleaning_fee: z.number().int().nullable()}); export const geomPoint = z.object({lat: z.number(), lng: z.number()}); export const listingLocationArea = z.object({country_name: z.string(), city_name: z.string(), state_name: z.string().nullable(), zipcode: z.string().nullable(), msa_name: z.string().nullable()}); export const listingMetrics = z.object({occupancy: z.number(), adr: z.number(), revenue: z.number().int(), revenue_potential: z.number().int(), days_available: z.number().int(), days_blocked: z.number().int(), days_reserved: z.number().int(), num_reservations: z.number().int(), response_rate: z.number().int().lte(100).nullable(), response_time: z.number().nullable(), last_calendar_update: z.string(), last_scraped_date: z.string()}); export const listingRatings = z.object({overall_rating: z.number().lte(5).nullable(), airbnb_accuracy_rating: z.number().lte(5).nullable(), airbnb_cleanliness_rating: z.number().lte(5).nullable(), airbnb_checkin_rating: z.number().lte(5).nullable(), airbnb_communication_rating: z.number().lte(5).nullable(), airbnb_location_rating: z.number().lte(5).nullable(), airbnb_value_rating: z.number().lte(5).nullable()}); export const platforms = z.object({airbnb_host_id: z.string().nullable(), airbnb_property_id: z.string().nullable(), airbnb_property_url: z.string(), vrbo_property_id: z.string().nullable(), vrbo_property_url: z.string(), booking_property_id: z.string().nullable(), booking_property_url: z.string().nullable()}); export const amenities = z.object({has_aircon: z.boolean(), has_breakfast: z.boolean(), has_cable_tv: z.boolean(), has_doorman: z.boolean(), has_dryer: z.boolean(), has_elevator: z.boolean(), has_family_friendly: z.boolean(), has_guidebook: z.boolean(), has_gym: z.boolean(), has_handicap_access: z.boolean(), has_heating: z.boolean(), has_hottub: z.boolean(), has_indoor_fireplace: z.boolean(), has_intercom: z.boolean(), has_internet: z.boolean(), has_kitchen: z.boolean(), has_parking: z.boolean(), has_pets_allowed: z.boolean(), has_pool: z.boolean(), has_smoking: z.boolean(), has_suitable_for_events: z.boolean(), has_tv: z.boolean(), has_washer: z.boolean(), has_wireless_internet: z.boolean()}); export const countryISO = z.enum(["ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bl", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mf", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "xk", "ye", "yt", "za", "zm", "zw"]); export const marketType = z.enum(["urban_metro", "mountains_lakes", "coastal", "suburban", "rural", "mid_size_city"]); export const listingPropertyManager = z.object({property_manager_name: z.string(), property_count: z.number().int(), review_count: z.number().int()}); export const currencyISO = z.enum(["afn", "all", "dzd", "usd", "eur", "aoa", "xcd", "ars", "amd", "awg", "aud", "azn", "bsd", "bhd", "bdt", "bbd", "byn", "bzd", "xof", "bmd", "btn", "inr", "bob", "bov", "bam", "bwp", "nok", "brl", "bnd", "bgn", "bif", "cve", "khr", "xaf", "cad", "kyd", "clf", "clp", "cny", "cop", "cou", "kmf", "cdf", "nzd", "crc", "hrk", "cuc", "cup", "ang", "czk", "dkk", "djf", "dop", "egp", "svc", "ern", "etb", "fkp", "fjd", "xpf", "gmd", "gel", "ghs", "gip", "gtq", "gbp", "gnf", "gyd", "htg", "hnl", "hkd", "huf", "isk", "idr", "xdr", "irr", "iqd", "ils", "jmd", "jpy", "jod", "kzt", "kes", "kpw", "krw", "kwd", "kgs", "lak", "lbp", "lsl", "zar", "lrd", "lyd", "chf", "mop", "mga", "mwk", "myr", "mvr", "mru", "mur", "xua", "mxn", "mxv", "mdl", "mnt", "mad", "mzn", "mmk", "nad", "npr", "nio", "ngn", "omr", "pkr", "pab", "pgk", "pyg", "pen", "php", "pln", "qar", "mkd", "ron", "rub", "rwf", "shp", "wst", "stn", "sar", "rsd", "scr", "sle", "sgd", "xsu", "sbd", "sos", "ssp", "lkr", "sdg", "srd", "szl", "sek", "che", "chw", "syp", "twd", "tjs", "tzs", "thb", "top", "ttd", "tnd", "try", "tmt", "ugx", "uah", "aed", "usn", "uyi", "uyu", "uzs", "vuv", "vef", "ved", "vnd", "yer", "zmw", "zwl"]); export const listing = z.object({property_id: z.string(), details: listingDetails, location: geomPoint, exact_location: z.boolean().nullable(), location_area: listingLocationArea, metrics: listingMetrics, ratings: listingRatings, platforms: platforms, amenities: amenities, country: z.object({country_id: z.number().optional(), country_name: z.string().optional(), code: countryISO.optional()}).optional(), market: z.object({id: z.string().optional(), name: z.string().optional(), market_type: marketType.optional(), market_score: z.number().optional(), country_code: countryISO.optional()}).optional(), professionally_managed: z.boolean(), property_manager: listingPropertyManager.nullable(), currency: currencyISO}); export const status = z.object({type: z.enum(["success", "error"]), response_id: z.string(), message: z.string()}); export const createApiListingBulkFetchResponse = z.object({payload: z.object({listings: z.array(listing), failures: z.array(z.string())}), status: status}); 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 }; /** * Fetch details about multiple Listings. * * POST /listing/bulk/fetch */ export const createApiListingBulkFetch = async (args: CreateApiListingBulkFetchArgs, options?: CreateApiListingBulkFetchOptions) => { const {baseUrl = "https://api.airdna.co/api/enterprise/v2", fetch: fetchFn = fetch, headerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (headerAuth !== undefined) headers.set('authorization', `Bearer ${headerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/listing/bulk/fetch${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(createApiListingBulkFetchResponse, await res.json()) };