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