import {z} from 'zod' export type StartDate = string; export type EndDate = string; export type ApiId = string; export type TransactionTrendCompareType = 'avg_chain_in_state' | 'avg_chain_nationwide' | 'avg_category_in_state' | 'avg_category_nationwide' | null; export type SalesTrendGranularity = 'day' | 'week' | 'month'; export type TransactionTrendRequest = {startDate: StartDate, endDate: EndDate, apiId: ApiId, compareType?: TransactionTrendCompareType | undefined, granularity?: SalesTrendGranularity | undefined}; export type CreateApiV1ReportsRetailSalesTransactionTrendsArgs = {body: TransactionTrendRequest}; export type CreateApiV1ReportsRetailSalesTransactionTrendsOptions = {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 salesTrendGranularity = z.enum(["day", "week", "month"]); export const transactionTrendResponse = z.object({requestId: requestId, data: z.object({startDate: startDate, endDate: endDate, apiId: apiId, compareType: z.enum(["avg_chain_in_state", "avg_chain_nationwide", "avg_category_in_state", "avg_category_nationwide"]).nullable().optional(), granularity: salesTrendGranularity.optional(), trend: z.object({bins: z.array(z.string()).optional(), values: z.array(z.number()).optional()}).optional(), compare: z.object({bins: z.array(z.string()).optional(), values: z.array(z.number()).optional()}).nullable().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 Transactions Trends Report * * POST /v1/reports/retail-sales/transaction-trends */ export const createApiV1ReportsRetailSalesTransactionTrends = async (args: CreateApiV1ReportsRetailSalesTransactionTrendsArgs, options?: CreateApiV1ReportsRetailSalesTransactionTrendsOptions) => { 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/transaction-trends${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(transactionTrendResponse, await res.json()) };