import {z} from 'zod' export type PreMarketV1PrePortalListingStatus = 'preMarket' | 'sold' | 'listed' | 'withdrawn' | 'draft'; export type PreMarketV1State = 'nsw' | 'vic' | 'act' | 'sa' | 'wa' | 'tas' | 'qld' | 'nt'; export type PreMarketV1Address = {streetNumber: string, street: string, suburb: string, state: PreMarketV1State, postcode: string}; export type PreMarketV1Contact = {firstName: string, lastName: string, email: string, phone?: (string | null) | undefined, mobile?: (string | null) | undefined, fax?: (string | null) | undefined}; export type PreMarketV1Price = {from: number, to: number, displayText?: (string | null) | undefined}; export type PreMarketV1PropertyType = 'house' | 'townhouse' | 'apartmentUnitFlat' | 'vacantLand'; export type PreMarketV1AuthorityType = 'privateSale' | 'auction' | 'expressionOfInterest'; export type PreMarketV1StatementOfInformation = {documentationUrl: string}; export type PreMarketV1ResourceType = 'photograph' | 'floorPlan'; export type PreMarketV1PropertyMedia = {resourceType: PreMarketV1ResourceType, url: string}; export type PreMarketV1SoldDetails = {soldPrice?: (number | null) | undefined, soldDate?: (string | null) | undefined}; export type PreMarketV1ListingRequest = {listingStatus: PreMarketV1PrePortalListingStatus, address: PreMarketV1Address, domainAgencyId: number, listingProvider: string, providerAdId: string, contacts?: (Array | null) | undefined, bedrooms: number, bathrooms: number, carspaces: number, summary?: (string | null) | undefined, description?: (string | null) | undefined, authorityExecutedDate?: (string | null) | undefined, reservePrice?: (number | null) | undefined, estimatedSalePrice: PreMarketV1Price, propertyTypes: Array, authorityType?: PreMarketV1AuthorityType | undefined, exclusivePeriodDays?: (number | null) | undefined, exclusiveContinuingPeriodDays?: (number | null) | undefined, exclusivePeriodStartDate?: (string | null) | undefined, statementOfInformation?: PreMarketV1StatementOfInformation | undefined, images?: (Array | null) | undefined, metadata?: ({[key: string]: string} | null) | undefined, comment?: (string | null) | undefined, soldDetails?: PreMarketV1SoldDetails | undefined}; export type CreateApiV2PremarketListingsArgs = {body: PreMarketV1ListingRequest}; export type CreateApiV2PremarketListingsOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const preMarketV1CreateOrUpdateListingResponse = z.object({id: z.string().min(1)}); 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 or updates a pre-portal listing. * * POST /v2/premarket/listings * * Also accepts auth: oauth2 */ export const createApiV2PremarketListings = async (args: CreateApiV2PremarketListingsArgs, options?: CreateApiV2PremarketListingsOptions) => { 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/premarket/listings${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(preMarketV1CreateOrUpdateListingResponse, await res.json()) };