import {z} from 'zod' export type CountryId = 'DE' | 'AT' | 'CH'; export type CurrencyId = 'EUR' | 'CHF'; export type Metadata = {[key: string]: unknown} | null; export type EasyOnboarding = {success_link?: string | undefined, abort_link?: string | undefined}; export type Language = 'de' | 'en'; export type Xs2aAccountCharacteristicsCheck = Record; export type Xs2aAccountLinkedOffersCheck = Record; export type Filters = Array; export type Xs2aAccountSnapshot = {days?: number | undefined, from?: string | undefined, to?: string | undefined, filters?: Filters | undefined, all_accounts?: boolean | undefined, all_tags?: boolean | undefined}; export type Xs2aAccountStatusCheck = {limit_turnover_days?: number | undefined, all_accounts?: boolean | undefined}; export type Xs2aAgeVerificationCheck = Record; export type Xs2aAllAccountsCheck = Record; export type Xs2aBalanceCheck = {check_amount?: number | undefined, check_currency_id?: CurrencyId | undefined}; export type Xs2aBalanceOverview = Record; export type Xs2aCashflowOverview = Record; export type Xs2aChargebackCheck = Record; export type Xs2aChildrenCheck = Record; export type Xs2aCollectionCheck = {check_amount?: number | undefined, check_currency_id?: CurrencyId | undefined}; export type Checks = Array; export type Xs2aCreditCheck = {checks?: Checks | undefined}; export type Xs2aDirectDebitCheck = {check_amount?: number | undefined, check_currency_id?: CurrencyId | undefined, start_date?: string | undefined, end_date?: string | undefined}; export type Xs2aFactSheetLists = {limit_turnover_days?: number | undefined, fields?: (Array<{tags?: Array | undefined, categories?: Array | undefined, name?: string | undefined, min_amount?: number | undefined, max_amount?: number | undefined}>) | undefined}; export type ExpectedHolderObject = {firstname?: string | undefined, name?: string | undefined}; export type Xs2aHolderIdentificationCheck = {expect_holders: Array}; export type Xs2aIncomeCheck = {limit_turnover_days?: number | undefined}; export type Xs2aInsuranceContractCheck = {days?: number | undefined}; export type Xs2aNameCheck = {name: string, firstname: string}; export type Xs2aOverdraftLimitCheck = Record; export type Xs2aProfitLossCheck = Record; export type CalculationsTags = string; export type CalculationsCategories = string; export type CalculationsFunctions = 'sum' | 'count' | 'median' | 'average' | 'last' | 'min' | 'max' | 'min_date' | 'max_date' | 'clean_sum' | 'exists' | 'sums_of_every_month' | 'counts_of_every_month' | 'median_of_all_month_sums' | 'average_of_all_month_sums' | 'min_of_all_month_sums' | 'max_of_all_month_sums' | 'distinct_counter_holders'; export type RiskCalculations = {tags?: Array | undefined, categories?: Array | undefined, functions?: Array | undefined, name?: string | undefined, days?: number | undefined, from?: string | undefined, to?: string | undefined, full_months?: string | undefined, children_categories?: boolean | undefined, match_tags_with_categories?: boolean | undefined, include_current_month?: boolean | undefined}; export type Xs2aRiskCalculationsCheck = {calculations: Array}; export type Xs2aSeizureCheck = {days?: number | undefined}; export type Xs2aStandingOrdersCheck = Record; export type Xs2aRisk = {bank_code?: number | undefined, country_id?: CountryId | undefined, account_number?: string | undefined, iban?: string | undefined, bic?: string | undefined, check_amount?: number | undefined, check_currency_id?: CurrencyId | undefined, metadata?: Metadata | undefined, merchant_id?: string | undefined, fints_product_id?: string | undefined, easy_onboarding?: EasyOnboarding | undefined, language?: Language | undefined, xs2a_account_characteristics_check?: Xs2aAccountCharacteristicsCheck | undefined, xs2a_account_linked_offers_check?: Xs2aAccountLinkedOffersCheck | undefined, xs2a_account_snapshot?: Xs2aAccountSnapshot | undefined, xs2a_account_status_check?: Xs2aAccountStatusCheck | undefined, xs2a_age_verification_check?: Xs2aAgeVerificationCheck | undefined, xs2a_all_accounts_check?: Xs2aAllAccountsCheck | undefined, xs2a_balance_check?: Xs2aBalanceCheck | undefined, xs2a_balance_overview?: Xs2aBalanceOverview | undefined, xs2a_cashflow_overview?: Xs2aCashflowOverview | undefined, xs2a_chargeback_check?: Xs2aChargebackCheck | undefined, xs2a_children_check?: Xs2aChildrenCheck | undefined, xs2a_collection_check?: Xs2aCollectionCheck | undefined, xs2a_credit_check?: Xs2aCreditCheck | undefined, xs2a_direct_debit_check?: Xs2aDirectDebitCheck | undefined, xs2a_fact_sheet_lists?: Xs2aFactSheetLists | undefined, xs2a_holder_identification_check?: Xs2aHolderIdentificationCheck | undefined, xs2a_income_check?: Xs2aIncomeCheck | undefined, xs2a_insurance_contract_check?: Xs2aInsuranceContractCheck | undefined, xs2a_name_check?: Xs2aNameCheck | undefined, xs2a_overdraft_limit_check?: Xs2aOverdraftLimitCheck | undefined, xs2a_profit_loss_check?: Xs2aProfitLossCheck | undefined, xs2a_risk_calculations_check?: Xs2aRiskCalculationsCheck | undefined, xs2a_seizure_check?: Xs2aSeizureCheck | undefined, xs2a_standing_orders_check?: Xs2aStandingOrdersCheck | undefined}; export type CreateApiRisksArgs = {body: Xs2aRisk}; export type CreateApiRisksOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const wizardSessionResponse = z.object({wizard_session_key: z.string().optional(), transaction: z.string().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 xs2a.risk object * * POST /risks */ export const createApiRisks = async (args: CreateApiRisksArgs, options?: CreateApiRisksOptions) => { const {baseUrl = "https://api.xs2a.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/risks${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(wizardSessionResponse, await res.json()) };