Skip to content

Commit 7a4218d

Browse files
committed
Add type for results
1 parent 21bb1fc commit 7a4218d

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ interface RulesEngine {
407407
on('error', subscriber: ErrorSubscriber): Unsubscribe
408408
on('start', subscriber: StartSubscriber): Unsubscribe
409409
on('complete', subscriber: CompleteSubscriber): Unsubscribe
410-
run(context: Record<string, any>): Promise<void>;
410+
run(context: Record<string, any>): Promise<EngineResults>;
411411
}
412412

413413
type PatchFunction<T> = (o: T) => T;

src/index.d.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ type RuleActions = {
2020
actions: Action[];
2121
};
2222

23-
export type FactMap = Record<string, Condition>;
24-
export type Condition = {
23+
interface FactMap {
24+
[fact: string]: Evaluator;
25+
}
26+
27+
export type Evaluator = {
2528
params?: Record<string, any>;
2629
path?: string;
2730
is: Record<string, any>;
@@ -110,6 +113,21 @@ type ActionExecutionError = {
110113
error: Error;
111114
};
112115

116+
type FactMapResult = {
117+
[key: string]: {
118+
result: boolean;
119+
value: any;
120+
resolved: any;
121+
};
122+
};
123+
124+
type RuleResult = {
125+
actions?: Record<string, Action[]>;
126+
results?: Record<string, Record<string, FactMapResult>>;
127+
};
128+
129+
type EngineResults = Record<string, RuleResult>;
130+
113131
export type DebugEvent =
114132
| StartingFactMapEvent
115133
| StartingFactEvent
@@ -127,6 +145,7 @@ export type StartEvent = {
127145
};
128146
export type CompleteEvent = {
129147
context: Context;
148+
results: EngineResults;
130149
};
131150

132151
export type DebugSubscriber = (event: DebugEvent) => void;
@@ -158,7 +177,7 @@ export interface RulesEngine {
158177
setRules(rules: Patch<Rules>): void;
159178
setActions(actions: Patch<Actions>): void;
160179
setFacts(facts: Patch<Facts>): void;
161-
run(context?: Context): Promise<void>;
180+
run(context?: Context): Promise<EngineResults>;
162181
on(event: 'debug', subscriber: DebugSubscriber): Unsubscribe;
163182
on(event: 'start', subscriber: StartSubscriber): Unsubscribe;
164183
on(event: 'complete', subscriber: CompleteSubscriber): Unsubscribe;

test/engine.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ describe('rules engine', () => {
1717
it('should execute a rule', async () => {
1818
const rules = {
1919
salutation: {
20-
when: [{ firstName: { is: { type: 'string', pattern: '^J' } } }],
20+
when: [
21+
{
22+
firstName: { is: { type: 'string', pattern: '^J' } },
23+
},
24+
],
2125
then: {
2226
actions: [{ type: 'log', params: { message: 'Hi friend!' } }],
2327
},
@@ -30,7 +34,6 @@ describe('rules engine', () => {
3034
await engine.run({ firstName: 'John' });
3135
expect(log).toHaveBeenCalledWith({ message: 'Hi friend!' });
3236
log.mockClear();
33-
await engine.run({ firstName: 'Bill' });
3437
expect(log).not.toHaveBeenCalled();
3538
expect(call).toHaveBeenCalledWith({ message: 'Who are you?' });
3639
});

test/validators.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)