import {z} from 'zod' export type GetApiBasketsDashboardOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const basket = z.object({uid: z.number().int().optional(), crdate: z.number().int().optional(), tstamp: z.number().int().optional(), crdate_formatted: z.string().optional(), tstamp_formatted: z.string().optional(), type: z.number().int().optional(), type_text: z.string().optional(), total_amount: z.number().int().optional(), client: z.number().int().optional(), discount: z.number().int().optional(), disount_type: z.string().optional(), discount_reason_id: z.number().int().optional(), customer: z.number().int().optional(), account: z.number().int().optional(), canceled: z.number().int().optional(), canceled_formatted: z.string().optional(), branch: z.number().int().optional(), cash_register: z.number().int().optional(), table: z.number().int().optional(), total_stock_amount: z.number().int().optional(), total_no_tax: z.number().int().optional(), card_data: z.object({}).optional(), status: z.object({}).optional(), canceled_note: z.object({}).optional(), deleted_note: z.string().optional(), order_hash: z.string().optional(), order_id: z.number().int().optional(), sequence_number: z.number().int().optional(), frame_text: z.string().optional(), canceled_frame_text: z.string().optional(), cash_return: z.object({}).optional(), terminal_type: z.number().int().optional(), sales: z.number().int().optional(), fiscal_data: z.object({}).optional(), copied_formatted: z.string().optional(), parent: z.number().int().optional(), total_no_discount: z.number().int().optional(), discount_margin: z.number().int().optional(), warning: z.string().optional(), extra_amount: z.number().int().optional(), tips: z.number().int().optional(), is_cancelable: z.boolean().optional(), return_reason: z.number().int().optional(), currency_exchange_rate: z.number().int().optional(), total_in_custom_currency: z.number().int().optional(), currency_symbol: z.string().optional(), currency_letters: z.string().optional(), currency_icon_number: z.number().int().optional(), mobile_payment: z.object({provider: z.number().int().optional(), transaction_id: z.number().int().optional()}).optional(), external_id: z.number().int().optional(), card_type: z.string().optional(), is_external_request: z.boolean().optional(), name: z.string().optional()}); export const getApiBasketsDashboardResponse = z.object({graph: z.object({creation_date: z.string().optional(), creation_hour: z.string().optional(), total_amount: z.number().optional()}).optional(), total_revenue: z.number().optional(), total_number: z.number().optional(), average_revenue: z.number().optional(), shares: z.array(z.object({type: z.number().int().optional(), share_percent: z.number().optional()})).optional(), shares_revenue: z.array(z.object({type: z.number().int().optional(), share_amount: z.number().optional()})).optional(), customers: z.number().int().optional(), new_customers: z.number().int().optional(), not_registered: z.number().int().optional(), new_registered: z.number().optional(), last_10_sales: z.array(basket).optional(), transactions: z.number().int().optional(), average_item: z.number().optional(), "returns %": z.number().optional(), returned: z.number().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 }; /** * Get Baskets Dashboard Data * * GET /baskets/dashboard */ export const getApiBasketsDashboard = async (options?: GetApiBasketsDashboardOptions) => { const {baseUrl = "https://api-dev.shopbox.com/api/v3", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (TokenAuth !== undefined) query.set("accessToken", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/baskets/dashboard${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(getApiBasketsDashboardResponse, await res.json()) };