import {z} from 'zod' export type GetApiResellerV1ReconciliationTransfersArgs = {number?: number | undefined, size?: number | undefined}; export type GetApiResellerV1ReconciliationTransfersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transfer = z.object({id: z.string().optional(), transferRequestId: z.number().int().optional(), status: z.enum(["NEW", "SUBMITTED", "PENDING", "COMPLETE", "FAILED", "OVERDUE", "CANCELED"]).optional(), accountReceivable: z.number().optional(), accountPayable: z.number().optional(), netReceivable: z.number().optional(), netPayable: z.number().optional(), currency: z.enum(["USD", "CAD", "EUR", "JPY", "GBP", "KRW", "CHF", "SEK", "SGD", "MYR", "AUD", "MXN", "INR", "BRL", "DKK", "NZD", "NOK", "ZAR", "PHP", "CNY", "SAR", "GTQ", "IDR", "ARS", "COP", "PEN"]).optional(), transferDate: z.number().optional()}); export const pageMetadata = z.object({number: z.number(), size: z.number(), totalElements: z.number(), totalPages: z.number()}); export const transferList = z.object({content: z.array(transfer).optional(), page: pageMetadata.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 }; /** * List transfers * * GET /reseller/v1/reconciliation/transfers */ export const getApiResellerV1ReconciliationTransfers = async (args: GetApiResellerV1ReconciliationTransfersArgs, options?: GetApiResellerV1ReconciliationTransfersOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.number != null) query.append("number", String(args.number)) if (args.size != null) query.append("size", String(args.size)) const url = `${baseUrl.replace(/\/+$/, '')}/reseller/v1/reconciliation/transfers${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(transferList, await res.json()) };