import {z} from 'zod' export type GetApiV1AgenciesArgs = {q?: string | undefined, pageNumber?: number | undefined, pageSize?: number | undefined}; export type GetApiV1AgenciesOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const agenciesV2AgencySummary = z.object({inSuburb: z.boolean().nullable().optional(), querySuburb: z.string().nullable().optional(), hasRecentlySold: z.boolean().nullable().optional(), id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), suburb: z.string().nullable().optional(), logoUrl: z.string().nullable().optional(), baseUrl: z.string().nullable().optional(), address1: z.string().nullable().optional(), address2: z.string().nullable().optional(), telephone: z.string().nullable().optional(), rentalTelephone: z.string().nullable().optional(), mobile: z.string().nullable().optional(), fax: z.string().nullable().optional(), state: z.string().nullable().optional(), description: z.string().nullable().optional(), email: z.string().nullable().optional(), rentalEmail: z.string().nullable().optional(), homePageSearchOptions: z.string().nullable().optional(), accountType: z.number().int().nullable().optional(), numberForSale: z.number().int().nullable().optional(), numberForRent: z.number().int().nullable().optional(), domainUrl: z.string().nullable().optional(), showTabSoldLastYear: z.boolean().nullable().optional()}); export const getApiV1AgenciesResponse = z.array(agenciesV2AgencySummary); 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 }; /** * Retrieves summary of agencies matching the specified criteria. * * GET /v1/agencies * * Also accepts auth: oauth2 */ export const getApiV1Agencies = async (args: GetApiV1AgenciesArgs, options?: GetApiV1AgenciesOptions) => { 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.q != null) query.append("q", String(args.q)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agencies${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(getApiV1AgenciesResponse, await res.json()) };