import {z} from 'zod' export type CommonAddress = {line_1?: string | undefined, line_2?: string | undefined, building_number?: string | undefined, street_name?: string | undefined, postal_code?: string | undefined, region_state?: string | undefined, city?: string | undefined, country?: string | undefined, department?: (string | null) | undefined, sub_department?: (string | null) | undefined, building_name?: (string | null) | undefined, floor?: (string | null) | undefined, postal_box?: (string | null) | undefined, room?: (string | null) | undefined, city_location_name?: (string | null) | undefined, district_name?: (string | null) | undefined}; export type DirectDebitMandatesCreditorAccount = {account_number: string, bank_code: string, account_number_format?: ('iban' | 'uk_account_number' | 'us_account_number' | 'tokenized' | 'other') | undefined, bank_code_format?: ('bic' | 'uk_sorting_code' | 'us_aba_routing_number' | 'tokenized' | 'other') | undefined, holder_name: string, creditor_identifier: string, holder_address?: CommonAddress | undefined}; export type DirectDebitMandatesOriginatingAccount = {account_number: string, bank_code: string, account_number_format?: ('iban' | 'uk_account_number' | 'us_account_number' | 'tokenized' | 'other') | undefined, bank_code_format?: ('bic' | 'uk_sorting_code' | 'us_aba_routing_number' | 'tokenized' | 'other') | undefined, holder_name: string, creditor_identifier: string, holder_address?: CommonAddress | undefined}; export type DirectDebitMandatesDebtorAccount = {account_number: string, bank_code: string, account_number_format?: ('iban' | 'uk_account_number' | 'us_account_number' | 'tokenized' | 'other') | undefined, bank_code_format?: ('bic' | 'uk_sorting_code' | 'us_aba_routing_number' | 'tokenized' | 'other') | undefined, holder_name: string, holder_address?: CommonAddress | undefined}; export type DirectDebitMandatesReceivingAccount = {account_number: string, bank_code: string, account_number_format?: ('iban' | 'uk_account_number' | 'us_account_number' | 'tokenized' | 'other') | undefined, bank_code_format?: ('bic' | 'uk_sorting_code' | 'us_aba_routing_number' | 'tokenized' | 'other') | undefined, holder_name: string, holder_address?: CommonAddress | undefined}; export type CommonCustomFields = Record; export type DirectDebitMandatesCreateDirectDebitMandate = {direction: 'outgoing' | 'incoming', type: 'sepa_core' | 'sepa_b2b' | 'bacs_debit', connected_account_id: string, creditor_account?: DirectDebitMandatesCreditorAccount | undefined, originating_account?: DirectDebitMandatesOriginatingAccount | undefined, creditor_account_id?: string | undefined, originating_account_id?: string | undefined, debtor_account?: DirectDebitMandatesDebtorAccount | undefined, receiving_account?: DirectDebitMandatesReceivingAccount | undefined, debtor_account_id?: string | undefined, receiving_account_id?: string | undefined, sequence?: ('one_off' | 'recurring') | undefined, frequency?: ('one_off' | 'recurring') | undefined, generate_mandate_reference?: boolean | undefined, reference?: string | undefined, signature_date?: string | undefined, metadata?: Record | undefined, custom_fields?: CommonCustomFields | undefined}; export type CreateApiDirectDebitMandatesArgs = {body: DirectDebitMandatesCreateDirectDebitMandate}; export type CreateApiDirectDebitMandatesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const commonAddress = z.object({line_1: z.string().optional(), line_2: z.string().optional(), building_number: z.string().optional(), street_name: z.string().optional(), postal_code: z.string().optional(), region_state: z.string().optional(), city: z.string().optional(), country: z.string().optional(), department: z.string().nullable().optional(), sub_department: z.string().nullable().optional(), building_name: z.string().nullable().optional(), floor: z.string().nullable().optional(), postal_box: z.string().nullable().optional(), room: z.string().nullable().optional(), city_location_name: z.string().nullable().optional(), district_name: z.string().nullable().optional()}); export const directDebitMandatesCreditorAccount = z.object({account_number: z.string(), bank_code: 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(), holder_name: z.string(), creditor_identifier: z.string().max(35), holder_address: commonAddress.optional()}); export const directDebitMandatesDebtorAccount = z.object({account_number: z.string(), bank_code: 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(), holder_name: z.string(), holder_address: commonAddress.optional()}); export const directDebitMandatesOriginatingAccount = z.object({account_number: z.string(), bank_code: 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(), holder_name: z.string(), creditor_identifier: z.string().max(35), holder_address: commonAddress.optional()}); export const directDebitMandatesReceivingAccount = z.object({account_number: z.string(), bank_code: 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(), holder_name: z.string(), holder_address: commonAddress.optional()}); export const commonCustomFields = z.object({}); export const directDebitMandatesDirectDebitMandate = z.object({id: z.string(), object: z.string(), direction: z.enum(["outgoing", "incoming"]), type: z.enum(["sepa_core", "sepa_b2b", "bacs_debit"]), status: z.enum(["active", "blocked", "canceled", "disabled", "expired", "pending_review", "rejected"]), connected_account_id: z.string(), creditor_account: directDebitMandatesCreditorAccount.optional(), creditor_account_id: z.string().optional(), creditor_account_type: z.literal("internal_account").optional(), debtor_account: directDebitMandatesDebtorAccount.optional(), debtor_account_id: z.string().optional(), debtor_account_type: z.enum(["internal_account", "external_account"]).optional(), originating_account: directDebitMandatesOriginatingAccount, originating_account_id: z.string().optional(), receiving_account: directDebitMandatesReceivingAccount, receiving_account_id: z.string().optional(), sequence: z.enum(["one_off", "recurring"]).optional(), frequency: z.enum(["one_off", "recurring"]).optional(), reference: z.string(), signature_date: z.string().optional(), last_debit_collection_date: z.string().nullable().optional(), expiry_date: z.string().nullable().optional(), metadata: z.object({}), custom_fields: commonCustomFields.optional(), created_at: z.string(), bank_data: z.object({message_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 }; /** * Create direct debit mandate * * POST /direct_debit_mandates */ export const createApiDirectDebitMandates = async (args: CreateApiDirectDebitMandatesArgs, options?: CreateApiDirectDebitMandatesOptions) => { 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(/\/+$/, '')}/direct_debit_mandates${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(directDebitMandatesDirectDebitMandate, await res.json()) };