@calimero-network/abi-codegen

中文文档
2.5.0发布于 2026/10/29MITTypeScriptES Module构建来源已验证近 30 天下载 3906

WASM-ABI v1 parser and code generator for Calimero

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. CLI
  4. Programmatic
  5. Generated Files
  6. Swift and Kotlin clients
  7. Examples
  8. Development

@calimero-network/abi-codegen

A WASM-ABI v1 parser and code generator for Calimero applications.

Installation

npm install @calimero-network/abi-codegen

Usage

CLI

# Basic usage
npx calimero-abi-codegen -i abi.json -o src

# With custom client name
npx calimero-abi-codegen -i abi.json -o src --client-name MyClient

# Derive the client name from a file path (e.g. the .wasm binary)
npx calimero-abi-codegen -i abi.json -o src --name-from kv_store.wasm

# Point generated imports at a different runtime package
npx calimero-abi-codegen -i abi.json -o src --import-path @calimero-network/mero-js

# Validate ABI manifest only (no code generation)
npx calimero-abi-codegen --validate -i abi.json

Flags: -i, --input <file> (default abi.json), -o, --output <dir> (default src), --client-name <Name>, --name-from <path>, --import-path <path> (default @calimero-network/mero-react), --target <ts|swift|kotlin> (default ts), --package <name> (Kotlin only), --validate, -h, --help. Full details in the CLI reference.

Programmatic

import { loadAbiManifestFromFile } from '@calimero-network/abi-codegen/parse';
import { generateClient } from '@calimero-network/abi-codegen/generate/client';
import fs from 'node:fs';

const manifest = loadAbiManifestFromFile('./abi.json');
const clientContent = generateClient(manifest, 'MyClient');
fs.writeFileSync('src/generated/MyClient.ts', clientContent);

generateClient is self-contained: its output includes the type definitions and the client class together. See the programmatic API reference for the full surface (parseAbiManifest and the naming utilities in generate/emit).

Generated Files

The CLI writes a single file, {ClientName}.ts (or .swift / .kt for the native targets), into the output directory. Type definitions and the client class live together in that one file — there is no separate types.ts module.

Swift and Kotlin clients

--target swift and --target kotlin generate a native client for mobile apps instead of the TypeScript one. Each is one self-contained file with no SDK dependency: the types, the client, and a ContextContractCalling transport the app implements over its own node or relay connection.

# Writes Sources/Generated/ForumClient.swift
npx calimero-abi-codegen -i logic/res/abi.json -o Sources/Generated \
  --target swift --client-name ForumClient

# Writes app/src/main/kotlin/network/calimero/forum/ForumClient.kt
npx calimero-abi-codegen -i logic/res/abi.json \
  -o app/src/main/kotlin/network/calimero/forum \
  --target kotlin --package network.calimero.forum --client-name ForumClient

--target is ts (default, unchanged), swift or kotlin; --package sets the Kotlin file's package (no package line without it). --import-path applies to ts only.

What is generated:

  • Swift: Codable, Sendable, Equatable structs and enums for every ABI type, and a struct {ClientName} with one async throws function per method.
  • Kotlin: kotlinx.serialization @Serializable data classes, enum classes and sealed classes, and a class {ClientName} with one suspend function per method. Properties are camelCase with @SerialName carrying the wire name. Needs the org.jetbrains.kotlin.plugin.serialization plugin and kotlinx-serialization-json.

The transport is one call per route:

public protocol ContextContractCalling: Sendable {
    func query(method: String, argsJSON: Data) async throws -> Data
    func execute(method: String, argsJSON: Data) async throws -> Data
}
interface ContextContractCalling {
    suspend fun query(method: String, argsJson: String): String
    suspend fun execute(method: String, argsJson: String): String
}

A method whose ABI intent is read_only is a view and goes to query; every other method (mutating, unspecified, or no intent) goes to execute, as the node treats it. Each function's doc comment says which.

The wire shape is the TypeScript client's: integers are JSON numbers, bytes are arrays of numbers ([UInt8] / List<UByte>), tuples are arrays (ContractTuple2<A, B>, ...), and enums follow serde's tagging (bare name, { "Variant": payload }, internally/adjacently tagged, or untagged). A map key that is not a string is typed String, since JSON keys are. One difference: an optional argument left nil/null is omitted from the args object rather than sent as null; the contract reads a missing Option as None.

Generate one client per Swift module or Kotlin package: the transport and helper types are declared in every generated file.

With the native SDKs, pass --transport-import to use their ContextContractCalling instead, so the SDK's bound contract is the transport with no adapter: --transport-import MeroKit (Swift) or --transport-import com.calimero.mero.app.ContextContractCalling (Kotlin).

let forum = ForumClient(transport: contract.bound(to: contextId))

Examples

See the codegen-example for a complete React example.

Development

npm install
npm run build
npm run test

版本与发布记录

@calimero-network/abi-codegen 共发布 20 个版本,首次发布于 2025/10/03;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.5.0latest2026/10/2911 天前
2.4.02026/10/021 个月前
2.3.02026/09/281 个月前
2.2.02026/09/261 个月前
2.1.02026/09/241 个月前
2.0.12026/09/101 个月前

关于 @calimero-network/abi-codegen 的常见问题

@calimero-network/abi-codegen 是什么?

WASM-ABI v1 parser and code generator for Calimero

如何安装 @calimero-network/abi-codegen?

在项目目录运行 npm install @calimero-network/abi-codegen,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 calimero-abi-codegen,可以用 npx calimero-abi-codegen 直接运行。

@calimero-network/abi-codegen 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@calimero-network/abi-codegen 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@calimero-network/abi-codegen 可以商用吗?

@calimero-network/abi-codegen 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@calimero-network/abi-codegen 的下载量有多少?

根据 npm 官方下载统计,@calimero-network/abi-codegen 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,906 次,日均约 130 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@calimero-network/abi-codegen 还在维护吗?

最新版本 2.5.0 发布于 2026/10/29(11 天前);至今共发布 20 个版本,其中过去 12 个月发布了 18 个;首次发布于 2025/10/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。