@criterionx/express 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 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.
@@ -0,0 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ export {
9
+ __require
10
+ };
@@ -0,0 +1,139 @@
1
+ // src/fastify.ts
2
+ import { Engine } from "@criterionx/core";
3
+ var defaultEngine = new Engine();
4
+ var criterionPlugin = async (fastify, options) => {
5
+ const {
6
+ decisions,
7
+ profiles = {},
8
+ engine = defaultEngine,
9
+ registry,
10
+ prefix = "/decisions"
11
+ } = options;
12
+ for (const decision of decisions) {
13
+ fastify.post(`${prefix}/${decision.id}`, async (request, reply) => {
14
+ try {
15
+ const body = request.body;
16
+ const query = request.query;
17
+ const input = body;
18
+ const profileKey = query?.profile ?? body?.profile;
19
+ const profile = profileKey && profiles[profileKey] ? profiles[profileKey] : profileKey ?? profiles[decision.id];
20
+ const result = engine.run(decision, input, { profile }, registry);
21
+ request.criterion = {
22
+ result,
23
+ decision: decision.id,
24
+ evaluatedAt: (/* @__PURE__ */ new Date()).toISOString()
25
+ };
26
+ if (result.status !== "OK") {
27
+ reply.status(400);
28
+ return {
29
+ error: {
30
+ code: result.status,
31
+ message: result.meta.explanation
32
+ },
33
+ result,
34
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
35
+ };
36
+ }
37
+ return result;
38
+ } catch (error) {
39
+ const err = error instanceof Error ? error : new Error(String(error));
40
+ reply.status(400);
41
+ return {
42
+ error: {
43
+ code: "EVALUATION_ERROR",
44
+ message: err.message
45
+ },
46
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
47
+ };
48
+ }
49
+ });
50
+ }
51
+ };
52
+ function createDecisionRoute(options) {
53
+ const {
54
+ decision,
55
+ engine = defaultEngine,
56
+ registry,
57
+ getInput = (request) => request.body,
58
+ getProfile = (request) => {
59
+ const query = request.query;
60
+ const body = request.body;
61
+ return query?.profile ?? body?.profile;
62
+ },
63
+ formatResponse = (result) => result
64
+ } = options;
65
+ return async (request, reply) => {
66
+ try {
67
+ const input = getInput(request);
68
+ const profile = getProfile(request);
69
+ const result = engine.run(decision, input, { profile }, registry);
70
+ request.criterion = {
71
+ result,
72
+ decision: decision.id,
73
+ evaluatedAt: (/* @__PURE__ */ new Date()).toISOString()
74
+ };
75
+ if (result.status !== "OK") {
76
+ reply.status(400);
77
+ return {
78
+ error: {
79
+ code: result.status,
80
+ message: result.meta.explanation
81
+ },
82
+ result: formatResponse(result),
83
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
84
+ };
85
+ }
86
+ return formatResponse(result);
87
+ } catch (error) {
88
+ const err = error instanceof Error ? error : new Error(String(error));
89
+ reply.status(400);
90
+ return {
91
+ error: {
92
+ code: "EVALUATION_ERROR",
93
+ message: err.message
94
+ },
95
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
96
+ };
97
+ }
98
+ };
99
+ }
100
+ function createDecisionHook(options) {
101
+ const {
102
+ decision,
103
+ engine = defaultEngine,
104
+ registry,
105
+ getInput = (request) => request.body,
106
+ getProfile = (request) => {
107
+ const query = request.query;
108
+ const body = request.body;
109
+ return query?.profile ?? body?.profile;
110
+ }
111
+ } = options;
112
+ return async (request, reply) => {
113
+ try {
114
+ const input = getInput(request);
115
+ const profile = getProfile(request);
116
+ const result = engine.run(decision, input, { profile }, registry);
117
+ request.criterion = {
118
+ result,
119
+ decision: decision.id,
120
+ evaluatedAt: (/* @__PURE__ */ new Date()).toISOString()
121
+ };
122
+ } catch (error) {
123
+ const err = error instanceof Error ? error : new Error(String(error));
124
+ reply.status(400).send({
125
+ error: {
126
+ code: "EVALUATION_ERROR",
127
+ message: err.message
128
+ },
129
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
130
+ });
131
+ }
132
+ };
133
+ }
134
+
135
+ export {
136
+ criterionPlugin,
137
+ createDecisionRoute,
138
+ createDecisionHook
139
+ };
@@ -0,0 +1,117 @@
1
+ import {
2
+ __require
3
+ } from "./chunk-3RG5ZIWI.js";
4
+
5
+ // src/express.ts
6
+ import { Engine } from "@criterionx/core";
7
+ var defaultEngine = new Engine();
8
+ function createDecisionMiddleware(options) {
9
+ const {
10
+ decision,
11
+ engine = defaultEngine,
12
+ registry,
13
+ getInput = (req) => req.body,
14
+ getProfile = (req) => {
15
+ const r = req;
16
+ return r.query?.profile ?? r.body?.profile;
17
+ },
18
+ formatResponse = (result) => result,
19
+ onError
20
+ } = options;
21
+ return (req, res, _next) => {
22
+ try {
23
+ const input = getInput(req);
24
+ const profile = getProfile(req);
25
+ const result = engine.run(decision, input, { profile }, registry);
26
+ req.criterion = {
27
+ result,
28
+ decision: decision.id,
29
+ evaluatedAt: (/* @__PURE__ */ new Date()).toISOString()
30
+ };
31
+ if (result.status !== "OK") {
32
+ res.status(400).json({
33
+ error: {
34
+ code: result.status,
35
+ message: result.meta.explanation
36
+ },
37
+ result: formatResponse(result),
38
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
39
+ });
40
+ return;
41
+ }
42
+ res.json(formatResponse(result));
43
+ } catch (error) {
44
+ if (onError) {
45
+ onError(error instanceof Error ? error : new Error(String(error)), req, res);
46
+ return;
47
+ }
48
+ const err = error instanceof Error ? error : new Error(String(error));
49
+ res.status(400).json({
50
+ error: {
51
+ code: "EVALUATION_ERROR",
52
+ message: err.message
53
+ },
54
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
55
+ });
56
+ }
57
+ };
58
+ }
59
+ function createDecisionHandler(options) {
60
+ const {
61
+ decision,
62
+ engine = defaultEngine,
63
+ registry,
64
+ getInput = (req) => req.body,
65
+ getProfile = (req) => {
66
+ const r = req;
67
+ return r.query?.profile ?? r.body?.profile;
68
+ },
69
+ onError
70
+ } = options;
71
+ return (req, res, next) => {
72
+ try {
73
+ const input = getInput(req);
74
+ const profile = getProfile(req);
75
+ const result = engine.run(decision, input, { profile }, registry);
76
+ req.criterion = {
77
+ result,
78
+ decision: decision.id,
79
+ evaluatedAt: (/* @__PURE__ */ new Date()).toISOString()
80
+ };
81
+ next();
82
+ } catch (error) {
83
+ if (onError) {
84
+ onError(error instanceof Error ? error : new Error(String(error)), req, res);
85
+ return;
86
+ }
87
+ next(error);
88
+ }
89
+ };
90
+ }
91
+ function createDecisionRouter(options) {
92
+ const { Router } = __require("express");
93
+ const router = Router();
94
+ const { decisions, profiles = {}, engine = defaultEngine, registry } = options;
95
+ for (const decision of decisions) {
96
+ router.post(`/${decision.id}`, createDecisionMiddleware({
97
+ decision,
98
+ engine,
99
+ registry,
100
+ getProfile: (req) => {
101
+ const r = req;
102
+ const profileKey = r.query?.profile ?? r.body?.profile;
103
+ if (typeof profileKey === "string" && profiles[profileKey]) {
104
+ return profiles[profileKey];
105
+ }
106
+ return profileKey ?? profiles[decision.id];
107
+ }
108
+ }));
109
+ }
110
+ return router;
111
+ }
112
+
113
+ export {
114
+ createDecisionMiddleware,
115
+ createDecisionHandler,
116
+ createDecisionRouter
117
+ };
@@ -0,0 +1,81 @@
1
+ import * as express from 'express';
2
+ import { RequestHandler } from 'express';
3
+ import * as _criterionx_core from '@criterionx/core';
4
+ import { Engine } from '@criterionx/core';
5
+ import { C as CriterionResult, D as DecisionMiddlewareOptions } from './types-Dz8OTTmP.js';
6
+
7
+ declare global {
8
+ namespace Express {
9
+ interface Request {
10
+ criterion?: CriterionResult<any>;
11
+ }
12
+ }
13
+ }
14
+ /**
15
+ * Create Express middleware that evaluates a decision and sends the result
16
+ *
17
+ * The middleware extracts input from the request, evaluates the decision,
18
+ * and sends the result as JSON response.
19
+ *
20
+ * @example With custom input extraction
21
+ * ```typescript
22
+ * app.post("/evaluate/:decisionId", createDecisionMiddleware({
23
+ * decision: myDecision,
24
+ * getInput: (req) => ({
25
+ * ...req.body,
26
+ * userId: req.params.userId,
27
+ * }),
28
+ * getProfile: (req) => req.query.profile as string,
29
+ * }));
30
+ * ```
31
+ */
32
+ declare function createDecisionMiddleware<TInput, TOutput, TProfile>(options: DecisionMiddlewareOptions<TInput, TOutput, TProfile>): RequestHandler;
33
+ /**
34
+ * Create Express middleware that evaluates a decision and attaches result to request
35
+ *
36
+ * Unlike createDecisionMiddleware, this middleware calls next() instead of
37
+ * sending a response, allowing downstream middleware to access the result.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * app.post("/pricing",
42
+ * createDecisionHandler({
43
+ * decision: pricingDecision,
44
+ * getProfile: () => ({ basePrice: 100 }),
45
+ * }),
46
+ * (req, res) => {
47
+ * const { result } = req.criterion!;
48
+ * res.json({ price: result.data?.price, meta: { custom: true } });
49
+ * }
50
+ * );
51
+ * ```
52
+ */
53
+ declare function createDecisionHandler<TInput, TOutput, TProfile>(options: DecisionMiddlewareOptions<TInput, TOutput, TProfile>): RequestHandler;
54
+ /**
55
+ * Create an Express router with decision endpoints
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * import { createDecisionRouter } from "@criterionx/express/express";
60
+ *
61
+ * const router = createDecisionRouter({
62
+ * decisions: [pricingDecision, eligibilityDecision],
63
+ * profiles: {
64
+ * "pricing": { basePrice: 100 },
65
+ * "eligibility": { minAge: 18 },
66
+ * },
67
+ * });
68
+ *
69
+ * app.use("/decisions", router);
70
+ * // POST /decisions/pricing
71
+ * // POST /decisions/eligibility
72
+ * ```
73
+ */
74
+ declare function createDecisionRouter<TProfile>(options: {
75
+ decisions: Array<_criterionx_core.Decision<any, any, TProfile>>;
76
+ profiles?: Record<string, TProfile>;
77
+ engine?: Engine;
78
+ registry?: _criterionx_core.ProfileRegistry<TProfile>;
79
+ }): express.Router;
80
+
81
+ export { createDecisionHandler, createDecisionMiddleware, createDecisionRouter };
@@ -0,0 +1,11 @@
1
+ import {
2
+ createDecisionHandler,
3
+ createDecisionMiddleware,
4
+ createDecisionRouter
5
+ } from "./chunk-YSMNRLEG.js";
6
+ import "./chunk-3RG5ZIWI.js";
7
+ export {
8
+ createDecisionHandler,
9
+ createDecisionMiddleware,
10
+ createDecisionRouter
11
+ };
@@ -0,0 +1,115 @@
1
+ import { FastifyPluginAsync, FastifyRequest, RouteHandlerMethod, FastifyReply } from 'fastify';
2
+ import { Decision, Engine, ProfileRegistry, Result } from '@criterionx/core';
3
+ import { C as CriterionResult } from './types-Dz8OTTmP.js';
4
+
5
+ /**
6
+ * Fastify plugin for Criterion decision engine
7
+ *
8
+ * @example Basic usage
9
+ * ```typescript
10
+ * import Fastify from "fastify";
11
+ * import { criterionPlugin } from "@criterionx/express/fastify";
12
+ * import { pricingDecision } from "./decisions";
13
+ *
14
+ * const app = Fastify();
15
+ *
16
+ * app.register(criterionPlugin, {
17
+ * decisions: [pricingDecision],
18
+ * profiles: { pricing: { basePrice: 100 } },
19
+ * });
20
+ *
21
+ * app.listen({ port: 3000 });
22
+ * // POST /decisions/pricing
23
+ * ```
24
+ */
25
+
26
+ declare module "fastify" {
27
+ interface FastifyRequest {
28
+ criterion?: CriterionResult<any>;
29
+ }
30
+ }
31
+ /**
32
+ * Options for Fastify Criterion plugin
33
+ */
34
+ interface CriterionPluginOptions<TProfile> {
35
+ /** Decisions to register */
36
+ decisions: Array<Decision<any, any, TProfile>>;
37
+ /** Profile map by decision ID */
38
+ profiles?: Record<string, TProfile>;
39
+ /** Engine instance */
40
+ engine?: Engine;
41
+ /** Profile registry */
42
+ registry?: ProfileRegistry<TProfile>;
43
+ /** Route prefix (default: "/decisions") */
44
+ prefix?: string;
45
+ }
46
+ /**
47
+ * Fastify plugin that registers decision endpoints
48
+ *
49
+ * @example With custom prefix
50
+ * ```typescript
51
+ * app.register(criterionPlugin, {
52
+ * decisions: [pricingDecision],
53
+ * profiles: { pricing: { basePrice: 100 } },
54
+ * prefix: "/api/v1/evaluate",
55
+ * });
56
+ * // POST /api/v1/evaluate/pricing
57
+ * ```
58
+ */
59
+ declare const criterionPlugin: FastifyPluginAsync<CriterionPluginOptions<unknown>>;
60
+ /**
61
+ * Options for creating a decision route handler
62
+ */
63
+ interface DecisionRouteOptions<TInput, TOutput, TProfile> {
64
+ /** The decision to evaluate */
65
+ decision: Decision<TInput, TOutput, TProfile>;
66
+ /** Engine instance */
67
+ engine?: Engine;
68
+ /** Profile registry */
69
+ registry?: ProfileRegistry<TProfile>;
70
+ /** Extract input from request */
71
+ getInput?: (request: FastifyRequest) => TInput;
72
+ /** Get profile or profile ID */
73
+ getProfile?: (request: FastifyRequest) => TProfile | string;
74
+ /** Custom response formatter */
75
+ formatResponse?: (result: Result<TOutput>) => unknown;
76
+ }
77
+ /**
78
+ * Create a Fastify route handler for a decision
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { createDecisionRoute } from "@criterionx/express/fastify";
83
+ *
84
+ * app.post("/pricing", createDecisionRoute({
85
+ * decision: pricingDecision,
86
+ * getProfile: () => ({ basePrice: 100 }),
87
+ * }));
88
+ * ```
89
+ */
90
+ declare function createDecisionRoute<TInput, TOutput, TProfile>(options: DecisionRouteOptions<TInput, TOutput, TProfile>): RouteHandlerMethod;
91
+ /**
92
+ * Fastify preHandler hook for decision evaluation
93
+ *
94
+ * Evaluates a decision and attaches the result to the request,
95
+ * allowing the route handler to access it.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { createDecisionHook } from "@criterionx/express/fastify";
100
+ *
101
+ * app.post("/pricing", {
102
+ * preHandler: createDecisionHook({
103
+ * decision: pricingDecision,
104
+ * getProfile: () => ({ basePrice: 100 }),
105
+ * }),
106
+ * handler: (request, reply) => {
107
+ * const { result } = request.criterion!;
108
+ * return { price: result.data?.price, meta: { custom: true } };
109
+ * },
110
+ * });
111
+ * ```
112
+ */
113
+ declare function createDecisionHook<TInput, TOutput, TProfile>(options: Omit<DecisionRouteOptions<TInput, TOutput, TProfile>, "formatResponse">): (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
114
+
115
+ export { type CriterionPluginOptions, type DecisionRouteOptions, createDecisionHook, createDecisionRoute, criterionPlugin };
@@ -0,0 +1,11 @@
1
+ import {
2
+ createDecisionHook,
3
+ createDecisionRoute,
4
+ criterionPlugin
5
+ } from "./chunk-Y5OYMKPU.js";
6
+ import "./chunk-3RG5ZIWI.js";
7
+ export {
8
+ createDecisionHook,
9
+ createDecisionRoute,
10
+ criterionPlugin
11
+ };
@@ -0,0 +1,6 @@
1
+ export { C as CriterionResult, D as DecisionMiddlewareOptions } from './types-Dz8OTTmP.js';
2
+ export { createDecisionHandler, createDecisionMiddleware, createDecisionRouter } from './express.js';
3
+ export { CriterionPluginOptions, DecisionRouteOptions, createDecisionHook, createDecisionRoute, criterionPlugin } from './fastify.js';
4
+ import '@criterionx/core';
5
+ import 'express';
6
+ import 'fastify';
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ import {
2
+ createDecisionHandler,
3
+ createDecisionMiddleware,
4
+ createDecisionRouter
5
+ } from "./chunk-YSMNRLEG.js";
6
+ import {
7
+ createDecisionHook,
8
+ createDecisionRoute,
9
+ criterionPlugin
10
+ } from "./chunk-Y5OYMKPU.js";
11
+ import "./chunk-3RG5ZIWI.js";
12
+ export {
13
+ createDecisionHandler,
14
+ createDecisionHook,
15
+ createDecisionMiddleware,
16
+ createDecisionRoute,
17
+ createDecisionRouter,
18
+ criterionPlugin
19
+ };
@@ -0,0 +1,31 @@
1
+ import { Result, Decision, Engine, ProfileRegistry } from '@criterionx/core';
2
+
3
+ /**
4
+ * Options for creating decision middleware
5
+ */
6
+ interface DecisionMiddlewareOptions<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
+ /** Extract input from request (default: req.body) */
14
+ getInput?: (req: unknown) => TInput;
15
+ /** Get profile or profile ID (default: req.query.profile or req.body.profile) */
16
+ getProfile?: (req: unknown) => TProfile | string;
17
+ /** Custom response formatter */
18
+ formatResponse?: (result: Result<TOutput>) => unknown;
19
+ /** Custom error handler */
20
+ onError?: (error: Error, req: unknown, res: unknown) => void;
21
+ }
22
+ /**
23
+ * Criterion middleware result attached to request
24
+ */
25
+ interface CriterionResult<TOutput> {
26
+ result: Result<TOutput>;
27
+ decision: string;
28
+ evaluatedAt: string;
29
+ }
30
+
31
+ export type { CriterionResult as C, DecisionMiddlewareOptions as D };
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@criterionx/express",
3
+ "version": "0.3.5",
4
+ "description": "Express and Fastify middleware for Criterion decision engine",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./express": {
14
+ "types": "./dist/express.d.ts",
15
+ "import": "./dist/express.js"
16
+ },
17
+ "./fastify": {
18
+ "types": "./dist/fastify.d.ts",
19
+ "import": "./dist/fastify.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "dependencies": {
26
+ "@criterionx/core": "0.3.5"
27
+ },
28
+ "peerDependencies": {
29
+ "express": ">=4.0.0",
30
+ "fastify": ">=4.0.0"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "express": {
34
+ "optional": true
35
+ },
36
+ "fastify": {
37
+ "optional": true
38
+ }
39
+ },
40
+ "devDependencies": {
41
+ "zod": "^3.24.0",
42
+ "@types/express": "^5.0.0",
43
+ "@types/node": "^20.0.0",
44
+ "@types/supertest": "^6.0.0",
45
+ "express": "^5.0.0",
46
+ "fastify": "^5.0.0",
47
+ "supertest": "^7.0.0",
48
+ "tsup": "^8.0.0",
49
+ "typescript": "^5.3.0",
50
+ "vitest": "^4.0.0"
51
+ },
52
+ "keywords": [
53
+ "criterion",
54
+ "decision",
55
+ "rules",
56
+ "express",
57
+ "fastify",
58
+ "middleware"
59
+ ],
60
+ "license": "MIT",
61
+ "repository": {
62
+ "type": "git",
63
+ "url": "https://github.com/tomymaritano/criterionx.git",
64
+ "directory": "packages/express"
65
+ },
66
+ "scripts": {
67
+ "build": "tsup src/index.ts src/express.ts src/fastify.ts --format esm --dts --clean",
68
+ "test": "vitest run",
69
+ "typecheck": "tsc --noEmit"
70
+ }
71
+ }