import {z} from 'zod' export type CurrencyCode = string; export type CreateBankAccountRequest = {name: string, currency: CurrencyCode, bank_name: string, subsidiary_id: string, account_code: string, mask?: string | undefined, transactions_start_date?: string | undefined}; export type CreateApiBankAccountsArgs = {body: CreateBankAccountRequest}; export type CreateApiBankAccountsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const bankAccountStatus = z.enum(["ACTIVE", "INACTIVE"]); export const bankAccount = z.object({id: z.string(), name: z.string(), currency: currencyCode, bank_name: z.string(), subsidiary_id: z.string().optional(), account_code: z.string().optional(), mask: z.string().optional(), transactions_start_date: z.string().nullable().optional(), status: bankAccountStatus}); 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 account * * POST /bank-accounts */ export const createApiBankAccounts = async (args: CreateApiBankAccountsArgs, options?: CreateApiBankAccountsOptions) => { 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-accounts${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(bankAccount, await res.json()) };