import {z} from 'zod' export type GetApiV1ToolsArgs = {chains?: (Array) | undefined, 'x-lifi-api-key'?: string | undefined}; export type GetApiV1ToolsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangesEnum = z.string(); export const exchange = z.object({key: exchangesEnum.optional(), name: z.string().optional(), logoURI: z.string().optional(), supportedChains: z.string().optional()}); export const bridgesEnum = z.string(); export const supportedChains = z.object({fromChainId: z.string().optional(), toChainId: z.string().optional()}); export const bridge = z.object({key: bridgesEnum.optional(), name: z.string().optional(), logoURI: z.string().optional(), supportedChains: z.array(supportedChains).optional()}); export const tools = z.object({exchanges: z.array(exchange).optional(), bridges: z.array(bridge).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 available bridges and exchanges * * GET /v1/tools */ export const getApiV1Tools = async (args: GetApiV1ToolsArgs, options?: GetApiV1ToolsOptions) => { const {baseUrl = "https://li.quest", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.chains ?? []) query.append("chains", String(item)) if (args['x-lifi-api-key'] != null) headers.set("x-lifi-api-key", String(args['x-lifi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tools${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(tools, await res.json()) };