import {z} from 'zod' export type GetApiCosmosBaseTendermintV1beta1AbciQueryArgs = {data?: string | undefined, path?: string | undefined, height?: unknown | undefined, prove?: boolean | undefined}; export type GetApiCosmosBaseTendermintV1beta1AbciQueryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const cosmosBaseTendermintV1beta1ProofOp = z.object({type: z.string().optional(), key: z.string().optional(), data: z.string().optional()}); export const cosmosBaseTendermintV1beta1ProofOps = z.object({ops: z.array(cosmosBaseTendermintV1beta1ProofOp).optional()}); export const cosmosBaseTendermintV1beta1ABCIQueryResponse = z.object({code: z.number().int().optional(), log: z.string().optional(), info: z.string().optional(), index: z.string().optional(), key: z.string().optional(), value: z.string().optional(), proofOps: cosmosBaseTendermintV1beta1ProofOps.optional(), height: z.string().optional(), codespace: z.string().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 }; /** * ABCIQuery defines a query handler that supports ABCI queries directly to the * application, bypassing Tendermint completely. The ABCI query must contain * a valid and supported path, including app, custom, p2p, and store. * * GET /cosmos/base/tendermint/v1beta1/abci_query */ export const getApiCosmosBaseTendermintV1beta1AbciQuery = async (args: GetApiCosmosBaseTendermintV1beta1AbciQueryArgs, options: GetApiCosmosBaseTendermintV1beta1AbciQueryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.data != null) query.append("data", String(args.data)) if (args.path != null) query.append("path", String(args.path)) if (args.height != null) query.append("height", String(args.height)) if (args.prove != null) query.append("prove", String(args.prove)) const url = `${baseUrl.replace(/\/+$/, '')}/cosmos/base/tendermint/v1beta1/abci_query${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(cosmosBaseTendermintV1beta1ABCIQueryResponse, await res.json()) };