@criterionx/trpc 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 +152 -0
- package/dist/index.js +4127 -0
- package/package.json +50 -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,152 @@
|
|
|
1
|
+
import { Decision, Engine, ProfileRegistry, Result } from '@criterionx/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for creating a decision procedure
|
|
5
|
+
*/
|
|
6
|
+
interface DecisionProcedureOptions<TInput, TOutput, TProfile> {
|
|
7
|
+
/** The decision to evaluate */
|
|
8
|
+
decision: Decision<TInput, TOutput, TProfile>;
|
|
9
|
+
/** Engine instance (uses default if not provided) */
|
|
10
|
+
engine?: Engine;
|
|
11
|
+
/** Profile registry for ID-based profile resolution */
|
|
12
|
+
registry?: ProfileRegistry<TProfile>;
|
|
13
|
+
/** Default profile to use if not specified in input */
|
|
14
|
+
defaultProfile?: TProfile | string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Input for decision evaluation via tRPC
|
|
18
|
+
*/
|
|
19
|
+
interface DecisionInput<TInput, TProfile> {
|
|
20
|
+
/** Decision input data */
|
|
21
|
+
input: TInput;
|
|
22
|
+
/** Profile object or profile ID */
|
|
23
|
+
profile?: TProfile | string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Options for creating a decision router
|
|
27
|
+
*/
|
|
28
|
+
interface DecisionRouterOptions<TProfile> {
|
|
29
|
+
/** Decisions to include in the router */
|
|
30
|
+
decisions: Array<Decision<any, any, TProfile>>;
|
|
31
|
+
/** Profile map by decision ID or profile name */
|
|
32
|
+
profiles?: Record<string, TProfile>;
|
|
33
|
+
/** Engine instance */
|
|
34
|
+
engine?: Engine;
|
|
35
|
+
/** Profile registry */
|
|
36
|
+
registry?: ProfileRegistry<TProfile>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Result type for decision evaluation
|
|
40
|
+
*/
|
|
41
|
+
type DecisionResult<TOutput> = Result<TOutput>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* tRPC integration for Criterion decision engine
|
|
45
|
+
*
|
|
46
|
+
* @example Basic usage
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { initTRPC } from "@trpc/server";
|
|
49
|
+
* import { createDecisionProcedure } from "@criterionx/trpc";
|
|
50
|
+
* import { pricingDecision } from "./decisions";
|
|
51
|
+
*
|
|
52
|
+
* const t = initTRPC.create();
|
|
53
|
+
*
|
|
54
|
+
* const appRouter = t.router({
|
|
55
|
+
* pricing: createDecisionProcedure(t, {
|
|
56
|
+
* decision: pricingDecision,
|
|
57
|
+
* defaultProfile: { basePrice: 100 },
|
|
58
|
+
* }),
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Client usage (fully typed)
|
|
62
|
+
* const result = await trpc.pricing.mutate({
|
|
63
|
+
* input: { quantity: 5 },
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create a tRPC procedure for evaluating a decision
|
|
70
|
+
*
|
|
71
|
+
* Returns a mutation procedure that accepts decision input and optional profile,
|
|
72
|
+
* and returns the evaluation result with full type inference.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* import { initTRPC } from "@trpc/server";
|
|
77
|
+
* import { createDecisionProcedure } from "@criterionx/trpc";
|
|
78
|
+
*
|
|
79
|
+
* const t = initTRPC.create();
|
|
80
|
+
*
|
|
81
|
+
* const router = t.router({
|
|
82
|
+
* pricing: createDecisionProcedure(t, {
|
|
83
|
+
* decision: pricingDecision,
|
|
84
|
+
* defaultProfile: { basePrice: 100 },
|
|
85
|
+
* }),
|
|
86
|
+
* eligibility: createDecisionProcedure(t, {
|
|
87
|
+
* decision: eligibilityDecision,
|
|
88
|
+
* defaultProfile: "standard",
|
|
89
|
+
* }),
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
declare function createDecisionProcedure<TInput, TOutput, TProfile, T extends {
|
|
94
|
+
procedure: any;
|
|
95
|
+
router: any;
|
|
96
|
+
}>(t: T, options: DecisionProcedureOptions<TInput, TOutput, TProfile>): ReturnType<T["procedure"]["input"]>;
|
|
97
|
+
/**
|
|
98
|
+
* Create a tRPC router with procedures for multiple decisions
|
|
99
|
+
*
|
|
100
|
+
* Each decision becomes a procedure in the router, accessible by its ID.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* import { initTRPC } from "@trpc/server";
|
|
105
|
+
* import { createDecisionRouter } from "@criterionx/trpc";
|
|
106
|
+
*
|
|
107
|
+
* const t = initTRPC.create();
|
|
108
|
+
*
|
|
109
|
+
* const decisionsRouter = createDecisionRouter(t, {
|
|
110
|
+
* decisions: [pricingDecision, eligibilityDecision],
|
|
111
|
+
* profiles: {
|
|
112
|
+
* pricing: { basePrice: 100 },
|
|
113
|
+
* eligibility: { minAge: 18 },
|
|
114
|
+
* },
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* const appRouter = t.router({
|
|
118
|
+
* decisions: decisionsRouter,
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* // Client usage
|
|
122
|
+
* const result = await trpc.decisions.pricing.mutate({ input: { quantity: 5 } });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare function createDecisionRouter<TProfile, T extends {
|
|
126
|
+
procedure: any;
|
|
127
|
+
router: any;
|
|
128
|
+
}>(t: T, options: DecisionRouterOptions<TProfile>): ReturnType<T["router"]>;
|
|
129
|
+
/**
|
|
130
|
+
* Helper to create a typed decision caller for use outside of tRPC context
|
|
131
|
+
*
|
|
132
|
+
* Useful for server-side code that needs to call decisions directly
|
|
133
|
+
* with the same interface as tRPC procedures.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* import { createDecisionCaller } from "@criterionx/trpc";
|
|
138
|
+
*
|
|
139
|
+
* const callPricing = createDecisionCaller({
|
|
140
|
+
* decision: pricingDecision,
|
|
141
|
+
* defaultProfile: { basePrice: 100 },
|
|
142
|
+
* });
|
|
143
|
+
*
|
|
144
|
+
* const result = callPricing({ input: { quantity: 5 } });
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
declare function createDecisionCaller<TInput, TOutput, TProfile>(options: DecisionProcedureOptions<TInput, TOutput, TProfile>): (args: {
|
|
148
|
+
input: TInput;
|
|
149
|
+
profile?: TProfile | string;
|
|
150
|
+
}) => Result<TOutput>;
|
|
151
|
+
|
|
152
|
+
export { type DecisionInput, type DecisionProcedureOptions, type DecisionResult, type DecisionRouterOptions, createDecisionCaller, createDecisionProcedure, createDecisionRouter };
|