import {z} from 'zod' export type GetApiPayablesPlatformLedgerArgs = {Authorization: string, after_cursor?: string | undefined, before_cursor?: string | undefined, limit?: number | undefined, created_after?: string | undefined, created_before?: string | undefined, txn_type?: ('processing_fee' | 'justifi_fee' | 'return_fee' | 'noc_fee' | 'settlement' | 'adjustment') | undefined, settled?: boolean | undefined}; export type GetApiPayablesPlatformLedgerOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPayablesPlatformLedgerResponse = z.object({id: z.unknown(), type: z.string(), data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), platform_account_id: z.string().optional(), txn_type: z.enum(["processing_fee", "justifi_fee", "return_fee", "noc_fee", "settlement", "adjustment"]).optional(), amount_cents: z.number().int().optional(), currency: z.literal("usd").optional(), payer_account_id: z.string().nullable().optional(), payee_payment_id: z.string().nullable().optional(), payee_payment_fee_id: z.string().nullable().optional(), source_type: z.enum(["payee_payment", "transfer", "settlement", "null"]).nullable().optional(), source_id: z.string().nullable().optional(), external_reference: z.string().nullable().optional(), settlement_id: z.string().nullable().optional(), created_at: z.string().optional()})), page_info: z.object({start_cursor: z.string().optional(), end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().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 Platform Ledger Entries * * GET /payables/platform_ledger */ export const getApiPayablesPlatformLedger = async (args: GetApiPayablesPlatformLedgerArgs, options?: GetApiPayablesPlatformLedgerOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.after_cursor != null) query.append("after_cursor", String(args.after_cursor)) if (args.before_cursor != null) query.append("before_cursor", String(args.before_cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.txn_type != null) query.append("txn_type", String(args.txn_type)) if (args.settled != null) query.append("settled", String(args.settled)) const url = `${baseUrl.replace(/\/+$/, '')}/payables/platform_ledger${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(getApiPayablesPlatformLedgerResponse, await res.json()) };