import {z} from 'zod' export type ListingAdminV2Contact = {domainAgentId?: number | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: string | undefined, fax?: string | undefined, mobile?: string | undefined, email?: string | undefined, receiveEmails?: boolean | undefined}; export type ListingAdminV2SoldDetails = {soldType: 'auction' | 'privateTreaty' | 'priorToAuction', soldPrice: number, displaySoldPrice?: boolean | undefined}; export type ListingAdminV2LeasedDetails = {leasedPrice?: number | undefined, leasedDuration?: number | undefined}; export type ListingAdminV2OffMarketDetailsBase = {offMarketAction: 'withDrawn' | 'sold' | 'leased', actionDate: string, soldDetails?: ListingAdminV2SoldDetails | undefined, leasedDetails?: ListingAdminV2LeasedDetails | undefined, comment?: string | undefined}; export type ListingAdminV2ParkingDetails = {parkingType: 'onSite' | 'onStreet' | 'noParking' | 'garage' | 'carport' | 'outdoor', numberOfSpaces: number}; export type ListingAdminV2ParkingInfo = {details?: Array | undefined, information?: string | undefined}; export type ListingAdminV2PropertyMedia = {resourceType?: ('photograph' | 'floorPlan' | 'video' | 'virtualTour' | 'webLink') | undefined, url: string}; export type ListingAdminV2GeoLocation = {latitude?: number | undefined, longitude?: number | undefined}; export type ListingAdminV2Address = {unitNumber?: string | undefined, street: string, displayOption?: ('unspecified' | 'fullAddress' | 'streetAndSuburb' | 'suburbOnly' | 'regionOnly' | 'areaOnly' | 'stateOnly') | undefined, suggestedGeoLocation?: ListingAdminV2GeoLocation | undefined, streetNumber?: string | undefined, suburb: string, postcode: string, state: 'nsw' | 'vic' | 'act' | 'sa' | 'wa' | 'tas' | 'qld' | 'nt'}; export type ListingAdminV2Area = {value?: number | undefined, from?: number | undefined, to?: number | undefined, unit?: ('squareMetres' | 'acres' | 'hectares' | 'squareFeet' | 'squareYards' | 'squares') | undefined}; export type ListingAdminV2LandArea = {unit: 'squareMetres' | 'acres' | 'hectares' | 'squareFeet' | 'squareYards' | 'squares', value?: number | undefined, from?: number | undefined, to?: number | undefined}; export type ListingAdminV2ResidentialOffMarketProperty = {propertyType: Array<'acreageSemiRural' | 'apartmentUnitFlat' | 'aquaculture' | 'blockOfUnits' | 'carSpace' | 'dairyFarming' | 'developmentSite' | 'duplex' | 'farm' | 'fishingForestry' | 'newHomeDesigns' | 'house' | 'newHouseLand' | 'irrigationServices' | 'newLand' | 'livestock' | 'newApartments' | 'penthouse' | 'retirement' | 'rural' | 'semiDetached' | 'specialistFarm' | 'studio' | 'terrace' | 'townhouse' | 'vacantLand' | 'villa' | 'cropping' | 'viticulture' | 'mixedFarming' | 'grazing' | 'horticulture' | 'equine' | 'farmlet' | 'orchard' | 'ruralLifestyle'>, bedRooms?: number | undefined, bathRooms?: number | undefined, parkingInfo?: ListingAdminV2ParkingInfo | undefined, images?: Array | undefined, address: ListingAdminV2Address, area?: ListingAdminV2Area | undefined, landArea?: ListingAdminV2LandArea | undefined}; export type ListingAdminV2ResidentialOffMarketListing = {listingProvider?: string | undefined, domainAgencyID: number, providerAdId: string, description?: string | undefined, summary?: string | undefined, contacts?: Array | undefined, listingAction: 'sale' | 'rent', offMarketDetails: ListingAdminV2OffMarketDetailsBase, propertyDetails: ListingAdminV2ResidentialOffMarketProperty}; export type CreateApiV2ListingsResidentialOffmarketArgs = {body: ListingAdminV2ResidentialOffMarketListing}; export type CreateApiV2ListingsResidentialOffmarketOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const listingAdminV2ListingResponse = z.object({id: z.string().optional(), agencyId: z.number().int().optional(), providerId: z.string().optional(), providerAdId: z.string().optional(), versionId: z.string().optional(), processStatus: z.enum(["queued", "processing", "processed", "searchable", "failed", "error"]).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 }; /** * Creates an externally sold or leased residential listing; or takes an existing residential listing off the market. * * POST /v2/listings/residential/offmarket * * Also accepts auth: oauth2 */ export const createApiV2ListingsResidentialOffmarket = async (args: CreateApiV2ListingsResidentialOffmarketArgs, options?: CreateApiV2ListingsResidentialOffmarketOptions) => { const {baseUrl = "https://api.domain.com.au", fetch: fetchFn = fetch, apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/listings/residential/offmarket${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(listingAdminV2ListingResponse, await res.json()) };