import {z} from 'zod' export type GetApiPaymentsArgs = {payroll?: string | undefined, payroll_item?: string | undefined, contractor_payment?: string | undefined, parent_id?: string | undefined, company?: string | undefined, direction?: ('credit' | 'debit') | undefined, amount_min?: string | undefined, amount_max?: string | undefined, type?: ('company_cash_requirement' | 'employee_net_pay' | 'net_pay_refund' | 'collection' | 'refund') | undefined, completion_date_after?: string | undefined, completion_date_before?: string | undefined, limit?: number | undefined, id?: Array | undefined}; export type GetApiPaymentsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPaymentsResponse = z.object({next: z.unknown(), previous: z.unknown(), results: z.array(z.object({id: z.string().optional(), status: z.string().optional(), amount: z.string().optional(), type: z.string().optional(), parent_type: z.string().optional(), parent_id: z.string().optional(), payment_attempts: z.array(z.object({id: z.string().optional(), created_at: z.string().optional(), expected_completion_date: z.string().optional(), status: z.string().optional(), failure_code: z.unknown(), payment_method: z.string().optional(), payment_instrument: z.string().optional(), wire_details: z.unknown(), ach_details: z.object({ach_trace_id: z.string().optional(), description: z.string().optional(), transaction_type_code: z.string().optional()}).optional()})).optional(), can_retry: z.boolean().optional(), can_refund: z.boolean().optional(), can_cancel: z.boolean().optional(), cancel_deadline: z.string().optional(), direction: z.string().optional(), company: z.string().optional(), can_reverse: z.boolean().optional(), reversal_deadline: z.string().nullable().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 }; /** * List payments * * GET /payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options?: GetApiPaymentsOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.payroll != null) query.append("payroll", String(args.payroll)) if (args.payroll_item != null) query.append("payroll_item", String(args.payroll_item)) if (args.contractor_payment != null) query.append("contractor_payment", String(args.contractor_payment)) if (args.parent_id != null) query.append("parent_id", String(args.parent_id)) if (args.company != null) query.append("company", String(args.company)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.amount_min != null) query.append("amount_min", String(args.amount_min)) if (args.amount_max != null) query.append("amount_max", String(args.amount_max)) if (args.type != null) query.append("type", String(args.type)) if (args.completion_date_after != null) query.append("completion_date_after", String(args.completion_date_after)) if (args.completion_date_before != null) query.append("completion_date_before", String(args.completion_date_before)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.id ?? []) query.append("id", String(item)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/payments${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(getApiPaymentsResponse, await res.json()) };