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
package/dist/http/Request.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { Effect, Pipeable, Schema } from "effect";
|
|
|
2
2
|
import { HttpClientRequest, UrlParams } from "effect/http";
|
|
3
3
|
import type * as Body from "./Body.js";
|
|
4
4
|
import * as Fields from "./internal/fields.js";
|
|
5
|
+
import * as Inputs from "./internal/input.js";
|
|
6
|
+
import * as Shapes from "./internal/shape.js";
|
|
5
7
|
import * as Transport from "./Transport.js";
|
|
6
8
|
import * as UrlEncoding from "./UrlEncoding.js";
|
|
7
9
|
declare const InvalidRequest_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
@@ -11,8 +13,10 @@ declare const InvalidRequest_base: new <A extends Record<string, any> = {}>(args
|
|
|
11
13
|
* Request preparation failed before dispatch.
|
|
12
14
|
*
|
|
13
15
|
* @remarks
|
|
14
|
-
* Covers computed-field failures, URL-rendering exceptions,
|
|
15
|
-
*
|
|
16
|
+
* Covers computed-field failures, URL-rendering and {@link mapBaseUrl} exceptions, a path value
|
|
17
|
+
* that does not encode to one value, and fields that do not fit the schemas bound to their group.
|
|
18
|
+
* Schema failures of the caller's input remain separate `SchemaError` values.
|
|
19
|
+
* A failed check of a group names its fields and leaves out their values, which may be credentials.
|
|
16
20
|
*/
|
|
17
21
|
export declare class InvalidRequest extends InvalidRequest_base<{
|
|
18
22
|
readonly message: string;
|
|
@@ -27,29 +31,143 @@ export declare class InvalidRequest extends InvalidRequest_base<{
|
|
|
27
31
|
* the field. Query fields also accept `null` for a bare key.
|
|
28
32
|
*/
|
|
29
33
|
export type FieldValue<A = string> = Fields.Value<A>;
|
|
34
|
+
/**
|
|
35
|
+
* Headers declared together: a name's value, or `undefined` to remove its declarations.
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* Use with {@link setHeader}. A value is text or a function that supplies it, as in
|
|
39
|
+
* {@link FieldValue}.
|
|
40
|
+
*/
|
|
41
|
+
export type HeaderFields = Readonly<Record<string, FieldValue | undefined>>;
|
|
42
|
+
/**
|
|
43
|
+
* Query fields declared together: what a query schema may encode to, or a function.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* Use with {@link setQuery}. Numbers, bigints and booleans become text, a `Date` becomes ISO text,
|
|
47
|
+
* `null` is a bare key, an array repeats the name, and `undefined` removes the name's declarations.
|
|
48
|
+
* A function supplies one such value during preparation, written the same way, and `undefined`
|
|
49
|
+
* from it declares nothing, as in {@link FieldValue}. Fields apply in the object's key order, in
|
|
50
|
+
* which JavaScript puts integer-like keys first.
|
|
51
|
+
*/
|
|
52
|
+
export type QueryFields = Readonly<Record<string, UrlEncoding.QueryValue | ReadonlyArray<UrlEncoding.QueryValue> | (() => UrlEncoding.QueryValue)>>;
|
|
53
|
+
/**
|
|
54
|
+
* What a schema-backed input takes: the schema's decoded side, its encoded side, or a mix by field.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* The decoded side is what the schema's constructor takes, so a plain object works for a
|
|
58
|
+
* `Schema.Class`, and a field with a constructor default can be left out. The encoded side is what
|
|
59
|
+
* goes on the wire, and {@link path}, {@link query}, {@link headers} and the schema body encoders
|
|
60
|
+
* all take it. The request always carries what the schema writes.
|
|
61
|
+
*
|
|
62
|
+
* Preparation tries the decoded side first, then the decoder as a whole, then the decoder field by
|
|
63
|
+
* field. If all fail, it reports the decoded side's error, a `SchemaError` before dispatch. Both
|
|
64
|
+
* routes validate, so text cannot get past a schema, but a weak schema lets more through:
|
|
65
|
+
* `Schema.NumberFromString` turns `"many"` into `NaN`, and `Schema.FiniteFromString` refuses it.
|
|
66
|
+
*
|
|
67
|
+
* A value that fits the decoded side is read as decoded. Under a schema whose two sides share a
|
|
68
|
+
* type, such as one from text to text, an already encoded value fits the decoded side, so it is
|
|
69
|
+
* encoded again, with no error. A mix is one level deep in the type. Renamed fields take their
|
|
70
|
+
* encoded names only when the whole value is on the encoded side, and a value on that side stays
|
|
71
|
+
* there, even if it would fit the decoded side by losing them.
|
|
72
|
+
*
|
|
73
|
+
* Under a union, a member that keeps every field wins over an earlier one that would strip one, on
|
|
74
|
+
* both sides and at any depth. A field no member can keep beside the others is a `SchemaError` that
|
|
75
|
+
* names it, because the types allow that value and any member would lose a field the schema states.
|
|
76
|
+
* A key no member states is stripped, as a struct strips it, and so is a key the tag has ruled out.
|
|
77
|
+
* A key set to `undefined` is no field, and an accessor on a class is one, as it is to a struct. In a
|
|
78
|
+
* header or query group, a field a schema stacked with the union takes is not lost. Inside text given
|
|
79
|
+
* as text, a `Schema.Map`, a `Schema.Option` or a declaration with no struct behind it, the schema's
|
|
80
|
+
* own reading applies.
|
|
81
|
+
*/
|
|
82
|
+
export type Input<S extends Schema.Top> = Inputs.Input<S>;
|
|
83
|
+
/**
|
|
84
|
+
* The whole input of a request: the object its call takes, with every binding's property.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { Schema } from "effect";
|
|
89
|
+
* import { Request } from "viell-effect-schema-http";
|
|
90
|
+
* const listThings = Request.get("https://api.example.test/things").pipe(
|
|
91
|
+
* Request.query(Schema.Struct({ sort: Schema.String })),
|
|
92
|
+
* Request.headers(Schema.Struct({ authorization: Schema.String })),
|
|
93
|
+
* );
|
|
94
|
+
* type ListInput = Request.InputOf<typeof listThings>;
|
|
95
|
+
* const withToken = listThings.pipe(
|
|
96
|
+
* Request.mapInput(
|
|
97
|
+
* (input: Omit<ListInput, "headers">): ListInput => ({
|
|
98
|
+
* ...input,
|
|
99
|
+
* headers: { authorization: "Bearer token" },
|
|
100
|
+
* }),
|
|
101
|
+
* ),
|
|
102
|
+
* );
|
|
103
|
+
* // Input: { query: { sort: "name" } }
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* This takes a request, and {@link Input} takes a schema. `Input` is what one binding takes, such
|
|
108
|
+
* as the value under `query`, and this is the object that holds them all, which is what
|
|
109
|
+
* {@link mapInput} has to return. `Operation.InputOf` gives the same for an operation.
|
|
110
|
+
*/
|
|
111
|
+
export type InputOf<R> = R extends Request<infer I, unknown, unknown> ? I : never;
|
|
30
112
|
/**
|
|
31
113
|
* The method, URL and raw fields from which request preparation starts.
|
|
32
114
|
*
|
|
33
115
|
* @remarks
|
|
34
116
|
* This is a low-level extension type. Prefer {@link make} and the request combinators.
|
|
35
|
-
* `H` and `Q` record whether a schema owns each field group.
|
|
36
117
|
*/
|
|
37
|
-
export interface Declaration
|
|
118
|
+
export interface Declaration {
|
|
119
|
+
/** The HTTP method, passed on as written. */
|
|
38
120
|
readonly method: string;
|
|
121
|
+
/** The constructor's path or URL, without its query and fragment. */
|
|
39
122
|
readonly url: string;
|
|
40
|
-
|
|
123
|
+
/** The fragment written in the constructor, without its `#`. */
|
|
124
|
+
readonly hash: string | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* The base URL as text, or an Effect that supplies it during preparation. The request's own error
|
|
127
|
+
* and service types carry what that Effect needs. {@link mapBaseUrl} always leaves an Effect.
|
|
128
|
+
*/
|
|
129
|
+
readonly baseUrl: string | Effect.Effect<string, unknown, unknown>;
|
|
130
|
+
/** The raw header declarations, in order. */
|
|
41
131
|
readonly headers: Fields.Fields;
|
|
132
|
+
/** The raw query declarations, in order, including entries written in the constructor. */
|
|
42
133
|
readonly query: Fields.Fields<string | null>;
|
|
43
|
-
/**
|
|
44
|
-
readonly
|
|
45
|
-
/**
|
|
46
|
-
readonly
|
|
134
|
+
/** The constructor wrote a `?` with nothing after it, which holds no entries. */
|
|
135
|
+
readonly emptyQuery: boolean;
|
|
136
|
+
/** Lowercase header names the request removed, so the library adds no automatic value. */
|
|
137
|
+
readonly removedHeaders: ReadonlySet<string>;
|
|
138
|
+
/** The schemas bound to the header group, which together state its shape. */
|
|
139
|
+
readonly headerShapes: ReadonlyArray<Shape>;
|
|
140
|
+
/** The schemas bound to the query group, which together state its shape. */
|
|
141
|
+
readonly queryShapes: ReadonlyArray<Shape>;
|
|
142
|
+
/** Raw edits, after a schema, of a name it always writes, which could never reach the client. */
|
|
143
|
+
readonly conflicts: ReadonlyArray<readonly [group: "query" | "headers", name: string]>;
|
|
47
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* What one header or query schema says about its group: the names it allows, whether it allows
|
|
147
|
+
* others, the names it always writes, and whether a union in it can lose a field.
|
|
148
|
+
*
|
|
149
|
+
* @remarks
|
|
150
|
+
* This is a low-level extension type, read from the schema's encoded side and its fields' own
|
|
151
|
+
* encodings when it is bound. Header names are lowercase.
|
|
152
|
+
*/
|
|
153
|
+
export type Shape = Shapes.Shape;
|
|
154
|
+
/**
|
|
155
|
+
* What the schemas stacked on a group have told each other during preparation.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* This is a low-level extension type. A field a union would lose is not lost if another schema in
|
|
159
|
+
* the group took it, and `settled` is never an answer to that: a schema can allow a name it did not
|
|
160
|
+
* take.
|
|
161
|
+
*/
|
|
162
|
+
export type Taken = Shapes.Taken;
|
|
163
|
+
/** A part of a request that a binding can supply, and that {@link unbind} can clear. */
|
|
164
|
+
export type Group = "path" | "query" | "headers" | "body";
|
|
48
165
|
/**
|
|
49
166
|
* One entry in Effect's native query collection.
|
|
50
167
|
*
|
|
51
168
|
* @remarks
|
|
52
|
-
* Drafts use entry identity to distinguish bare keys from empty values
|
|
169
|
+
* Drafts use entry identity to distinguish bare keys from empty values, and to keep the text a
|
|
170
|
+
* constructor entry was written with.
|
|
53
171
|
*/
|
|
54
172
|
export type UrlParam = UrlParams.UrlParams["params"][number];
|
|
55
173
|
/**
|
|
@@ -66,6 +184,25 @@ export interface Draft {
|
|
|
66
184
|
readonly headerLines: Transport.Lines;
|
|
67
185
|
/** Native query entries that render as bare keys. Replacing an entry loses its identity marker. */
|
|
68
186
|
readonly bareKeys: ReadonlySet<UrlParam>;
|
|
187
|
+
/** Native query entries that keep their written text. Replacing an entry loses that text. */
|
|
188
|
+
readonly queryText: ReadonlyMap<UrlParam, string>;
|
|
189
|
+
/**
|
|
190
|
+
* Native query entries the request declared, which a bound schema checks. A native edit makes a
|
|
191
|
+
* new entry, so {@link mapEffect} stays outside the check.
|
|
192
|
+
*/
|
|
193
|
+
readonly declaredQuery: ReadonlySet<UrlParam>;
|
|
194
|
+
/** Lowercase header names the request removed, so the library adds no automatic value. */
|
|
195
|
+
readonly removedHeaders: ReadonlySet<string>;
|
|
196
|
+
/** The groups a later {@link unbind} cleared, so their earlier bindings skip themselves. */
|
|
197
|
+
readonly unbound: ReadonlySet<Group>;
|
|
198
|
+
/**
|
|
199
|
+
* What the schemas stacked on the query group took from the caller, and which raw values are
|
|
200
|
+
* theirs to settle. `undefined` where the group needs no record, because no stacked schema can lose
|
|
201
|
+
* a field.
|
|
202
|
+
*/
|
|
203
|
+
readonly takenQuery: Taken | undefined;
|
|
204
|
+
/** The same for the header group, as in {@link takenQuery}. */
|
|
205
|
+
readonly takenHeaders: Taken | undefined;
|
|
69
206
|
}
|
|
70
207
|
/**
|
|
71
208
|
* An immutable HTTP request description with typed application input.
|
|
@@ -73,11 +210,12 @@ export interface Draft {
|
|
|
73
210
|
* @remarks
|
|
74
211
|
* Construction sends nothing. {@link compile} prepares it; `Operation.make` prepares and sends it.
|
|
75
212
|
* `I` is the complete input, `E` its preparation errors, and `R` its required services.
|
|
76
|
-
* `H` and `Q` record whether schemas own the header and query groups.
|
|
77
213
|
* `declaration` and `prepare` are low-level extension points.
|
|
78
214
|
*/
|
|
79
|
-
export interface Request<in I, out E = never, out R = never
|
|
80
|
-
|
|
215
|
+
export interface Request<in I, out E = never, out R = never> extends Pipeable.Pipeable {
|
|
216
|
+
/** What preparation starts from. */
|
|
217
|
+
readonly declaration: Declaration;
|
|
218
|
+
/** Apply the request's bindings and transforms to a draft, with the call's input. */
|
|
81
219
|
readonly prepare: (input: I, draft: Draft) => Effect.Effect<Draft, E, R>;
|
|
82
220
|
}
|
|
83
221
|
/**
|
|
@@ -85,12 +223,20 @@ export interface Request<in I, out E = never, out R = never, out H extends boole
|
|
|
85
223
|
*
|
|
86
224
|
* @remarks
|
|
87
225
|
* Use `undefined` as input until a binding adds required fields. A relative path can be combined
|
|
88
|
-
* with {@link baseUrl}
|
|
89
|
-
* Custom method strings pass through unchanged.
|
|
226
|
+
* with {@link baseUrl}, and {@link path} adds to it, so `""` starts a path written whole in a
|
|
227
|
+
* template. Custom method strings pass through unchanged.
|
|
228
|
+
*
|
|
229
|
+
* A constructor query is passed to the client as written until something edits that entry, so
|
|
230
|
+
* `?a=1;b=2`, `?q=a+b` and `?a=%41` keep their spelling, as do empty `&` segments and a lone `?`.
|
|
231
|
+
* Each entry also has a form-decoded name and value, which {@link setQuery} and {@link removeQuery}
|
|
232
|
+
* compare against. An entry that is edited, or added, is rendered through `UrlEncoding.query`. An
|
|
233
|
+
* entry under a name a {@link query} schema allows goes through that schema, and one under a name
|
|
234
|
+
* no bound schema allows is an incompatible shape. A written empty segment has no name, so it is
|
|
235
|
+
* not a field, and it stays as written.
|
|
90
236
|
*
|
|
91
|
-
* A
|
|
92
|
-
* and
|
|
93
|
-
*
|
|
237
|
+
* A fragment stays on the request, exactly as written, in the native request's `hash` field. It
|
|
238
|
+
* survives {@link path} and query edits, and wins over a fragment in path output or a base URL.
|
|
239
|
+
* Whether it goes out is up to the client; `fetch` does not send one.
|
|
94
240
|
*/
|
|
95
241
|
export declare const make: (method: string, url: string) => Request<unknown>;
|
|
96
242
|
/**
|
|
@@ -122,7 +268,7 @@ export declare const patch: (url: string) => Request<unknown>;
|
|
|
122
268
|
*/
|
|
123
269
|
export declare const del: (url: string) => Request<unknown>;
|
|
124
270
|
/**
|
|
125
|
-
* Describe a HEAD request.
|
|
271
|
+
* Describe a HEAD request. A {@link body} is passed on as for any method; the client may refuse it.
|
|
126
272
|
*/
|
|
127
273
|
export declare const head: (url: string) => Request<unknown>;
|
|
128
274
|
/**
|
|
@@ -130,7 +276,7 @@ export declare const head: (url: string) => Request<unknown>;
|
|
|
130
276
|
*/
|
|
131
277
|
export declare const options: (url: string) => Request<unknown>;
|
|
132
278
|
/**
|
|
133
|
-
* Set the shared origin and fixed path prefix for a request.
|
|
279
|
+
* Set the shared origin and fixed path prefix for a request, as text or from an Effect.
|
|
134
280
|
*
|
|
135
281
|
* @example
|
|
136
282
|
* ```ts
|
|
@@ -140,72 +286,191 @@ export declare const options: (url: string) => Request<unknown>;
|
|
|
140
286
|
* const teams = Request.get("/teams").pipe(api);
|
|
141
287
|
* ```
|
|
142
288
|
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* import { Context, Effect } from "effect";
|
|
292
|
+
* import { Request } from "viell-effect-schema-http";
|
|
293
|
+
* class ApiHost extends Context.Service<ApiHost, string>()("app/ApiHost") {}
|
|
294
|
+
* const discovered = Effect.gen(function* () {
|
|
295
|
+
* const host = yield* ApiHost;
|
|
296
|
+
* return `https://${host}/api/v1`;
|
|
297
|
+
* });
|
|
298
|
+
* const users = Request.get("/users").pipe(Request.baseUrl(discovered));
|
|
299
|
+
* // The request needs ApiHost, as it would need any other service.
|
|
300
|
+
* ```
|
|
301
|
+
*
|
|
143
302
|
* @remarks
|
|
144
303
|
* The base is joined after path preparation. With the default URL policy, an absolute request
|
|
145
304
|
* URL bypasses it; a leading `/` on a relative path keeps the base's prefix. Calling this again
|
|
146
|
-
* replaces the base, rather than appending to it
|
|
305
|
+
* replaces the base, rather than appending to it, and {@link mapBaseUrl} extends it. Keep query
|
|
306
|
+
* fields in request combinators. A fragment in the base is taken off before joining and used if the
|
|
307
|
+
* request has none of its own.
|
|
308
|
+
*
|
|
309
|
+
* An Effect supplies a location that is not known when the request is defined, from a service or a
|
|
310
|
+
* `Config` value, say. Its failures and services join the request's type, so leaving out a service
|
|
311
|
+
* is a compile error. It runs once per evaluation, after the input is encoded and before any
|
|
312
|
+
* attempt, so every retry uses the same base. Its failure is returned as it is, before anything is
|
|
313
|
+
* sent, and a retry policy does not repeat it. What it supplies is treated exactly as text would
|
|
314
|
+
* be, including by a replaced `UrlEncoding.join`.
|
|
315
|
+
*
|
|
316
|
+
* A location that must change between attempts belongs in `Operation.make`'s `transformClient`. A
|
|
317
|
+
* base that is replaced never runs, though the request's types keep what it added.
|
|
147
318
|
*/
|
|
148
|
-
export declare const baseUrl: (url: string) => <I, E, R
|
|
319
|
+
export declare const baseUrl: <E2 = never, R2 = never>(url: string | Effect.Effect<string, E2, R2>) => <I, E, R>(self: Request<I, E, R>) => Request<I, E | E2, R | R2>;
|
|
149
320
|
/**
|
|
150
|
-
*
|
|
321
|
+
* Change the base URL, whether it is text or an Effect, to extend a prefix or swap part of it.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { Request } from "viell-effect-schema-http";
|
|
326
|
+
* const api = Request.baseUrl("https://api.example.test/api/v1");
|
|
327
|
+
* const toAdmin = (url: string) => `${url}/admin`;
|
|
328
|
+
* const admin = Request.mapBaseUrl(toAdmin);
|
|
329
|
+
* const listUsers = Request.get("/users").pipe(api, admin);
|
|
330
|
+
* // https://api.example.test/api/v1/admin/users
|
|
331
|
+
* ```
|
|
151
332
|
*
|
|
152
333
|
* @remarks
|
|
153
|
-
*
|
|
154
|
-
*
|
|
334
|
+
* Use it where a request holds a base you cannot read, as an imported definition does, or one that
|
|
335
|
+
* is not known yet. `f` runs during preparation, once per evaluation, and receives the base as it
|
|
336
|
+
* stands at this step, so `""` where none is set. That is the whole text, including any query or
|
|
337
|
+
* fragment in it, and what `f` returns is joined exactly as a base from {@link baseUrl} would be. A
|
|
338
|
+
* later {@link baseUrl} replaces the result. An exception from `f` is an {@link InvalidRequest}.
|
|
155
339
|
*/
|
|
156
|
-
export declare const
|
|
340
|
+
export declare const mapBaseUrl: (f: (url: string) => string) => Edit;
|
|
341
|
+
/** A raw edit: a request's fields change, and its input, errors and services stay. */
|
|
342
|
+
type Edit = <I, E, R>(self: Request<I, E, R>) => Request<I, E, R>;
|
|
343
|
+
/**
|
|
344
|
+
* Set a raw header, replacing every existing line with that name, or set several from an object.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* import { Request } from "viell-effect-schema-http";
|
|
349
|
+
* const request = Request.get("https://api.example.test/things").pipe(
|
|
350
|
+
* Request.setHeader("X-One", "1"),
|
|
351
|
+
* Request.setHeader({ "X-Two": "2", "X-Three": () => "3" }),
|
|
352
|
+
* );
|
|
353
|
+
* ```
|
|
354
|
+
*
|
|
355
|
+
* @remarks
|
|
356
|
+
* Names compare without case, and the new line keeps the spelling of this call. A declared
|
|
357
|
+
* `Accept`, `Content-Type` or `Content-Length` replaces the automatic value, before or after the
|
|
358
|
+
* body.
|
|
359
|
+
*
|
|
360
|
+
* With a {@link headers} schema bound, in either order, the name has to be one a schema allows, and
|
|
361
|
+
* the value is a default that goes through the schema. Setting a name the schema always writes,
|
|
362
|
+
* after that schema, is an {@link InvalidRequest}, because the value could never reach the client.
|
|
363
|
+
*
|
|
364
|
+
* In the object form each key replaces its name as above, in key order, and other names stay. A key
|
|
365
|
+
* set to `undefined` removes that name's declarations and stops no automatic value, which is what
|
|
366
|
+
* {@link removeHeader} is for.
|
|
367
|
+
*/
|
|
368
|
+
export declare const setHeader: {
|
|
369
|
+
(name: string, value: FieldValue): Edit;
|
|
370
|
+
(fields: HeaderFields): Edit;
|
|
371
|
+
};
|
|
157
372
|
/**
|
|
158
373
|
* Add a raw header line after the existing lines, preserving repetition.
|
|
159
374
|
*
|
|
160
375
|
* @remarks
|
|
161
|
-
*
|
|
162
|
-
* lines requires a provider that supports them; Fetch may
|
|
376
|
+
* Each line keeps the spelling of its call. A bound {@link headers} schema checks it as it does
|
|
377
|
+
* {@link setHeader}. Sending separate lines requires a provider that supports them; Fetch may
|
|
378
|
+
* combine or restrict headers.
|
|
163
379
|
*/
|
|
164
|
-
export declare const appendHeader: (name: string, value: FieldValue) =>
|
|
380
|
+
export declare const appendHeader: (name: string, value: FieldValue) => Edit;
|
|
165
381
|
/**
|
|
166
382
|
* Remove a raw header, or only its declarations with the supplied fixed value.
|
|
167
383
|
*
|
|
168
384
|
* @remarks
|
|
169
|
-
* Names compare without case. Value functions are never evaluated to compare with `value`.
|
|
170
|
-
*
|
|
385
|
+
* Names compare without case. Value functions are never evaluated to compare with `value`. Removing
|
|
386
|
+
* a name a {@link headers} schema always writes, after that schema, is an {@link InvalidRequest},
|
|
387
|
+
* because the schema puts the value back. Removing an optional field clears its default.
|
|
388
|
+
*
|
|
389
|
+
* Without a value, this also stops the library's automatic value for that name: an operation's
|
|
390
|
+
* `Accept`, or a body's `Content-Type` and `Content-Length`. It stops only that. A value that a
|
|
391
|
+
* later declaration, a header schema or {@link mapEffect} supplies still goes out, and the client
|
|
392
|
+
* may add its own. A computed value that returns `undefined` declares nothing, so the removal stays
|
|
393
|
+
* in force. A removal limited to one value stops nothing, as the name may still be declared.
|
|
171
394
|
*/
|
|
172
|
-
export declare const removeHeader: (name: string, value?: string) =>
|
|
395
|
+
export declare const removeHeader: (name: string, value?: string) => Edit;
|
|
173
396
|
/**
|
|
174
|
-
* Set a raw query field, replacing entries with the same case-sensitive name
|
|
397
|
+
* Set a raw query field, replacing entries with the same case-sensitive name, or set several from
|
|
398
|
+
* an object.
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* import { Request } from "viell-effect-schema-http";
|
|
403
|
+
* const search = Request.get("https://api.example.test/users").pipe(
|
|
404
|
+
* Request.setQuery({ page: 2, tag: ["a", "b"], preview: null }),
|
|
405
|
+
* );
|
|
406
|
+
* // URL query: ?page=2&tag=a&tag=b&preview
|
|
407
|
+
* ```
|
|
175
408
|
*
|
|
176
409
|
* @remarks
|
|
177
|
-
* `null` produces a bare key, `""` an empty value, and `undefined` omits the entry.
|
|
178
|
-
*
|
|
410
|
+
* `null` produces a bare key, `""` an empty value, and `undefined` omits the entry. Names compare
|
|
411
|
+
* with an entry's decoded name, so this can replace an entry written in the constructor; other
|
|
412
|
+
* entries stay as written.
|
|
413
|
+
*
|
|
414
|
+
* With a {@link query} schema bound, in either order, the name has to be one a schema allows, and
|
|
415
|
+
* the value is a default that goes through the schema. Setting a name the schema always writes,
|
|
416
|
+
* after that schema, is an {@link InvalidRequest}, because the value could never reach the client.
|
|
417
|
+
*
|
|
418
|
+
* The object form takes {@link QueryFields}. Each key replaces its name as above, in key order, and
|
|
419
|
+
* other names stay. Use {@link appendQuery} for exact order across repeated names.
|
|
179
420
|
*/
|
|
180
|
-
export declare const setQuery:
|
|
421
|
+
export declare const setQuery: {
|
|
422
|
+
(name: string, value: FieldValue<string | null>): Edit;
|
|
423
|
+
(fields: QueryFields): Edit;
|
|
424
|
+
};
|
|
181
425
|
/**
|
|
182
426
|
* Append a raw query entry, preserving order and repeated names.
|
|
183
427
|
*
|
|
184
428
|
* @remarks
|
|
185
|
-
* Uses the same value rules as {@link setQuery}
|
|
429
|
+
* Uses the same value rules as {@link setQuery}, and a bound {@link query} schema checks it the
|
|
430
|
+
* same way.
|
|
186
431
|
*/
|
|
187
|
-
export declare const appendQuery: (name: string, value: FieldValue<string | null>) =>
|
|
432
|
+
export declare const appendQuery: (name: string, value: FieldValue<string | null>) => Edit;
|
|
188
433
|
/**
|
|
189
434
|
* Remove a raw query field, or only declarations with the supplied fixed value.
|
|
190
435
|
*
|
|
191
436
|
* @remarks
|
|
192
|
-
* Names compare with case
|
|
193
|
-
*
|
|
437
|
+
* Names compare with case, and both names and values compare with an entry's decoded form. Value
|
|
438
|
+
* functions are not evaluated for comparison. Removing a name a {@link query} schema always writes,
|
|
439
|
+
* after that schema, is an {@link InvalidRequest}, because the schema puts the value back. Removing
|
|
440
|
+
* an optional field clears its default.
|
|
194
441
|
*/
|
|
195
|
-
export declare const removeQuery: (name: string, value?: string | null) =>
|
|
442
|
+
export declare const removeQuery: (name: string, value?: string | null) => Edit;
|
|
196
443
|
/**
|
|
197
444
|
* Bind a schema to the call's `{ headers }` input.
|
|
198
445
|
*
|
|
199
446
|
* @remarks
|
|
200
|
-
* Encoding produces ordered header lines. The schema
|
|
201
|
-
* the group
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
447
|
+
* The input is an {@link Input} of the schema. Encoding produces ordered header lines. The schema
|
|
448
|
+
* states the shape of the group, whatever the order of the calls: once any schema is bound, every
|
|
449
|
+
* header the request declares must be a name some schema allows. A raw header under such a name
|
|
450
|
+
* is a default: what the schema writes replaces it, and otherwise it goes through the schema,
|
|
451
|
+
* which writes its value, spelling and order. An omitted optional field therefore leaves the
|
|
452
|
+
* default in place. A raw header under a name no schema allows is an {@link InvalidRequest} before
|
|
453
|
+
* anything is sent. A record or `Schema.StructWithRest` allows any name. Names compare without
|
|
454
|
+
* case.
|
|
455
|
+
*
|
|
456
|
+
* Under a union, the defaults and what the schema wrote have to fit one member together. A field
|
|
457
|
+
* that would be lost otherwise is an {@link InvalidRequest}, which names that field. The caller's
|
|
458
|
+
* own value is read before that, as {@link Input} describes.
|
|
459
|
+
*
|
|
460
|
+
* Header schemas stack: a second one adds its fields, and together they state the shape. A field
|
|
461
|
+
* one of them takes stays with it, whatever a union beside it leaves out. Use {@link unbind} to
|
|
462
|
+
* start again.
|
|
463
|
+
*
|
|
464
|
+
* A field the schema produces wins over an automatic `Accept`, `Content-Type` or `Content-Length`,
|
|
465
|
+
* whether the schema is bound before or after the body, and so does a default. An optional field
|
|
466
|
+
* left out with no default declares nothing, so the automatic value still applies.
|
|
467
|
+
*
|
|
468
|
+
* Header values are text, so a schema writes its own. A query schema can encode to a `Date`, but
|
|
469
|
+
* HTTP date headers are not ISO text, and `Schema.DateTimeUtcFromHttpDate` writes them.
|
|
205
470
|
*/
|
|
206
|
-
export declare const headers: <S extends Schema.ConstraintCodec<unknown, Transport.HeaderInput, unknown, unknown>>(contract: S) => <I, E, R
|
|
207
|
-
readonly headers: S
|
|
208
|
-
}, E | Schema.SchemaError, R | S["EncodingServices"]
|
|
471
|
+
export declare const headers: <S extends Schema.Top & Schema.ConstraintCodec<unknown, Transport.HeaderInput, unknown, unknown>>(contract: S) => <I, E, R>(self: Request<I, E, R>) => Request<I & {
|
|
472
|
+
readonly headers: Input<S>;
|
|
473
|
+
}, E | Schema.SchemaError | InvalidRequest, R | S["EncodingServices"] | S["DecodingServices"]>;
|
|
209
474
|
/**
|
|
210
475
|
* Bind a schema to the call's `{ query }` input.
|
|
211
476
|
*
|
|
@@ -214,66 +479,226 @@ export declare const headers: <S extends Schema.ConstraintCodec<unknown, Transpo
|
|
|
214
479
|
* import { Schema } from "effect";
|
|
215
480
|
* import { Request } from "viell-effect-schema-http";
|
|
216
481
|
* const search = Request.get("https://api.example.test/users").pipe(
|
|
217
|
-
* Request.query(Schema.Struct({ page: Schema.
|
|
482
|
+
* Request.query(Schema.Struct({ page: Schema.FiniteFromString })),
|
|
218
483
|
* );
|
|
219
484
|
* // Input: { query: { page: 2 } }; URL query: ?page=2
|
|
220
485
|
* ```
|
|
221
486
|
*
|
|
222
487
|
* @remarks
|
|
223
|
-
* The schema encodes to `UrlEncoding.QueryInput
|
|
224
|
-
* the
|
|
225
|
-
*
|
|
488
|
+
* The input is an {@link Input} of the schema. The schema encodes to `UrlEncoding.QueryInput` and
|
|
489
|
+
* states the shape of the group, whatever the order of the calls: once any schema is bound, every
|
|
490
|
+
* query field the request declares must be a name some schema allows. A raw entry under such a
|
|
491
|
+
* name is a default, including one written in the constructor: what the schema writes replaces
|
|
492
|
+
* it, and otherwise it goes through the schema, which writes its value and order. An omitted
|
|
493
|
+
* optional field therefore leaves the default in place. A raw entry under a name no schema allows
|
|
494
|
+
* is an {@link InvalidRequest} before anything is sent. A record, `Schema.StructWithRest` or a list
|
|
495
|
+
* of pairs allows any name.
|
|
496
|
+
*
|
|
497
|
+
* Under a union, the defaults and what the schema wrote have to fit one member together. A field
|
|
498
|
+
* that would be lost otherwise is an {@link InvalidRequest}, which names that field. The caller's
|
|
499
|
+
* own value is read before that, as {@link Input} describes.
|
|
500
|
+
*
|
|
501
|
+
* A field can encode to a `Date`, which is written as ISO text. Text under a field that encodes to
|
|
502
|
+
* a number, boolean, bigint or `Date` is read as one first, and a single value under a list field
|
|
503
|
+
* fits it. Date text is read as `Schema.DateFromString` reads it. The check is as strict as the
|
|
504
|
+
* schema. A `?` in a base URL or path output is text in the URL, not a field, so it is outside this
|
|
505
|
+
* check.
|
|
506
|
+
*
|
|
507
|
+
* Query schemas stack: a second one adds its fields, and together they state the shape. A field one
|
|
508
|
+
* of them takes stays with it, whatever a union beside it leaves out. Use {@link unbind} to start
|
|
509
|
+
* again, and {@link mapInput} to supply a required field's value.
|
|
510
|
+
*/
|
|
511
|
+
export declare const query: <S extends Schema.Top & Schema.ConstraintCodec<unknown, UrlEncoding.QueryInput, unknown, unknown>>(contract: S) => <I, E, R>(self: Request<I, E, R>) => Request<I & {
|
|
512
|
+
readonly query: Input<S>;
|
|
513
|
+
}, E | Schema.SchemaError | InvalidRequest, R | S["EncodingServices"] | S["DecodingServices"]>;
|
|
514
|
+
/** What a {@link PathValue}'s schema may encode to: one value with an obvious URL text. */
|
|
515
|
+
export type PathScalar = string | number | bigint | boolean;
|
|
516
|
+
/** A schema that encodes to one {@link PathScalar}, so its value fits in a path. */
|
|
517
|
+
export type PathSchema = Schema.Top & Schema.ConstraintCodec<unknown, PathScalar, unknown, unknown>;
|
|
518
|
+
/**
|
|
519
|
+
* A named value for a {@link path} template.
|
|
520
|
+
*
|
|
521
|
+
* @remarks
|
|
522
|
+
* Declare one with {@link pathValue}. `name` is its field under `path` in the input, and `schema` is
|
|
523
|
+
* what checks and encodes it.
|
|
226
524
|
*/
|
|
227
|
-
export
|
|
228
|
-
|
|
229
|
-
|
|
525
|
+
export interface PathValue<K extends string = string, S extends PathSchema = PathSchema> {
|
|
526
|
+
/** The value's field under `path` in the input. */
|
|
527
|
+
readonly name: K;
|
|
528
|
+
/** What checks and encodes the value. */
|
|
529
|
+
readonly schema: S;
|
|
530
|
+
}
|
|
230
531
|
/**
|
|
231
|
-
*
|
|
532
|
+
* Declare a named value for a {@link path} template.
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* import { Schema } from "effect";
|
|
537
|
+
* import { Request } from "viell-effect-schema-http";
|
|
538
|
+
* const customer = Request.pathValue("customer");
|
|
539
|
+
* const calendar = Request.pathValue("calendar", Schema.FiniteFromString);
|
|
540
|
+
* const getCalendar = Request.get("").pipe(
|
|
541
|
+
* Request.path`/customers/${customer}/calendars/${calendar}`,
|
|
542
|
+
* );
|
|
543
|
+
* // Input: { path: { customer: "a/b", calendar: 7 } }
|
|
544
|
+
* ```
|
|
545
|
+
*
|
|
546
|
+
* @remarks
|
|
547
|
+
* The value arrives under its name in `{ path }`, and without a schema it is a string. A schema has
|
|
548
|
+
* to encode to one {@link PathScalar}, and a value that gets past that type, as under `Schema.Any`,
|
|
549
|
+
* is an {@link InvalidRequest}. The input is an {@link Input} of the schema, so it takes either side.
|
|
550
|
+
* Declare a value once and reuse it across templates and definitions.
|
|
551
|
+
*/
|
|
552
|
+
export declare const pathValue: {
|
|
553
|
+
<const K extends string>(name: K): PathValue<K, typeof Schema.String>;
|
|
554
|
+
<const K extends string, S extends PathSchema>(name: K, schema: S): PathValue<K, S>;
|
|
555
|
+
};
|
|
556
|
+
/**
|
|
557
|
+
* Add to the request's path, from a template or from a renderer.
|
|
558
|
+
*
|
|
559
|
+
* @example
|
|
560
|
+
* ```ts
|
|
561
|
+
* import { Schema } from "effect";
|
|
562
|
+
* import { Request } from "viell-effect-schema-http";
|
|
563
|
+
* const customer = Request.pathValue("customer");
|
|
564
|
+
* const calendar = Request.pathValue("calendar", Schema.FiniteFromString);
|
|
565
|
+
* const getCalendar = Request.get("").pipe(
|
|
566
|
+
* Request.baseUrl("https://api.example.test/v1"),
|
|
567
|
+
* Request.path`/customers/${customer}/calendars/${calendar}`,
|
|
568
|
+
* );
|
|
569
|
+
* // Input: { path: { customer: "a/b", calendar: 7 } }
|
|
570
|
+
* // URL: https://api.example.test/v1/customers/a%2Fb/calendars/7
|
|
571
|
+
* ```
|
|
232
572
|
*
|
|
233
573
|
* @example
|
|
234
574
|
* ```ts
|
|
235
575
|
* import { Schema } from "effect";
|
|
236
576
|
* import { Request } from "viell-effect-schema-http";
|
|
237
|
-
* const
|
|
238
|
-
* Request.
|
|
239
|
-
*
|
|
577
|
+
* const listFolder = Request.get("/files").pipe(
|
|
578
|
+
* Request.path(Schema.String, (folder, encode) =>
|
|
579
|
+
* folder === "" ? "/" : `/${folder.split("/").map(encode).join("/")}/`,
|
|
580
|
+
* ),
|
|
240
581
|
* );
|
|
241
|
-
* // Input: { path:
|
|
582
|
+
* // Input: { path: "a b/c" }; URL: /files/a%20b/c/
|
|
242
583
|
* ```
|
|
243
584
|
*
|
|
244
585
|
* @remarks
|
|
245
|
-
* The
|
|
246
|
-
*
|
|
247
|
-
*
|
|
586
|
+
* The path is the constructor's path followed by what each `path` call adds, in order, joined
|
|
587
|
+
* exactly as written. The library adds no `/` and removes none, so `/users` and `42` make
|
|
588
|
+
* `/users42`, and `/users/` and `/42` make `/users//42`. The base URL is joined afterwards. A `?` or
|
|
589
|
+
* `#` in the text is URL syntax, so a `#` starts the fragment for all that follows it. A fragment
|
|
590
|
+
* moves to the compiled request's `hash`, so the query still goes before it. The result is passed
|
|
591
|
+
* to the client as written, and the client may rewrite or refuse it.
|
|
592
|
+
*
|
|
593
|
+
* Use a template when the path has a fixed shape. Only a {@link pathValue} can go in one, so each
|
|
594
|
+
* value has its schema and always goes through the active `UrlEncoding` path encoder, and a run-time
|
|
595
|
+
* string is a compile error. A template with no values adds fixed text. The text is used as written
|
|
596
|
+
* and never parsed, so a backslash is a backslash and not an escape. Stacked templates add their
|
|
597
|
+
* fields to one `path` input, and a name used more than once gets the same value.
|
|
598
|
+
*
|
|
599
|
+
* Use a renderer when the shape of the path depends on the value, such as a value holding several
|
|
600
|
+
* segments or a whole URL. It receives the schema-encoded value and the active path-value encoder,
|
|
601
|
+
* and escaping each dynamic value is up to it. The value under `path` is an {@link Input} of the
|
|
602
|
+
* schema, so a scalar schema takes a scalar and a struct takes its fields. A scalar is one value for
|
|
603
|
+
* the whole `path`, so it does not stack with another scalar of a different type or with a template.
|
|
248
604
|
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
605
|
+
* Encoding does not make a value safe. A `/` in a value cannot add a segment, but `..` and an empty
|
|
606
|
+
* string have nothing to encode, and the client may resolve a dot segment. Constrain a value from a
|
|
607
|
+
* user in its schema.
|
|
608
|
+
*
|
|
609
|
+
* {@link unbind} removes what earlier calls added, and {@link setPath} replaces the constructor's
|
|
610
|
+
* text. Renderer or path-encoding exceptions become {@link InvalidRequest}; schema failures remain
|
|
254
611
|
* `SchemaError` values.
|
|
255
612
|
*/
|
|
256
|
-
export declare const path:
|
|
257
|
-
|
|
258
|
-
|
|
613
|
+
export declare const path: {
|
|
614
|
+
(text: TemplateStringsArray): Edit;
|
|
615
|
+
<const V extends readonly [PathValue, ...ReadonlyArray<PathValue>]>(text: TemplateStringsArray, ...values: V): <I, E, R>(self: Request<I, E, R>) => Request<I & {
|
|
616
|
+
readonly path: {
|
|
617
|
+
readonly [P in V[number] as P["name"]]: Input<P["schema"]>;
|
|
618
|
+
};
|
|
619
|
+
}, E | Schema.SchemaError | InvalidRequest, R | V[number]["schema"]["EncodingServices"] | V[number]["schema"]["DecodingServices"]>;
|
|
620
|
+
<S extends Schema.Top>(contract: S, render: (value: S["Encoded"], encode: UrlEncoding.Service["path"]) => string): <I, E, R>(self: Request<I, E, R>) => Request<I & {
|
|
621
|
+
readonly path: Input<S>;
|
|
622
|
+
}, E | Schema.SchemaError | InvalidRequest, R | S["EncodingServices"] | S["DecodingServices"]>;
|
|
623
|
+
};
|
|
624
|
+
/**
|
|
625
|
+
* Replace the constructor's path, leaving every {@link path} binding in place.
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* ```ts
|
|
629
|
+
* import { Request } from "viell-effect-schema-http";
|
|
630
|
+
* const id = Request.pathValue("id");
|
|
631
|
+
* const current = Request.get("/things").pipe(Request.path`/${id}`);
|
|
632
|
+
* const legacy = current.pipe(Request.setPath("/legacy/things"));
|
|
633
|
+
* // Input: { path: { id: "a" } }; URL: /legacy/things/a
|
|
634
|
+
* ```
|
|
635
|
+
*
|
|
636
|
+
* @remarks
|
|
637
|
+
* Only the path changes. A constructor's query and fragment stay, and a `?` or `#` in this text is
|
|
638
|
+
* URL text, as it is in {@link path}. To replace a whole path, use {@link unbind} and this together.
|
|
639
|
+
*/
|
|
640
|
+
export declare const setPath: (url: string) => Edit;
|
|
259
641
|
/**
|
|
260
642
|
* Bind an encoder to the call's `{ body }` input.
|
|
261
643
|
*
|
|
262
644
|
* @remarks
|
|
263
|
-
* The body is encoded once per operation evaluation. Its encoder
|
|
264
|
-
* `Content-Length`; multipart metadata is supplied by the transport.
|
|
645
|
+
* The body is encoded once per operation evaluation. Its encoder supplies `Content-Type` and
|
|
646
|
+
* `Content-Length`; multipart metadata is supplied by the transport. A value the request declares
|
|
647
|
+
* for either field wins, before or after this binding, and {@link removeHeader} stops the automatic
|
|
648
|
+
* value. Neither changes the encoded bytes.
|
|
649
|
+
*
|
|
650
|
+
* Any method can carry a body, and the library adds no check of its own. A client can refuse the
|
|
651
|
+
* request: `fetch` does so for GET and HEAD, which surfaces as a transport failure.
|
|
265
652
|
*/
|
|
266
|
-
export declare const body: <A, E2, R2>(codec: Body.Encoder<A, E2, R2>) => <I, E, R
|
|
653
|
+
export declare const body: <A, E2, R2>(codec: Body.Encoder<A, E2, R2>) => <I, E, R>(self: Request<I, E, R>) => Request<I & {
|
|
267
654
|
readonly body: A;
|
|
268
|
-
}, E | E2, R | R2
|
|
655
|
+
}, E | E2, R | R2>;
|
|
656
|
+
/**
|
|
657
|
+
* A request's input once {@link unbind} has cleared a group: the same, without that property.
|
|
658
|
+
*
|
|
659
|
+
* @remarks
|
|
660
|
+
* An input left with no properties becomes `unknown`, so `undefined` satisfies it.
|
|
661
|
+
*/
|
|
662
|
+
export type Unbound<I, K extends Group> = K extends keyof I ? [keyof Omit<I, K>] extends [never] ? unknown : Omit<I, K> : I;
|
|
663
|
+
/**
|
|
664
|
+
* Remove a group's earlier bindings, their input and the shape they state.
|
|
665
|
+
*
|
|
666
|
+
* @example
|
|
667
|
+
* ```ts
|
|
668
|
+
* import { Schema } from "effect";
|
|
669
|
+
* import { Request } from "viell-effect-schema-http";
|
|
670
|
+
* const paged = Request.get("https://api.example.test/things").pipe(
|
|
671
|
+
* Request.query(Schema.Struct({ page: Schema.FiniteFromString })),
|
|
672
|
+
* );
|
|
673
|
+
* const sorted = paged.pipe(
|
|
674
|
+
* Request.unbind("query"),
|
|
675
|
+
* Request.query(Schema.Struct({ sort: Schema.String })),
|
|
676
|
+
* );
|
|
677
|
+
* // Input: { query: { sort: "name" } }; URL query: ?sort=name
|
|
678
|
+
* ```
|
|
679
|
+
*
|
|
680
|
+
* @remarks
|
|
681
|
+
* Takes `"path"`, `"query"`, `"headers"` or `"body"`, and affects only bindings before it. To
|
|
682
|
+
* replace a binding, unbind the group and bind again. Raw declarations stay, and go out unchecked
|
|
683
|
+
* once no schema states the group's shape. A constructor path and query are declarations, so they
|
|
684
|
+
* stay too. For `"path"`, the text of earlier {@link path} calls goes with their values, and
|
|
685
|
+
* {@link setPath} is what changes the constructor's path.
|
|
686
|
+
*
|
|
687
|
+
* Other steps stay as well. A {@link mapEffect} finds the removed property `undefined`, and where
|
|
688
|
+
* no other input is left the whole input is `undefined`, so reading the property there is a defect.
|
|
689
|
+
* After {@link mapInput}, the binding is skipped, but the mapped input keeps whatever the mapping
|
|
690
|
+
* requires. The request's error and service types keep what the removed bindings added.
|
|
691
|
+
*/
|
|
692
|
+
export declare const unbind: <K extends Group>(group: K) => <I, E, R>(self: Request<I, E, R>) => Request<Unbound<I, K>, E, R>;
|
|
269
693
|
/**
|
|
270
694
|
* Adapt your application's argument to this request's required input.
|
|
271
695
|
*
|
|
272
696
|
* @remarks
|
|
273
697
|
* Use it to rename fields or supply defaults while preserving schema validation. The mapping
|
|
274
|
-
* runs during preparation; a thrown exception is a defect.
|
|
698
|
+
* runs during preparation; a thrown exception is a defect. It returns the request's whole input,
|
|
699
|
+
* which {@link InputOf} names.
|
|
275
700
|
*/
|
|
276
|
-
export declare const mapInput: <I, I2>(f: (input: I2) => I) => <E, R
|
|
701
|
+
export declare const mapInput: <I, I2>(f: (input: I2) => I) => <E, R>(self: Request<I, E, R>) => Request<I2, E, R>;
|
|
277
702
|
/**
|
|
278
703
|
* Edit the native request with an Effect during preparation.
|
|
279
704
|
*
|
|
@@ -284,15 +709,23 @@ export declare const mapInput: <I, I2>(f: (input: I2) => I) => <E, R, H extends
|
|
|
284
709
|
* applies afterwards.
|
|
285
710
|
*
|
|
286
711
|
* A transform can be reused on requests whose input contains what its function reads. Inline
|
|
287
|
-
* callbacks in `pipe` infer their input from the request.
|
|
712
|
+
* callbacks in `pipe` infer their input from the request. This is the deliberate way around a
|
|
713
|
+
* group's schemas: what it edits on the native request is outside their check.
|
|
288
714
|
*/
|
|
289
|
-
export declare const mapEffect: <I, E2, R2>(f: (request: HttpClientRequest.HttpClientRequest, input: I) => Effect.Effect<HttpClientRequest.HttpClientRequest, E2, R2>) => <E, R,
|
|
715
|
+
export declare const mapEffect: <I, E2, R2>(f: (request: HttpClientRequest.HttpClientRequest, input: I) => Effect.Effect<HttpClientRequest.HttpClientRequest, E2, R2>) => <E, R, I2 extends I = I>(self: Request<I, E, R> | Request<I2, E, R>) => Request<I2, E | E2, R | R2>;
|
|
290
716
|
/**
|
|
291
717
|
* A prepared native request and the header lines a supporting provider can send.
|
|
718
|
+
*
|
|
719
|
+
* @remarks
|
|
720
|
+
* `request.url` holds the joined URL and its query. A fragment is in `request.hash`, as written.
|
|
292
721
|
*/
|
|
293
722
|
export interface Compiled {
|
|
723
|
+
/** The native request, with its URL and query joined. */
|
|
294
724
|
readonly request: HttpClientRequest.HttpClientRequest;
|
|
725
|
+
/** The header lines, in the spelling they were written with. */
|
|
295
726
|
readonly headerLines: Transport.Lines;
|
|
727
|
+
/** Lowercase header names the request removed, so the library adds no automatic value. */
|
|
728
|
+
readonly removedHeaders: ReadonlySet<string>;
|
|
296
729
|
}
|
|
297
730
|
/**
|
|
298
731
|
* Prepare a request and its header lines without sending it.
|
|
@@ -308,17 +741,19 @@ export interface Compiled {
|
|
|
308
741
|
* ```
|
|
309
742
|
*
|
|
310
743
|
* @remarks
|
|
311
|
-
* Each evaluation resolves computed fields
|
|
312
|
-
* any services used by the input schemas
|
|
313
|
-
*
|
|
744
|
+
* Each evaluation resolves computed fields, encodes input and resolves the base URL once, in that
|
|
745
|
+
* order. The returned Effect requires any services used by the input schemas, preparation or the
|
|
746
|
+
* base URL. A declared field that does not fit the schemas bound to its group fails here with
|
|
747
|
+
* {@link InvalidRequest}, whatever the input. Use {@link materialize} when only the native request
|
|
748
|
+
* is needed.
|
|
314
749
|
*/
|
|
315
|
-
export declare const compile: <I, E, R
|
|
750
|
+
export declare const compile: <I, E, R>(self: Request<I, E, R>, input: I) => Effect.Effect<Compiled, InvalidRequest | E, R>;
|
|
316
751
|
/**
|
|
317
752
|
* Prepare only the native request, without sending it.
|
|
318
753
|
*
|
|
319
754
|
* @remarks
|
|
320
755
|
* Use {@link compile} to retain the original header-line representation as well.
|
|
321
756
|
*/
|
|
322
|
-
export declare const materialize: <I, E, R
|
|
757
|
+
export declare const materialize: <I, E, R>(self: Request<I, E, R>, input: I) => Effect.Effect<HttpClientRequest.HttpClientRequest, E | InvalidRequest, R>;
|
|
323
758
|
export {};
|
|
324
759
|
//# sourceMappingURL=Request.d.ts.map
|