import {z} from 'zod' export type GetApiClaimsArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, start_value_date?: string | undefined, end_value_date?: string | undefined, type?: ('sepa_value_date_correction' | 'sepa_non_receipt') | undefined, connected_account_id?: string | undefined, related_payment_type?: ('payment_order' | 'incoming_payment') | undefined, related_payment_id?: string | undefined, originating_account_number?: string | undefined, receiving_account_number?: string | undefined, status?: ('created' | 'processing' | 'sent' | 'received' | 'accepted' | 'rejected') | undefined, file_id?: string | undefined}; export type GetApiClaimsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const claimCreditorReferenceType = z.object({code: z.enum(["dispatch_advice", "foreign_exchange_deal_reference", "purchase_order", "related_payment_instruction", "remittance_advice_message", "structured_communication_reference", "other"]).optional(), issuer: z.string().optional()}); export const claimCreditorReferenceInformation = z.object({type: claimCreditorReferenceType.optional(), reference: z.string().optional()}); export const claimReferredDocumentType = z.object({code: z.enum(["commercial_invoice", "credit_note_related_to_financial_adjustment", "contract", "credit_note", "debit_note", "dispatch_advice", "debit_note_related_to_financial_adjustment", "hire_invoice", "invoice_signed", "metered_service_invoice", "proforma_invoice", "purchase_order", "quotation", "self_billed_invoice", "seller_presentment", "time_sheet", "usage_report", "account_receivable_open_item", "bill_of_lading", "commercial_contract", "statement_of_account", "trade_services_utility_transaction", "voucher", "other"]).optional(), issuer: z.string().optional()}); export const claimReferredDocumentInformation = z.object({type: claimReferredDocumentType.optional(), number: z.string().optional(), related_date: z.string().optional()}); export const claimStructuredReference = z.array(z.object({creditor_reference_information: claimCreditorReferenceInformation.optional(), referred_document_information: z.array(claimReferredDocumentInformation).optional(), invoicer: z.object({name: z.string().optional()}).optional(), invoicee: z.object({name: z.string().optional()}).optional(), additional_remittance_info: z.array(z.string()).optional()})); 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 claimRelatedAccountInfo = z.object({account_number: z.string(), bank_code: z.string(), holder_name: z.string(), holder_address: commonAddress}); export const claimRelatedPaymentBankData = z.object({message_id: z.string(), end_to_end_id: z.string(), transaction_id: z.string()}); export const claimRelatedPayment = z.object({amount: z.number().int(), currency: z.literal("EUR"), direction: z.string(), value_date: z.string(), reference: z.string(), structured_reference: claimStructuredReference.optional(), originating_account: claimRelatedAccountInfo, receiving_account: claimRelatedAccountInfo, bank_data: claimRelatedPaymentBankData.optional()}); export const claimDetails = z.object({id: z.string(), object: z.literal("claim"), type: z.enum(["sepa_non_receipt", "sepa_value_date_correction"]), status: z.enum(["created", "processing", "sent", "received", "accepted", "rejected"]), status_details: z.string(), value_date: z.string(), connected_account_id: z.string(), related_payment_type: z.enum(["payment_order", "incoming_payment"]), related_payment_id: z.string(), related_payment: claimRelatedPayment, description: z.string().max(140), metadata: z.object({}), investigation_fee: z.object({amount: z.number().int().optional(), currency: z.string().optional(), account_number: z.string().optional(), bank_code: z.string().optional()}).optional(), compensation_fee: z.object({amount: z.number().int().optional(), currency: z.string().optional(), account_number: z.string().optional(), bank_code: z.string().optional()}).optional(), bank_data: z.object({reference: z.string(), assignment_id: z.string(), case_id: z.string(), file_id: z.string(), file_reference: z.string().optional()}), created_at: z.string()}); export const claimSearchResult = z.object({records: z.array(claimDetails)}); 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 claims * * GET /claims */ export const getApiClaims = async (args: GetApiClaimsArgs, options?: GetApiClaimsOptions) => { 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_value_date != null) query.append("start_value_date", String(args.start_value_date)) if (args.end_value_date != null) query.append("end_value_date", String(args.end_value_date)) if (args.type != null) query.append("type", String(args.type)) if (args.connected_account_id != null) query.append("connected_account_id", String(args.connected_account_id)) if (args.related_payment_type != null) query.append("related_payment_type", String(args.related_payment_type)) if (args.related_payment_id != null) query.append("related_payment_id", String(args.related_payment_id)) if (args.originating_account_number != null) query.append("originating_account_number", String(args.originating_account_number)) if (args.receiving_account_number != null) query.append("receiving_account_number", String(args.receiving_account_number)) if (args.status != null) query.append("status", String(args.status)) if (args.file_id != null) query.append("file_id", String(args.file_id)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/claims${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(claimSearchResult, await res.json()) };