viell-effect-schema-http 0.2.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 +7 -6
- 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 +2 -0
- package/dist/http/Body.d.ts.map +1 -1
- package/dist/http/Body.js.map +1 -1
- package/dist/http/Operation.d.ts +6 -0
- package/dist/http/Operation.d.ts.map +1 -1
- package/dist/http/Operation.js.map +1 -1
- package/dist/http/Policy.d.ts +25 -0
- package/dist/http/Policy.d.ts.map +1 -1
- package/dist/http/Policy.js +25 -0
- package/dist/http/Policy.js.map +1 -1
- package/dist/http/Request.d.ts +58 -15
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +54 -19
- package/dist/http/Request.js.map +1 -1
- package/dist/http/Response.d.ts +11 -1
- package/dist/http/Response.d.ts.map +1 -1
- package/dist/http/Response.js.map +1 -1
- package/dist/http/UrlEncoding.d.ts +2 -1
- package/dist/http/UrlEncoding.d.ts.map +1 -1
- 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/input.d.ts +36 -5
- package/dist/http/internal/input.d.ts.map +1 -1
- package/dist/http/internal/input.js +72 -19
- package/dist/http/internal/input.js.map +1 -1
- package/dist/http/internal/shape.d.ts +19 -0
- package/dist/http/internal/shape.d.ts.map +1 -1
- package/dist/http/internal/shape.js +58 -12
- package/dist/http/internal/shape.js.map +1 -1
- package/package.json +3 -2
- package/src/concurrency/Interaction.ts +7 -1
- package/src/http/Body.ts +2 -0
- package/src/http/Operation.ts +6 -0
- package/src/http/Policy.ts +27 -0
- package/src/http/Request.ts +122 -23
- package/src/http/Response.ts +11 -1
- package/src/http/UrlEncoding.ts +2 -1
- package/src/http/internal/excess.ts +333 -0
- package/src/http/internal/input.ts +133 -28
- package/src/http/internal/shape.ts +92 -12
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
|
+
import * as Excess from "./excess.js";
|
|
2
3
|
import type { Fold } from "./fields.js";
|
|
3
4
|
|
|
4
5
|
/** What one schema says about its field group. Names are wire names, in their compared form. */
|
|
@@ -9,6 +10,16 @@ export interface Shape {
|
|
|
9
10
|
readonly open: boolean;
|
|
10
11
|
/** The names it always writes, so a raw edit of one could never reach the client. */
|
|
11
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>;
|
|
12
23
|
}
|
|
13
24
|
|
|
14
25
|
/** What a schema's encoded side states, in the schema's own spelling. */
|
|
@@ -26,18 +37,38 @@ const holdsUndefined = (ast: SchemaAST.AST): boolean =>
|
|
|
26
37
|
ast._tag === "Void" ||
|
|
27
38
|
(ast._tag === "Union" && ast.types.some(holdsUndefined));
|
|
28
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
|
+
|
|
29
57
|
const readObjects = (ast: SchemaAST.Objects): Reading => {
|
|
30
58
|
const stated = ast.propertySignatures.map(
|
|
31
|
-
({ name, type }) =>
|
|
59
|
+
({ name, type }) =>
|
|
60
|
+
[String(name), SchemaAST.toEncoded(type), type] as const,
|
|
32
61
|
);
|
|
33
62
|
return {
|
|
34
|
-
fields: new Map(stated.map(([name,
|
|
35
|
-
rest: ast.indexSignatures.map(({ type }) => type),
|
|
63
|
+
fields: new Map(stated.map(([name, encoded]) => [name, [encoded]])),
|
|
64
|
+
rest: ast.indexSignatures.map(({ type }) => SchemaAST.toEncoded(type)),
|
|
36
65
|
open: ast.indexSignatures.length > 0,
|
|
37
66
|
required: new Set(
|
|
38
67
|
stated
|
|
39
68
|
.filter(
|
|
40
|
-
([, type]) =>
|
|
69
|
+
([, encoded, type]) =>
|
|
70
|
+
(!SchemaAST.isOptional(encoded) && !holdsUndefined(encoded)) ||
|
|
71
|
+
writesOptional(type),
|
|
41
72
|
)
|
|
42
73
|
.map(([name]) => name),
|
|
43
74
|
),
|
|
@@ -63,13 +94,15 @@ const readUnion = (members: ReadonlyArray<Reading>): Reading => {
|
|
|
63
94
|
};
|
|
64
95
|
};
|
|
65
96
|
|
|
66
|
-
/** Read a schema's encoded
|
|
67
|
-
const read = (ast: SchemaAST.AST): Reading =>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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))
|
|
72
104
|
: { fields: new Map(), rest: [], open: true, required: new Set() };
|
|
105
|
+
};
|
|
73
106
|
|
|
74
107
|
const takesList = (ast: SchemaAST.AST): boolean =>
|
|
75
108
|
ast._tag === "Arrays" || (ast._tag === "Union" && ast.types.some(takesList));
|
|
@@ -195,6 +228,36 @@ const writtenNames = (output: object): ReadonlyArray<string> =>
|
|
|
195
228
|
export const written = (output: object, fold: Fold): ReadonlySet<string> =>
|
|
196
229
|
new Set(writtenNames(output).map(fold));
|
|
197
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
|
+
|
|
198
261
|
/**
|
|
199
262
|
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
200
263
|
* can write less than it was given, and that must not pass as a group that was built.
|
|
@@ -224,17 +287,34 @@ export const lost = (
|
|
|
224
287
|
* Without one it takes the looser match, and {@link lost} tells the caller what that left out.
|
|
225
288
|
*/
|
|
226
289
|
export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
227
|
-
const reading = read(
|
|
290
|
+
const reading = read(contract.ast);
|
|
228
291
|
const spelling = new Map(
|
|
229
292
|
Array.from(reading.fields.keys(), (name) => [fold(name), name] as const),
|
|
230
293
|
);
|
|
231
294
|
const decode = Schema.decodeUnknownEffect(contract);
|
|
232
295
|
const encode = Schema.encodeEffect(contract);
|
|
296
|
+
const chooses = Excess.choosesMember(contract.ast);
|
|
233
297
|
const shape: Shape = {
|
|
234
298
|
names: new Set(spelling.keys()),
|
|
235
299
|
open: reading.open,
|
|
236
300
|
required: new Set(Array.from(reading.required, fold)),
|
|
301
|
+
chooses,
|
|
237
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
|
+
);
|
|
238
318
|
const settle = <W extends object, A>(
|
|
239
319
|
output: W,
|
|
240
320
|
defaults: ReadonlyArray<readonly [string, A]>,
|
|
@@ -275,5 +355,5 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
|
275
355
|
through(),
|
|
276
356
|
);
|
|
277
357
|
};
|
|
278
|
-
return { shape, settle };
|
|
358
|
+
return { shape, settle, taken };
|
|
279
359
|
};
|