@f-o-t/rules-engine 2.0.2 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,9 +1,21 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 FOT
3
+ Copyright (c) 2026 FOT (F-O-T)
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
6
11
 
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
8
14
 
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -23,35 +23,65 @@ bun add @f-o-t/rules-engine
23
23
  ## Quick Start
24
24
 
25
25
  ```typescript
26
- import { createEngine, rule, all, num, str } from "@f-o-t/rules-engine";
26
+ import { createEngine, createEvaluator } from "@f-o-t/rules-engine";
27
+ import { z } from "zod";
27
28
 
28
- // Create an engine
29
- const engine = createEngine();
29
+ // Define your consequence types
30
+ const MyConsequences = {
31
+ send_email: z.object({
32
+ to: z.string().email(),
33
+ subject: z.string(),
34
+ }),
35
+ apply_discount: z.object({
36
+ percentage: z.number(),
37
+ }),
38
+ };
39
+
40
+ // Create engine with built-in operators
41
+ const engine = createEngine({
42
+ consequences: MyConsequences,
43
+ evaluator: createEvaluator(), // Required!
44
+ });
30
45
 
31
- // Add rules using the fluent builder
32
- engine.addRule(
33
- rule()
34
- .named("Premium Discount")
35
- .when(
36
- all(
37
- num("orderTotal", "gt", 100),
38
- str("customerType", "eq", "premium")
39
- )
40
- )
41
- .then("apply_discount", { percentage: 15 })
42
- .withPriority(100)
43
- .tagged("pricing", "discount")
44
- .build()
45
- );
46
+ // Or with custom operators
47
+ import { moneyOperators } from "@f-o-t/money/operators";
48
+
49
+ const engine = createEngine({
50
+ consequences: MyConsequences,
51
+ operators: moneyOperators, // Convenience: engine creates evaluator
52
+ });
46
53
 
47
- // Evaluate against context
54
+ // Add rules
55
+ engine.addRule({
56
+ name: "high-value-customer",
57
+ conditions: {
58
+ id: "g1",
59
+ operator: "AND",
60
+ conditions: [
61
+ {
62
+ id: "c1",
63
+ type: "number",
64
+ field: "totalPurchases",
65
+ operator: "gt",
66
+ value: 1000,
67
+ },
68
+ ],
69
+ },
70
+ consequences: [
71
+ {
72
+ type: "apply_discount",
73
+ payload: { percentage: 10 },
74
+ },
75
+ ],
76
+ });
77
+
78
+ // Evaluate
48
79
  const result = await engine.evaluate({
49
- orderTotal: 150,
50
- customerType: "premium",
80
+ totalPurchases: 1500,
51
81
  });
52
82
 
53
- console.log(result.matchedRules); // Rules that matched
54
- console.log(result.consequences); // Actions to execute
83
+ console.log(result.matchedRules); // Rules that matched
84
+ console.log(result.consequences); // Actions to take
55
85
  ```
56
86
 
57
87
  ## Building Conditions
@@ -125,6 +155,59 @@ const myRule = rule()
125
155
  .build();
