@criterionx/react 0.3.5
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 +21 -0
- package/dist/index.d.ts +184 -0
- package/dist/index.js +80 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Tomas Maritano
|
|
4
|
+
|
|
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:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { Engine, ProfileRegistry, Result, RunOptions, Decision } from '@criterionx/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* State returned by useDecision hook
|
|
6
|
+
*/
|
|
7
|
+
interface UseDecisionState<TOutput> {
|
|
8
|
+
/** Current result (null if not yet evaluated) */
|
|
9
|
+
result: Result<TOutput> | null;
|
|
10
|
+
/** Whether an evaluation is in progress */
|
|
11
|
+
isEvaluating: boolean;
|
|
12
|
+
/** Error from the last evaluation (null if no error) */
|
|
13
|
+
error: Error | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Actions returned by useDecision hook
|
|
17
|
+
*/
|
|
18
|
+
interface UseDecisionActions<TInput, TProfile> {
|
|
19
|
+
/** Evaluate the decision with given input and options */
|
|
20
|
+
evaluate: (input: TInput, options: RunOptions<TProfile>) => void;
|
|
21
|
+
/** Reset the state (clear result and error) */
|
|
22
|
+
reset: () => void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Complete return type for useDecision hook
|
|
26
|
+
*/
|
|
27
|
+
type UseDecisionReturn<TInput, TOutput, TProfile> = UseDecisionState<TOutput> & UseDecisionActions<TInput, TProfile>;
|
|
28
|
+
/**
|
|
29
|
+
* Options for useDecision hook
|
|
30
|
+
*/
|
|
31
|
+
interface UseDecisionOptions<TProfile> {
|
|
32
|
+
/** Custom engine instance (uses default if not provided) */
|
|
33
|
+
engine?: Engine;
|
|
34
|
+
/** Profile registry for ID-based profile resolution */
|
|
35
|
+
registry?: ProfileRegistry<TProfile>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Context value for CriterionProvider
|
|
39
|
+
*/
|
|
40
|
+
interface CriterionContextValue {
|
|
41
|
+
/** Engine instance */
|
|
42
|
+
engine: Engine;
|
|
43
|
+
/** Optional profile registry */
|
|
44
|
+
registry?: ProfileRegistry<any>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Props for CriterionProvider
|
|
48
|
+
*/
|
|
49
|
+
interface CriterionProviderProps {
|
|
50
|
+
/** React children */
|
|
51
|
+
children: React.ReactNode;
|
|
52
|
+
/** Custom engine instance (creates default if not provided) */
|
|
53
|
+
engine?: Engine;
|
|
54
|
+
/** Profile registry for shared profiles */
|
|
55
|
+
registry?: ProfileRegistry<any>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* React Context for Criterion
|
|
60
|
+
*/
|
|
61
|
+
declare const CriterionContext: react.Context<CriterionContextValue>;
|
|
62
|
+
/**
|
|
63
|
+
* Provider component for Criterion context
|
|
64
|
+
*
|
|
65
|
+
* Provides an Engine instance and optional ProfileRegistry to all child components.
|
|
66
|
+
* If no engine is provided, a default singleton instance is used.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* import { CriterionProvider } from "@criterionx/react";
|
|
71
|
+
* import { createProfileRegistry } from "@criterionx/core";
|
|
72
|
+
*
|
|
73
|
+
* const registry = createProfileRegistry();
|
|
74
|
+
* registry.register("default", { threshold: 100 });
|
|
75
|
+
*
|
|
76
|
+
* function App() {
|
|
77
|
+
* return (
|
|
78
|
+
* <CriterionProvider registry={registry}>
|
|
79
|
+
* <MyComponent />
|
|
80
|
+
* </CriterionProvider>
|
|
81
|
+
* );
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare function CriterionProvider({ children, engine, registry, }: CriterionProviderProps): React.ReactElement;
|
|
86
|
+
/**
|
|
87
|
+
* Hook to access the Criterion context
|
|
88
|
+
*
|
|
89
|
+
* Returns the engine and optional registry from the nearest CriterionProvider.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```tsx
|
|
93
|
+
* function MyComponent() {
|
|
94
|
+
* const { engine, registry } = useCriterion();
|
|
95
|
+
* // Use engine directly if needed
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function useCriterion(): CriterionContextValue;
|
|
100
|
+
/**
|
|
101
|
+
* Hook to access the Engine instance
|
|
102
|
+
*
|
|
103
|
+
* Shorthand for accessing just the engine from context.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```tsx
|
|
107
|
+
* function MyComponent() {
|
|
108
|
+
* const engine = useEngine();
|
|
109
|
+
* const result = engine.run(decision, input, { profile });
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function useEngine(): Engine;
|
|
114
|
+
/**
|
|
115
|
+
* Hook to access the ProfileRegistry
|
|
116
|
+
*
|
|
117
|
+
* Returns the registry from context, or undefined if not provided.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```tsx
|
|
121
|
+
* function MyComponent() {
|
|
122
|
+
* const registry = useProfileRegistry();
|
|
123
|
+
* if (registry) {
|
|
124
|
+
* const profile = registry.get("default");
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
declare function useProfileRegistry<TProfile>(): ProfileRegistry<TProfile> | undefined;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* React hook for evaluating Criterion decisions
|
|
133
|
+
*
|
|
134
|
+
* Provides a convenient way to run decisions in React components with
|
|
135
|
+
* automatic state management for results, loading state, and errors.
|
|
136
|
+
*
|
|
137
|
+
* @param decision - The decision to evaluate
|
|
138
|
+
* @param options - Optional configuration (custom engine, registry)
|
|
139
|
+
* @returns State and actions for decision evaluation
|
|
140
|
+
*
|
|
141
|
+
* @example Basic usage
|
|
142
|
+
* ```tsx
|
|
143
|
+
* import { useDecision } from "@criterionx/react";
|
|
144
|
+
* import { pricingDecision } from "./decisions";
|
|
145
|
+
*
|
|
146
|
+
* function PricingComponent() {
|
|
147
|
+
* const { result, isEvaluating, error, evaluate } = useDecision(pricingDecision);
|
|
148
|
+
*
|
|
149
|
+
* const handleCalculate = () => {
|
|
150
|
+
* evaluate(
|
|
151
|
+
* { quantity: 10, customerType: "premium" },
|
|
152
|
+
* { profile: { basePrice: 100, premiumDiscount: 0.2 } }
|
|
153
|
+
* );
|
|
154
|
+
* };
|
|
155
|
+
*
|
|
156
|
+
* if (isEvaluating) return <div>Calculating...</div>;
|
|
157
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
158
|
+
*
|
|
159
|
+
* return (
|
|
160
|
+
* <div>
|
|
161
|
+
* <button onClick={handleCalculate}>Calculate Price</button>
|
|
162
|
+
* {result && <div>Price: ${result.data?.price}</div>}
|
|
163
|
+
* </div>
|
|
164
|
+
* );
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* @example With profile registry
|
|
169
|
+
* ```tsx
|
|
170
|
+
* function PricingComponent() {
|
|
171
|
+
* const { evaluate, result } = useDecision(pricingDecision);
|
|
172
|
+
*
|
|
173
|
+
* // Use profile ID from registry
|
|
174
|
+
* const handleCalculate = () => {
|
|
175
|
+
* evaluate({ quantity: 10 }, { profile: "premium-pricing" });
|
|
176
|
+
* };
|
|
177
|
+
*
|
|
178
|
+
* // ...
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
declare function useDecision<TInput, TOutput, TProfile>(decision: Decision<TInput, TOutput, TProfile>, options?: UseDecisionOptions<TProfile>): UseDecisionReturn<TInput, TOutput, TProfile>;
|
|
183
|
+
|
|
184
|
+
export { CriterionContext, type CriterionContextValue, CriterionProvider, type CriterionProviderProps, type UseDecisionActions, type UseDecisionOptions, type UseDecisionReturn, type UseDecisionState, useCriterion, useDecision, useEngine, useProfileRegistry };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// src/context.tsx
|
|
2
|
+
import { createContext, useContext, useMemo } from "react";
|
|
3
|
+
import { Engine } from "@criterionx/core";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
var defaultEngine = new Engine();
|
|
6
|
+
var CriterionContext = createContext({
|
|
7
|
+
engine: defaultEngine
|
|
8
|
+
});
|
|
9
|
+
function CriterionProvider({
|
|
10
|
+
children,
|
|
11
|
+
engine,
|
|
12
|
+
registry
|
|
13
|
+
}) {
|
|
14
|
+
const value = useMemo(
|
|
15
|
+
() => ({
|
|
16
|
+
engine: engine ?? defaultEngine,
|
|
17
|
+
registry
|
|
18
|
+
}),
|
|
19
|
+
[engine, registry]
|
|
20
|
+
);
|
|
21
|
+
return /* @__PURE__ */ jsx(CriterionContext.Provider, { value, children });
|
|
22
|
+
}
|
|
23
|
+
function useCriterion() {
|
|
24
|
+
return useContext(CriterionContext);
|
|
25
|
+
}
|
|
26
|
+
function useEngine() {
|
|
27
|
+
const { engine } = useContext(CriterionContext);
|
|
28
|
+
return engine;
|
|
29
|
+
}
|
|
30
|
+
function useProfileRegistry() {
|
|
31
|
+
const { registry } = useContext(CriterionContext);
|
|
32
|
+
return registry;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/useDecision.ts
|
|
36
|
+
import { useCallback, useState } from "react";
|
|
37
|
+
function useDecision(decision, options) {
|
|
38
|
+
const context = useCriterion();
|
|
39
|
+
const engine = options?.engine ?? context.engine;
|
|
40
|
+
const registry = options?.registry ?? context.registry;
|
|
41
|
+
const [result, setResult] = useState(null);
|
|
42
|
+
const [isEvaluating, setIsEvaluating] = useState(false);
|
|
43
|
+
const [error, setError] = useState(null);
|
|
44
|
+
const evaluate = useCallback(
|
|
45
|
+
(input, runOptions) => {
|
|
46
|
+
setIsEvaluating(true);
|
|
47
|
+
setError(null);
|
|
48
|
+
try {
|
|
49
|
+
const evalResult = engine.run(decision, input, runOptions, registry);
|
|
50
|
+
setResult(evalResult);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
53
|
+
setResult(null);
|
|
54
|
+
} finally {
|
|
55
|
+
setIsEvaluating(false);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
[decision, engine, registry]
|
|
59
|
+
);
|
|
60
|
+
const reset = useCallback(() => {
|
|
61
|
+
setResult(null);
|
|
62
|
+
setError(null);
|
|
63
|
+
setIsEvaluating(false);
|
|
64
|
+
}, []);
|
|
65
|
+
return {
|
|
66
|
+
result,
|
|
67
|
+
isEvaluating,
|
|
68
|
+
error,
|
|
69
|
+
evaluate,
|
|
70
|
+
reset
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
CriterionContext,
|
|
75
|
+
CriterionProvider,
|
|
76
|
+
useCriterion,
|
|
77
|
+
useDecision,
|
|
78
|
+
useEngine,
|
|
79
|
+
useProfileRegistry
|
|
80
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@criterionx/react",
|
|
3
|
+
"version": "0.3.5",
|
|
4
|
+
"description": "React hooks for Criterion decision engine",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"criterion",
|
|
21
|
+
"react",
|
|
22
|
+
"hooks",
|
|
23
|
+
"decision-engine",
|
|
24
|
+
"rules-engine",
|
|
25
|
+
"business-rules"
|
|
26
|
+
],
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "Tomas Maritano",
|
|
29
|
+
"url": "https://github.com/tomymaritano"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/tomymaritano/criterionx.git",
|
|
35
|
+
"directory": "packages/react"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@criterionx/core": "0.3.5"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=18.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"zod": "^3.24.0",
|
|
45
|
+
"@testing-library/react": "^16.0.0",
|
|
46
|
+
"@types/node": "^20.0.0",
|
|
47
|
+
"@types/react": "^18.0.0",
|
|
48
|
+
"jsdom": "^25.0.0",
|
|
49
|
+
"react": "^18.0.0",
|
|
50
|
+
"react-dom": "^18.0.0",
|
|
51
|
+
"tsup": "^8.0.0",
|
|
52
|
+
"typescript": "^5.3.0",
|
|
53
|
+
"vitest": "^4.0.0"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=18"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsup src/index.ts --format esm --dts --clean --external react",
|
|
60
|
+
"test": "vitest run",
|
|
61
|
+
"test:watch": "vitest",
|
|
62
|
+
"test:coverage": "vitest run --coverage",
|
|
63
|
+
"typecheck": "tsc --noEmit"
|
|
64
|
+
}
|
|
65
|
+
}
|