@criterionx/server 0.3.0
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/README.md +120 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +389 -0
- package/package.json +68 -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/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# @criterionx/server
|
|
2
|
+
|
|
3
|
+
HTTP server for Criterion decisions with auto-generated documentation.
|
|
4
|
+
|
|
5
|
+
> **The server is a delivery mechanism, not a decision engine.**
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @criterionx/server @criterionx/core zod
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { createServer } from "@criterionx/server";
|
|
17
|
+
import { defineDecision } from "@criterionx/core";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
|
|
20
|
+
const riskDecision = defineDecision({
|
|
21
|
+
id: "transaction-risk",
|
|
22
|
+
version: "1.0.0",
|
|
23
|
+
inputSchema: z.object({ amount: z.number() }),
|
|
24
|
+
outputSchema: z.object({ risk: z.enum(["HIGH", "LOW"]) }),
|
|
25
|
+
profileSchema: z.object({ threshold: z.number() }),
|
|
26
|
+
rules: [
|
|
27
|
+
{
|
|
28
|
+
id: "high-risk",
|
|
29
|
+
when: (input, profile) => input.amount > profile.threshold,
|
|
30
|
+
emit: () => ({ risk: "HIGH" }),
|
|
31
|
+
explain: (input, profile) =>
|
|
32
|
+
`Amount ${input.amount} exceeds threshold ${profile.threshold}`,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "low-risk",
|
|
36
|
+
when: () => true,
|
|
37
|
+
emit: () => ({ risk: "LOW" }),
|
|
38
|
+
explain: () => "Amount within acceptable range",
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const server = createServer({
|
|
44
|
+
decisions: [riskDecision],
|
|
45
|
+
profiles: {
|
|
46
|
+
"transaction-risk": { threshold: 10000 },
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
server.listen(3000);
|
|
51
|
+
// Server running at http://localhost:3000
|
|
52
|
+
// Docs at http://localhost:3000/docs
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Endpoints
|
|
56
|
+
|
|
57
|
+
| Method | Path | Description |
|
|
58
|
+
|--------|------|-------------|
|
|
59
|
+
| GET | `/` | Health check |
|
|
60
|
+
| GET | `/docs` | Interactive documentation UI |
|
|
61
|
+
| GET | `/decisions` | List all registered decisions |
|
|
62
|
+
| GET | `/decisions/:id/schema` | JSON Schema for decision |
|
|
63
|
+
| POST | `/decisions/:id` | Evaluate a decision |
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `createServer(options)`
|
|
68
|
+
|
|
69
|
+
Creates a new Criterion server.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
interface ServerOptions<TDecisions> {
|
|
73
|
+
decisions: TDecisions[];
|
|
74
|
+
profiles?: Record<string, unknown>;
|
|
75
|
+
port?: number;
|
|
76
|
+
cors?: boolean;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### POST `/decisions/:id`
|
|
81
|
+
|
|
82
|
+
Evaluate a decision with the given input.
|
|
83
|
+
|
|
84
|
+
**Request:**
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"input": { "amount": 15000 },
|
|
88
|
+
"profile": { "threshold": 10000 }
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Response:**
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"status": "OK",
|
|
96
|
+
"data": { "risk": "HIGH" },
|
|
97
|
+
"meta": {
|
|
98
|
+
"decisionId": "transaction-risk",
|
|
99
|
+
"decisionVersion": "1.0.0",
|
|
100
|
+
"matchedRule": "high-risk",
|
|
101
|
+
"explanation": "Amount 15000 exceeds threshold 10000",
|
|
102
|
+
"evaluatedAt": "2024-12-29T12:00:00.000Z"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Design Principles
|
|
108
|
+
|
|
109
|
+
1. **Server does NOT add decision logic** — only calls `engine.run()`
|
|
110
|
+
2. **Decisions are explicitly registered** — no auto-discovery
|
|
111
|
+
3. **UI cannot invent defaults** — shows exactly what's required
|
|
112
|
+
4. **JSON Schema is primary** — OpenAPI is derived
|
|
113
|
+
|
|
114
|
+
## Documentation
|
|
115
|
+
|
|
116
|
+
Full documentation: [https://tomymaritano.github.io/criterionx/](https://tomymaritano.github.io/criterionx/)
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Decision } from '@criterionx/core';
|
|
2
|
+
import { ZodSchema } from 'zod';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Server configuration options
|
|
7
|
+
*/
|
|
8
|
+
interface ServerOptions {
|
|
9
|
+
/** Decisions to expose via HTTP */
|
|
10
|
+
decisions: Decision<any, any, any>[];
|
|
11
|
+
/** Default profiles for decisions (keyed by decision ID) */
|
|
12
|
+
profiles?: Record<string, unknown>;
|
|
13
|
+
/** Enable CORS (default: true) */
|
|
14
|
+
cors?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Request body for decision evaluation
|
|
18
|
+
*/
|
|
19
|
+
interface EvaluateRequest {
|
|
20
|
+
/** Input data for the decision */
|
|
21
|
+
input: unknown;
|
|
22
|
+
/** Profile to use (overrides default) */
|
|
23
|
+
profile?: unknown;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Decision info for listing
|
|
27
|
+
*/
|
|
28
|
+
interface DecisionInfo {
|
|
29
|
+
id: string;
|
|
30
|
+
version: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
meta?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* JSON Schema representation
|
|
36
|
+
*/
|
|
37
|
+
interface JsonSchema {
|
|
38
|
+
$schema?: string;
|
|
39
|
+
type?: string;
|
|
40
|
+
properties?: Record<string, JsonSchema>;
|
|
41
|
+
required?: string[];
|
|
42
|
+
additionalProperties?: boolean;
|
|
43
|
+
items?: JsonSchema;
|
|
44
|
+
enum?: unknown[];
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Decision schema export
|
|
49
|
+
*/
|
|
50
|
+
interface DecisionSchema {
|
|
51
|
+
id: string;
|
|
52
|
+
version: string;
|
|
53
|
+
inputSchema: JsonSchema;
|
|
54
|
+
outputSchema: JsonSchema;
|
|
55
|
+
profileSchema: JsonSchema;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Convert a Zod schema to JSON Schema
|
|
60
|
+
*/
|
|
61
|
+
declare function toJsonSchema(schema: ZodSchema): JsonSchema;
|
|
62
|
+
/**
|
|
63
|
+
* Extract JSON Schemas from a decision
|
|
64
|
+
*/
|
|
65
|
+
declare function extractDecisionSchema(decision: Decision<unknown, unknown, unknown>): DecisionSchema;
|
|
66
|
+
/**
|
|
67
|
+
* Generate OpenAPI-compatible schema for a decision endpoint
|
|
68
|
+
*/
|
|
69
|
+
declare function generateEndpointSchema(decision: Decision<unknown, unknown, unknown>): {
|
|
70
|
+
requestBody: JsonSchema;
|
|
71
|
+
response: JsonSchema;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Criterion Server
|
|
76
|
+
*
|
|
77
|
+
* Exposes decisions as HTTP endpoints with auto-generated documentation.
|
|
78
|
+
*/
|
|
79
|
+
declare class CriterionServer {
|
|
80
|
+
private app;
|
|
81
|
+
private engine;
|
|
82
|
+
private decisions;
|
|
83
|
+
private profiles;
|
|
84
|
+
constructor(options: ServerOptions);
|
|
85
|
+
private setupRoutes;
|
|
86
|
+
private generateDocsHtml;
|
|
87
|
+
/**
|
|
88
|
+
* Get the Hono app instance (for custom middleware)
|
|
89
|
+
*/
|
|
90
|
+
get handler(): Hono;
|
|
91
|
+
/**
|
|
92
|
+
* Start the server
|
|
93
|
+
*/
|
|
94
|
+
listen(port?: number): void;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create a new Criterion server
|
|
98
|
+
*/
|
|
99
|
+
declare function createServer(options: ServerOptions): CriterionServer;
|
|
100
|
+
|
|
101
|
+
export { CriterionServer, type DecisionInfo, type DecisionSchema, type EvaluateRequest, type JsonSchema, type ServerOptions, createServer, extractDecisionSchema, generateEndpointSchema, toJsonSchema };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// src/schema.ts
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
+
function toJsonSchema(schema) {
|
|
4
|
+
return zodToJsonSchema(schema, { $refStrategy: "none" });
|
|
5
|
+
}
|
|
6
|
+
function extractDecisionSchema(decision) {
|
|
7
|
+
return {
|
|
8
|
+
id: decision.id,
|
|
9
|
+
version: decision.version,
|
|
10
|
+
inputSchema: toJsonSchema(decision.inputSchema),
|
|
11
|
+
outputSchema: toJsonSchema(decision.outputSchema),
|
|
12
|
+
profileSchema: toJsonSchema(decision.profileSchema)
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function generateEndpointSchema(decision) {
|
|
16
|
+
const inputSchema = toJsonSchema(decision.inputSchema);
|
|
17
|
+
const profileSchema = toJsonSchema(decision.profileSchema);
|
|
18
|
+
return {
|
|
19
|
+
requestBody: {
|
|
20
|
+
type: "object",
|
|
21
|
+
properties: {
|
|
22
|
+
input: inputSchema,
|
|
23
|
+
profile: profileSchema
|
|
24
|
+
},
|
|
25
|
+
required: ["input"]
|
|
26
|
+
},
|
|
27
|
+
response: {
|
|
28
|
+
type: "object",
|
|
29
|
+
properties: {
|
|
30
|
+
status: {
|
|
31
|
+
type: "string",
|
|
32
|
+
enum: ["OK", "NO_MATCH", "INVALID_INPUT", "INVALID_OUTPUT"]
|
|
33
|
+
},
|
|
34
|
+
data: toJsonSchema(decision.outputSchema),
|
|
35
|
+
meta: {
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: {
|
|
38
|
+
decisionId: { type: "string" },
|
|
39
|
+
decisionVersion: { type: "string" },
|
|
40
|
+
profileId: { type: "string" },
|
|
41
|
+
matchedRule: { type: "string" },
|
|
42
|
+
explanation: { type: "string" },
|
|
43
|
+
evaluatedAt: { type: "string", format: "date-time" }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
required: ["status", "data", "meta"]
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/server.ts
|
|
53
|
+
import { Hono } from "hono";
|
|
54
|
+
import { cors } from "hono/cors";
|
|
55
|
+
import { serve } from "@hono/node-server";
|
|
56
|
+
import { Engine } from "@criterionx/core";
|
|
57
|
+
var CriterionServer = class {
|
|
58
|
+
app;
|
|
59
|
+
engine;
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
decisions;
|
|
62
|
+
profiles;
|
|
63
|
+
constructor(options) {
|
|
64
|
+
this.app = new Hono();
|
|
65
|
+
this.engine = new Engine();
|
|
66
|
+
this.decisions = /* @__PURE__ */ new Map();
|
|
67
|
+
this.profiles = /* @__PURE__ */ new Map();
|
|
68
|
+
for (const decision of options.decisions) {
|
|
69
|
+
this.decisions.set(decision.id, decision);
|
|
70
|
+
}
|
|
71
|
+
if (options.profiles) {
|
|
72
|
+
for (const [id, profile] of Object.entries(options.profiles)) {
|
|
73
|
+
this.profiles.set(id, profile);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (options.cors !== false) {
|
|
77
|
+
this.app.use("*", cors());
|
|
78
|
+
}
|
|
79
|
+
this.setupRoutes();
|
|
80
|
+
}
|
|
81
|
+
setupRoutes() {
|
|
82
|
+
this.app.get("/", (c) => {
|
|
83
|
+
return c.json({
|
|
84
|
+
name: "Criterion Server",
|
|
85
|
+
version: "0.1.0",
|
|
86
|
+
decisions: this.decisions.size
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
this.app.get("/decisions", (c) => {
|
|
90
|
+
const decisions = [];
|
|
91
|
+
for (const decision of this.decisions.values()) {
|
|
92
|
+
decisions.push({
|
|
93
|
+
id: decision.id,
|
|
94
|
+
version: decision.version,
|
|
95
|
+
description: decision.meta?.description,
|
|
96
|
+
meta: decision.meta
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return c.json({ decisions });
|
|
100
|
+
});
|
|
101
|
+
this.app.get("/decisions/:id/schema", (c) => {
|
|
102
|
+
const id = c.req.param("id");
|
|
103
|
+
const decision = this.decisions.get(id);
|
|
104
|
+
if (!decision) {
|
|
105
|
+
return c.json({ error: `Decision not found: ${id}` }, 404);
|
|
106
|
+
}
|
|
107
|
+
const schema = extractDecisionSchema(decision);
|
|
108
|
+
return c.json(schema);
|
|
109
|
+
});
|
|
110
|
+
this.app.get("/decisions/:id/endpoint-schema", (c) => {
|
|
111
|
+
const id = c.req.param("id");
|
|
112
|
+
const decision = this.decisions.get(id);
|
|
113
|
+
if (!decision) {
|
|
114
|
+
return c.json({ error: `Decision not found: ${id}` }, 404);
|
|
115
|
+
}
|
|
116
|
+
const schema = generateEndpointSchema(decision);
|
|
117
|
+
return c.json(schema);
|
|
118
|
+
});
|
|
119
|
+
this.app.post("/decisions/:id", async (c) => {
|
|
120
|
+
const id = c.req.param("id");
|
|
121
|
+
const decision = this.decisions.get(id);
|
|
122
|
+
if (!decision) {
|
|
123
|
+
return c.json({ error: `Decision not found: ${id}` }, 404);
|
|
124
|
+
}
|
|
125
|
+
let body;
|
|
126
|
+
try {
|
|
127
|
+
body = await c.req.json();
|
|
128
|
+
} catch {
|
|
129
|
+
return c.json({ error: "Invalid JSON body" }, 400);
|
|
130
|
+
}
|
|
131
|
+
if (body.input === void 0) {
|
|
132
|
+
return c.json({ error: "Missing 'input' in request body" }, 400);
|
|
133
|
+
}
|
|
134
|
+
let profile = body.profile;
|
|
135
|
+
if (!profile) {
|
|
136
|
+
profile = this.profiles.get(id);
|
|
137
|
+
if (!profile) {
|
|
138
|
+
return c.json(
|
|
139
|
+
{
|
|
140
|
+
error: `No profile provided and no default profile for decision: ${id}`
|
|
141
|
+
},
|
|
142
|
+
400
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const result = this.engine.run(decision, body.input, { profile });
|
|
147
|
+
const statusCode = result.status === "OK" ? 200 : 400;
|
|
148
|
+
return c.json(result, statusCode);
|
|
149
|
+
});
|
|
150
|
+
this.app.get("/docs", (c) => {
|
|
151
|
+
const decisions = Array.from(this.decisions.values()).map((d) => ({
|
|
152
|
+
id: d.id,
|
|
153
|
+
version: d.version,
|
|
154
|
+
description: d.meta?.description
|
|
155
|
+
}));
|
|
156
|
+
const html = this.generateDocsHtml(decisions);
|
|
157
|
+
return c.html(html);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
generateDocsHtml(decisions) {
|
|
161
|
+
return `<!DOCTYPE html>
|
|
162
|
+
<html lang="en">
|
|
163
|
+
<head>
|
|
164
|
+
<meta charset="UTF-8">
|
|
165
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
166
|
+
<title>Criterion API Docs</title>
|
|
167
|
+
<style>
|
|
168
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
169
|
+
body {
|
|
170
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
171
|
+
line-height: 1.6;
|
|
172
|
+
color: #1a1a1a;
|
|
173
|
+
background: #f5f5f5;
|
|
174
|
+
}
|
|
175
|
+
.container { max-width: 1200px; margin: 0 auto; padding: 2rem; }
|
|
176
|
+
header {
|
|
177
|
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
178
|
+
color: white;
|
|
179
|
+
padding: 2rem;
|
|
180
|
+
margin-bottom: 2rem;
|
|
181
|
+
border-radius: 8px;
|
|
182
|
+
}
|
|
183
|
+
header h1 { font-size: 2rem; margin-bottom: 0.5rem; }
|
|
184
|
+
header p { opacity: 0.9; }
|
|
185
|
+
.decisions { display: grid; gap: 1rem; }
|
|
186
|
+
.decision {
|
|
187
|
+
background: white;
|
|
188
|
+
border-radius: 8px;
|
|
189
|
+
padding: 1.5rem;
|
|
190
|
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
|
191
|
+
}
|
|
192
|
+
.decision h2 {
|
|
193
|
+
font-size: 1.25rem;
|
|
194
|
+
color: #667eea;
|
|
195
|
+
margin-bottom: 0.5rem;
|
|
196
|
+
}
|
|
197
|
+
.decision .version {
|
|
198
|
+
font-size: 0.875rem;
|
|
199
|
+
color: #666;
|
|
200
|
+
margin-bottom: 1rem;
|
|
201
|
+
}
|
|
202
|
+
.decision .description {
|
|
203
|
+
color: #444;
|
|
204
|
+
margin-bottom: 1rem;
|
|
205
|
+
}
|
|
206
|
+
.endpoint {
|
|
207
|
+
background: #f8f9fa;
|
|
208
|
+
border-radius: 4px;
|
|
209
|
+
padding: 1rem;
|
|
210
|
+
font-family: monospace;
|
|
211
|
+
font-size: 0.875rem;
|
|
212
|
+
margin-bottom: 1rem;
|
|
213
|
+
}
|
|
214
|
+
.method {
|
|
215
|
+
background: #28a745;
|
|
216
|
+
color: white;
|
|
217
|
+
padding: 0.25rem 0.5rem;
|
|
218
|
+
border-radius: 4px;
|
|
219
|
+
margin-right: 0.5rem;
|
|
220
|
+
}
|
|
221
|
+
.playground {
|
|
222
|
+
margin-top: 1rem;
|
|
223
|
+
padding-top: 1rem;
|
|
224
|
+
border-top: 1px solid #eee;
|
|
225
|
+
}
|
|
226
|
+
textarea {
|
|
227
|
+
width: 100%;
|
|
228
|
+
min-height: 150px;
|
|
229
|
+
padding: 1rem;
|
|
230
|
+
font-family: monospace;
|
|
231
|
+
font-size: 0.875rem;
|
|
232
|
+
border: 1px solid #ddd;
|
|
233
|
+
border-radius: 4px;
|
|
234
|
+
margin-bottom: 0.5rem;
|
|
235
|
+
}
|
|
236
|
+
button {
|
|
237
|
+
background: #667eea;
|
|
238
|
+
color: white;
|
|
239
|
+
border: none;
|
|
240
|
+
padding: 0.75rem 1.5rem;
|
|
241
|
+
border-radius: 4px;
|
|
242
|
+
cursor: pointer;
|
|
243
|
+
font-size: 1rem;
|
|
244
|
+
}
|
|
245
|
+
button:hover { background: #5a6fd6; }
|
|
246
|
+
.result {
|
|
247
|
+
margin-top: 1rem;
|
|
248
|
+
padding: 1rem;
|
|
249
|
+
background: #f8f9fa;
|
|
250
|
+
border-radius: 4px;
|
|
251
|
+
font-family: monospace;
|
|
252
|
+
font-size: 0.875rem;
|
|
253
|
+
white-space: pre-wrap;
|
|
254
|
+
display: none;
|
|
255
|
+
}
|
|
256
|
+
.result.show { display: block; }
|
|
257
|
+
.result.error { background: #fff3f3; border: 1px solid #ffcccc; }
|
|
258
|
+
.result.success { background: #f0fff4; border: 1px solid #9ae6b4; }
|
|
259
|
+
.links { margin-top: 0.5rem; }
|
|
260
|
+
.links a {
|
|
261
|
+
color: #667eea;
|
|
262
|
+
text-decoration: none;
|
|
263
|
+
margin-right: 1rem;
|
|
264
|
+
font-size: 0.875rem;
|
|
265
|
+
}
|
|
266
|
+
.links a:hover { text-decoration: underline; }
|
|
267
|
+
</style>
|
|
268
|
+
</head>
|
|
269
|
+
<body>
|
|
270
|
+
<div class="container">
|
|
271
|
+
<header>
|
|
272
|
+
<h1>Criterion API</h1>
|
|
273
|
+
<p>Interactive documentation for registered decisions</p>
|
|
274
|
+
</header>
|
|
275
|
+
|
|
276
|
+
<div class="decisions">
|
|
277
|
+
${decisions.map(
|
|
278
|
+
(d) => `
|
|
279
|
+
<div class="decision" data-id="${d.id}">
|
|
280
|
+
<h2>${d.id}</h2>
|
|
281
|
+
<div class="version">v${d.version}</div>
|
|
282
|
+
${d.description ? `<div class="description">${d.description}</div>` : ""}
|
|
283
|
+
<div class="endpoint">
|
|
284
|
+
<span class="method">POST</span>
|
|
285
|
+
/decisions/${d.id}
|
|
286
|
+
</div>
|
|
287
|
+
<div class="links">
|
|
288
|
+
<a href="/decisions/${d.id}/schema" target="_blank">View Schema</a>
|
|
289
|
+
<a href="/decisions/${d.id}/endpoint-schema" target="_blank">Endpoint Schema</a>
|
|
290
|
+
</div>
|
|
291
|
+
<div class="playground">
|
|
292
|
+
<textarea id="input-${d.id}" placeholder='{"input": {...}, "profile": {...}}'></textarea>
|
|
293
|
+
<button onclick="evaluate('${d.id}')">Evaluate</button>
|
|
294
|
+
<div class="result" id="result-${d.id}"></div>
|
|
295
|
+
</div>
|
|
296
|
+
</div>
|
|
297
|
+
`
|
|
298
|
+
).join("")}
|
|
299
|
+
</div>
|
|
300
|
+
</div>
|
|
301
|
+
|
|
302
|
+
<script>
|
|
303
|
+
// Load schemas for each decision
|
|
304
|
+
document.querySelectorAll('.decision').forEach(async (el) => {
|
|
305
|
+
const id = el.dataset.id;
|
|
306
|
+
const textarea = document.getElementById('input-' + id);
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
const res = await fetch('/decisions/' + id + '/schema');
|
|
310
|
+
const schema = await res.json();
|
|
311
|
+
|
|
312
|
+
// Generate example from schema
|
|
313
|
+
const example = {
|
|
314
|
+
input: generateExample(schema.inputSchema),
|
|
315
|
+
profile: generateExample(schema.profileSchema)
|
|
316
|
+
};
|
|
317
|
+
textarea.value = JSON.stringify(example, null, 2);
|
|
318
|
+
} catch (e) {
|
|
319
|
+
console.error('Failed to load schema for', id, e);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
function generateExample(schema) {
|
|
324
|
+
if (!schema || !schema.properties) return {};
|
|
325
|
+
const obj = {};
|
|
326
|
+
for (const [key, prop] of Object.entries(schema.properties)) {
|
|
327
|
+
if (prop.type === 'string') obj[key] = prop.enum ? prop.enum[0] : 'example';
|
|
328
|
+
else if (prop.type === 'number') obj[key] = 0;
|
|
329
|
+
else if (prop.type === 'boolean') obj[key] = false;
|
|
330
|
+
else if (prop.type === 'array') obj[key] = [];
|
|
331
|
+
else if (prop.type === 'object') obj[key] = generateExample(prop);
|
|
332
|
+
}
|
|
333
|
+
return obj;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function evaluate(id) {
|
|
337
|
+
const textarea = document.getElementById('input-' + id);
|
|
338
|
+
const resultEl = document.getElementById('result-' + id);
|
|
339
|
+
|
|
340
|
+
try {
|
|
341
|
+
const body = JSON.parse(textarea.value);
|
|
342
|
+
const res = await fetch('/decisions/' + id, {
|
|
343
|
+
method: 'POST',
|
|
344
|
+
headers: { 'Content-Type': 'application/json' },
|
|
345
|
+
body: JSON.stringify(body)
|
|
346
|
+
});
|
|
347
|
+
const data = await res.json();
|
|
348
|
+
|
|
349
|
+
resultEl.textContent = JSON.stringify(data, null, 2);
|
|
350
|
+
resultEl.className = 'result show ' + (data.status === 'OK' ? 'success' : 'error');
|
|
351
|
+
} catch (e) {
|
|
352
|
+
resultEl.textContent = 'Error: ' + e.message;
|
|
353
|
+
resultEl.className = 'result show error';
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
</script>
|
|
357
|
+
</body>
|
|
358
|
+
</html>`;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Get the Hono app instance (for custom middleware)
|
|
362
|
+
*/
|
|
363
|
+
get handler() {
|
|
364
|
+
return this.app;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Start the server
|
|
368
|
+
*/
|
|
369
|
+
listen(port = 3e3) {
|
|
370
|
+
console.log(`Criterion Server starting on port ${port}...`);
|
|
371
|
+
console.log(` Decisions: ${this.decisions.size}`);
|
|
372
|
+
console.log(` Docs: http://localhost:${port}/docs`);
|
|
373
|
+
console.log(` API: http://localhost:${port}/decisions`);
|
|
374
|
+
serve({
|
|
375
|
+
fetch: this.app.fetch,
|
|
376
|
+
port
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
function createServer(options) {
|
|
381
|
+
return new CriterionServer(options);
|
|
382
|
+
}
|
|
383
|
+
export {
|
|
384
|
+
CriterionServer,
|
|
385
|
+
createServer,
|
|
386
|
+
extractDecisionSchema,
|
|
387
|
+
generateEndpointSchema,
|
|
388
|
+
toJsonSchema
|
|
389
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@criterionx/server",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "HTTP server for Criterion decisions with auto-generated docs",
|
|
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
|
+
"decision-engine",
|
|
22
|
+
"http-server",
|
|
23
|
+
"hono",
|
|
24
|
+
"rest-api",
|
|
25
|
+
"auto-docs"
|
|
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/server"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/tomymaritano/criterionx/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/tomymaritano/criterionx#readme",
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@hono/node-server": "^1.13.0",
|
|
43
|
+
"hono": "^4.6.0",
|
|
44
|
+
"zod-to-json-schema": "^3.24.0",
|
|
45
|
+
"@criterionx/core": "0.3.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"zod": "^3.22.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^22.0.0",
|
|
52
|
+
"tsup": "^8.0.0",
|
|
53
|
+
"tsx": "^4.7.0",
|
|
54
|
+
"typescript": "^5.3.0",
|
|
55
|
+
"vitest": "^2.0.0",
|
|
56
|
+
"zod": "^3.22.0"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=18"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"test:watch": "vitest",
|
|
65
|
+
"typecheck": "tsc --noEmit",
|
|
66
|
+
"dev": "tsx src/dev.ts"
|
|
67
|
+
}
|
|
68
|
+
}
|