import {z} from 'zod' export type PartnerFee = {factor: number | string}; export type OrderPurposeCategory = string; export type OrderPurpose = {category?: OrderPurposeCategory | undefined, comment?: string | undefined}; export type CurrencyCode = string; export type OrderInputType = 'bank_account' | 'bity_account' | 'crypto_address' | 'online_instant_payment'; export type InputAmountEstimateRequestInput = {currency: CurrencyCode, type?: OrderInputType | undefined}; export type OrderOutputType = 'bank_account' | 'bity_account' | 'crypto_address'; export type OutputAmount = string; export type InputAmountEstimateRequestOutput = {currency: CurrencyCode, type?: OrderOutputType | undefined, amount: OutputAmount}; export type InputAmountEstimateRequest = {fix_invalid_amount?: boolean | undefined, partner_fee?: PartnerFee | undefined, purpose?: OrderPurpose | undefined, input: InputAmountEstimateRequestInput, output: InputAmountEstimateRequestOutput}; export type InputAmount = string; export type OutputAmountEstimateRequestInput = {currency: CurrencyCode, type?: OrderInputType | undefined, amount: InputAmount}; export type OutputAmountEstimateRequestOutput = {currency: CurrencyCode, type?: OrderOutputType | undefined}; export type OutputAmountEstimateRequest = {fix_invalid_amount?: boolean | undefined, partner_fee?: PartnerFee | undefined, purpose?: OrderPurpose | undefined, input: OutputAmountEstimateRequestInput, output: OutputAmountEstimateRequestOutput}; export type AmountEstimateRequest = InputAmountEstimateRequest | OutputAmountEstimateRequest; export type CreateApiOrdersEstimateMultipleArgs = {body: Array}; export type CreateApiOrdersEstimateMultipleOptions = {baseUrl?: string | undefined, bityAccountOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const inputAmount = z.string(); export const currencyCode = z.string(); export const amount = z.string(); export const orderInputType = z.enum(["bank_account", "bity_account", "crypto_address", "online_instant_payment"]); export const amountEstimateInput = z.object({amount: inputAmount, currency: currencyCode, minimum_amount: amount.optional(), object_information_optional: z.boolean().optional(), type: orderInputType}); export const orderingRequirementAccount = z.object({satisfied: z.boolean().optional()}); export const orderingRequirementAccountQuota = z.object({satisfied: z.boolean().optional()}); export const orderingRequirementKyc = z.object({satisfied: z.boolean().optional()}); export const orderingRequirements = z.object({account: orderingRequirementAccount.optional(), account_quota: orderingRequirementAccountQuota.optional(), kyc: orderingRequirementKyc.optional()}); export const outputAmount = z.string(); export const orderOutputType = z.enum(["bank_account", "bity_account", "crypto_address"]); export const amountEstimateOutput = z.object({amount: outputAmount, currency: currencyCode, minimum_amount: amount.optional(), type: orderOutputType}); export const priceBreakdown = z.object({customer_trading_fee: z.object({amount: amount, currency: currencyCode}).optional(), instant_payment_fee: z.object({amount: amount, currency: currencyCode}).optional(), "non-verified_fee": z.object({amount: amount, currency: currencyCode}).optional(), output_transaction_cost: z.object({amount: amount, currency: currencyCode}).optional(), partner_fee: z.object({amount: amount, currency: currencyCode}).optional()}); export const amountEstimate = z.object({input: amountEstimateInput, ordering_requirements: orderingRequirements.optional(), output: amountEstimateOutput, price_breakdown: priceBreakdown}); export const createApiOrdersEstimateMultipleResponse = z.array(amountEstimate); 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 }; /** * Perform several estimations in one request * * POST /orders/estimate/multiple * * Also accepts auth: bity_account_client_oauth, client_oauth */ export const createApiOrdersEstimateMultiple = async (args: CreateApiOrdersEstimateMultipleArgs, options?: CreateApiOrdersEstimateMultipleOptions) => { const {baseUrl = "https://exchange.api.bity.com/v2", fetch: fetchFn = fetch, bityAccountOauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bityAccountOauth !== undefined) headers.set('authorization', `Bearer ${bityAccountOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/estimate/multiple${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(createApiOrdersEstimateMultipleResponse, await res.json()) };