import {z} from 'zod' export type GetApiV2SuburbPerformanceStatisticsStateSuburbPostcodeArgs = {state: 'NSW' | 'VIC' | 'QLD' | 'SA' | 'WA' | 'TAS' | 'NT' | 'ACT', suburb: string, postcode: string, propertyCategory?: ('House' | 'Unit') | undefined, bedrooms?: number | undefined, periodSize?: ('quarters' | 'halfYears' | 'years') | undefined, startingPeriodRelativeToCurrent?: number | undefined, totalPeriods?: number | undefined}; export type GetApiV2SuburbPerformanceStatisticsStateSuburbPostcodeOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const suburbPerformance = z.object({header: z.object({suburb: z.string().optional(), state: z.string().optional(), propertyCategory: z.string().optional()}).optional(), series: z.object({seriesInfo: z.array(z.object({year: z.number().int().optional(), month: z.number().int().optional(), values: z.object({medianSoldPrice: z.number().int().optional(), numberSold: z.number().int().optional(), highestSoldPrice: z.number().int().optional(), lowestSoldPrice: z.number().int().optional(), "5thPercentileSoldPrice": z.number().int().optional(), "25thPercentileSoldPrice": z.number().int().optional(), "75thPercentileSoldPrice": z.number().int().optional(), "95thPercentileSoldPrice": z.number().int().optional(), medianSaleListingPrice: z.number().int().optional(), numberSaleListing: z.number().int().optional(), highestSaleListingPrice: z.number().int().optional(), lowestSaleListingPrice: z.number().int().optional(), auctionNumberAuctioned: z.number().int().optional(), auctionNumberSold: z.number().int().optional(), auctionNumberWithdrawn: z.number().int().optional(), daysOnMarket: z.number().int().optional(), discountPercentage: z.number().optional(), medianRentListingPrice: z.number().int().optional(), numberRentListing: z.number().int().optional(), highestRentListingPrice: z.number().int().optional(), lowestRentListingPrice: z.number().int().optional(), meanRentYield: z.number().optional()}).optional()})).optional()}).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 }; /** * Suburb Performance by State/Suburb/Postcode * * GET /v2/suburbPerformanceStatistics/{state}/{suburb}/{postcode} * * Also accepts auth: oauth2 */ export const getApiV2SuburbPerformanceStatisticsStateSuburbPostcode = async (args: GetApiV2SuburbPerformanceStatisticsStateSuburbPostcodeArgs, options?: GetApiV2SuburbPerformanceStatisticsStateSuburbPostcodeOptions) => { const {baseUrl = "https://api.domain.com.au", fetch: fetchFn = fetch, apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.propertyCategory != null) query.append("propertyCategory", String(args.propertyCategory)) if (args.bedrooms != null) query.append("bedrooms", String(args.bedrooms)) if (args.periodSize != null) query.append("periodSize", String(args.periodSize)) if (args.startingPeriodRelativeToCurrent != null) query.append("startingPeriodRelativeToCurrent", String(args.startingPeriodRelativeToCurrent)) if (args.totalPeriods != null) query.append("totalPeriods", String(args.totalPeriods)) if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/suburbPerformanceStatistics/${encodeURIComponent(String(args.state))}/${encodeURIComponent(String(args.suburb))}/${encodeURIComponent(String(args.postcode))}${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(suburbPerformance, await res.json()) };