import {z} from 'zod' export type GetApiV3ReportUsersalesArgs = {merchantId: number, dateRange?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, limit?: number | undefined, offset?: number | undefined, transactionType?: string | undefined, status?: string | undefined, minAmount?: number | undefined, maxAmount?: number | undefined}; export type GetApiV3ReportUsersalesOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV3ReportUsersalesResponse = z.object({records: z.array(z.object({transactionCount: z.number().int().optional(), amount: z.string().optional(), username: z.string().optional(), reference: z.string().optional(), transactionType: z.string().optional(), tenderType: z.string().optional(), transactionDate: z.string().optional(), invoiceNumber: z.string().optional(), recordCount: z.number().int().optional(), totalAmount: z.string().optional(), paymentType: z.string().optional(), id: z.number().int().optional(), source: z.string().optional(), status: z.string().optional()})).optional(), recordCount: z.number().int().optional(), totals: z.object({transactionCount: z.number().int().optional(), totalAmount: z.string().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 }; /** * Get User Sales Report * * GET /v3/report/usersales */ export const getApiV3ReportUsersales = async (args: GetApiV3ReportUsersalesArgs, options?: GetApiV3ReportUsersalesOptions) => { const {baseUrl = "https://sandbox.api.mxmerchant.com/checkout", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchantId != null) query.append("merchantId", String(args.merchantId)) if (args.dateRange != null) query.append("dateRange", String(args.dateRange)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.transactionType != null) query.append("transactionType", String(args.transactionType)) if (args.status != null) query.append("status", String(args.status)) if (args.minAmount != null) query.append("minAmount", String(args.minAmount)) if (args.maxAmount != null) query.append("maxAmount", String(args.maxAmount)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/report/usersales${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(getApiV3ReportUsersalesResponse, await res.json()) };