@happyvertical/smrt-mobile-contract

中文文档
0.62.1发布于 2026/11/17MITTypeScriptES Module零依赖近 30 天下载 2.7万

SMRT mobile contract codegen: manifest + allowlist → mobile-contract.json → Kotlin/Swift DTOs, plus the generated framework contract for @happyvertical/smrt-mobile

原始使用文档
文档目录6 节
  1. Install
  2. Usage — per-app domain DTOs
  3. Allowlist rules
  4. Type mapping
  5. Usage — the framework contract
  6. Development

@happyvertical/smrt-mobile-contract

Codegen for the s-m-r-t mobile foundation: projects a s-m-r-t manifest.json through a mobile allowlist into typed Kotlin (@Serializable DTOs for the KMP module) and Swift (plain structs) — plus the generated framework contract files checked into @happyvertical/smrt-mobile.

Design: ADR 0001; epic happyvertical/smrt#1745. Agent/developer guidance: AGENTS.md. Zero runtime dependencies by design — it reads manifest.json files directly and stays out of the smrt package DAG.

Install

pnpm add -D @happyvertical/smrt-mobile-contract

Usage — per-app domain DTOs

import {
  buildMobileContract,
  generateKotlinDtoFiles,
  generateSwiftDtoFile,
  writeFileSet,
} from '@happyvertical/smrt-mobile-contract';
import { readFileSync, writeFileSync } from 'node:fs';

const manifest = JSON.parse(
  readFileSync('packages/my-objects/src/manifest/manifest.json', 'utf8'),
);

const contract = buildMobileContract({
  manifests: [manifest],                     // one or more SMRT manifests
  allowlist: ['FieldReport', 'Attachment'],  // short or qualified names
  kotlinPackage: 'com.example.mobile.generated',
  sourceLabel: 'packages/my-objects/src/manifest/manifest.json',
});

writeFileSet(
  'apps/mobile/shared/src/commonMain/kotlin/generated',
  generateKotlinDtoFiles(contract),
);
writeFileSync(
  'apps/mobile/iosApp/App/GeneratedMobileDtos.swift',
  generateSwiftDtoFile(contract),
);

Allowlist rules

  • Entries may be short names (FieldReport) or qualified names (@scope/pkg:FieldReport). A short name that matches objects in more than one manifest is an error — use the qualified name.
  • Two allowlisted objects may not share a short name: generated <Name>Dto files would overwrite each other, so the build throws instead.
  • Relationship declarations (oneToMany/manyToMany/hasMany) are excluded from DTO projection — they are not scalar columns.

Type mapping

Manifest typeKotlinSwift
text / status / foreignKey / crossPackageRefStringString
integerLong (64-bit — byte sizes/epochs overflow Int)Int64
decimalDecimalString? (string-encoded, no float precision loss)String?
booleanBooleanBool
datetimeInstant?String?
jsonJsonObjectString (raw JSON)

Field defaults come from default (current manifests) with defaultValue (amaru-era) as a fallback; every generated Kotlin field default-initializes for safe partial deserialization. Field names stay manifest-faithful (wire-format names, including created_at); keyword collisions are backtick-escaped per language. sourceHash (sha256 over allowlist + projected objects) is stamped into all output so drift is detectable.

Usage — the framework contract

The stable framework contract (support types, pack localization, and the /api/mobile auth/session/device DTOs) is emitted by this package and checked into smrt-mobile's contract/ directory. This package's build byte-verifies those files; regenerate after editing src/emit-framework.ts:

pnpm --filter @happyvertical/smrt-mobile-contract generate:framework

The Swift mirror (frameworkSwiftFiles()) is emitted on demand for SwiftUI apps; a parity test keeps it field-for-field in sync with the Kotlin side.

Development

pnpm build   # vite build + framework contract freshness verify
pnpm test    # vitest (goldens, mapping rules, parity, file-set round-trip)

版本与发布记录

@happyvertical/smrt-mobile-contract 共发布 326 个版本,首次发布于 2026/07/03;过去 12 个月发布了 326 个版本。

版本发布日期距今
0.62.1latest2026/11/17今天
0.62.02026/11/17今天
0.61.62026/11/161 天前
0.61.52026/11/161 天前
0.61.42026/11/161 天前
0.61.32026/11/152 天前

关于 @happyvertical/smrt-mobile-contract 的常见问题

@happyvertical/smrt-mobile-contract 是什么?

SMRT mobile contract codegen: manifest + allowlist → mobile-contract.json → Kotlin/Swift DTOs, plus the generated framework contract for @happyvertical/smrt-mobile

如何安装 @happyvertical/smrt-mobile-contract?

在项目目录运行 npm install @happyvertical/smrt-mobile-contract,或使用 pnpm add / yarn add / bun add。

@happyvertical/smrt-mobile-contract 支持 TypeScript 吗?

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

@happyvertical/smrt-mobile-contract 支持 ESM 还是 CommonJS?

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

@happyvertical/smrt-mobile-contract 可以商用吗?

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

@happyvertical/smrt-mobile-contract 的下载量有多少?

根据 npm 官方下载统计,@happyvertical/smrt-mobile-contract 在 2026/10/17 – 2026/11/15 这 30 天内被下载 26,888 次,日均约 896 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@happyvertical/smrt-mobile-contract 还在维护吗?

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