import {z} from 'zod' export type CurrencyCode = string; export type RoundedMonetaryAmount = {amount: string, currency: CurrencyCode}; export type ExternalReference = {type: string, id: string, url?: string | undefined}; export type CreateBankTransactionRequest = {bank_account_id: string, date: string, name: string, merchant: string, net_amount: RoundedMonetaryAmount, fee?: RoundedMonetaryAmount | undefined, pending?: boolean | undefined, excluded?: boolean | undefined, external_references?: Array | undefined}; export type CreateApiBankTransactionsArgs = {body: CreateBankTransactionRequest}; export type CreateApiBankTransactionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const externalReference = z.object({type: z.string(), id: z.string(), url: z.string().optional()}); export const paidTransaction = z.object({id: z.string(), type: z.enum(["INVOICE", "BILL", "CHARGE", "REIMBURSEMENT"])}); export const reconciledTransaction = z.object({id: z.string(), type: z.enum(["BILL", "CHARGE", "REIMBURSEMENT", "INVOICE", "CREDIT_MEMO", "VENDOR_CREDIT", "JOURNAL_ENTRY", "BANK_TRANSACTION"]), payment_id: z.string().optional(), amount: roundedMonetaryAmount.optional(), paid_transaction: paidTransaction.optional()}); export const bankTransaction = z.object({id: z.string(), bank_account_id: z.string(), subsidiary_id: z.string(), date: z.string(), name: z.string(), merchant: z.string(), net_amount: roundedMonetaryAmount, fee: roundedMonetaryAmount, pending: z.boolean(), excluded: z.boolean(), external_references: z.array(externalReference).optional(), reconciled_transactions: z.array(reconciledTransaction)}); 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 }; /** * Creates a bank transaction * * POST /bank-transactions */ export const createApiBankTransactions = async (args: CreateApiBankTransactionsArgs, options?: CreateApiBankTransactionsOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/bank-transactions${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(bankTransaction, await res.json()) };