import {z} from 'zod' export type ExpectedPaymentCounterpartyAccount = {account_number?: string | undefined, holder_name?: string | undefined}; export type ExpectedPaymentExternalAccount = {account_number?: string | undefined, holder_name?: string | undefined}; export type ExpectedPaymentCreationDetailsVirtualAccount = {virtual_account_number: string, name?: string | undefined}; export type ExpectedPaymentCreationDetailsInternalAccount = {account_number: string}; export type CommonCustomFields = Record; export type ExpectedPaymentCreationDetails = {direction: 'debit' | 'credit', amount_from: number, amount_to: number, currency: string, start_date: string, end_date: string, counterparty_account_id?: string | undefined, external_account_id?: string | undefined, counterparty_account?: ExpectedPaymentCounterpartyAccount | undefined, external_account?: ExpectedPaymentExternalAccount | undefined, virtual_account_id?: string | undefined, internal_account_id?: string | undefined, virtual_account?: ExpectedPaymentCreationDetailsVirtualAccount | undefined, internal_account?: ExpectedPaymentCreationDetailsInternalAccount | undefined, connected_account_id: string, metadata?: Record | undefined, custom_fields?: CommonCustomFields | undefined, descriptions?: Array | undefined}; export type CreateApiExpectedPaymentsArgs = {'Idempotency-Key'?: string | undefined, body: ExpectedPaymentCreationDetails}; export type CreateApiExpectedPaymentsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const expectedPaymentCounterpartyAccount = z.object({account_number: z.string().optional(), holder_name: z.string().optional()}); export const commonVirtualAccount = z.object({virtual_account_number: z.string(), name: z.string().max(100).optional()}); export const expectedPaymentExternalAccount = z.object({account_number: z.string().optional(), holder_name: z.string().optional()}); export const expectedPaymentInternalAccount = z.object({account_number: z.string().optional(), name: z.string().optional(), type: z.string().optional()}); export const commonCustomFields = z.object({}); export const expectedPaymentDetails = z.object({id: z.string(), idempotency_key: z.string().optional(), object: z.literal("expected_payment"), direction: z.enum(["credit", "debit"]), amount_from: z.number().int().gte(0), amount_to: z.number().int().gte(0), currency: z.string(), start_date: z.string().optional(), end_date: z.string().optional(), connected_account_id: z.string().optional(), counterparty_account_id: z.string().optional(), counterparty_account: expectedPaymentCounterpartyAccount, virtual_account_id: z.string().optional(), virtual_account: commonVirtualAccount, external_account_id: z.string().optional(), external_account: expectedPaymentExternalAccount.optional(), internal_account_id: z.string().optional(), internal_account: expectedPaymentInternalAccount.optional(), reconciliation_status: z.enum(["unreconciled", "partially_reconciled", "reconciled", "excluded"]), reconciled_amount: z.number().int().gte(0), metadata: z.object({}), custom_fields: commonCustomFields, descriptions: z.array(z.string()), virtual_account_number: z.string().optional(), created_at: z.string(), canceled_at: z.string().nullable()}); 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 }; /** * Create expected payment * * POST /expected_payments */ export const createApiExpectedPayments = async (args: CreateApiExpectedPaymentsArgs, options?: CreateApiExpectedPaymentsOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/expected_payments${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(expectedPaymentDetails, await res.json()) };