import {z} from 'zod' export type Address = {address_line1?: string | undefined, address_line2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined}; export type PhoneNumber = {country_code?: string | undefined, number?: string | undefined}; export type UpdateCardAccountHolder = {first_name?: string | undefined, last_name?: string | undefined, billing_address?: Address | undefined, phone?: PhoneNumber | undefined}; export type UpdateCustomerRequest = {id?: string | undefined, default?: boolean | undefined}; export type NullableInstrumentMetadata = {[key: string]: string} | null; export type AccountHolder = {type: 'individual' | 'corporate' | 'government', first_name: string, last_name: string, tax_id?: string | undefined, date_of_birth?: string | undefined, country_of_birth?: string | undefined, residential_status?: ('resident' | 'non_resident') | undefined, billing_address: Address, phone?: PhoneNumber | undefined, identification?: ({type?: string | undefined, issuing_country?: string | undefined, number?: string | undefined}) | undefined} | {type: 'individual' | 'corporate' | 'government', company_name: string, tax_id?: string | undefined, billing_address?: Address | undefined} | {type: 'individual' | 'corporate' | 'government', company_name: string, tax_id?: string | undefined, billing_address?: Address | undefined}; export type BankDetails = {name?: string | undefined, branch?: string | undefined, address?: Address | undefined}; export type UpdateInstrumentRequest = {type?: string | undefined, expiry_month?: number | undefined, expiry_year?: number | undefined, name?: string | undefined, account_holder?: UpdateCardAccountHolder | undefined, customer?: UpdateCustomerRequest | undefined, metadata?: NullableInstrumentMetadata | undefined} | {type?: string | undefined, account_type?: ('savings' | 'current' | 'cash') | undefined, account_number?: string | undefined, bank_code?: string | undefined, branch_code?: string | undefined, iban?: string | undefined, bban?: string | undefined, swift_bic?: string | undefined, currency?: string | undefined, country?: string | undefined, processing_channel_id?: string | undefined, account_holder?: AccountHolder | undefined, bank?: BankDetails | undefined, customer?: UpdateCustomerRequest | undefined, metadata?: NullableInstrumentMetadata | undefined} | {type?: string | undefined, instrument_data?: ({type?: ('Core' | 'B2B') | undefined, account_number: string, country: string, currency: string, payment_type: 'recurring' | 'regular', mandate_id?: string | undefined, date_of_signature?: string | undefined}) | undefined, account_holder?: ({first_name: string, last_name: string, billing_address: {address_line1: string, address_line2: string, city: string, zip: string, country: string}, company_name?: string | undefined, type?: ('individual' | 'corporate') | undefined}) | undefined} | {type?: string | undefined, instrument_data?: ({account_type?: ('savings' | 'checking') | undefined, account_number?: string | undefined, bank_code?: string | undefined, currency?: string | undefined, country?: string | undefined}) | undefined, account_holder?: ({first_name: string, last_name: string, company_name: string, type: 'individual' | 'corporate'}) | undefined} | {type?: string | undefined, instrument_data?: ({account_number?: string | undefined, bank_code?: string | undefined, country?: string | undefined, currency?: string | undefined, payment_type?: ('Recurring' | 'Regular') | undefined, allow_partial_match?: boolean | undefined}) | undefined, account_holder?: ({first_name?: string | undefined, last_name?: string | undefined, company_name?: string | undefined, billing_address?: ({address_line1?: string | undefined, address_line2?: string | undefined, city?: string | undefined, zip?: string | undefined, country?: string | undefined}) | undefined, type?: ('individual' | 'corporate') | undefined}) | undefined}; export type PatchApiInstrumentsIdArgs = {id: string, body: UpdateInstrumentRequest}; export type PatchApiInstrumentsIdOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const updateInstrumentResponse = z.discriminatedUnion("type", [z.object({type: z.literal("card"), fingerprint: z.string()}), z.object({type: z.literal("bank_account"), fingerprint: z.string()}), z.object({type: z.literal("sepa"), id: z.string(), fingerprint: z.string()}), z.object({type: z.literal("ach"), id: z.string(), fingerprint: z.string()}), z.object({type: z.literal("bacs"), id: z.string(), fingerprint: z.string()})]); 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 an instrument * * PATCH /instruments/{id} * * Also accepts auth: ApiSecretKey */ export const patchApiInstrumentsId = async (args: PatchApiInstrumentsIdArgs, options: PatchApiInstrumentsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/instruments/${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(updateInstrumentResponse, await res.json()) };