import {z} from 'zod' export type CountryId = 'DE' | 'AT' | 'CH'; export type Xs2aRiskUploadJsonAccount = {holder: string, description: string, iban: string, bic?: string | undefined, bank_name?: string | undefined, country_id: CountryId, joint_account?: boolean | undefined}; export type CurrencyId = 'EUR' | 'CHF'; export type Xs2aRiskUploadJsonBalance = {balance: number | null, limit: number, available: number, currency: CurrencyId}; export type Xs2aRiskUploadJsonTurnoversTurnover = {booking_date: string, amount: number, currency_id?: CurrencyId | undefined, purpose: Array, counter_iban: string, counter_bic: string, counter_holder: string}; export type Xs2aRiskUploadJsonWrapper = {account?: Xs2aRiskUploadJsonAccount | undefined, balance?: Xs2aRiskUploadJsonBalance | undefined, turnovers?: Array | undefined}; export type Xs2aRiskUploadJson = Array; export type CreateApiRisksUploadWizardSessionIdArgs = {'wizard-session-id': string, format?: ('json' | 'json2' | 'camt') | undefined, body: Xs2aRiskUploadJson}; export type CreateApiRisksUploadWizardSessionIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const countryId = z.enum(["DE", "AT", "CH"]); export const metadata = z.record(z.string(), z.unknown()).nullable(); export const xs2aRiskUploadJsonSuccess = z.object({id: z.string().optional(), transaction: z.string().optional(), account_holder: z.string().optional(), iban: z.string().optional(), bic: z.string().optional(), bank_name: z.string().optional(), country_id: countryId.optional(), testmode: z.number().int().optional(), created_at: z.string().optional(), metadata: metadata.optional(), merchant_id: z.string().optional(), object: 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 }; /** * Complete xs2a.risk by uploading account data to our systems. * * POST /risks/upload/{wizard-session-id} */ export const createApiRisksUploadWizardSessionId = async (args: CreateApiRisksUploadWizardSessionIdArgs, options?: CreateApiRisksUploadWizardSessionIdOptions) => { 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 (args.format != null) query.append("format", String(args.format)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/risks/upload/${encodeURIComponent(String(args['wizard-session-id']))}${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(xs2aRiskUploadJsonSuccess, await res.json()) };