import {z} from 'zod' export type GetApiApiV4LocationsLocationIdReviewAnalyticsOverviewArgs = {locationId: number | unknown, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiApiV4LocationsLocationIdReviewAnalyticsOverviewOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const interactionStat = z.object({name: z.string().optional(), value: z.number().optional(), delta: z.number().optional(), abs: z.number().optional()}); export const interactionsAnalyticsStats = z.object({stats: z.array(interactionStat).optional()}); export const apiError = z.object({message: z.string(), code: z.string().optional(), contextInfo: z.array(z.object({key: z.string().optional(), value: z.string().optional()})).optional()}); export const getApiApiV4LocationsLocationIdReviewAnalyticsOverviewResponse = z.object({data: z.object({interactionsAnalyticsStats: interactionsAnalyticsStats.optional()}), errors: z.array(apiError).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 }; /** * Analytics KPIs for a single location over a date range. * * GET /api/v4/locations/{locationId}/review-analytics-overview * * Also accepts auth: rapidApiKey + rapidApiHost */ export const getApiApiV4LocationsLocationIdReviewAnalyticsOverview = async (args: GetApiApiV4LocationsLocationIdReviewAnalyticsOverviewArgs, options?: GetApiApiV4LocationsLocationIdReviewAnalyticsOverviewOptions) => { const {baseUrl = "https://listingsapi.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/locations/${encodeURIComponent(String(args.locationId))}/review-analytics-overview${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(getApiApiV4LocationsLocationIdReviewAnalyticsOverviewResponse, await res.json()) };