viell-effect-schema-http 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -29
- package/dist/http/Body.d.ts +23 -13
- package/dist/http/Body.d.ts.map +1 -1
- package/dist/http/Body.js +18 -8
- package/dist/http/Body.js.map +1 -1
- package/dist/http/Operation.d.ts +270 -13
- package/dist/http/Operation.d.ts.map +1 -1
- package/dist/http/Operation.js +210 -11
- package/dist/http/Operation.js.map +1 -1
- package/dist/http/Policy.d.ts +3 -2
- package/dist/http/Policy.d.ts.map +1 -1
- package/dist/http/Policy.js +3 -2
- package/dist/http/Policy.js.map +1 -1
- package/dist/http/Request.d.ts +477 -85
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +621 -141
- package/dist/http/Request.js.map +1 -1
- package/dist/http/Response.d.ts +119 -5
- package/dist/http/Response.d.ts.map +1 -1
- package/dist/http/Response.js +88 -7
- package/dist/http/Response.js.map +1 -1
- package/dist/http/UrlEncoding.d.ts +23 -7
- package/dist/http/UrlEncoding.d.ts.map +1 -1
- package/dist/http/UrlEncoding.js +7 -3
- package/dist/http/UrlEncoding.js.map +1 -1
- package/dist/http/internal/fields.d.ts +17 -4
- package/dist/http/internal/fields.d.ts.map +1 -1
- package/dist/http/internal/fields.js +11 -6
- package/dist/http/internal/fields.js.map +1 -1
- package/dist/http/internal/input.d.ts +39 -0
- package/dist/http/internal/input.d.ts.map +1 -0
- package/dist/http/internal/input.js +40 -0
- package/dist/http/internal/input.js.map +1 -0
- package/dist/http/internal/shape.d.ts +44 -0
- package/dist/http/internal/shape.d.ts.map +1 -0
- package/dist/http/internal/shape.js +179 -0
- package/dist/http/internal/shape.js.map +1 -0
- package/package.json +12 -12
- package/src/http/Body.ts +39 -18
- package/src/http/Operation.ts +468 -33
- package/src/http/Policy.ts +3 -2
- package/src/http/Request.ts +1075 -251
- package/src/http/Response.ts +235 -22
- package/src/http/UrlEncoding.ts +31 -8
- package/src/http/internal/fields.ts +37 -9
- package/src/http/internal/input.ts +123 -0
- package/src/http/internal/shape.ts +279 -0
package/src/http/Operation.ts
CHANGED
|
@@ -28,20 +28,69 @@ import * as Transport from "./Transport.js";
|
|
|
28
28
|
* @remarks
|
|
29
29
|
* `execute(input)` returns a cold Effect: work starts when it is evaluated, and each evaluation
|
|
30
30
|
* runs again. `Operation.make` supplies HTTP execution; `fromEffect` adapts an existing function.
|
|
31
|
-
* With `make`, the request bindings determine `I`.
|
|
32
|
-
*
|
|
31
|
+
* With `make`, the request bindings determine `I`. {@link InputOf} extracts it.
|
|
32
|
+
*
|
|
33
|
+
* `make` also keeps what it was built from in `parts`, which `P` describes, so {@link mapRequest},
|
|
34
|
+
* {@link mapResponse} and {@link mapOptions} can derive a variant. Annotating a value as
|
|
35
|
+
* `Operation<I, A, E, R>` leaves `P` unknown, which stops those functions compiling on it.
|
|
33
36
|
*/
|
|
34
|
-
export interface Operation<
|
|
37
|
+
export interface Operation<
|
|
38
|
+
in I,
|
|
39
|
+
out A,
|
|
40
|
+
out E = never,
|
|
41
|
+
out R = never,
|
|
42
|
+
out P = unknown,
|
|
43
|
+
>
|
|
35
44
|
extends Pipeable.Pipeable {
|
|
36
45
|
readonly execute: (input: I) => Effect.Effect<A, E, R>;
|
|
46
|
+
/** What the operation was built from. A value from {@link fromEffect} has no parts. */
|
|
47
|
+
readonly parts?: P;
|
|
37
48
|
}
|
|
38
49
|
|
|
50
|
+
/**
|
|
51
|
+
* The input an operation's `execute` takes.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { Schema } from "effect";
|
|
56
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
57
|
+
* const search = Operation.make(
|
|
58
|
+
* Request.get("https://api.example.test/things").pipe(
|
|
59
|
+
* Request.query(Schema.Struct({ sort: Schema.String })),
|
|
60
|
+
* ),
|
|
61
|
+
* Response.status(200, Body.json),
|
|
62
|
+
* );
|
|
63
|
+
* type SearchInput = Operation.InputOf<typeof search>;
|
|
64
|
+
* const searchAll = (input: SearchInput) => search.execute(input);
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* `Request.InputOf` gives the same for a request.
|
|
69
|
+
*/
|
|
70
|
+
export type InputOf<O> =
|
|
71
|
+
O extends Operation<infer I, unknown, unknown, unknown, unknown> ? I : never;
|
|
72
|
+
|
|
39
73
|
/**
|
|
40
74
|
* Adapt an Effect-returning function to the operation interface.
|
|
41
75
|
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
79
|
+
* const health = Operation.make(
|
|
80
|
+
* Request.get("https://api.example.test/health"),
|
|
81
|
+
* Response.status(200, Body.text),
|
|
82
|
+
* );
|
|
83
|
+
* // Callers can run this and map its result, and cannot adjust what it sends.
|
|
84
|
+
* const sealed = Operation.fromEffect(health.execute);
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
42
87
|
* @remarks
|
|
43
88
|
* The function is suspended until evaluation. This adapter adds no HTTP dispatch, decoding,
|
|
44
89
|
* retry policy or scope management beyond what the supplied function already does.
|
|
90
|
+
*
|
|
91
|
+
* The result has no parts, at compile time or at run time. So
|
|
92
|
+
* `Operation.fromEffect(operation.execute)` seals a definition: it keeps the same types and offers
|
|
93
|
+
* no adjustment.
|
|
45
94
|
*/
|
|
46
95
|
export const fromEffect = <I, A, E, R>(
|
|
47
96
|
execute: (input: I) => Effect.Effect<A, E, R>,
|
|
@@ -68,11 +117,16 @@ export interface Options<
|
|
|
68
117
|
* Applied after request preparation, suitable for fresh credentials or URL-dependent signatures.
|
|
69
118
|
* Run a signer after other request edits it must cover. Native status filters can reject responses
|
|
70
119
|
* before the declared response contract sees them.
|
|
120
|
+
* `attempt` starts at `1` and matches the `http.attempt` log annotation. A function with an optional
|
|
121
|
+
* second parameter of its own receives the number there, so wrap one in an arrow function.
|
|
71
122
|
* Appending native URL parameters can reserialize the entire query as form data, changing bare keys and
|
|
72
123
|
* escapes. Prefer Request query combinators, or preserve the query text when editing the URL directly.
|
|
73
124
|
*/
|
|
74
125
|
readonly transformClient?:
|
|
75
|
-
| ((
|
|
126
|
+
| ((
|
|
127
|
+
client: HttpClient.HttpClient,
|
|
128
|
+
attempt: number,
|
|
129
|
+
) => HttpClient.HttpClient.With<E, R>)
|
|
76
130
|
| undefined;
|
|
77
131
|
}
|
|
78
132
|
|
|
@@ -109,14 +163,16 @@ const outcome = (exit: Exit.Exit<unknown, unknown>): string =>
|
|
|
109
163
|
* Each evaluation prepares input once. Each attempt then owns a scope covering client execution
|
|
110
164
|
* and body decoding; cleanup completes before classification or a retry delay. Decoder and
|
|
111
165
|
* client `Scope` requirements are supplied by the attempt. Interruption and defects never retry.
|
|
112
|
-
* An automatic `Accept` header is added only when the request has
|
|
166
|
+
* An automatic `Accept` header is added only when the request declares none and has not removed
|
|
167
|
+
* it with `Request.removeHeader`.
|
|
168
|
+
*
|
|
169
|
+
* The result keeps its parts, so a definition written once can be adjusted where it is used with
|
|
170
|
+
* {@link mapRequest}, {@link mapResponse} and {@link mapOptions}. The original never changes.
|
|
113
171
|
*/
|
|
114
172
|
export const make = <
|
|
115
173
|
I,
|
|
116
174
|
E,
|
|
117
175
|
R,
|
|
118
|
-
H extends boolean,
|
|
119
|
-
Q extends boolean,
|
|
120
176
|
A,
|
|
121
177
|
E2,
|
|
122
178
|
R2,
|
|
@@ -124,22 +180,15 @@ export const make = <
|
|
|
124
180
|
R3 = never,
|
|
125
181
|
EP = never,
|
|
126
182
|
RP = never,
|
|
183
|
+
C extends Response.AnyCase = Response.AnyCase,
|
|
127
184
|
>(
|
|
128
|
-
request: Request.Request<I, E, R
|
|
129
|
-
response: Response.Response<A, E2, R2>,
|
|
185
|
+
request: Request.Request<I, E, R>,
|
|
186
|
+
response: Response.Response<A, E2, R2, C>,
|
|
130
187
|
options?: Options<E3, R3, EP, RP>,
|
|
131
|
-
):
|
|
132
|
-
I,
|
|
133
|
-
A,
|
|
134
|
-
|
|
135
|
-
| E3
|
|
136
|
-
| EP
|
|
137
|
-
| Request.InvalidRequest
|
|
138
|
-
| HttpClientError.HttpClientError
|
|
139
|
-
| Response.UnexpectedResponse
|
|
140
|
-
| Response.DecodeError<E2 | HttpClientError.HttpClientError>
|
|
141
|
-
| Cause.TimeoutError,
|
|
142
|
-
R | RP | Exclude<R2 | R3, Scope.Scope> | HttpClient.HttpClient
|
|
188
|
+
): Http<
|
|
189
|
+
Request.Request<I, E, R>,
|
|
190
|
+
Response.Response<A, E2, R2, C>,
|
|
191
|
+
Options<E3, R3, EP, RP>
|
|
143
192
|
> => {
|
|
144
193
|
const policy: Policy.Policy<EP, RP> = options?.policy ?? Policy.none;
|
|
145
194
|
const accept = response.accepts.join(", ");
|
|
@@ -147,7 +196,9 @@ export const make = <
|
|
|
147
196
|
function* (input: I) {
|
|
148
197
|
const compiled = yield* Request.compile(request, input);
|
|
149
198
|
const prepared =
|
|
150
|
-
accept !== "" &&
|
|
199
|
+
accept !== "" &&
|
|
200
|
+
compiled.request.headers.accept === undefined &&
|
|
201
|
+
!compiled.removedHeaders.has("accept")
|
|
151
202
|
? HttpClientRequest.setHeader(compiled.request, "accept", accept)
|
|
152
203
|
: compiled.request;
|
|
153
204
|
const client = yield* HttpClient.HttpClient;
|
|
@@ -174,7 +225,8 @@ export const make = <
|
|
|
174
225
|
> =
|
|
175
226
|
options?.transformClient === undefined
|
|
176
227
|
? HttpClient.withScope(client).execute
|
|
177
|
-
: HttpClient.withScope(options.transformClient(client))
|
|
228
|
+
: HttpClient.withScope(options.transformClient(client, attempt))
|
|
229
|
+
.execute;
|
|
178
230
|
// A fresh request identity keeps the scoped abort controller of this attempt separate.
|
|
179
231
|
const received = yield* send(
|
|
180
232
|
HttpClientRequest.setHeaders(prepared, {}),
|
|
@@ -254,7 +306,11 @@ export const make = <
|
|
|
254
306
|
),
|
|
255
307
|
Effect.withLogSpan("schema-http.operation"),
|
|
256
308
|
);
|
|
257
|
-
return {
|
|
309
|
+
return {
|
|
310
|
+
...Pipeable.Prototype,
|
|
311
|
+
execute,
|
|
312
|
+
parts: { request, response, options, step: undefined },
|
|
313
|
+
};
|
|
258
314
|
};
|
|
259
315
|
|
|
260
316
|
/**
|
|
@@ -274,27 +330,406 @@ export const execute: {
|
|
|
274
330
|
self.execute(input),
|
|
275
331
|
);
|
|
276
332
|
|
|
333
|
+
/**
|
|
334
|
+
* The conversion {@link map} and {@link mapEffect} recorded, run once after the parts.
|
|
335
|
+
*
|
|
336
|
+
* @remarks
|
|
337
|
+
* Only its failures and services are in the type. Its result is the operation's own `A`, and what
|
|
338
|
+
* it was written for is what the response still produces, so neither is repeated here.
|
|
339
|
+
*/
|
|
340
|
+
export interface Step<out E = never, out R = never> {
|
|
341
|
+
readonly run: (value: never) => Effect.Effect<unknown, E, R>;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* What {@link make} keeps: the three parts as given, and any recorded {@link Step}.
|
|
346
|
+
*
|
|
347
|
+
* @remarks
|
|
348
|
+
* Read a part with `operation.parts?.request`. Each whole part is one type parameter, which keeps
|
|
349
|
+
* the parameter count down as `Schema.decodeTo` does.
|
|
350
|
+
*
|
|
351
|
+
* `S` is invariant: widening it by annotation could otherwise make any response look as if it fits
|
|
352
|
+
* a recorded step.
|
|
353
|
+
*
|
|
354
|
+
* {@link mapRequest}, {@link mapResponse}, {@link mapOptions} and {@link unmap} build from these,
|
|
355
|
+
* so they drop an `execute` replaced on the value. {@link map} and {@link mapEffect} wrap
|
|
356
|
+
* `execute` itself, and keep it.
|
|
357
|
+
*/
|
|
358
|
+
export interface Parts<out Q, in out S, out O, out T = undefined> {
|
|
359
|
+
readonly request: Q;
|
|
360
|
+
readonly response: S;
|
|
361
|
+
readonly options: O | undefined;
|
|
362
|
+
readonly step: T;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
type RequestFailure<Q> =
|
|
366
|
+
Q extends Request.Request<never, infer E, unknown> ? E : never;
|
|
367
|
+
type RequestServices<Q> =
|
|
368
|
+
Q extends Request.Request<never, unknown, infer R> ? R : never;
|
|
369
|
+
// The cases are inferred too, or the check would not resolve while `C` is still generic.
|
|
370
|
+
type ValueOf<S> =
|
|
371
|
+
S extends Response.Response<infer A, unknown, unknown, infer _C> ? A : never;
|
|
372
|
+
type ResponseFailure<S> =
|
|
373
|
+
S extends Response.Response<unknown, infer E, unknown, infer _C> ? E : never;
|
|
374
|
+
type ResponseServices<S> =
|
|
375
|
+
S extends Response.Response<unknown, unknown, infer R, infer _C> ? R : never;
|
|
376
|
+
// Every slot is inferred: `Options` is invariant, and a fixed slot silently gives `never`.
|
|
377
|
+
type ClientFailure<O> =
|
|
378
|
+
O extends Options<infer E, infer _R, infer _EP, infer _RP> ? E : never;
|
|
379
|
+
type ClientServices<O> =
|
|
380
|
+
O extends Options<infer _E, infer R, infer _EP, infer _RP> ? R : never;
|
|
381
|
+
type PolicyFailure<O> =
|
|
382
|
+
O extends Options<infer _E, infer _R, infer E, infer _RP> ? E : never;
|
|
383
|
+
type PolicyServices<O> =
|
|
384
|
+
O extends Options<infer _E, infer _R, infer _EP, infer R> ? R : never;
|
|
385
|
+
type StepFailure<T> = T extends Step<infer E, unknown> ? E : never;
|
|
386
|
+
type StepServices<T> = T extends Step<unknown, infer R> ? R : never;
|
|
387
|
+
|
|
388
|
+
/** An operation fresh from {@link make}: three parts, no step, and the response's own result. */
|
|
389
|
+
export type Http<Q, S, O> = Derived<ValueOf<S>, Q, S, O>;
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* An operation built from parts, returning `B`.
|
|
393
|
+
*
|
|
394
|
+
* @remarks
|
|
395
|
+
* `B` comes first so that an editor hover shows the result before the parts. The unions stay
|
|
396
|
+
* inline: a named alias would print with all of `Q`, `S` and `O` repeated inside it.
|
|
397
|
+
*/
|
|
398
|
+
export type Derived<B, Q, S, O, T = undefined> = Operation<
|
|
399
|
+
Request.InputOf<Q>,
|
|
400
|
+
B,
|
|
401
|
+
| RequestFailure<Q>
|
|
402
|
+
| ClientFailure<O>
|
|
403
|
+
| PolicyFailure<O>
|
|
404
|
+
| StepFailure<T>
|
|
405
|
+
| Request.InvalidRequest
|
|
406
|
+
| HttpClientError.HttpClientError
|
|
407
|
+
| Response.UnexpectedResponse
|
|
408
|
+
| Response.DecodeError<ResponseFailure<S> | HttpClientError.HttpClientError>
|
|
409
|
+
| Cause.TimeoutError,
|
|
410
|
+
| RequestServices<Q>
|
|
411
|
+
| PolicyServices<O>
|
|
412
|
+
| StepServices<T>
|
|
413
|
+
| Exclude<ResponseServices<S> | ClientServices<O>, Scope.Scope>
|
|
414
|
+
| HttpClient.HttpClient,
|
|
415
|
+
Parts<Q, S, O, T>
|
|
416
|
+
>;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Any operation carrying the parts `P` and returning `B`.
|
|
420
|
+
*
|
|
421
|
+
* @remarks
|
|
422
|
+
* What {@link mapRequest}, {@link mapResponse}, {@link mapOptions} and {@link unmap} accept. The
|
|
423
|
+
* input, failures and services are left open because a variant works them out again from `P`.
|
|
424
|
+
*/
|
|
425
|
+
export type Adjustable<B, P> = Operation<never, B, unknown, unknown, P>;
|
|
426
|
+
|
|
427
|
+
type Runner = (value: unknown) => Effect.Effect<unknown, unknown, unknown>;
|
|
428
|
+
|
|
429
|
+
/** Parts with their types erased, for the helpers below. Each caller's signature states them. */
|
|
430
|
+
type ErasedParts = Parts<
|
|
431
|
+
Request.Request<never, unknown, unknown>,
|
|
432
|
+
Response.Response<unknown, unknown, unknown>,
|
|
433
|
+
Options<unknown, unknown, unknown, unknown>,
|
|
434
|
+
{ readonly run: Runner } | undefined
|
|
435
|
+
>;
|
|
436
|
+
|
|
437
|
+
/** The types refuse an operation with no parts; a cast or plain JavaScript gets a clear failure. */
|
|
438
|
+
const partsOf = <P>(self: Adjustable<unknown, P>): P => {
|
|
439
|
+
if (self.parts === undefined)
|
|
440
|
+
throw new Error(
|
|
441
|
+
"This operation has no parts to adjust: it came from Operation.fromEffect",
|
|
442
|
+
);
|
|
443
|
+
return self.parts;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/** Rebuild through {@link make}, so the automatic `Accept` follows the response. */
|
|
447
|
+
const rebuild = <Q, S, O, T, Result>(parts: Parts<Q, S, O, T>): Result => {
|
|
448
|
+
const { request, response, options, step } = parts as unknown as ErasedParts;
|
|
449
|
+
const built = make(request, response, options);
|
|
450
|
+
const result: unknown =
|
|
451
|
+
step === undefined
|
|
452
|
+
? built
|
|
453
|
+
: {
|
|
454
|
+
...built,
|
|
455
|
+
execute: (input: never) =>
|
|
456
|
+
Effect.flatMap(built.execute(input), step.run),
|
|
457
|
+
parts: { request, response, options, step },
|
|
458
|
+
};
|
|
459
|
+
return result as Result;
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Derive a variant with a changed request. A recorded result step stays.
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
468
|
+
* const getThings = Operation.make(
|
|
469
|
+
* Request.get("https://api.example.test/things?limit=25"),
|
|
470
|
+
* Response.status(200, Body.json),
|
|
471
|
+
* );
|
|
472
|
+
* const firstFive = Request.setQuery("limit", "5");
|
|
473
|
+
* const fewerThings = getThings.pipe(Operation.mapRequest(firstFive));
|
|
474
|
+
* // getThings still asks for 25.
|
|
475
|
+
* ```
|
|
476
|
+
*
|
|
477
|
+
* @remarks
|
|
478
|
+
* The function receives the current request, so every `Request` function works here, including a
|
|
479
|
+
* binding that changes the operation's input. The automatic `Accept` and the options carry over.
|
|
480
|
+
* The library cannot tell whether a change alters the shape of the answer: selecting fields or
|
|
481
|
+
* embedding relations usually does, and then needs {@link mapResponse} too.
|
|
482
|
+
*
|
|
483
|
+
* Works on any operation from {@link make}, mapped or not. A value from {@link fromEffect} has no
|
|
484
|
+
* parts, and one annotated as a plain `Operation<I, A, E, R>` hides them: both are compile errors.
|
|
485
|
+
*/
|
|
486
|
+
export const mapRequest =
|
|
487
|
+
<I, E, R, I2, E2, R2>(
|
|
488
|
+
f: (request: Request.Request<I, E, R>) => Request.Request<I2, E2, R2>,
|
|
489
|
+
) =>
|
|
490
|
+
<B, S, O, T>(
|
|
491
|
+
self: Adjustable<B, Parts<Request.Request<I, E, R>, S, O, T>>,
|
|
492
|
+
): Derived<B, Request.Request<I2, E2, R2>, S, O, T> => {
|
|
493
|
+
const parts = partsOf(self);
|
|
494
|
+
return rebuild({ ...parts, request: f(parts.request) });
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
interface NoLongerFits<A> {
|
|
498
|
+
readonly "Use Operation.unmap first, then map again: this response no longer fits the mapping.": A;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* The rule {@link mapResponse} applies to a new result `A2`, given the current result `A`.
|
|
503
|
+
*
|
|
504
|
+
* @remarks
|
|
505
|
+
* With no step, `T` is `undefined` and anything fits. With a step, `A2` must be something the
|
|
506
|
+
* current response could produce. An `any` result proves nothing, so it does not fit either.
|
|
507
|
+
*
|
|
508
|
+
* `W` is everything the function returned, and stays `never` when nothing was inferred from it.
|
|
509
|
+
* That means the function itself does not compile, as with a key the contract lacks. `A2` then
|
|
510
|
+
* reads `unknown` for no reason of its own, so the rule stands aside and leaves the function's own
|
|
511
|
+
* error as the only one. A function that returns a response, or `any`, always gives `W` a value, so
|
|
512
|
+
* a result that really is `unknown` is still refused.
|
|
513
|
+
*/
|
|
514
|
+
export type Fits<A2, A, T, W = unknown> = [T] extends [undefined]
|
|
515
|
+
? unknown
|
|
516
|
+
: [W] extends [never]
|
|
517
|
+
? unknown
|
|
518
|
+
: 0 extends 1 & A2
|
|
519
|
+
? NoLongerFits<A2>
|
|
520
|
+
: [A2] extends [A]
|
|
521
|
+
? unknown
|
|
522
|
+
: NoLongerFits<A2>;
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Derive a variant with a changed response. A recorded result step stays, and has to fit.
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* ```ts
|
|
529
|
+
* import { Schema } from "effect";
|
|
530
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
531
|
+
* const Thing = Schema.Struct({ id: Schema.Number, name: Schema.String });
|
|
532
|
+
* const ThingOption = Schema.Struct({ id: Schema.Number });
|
|
533
|
+
* const getThing = Operation.make(
|
|
534
|
+
* Request.get("https://api.example.test/things/1"),
|
|
535
|
+
* Response.status(200, Body.jsonSchema(Thing)),
|
|
536
|
+
* );
|
|
537
|
+
* const asOption = Response.replace(200, Body.jsonSchema(ThingOption));
|
|
538
|
+
* const thingOption = getThing.pipe(Operation.mapResponse(asOption));
|
|
539
|
+
* ```
|
|
540
|
+
*
|
|
541
|
+
* @remarks
|
|
542
|
+
* The function receives the current response, so {@link Response.replace},
|
|
543
|
+
* {@link Response.remove} and {@link Response.orElse} all work here. The automatic `Accept` follows
|
|
544
|
+
* the new response.
|
|
545
|
+
*
|
|
546
|
+
* On a mapped operation the new result must fit: it has to be assignable to the current response's
|
|
547
|
+
* result, and not `any`, because the recorded step was written for that result. So a same-shape
|
|
548
|
+
* replace or a removal works, and anything else is a compile error that names {@link unmap}.
|
|
549
|
+
* A narrowed result is the new baseline, so widening it back needs {@link unmap} as well.
|
|
550
|
+
* This is a compile-time rule: a cast gets past it, and the step then receives the wrong shape.
|
|
551
|
+
*/
|
|
552
|
+
export const mapResponse =
|
|
553
|
+
<
|
|
554
|
+
A,
|
|
555
|
+
E,
|
|
556
|
+
R,
|
|
557
|
+
C extends Response.AnyCase,
|
|
558
|
+
A2,
|
|
559
|
+
E2,
|
|
560
|
+
R2,
|
|
561
|
+
C2 extends Response.AnyCase,
|
|
562
|
+
W = never,
|
|
563
|
+
>(
|
|
564
|
+
f: (
|
|
565
|
+
response: Response.Response<A, E, R, C>,
|
|
566
|
+
) => Response.Response<A2, E2, R2, C2> & W,
|
|
567
|
+
) =>
|
|
568
|
+
<B, Q, O, T>(
|
|
569
|
+
self: Adjustable<B, Parts<Q, Response.Response<A, E, R, C>, O, T>> &
|
|
570
|
+
Fits<A2, A, T, W>,
|
|
571
|
+
): Derived<
|
|
572
|
+
[T] extends [undefined] ? A2 : B,
|
|
573
|
+
Q,
|
|
574
|
+
Response.Response<A2, E2, R2, C2>,
|
|
575
|
+
O,
|
|
576
|
+
T
|
|
577
|
+
> => {
|
|
578
|
+
const parts = partsOf(self);
|
|
579
|
+
return rebuild({ ...parts, response: f(parts.response) });
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Derive a variant with changed options. A recorded result step stays.
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* ```ts
|
|
587
|
+
* import { Schedule } from "effect";
|
|
588
|
+
* import {
|
|
589
|
+
* Body,
|
|
590
|
+
* Operation,
|
|
591
|
+
* Policy,
|
|
592
|
+
* Request,
|
|
593
|
+
* Response,
|
|
594
|
+
* } from "viell-effect-schema-http";
|
|
595
|
+
* const getThings = Operation.make(
|
|
596
|
+
* Request.get("https://api.example.test/things"),
|
|
597
|
+
* Response.status(200, Body.json),
|
|
598
|
+
* );
|
|
599
|
+
* const retryTwice = Policy.make(Schedule.recurs(2));
|
|
600
|
+
* const patient = getThings.pipe(
|
|
601
|
+
* Operation.mapOptions((options) => ({ ...options, policy: retryTwice })),
|
|
602
|
+
* );
|
|
603
|
+
* ```
|
|
604
|
+
*
|
|
605
|
+
* @remarks
|
|
606
|
+
* The function receives the current options, or `undefined` if there are none, and its result
|
|
607
|
+
* replaces them. Spread them to keep what you do not change. A failure or service type that only
|
|
608
|
+
* a dropped option added leaves with it.
|
|
609
|
+
*/
|
|
610
|
+
export const mapOptions =
|
|
611
|
+
<O, E = HttpClientError.HttpClientError, R = never, EP = never, RP = never>(
|
|
612
|
+
f: (options: O | undefined) => Options<E, R, EP, RP>,
|
|
613
|
+
) =>
|
|
614
|
+
<B, Q, S, T>(
|
|
615
|
+
self: Adjustable<B, Parts<Q, S, O, T>>,
|
|
616
|
+
): Derived<B, Q, S, Options<E, R, EP, RP>, T> => {
|
|
617
|
+
const parts = partsOf(self);
|
|
618
|
+
return rebuild({ ...parts, options: f(parts.options) });
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Remove every recorded result step, so the operation returns what its response decodes.
|
|
623
|
+
*
|
|
624
|
+
* @example
|
|
625
|
+
* ```ts
|
|
626
|
+
* import { Schema } from "effect";
|
|
627
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
628
|
+
* const Page = Schema.Struct({ data: Schema.Array(Schema.String) });
|
|
629
|
+
* const Names = Schema.Struct({ data: Schema.Array(Schema.Number) });
|
|
630
|
+
* const getThings = Operation.make(
|
|
631
|
+
* Request.get("https://api.example.test/things"),
|
|
632
|
+
* Response.status(200, Body.jsonSchema(Page)),
|
|
633
|
+
* ).pipe(Operation.map((page) => page.data));
|
|
634
|
+
*
|
|
635
|
+
* const asIds = Response.replace(200, Body.jsonSchema(Names));
|
|
636
|
+
* const thingIds = getThings.pipe(
|
|
637
|
+
* Operation.unmap,
|
|
638
|
+
* Operation.mapResponse(asIds),
|
|
639
|
+
* Operation.map((page) => page.data),
|
|
640
|
+
* );
|
|
641
|
+
* ```
|
|
642
|
+
*
|
|
643
|
+
* @remarks
|
|
644
|
+
* This is the route to a response {@link mapResponse} refuses: remove the mapping, change the
|
|
645
|
+
* response, then map again. The failures and services the steps added leave with them. On an
|
|
646
|
+
* operation with no step this changes nothing.
|
|
647
|
+
*/
|
|
648
|
+
export const unmap = <B, Q, S, O, T>(
|
|
649
|
+
self: Adjustable<B, Parts<Q, S, O, T>>,
|
|
650
|
+
): Http<Q, S, O> => rebuild({ ...partsOf(self), step: undefined });
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* The parts after {@link map} or {@link mapEffect} records a step adding failures `E` and services
|
|
654
|
+
* `R`. An operation with no parts stays that way.
|
|
655
|
+
*
|
|
656
|
+
* @remarks
|
|
657
|
+
* The conditional lives here, in the fifth argument, because a conditional return type would stop
|
|
658
|
+
* `flow` composing these functions.
|
|
659
|
+
*/
|
|
660
|
+
export type Next<P, E, R> = [P] extends [
|
|
661
|
+
Parts<infer Q, infer S, infer O, infer T>,
|
|
662
|
+
]
|
|
663
|
+
? Parts<Q, S, O, Step<StepFailure<T> | E, StepServices<T> | R>>
|
|
664
|
+
: P;
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Wrap the operation's own `execute`, and record the step for a later {@link rebuild}. Two steps
|
|
668
|
+
* combine into one, so the type does not grow with each map.
|
|
669
|
+
*/
|
|
670
|
+
const addStep = <Result>(
|
|
671
|
+
self: Adjustable<unknown, unknown>,
|
|
672
|
+
f: Runner,
|
|
673
|
+
): Result => {
|
|
674
|
+
const wrapped = fromEffect((input: never) =>
|
|
675
|
+
Effect.flatMap(self.execute(input), f),
|
|
676
|
+
);
|
|
677
|
+
const parts = self.parts as ErasedParts | undefined;
|
|
678
|
+
const previous = parts?.step;
|
|
679
|
+
const result: unknown =
|
|
680
|
+
parts === undefined
|
|
681
|
+
? wrapped
|
|
682
|
+
: {
|
|
683
|
+
...wrapped,
|
|
684
|
+
parts: {
|
|
685
|
+
...parts,
|
|
686
|
+
step: {
|
|
687
|
+
run:
|
|
688
|
+
previous === undefined
|
|
689
|
+
? f
|
|
690
|
+
: (value: unknown) => Effect.flatMap(previous.run(value), f),
|
|
691
|
+
},
|
|
692
|
+
},
|
|
693
|
+
};
|
|
694
|
+
return result as Result;
|
|
695
|
+
};
|
|
696
|
+
|
|
277
697
|
/**
|
|
278
698
|
* Transform an operation's successful result with a pure function.
|
|
279
699
|
*
|
|
280
700
|
* @remarks
|
|
281
|
-
* Keeps the input, error and service types. Runs after the operation and its retry policy
|
|
282
|
-
* Use {@link mapEffect} to return a typed failure
|
|
701
|
+
* Keeps the input, error and service types. Runs once, after the operation and its retry policy
|
|
702
|
+
* finish, outside each attempt's scope and timeout. Use {@link mapEffect} to return a typed failure
|
|
703
|
+
* or perform further effectful work.
|
|
704
|
+
*
|
|
705
|
+
* The operation keeps its parts and records this as a step, so {@link mapRequest} and
|
|
706
|
+
* {@link mapOptions} still work on the result, and {@link mapResponse} works while the new result
|
|
707
|
+
* fits. Two maps combine into one step. The mapping applies to the whole result, whichever case
|
|
708
|
+
* produced it. For a conversion that belongs to one case, use `Body.map` on that case's decoder: a
|
|
709
|
+
* later `Response.replace` takes the conversion with the case.
|
|
283
710
|
*/
|
|
284
711
|
export const map =
|
|
285
712
|
<A, B>(f: (value: A) => B) =>
|
|
286
|
-
<I, E, R
|
|
287
|
-
|
|
713
|
+
<I, E, R, P = unknown>(
|
|
714
|
+
self: Operation<I, A, E, R, P>,
|
|
715
|
+
): Operation<I, B, E, R, Next<P, never, never>> =>
|
|
716
|
+
addStep(self, (value) => Effect.succeed(f(value as A)));
|
|
288
717
|
|
|
289
718
|
/**
|
|
290
719
|
* Transform an operation's successful result with an Effect.
|
|
291
720
|
*
|
|
292
721
|
* @remarks
|
|
293
|
-
* Adds the transformation's errors and required services. Runs after the operation's retry
|
|
294
|
-
* policy completes, so failures here do not trigger that
|
|
295
|
-
* response outcome into an application
|
|
722
|
+
* Adds the transformation's errors and required services. Runs once, after the operation's retry
|
|
723
|
+
* policy completes, outside each attempt's scope and timeout, so failures here do not trigger that
|
|
724
|
+
* policy and arrive as themselves. Use it to turn a recognized response outcome into an application
|
|
725
|
+
* error.
|
|
726
|
+
*
|
|
727
|
+
* The operation keeps its parts and records this as a step, as {@link map} describes. Its failures
|
|
728
|
+
* and services stay through a variant and leave with {@link unmap}.
|
|
296
729
|
*/
|
|
297
730
|
export const mapEffect =
|
|
298
731
|
<A, B, E2, R2>(f: (value: A) => Effect.Effect<B, E2, R2>) =>
|
|
299
|
-
<I, E, R
|
|
300
|
-
|
|
732
|
+
<I, E, R, P = unknown>(
|
|
733
|
+
self: Operation<I, A, E, R, P>,
|
|
734
|
+
): Operation<I, B, E | E2, R | R2, Next<P, E2, R2>> =>
|
|
735
|
+
addStep(self, (value) => f(value as A));
|
package/src/http/Policy.ts
CHANGED
|
@@ -87,8 +87,9 @@ export const transient = (reason: Reason): boolean => {
|
|
|
87
87
|
* `Schedule.recurs(n)` permits up to `n` retries after the first attempt. Defaults to
|
|
88
88
|
* {@link transient}, honoring `Retry-After`, and no attempt timeout. Enable retries only when
|
|
89
89
|
* repeating the service action is acceptable; transport failure does not prove it was unapplied.
|
|
90
|
-
*
|
|
91
|
-
*
|
|
90
|
+
* `while` sees every completed outcome, so `while: () => true` also repeats a successful 200 until
|
|
91
|
+
* the schedule ends. The attempt timeout covers client execution and decoding, not initial request
|
|
92
|
+
* preparation or retry delays. Apply `Effect.timeout` around the operation for an overall limit.
|
|
92
93
|
*/
|
|
93
94
|
export const make = <Out, E = never, R = never>(
|
|
94
95
|
schedule: Schedule.Schedule<Out, Reason, E, R>,
|