import {z} from 'zod' export type GetApiV1ConnectionsArgs = {fromChain: string, toChain: string, fromToken?: string | undefined, toToken?: string | undefined, chainTypes?: string | undefined, allowBridges?: Array | undefined, denyBridges?: Array | undefined, preferBridges?: Array | undefined, allowExchanges?: Array | undefined, denyExchanges?: Array | undefined, preferExchanges?: Array | undefined, allowSwitchChain?: boolean | undefined, allowDestinationCall?: boolean | undefined, 'x-lifi-api-key'?: string | undefined}; export type GetApiV1ConnectionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const baseToken = z.object({address: z.string(), chainId: z.number()}); export const connection = z.object({fromChainId: z.number(), toChainId: z.number(), fromTokens: z.array(baseToken), toTokens: z.array(baseToken)}); export const connectionsResponse = z.object({connections: z.array(connection).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 }; /** * Returns all possible connections between two chains. * * GET /v1/connections */ export const getApiV1Connections = async (args: GetApiV1ConnectionsArgs, options?: GetApiV1ConnectionsOptions) => { const {baseUrl = "https://li.quest", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromChain != null) query.append("fromChain", String(args.fromChain)) if (args.toChain != null) query.append("toChain", String(args.toChain)) if (args.fromToken != null) query.append("fromToken", String(args.fromToken)) if (args.toToken != null) query.append("toToken", String(args.toToken)) if (args.chainTypes != null) query.append("chainTypes", String(args.chainTypes)) for (const item of args.allowBridges ?? []) query.append("allowBridges", String(item)) for (const item of args.denyBridges ?? []) query.append("denyBridges", String(item)) for (const item of args.preferBridges ?? []) query.append("preferBridges", String(item)) for (const item of args.allowExchanges ?? []) query.append("allowExchanges", String(item)) for (const item of args.denyExchanges ?? []) query.append("denyExchanges", String(item)) for (const item of args.preferExchanges ?? []) query.append("preferExchanges", String(item)) if (args.allowSwitchChain != null) query.append("allowSwitchChain", String(args.allowSwitchChain)) if (args.allowDestinationCall != null) query.append("allowDestinationCall", String(args.allowDestinationCall)) if (args['x-lifi-api-key'] != null) headers.set("x-lifi-api-key", String(args['x-lifi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/connections${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(connectionsResponse, await res.json()) };