import {z} from 'zod' export type GetApiV1AgenciesIdStatisticsArgs = {id: number, timePeriod?: ('last7Days' | 'last90Days' | 'wholeCampaign') | undefined, statusFilter?: ('live' | 'liveAndArchived') | undefined, channel?: ('residential' | 'commercial' | 'business') | undefined, pageNumber?: number | undefined, pageSize?: number | undefined}; export type GetApiV1AgenciesIdStatisticsOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const listingPerformanceV1Statistics = z.object({listingId: z.number().int().nullable().optional(), totalListingViews: z.number().int().nullable().optional(), totalPhotoViews: z.number().int().nullable().optional(), totalPhotoGalleryViews: z.number().int().nullable().optional(), totalFloorplanViews: z.number().int().nullable().optional(), totalMapViews: z.number().int().nullable().optional(), totalVideoViews: z.number().int().nullable().optional(), totalEbrochureViews: z.number().int().nullable().optional(), totalAgentDetailViews: z.number().int().nullable().optional(), totalShortlisted: z.number().int().nullable().optional(), totalPrinted: z.number().int().nullable().optional(), totalAgentPhoneNumberReveals: z.number().int().nullable().optional(), totalEnquiries: z.number().int().nullable().optional(), totalEmailToFriend: z.number().int().nullable().optional(), totalSharedViaSocialMedia: z.number().int().nullable().optional(), totalInspectionTimeSaving: z.number().int().nullable().optional(), totalCallToAgentFromMobile: z.number().int().nullable().optional(), percentageWebsiteViews: z.number().nullable().optional(), percentageMobileSiteViews: z.number().nullable().optional(), percentageSmartPhoneViews: z.number().nullable().optional(), percentageTabletViews: z.number().nullable().optional()}); export const getApiV1AgenciesIdStatisticsResponse = z.array(listingPerformanceV1Statistics); 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 statistics for a specific agency. * * GET /v1/agencies/{id}/statistics * * Also accepts auth: oauth2 */ export const getApiV1AgenciesIdStatistics = async (args: GetApiV1AgenciesIdStatisticsArgs, options?: GetApiV1AgenciesIdStatisticsOptions) => { 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.timePeriod != null) query.append("timePeriod", String(args.timePeriod)) if (args.statusFilter != null) query.append("statusFilter", String(args.statusFilter)) if (args.channel != null) query.append("channel", String(args.channel)) 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/${encodeURIComponent(String(args.id))}/statistics${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(getApiV1AgenciesIdStatisticsResponse, await res.json()) };