import {z} from 'zod' export type GetApiOrdersFinancialArgs = {limit?: string | undefined, offset?: string | undefined, clientId?: string | undefined, 'access-token'?: string | undefined, body: {channel?: Array | undefined, endDate?: string | undefined, listingIds?: Array | undefined, startDate?: string | undefined}}; export type GetApiOrdersFinancialOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOrdersFinancialResponse = z.object({error: z.unknown(), message: z.string().optional(), result: z.object({ordersData: z.array(z.object({channel: z.string().optional(), deductions: z.object({commission: z.number().optional(), commissionTax: z.number().optional(), deliveryCommission: z.number().optional(), deliveryFee: z.number().optional(), ezReward: z.number().optional(), marketingAdjustments: z.number().optional(), marketingFees: z.number().optional(), merchantFee: z.number().optional(), otherPayments: z.number().optional(), paymentTransactionFee: z.number().optional(), preferredPartnerProgram: z.number().optional(), promoValue: z.number().optional(), taxWithHeld: z.number().optional(), tips: z.number().optional(), total: z.number().optional()}).optional(), deliveryUUID: z.string().optional(), difference: z.object({adjustments: z.object({itemTax: z.number().optional(), itemValue: z.number().optional(), total: z.number().optional()}).optional(), errorCharges: z.object({itemTax: z.number().optional(), itemValue: z.number().optional(), total: z.number().optional()}).optional(), refundByVoosh: z.object({itemTax: z.number().optional(), itemValue: z.number().optional(), total: z.number().optional()}).optional(), total: z.object({itemTax: z.number().optional(), itemValue: z.number().optional(), total: z.number().optional()}).optional()}).optional(), estimatedPayout: z.number().optional(), listingId: z.string().optional(), netPayout: z.object({total: z.number().optional()}).optional(), orderDate: z.string().optional(), orderId: z.string().optional(), orderStatus: z.object({isPositive: z.boolean().optional(), value: z.string().optional()}).optional(), payoutDiff: z.unknown(), storeId: z.string().optional(), subTotal: z.object({offPremise: z.number().optional()}).optional(), tax: z.number().optional(), transactions: z.array(z.object({description: z.unknown(), netPayout: z.number().optional(), payoutId: z.string().optional(), transactionDate: z.string().optional(), transactionId: z.string().optional(), transactionType: z.string().optional()})).optional()})).optional(), showDummyData: z.boolean().optional()}).optional(), status: z.boolean().optional(), statusCode: 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 Order Financials * * GET /orders/financial */ export const getApiOrdersFinancial = async (args: GetApiOrdersFinancialArgs, options?: GetApiOrdersFinancialOptions) => { const {baseUrl = "https://growapiservicesv2server.voosh.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.clientId != null) query.append("clientId", String(args.clientId)) if (args['access-token'] != null) headers.set("access-token", String(args['access-token'])) const url = `${baseUrl.replace(/\/+$/, '')}/orders/financial${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(getApiOrdersFinancialResponse, await res.json()) };