import {z} from 'zod' export type OffersSvcOfferType = 'ONRAMP' | 'OFFRAMP'; export type GetApiOffersLimitsArgs = {tokenSymbol?: string | undefined, fiatSymbol?: string | undefined, offerType?: OffersSvcOfferType | undefined, network?: string | undefined}; export type GetApiOffersLimitsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const offersSvcOfferType = z.enum(["ONRAMP", "OFFRAMP"]); export const offerLimitsDto = z.object({fiatSymbol: z.string(), tokenSymbol: z.string(), network: z.string().optional(), offerType: offersSvcOfferType.optional(), minTokens: z.number(), maxTokens: z.number(), minFiat: z.number().optional(), maxFiat: 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 }; /** * Find matching offer for trading pair * * GET /offers/limits */ export const getApiOffersLimits = async (args: GetApiOffersLimitsArgs, options?: GetApiOffersLimitsOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tokenSymbol != null) query.append("tokenSymbol", String(args.tokenSymbol)) if (args.fiatSymbol != null) query.append("fiatSymbol", String(args.fiatSymbol)) if (args.offerType != null) query.append("offerType", String(args.offerType)) if (args.network != null) query.append("network", String(args.network)) const url = `${baseUrl.replace(/\/+$/, '')}/offers/limits${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(offerLimitsDto, await res.json()) };