viell-effect-schema-http 0.1.0 → 0.2.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/README.md +43 -29
- package/dist/http/Body.d.ts +23 -13
- package/dist/http/Body.d.ts.map +1 -1
- package/dist/http/Body.js +18 -8
- package/dist/http/Body.js.map +1 -1
- package/dist/http/Operation.d.ts +270 -13
- package/dist/http/Operation.d.ts.map +1 -1
- package/dist/http/Operation.js +210 -11
- package/dist/http/Operation.js.map +1 -1
- package/dist/http/Policy.d.ts +3 -2
- package/dist/http/Policy.d.ts.map +1 -1
- package/dist/http/Policy.js +3 -2
- package/dist/http/Policy.js.map +1 -1
- package/dist/http/Request.d.ts +477 -85
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +621 -141
- package/dist/http/Request.js.map +1 -1
- package/dist/http/Response.d.ts +119 -5
- package/dist/http/Response.d.ts.map +1 -1
- package/dist/http/Response.js +88 -7
- package/dist/http/Response.js.map +1 -1
- package/dist/http/UrlEncoding.d.ts +23 -7
- package/dist/http/UrlEncoding.d.ts.map +1 -1
- package/dist/http/UrlEncoding.js +7 -3
- package/dist/http/UrlEncoding.js.map +1 -1
- package/dist/http/internal/fields.d.ts +17 -4
- package/dist/http/internal/fields.d.ts.map +1 -1
- package/dist/http/internal/fields.js +11 -6
- package/dist/http/internal/fields.js.map +1 -1
- package/dist/http/internal/input.d.ts +39 -0
- package/dist/http/internal/input.d.ts.map +1 -0
- package/dist/http/internal/input.js +40 -0
- package/dist/http/internal/input.js.map +1 -0
- package/dist/http/internal/shape.d.ts +44 -0
- package/dist/http/internal/shape.d.ts.map +1 -0
- package/dist/http/internal/shape.js +179 -0
- package/dist/http/internal/shape.js.map +1 -0
- package/package.json +12 -12
- package/src/http/Body.ts +39 -18
- package/src/http/Operation.ts +468 -33
- package/src/http/Policy.ts +3 -2
- package/src/http/Request.ts +1075 -251
- package/src/http/Response.ts +235 -22
- package/src/http/UrlEncoding.ts +31 -8
- package/src/http/internal/fields.ts +37 -9
- package/src/http/internal/input.ts +123 -0
- package/src/http/internal/shape.ts +279 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
|
+
import type { Fold } from "./fields.js";
|
|
3
|
+
|
|
4
|
+
/** What one schema says about its field group. Names are wire names, in their compared form. */
|
|
5
|
+
export interface Shape {
|
|
6
|
+
/** The names the schema states. */
|
|
7
|
+
readonly names: ReadonlySet<string>;
|
|
8
|
+
/** Whether it allows names it does not state, as a record or a list of pairs does. */
|
|
9
|
+
readonly open: boolean;
|
|
10
|
+
/** The names it always writes, so a raw edit of one could never reach the client. */
|
|
11
|
+
readonly required: ReadonlySet<string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** What a schema's encoded side states, in the schema's own spelling. */
|
|
15
|
+
interface Reading {
|
|
16
|
+
/** Each stated name, with its encoded type in every union member that states it. */
|
|
17
|
+
readonly fields: ReadonlyMap<string, ReadonlyArray<SchemaAST.AST>>;
|
|
18
|
+
/** The encoded types of names a record or a rest allows. */
|
|
19
|
+
readonly rest: ReadonlyArray<SchemaAST.AST>;
|
|
20
|
+
readonly open: boolean;
|
|
21
|
+
readonly required: ReadonlySet<string>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const holdsUndefined = (ast: SchemaAST.AST): boolean =>
|
|
25
|
+
ast._tag === "Undefined" ||
|
|
26
|
+
ast._tag === "Void" ||
|
|
27
|
+
(ast._tag === "Union" && ast.types.some(holdsUndefined));
|
|
28
|
+
|
|
29
|
+
const readObjects = (ast: SchemaAST.Objects): Reading => {
|
|
30
|
+
const stated = ast.propertySignatures.map(
|
|
31
|
+
({ name, type }) => [String(name), type] as const,
|
|
32
|
+
);
|
|
33
|
+
return {
|
|
34
|
+
fields: new Map(stated.map(([name, type]) => [name, [type]])),
|
|
35
|
+
rest: ast.indexSignatures.map(({ type }) => type),
|
|
36
|
+
open: ast.indexSignatures.length > 0,
|
|
37
|
+
required: new Set(
|
|
38
|
+
stated
|
|
39
|
+
.filter(
|
|
40
|
+
([, type]) => !SchemaAST.isOptional(type) && !holdsUndefined(type),
|
|
41
|
+
)
|
|
42
|
+
.map(([name]) => name),
|
|
43
|
+
),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A union allows what any member allows and always writes what every member writes. */
|
|
48
|
+
const readUnion = (members: ReadonlyArray<Reading>): Reading => {
|
|
49
|
+
const fields = new Map<string, ReadonlyArray<SchemaAST.AST>>();
|
|
50
|
+
for (const member of members)
|
|
51
|
+
for (const [name, types] of member.fields)
|
|
52
|
+
fields.set(name, [...(fields.get(name) ?? []), ...types]);
|
|
53
|
+
const [first, ...others] = members;
|
|
54
|
+
return {
|
|
55
|
+
fields,
|
|
56
|
+
rest: members.flatMap(({ rest }) => rest),
|
|
57
|
+
open: members.some(({ open }) => open),
|
|
58
|
+
required: new Set(
|
|
59
|
+
Array.from(first?.required ?? []).filter((name) =>
|
|
60
|
+
others.every(({ required }) => required.has(name)),
|
|
61
|
+
),
|
|
62
|
+
),
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** Read a schema's encoded AST. Where names cannot be read, the schema allows any name. */
|
|
67
|
+
const read = (ast: SchemaAST.AST): Reading =>
|
|
68
|
+
ast._tag === "Objects"
|
|
69
|
+
? readObjects(ast)
|
|
70
|
+
: ast._tag === "Union"
|
|
71
|
+
? readUnion(ast.types.map(read))
|
|
72
|
+
: { fields: new Map(), rest: [], open: true, required: new Set() };
|
|
73
|
+
|
|
74
|
+
const takesList = (ast: SchemaAST.AST): boolean =>
|
|
75
|
+
ast._tag === "Arrays" || (ast._tag === "Union" && ast.types.some(takesList));
|
|
76
|
+
|
|
77
|
+
/** The single values a field's encoded type takes, looking inside unions and lists. */
|
|
78
|
+
const scalars = (ast: SchemaAST.AST): ReadonlyArray<SchemaAST.AST> =>
|
|
79
|
+
ast._tag === "Union"
|
|
80
|
+
? ast.types.flatMap(scalars)
|
|
81
|
+
: ast._tag === "Arrays"
|
|
82
|
+
? [...ast.elements, ...ast.rest].flatMap(scalars)
|
|
83
|
+
: [ast];
|
|
84
|
+
|
|
85
|
+
type TextReader = (text: string) => unknown;
|
|
86
|
+
|
|
87
|
+
const readNumber: TextReader = (text) => {
|
|
88
|
+
if (text === "NaN") return Number.NaN;
|
|
89
|
+
const value = Number(text);
|
|
90
|
+
return text.trim() === "" || Number.isNaN(value) ? text : value;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const readBoolean: TextReader = (text) =>
|
|
94
|
+
text === "true" ? true : text === "false" ? false : text;
|
|
95
|
+
|
|
96
|
+
const readBigInt: TextReader = (text) =>
|
|
97
|
+
/^-?\d+$/.test(text) ? BigInt(text) : text;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Text reads as `Schema.DateFromString` reads it. A declaration need not refuse an invalid `Date`
|
|
101
|
+
* as `Schema.Date` does, so text with no date in it stays text for the schema to refuse.
|
|
102
|
+
*/
|
|
103
|
+
const readDate = (ast: SchemaAST.Declaration): TextReader => {
|
|
104
|
+
const takesDate = Schema.is(Schema.make<Schema.Top>(ast));
|
|
105
|
+
return (text) => {
|
|
106
|
+
const date = new Date(text);
|
|
107
|
+
return !Number.isNaN(date.getTime()) && takesDate(date) ? date : text;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const readerFor = (ast: SchemaAST.AST): ReadonlyArray<TextReader> => {
|
|
112
|
+
switch (ast._tag) {
|
|
113
|
+
case "Declaration":
|
|
114
|
+
return [readDate(ast)];
|
|
115
|
+
case "Number":
|
|
116
|
+
return [readNumber];
|
|
117
|
+
case "Boolean":
|
|
118
|
+
return [readBoolean];
|
|
119
|
+
case "BigInt":
|
|
120
|
+
return [readBigInt];
|
|
121
|
+
case "Literal":
|
|
122
|
+
return typeof ast.literal === "number"
|
|
123
|
+
? [readNumber]
|
|
124
|
+
: typeof ast.literal === "boolean"
|
|
125
|
+
? [readBoolean]
|
|
126
|
+
: typeof ast.literal === "bigint"
|
|
127
|
+
? [readBigInt]
|
|
128
|
+
: [];
|
|
129
|
+
case "Enum":
|
|
130
|
+
return ast.enums.some(([, value]) => typeof value === "number")
|
|
131
|
+
? [readNumber]
|
|
132
|
+
: [];
|
|
133
|
+
default:
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Read wire text as the value a field encodes to, the reverse of how the library writes it. A field
|
|
140
|
+
* that can hold text takes it as it is, and text that reads as nothing else stays text for the
|
|
141
|
+
* schema to refuse.
|
|
142
|
+
*/
|
|
143
|
+
const readText = (
|
|
144
|
+
types: ReadonlyArray<SchemaAST.AST>,
|
|
145
|
+
text: string,
|
|
146
|
+
): unknown => {
|
|
147
|
+
const forms = types.flatMap(scalars);
|
|
148
|
+
if (forms.some(({ _tag }) => _tag === "String")) return text;
|
|
149
|
+
for (const reader of forms.flatMap(readerFor)) {
|
|
150
|
+
const value = reader(text);
|
|
151
|
+
if (typeof value !== "string") return value;
|
|
152
|
+
}
|
|
153
|
+
return text;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** Whether a shape allows a name, given in its compared form. */
|
|
157
|
+
export const allows = (shape: Shape, name: string): boolean =>
|
|
158
|
+
shape.open || shape.names.has(name);
|
|
159
|
+
|
|
160
|
+
/** Declared names no bound schema allows. With no schema there is no shape to meet. */
|
|
161
|
+
export const outside = (
|
|
162
|
+
names: ReadonlyArray<string>,
|
|
163
|
+
shapes: ReadonlyArray<Shape>,
|
|
164
|
+
fold: Fold,
|
|
165
|
+
): ReadonlyArray<string> =>
|
|
166
|
+
shapes.length === 0
|
|
167
|
+
? []
|
|
168
|
+
: names.filter(
|
|
169
|
+
(name) => !shapes.some((shape) => allows(shape, fold(name))),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
/** The edited names some bound schema always writes. */
|
|
173
|
+
export const required = (
|
|
174
|
+
names: ReadonlyArray<string>,
|
|
175
|
+
shapes: ReadonlyArray<Shape>,
|
|
176
|
+
fold: Fold,
|
|
177
|
+
): ReadonlyArray<string> =>
|
|
178
|
+
names.filter((name) =>
|
|
179
|
+
shapes.some((shape) => shape.required.has(fold(name))),
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
/** The names a schema's output holds a value under, as the schema spells them. */
|
|
183
|
+
const writtenNames = (output: object): ReadonlyArray<string> =>
|
|
184
|
+
(Symbol.iterator in output
|
|
185
|
+
? Array.from(output as Iterable<readonly [string, unknown]>)
|
|
186
|
+
: Object.entries(output)
|
|
187
|
+
)
|
|
188
|
+
.filter(([, value]) => value !== undefined)
|
|
189
|
+
.map(([name]) => name);
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The names a schema's output writes, in their compared form. A name with no value writes nothing,
|
|
193
|
+
* so a default stays, while an empty list is the caller saying there are no entries.
|
|
194
|
+
*/
|
|
195
|
+
export const written = (output: object, fold: Fold): ReadonlySet<string> =>
|
|
196
|
+
new Set(writtenNames(output).map(fold));
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
200
|
+
* can write less than it was given, and that must not pass as a group that was built.
|
|
201
|
+
*/
|
|
202
|
+
export const lost = (
|
|
203
|
+
output: object,
|
|
204
|
+
defaults: ReadonlyArray<readonly [string, unknown]>,
|
|
205
|
+
settled: object,
|
|
206
|
+
fold: Fold,
|
|
207
|
+
): ReadonlyArray<string> => {
|
|
208
|
+
const kept = written(settled, fold);
|
|
209
|
+
return [...writtenNames(output), ...defaults.map(([name]) => name)].filter(
|
|
210
|
+
(name) => !kept.has(fold(name)),
|
|
211
|
+
);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Read what a schema says about its group, and build what it sends.
|
|
216
|
+
*
|
|
217
|
+
* @remarks
|
|
218
|
+
* `settle` adds defaults to what the schema wrote and takes the whole group through the schema in
|
|
219
|
+
* both directions. A decoder may accept more spellings than its encoder writes, so a default that
|
|
220
|
+
* decodes is not yet what the schema sends.
|
|
221
|
+
*
|
|
222
|
+
* Effect strips what a union member does not state, so the first member that fits part of the group
|
|
223
|
+
* would win over a later one that holds all of it. `settle` therefore looks for such a member first.
|
|
224
|
+
* Without one it takes the looser match, and {@link lost} tells the caller what that left out.
|
|
225
|
+
*/
|
|
226
|
+
export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
227
|
+
const reading = read(SchemaAST.toEncoded(contract.ast));
|
|
228
|
+
const spelling = new Map(
|
|
229
|
+
Array.from(reading.fields.keys(), (name) => [fold(name), name] as const),
|
|
230
|
+
);
|
|
231
|
+
const decode = Schema.decodeUnknownEffect(contract);
|
|
232
|
+
const encode = Schema.encodeEffect(contract);
|
|
233
|
+
const shape: Shape = {
|
|
234
|
+
names: new Set(spelling.keys()),
|
|
235
|
+
open: reading.open,
|
|
236
|
+
required: new Set(Array.from(reading.required, fold)),
|
|
237
|
+
};
|
|
238
|
+
const settle = <W extends object, A>(
|
|
239
|
+
output: W,
|
|
240
|
+
defaults: ReadonlyArray<readonly [string, A]>,
|
|
241
|
+
): Effect.Effect<
|
|
242
|
+
W,
|
|
243
|
+
Schema.SchemaError,
|
|
244
|
+
S["DecodingServices"] | S["EncodingServices"]
|
|
245
|
+
> => {
|
|
246
|
+
if (defaults.length === 0) return Effect.succeed(output);
|
|
247
|
+
const values = new Map<string, Array<unknown>>();
|
|
248
|
+
const pairs = defaults.map(([name, value]) => {
|
|
249
|
+
const spelled = spelling.get(fold(name)) ?? name;
|
|
250
|
+
const types = reading.fields.get(spelled) ?? reading.rest;
|
|
251
|
+
const entry = typeof value === "string" ? readText(types, value) : value;
|
|
252
|
+
values.set(spelled, [...(values.get(spelled) ?? []), entry]);
|
|
253
|
+
return [spelled, entry] as const;
|
|
254
|
+
});
|
|
255
|
+
const record = Object.fromEntries(
|
|
256
|
+
Array.from(values, ([name, entries]) => [
|
|
257
|
+
name,
|
|
258
|
+
entries.length > 1 ||
|
|
259
|
+
(reading.fields.get(name) ?? reading.rest).some(takesList)
|
|
260
|
+
? entries
|
|
261
|
+
: entries[0],
|
|
262
|
+
]),
|
|
263
|
+
);
|
|
264
|
+
const group =
|
|
265
|
+
Symbol.iterator in output
|
|
266
|
+
? [...(output as unknown as Iterable<unknown>), ...pairs]
|
|
267
|
+
: { ...output, ...record };
|
|
268
|
+
const through = (options?: SchemaAST.ParseOptions) =>
|
|
269
|
+
Effect.flatMap(
|
|
270
|
+
decode(group, options),
|
|
271
|
+
(decoded) =>
|
|
272
|
+
encode(decoded, options) as Effect.Effect<W, Schema.SchemaError>,
|
|
273
|
+
);
|
|
274
|
+
return Effect.catch(through({ onExcessProperty: "error" }), () =>
|
|
275
|
+
through(),
|
|
276
|
+
);
|
|
277
|
+
};
|
|
278
|
+
return { shape, settle };
|
|
279
|
+
};
|