import {z} from 'zod' export type GetApiSalesArgs = {page?: number | undefined, branch_ids?: Array | undefined, created_at?: Array | undefined, created_by_ids?: Array | undefined, client_ids?: Array | undefined, sort?: ('id' | '-id') | undefined}; export type GetApiSalesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSalesResponse = z.object({data: z.array(z.object({id: z.number().int().optional(), id_label: z.string().optional(), created_at: z.number().int().optional(), created_by_id: z.number().int().optional(), client_id: z.number().int().optional(), warehouse_id: z.number().int().optional(), products: z.array(z.object({title: z.string().optional(), uom: z.object({id: z.number().int().optional(), description: z.string().optional(), title: z.string().optional()}).optional(), code: z.string().optional(), article: z.string().optional(), amount: z.number().int().optional(), price: z.number().int().optional(), cost: z.number().int().optional(), discount_value: z.number().int().optional(), service: z.boolean().optional(), type: z.number().int().optional()})).optional(), description: z.string().optional(), ad_campaign: z.object({}).optional()})).optional(), page: z.number().int().optional(), count: z.number().int().optional(), success: z.boolean().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 Sales * * GET /sales */ export const getApiSales = async (args: GetApiSalesArgs, options?: GetApiSalesOptions) => { const {baseUrl = "https://api.roapp.io/v2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) for (const item of args.branch_ids ?? []) query.append("branch_ids", String(item)) for (const item of args.created_at ?? []) query.append("created_at", String(item)) for (const item of args.created_by_ids ?? []) query.append("created_by_ids", String(item)) for (const item of args.client_ids ?? []) query.append("client_ids", String(item)) if (args.sort != null) query.append("sort", String(args.sort)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/sales${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(getApiSalesResponse, await res.json()) };