@edgerules/portable

中文文档
0.2.8-alpha.202611121551发布于 2026/11/12TypeScriptES Module零依赖近 30 天下载 2906

Portable JSON type definitions for EdgeRules.

原始使用文档
文档目录11 节
  1. Installation
  2. What is Portable JSON?
  3. Usage
  4. Typed model construction
  5. Defining a method
  6. Defining a type
  7. Calling a ruleset (invocation)
  8. Reading with a kind filter
  9. Handling errors
  10. Key Types
  11. Related packages

@edgerules/portable

Quality Gate Status Bugs Code Smells Coverage Duplicated Lines

TypeScript type definitions for the EdgeRules Portable JSON format — the canonical serialization schema for EdgeRules models, functions, types, and errors.

This package holds the TypeScript types and one runtime constant, Kinds: the common @kind groups of service.get(path, kinds?). It is safe to use in both browser and Node.js projects.


Installation

npm install @edgerules/portable

What is Portable JSON?

Every EdgeRules model is a plain JSON object. The Portable format is how you store a model in a database, send it over HTTP, and pass it to DecisionService — without any special serialization library.

  • Every object node has an @kind. Only @kind, @node, and @name have the @ prefix.
  • A model root is a PortableContext: { '@kind': 'context', fields: { ... } }.
  • A body declaration has a compact interface node (*-interface) without the body.
@kindTypeScript typeDescription
context, workbookPortableContextRecord of fields; a workbook is root-only
placeholderPortablePlaceholderValueTyped input slot — the host supplies a value
expression, expression-interfacePortableExpression, PortableExpressionInterfaceComputed field with its variables
invocationPortableInvocationCall to a user method within the model
typePortableTypeUser-defined type: fields or alias
factsPortableFactsDerived fields of a structure type
method, method-interfacePortableMethod, PortableMethodInterfacefunc: parameters, return, body
ruleset, ruleset-interfacePortableRuleset, PortableRulesetInterfaceDecision table: hitPolicy, rules, default
loop, loop-interfacePortableLoop, PortableLoopInterfaceBounded state machine: state, do, output
optimise, optimise-interfacePortableOptimise, PortableOptimiseInterfaceLP/MILP: variables, objective, constraints
external-methodPortableExternalMethodexternal func: parameters, required return
decision, decision-interfacePortableDecision, PortableDecisionInterfacedecision: context, body, linked requires
service, service-interfacePortableService, PortableServiceInterfaceservice: parameters, body, linked requires
errorPortableErrorStructured error crossing the host boundary

Usage

Typed model construction

import type { PortableContext, PortablePlaceholderValue } from '@edgerules/portable';

const ageInput: PortablePlaceholderValue = {
  '@kind': 'placeholder',
  type: 'number',
  required: true,
};

const model: PortableContext = {
  '@kind': 'context',
  fields: {
    minAge: 18,
    applicant: {
      '@kind': 'context',
      fields: {
        age: ageInput,
        income: { '@kind': 'placeholder', type: 'number', default: 0 },
      },
    },
    isEligible: 'applicant.age >= minAge and applicant.income >= 1000',
  },
};

Defining a method

import type { PortableMethod } from '@edgerules/portable';

const isEligible: PortableMethod = {
  '@kind': 'method',
  parameters: { age: 'number', income: 'number' },
  body: {
    '@kind': 'context',
    fields: { result: 'age >= 18 and income >= 1000' },
  },
};

Defining a type

import type { PortableType } from '@edgerules/portable';

const CustomerType: PortableType = {
  '@kind': 'type',
  fields: {
    name: { '@kind': 'placeholder', type: 'string', required: true },
    age: 'number',
    tier: { '@kind': 'placeholder', type: 'string', default: 'STANDARD', enum: ['GOLD', 'SILVER', 'STANDARD'] },
  },
};

Calling a ruleset (invocation)

import type { PortableInvocation } from '@edgerules/portable';

const call: PortableInvocation = {
  '@kind': 'invocation',
  method: 'risk',
  arguments: { age: 'applicant.age', income: 'applicant.income' },
};

Reading with a kind filter

import { Kinds } from '@edgerules/portable';

service.get('*', Kinds.METHODS); // methods, rulesets, loops, optimise declarations
service.get('*', Kinds.EXTERNALS); // the resolver-wiring catalog
service.get('*', [...Kinds.EXPRESSION_INTERFACES, ...Kinds.TYPES, ...Kinds.INVOCABLE_INTERFACES]); // project explorer

Handling errors

import type { PortableError, PortableErrorType } from '@edgerules/portable';

function handleError(err: unknown): void {
  const error = err as PortableError;
  switch (error.type as PortableErrorType) {
    case 'EntryNotFound':
      console.error('Path not found:', error.path);
      break;
    case 'WrongFilter':
      console.error('Unknown @kind in kinds:', error.message);
      break;
    default:
      console.error(error.message);
  }
}

Key Types

// The root model object, a nested record, or a root workbook
PortableContext

// Any node returned by `service.get(path, kinds?)`
PortableNode

// Any value `service.set(path, value)` accepts
PortableSetValue

// A filterable @kind, and the common groups of them
Kind
Kinds

// The base of each method-like interface: { parameters, return? }
MethodDefinition

// A structured error from the engine, and its discriminant
PortableError
PortableErrorType  // "EntryNotFound" | "WrongFieldPath" | "DuplicateName" | "Parse" | "Execution" | "WrongFilter"

版本与发布记录

@edgerules/portable 共发布 35 个版本,首次发布于 2026/07/04;过去 12 个月发布了 35 个版本。

版本发布日期距今
0.2.8-alpha.202611121551latest2026/11/12今天
0.2.7-alpha.2026110819152026/11/094 天前
0.2.6-alpha.2026110818092026/11/094 天前
0.2.6-alpha.2026110818082026/11/094 天前
0.2.5-alpha.2026110720262026/11/085 天前
0.2.4-alpha.2026110716572026/11/085 天前

关于 @edgerules/portable 的常见问题

@edgerules/portable 是什么?

Portable JSON type definitions for EdgeRules.

如何安装 @edgerules/portable?

在项目目录运行 npm install @edgerules/portable,或使用 pnpm add / yarn add / bun add。

@edgerules/portable 支持 TypeScript 吗?

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

@edgerules/portable 支持 ESM 还是 CommonJS?

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

@edgerules/portable 可以商用吗?

@edgerules/portable 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@edgerules/portable 的下载量有多少?

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

@edgerules/portable 还在维护吗?

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