import {z} from 'zod' export type GetApiApiV1PaymentMethodResolveArgs = {channel: 'MOBILE_MONEY' | 'BANK_ACCOUNT', accountNumber?: string | undefined, institutionCode: string, countryCode: string}; export type GetApiApiV1PaymentMethodResolveOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentMethodResolveResponse = z.object({recipientName: z.string().optional(), institutionName: z.string().optional(), institutionCode: z.string().optional()}); export const getApiApiV1PaymentMethodResolveResponse = z.object({data: paymentMethodResolveResponse.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 }; /** * Resolve payment method recipient by account number * * GET /api/v1/payment-method/resolve */ export const getApiApiV1PaymentMethodResolve = async (args: GetApiApiV1PaymentMethodResolveArgs, options?: GetApiApiV1PaymentMethodResolveOptions) => { const {baseUrl = "https://sandbox.api.afriex.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.channel != null) query.append("channel", String(args.channel)) if (args.accountNumber != null) query.append("accountNumber", String(args.accountNumber)) if (args.institutionCode != null) query.append("institutionCode", String(args.institutionCode)) if (args.countryCode != null) query.append("countryCode", String(args.countryCode)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment-method/resolve${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(getApiApiV1PaymentMethodResolveResponse, await res.json()) };