import {z} from 'zod' export type BankTokenCountryEnum = 'AT' | 'BE' | 'BG' | 'HR' | 'CY' | 'DK' | 'EE' | 'FI' | 'FR' | 'DE' | 'GR' | 'HU' | 'IS' | 'IE' | 'IT' | 'LV' | 'LT' | 'LU' | 'MT' | 'NL' | 'NO' | 'PL' | 'PT' | 'RO' | 'SK' | 'SI' | 'ES' | 'SE'; export type BankAccessRequest = {accounts?: boolean | undefined, balances?: boolean | undefined, transactions?: boolean | undefined}; export type BankTokenRequest = {lifetime?: number | undefined, country?: (BankTokenCountryEnum | null) | undefined, bank?: (string | null) | undefined, access?: BankAccessRequest | undefined}; export type CreateApiBankTokensArgs = {body: BankTokenRequest}; export type CreateApiBankTokensOptions = {baseUrl?: string | undefined, apiKeyPair?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bankTokenCountryEnum = z.enum(["AT", "BE", "BG", "HR", "CY", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IS", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "NO", "PL", "PT", "RO", "SK", "SI", "ES", "SE"]); export const bankAccess = z.object({accounts: z.boolean().optional(), balances: z.boolean().optional(), transactions: z.boolean().optional()}); export const bankToken = z.object({tokenString: z.string(), expiration: z.string(), isValid: z.boolean(), country: bankTokenCountryEnum.nullable().optional(), bank: z.string().max(32).nullable().optional(), access: bankAccess.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 }; /** * Generate bank verification token * * POST /bank/tokens/ */ export const createApiBankTokens = async (args: CreateApiBankTokensArgs, options?: CreateApiBankTokensOptions) => { const {baseUrl = "https://ivs.idenfy.com", fetch: fetchFn = fetch, apiKeyPair} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyPair !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyPair.username}:${apiKeyPair.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bank/tokens/${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(bankToken, await res.json()) };