import {z} from 'zod' export type ApiId = string; export type Region = '5' | '10' | '15' | '30' | '50' | 'nationwide' | 'state' | 'dma' | 'cbsa'; export type PeriodPreset = 'last3Months' | 'last6Months' | 'last12Months' | 'lastWeek' | 'lastMonth' | 'lastQuarter' | 'yearToDate' | 'year2018' | 'year2019' | 'year2020' | 'year2021' | 'year2022' | 'year2023' | 'year2024' | 'year2025' | null; export type RankingIndexTopRankingRequest = {apiId: ApiId, region: Region, period: PeriodPreset, scope: 'chain' | 'category' | 'groupCategory', metric: 'visitsPerSqft' | 'visits', limit?: number | undefined, skip?: number | undefined}; export type CreateApiV1ReportsRankingIndexTopRankingArgs = {body: RankingIndexTopRankingRequest}; export type CreateApiV1ReportsRankingIndexTopRankingOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiId = z.string(); export const rankingIndexAddress = z.object({city: z.string().optional(), state: z.string().optional(), countryCode: z.string().optional(), streetName: z.string().optional(), formattedAddress: z.string().optional(), shortFormattedAddress: z.string().optional(), zipCode: z.string().optional(), country: z.string().optional(), address: z.string().optional()}); export const visitDurationSegmentationRespRanking = z.enum(["under10Min", "10MinOrLonger", "allVisits", "mixedSegments"]).nullable(); export const rankingIndexResponse = z.object({totalRanked: z.number().int().optional(), rank: z.number().int().optional(), region: z.string().optional(), scope: z.string().optional(), metricType: z.string().optional(), visits: z.number().optional(), rankingIndexArray: z.array(z.object({apiId: apiId.optional(), name: z.string().optional(), address: rankingIndexAddress.optional(), rank: z.number().int().optional(), visits: z.number().optional()})).optional(), visitDurationSegmentation: visitDurationSegmentationRespRanking.optional()}); export const rankingIndexError = z.object({region: z.string().optional(), scope: z.string().optional(), metricType: z.string().optional(), rankError: z.string().optional()}); export const createApiV1ReportsRankingIndexTopRankingResponse = z.union([rankingIndexResponse, rankingIndexError]); 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 }; /** * Ranking Index - Top Ranked * * POST /v1/reports/ranking-index/top-ranking */ export const createApiV1ReportsRankingIndexTopRanking = async (args: CreateApiV1ReportsRankingIndexTopRankingArgs, options?: CreateApiV1ReportsRankingIndexTopRankingOptions) => { const {baseUrl = "https://papi.placer.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reports/ranking-index/top-ranking${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1ReportsRankingIndexTopRankingResponse, await res.json()) };