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 UpdateEstimateRequestDto = {walletRequiredAmount: number, paidRequiredAmount?: number | undefined, reference: string, complianceDetails?: ComplianceDetailDto | undefined} | {walletRequiredAmount?: number | undefined, paidRequiredAmount: number, reference: string, complianceDetails?: ComplianceDetailDto | undefined}; export type UpdateApiApiV1PayEstimateExternalIdUpdateArgs = {externalId: string, body: UpdateEstimateRequestDto}; export type UpdateApiApiV1PayEstimateExternalIdUpdateOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const estimatePayoutResponseDto = z.object({externalId: z.string().optional(), accountId: z.string().optional(), walletId: z.string().max(255).optional(), customerReference: z.string().optional(), walletCurrency: z.string().optional(), walletRequiredAmount: z.number().optional(), paidCurrency: z.string().optional(), paidRequiredAmount: z.number().optional(), feeCurrency: z.string().optional(), feePredictedAmount: z.number().optional(), networkFeeCurrency: z.string().optional(), networkFeePredictedAmount: z.number().optional(), totalWalletAmount: z.number().optional(), exchangeRate: z.number().optional(), customerId: z.string().optional(), paymentExternalId: z.string().optional(), network: z.string().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 }; /** * Update an existing estimate payout * * PUT /api/v1/pay/estimate/{externalId}/update */ export const updateApiApiV1PayEstimateExternalIdUpdate = async (args: UpdateApiApiV1PayEstimateExternalIdUpdateArgs, options?: UpdateApiApiV1PayEstimateExternalIdUpdateOptions) => { 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/v1/pay/estimate/${encodeURIComponent(String(args.externalId))}/update${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(estimatePayoutResponseDto, await res.json()) };