import {z} from 'zod' export type CountryId = 'DE' | 'AT' | 'CH'; export type GetApiBankcodesArgs = {bank_code?: string | undefined, country_id?: CountryId | undefined, bic?: string | undefined, per_page?: number | undefined, page?: number | undefined, q?: string | undefined}; export type GetApiBankcodesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const countryId = z.enum(["DE", "AT", "CH"]); export const product = z.enum(["xs2a_verify", "xs2a_risk", "xs2a_pay", "xs2a_api"]); export const bankObject = z.object({bank_code: z.string().optional(), bic: z.string().optional(), name: z.string().optional(), short_name: z.string().optional(), zipcode: z.string().optional(), city: z.string().optional(), country_id: countryId.optional(), testmode: z.string().optional(), xs2a_products: z.array(product).optional(), xs2a_supported: z.boolean().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 }; /** * Check if a bank is supported by xs2a * * GET /bankcodes */ export const getApiBankcodes = async (args: GetApiBankcodesArgs, options?: GetApiBankcodesOptions) => { const {baseUrl = "https://api.xs2a.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bank_code != null) query.append("bank_code", String(args.bank_code)) if (args.country_id != null) query.append("country_id", String(args.country_id)) if (args.bic != null) query.append("bic", String(args.bic)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (args.q != null) query.append("q", String(args.q)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bankcodes${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(bankObject, await res.json()) };