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,39 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
/** What a schema's constructor takes. This is the one place that names the `~`-prefixed key. */
|
|
3
|
+
type ConstructorInput<S extends Schema.Top> = S["~type.make.in"];
|
|
4
|
+
/** A field's type on the encoded side, in whichever union members have it. */
|
|
5
|
+
type EncodedField<B, K> = B extends unknown ? K extends keyof B ? B[K] : never : never;
|
|
6
|
+
/**
|
|
7
|
+
* Either side of a schema, chosen field by field. A list is a value, so it has no fields, and an
|
|
8
|
+
* object whose encoded side is not one, such as a `Date` from text, has none to choose from.
|
|
9
|
+
*/
|
|
10
|
+
type Fieldwise<A, B> = A extends ReadonlyArray<unknown> ? never : A extends object ? B extends object ? {
|
|
11
|
+
readonly [K in keyof A]: A[K] | EncodedField<B, K>;
|
|
12
|
+
} : never : never;
|
|
13
|
+
/** Every key of every member. `keyof` on a union keeps only the keys all its members share. */
|
|
14
|
+
type KeysOf<T> = T extends unknown ? keyof T : never;
|
|
15
|
+
/**
|
|
16
|
+
* Leave out the sides when the mix takes everything they do, which an editor would show three times.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Assignability is not enough on its own, because a stray key in a literal is an error against the
|
|
20
|
+
* mix even when the value is assignable to it. A renamed field needs its side for that reason, and
|
|
21
|
+
* a field the mix requires and one side leaves out needs its side too.
|
|
22
|
+
*/
|
|
23
|
+
type Merged<Sides, Mix> = [Sides] extends [Mix] ? [KeysOf<Sides>] extends [KeysOf<Mix>] ? Mix : Sides | Mix : Sides | Mix;
|
|
24
|
+
type EitherSide<A, B> = [A, B] extends [B, A] ? A : Merged<A | B, Fieldwise<A, B>>;
|
|
25
|
+
/** What a schema-backed input takes: its decoded side, its encoded side, or a mix by field. */
|
|
26
|
+
export type Input<S extends Schema.Top> = EitherSide<ConstructorInput<S>, S["Encoded"]>;
|
|
27
|
+
/**
|
|
28
|
+
* Bring a value on either side of a schema to its decoded side.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* A value that fits the decoded side is read as decoded, so it is encoded once. Anything else goes
|
|
32
|
+
* through the decoder as a whole, then field by field. Both routes validate, and a value that fits
|
|
33
|
+
* neither fails with the decoded side's error, so existing failures read the same.
|
|
34
|
+
*/
|
|
35
|
+
export declare const toDecoded: <S extends Schema.Top>(contract: S) => (input: Input<S>) => Effect.Effect<S["Type"], Schema.SchemaError, S["DecodingServices"]>;
|
|
36
|
+
/** Bring a value to the schema's decoded side, then write it. */
|
|
37
|
+
export declare const encoder: <S extends Schema.Top, A, R>(contract: S, write: (value: S["Type"]) => Effect.Effect<A, Schema.SchemaError, R>) => (input: Input<S>) => Effect.Effect<A, Schema.SchemaError, R | S["DecodingServices"]>;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../../src/http/internal/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAa,MAAM,QAAQ,CAAC;AAEnD,gGAAgG;AAChG,KAAK,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,eAAe,CAAC,CAAC;AAEjE,8EAA8E;AAC9E,KAAK,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,OAAO,GACvC,CAAC,SAAS,MAAM,CAAC,GACf,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK,GACP,KAAK,CAAC;AAEV;;;GAGG;AACH,KAAK,SAAS,CAAC,CAAC,EAAE,CAAC,IACjB,CAAC,SAAS,aAAa,CAAC,OAAO,CAAC,GAC5B,KAAK,GACL,CAAC,SAAS,MAAM,GACd,CAAC,SAAS,MAAM,GACd;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM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ACX,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,MAIlE,OAAO,KAAK,CAAC,CAAC,CAAC,KACd,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC,kBAAkB,CAAC,CAElE,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
|
+
/** The fields of a struct or class, as they are declared, with each field's own encoding. */
|
|
3
|
+
const fieldsOf = (ast) => ast._tag === "Objects"
|
|
4
|
+
? ast.propertySignatures
|
|
5
|
+
: ast._tag === "Declaration" && ast.encoding !== undefined
|
|
6
|
+
? fieldsOf(ast.encoding[0].to)
|
|
7
|
+
: [];
|
|
8
|
+
/**
|
|
9
|
+
* Bring a value on either side of a schema to its decoded side.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* A value that fits the decoded side is read as decoded, so it is encoded once. Anything else goes
|
|
13
|
+
* through the decoder as a whole, then field by field. Both routes validate, and a value that fits
|
|
14
|
+
* neither fails with the decoded side's error, so existing failures read the same.
|
|
15
|
+
*/
|
|
16
|
+
export const toDecoded = (contract) => {
|
|
17
|
+
const make = (input) => Effect.mapError(contract.makeEffect(input), (issue) => new Schema.SchemaError(issue));
|
|
18
|
+
const decode = Schema.decodeUnknownEffect(contract);
|
|
19
|
+
const byField = Effect.fnUntraced(function* (input) {
|
|
20
|
+
const output = { ...input };
|
|
21
|
+
for (const { name, type } of fieldsOf(contract.ast))
|
|
22
|
+
if (output[name] !== undefined)
|
|
23
|
+
output[name] = yield* fromPart(type, output[name]);
|
|
24
|
+
return yield* make(output);
|
|
25
|
+
});
|
|
26
|
+
const byMember = (input) => contract.ast._tag === "Union"
|
|
27
|
+
? Effect.firstSuccessOf(contract.ast.types.map((member) => fromPart(member, input)))
|
|
28
|
+
: byField(input);
|
|
29
|
+
return (input) => Effect.catch(make(input), (failure) => Effect.mapError(Effect.catch(decode(input), (encodedFailure) => typeof input === "object" && input !== null
|
|
30
|
+
? byMember(input)
|
|
31
|
+
: Effect.fail(encodedFailure)), () => failure));
|
|
32
|
+
};
|
|
33
|
+
/** Read a field or union member, which may be on either side of its own schema. */
|
|
34
|
+
const fromPart = (ast, value) => toDecoded(Schema.make(ast))(value);
|
|
35
|
+
/** Bring a value to the schema's decoded side, then write it. */
|
|
36
|
+
export const encoder = (contract, write) => {
|
|
37
|
+
const decoded = toDecoded(contract);
|
|
38
|
+
return (input) => Effect.flatMap(decoded(input), write);
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../../src/http/internal/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAoDnD,6FAA6F;AAC7F,MAAM,QAAQ,GAAG,CACf,GAAkB,EAIjB,EAAE,CACH,GAAG,CAAC,IAAI,KAAK,SAAS;IACpB,CAAC,CAAC,GAAG,CAAC,kBAAkB;IACxB,CAAC,CAAC,GAAG,CAAC,IAAI,KAAK,aAAa,IAAI,GAAG,CAAC,QAAQ,KAAK,SAAS;QACxD,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9B,CAAC,CAAC,EAAE,CAAC;AAEX;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAuB,QAAW,EAAE,EAAE;IAC7D,MAAM,IAAI,GAAG,CAAC,KAAc,EAAE,EAAE,CAC9B,MAAM,CAAC,QAAQ,CACb,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,EAC1B,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI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CAAC;QACjB,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAChC,EACD,GAAG,EAAE,CAAC,OAAO,CACd,CACqE,CAAC;AAC7E,CAAC,CAAC;AAEF,mFAAmF;AACnF,MAAM,QAAQ,GAAG,CAAC,GAAkB,EAAE,KAAc,EAAE,EAAE,CACtD,SAAS,CAAC,MAAM,CAAC,IAAI,CAAa,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAEjD,iEAAiE;AACjE,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,QAAW,EACX,KAAoE,EACpE,EAAE;IACF,MAAM,OAAO,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;IACpC,OAAO,CACL,KAAe,EACkD,EAAE,CACnE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC;AAC1C,CAAC,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import type { Fold } from "./fields.js";
|
|
3
|
+
/** What one schema says about its field group. Names are wire names, in their compared form. */
|
|
4
|
+
export interface Shape {
|
|
5
|
+
/** The names the schema states. */
|
|
6
|
+
readonly names: ReadonlySet<string>;
|
|
7
|
+
/** Whether it allows names it does not state, as a record or a list of pairs does. */
|
|
8
|
+
readonly open: boolean;
|
|
9
|
+
/** The names it always writes, so a raw edit of one could never reach the client. */
|
|
10
|
+
readonly required: ReadonlySet<string>;
|
|
11
|
+
}
|
|
12
|
+
/** Whether a shape allows a name, given in its compared form. */
|
|
13
|
+
export declare const allows: (shape: Shape, name: string) => boolean;
|
|
14
|
+
/** Declared names no bound schema allows. With no schema there is no shape to meet. */
|
|
15
|
+
export declare const outside: (names: ReadonlyArray<string>, shapes: ReadonlyArray<Shape>, fold: Fold) => ReadonlyArray<string>;
|
|
16
|
+
/** The edited names some bound schema always writes. */
|
|
17
|
+
export declare const required: (names: ReadonlyArray<string>, shapes: ReadonlyArray<Shape>, fold: Fold) => ReadonlyArray<string>;
|
|
18
|
+
/**
|
|
19
|
+
* The names a schema's output writes, in their compared form. A name with no value writes nothing,
|
|
20
|
+
* so a default stays, while an empty list is the caller saying there are no entries.
|
|
21
|
+
*/
|
|
22
|
+
export declare const written: (output: object, fold: Fold) => ReadonlySet<string>;
|
|
23
|
+
/**
|
|
24
|
+
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
25
|
+
* can write less than it was given, and that must not pass as a group that was built.
|
|
26
|
+
*/
|
|
27
|
+
export declare const lost: (output: object, defaults: ReadonlyArray<readonly [string, unknown]>, settled: object, fold: Fold) => ReadonlyArray<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Read what a schema says about its group, and build what it sends.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* `settle` adds defaults to what the schema wrote and takes the whole group through the schema in
|
|
33
|
+
* both directions. A decoder may accept more spellings than its encoder writes, so a default that
|
|
34
|
+
* decodes is not yet what the schema sends.
|
|
35
|
+
*
|
|
36
|
+
* Effect strips what a union member does not state, so the first member that fits part of the group
|
|
37
|
+
* would win over a later one that holds all of it. `settle` therefore looks for such a member first.
|
|
38
|
+
* Without one it takes the looser match, and {@link lost} tells the caller what that left out.
|
|
39
|
+
*/
|
|
40
|
+
export declare const bind: <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
41
|
+
shape: Shape;
|
|
42
|
+
settle: <W extends object, A>(output: W, defaults: ReadonlyArray<readonly [string, A]>) => Effect.Effect<W, Schema.SchemaError, S["DecodingServices"] | S["EncodingServices"]>;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=shape.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.d.ts","sourceRoot":"","sources":["../../../src/http/internal/shape.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAa,MAAM,QAAQ,CAAC;AACnD,OAAO,KAAK,EAA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E,CAAC,CAAC,CAAC,KAC5C,MAAM,CAAC,MAAM,CACd,CAAC,EACD,MAAM,CAAC,WAAW,EAClB,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,kBAAkB,CAAC,CAC9C;CAkCF,CAAC"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
|
+
const holdsUndefined = (ast) => ast._tag === "Undefined" ||
|
|
3
|
+
ast._tag === "Void" ||
|
|
4
|
+
(ast._tag === "Union" && ast.types.some(holdsUndefined));
|
|
5
|
+
const readObjects = (ast) => {
|
|
6
|
+
const stated = ast.propertySignatures.map(({ name, type }) => [String(name), type]);
|
|
7
|
+
return {
|
|
8
|
+
fields: new Map(stated.map(([name, type]) => [name, [type]])),
|
|
9
|
+
rest: ast.indexSignatures.map(({ type }) => type),
|
|
10
|
+
open: ast.indexSignatures.length > 0,
|
|
11
|
+
required: new Set(stated
|
|
12
|
+
.filter(([, type]) => !SchemaAST.isOptional(type) && !holdsUndefined(type))
|
|
13
|
+
.map(([name]) => name)),
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/** A union allows what any member allows and always writes what every member writes. */
|
|
17
|
+
const readUnion = (members) => {
|
|
18
|
+
const fields = new Map();
|
|
19
|
+
for (const member of members)
|
|
20
|
+
for (const [name, types] of member.fields)
|
|
21
|
+
fields.set(name, [...(fields.get(name) ?? []), ...types]);
|
|
22
|
+
const [first, ...others] = members;
|
|
23
|
+
return {
|
|
24
|
+
fields,
|
|
25
|
+
rest: members.flatMap(({ rest }) => rest),
|
|
26
|
+
open: members.some(({ open }) => open),
|
|
27
|
+
required: new Set(Array.from(first?.required ?? []).filter((name) => others.every(({ required }) => required.has(name)))),
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/** Read a schema's encoded AST. Where names cannot be read, the schema allows any name. */
|
|
31
|
+
const read = (ast) => ast._tag === "Objects"
|
|
32
|
+
? readObjects(ast)
|
|
33
|
+
: ast._tag === "Union"
|
|
34
|
+
? readUnion(ast.types.map(read))
|
|
35
|
+
: { fields: new Map(), rest: [], open: true, required: new Set() };
|
|
36
|
+
const takesList = (ast) => ast._tag === "Arrays" || (ast._tag === "Union" && ast.types.some(takesList));
|
|
37
|
+
/** The single values a field's encoded type takes, looking inside unions and lists. */
|
|
38
|
+
const scalars = (ast) => ast._tag === "Union"
|
|
39
|
+
? ast.types.flatMap(scalars)
|
|
40
|
+
: ast._tag === "Arrays"
|
|
41
|
+
? [...ast.elements, ...ast.rest].flatMap(scalars)
|
|
42
|
+
: [ast];
|
|
43
|
+
const readNumber = (text) => {
|
|
44
|
+
if (text === "NaN")
|
|
45
|
+
return Number.NaN;
|
|
46
|
+
const value = Number(text);
|
|
47
|
+
return text.trim() === "" || Number.isNaN(value) ? text : value;
|
|
48
|
+
};
|
|
49
|
+
const readBoolean = (text) => text === "true" ? true : text === "false" ? false : text;
|
|
50
|
+
const readBigInt = (text) => /^-?\d+$/.test(text) ? BigInt(text) : text;
|
|
51
|
+
/**
|
|
52
|
+
* Text reads as `Schema.DateFromString` reads it. A declaration need not refuse an invalid `Date`
|
|
53
|
+
* as `Schema.Date` does, so text with no date in it stays text for the schema to refuse.
|
|
54
|
+
*/
|
|
55
|
+
const readDate = (ast) => {
|
|
56
|
+
const takesDate = Schema.is(Schema.make(ast));
|
|
57
|
+
return (text) => {
|
|
58
|
+
const date = new Date(text);
|
|
59
|
+
return !Number.isNaN(date.getTime()) && takesDate(date) ? date : text;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const readerFor = (ast) => {
|
|
63
|
+
switch (ast._tag) {
|
|
64
|
+
case "Declaration":
|
|
65
|
+
return [readDate(ast)];
|
|
66
|
+
case "Number":
|
|
67
|
+
return [readNumber];
|
|
68
|
+
case "Boolean":
|
|
69
|
+
return [readBoolean];
|
|
70
|
+
case "BigInt":
|
|
71
|
+
return [readBigInt];
|
|
72
|
+
case "Literal":
|
|
73
|
+
return typeof ast.literal === "number"
|
|
74
|
+
? [readNumber]
|
|
75
|
+
: typeof ast.literal === "boolean"
|
|
76
|
+
? [readBoolean]
|
|
77
|
+
: typeof ast.literal === "bigint"
|
|
78
|
+
? [readBigInt]
|
|
79
|
+
: [];
|
|
80
|
+
case "Enum":
|
|
81
|
+
return ast.enums.some(([, value]) => typeof value === "number")
|
|
82
|
+
? [readNumber]
|
|
83
|
+
: [];
|
|
84
|
+
default:
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Read wire text as the value a field encodes to, the reverse of how the library writes it. A field
|
|
90
|
+
* that can hold text takes it as it is, and text that reads as nothing else stays text for the
|
|
91
|
+
* schema to refuse.
|
|
92
|
+
*/
|
|
93
|
+
const readText = (types, text) => {
|
|
94
|
+
const forms = types.flatMap(scalars);
|
|
95
|
+
if (forms.some(({ _tag }) => _tag === "String"))
|
|
96
|
+
return text;
|
|
97
|
+
for (const reader of forms.flatMap(readerFor)) {
|
|
98
|
+
const value = reader(text);
|
|
99
|
+
if (typeof value !== "string")
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
return text;
|
|
103
|
+
};
|
|
104
|
+
/** Whether a shape allows a name, given in its compared form. */
|
|
105
|
+
export const allows = (shape, name) => shape.open || shape.names.has(name);
|
|
106
|
+
/** Declared names no bound schema allows. With no schema there is no shape to meet. */
|
|
107
|
+
export const outside = (names, shapes, fold) => shapes.length === 0
|
|
108
|
+
? []
|
|
109
|
+
: names.filter((name) => !shapes.some((shape) => allows(shape, fold(name))));
|
|
110
|
+
/** The edited names some bound schema always writes. */
|
|
111
|
+
export const required = (names, shapes, fold) => names.filter((name) => shapes.some((shape) => shape.required.has(fold(name))));
|
|
112
|
+
/** The names a schema's output holds a value under, as the schema spells them. */
|
|
113
|
+
const writtenNames = (output) => (Symbol.iterator in output
|
|
114
|
+
? Array.from(output)
|
|
115
|
+
: Object.entries(output))
|
|
116
|
+
.filter(([, value]) => value !== undefined)
|
|
117
|
+
.map(([name]) => name);
|
|
118
|
+
/**
|
|
119
|
+
* The names a schema's output writes, in their compared form. A name with no value writes nothing,
|
|
120
|
+
* so a default stays, while an empty list is the caller saying there are no entries.
|
|
121
|
+
*/
|
|
122
|
+
export const written = (output, fold) => new Set(writtenNames(output).map(fold));
|
|
123
|
+
/**
|
|
124
|
+
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
125
|
+
* can write less than it was given, and that must not pass as a group that was built.
|
|
126
|
+
*/
|
|
127
|
+
export const lost = (output, defaults, settled, fold) => {
|
|
128
|
+
const kept = written(settled, fold);
|
|
129
|
+
return [...writtenNames(output), ...defaults.map(([name]) => name)].filter((name) => !kept.has(fold(name)));
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Read what a schema says about its group, and build what it sends.
|
|
133
|
+
*
|
|
134
|
+
* @remarks
|
|
135
|
+
* `settle` adds defaults to what the schema wrote and takes the whole group through the schema in
|
|
136
|
+
* both directions. A decoder may accept more spellings than its encoder writes, so a default that
|
|
137
|
+
* decodes is not yet what the schema sends.
|
|
138
|
+
*
|
|
139
|
+
* Effect strips what a union member does not state, so the first member that fits part of the group
|
|
140
|
+
* would win over a later one that holds all of it. `settle` therefore looks for such a member first.
|
|
141
|
+
* Without one it takes the looser match, and {@link lost} tells the caller what that left out.
|
|
142
|
+
*/
|
|
143
|
+
export const bind = (contract, fold) => {
|
|
144
|
+
const reading = read(SchemaAST.toEncoded(contract.ast));
|
|
145
|
+
const spelling = new Map(Array.from(reading.fields.keys(), (name) => [fold(name), name]));
|
|
146
|
+
const decode = Schema.decodeUnknownEffect(contract);
|
|
147
|
+
const encode = Schema.encodeEffect(contract);
|
|
148
|
+
const shape = {
|
|
149
|
+
names: new Set(spelling.keys()),
|
|
150
|
+
open: reading.open,
|
|
151
|
+
required: new Set(Array.from(reading.required, fold)),
|
|
152
|
+
};
|
|
153
|
+
const settle = (output, defaults) => {
|
|
154
|
+
if (defaults.length === 0)
|
|
155
|
+
return Effect.succeed(output);
|
|
156
|
+
const values = new Map();
|
|
157
|
+
const pairs = defaults.map(([name, value]) => {
|
|
158
|
+
const spelled = spelling.get(fold(name)) ?? name;
|
|
159
|
+
const types = reading.fields.get(spelled) ?? reading.rest;
|
|
160
|
+
const entry = typeof value === "string" ? readText(types, value) : value;
|
|
161
|
+
values.set(spelled, [...(values.get(spelled) ?? []), entry]);
|
|
162
|
+
return [spelled, entry];
|
|
163
|
+
});
|
|
164
|
+
const record = Object.fromEntries(Array.from(values, ([name, entries]) => [
|
|
165
|
+
name,
|
|
166
|
+
entries.length > 1 ||
|
|
167
|
+
(reading.fields.get(name) ?? reading.rest).some(takesList)
|
|
168
|
+
? entries
|
|
169
|
+
: entries[0],
|
|
170
|
+
]));
|
|
171
|
+
const group = Symbol.iterator in output
|
|
172
|
+
? [...output, ...pairs]
|
|
173
|
+
: { ...output, ...record };
|
|
174
|
+
const through = (options) => Effect.flatMap(decode(group, options), (decoded) => encode(decoded, options));
|
|
175
|
+
return Effect.catch(through({ onExcessProperty: "error" }), () => through());
|
|
176
|
+
};
|
|
177
|
+
return { shape, settle };
|
|
178
|
+
};
|
|
179
|
+
//# sourceMappingURL=shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.js","sourceRoot":"","sources":["../../../src/http/internal/shape.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAuBnD,MAAM,cAAc,GAAG,CAAC,GAAkB,EAAW,EAAE,CACrD,GAAG,CAAC,IAAI,KAAK,WAAW;IACxB,GAAG,CAAC,IAAI,KAAK,MAAM;IACnB,CAAC,GAAG,CAAC,IAAI,KAAK,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC;AAE3D,MAAM,WAAW,GAAG,CAAC,GAAsB,EAAW,EAAE;IACtD,MAAM,MAAM,GAA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-1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "viell-effect-schema-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Consumer-owned HTTP contracts composed with Effect",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"type": "module",
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"engines": {
|
|
18
|
-
"node": ">=24
|
|
18
|
+
"node": ">=24"
|
|
19
19
|
},
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
@@ -58,11 +58,11 @@
|
|
|
58
58
|
},
|
|
59
59
|
"main": "./dist/main.js",
|
|
60
60
|
"types": "./dist/main.d.ts",
|
|
61
|
-
"packageManager": "npm@11.19.
|
|
61
|
+
"packageManager": "npm@11.19.1",
|
|
62
62
|
"devEngines": {
|
|
63
63
|
"packageManager": {
|
|
64
64
|
"name": "npm",
|
|
65
|
-
"version": "^11.19.
|
|
65
|
+
"version": "^11.19.1",
|
|
66
66
|
"onFail": "error"
|
|
67
67
|
}
|
|
68
68
|
},
|
|
@@ -81,13 +81,13 @@
|
|
|
81
81
|
"lint:mdrefine": "mdrefine lint",
|
|
82
82
|
"test": "vitest run",
|
|
83
83
|
"test:package": "node scripts/check-package.mjs",
|
|
84
|
-
"test:release": "node --test scripts/release.test.mjs",
|
|
84
|
+
"test:release": "node --test scripts/release.test.mjs scripts/registry.test.mjs",
|
|
85
85
|
"release:prepare": "node scripts/prepare-release.mjs",
|
|
86
86
|
"test:watch": "vitest",
|
|
87
87
|
"docs:dev": "vitepress dev docs",
|
|
88
88
|
"docs:build": "vitepress build docs && node scripts/check-doc-links.mjs",
|
|
89
89
|
"docs:preview": "vite preview --host 127.0.0.1 --outDir docs/.vitepress/dist --base /slop-effect-schema-http/",
|
|
90
|
-
"docs:examples": "tsc -p tsconfig.examples.json && node scripts/check-doc-examples.mjs && node examples/guides.mjs && node examples/modules.mjs"
|
|
90
|
+
"docs:examples": "tsc -p tsconfig.examples.json && node scripts/check-doc-examples.mjs && node examples/guides.mjs && node examples/modules.mjs && node examples/recipes.mjs"
|
|
91
91
|
},
|
|
92
92
|
"files": [
|
|
93
93
|
"dist",
|
|
@@ -108,20 +108,20 @@
|
|
|
108
108
|
"minimatch@10": "11.0.1"
|
|
109
109
|
},
|
|
110
110
|
"devDependencies": {
|
|
111
|
-
"@effect/vitest": "^4.1.
|
|
111
|
+
"@effect/vitest": "^4.1.2",
|
|
112
112
|
"@eslint/js": "^10.0.1",
|
|
113
113
|
"@types/node": "^26.6.6",
|
|
114
|
-
"effect": "^4.1.
|
|
114
|
+
"effect": "^4.1.2",
|
|
115
115
|
"eslint": "^10.14.0",
|
|
116
116
|
"eslint-config-prettier": "^10.1.8",
|
|
117
|
-
"eslint-plugin-oxlint": "^1.
|
|
118
|
-
"mdrefine": "^0.
|
|
117
|
+
"eslint-plugin-oxlint": "^1.92.0",
|
|
118
|
+
"mdrefine": "^0.7.1",
|
|
119
119
|
"npm-run-all2": "^9.0.4",
|
|
120
|
-
"oxlint": "^1.
|
|
120
|
+
"oxlint": "^1.92.0",
|
|
121
121
|
"prettier": "^3.9.10",
|
|
122
122
|
"typescript": "^6.0.3",
|
|
123
123
|
"typescript-eslint": "^8.72.1",
|
|
124
124
|
"vitepress": "2.0.0-alpha.21",
|
|
125
|
-
"vitest": "^5.0
|
|
125
|
+
"vitest": "^5.1.0"
|
|
126
126
|
}
|
|
127
127
|
}
|
package/src/http/Body.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type HttpClientResponse,
|
|
6
6
|
type UrlParams,
|
|
7
7
|
} from "effect/http";
|
|
8
|
+
import * as Inputs from "./internal/input.js";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Turn an application value into a finite outgoing HTTP body.
|
|
@@ -37,7 +38,8 @@ export interface Decoder<out A, out E = never, out R = never>
|
|
|
37
38
|
* An encoder and decoder for the same application value.
|
|
38
39
|
*
|
|
39
40
|
* @remarks
|
|
40
|
-
* Encoding and decoding errors and required services are tracked separately.
|
|
41
|
+
* Encoding and decoding errors and required services are tracked separately. `In` is what encoding
|
|
42
|
+
* takes, which is `A` unless the codec accepts more than it decodes to, as {@link jsonSchema} does.
|
|
41
43
|
*/
|
|
42
44
|
export interface Codec<
|
|
43
45
|
A,
|
|
@@ -45,9 +47,10 @@ export interface Codec<
|
|
|
45
47
|
DecodeError = never,
|
|
46
48
|
EncodeServices = never,
|
|
47
49
|
DecodeServices = never,
|
|
50
|
+
In = A,
|
|
48
51
|
>
|
|
49
52
|
extends
|
|
50
|
-
Encoder<
|
|
53
|
+
Encoder<In, EncodeError, EncodeServices>,
|
|
51
54
|
Decoder<A, DecodeError, DecodeServices> {}
|
|
52
55
|
|
|
53
56
|
/**
|
|
@@ -82,7 +85,7 @@ export const decoder = <A, E = never, R = never>(
|
|
|
82
85
|
*
|
|
83
86
|
* @remarks
|
|
84
87
|
* `mediaType` describes the accepted response representation. Outgoing content metadata comes
|
|
85
|
-
* from the `HttpBody` returned by `encode`.
|
|
88
|
+
* from the `HttpBody` returned by `encode`. `encode` may take more than `decode` returns.
|
|
86
89
|
*/
|
|
87
90
|
export const make = <
|
|
88
91
|
A,
|
|
@@ -90,15 +93,16 @@ export const make = <
|
|
|
90
93
|
DecodeError = never,
|
|
91
94
|
EncodeServices = never,
|
|
92
95
|
DecodeServices = never,
|
|
96
|
+
In = A,
|
|
93
97
|
>(options: {
|
|
94
98
|
readonly encode: (
|
|
95
|
-
value:
|
|
99
|
+
value: In,
|
|
96
100
|
) => Effect.Effect<HttpBody.HttpBody, EncodeError, EncodeServices>;
|
|
97
101
|
readonly decode: (
|
|
98
102
|
response: HttpClientResponse.HttpClientResponse,
|
|
99
103
|
) => Effect.Effect<A, DecodeError, DecodeServices>;
|
|
100
104
|
readonly mediaType?: string | undefined;
|
|
101
|
-
}): Codec<A, EncodeError, DecodeError, EncodeServices, DecodeServices> => ({
|
|
105
|
+
}): Codec<A, EncodeError, DecodeError, EncodeServices, DecodeServices, In> => ({
|
|
102
106
|
...Pipeable.Prototype,
|
|
103
107
|
encode: options.encode,
|
|
104
108
|
decode: options.decode,
|
|
@@ -176,8 +180,9 @@ export const bytes: Codec<Uint8Array, never, HttpClientError.HttpClientError> =
|
|
|
176
180
|
* Read and write Web `FormData` as multipart form data.
|
|
177
181
|
*
|
|
178
182
|
* @remarks
|
|
179
|
-
* The transport supplies the outgoing multipart boundary and its Content-Type.
|
|
180
|
-
*
|
|
183
|
+
* The transport supplies the outgoing multipart boundary and its Content-Type. A Content-Type the
|
|
184
|
+
* request declares replaces it, and without the boundary the body cannot be read. Decoding
|
|
185
|
+
* delegates to the HTTP provider.
|
|
181
186
|
*/
|
|
182
187
|
export const formData: Codec<FormData, never, HttpClientError.HttpClientError> =
|
|
183
188
|
make({
|
|
@@ -255,7 +260,7 @@ export const mapEffect =
|
|
|
255
260
|
* ```ts
|
|
256
261
|
* import { Schema } from "effect";
|
|
257
262
|
* import { Body } from "viell-effect-schema-http";
|
|
258
|
-
* const numberFromText = Body.text.pipe(Body.schema(Schema.
|
|
263
|
+
* const numberFromText = Body.text.pipe(Body.schema(Schema.FiniteFromString));
|
|
259
264
|
* // Decodes the text "42" to the number 42.
|
|
260
265
|
* ```
|
|
261
266
|
*
|
|
@@ -276,18 +281,26 @@ export const schema = <S extends Schema.Constraint>(contract: S) => {
|
|
|
276
281
|
*
|
|
277
282
|
* @remarks
|
|
278
283
|
* The schema's encoded type must match the underlying encoder's input. Returns an encoder only;
|
|
279
|
-
* use {@link schema} for the decoding direction.
|
|
284
|
+
* use {@link schema} for the decoding direction. The input takes either side of the schema, or a
|
|
285
|
+
* mix by field, as `Request.Input` describes, so a plain object works for a `Schema.Class`, and
|
|
286
|
+
* the underlying encoder receives what the schema writes.
|
|
280
287
|
*/
|
|
281
|
-
export const encodeSchema = <S extends Schema.
|
|
282
|
-
const encode = Schema.encodeEffect(contract);
|
|
288
|
+
export const encodeSchema = <S extends Schema.Top>(contract: S) => {
|
|
289
|
+
const encode = Inputs.encoder(contract, Schema.encodeEffect(contract));
|
|
283
290
|
return <E, R>(
|
|
284
291
|
self: Encoder<S["Encoded"], E, R>,
|
|
285
|
-
): Encoder<
|
|
286
|
-
|
|
292
|
+
): Encoder<
|
|
293
|
+
Inputs.Input<S>,
|
|
294
|
+
E | Schema.SchemaError,
|
|
295
|
+
R | S["EncodingServices"] | S["DecodingServices"]
|
|
296
|
+
> =>
|
|
297
|
+
encoder((value: Inputs.Input<S>) =>
|
|
298
|
+
Effect.flatMap(encode(value), self.encode),
|
|
299
|
+
);
|
|
287
300
|
};
|
|
288
301
|
|
|
289
302
|
/**
|
|
290
|
-
* Read and write schema-validated JSON
|
|
303
|
+
* Read and write schema-validated JSON, returning the schema's application type.
|
|
291
304
|
*
|
|
292
305
|
* @example
|
|
293
306
|
* ```ts
|
|
@@ -299,19 +312,27 @@ export const encodeSchema = <S extends Schema.Constraint>(contract: S) => {
|
|
|
299
312
|
* @remarks
|
|
300
313
|
* Uses `Schema.toCodecJson` to obtain the schema's canonical JSON codec. Encoding failures occur
|
|
301
314
|
* before dispatch; an operation wraps response decoding failures in `Response.DecodeError`.
|
|
315
|
+
*
|
|
316
|
+
* Encoding takes either side of the schema, or a mix by field, as `Request.Input` describes, and
|
|
317
|
+
* writes the canonical JSON. The encoded side is the schema's own, so a `Schema.Date` field takes a
|
|
318
|
+
* `Date`, not the ISO text JSON carries. Decoding returns the schema's application type, so a class
|
|
319
|
+
* comes back as an instance.
|
|
302
320
|
*/
|
|
303
|
-
export const jsonSchema = <S extends Schema.
|
|
321
|
+
export const jsonSchema = <S extends Schema.Top>(
|
|
304
322
|
contract: S,
|
|
305
323
|
): Codec<
|
|
306
324
|
S["Type"],
|
|
307
325
|
Schema.SchemaError | HttpBody.HttpBodyError,
|
|
308
326
|
Schema.SchemaError | HttpClientError.HttpClientError,
|
|
309
|
-
S["EncodingServices"],
|
|
310
|
-
S["DecodingServices"]
|
|
327
|
+
S["EncodingServices"] | S["DecodingServices"],
|
|
328
|
+
S["DecodingServices"],
|
|
329
|
+
Inputs.Input<S>
|
|
311
330
|
> => {
|
|
312
331
|
const codec = Schema.toCodecJson(contract);
|
|
332
|
+
const encode = Inputs.encoder(contract, Schema.encodeEffect(codec));
|
|
313
333
|
return make({
|
|
314
|
-
encode:
|
|
334
|
+
encode: (value: Inputs.Input<S>) =>
|
|
335
|
+
Effect.flatMap(encode(value), json.encode),
|
|
315
336
|
decode: json.pipe(schema(codec)).decode,
|
|
316
337
|
mediaType: json.mediaType,
|
|
317
338
|
});
|