import {z} from 'zod' export type BepKey = string; export type GetApiBepBepKeyArgs = {bepKey: BepKey}; export type GetApiBepBepKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bepKey = z.string(); export const schemasAmountValue = z.number().int().gte(0).lte(900000000); export const componentsSchemasCurrencyCode = z.literal("EUR"); export const schemasAmount = z.object({value: schemasAmountValue, currency: componentsSchemasCurrencyCode}); export const schemasVersion = z.number().int(); export const bepInitiatedResponse = z.object({authenticationStatus: z.literal("initiated"), key: bepKey, amount: schemasAmount, authenticationMethod: z.enum(["bancontactIntent", "qrCode"]), created: z.string(), version: schemasVersion}); export const schemasExpiryMonth = z.number().int().gte(1).lte(12); export const schemasExpiryYear = z.number().int().gte(2020).lte(2050); export const schemasPan = z.string(); export const authenticationMethod = z.enum(["bancontactIntent", "qrCode"]); export const bepSuccessResponse = z.object({authenticationStatus: z.literal("authenticated"), key: bepKey, amount: schemasAmount, card: z.object({expiryMonth: schemasExpiryMonth, expiryYear: schemasExpiryYear, number: schemasPan}), bepaf: z.string().min(1).max(28), authenticationMethod: authenticationMethod, created: z.string(), version: schemasVersion}); export const bepFailResponse = z.object({authenticationStatus: z.literal("non-authenticated"), key: bepKey, amount: schemasAmount, authenticationMethod: z.enum(["bancontactIntent", "qrCode"]), created: z.string(), version: schemasVersion}); export const bepResponse = z.union([bepInitiatedResponse, bepSuccessResponse, bepFailResponse]); 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 }; /** * Get BEP * * GET /bep/{bepKey} * * Also accepts auth: BearerToken */ export const getApiBepBepKey = async (args: GetApiBepBepKeyArgs, options?: GetApiBepBepKeyOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bep/${encodeURIComponent(String(args.bepKey))}${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(bepResponse, await res.json()) };