import {z} from 'zod' export type GetApiV1PropertiesImagesArgs = {propertyId?: string | undefined, gnafId?: string | undefined, flatNumber?: string | undefined, streetNumber?: string | undefined, streetName?: string | undefined, streetType?: string | undefined, postcode?: string | undefined, suburbName?: string | undefined, state?: ('ACT' | 'NSW' | 'NT' | 'QLD' | 'SA' | 'TAS' | 'VIC' | 'WA' | 'OT') | undefined}; export type GetApiV1PropertiesImagesOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyEnrichmentV2PropertyAddressV2 = z.object({flatNumber: z.string().nullable().optional(), streetNumber: z.string(), streetName: z.string(), streetType: z.string().nullable().optional(), streetTypeLong: z.string().nullable().optional(), suburb: z.string(), postcode: z.string(), state: z.string()}); export const propertyEnrichmentV2ImageTypes = z.enum(["ListingPhoto", "OldFloorPlanPhoto", "NewFloorPlanPhoto", "BuildingPhoto", "NewDevelopmentLandPhoto", "NewDevelopmentLumierePhoto", "NewDevelopmentFeaturedPhoto", "ConjunctionAgentLogo", "NewDevelopmentRegionalPhoto", "AdvertiserImage", "YouWishClip", "AgentCarousel", "DevelopmentProjectGalleryPhoto", "DevelopmentProjectAdvertisementPhoto"]); export const propertyEnrichmentV2ImageUrl = z.object({advertId: z.number().int().optional(), advertDate: z.string().nullable().optional(), images: z.array(z.object({imageUrl: z.string().optional(), imageType: propertyEnrichmentV2ImageTypes.optional()})).optional()}); export const propertyEnrichmentV2ImageResults = z.object({propertyId: z.string().optional(), addressComponents: propertyEnrichmentV2PropertyAddressV2.optional(), adverts: z.array(propertyEnrichmentV2ImageUrl).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 }; /** * Property images * * GET /v1/properties/images * * Also accepts auth: oauth2 */ export const getApiV1PropertiesImages = async (args: GetApiV1PropertiesImagesArgs, options?: GetApiV1PropertiesImagesOptions) => { 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.propertyId != null) query.append("propertyId", String(args.propertyId)) if (args.gnafId != null) query.append("gnafId", String(args.gnafId)) if (args.flatNumber != null) query.append("flatNumber", String(args.flatNumber)) if (args.streetNumber != null) query.append("streetNumber", String(args.streetNumber)) if (args.streetName != null) query.append("streetName", String(args.streetName)) if (args.streetType != null) query.append("streetType", String(args.streetType)) if (args.postcode != null) query.append("postcode", String(args.postcode)) if (args.suburbName != null) query.append("suburbName", String(args.suburbName)) if (args.state != null) query.append("state", String(args.state)) if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/properties/images${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(propertyEnrichmentV2ImageResults, await res.json()) };