import {z} from 'zod' export type GetApiCosmosBaseTendermintV1beta1NodeInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const tendermintP2pProtocolVersion = z.object({p2p: z.string().optional(), block: z.string().optional(), app: z.string().optional()}); export const tendermintP2pDefaultNodeInfoOther = z.object({txIndex: z.string().optional(), rpcAddress: z.string().optional()}); export const tendermintP2pDefaultNodeInfo = z.object({protocolVersion: tendermintP2pProtocolVersion.optional(), defaultNodeId: z.string().optional(), listenAddr: z.string().optional(), network: z.string().optional(), version: z.string().optional(), channels: z.string().optional(), moniker: z.string().optional(), other: tendermintP2pDefaultNodeInfoOther.optional()}); export const cosmosBaseTendermintV1beta1Module = z.object({path: z.string().optional(), version: z.string().optional(), sum: z.string().optional()}); export const cosmosBaseTendermintV1beta1VersionInfo = z.object({name: z.string().optional(), appName: z.string().optional(), version: z.string().optional(), gitCommit: z.string().optional(), buildTags: z.string().optional(), goVersion: z.string().optional(), buildDeps: z.array(cosmosBaseTendermintV1beta1Module).optional(), cosmosSdkVersion: z.string().optional()}); export const cosmosBaseTendermintV1beta1GetNodeInfoResponse = z.object({defaultNodeInfo: tendermintP2pDefaultNodeInfo.optional(), applicationVersion: cosmosBaseTendermintV1beta1VersionInfo.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 }; /** * GetNodeInfo queries the current node info. * * GET /cosmos/base/tendermint/v1beta1/node_info */ export const getApiCosmosBaseTendermintV1beta1NodeInfo = async (options: GetApiCosmosBaseTendermintV1beta1NodeInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/cosmos/base/tendermint/v1beta1/node_info${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(cosmosBaseTendermintV1beta1GetNodeInfoResponse, await res.json()) };