import {z} from 'zod' export type ExpressTheme = {logo?: string | undefined, primary?: string | undefined, secondary?: string | undefined, accent?: string | undefined}; export type CreateExpressSessionExistingCustomerRequest = {customerReference: string, theme?: ExpressTheme | undefined}; export type CreateExpressSessionNewCustomerRequest = {type: 'INDIVIDUAL' | 'COMPANY', countryCode: string, externalCustomerReference: string, theme?: ExpressTheme | undefined}; export type CreateApiPlatformV1WalletSessionsArgs = {body: CreateExpressSessionExistingCustomerRequest | CreateExpressSessionNewCustomerRequest}; export type CreateApiPlatformV1WalletSessionsOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const expressTheme = z.object({logo: z.string().optional(), primary: z.string().optional(), secondary: z.string().optional(), accent: z.string().optional()}); export const expressSessionResponse = z.object({externalCustomerReference: z.string().optional(), customerReference: z.string().optional(), customerType: z.enum(["INDIVIDUAL", "COMPANY"]).optional(), useCase: z.enum(["STABLECOIN_PAYOUTS", "EMBEDDED_STABLECOIN_WALLETS", "EMBEDDED_FIAT_ACCOUNTS"]).optional(), countryCode: z.string().max(2).optional(), theme: expressTheme.optional(), walletSessionUrl: z.string()}); 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 express session * * POST /platform/v1/wallet-sessions */ export const createApiPlatformV1WalletSessions = async (args: CreateApiPlatformV1WalletSessionsArgs, options?: CreateApiPlatformV1WalletSessionsOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/platform/v1/wallet-sessions${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(expressSessionResponse, await res.json()) };