import {z} from 'zod' export type GetApiOrdersArgs = {start_date?: string | undefined, end_date?: string | undefined, status?: ('opened' | 'paid') | undefined, page_offset?: number | undefined, page_size?: number | undefined}; export type GetApiOrdersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const itemObject = z.object({id: z.string().optional(), name: z.string().optional(), reference: z.string().optional(), price: z.number().optional(), quantity: z.number().optional(), discount: z.number().optional(), taxRate: z.number().optional(), totalPrice: z.number().optional()}); export const orderStatus = z.enum(["opened", "paid"]); export const orderObject = z.object({id: z.string().optional(), reference: z.string(), customerId: z.string().optional(), customerName: z.string().optional(), amount: z.number().optional(), currency: z.string(), orderDetails: z.array(itemObject), subTotalAmount: z.number().optional(), deliveryCharges: z.number().optional(), deductedAmount: z.number().optional(), totalTax: z.number().optional(), finalAmount: z.number().optional(), status: orderStatus.optional(), deviceId: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const getApiOrdersResponse = z.array(orderObject); 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 }; /** * Returns list of orders * * GET /orders */ export const getApiOrders = async (args: GetApiOrdersArgs, options?: GetApiOrdersOptions) => { const {baseUrl = "https://api.test.bictorys.com/pay/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.status != null) query.append("status", String(args.status)) if (args.page_offset != null) query.append("page_offset", String(args.page_offset)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/orders${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(getApiOrdersResponse, await res.json()) };