import {z} from 'zod' export type EntityTypeEnumDto = 'INDIVIDUAL' | 'COMPANY'; export type IndividualPartyDetailDto = {type: string, entityType: EntityTypeEnumDto, firstName: string, lastName: string, dateOfBirth: string, relationshipType: string, countryCode: string, regionCode?: unknown | undefined}; export type CompanyPartyDetailDto = {type: string, entityType: EntityTypeEnumDto, legalName: string, relationshipType: string, countryCode: string, regionCode?: unknown | undefined, registrationNumber: string}; export type PartyDetailDto = IndividualPartyDetailDto | CompanyPartyDetailDto; export type ComplianceDetailDto = {requesterIpAddress?: string | undefined, contactId?: string | undefined, partyDetails: Array}; export type MerchantChannelRequestDto = {walletId: string, payCurrency: string, displayCurrency: string, reference: string, customerId: string, complianceDetails: ComplianceDetailDto}; export type CreateApiApiV2ChannelArgs = {body: MerchantChannelRequestDto}; export type CreateApiApiV2ChannelOptions = {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 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 channel * * POST /api/v2/channel */ export const createApiApiV2Channel = async (args: CreateApiApiV2ChannelArgs, options?: CreateApiApiV2ChannelOptions) => { 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(/\/+$/, '')}/api/v2/channel${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(merchantChannelDto, await res.json()) };