import {z} from 'zod' export type AccountJSONPatchReplaceAlias = {op: 'replace', path: '/alias', value: string}; export type AccountJSONPatchRemoveAlias = {op: 'remove', path: '/alias'}; export type AccountJSONPatchReplaceEntity = {op: 'replace', path: '/entityId', value: string}; export type AccountJSONPatchReplaceMetadataKey = {op: 'replace', path: string, value: string}; export type AccountJSONPatchRemoveMetadataKey = {op: 'remove', path: string}; export type InstrumentType = 'US_ACH' | 'CITI_WORLDLINK'; export type Instrument = {type: InstrumentType, citiWorldlinkId: string} | {type: InstrumentType, usAchCompanyId: string, usAchCompanyName?: string | undefined, usAchImmediateOrigin?: string | undefined}; export type AccountJSONPatchAddPaymentOptionsInstrument = {op: 'add', path: '/paymentOptions/instruments/-', value: Instrument}; export type AccountJSONPatchReplacePaymentOptionsInstrument = {op: 'replace', path: string, value: Instrument}; export type AccountJSONPatchRemovePaymentOptionsInstrument = {op: 'remove', path: string}; export type AccountJSONPatchReplacePaymentOptionsInitiatorId = {op: 'replace', path: '/paymentOptions/initiatorId', value: string}; export type AccountJSONPatchRemovePaymentOptionsInitiatorId = {op: 'remove', path: '/paymentOptions/initiatorId'}; export type AccountStatus = 'OPEN' | 'CLOSED'; export type AccountJSONPatchReplaceStatus = {op: 'replace', path: '/status', value: AccountStatus}; export type BankAccountType = 'WALLET' | 'SAVINGS' | 'CHECKING' | 'INVESTMENT'; export type AccountJSONPatchReplaceType = {op: 'replace', path: '/type', value: BankAccountType}; export type AccountJSONPatchRequest = Array; export type PatchApiFinancialDataV2AccountsIdArgs = {id: string, 'If-Match'?: string | undefined, body: AccountJSONPatchRequest}; export type PatchApiFinancialDataV2AccountsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const resourceID = z.string().min(32).max(64); export const organizationID = z.string(); export const marketCode = z.string().min(2).max(2); export const currencyCode = z.string().min(3).max(3); export const accountIdentifierType = z.enum(["IBAN", "NUMBER", "ADYEN", "AU_BPAY_BILLER_CODE", "DK_FIK", "INVESTMENT_NUMBER", "MY_JOMPAY_BILLER_CODE", "NATIONAL_ID_NUMBER", "PAYPAL", "PROPRIETARY", "SE_BANKGIRO", "SE_PLUSGIRO", "SOLDO", "WALLET_ADYEN_BALANCE_PLATFORM", "WALLET_AIRWALLEX", "WALLET_AIRWALLEX_NUMBER", "WALLET_CURRENCYCLOUD", "WALLET_FLASH_PAYMENTS", "WALLET_FREEMARKET", "WALLET_HYPERWALLET", "WALLET_INPAY", "WALLET_LEAD", "WALLET_MERCURY", "WALLET_MONEYCORP", "WALLET_PAYHAWK", "WALLET_REVOLUT", "WALLET_SKRILL", "WALLET_STRIPE", "WALLET_SWISSQUOTE", "WALLET_UNIT"]); export const accountIdentifier = z.object({type: accountIdentifierType, market: marketCode, number: z.string().min(1).max(255), invalid: z.boolean().optional()}); export const routingIdentifierType = z.enum(["BIC", "AT_BLZ", "AU_BSB", "BD_BRN", "BR_BRANCH", "BR_COMPE", "BR_ISP", "CA_CPA", "CH_BCC", "CH_SIC", "CL_SBIF", "CN_APS", "CZ_NBC", "DE_BLZ", "ES_NCC", "GB_DSC", "GR_BIC", "HK_NCC", "IE_NCC", "IL_NCC", "IN_FSC", "IT_NCC", "JP_ZGN", "KR_BOK", "KR_KFTC", "MX_ABM", "MY_NCC", "NZ_NCC", "PH_BRSTN", "PL_KNR", "PT_NCC", "RU_CBC", "SE_SBA", "SG_IBG", "TH_CBC", "TW_NCC", "US_ABA", "US_PID", "VN_CITAD", "ZA_NCC"]); export const routingConstraints = z.object({accountNumberType: accountIdentifierType.optional(), scheme: z.string().optional()}); export const routingIdentifier = z.object({type: routingIdentifierType, number: z.string().min(1).max(255), invalid: z.boolean().optional(), constraints: routingConstraints.optional()}); export const bankAccountType = z.enum(["WALLET", "SAVINGS", "CHECKING", "INVESTMENT"]); export const accountStatus = z.enum(["OPEN", "CLOSED"]); export const externalID = z.string(); export const metadata = z.record(z.string(), z.string()).nullable(); export const instrumentType = z.enum(["US_ACH", "CITI_WORLDLINK"]); export const instrument = z.discriminatedUnion("type", [z.object({type: instrumentType, citiWorldlinkId: z.string()}), z.object({type: instrumentType, usAchCompanyId: z.string(), usAchCompanyName: z.string().optional(), usAchImmediateOrigin: z.string().optional()})]); export const paymentOptions = z.object({instruments: z.array(instrument).optional(), initiatorId: z.string().optional(), disabled: z.boolean().optional()}); export const resourceVersion = z.number().int().gte(1); export const eTag = z.string(); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const provisionState = z.enum(["UNPROVISIONED", "IGNORED", "PROVISIONED"]); export const account = z.object({id: resourceID, organizationId: organizationID, thirdPartyId: z.string(), affiliationId: z.string(), entityId: z.string(), market: marketCode, currency: currencyCode, name: z.string().max(255), alias: z.string().max(255).optional(), identifiers: z.array(accountIdentifier), routing: z.array(routingIdentifier), type: bankAccountType.optional(), status: accountStatus, fictive: z.boolean(), externalId: externalID.optional(), metadata: metadata.optional(), paymentOptions: paymentOptions.optional(), version: resourceVersion, etag: eTag, created: createdTimestamp, updated: updatedTimestamp, closed: z.string().optional(), provisionState: provisionState}); 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 account * * PATCH /financial-data/v2/accounts/{id} */ export const patchApiFinancialDataV2AccountsId = async (args: PatchApiFinancialDataV2AccountsIdArgs, options?: PatchApiFinancialDataV2AccountsIdOptions) => { const {baseUrl = "https://api.atlar.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json-patch+json") headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/financial-data/v2/accounts/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(account, await res.json()) };