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