import {z} from 'zod' export type GetApiApiV2ChannelArgs = {walletId: string, offset?: string | undefined, max?: string | undefined, q?: string | undefined}; export type GetApiApiV2ChannelOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const alternativeAddressDto = z.object({network: z.string().optional(), protocol: z.string().optional(), address: z.string().optional(), tag: z.string().optional(), uri: z.string().optional()}); export const merchantChannelDto = z.object({id: z.number().int().optional(), dateCreated: z.number().int().optional(), lastUpdated: z.number().int().optional(), merchantId: z.string().optional(), walletCurrency: z.string().optional(), displayCurrency: z.string().optional(), payCurrency: z.string().optional(), address: z.string().optional(), tag: z.string().optional(), network: z.string().optional(), protocol: z.string().optional(), reference: z.string().optional(), status: z.literal("OPEN").optional(), uuid: z.string().optional(), redirectUrl: z.string().optional(), uri: z.string().optional(), alternatives: z.array(alternativeAddressDto).optional(), walletId: z.string().optional(), walletDescription: z.string().optional(), pegged: z.boolean().optional(), accountReference: z.string().optional(), contact: z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), relationshipType: z.string().optional(), entity: z.object({externalId: z.string().optional(), firstName: z.string().optional(), middleName: z.string().optional(), lastName: z.string().optional(), dateOfBirth: z.string().optional(), address: z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), region: z.string().optional(), city: z.string().optional(), country: z.string().optional(), postCode: z.string().optional()}).optional(), category: z.string().optional(), type: z.string().optional()}).optional(), updatedAt: z.string().optional(), createdAt: z.string().optional()}).optional()}); export const getApiApiV2ChannelResponse = z.array(merchantChannelDto); 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 }; /** * List channels * * GET /api/v2/channel */ export const getApiApiV2Channel = async (args: GetApiApiV2ChannelArgs, options?: GetApiApiV2ChannelOptions) => { 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 (args.walletId != null) query.append("walletId", String(args.walletId)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.max != null) query.append("max", String(args.max)) if (args.q != null) query.append("q", String(args.q)) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/channel${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(getApiApiV2ChannelResponse, await res.json()) };