@edgerules/web

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

浏览器的 Edge 规则决定服务 .

中文使用文档
文档目录10 节
  1. 安装
  2. 生产用途
  3. 从边框规则 DSL 字符串装入
  4. 从便携式JSON型号装入
  5. 处理错误
  6. 可变/ 开发用法
  7. API 参考
  8. DecisionService
  9. MutableDecisionService (德文建设)
  10. 相关软件包

@ dedgerules/ web( 网络规则)

Quality Gate Status Bugs Code Smells Coverage Duplicated Lines

边缘规则 浏览器二号舰:

导出说明
@edgerules/web生产建设——只执行(DecisionService(中文(简体) )
@edgerules/web/mutable发展建设——执行+CRUDMutableDecisionService(中文(简体) )

浏览器软件包是 API- 相同到 @edgerules/node;仅限 基础 WASM 加载器不同( 浏览器) fetch 对节点.js fs) (中文(简体) ). 选择此软件包用于 Vite 、 Web package 和其他 浏览器捆绑器设置 .


安装

npm install @edgerules/web @edgerules/portable

仅限无害环境管理。 WASM 二进制在第一次使用时同步加载.


生产用途

从边框规则 DSL 字符串装入

import {DecisionService} from '@edgerules/web';

const service = new DecisionService(`{
    applicant: {
        age:    <number, required: true>
        income: <number, default: 0>
    }
    isEligible: applicant.age >= 18 and applicant.income >= 1000
}`);

// Execute the whole model against a request
const result = service.execute('*', {applicant: {age: 25, income: 2500}});
// { applicant: { age: 25, income: 2500 }, isEligible: true }

从便携式JSON型号装入

import {DecisionService} from '@edgerules/web';
import type {PortableContext} from '@edgerules/portable';

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

const service = new DecisionService(model);

const result = service.execute('isEligible', {applicant: {age: 30, income: 3000}});
// true

处理错误

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

try {
    service.execute('nonExistentField');
} catch (err) {
    const error = err as PortableError;
    console.error(error.type);    // "EntryNotFound"
    console.error(error.message); // human-readable, safe to surface in UI
}

可变/ 开发用法

使用 ./mutable 用于规则编辑器、视觉构建器和测试工具的子路径. 不要在其中使用此构建 生产.

import {Kinds, MutableDecisionService} from '@edgerules/web/mutable';

const service = new MutableDecisionService(initialModel);

// Inspect a field with inferred type information
const node = service.get('applicant.age');
// { "@kind": "placeholder", "type": "number", "required": true, "writeOnly": true, "linked": true }

// Hot-update a rule
service.set('minAge', 21);

// Add a new typed input
service.set('applicant.creditScore', {'@kind': 'placeholder', type: 'number', default: 0});

// Remove a field
service.remove('applicant.creditScore');

// Read the full model snapshot
const snapshot = service.get('*');

// Read the method signatures only
const signatures = service.get('*', Kinds.METHOD_INTERFACES);

// Read/write a node as DSL source text instead of Portable JSON
const code = service.getCode('minAge'); // '21'
service.setCode('minAge', '18'); // returns a PortableNode, not code text

API 参考

DecisionService

方法说明
new DecisionService(model)从 DSL 字符串或根创建 PortableContext
execute(method, args?)评价 method (字段路径、功能或 "*")反对选择性要求的约束

execute 细节

  • method: "*" ——评价每个顶级场并返回完整的模型结果.
  • method: "person.isAdult" 评估并返回这个单一领域.
  • method: "validateApplicant" 调用一个命名函数;通过 args。 。 。 .
  • args ——一个模型形状的对象,其条目被绑定为外部输入. 嵌入式打字孔遵循模型形状 (例如:. { applicant: { age: 30 } } 页:1 applicant: { age: <number> }) (中文(简体) ).

MutableDecisionService (德文建设)

继承 execute 并添加:

方法说明
get(path, kinds?)读取节点, 通过过滤 @kind (见 Kinds(三); linked 告诉模型是否链接
set(path, value)写入表达式、占位符、上下文、方法或类型
getCode(path)将节点读作 Edge Rules DSL 源文本
setCode(path, code)从 Edge Rules DSL 源文本中写入节点; 返回 a PortableNode代码文本
remove(path)删除给定路径上的条目
rename(oldPath, newPath)在上下文中重命名一个条目

版本与发布记录

@edgerules/web 共发布 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/web 的常见问题

@edgerules/web 是什么?

浏览器的 Edge 规则决定服务 .

如何安装 @edgerules/web?

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

@edgerules/web 支持 TypeScript 吗?

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

@edgerules/web 支持 ESM 还是 CommonJS?

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

@edgerules/web 可以商用吗?

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

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

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

@edgerules/web 还在维护吗?

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