import {z} from 'zod' export type CommonCustomFields = Record; export type ConnectedAccountUpdate = {custom_fields?: CommonCustomFields | undefined}; export type CreateApiConnectedAccountsIdArgs = {id: string, body: ConnectedAccountUpdate}; export type CreateApiConnectedAccountsIdOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const connectedAccountAddress = z.object({line_1: z.string(), line_2: z.string(), building_number: z.string().optional(), street_name: z.string().optional(), postal_code: z.string(), region_state: z.string(), city: z.string(), country: z.string(), room: z.string().optional(), floor: z.string().optional(), postal_box: z.string().optional(), department: z.string().optional(), sub_department: z.string().optional(), building_name: z.string().optional(), district_name: z.string().optional(), city_location_name: z.string().optional()}); export const connectedAccountBankService = z.enum(["sct", "sdd", "sct_inst", "sct_inst_watch", "psr_in", "psr_out", "end_of_day_statements", "intra_day_statements", "debit_credit_notifications"]); export const commonCustomFields = z.object({}); export const connectedAccountDetails = z.object({id: z.string(), object: z.string(), type: z.enum(["financial_institution", "corporate"]), name: z.string(), bank_code: z.string(), bank_name: z.string(), bank_address: connectedAccountAddress.optional(), creditor_identifier: z.string().max(35), legal_entity_identifier: z.string().min(20).max(20).optional(), currency: z.string(), account_number: z.string(), account_number_format: z.enum(["iban", "uk_account_number", "us_account_number", "tokenized", "other"]).optional(), bank_code_format: z.enum(["bic", "uk_sorting_code", "us_aba_routing_number", "tokenized", "other"]).optional(), services_activated: z.array(connectedAccountBankService), file_auto_approval: z.boolean(), address: connectedAccountAddress, metadata: z.object({}), custom_fields: commonCustomFields, disabled_at: z.string().nullable(), created_at: z.string(), bank_data: z.object({external_id: z.string().optional()}).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 }; /** * Update connected account * * POST /connected_accounts/{id} */ export const createApiConnectedAccountsId = async (args: CreateApiConnectedAccountsIdArgs, options?: CreateApiConnectedAccountsIdOptions) => { 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 (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/connected_accounts/${encodeURIComponent(String(args.id))}${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(connectedAccountDetails, await res.json()) };