import {z} from 'zod' export type GetApiPaymentV1TransfersTransactionReferenceArgs = {transactionReference: string}; export type GetApiPaymentV1TransfersTransactionReferenceOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const fiatCurrencyCode = z.enum(["USD", "EUR", "GBP"]); export const money = z.object({value: z.number(), currency: fiatCurrencyCode}); export const companyDetails = z.object({name: z.string()}); export const individualDetails = z.object({firstName: z.string(), lastName: z.string(), dateOfBirth: z.string()}); export const transferDetails = z.object({type: z.literal("WALLET").optional(), beneficiary: z.object({entityType: z.enum(["INDIVIDUAL", "COMPANY"]).optional(), companyDetails: companyDetails.optional(), individualDetails: individualDetails.optional(), address: z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), region: z.string().optional(), city: z.string().optional(), countryCode: z.string().optional(), postCode: z.string().optional()}).optional(), walletId: z.string().optional()}).optional()}); export const metadata = z.record(z.string(), z.string().max(100)); export const transferResponse = z.object({transactionReference: z.string().optional(), paymentReference: z.string().optional(), amount: money.optional(), fee: money.optional(), walletId: z.string().optional(), status: z.enum(["PENDING", "COMPLETED", "FAILED", "REJECTED"]).optional(), createdAt: z.string().optional(), details: transferDetails.optional(), metadata: metadata.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 }; /** * Get transfer * * GET /payment/v1/transfers/{transactionReference} */ export const getApiPaymentV1TransfersTransactionReference = async (args: GetApiPaymentV1TransfersTransactionReferenceArgs, options?: GetApiPaymentV1TransfersTransactionReferenceOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/payment/v1/transfers/${encodeURIComponent(String(args.transactionReference))}${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(transferResponse, await res.json()) };