import {z} from 'zod' export type StartDate = string; export type EndDate = string; export type ApiId = string; export type SalesDayOfWeekAgg = 'full_week' | 'weekday_weekend'; export type DayOfWeekMetricType = 'sales' | 'avg_sales' | 'sales_pct' | 'sales_sq_ft' | 'transactions' | 'transactions_pct' | 'avg_ticket_size'; export type SalesDaysTrendsRequest = {startDate: StartDate, endDate: EndDate, apiId: ApiId, aggregation?: SalesDayOfWeekAgg | undefined, metricType?: DayOfWeekMetricType | undefined}; export type CreateApiV1ReportsRetailSalesDaysArgs = {body: SalesDaysTrendsRequest}; export type CreateApiV1ReportsRetailSalesDaysOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const startDate = z.string(); export const endDate = z.string(); export const apiId = z.string(); export const salesDayOfWeekAgg = z.enum(["full_week", "weekday_weekend"]); export const dayOfWeekMetricType = z.enum(["sales", "avg_sales", "sales_pct", "sales_sq_ft", "transactions", "transactions_pct", "avg_ticket_size"]); export const salesDaysTrendsResponse = z.object({requestId: requestId, data: z.object({startDate: startDate, endDate: endDate, apiId: apiId, aggregation: salesDayOfWeekAgg.optional(), metricType: dayOfWeekMetricType.optional(), dayOfWeek: z.object({bins: z.array(z.string()).optional(), values: z.array(z.number()).optional()}).optional()}).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 }; /** * Retail Sales Daily Trends * * POST /v1/reports/retail-sales/days */ export const createApiV1ReportsRetailSalesDays = async (args: CreateApiV1ReportsRetailSalesDaysArgs, options?: CreateApiV1ReportsRetailSalesDaysOptions) => { 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/retail-sales/days${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(salesDaysTrendsResponse, await res.json()) };