import {z} from 'zod' export type GetApiDirectDebitMandatesArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, start_signature_date?: string | undefined, end_signature_date?: string | undefined, receiving_account_id?: string | undefined, originating_account_id?: string | undefined, debtor_account_id?: string | undefined, creditor_account_id?: string | undefined, type?: ('sepa_core' | 'sepa_b2b' | 'bacs_debit') | undefined, sequence?: ('one_off' | 'recurring') | undefined, frequency?: ('one_off' | 'recurring') | undefined, reference?: string | undefined}; export type GetApiDirectDebitMandatesOptions = {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 directDebitMandatesSearchResult = z.object({records: z.array(directDebitMandatesDirectDebitMandate).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 }; /** * List direct debit mandates * * GET /direct_debit_mandates */ export const getApiDirectDebitMandates = async (args: GetApiDirectDebitMandatesArgs, options?: GetApiDirectDebitMandatesOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.start_signature_date != null) query.append("start_signature_date", String(args.start_signature_date)) if (args.end_signature_date != null) query.append("end_signature_date", String(args.end_signature_date)) if (args.receiving_account_id != null) query.append("receiving_account_id", String(args.receiving_account_id)) if (args.originating_account_id != null) query.append("originating_account_id", String(args.originating_account_id)) if (args.debtor_account_id != null) query.append("debtor_account_id", String(args.debtor_account_id)) if (args.creditor_account_id != null) query.append("creditor_account_id", String(args.creditor_account_id)) if (args.type != null) query.append("type", String(args.type)) if (args.sequence != null) query.append("sequence", String(args.sequence)) if (args.frequency != null) query.append("frequency", String(args.frequency)) if (args.reference != null) query.append("reference", String(args.reference)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(directDebitMandatesSearchResult, await res.json()) };