import {z} from 'zod' export type GetApiV1MinAmountArgs = {currency_from?: string | undefined, currency_to?: string | undefined, fiat_equivalent?: string | undefined, is_fixed_rate?: string | undefined, is_fee_paid_by_user?: string | undefined, 'x-api-key'?: string | undefined}; export type GetApiV1MinAmountOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1MinAmountResponse = z.object({currency_from: z.string().optional(), currency_to: z.string().optional(), min_amount: z.number().optional(), fiat_equivalent: z.number().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 }; /** * Get the minimum payment amount * * GET /v1/min-amount */ export const getApiV1MinAmount = async (args: GetApiV1MinAmountArgs, options?: GetApiV1MinAmountOptions) => { const {baseUrl = "https://api.nowpayments.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency_from != null) query.append("currency_from", String(args.currency_from)) if (args.currency_to != null) query.append("currency_to", String(args.currency_to)) if (args.fiat_equivalent != null) query.append("fiat_equivalent", String(args.fiat_equivalent)) if (args.is_fixed_rate != null) query.append("is_fixed_rate", String(args.is_fixed_rate)) if (args.is_fee_paid_by_user != null) query.append("is_fee_paid_by_user", String(args.is_fee_paid_by_user)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/min-amount${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(getApiV1MinAmountResponse, await res.json()) };