126
156
  ```
127
157
 
158
+ ## Custom Operators
159
+
160
+ Use custom operators from libraries like `@f-o-t/money`:
161
+
162
+ ```typescript
163
+ import { createEngine } from "@f-o-t/rules-engine";
164
+ import { moneyOperators } from "@f-o-t/money/operators";
165
+
166
+ const engine = createEngine({
167
+ operators: moneyOperators,
168
+ });
169
+
170
+ engine.addRule({
171
+ name: "large-transaction",
172
+ conditions: {
173
+ id: "g1",
174
+ operator: "AND",
175
+ conditions: [
176
+ {
177
+ id: "c1",
178
+ type: "custom",
179
+ field: "amount",
180
+ operator: "money_gt",
181
+ value: { amount: "5000.00", currency: "BRL" },
182
+ },
183
+ ],
184
+ },
185
+ consequences: [
186
+ { type: "flag_for_review", payload: {} },
187
+ ],
188
+ });
189
+ ```
190
+
191
+ Create your own custom operators:
192
+
193
+ ```typescript
194
+ import { createEngine, createOperator } from "@f-o-t/rules-engine";
195
+
196
+ const customOperator = createOperator({
197
+ name: "is_valid_cpf",
198
+ type: "custom",
199
+ description: "Validate Brazilian CPF",
200
+ evaluate: (actual, expected) => {
201
+ // Your validation logic
202
+ return validateCPF(actual as string);
203
+ },
204
+ });
205
+
206
+ const engine = createEngine({
207
+ operators: { is_valid_cpf: customOperator },
208
+ });
209
+ ```
210
+
128
211
  ## Engine API
129
212
 
130
213
  ### Rule Management
package/package.json CHANGED
@@ -1,69 +1,36 @@
1
1
  {
2
- "bugs": {
3
- "url": "https://github.com/F-O-T/montte-nx/issues"
4
- },
5
- "description": "Type-safe, functional rules orchestration engine for TypeScript",
6
- "dependencies": {
7
- "@f-o-t/condition-evaluator": "2.0.0",
8
- "zod": "4.3.4"
9
- },
10
- "devDependencies": {
11
- "@biomejs/biome": "2.3.10",
12
- "@types/bun": "1.3.5",
13
- "bumpp": "10.3.2",
14
- "bunup": "0.16.11",
15
- "typescript": "5.9.3"
16
- },
17
- "exports": {
18
- ".": {
19
- "bun": "./src/index.ts",
20
- "import": {
21
- "default": "./dist/index.js",
22
- "types": "./dist/index.d.ts"
23
- },
24
- "require": {
25
- "default": "./dist/index.cjs",
26
- "types": "./dist/index.d.cts"
27
- },
28
- "types": "./src/index.ts"
29
- },
30
- "./package.json": "./package.json"
31
- },
32
- "main": "./dist/index.cjs",
33
- "files": [
34
- "dist"
35
- ],
36
- "homepage": "https://github.com/F-O-T/montte-nx/blob/master/libraries/rules-engine",
37
- "license": "MIT",
38
- "module": "./dist/index.js",
39
- "name": "@f-o-t/rules-engine",
40
- "peerDependencies": {
41
- "typescript": ">=4.5.0"
42
- },
43
- "peerDependenciesMeta": {
44
- "typescript": {
45
- "optional": true
46
- }
47
- },
48
- "private": false,
49
- "publishConfig": {
50
- "access": "public"
51
- },
52
- "repository": {
53
- "type": "git",
54
- "url": "https://github.com/F-O-T/montte-nx.git"
55
- },
56
- "scripts": {
57
- "build": "bunup",
58
- "check": "biome check --write .",
59
- "dev": "bunup --watch",
60
- "release": "bumpp --commit --push --tag",
61
- "test": "bun test",
62
- "test:coverage": "bun test --coverage",
63
- "test:watch": "bun test --watch",
64
- "typecheck": "tsc"
65
- },
66
- "type": "module",
67
- "types": "./dist/index.d.ts",
68
- "version": "2.0.2"
2
+ "name": "@f-o-t/rules-engine",
3
+ "version": "3.0.1",
4
+ "type": "module",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "build": "bun x --bun fot build",
18
+ "test": "bun x --bun fot test",
19
+ "lint": "bun x --bun fot lint",
20
+ "format": "bun x --bun fot format",
21
+ "typecheck": "bun x --bun fot typecheck"
22
+ },
23
+ "dependencies": {
24
+ "@f-o-t/condition-evaluator": "^2.0.2",
25
+ "zod": "^4.3.6"
26
+ },
27
+ "devDependencies": {
28
+ "@f-o-t/cli": "^1.0.0",
29
+ "@f-o-t/config": "^1.0.0"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/F-O-T/libraries.git",
34
+ "directory": "libraries/rules-engine"
35
+ }
69
36
  }