import {z} from 'zod' export type StartDate = string; export type EndDate = string; export type ApiId = string; export type AllocationType = 'weightedCentroid' | 'centroid'; export type VmFilterObject = {audienceType?: (Array<'visitors' | 'employees' | 'residents'>) | undefined, dwellTime?: ({operator?: ('<' | '>=') | undefined, value?: number | undefined}) | undefined, hoursRange?: {fromHour: string, toHour: string} | undefined, daysOfWeek?: Array | undefined, distance?: ({from?: ('home' | 'work') | undefined, values?: ({le?: number | undefined, ge?: number | undefined, lt?: number | undefined, gt?: number | undefined, eq?: number | undefined}) | undefined}) | undefined}; export type NearbyRadius = 100 | 250 | 500 | 750 | 1000; export type TrueTradeAreaRequest = {startDate: StartDate, endDate: EndDate, apiId: ApiId, reportType: 'trueTradeArea', fileFormat?: 'polygonGeoJson' | undefined, trafficVolPct?: number | undefined, allocationType?: AllocationType | undefined, withinDistance?: number | undefined, filter?: VmFilterObject | undefined, nearbyRadius?: NearbyRadius | undefined}; export type CreateApiV1ReportsTrueTradeAreaArgs = {body: TrueTradeAreaRequest}; export type CreateApiV1ReportsTrueTradeAreaOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const polygonResponse = z.object({requestId: requestId}); 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 }; /** * Trade Area Report * * POST /v1/reports/true-trade-area */ export const createApiV1ReportsTrueTradeArea = async (args: CreateApiV1ReportsTrueTradeAreaArgs, options?: CreateApiV1ReportsTrueTradeAreaOptions) => { 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/true-trade-area${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(polygonResponse, await res.json()) };