import {z} from 'zod' export type GetApiApiV4LocationsLocationIdFacebookAnalyticsArgs = {locationId: number | unknown, fromDate?: string | undefined, toDate?: string | undefined}; export type GetApiApiV4LocationsLocationIdFacebookAnalyticsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const metricDelta = z.object({current: z.number().int().optional(), previous: z.number().int().optional(), diff: z.number().int().optional(), diffPercentage: z.number().optional(), label: z.string().optional()}); export const facebookMetricV4 = z.object({views: metricDelta.optional(), ctaAction: metricDelta.optional(), websiteAction: metricDelta.optional(), directionsAction: metricDelta.optional(), phoneAction: metricDelta.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 getApiApiV4LocationsLocationIdFacebookAnalyticsResponse = z.object({data: z.object({facebookInsights: facebookMetricV4.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 }; /** * Facebook v4 metrics for a single location over a date range. * * GET /api/v4/locations/{locationId}/facebook-analytics * * Also accepts auth: rapidApiKey + rapidApiHost */ export const getApiApiV4LocationsLocationIdFacebookAnalytics = async (args: GetApiApiV4LocationsLocationIdFacebookAnalyticsArgs, options?: GetApiApiV4LocationsLocationIdFacebookAnalyticsOptions) => { 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.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/locations/${encodeURIComponent(String(args.locationId))}/facebook-analytics${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(getApiApiV4LocationsLocationIdFacebookAnalyticsResponse, await res.json()) };