import {z} from 'zod' export type GetApiExchangeApiInfoOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const legalEntityType = z.enum(["LIMITED_LIABILITY_COMPANY", "PUBLIC_COMPANY", "SOLE_PROPRIETORSHIP", "PARTNERSHIP", "CORPORATION", "TRUST", "PRIVATE_FOUNDATION", "CHARITY", "NON_PROFIT_ORGANIZATION", "PUBLIC_AGENCY"]); export const businessIndustry = z.enum(["TECHNOLOGY_SOFTWARE", "FINANCIAL_SERVICES", "ECOMMERCE", "MARKETING_ADVERTISING", "CONSULTING_PROFESSIONAL_SERVICES", "EDUCATION", "HEALTHCARE", "MANUFACTURING", "LOGISTICS_SUPPLY_CHAIN", "REAL_ESTATE", "CRYPTO_WEB3", "GAMING", "MEDIA_ENTERTAINMENT", "OTHER"]); export const businessCategory = z.enum(["GENERAL_BUSINESS", "OTC_FX_LIQUIDITY_PROVIDER", "LICENSED_FINANCIAL_SERVICES"]); export const ultimateBeneficialOwner = z.object({nationality: z.string().optional(), idType: z.string().optional(), idNumber: z.string().optional(), bvn: z.string().optional(), fullName: z.string(), email: z.string(), ownershipPercentage: z.number()}); export const businessDirector = z.object({nationality: z.string().optional(), idType: z.string().optional(), idNumber: z.string().optional(), bvn: z.string().optional(), fullName: z.string(), email: z.string()}); export const businessLicence = z.object({type: z.string(), authority: z.string(), url: z.string().optional(), primary: z.boolean().optional()}); export const businessAddressParts = z.object({line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), postcode: z.string().optional(), country: z.string().optional()}); export const businessDocument = z.object({label: z.string(), uploadId: z.string(), url: z.string().optional()}); export const cexProfileDto = z.object({businessName: z.string(), businessPhoneNumber: z.string().optional(), businessFormationDate: z.string(), businessLogoUrl: z.string().optional(), businessRegCountry: z.string().min(2).max(2), businessLocation: z.string(), productName: z.string().optional(), productWebsiteUrl: z.string().optional(), productDescription: z.string().optional(), operatingAddress: z.string().optional(), taxCountry: z.string().min(2).max(2), taxIdentificationNumber: z.string(), legalEntityType: legalEntityType, businessIndustry: businessIndustry, businessCategory: businessCategory, monthlyTransactionVolume: z.string(), transactsOnBehalfOfThirdParties: z.boolean(), operatesInRestrictedRegions: z.boolean(), restrictedRegions: z.string().optional(), ultimateBeneficialOwners: z.array(ultimateBeneficialOwner).optional(), directors: z.array(businessDirector).optional(), tradingName: z.string().optional(), onboardUseCases: z.array(z.string()).optional(), holdsLicence: z.boolean().optional(), licences: z.array(businessLicence).optional(), flowOfFundsExplanation: z.string().optional(), registeredAddressParts: businessAddressParts.optional(), authorisedSignatories: z.array(z.string()).optional(), documents: z.array(businessDocument).optional(), businessId: z.string(), businessEmail: z.string(), businessRegNo: z.string(), businessType: z.string().optional(), enabledProducts: z.array(z.string()).optional(), declarationAcceptedAt: z.string().optional(), isDisabled: z.boolean().optional(), disabledAt: z.string().optional(), disabledBy: z.string().optional(), disabledReason: z.string().optional(), reenabledAt: z.string().optional(), reenabledBy: z.string().optional(), createdAt: z.string(), updatedAt: z.string()}); 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 CEX Profile * * GET /exchange/api/info */ export const getApiExchangeApiInfo = async (options?: GetApiExchangeApiInfoOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/exchange/api/info${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(cexProfileDto, await res.json()) };