viell-effect-schema-http 0.2.0 → 0.4.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 +8 -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 +109 -23
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +121 -21
- 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 +252 -0
- package/dist/http/internal/excess.js.map +1 -0
- package/dist/http/internal/input.d.ts +33 -8
- package/dist/http/internal/input.d.ts.map +1 -1
- package/dist/http/internal/input.js +102 -20
- package/dist/http/internal/input.js.map +1 -1
- package/dist/http/internal/shape.d.ts +21 -0
- package/dist/http/internal/shape.d.ts.map +1 -1
- package/dist/http/internal/shape.js +96 -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 +236 -31
- package/src/http/Response.ts +11 -1
- package/src/http/UrlEncoding.ts +2 -1
- package/src/http/internal/excess.ts +370 -0
- package/src/http/internal/input.ts +200 -37
- package/src/http/internal/shape.ts +148 -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.
|
|
@@ -211,6 +274,14 @@ export const lost = (
|
|
|
211
274
|
);
|
|
212
275
|
};
|
|
213
276
|
|
|
277
|
+
/** Only stable absent/data properties prove the caller did not supply this field. */
|
|
278
|
+
const omitted = (input: object, name: string): boolean => {
|
|
279
|
+
const property = Object.getOwnPropertyDescriptor(input, name);
|
|
280
|
+
return property === undefined
|
|
281
|
+
? !(name in input)
|
|
282
|
+
: "value" in property && property.value === undefined;
|
|
283
|
+
};
|
|
284
|
+
|
|
214
285
|
/**
|
|
215
286
|
* Read what a schema says about its group, and build what it sends.
|
|
216
287
|
*
|
|
@@ -224,17 +295,82 @@ export const lost = (
|
|
|
224
295
|
* Without one it takes the looser match, and {@link lost} tells the caller what that left out.
|
|
225
296
|
*/
|
|
226
297
|
export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
227
|
-
const reading = read(
|
|
298
|
+
const reading = read(contract.ast);
|
|
228
299
|
const spelling = new Map(
|
|
229
300
|
Array.from(reading.fields.keys(), (name) => [fold(name), name] as const),
|
|
230
301
|
);
|
|
231
302
|
const decode = Schema.decodeUnknownEffect(contract);
|
|
232
303
|
const encode = Schema.encodeEffect(contract);
|
|
304
|
+
const chooses = Excess.choosesMember(contract.ast);
|
|
233
305
|
const shape: Shape = {
|
|
234
306
|
names: new Set(spelling.keys()),
|
|
235
307
|
open: reading.open,
|
|
236
308
|
required: new Set(Array.from(reading.required, fold)),
|
|
309
|
+
chooses,
|
|
237
310
|
};
|
|
311
|
+
// A group transformation or declaration can change names or construct in its own way.
|
|
312
|
+
const constructorNames =
|
|
313
|
+
contract.ast._tag === "Objects" && contract.ast.encoding === undefined
|
|
314
|
+
? contract.ast.propertySignatures.flatMap(({ name, type }) =>
|
|
315
|
+
typeof name === "string" &&
|
|
316
|
+
type.context?.constructorDefault !== undefined
|
|
317
|
+
? [name]
|
|
318
|
+
: [],
|
|
319
|
+
)
|
|
320
|
+
: [];
|
|
321
|
+
/** Snapshot caller omission before preparation can run a default or encoder. */
|
|
322
|
+
const candidates = (
|
|
323
|
+
input: unknown,
|
|
324
|
+
edits: ReadonlySet<string> | undefined,
|
|
325
|
+
): ReadonlyArray<string> =>
|
|
326
|
+
edits !== undefined && typeof input === "object" && input !== null
|
|
327
|
+
? constructorNames.filter(
|
|
328
|
+
(name) => edits.has(fold(name)) && omitted(input, name),
|
|
329
|
+
)
|
|
330
|
+
: [];
|
|
331
|
+
/** The successful first constructor actually supplied these edited, encoded fields. */
|
|
332
|
+
const filled = (
|
|
333
|
+
names: ReadonlyArray<string>,
|
|
334
|
+
constructed: unknown,
|
|
335
|
+
output: unknown,
|
|
336
|
+
): ReadonlyArray<string> => {
|
|
337
|
+
if (
|
|
338
|
+
typeof constructed !== "object" ||
|
|
339
|
+
constructed === null ||
|
|
340
|
+
typeof output !== "object" ||
|
|
341
|
+
output === null
|
|
342
|
+
)
|
|
343
|
+
return [];
|
|
344
|
+
return names
|
|
345
|
+
.filter((name) => {
|
|
346
|
+
const made = Object.getOwnPropertyDescriptor(constructed, name);
|
|
347
|
+
const encoded = Object.getOwnPropertyDescriptor(output, name);
|
|
348
|
+
return (
|
|
349
|
+
made !== undefined &&
|
|
350
|
+
"value" in made &&
|
|
351
|
+
made.value !== undefined &&
|
|
352
|
+
encoded?.enumerable === true &&
|
|
353
|
+
"value" in encoded &&
|
|
354
|
+
encoded.value !== undefined
|
|
355
|
+
);
|
|
356
|
+
})
|
|
357
|
+
.map(fold);
|
|
358
|
+
};
|
|
359
|
+
/**
|
|
360
|
+
* The caller's fields this schema took: what it wrote, and any it states and the caller gave,
|
|
361
|
+
* which may be written under another name or not at all. What it would lose is not among them.
|
|
362
|
+
*/
|
|
363
|
+
const taken = (
|
|
364
|
+
input: unknown,
|
|
365
|
+
output: object,
|
|
366
|
+
losing: ReadonlyArray<Excess.Path>,
|
|
367
|
+
): ReadonlySet<PropertyKey> =>
|
|
368
|
+
new Set(
|
|
369
|
+
[
|
|
370
|
+
...writtenNames(output),
|
|
371
|
+
...Excess.statedFields(contract.ast, input),
|
|
372
|
+
].filter((name) => !losing.some(([key]) => key === name)),
|
|
373
|
+
);
|
|
238
374
|
const settle = <W extends object, A>(
|
|
239
375
|
output: W,
|
|
240
376
|
defaults: ReadonlyArray<readonly [string, A]>,
|
|
@@ -275,5 +411,5 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
|
275
411
|
through(),
|
|
276
412
|
);
|
|
277
413
|
};
|
|
278
|
-
return { shape, settle };
|
|
414
|
+
return { shape, settle, taken, candidates, filled };
|
|
279
415
|
};
|