@edgerules/web

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

EdgeRules decision service for the browser.

原始使用文档
文档目录10 节
  1. Installation
  2. Production Usage
  3. Load from an EdgeRules DSL string
  4. Load from a Portable JSON model
  5. Error handling
  6. Mutable / Development Usage
  7. API Reference
  8. DecisionService
  9. MutableDecisionService (dev build)
  10. Related packages

@edgerules/web

Quality Gate Status Bugs Code Smells Coverage Duplicated Lines

EdgeRules decision service for the browser. Ships two builds:

ExportDescription
@edgerules/webProduction build — execution only (DecisionService)
@edgerules/web/mutableDevelopment build — execution + CRUD (MutableDecisionService)

The browser package is API-identical to @edgerules/node; only the underlying WASM loader differs (browser fetch vs Node.js fs). Choose this package for Vite, webpack, and other browser bundler setups.


Installation

npm install @edgerules/web @edgerules/portable

ESM only. The WASM binary is loaded asynchronously on first use.


Production Usage

Load from an EdgeRules DSL string

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 }

Load from a Portable JSON model

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

Error handling

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 / Development Usage

Use the ./mutable subpath for rule editors, visual builders, and test tooling. Do not use this build in production.

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 Reference

DecisionService

MethodDescription
new DecisionService(model)Create from a DSL string or a root PortableContext
execute(method, args?)Evaluate method (field path, function, or "*") against optional request bindings

execute details

  • method: "*" — evaluates every top-level field and returns the full model result.
  • method: "person.isAdult" — evaluates and returns that single field.
  • method: "validateApplicant" — calls a named function; supply its parameters through args.
  • args — a model-shaped object whose entries are bound as external inputs. Nested typed holes follow the model shape (e.g. { applicant: { age: 30 } } for applicant: { age: <number> }).

MutableDecisionService (dev build)

Inherits execute and adds:

MethodDescription
get(path, kinds?)Read a node, filtered by @kind (see Kinds); linked tells if the model links
set(path, value)Write an expression, placeholder, context, method, or type
getCode(path)Read a node as EdgeRules DSL source text
setCode(path, code)Write a node from EdgeRules DSL source text; returns a PortableNode, not code text
remove(path)Delete an entry at the given path
rename(oldPath, newPath)Rename an entry within its context

版本与发布记录

@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 是什么?

EdgeRules decision service for the browser.

如何安装 @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 和提交记录判断。