@middlewr/contracts 0.0.27 → 0.0.29
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/dist/cjs/index.d.ts +961 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +314 -219
- package/dist/cjs/links.schema.d.ts +320 -0
- package/dist/cjs/links.schema.d.ts.map +1 -1
- package/dist/cjs/rules.schema.d.ts +151 -0
- package/dist/cjs/rules.schema.d.ts.map +1 -0
- package/dist/esm/index.d.ts +961 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +312 -219
- package/dist/esm/links.schema.d.ts +320 -0
- package/dist/esm/links.schema.d.ts.map +1 -1
- package/dist/esm/rules.schema.d.ts +151 -0
- package/dist/esm/rules.schema.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/links.schema.ts +72 -55
- package/src/rules.schema.ts +98 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const RULE_OPERATORS = ['eq', 'neq', 'in', 'not_in', 'contains', 'not_contains', 'gt', 'lt', 'gte', 'lte', 'regex'] as const;
|
|
4
|
+
const VALID_FIELD_PREFIXES = ['cf.', 'request.', 'edge.', 'time.'];
|
|
5
|
+
|
|
6
|
+
const PositionSchema = z.object({
|
|
7
|
+
x: z.number(),
|
|
8
|
+
y: z.number(),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const ConditionNodeSchema = z.object({
|
|
12
|
+
id: z.string(),
|
|
13
|
+
type: z.literal('condition'),
|
|
14
|
+
position: PositionSchema,
|
|
15
|
+
field: z.string().refine((f) => VALID_FIELD_PREFIXES.some((p) => f.startsWith(p)), {
|
|
16
|
+
message: 'Field must start with cf., request., edge., or time.',
|
|
17
|
+
}),
|
|
18
|
+
operator: z.enum(RULE_OPERATORS),
|
|
19
|
+
value: z.union([z.string(), z.array(z.string()), z.number()]),
|
|
20
|
+
outputs: z.object({
|
|
21
|
+
true: z.string().nullable(),
|
|
22
|
+
false: z.string().nullable(),
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const SplitNodeSchema = z.object({
|
|
27
|
+
id: z.string(),
|
|
28
|
+
type: z.literal('split'),
|
|
29
|
+
position: PositionSchema,
|
|
30
|
+
variants: z
|
|
31
|
+
.array(
|
|
32
|
+
z.object({
|
|
33
|
+
weight: z.number().min(1).max(100),
|
|
34
|
+
target: z.string().nullable(),
|
|
35
|
+
}),
|
|
36
|
+
)
|
|
37
|
+
.min(2),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const DestinationNodeSchema = z.object({
|
|
41
|
+
id: z.string(),
|
|
42
|
+
type: z.literal('destination'),
|
|
43
|
+
position: PositionSchema,
|
|
44
|
+
url: z.string().url(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const FORBIDDEN_HEADERS = ['set-cookie', 'authorization', 'host', 'transfer-encoding'];
|
|
48
|
+
|
|
49
|
+
const TransformNodeSchema = z.object({
|
|
50
|
+
id: z.string(),
|
|
51
|
+
type: z.literal('transform'),
|
|
52
|
+
position: PositionSchema,
|
|
53
|
+
transforms: z.object({
|
|
54
|
+
query_params: z
|
|
55
|
+
.array(
|
|
56
|
+
z.object({
|
|
57
|
+
key: z.string().min(1),
|
|
58
|
+
value: z.string(),
|
|
59
|
+
mode: z.enum(['set', 'append']),
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
.optional(),
|
|
63
|
+
redirect_status: z.union([z.literal(301), z.literal(302), z.literal(307), z.literal(308)]).optional(),
|
|
64
|
+
response_headers: z
|
|
65
|
+
.array(
|
|
66
|
+
z.object({
|
|
67
|
+
key: z
|
|
68
|
+
.string()
|
|
69
|
+
.min(1)
|
|
70
|
+
.refine((k) => !FORBIDDEN_HEADERS.includes(k.toLowerCase()), {
|
|
71
|
+
message: 'This header is not allowed',
|
|
72
|
+
}),
|
|
73
|
+
value: z.string().regex(/^[^\r\n]*$/, 'Header value must not contain newlines'),
|
|
74
|
+
mode: z.enum(['set', 'append']),
|
|
75
|
+
}),
|
|
76
|
+
)
|
|
77
|
+
.optional(),
|
|
78
|
+
}),
|
|
79
|
+
next: z.string().nullable(),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
export const RuleGraphNodeSchema = z.discriminatedUnion('type', [
|
|
83
|
+
ConditionNodeSchema,
|
|
84
|
+
SplitNodeSchema,
|
|
85
|
+
DestinationNodeSchema,
|
|
86
|
+
TransformNodeSchema,
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
export const RuleGraphSchema = z
|
|
90
|
+
.object({
|
|
91
|
+
entry: z.string(),
|
|
92
|
+
nodes: z.record(z.string(), RuleGraphNodeSchema),
|
|
93
|
+
})
|
|
94
|
+
.refine((data) => data.nodes[data.entry] !== undefined, {
|
|
95
|
+
message: 'Entry node must reference an existing node in the graph',
|
|
96
|
+
path: ['entry'],
|
|
97
|
+
})
|
|
98
|
+
.nullable();
|