import {z} from 'zod' export type GetApiLoansArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, status?: ('approved' | 'ready_for_disbursement' | 'pending_disbursement' | 'active' | 'paused' | 'closed') | undefined}; export type GetApiLoansOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const loanDisbursementDetailsConnectedAccountId = z.string(); export const loanDisbursementDetailsInternalAccountId = z.string(); export const loanDisbursementDetailsReceivingAccountId = z.string(); export const loanRepaymentDetailsDirectDebit = z.object({direct_debit_mandate_id: z.string().optional()}); export const loanRepaymentDetailsCreditTransfer = z.object({receiving_account_id: z.string().optional()}); export const commonCustomFields = z.object({}); export const loanDetails = z.object({id: z.string(), object: z.string(), status: z.enum(["approved", "ready_for_disbursement", "pending_disbursement", "active", "paused", "closed"]), amount: z.number().int().gte(0), currency: z.string(), credit_arrangement_id: z.string().optional(), cbs_credit_arrangement_id: z.string().optional(), cbs_source: z.literal("mambu"), cbs_account_type: z.literal("loan"), cbs_account_id: z.string(), disbursement_details: z.object({connected_account_id: loanDisbursementDetailsConnectedAccountId.optional(), internal_account_id: loanDisbursementDetailsInternalAccountId.optional(), receiving_account_id: loanDisbursementDetailsReceivingAccountId.optional(), related_payment_order_ids: z.array(z.string()).optional()}).optional(), repayment_details: z.object({direct_debit: loanRepaymentDetailsDirectDebit.optional(), credit_transfer: loanRepaymentDetailsCreditTransfer.optional()}).optional(), metadata: z.object({}).optional(), custom_fields: commonCustomFields.optional(), created_at: z.string()}); export const loanSearchResult = z.object({records: z.array(loanDetails).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 loans * * GET /loans */ export const getApiLoans = async (args: GetApiLoansArgs, options?: GetApiLoansOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.status != null) query.append("status", String(args.status)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/loans${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(loanSearchResult, await res.json()) };