viell-effect-schema-http 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/concurrency/Interaction.d.ts +65 -0
- package/dist/concurrency/Interaction.d.ts.map +1 -0
- package/dist/concurrency/Interaction.js +99 -0
- package/dist/concurrency/Interaction.js.map +1 -0
- package/dist/http/Body.d.ts +162 -0
- package/dist/http/Body.d.ts.map +1 -0
- package/dist/http/Body.js +184 -0
- package/dist/http/Body.js.map +1 -0
- package/dist/http/Operation.d.ts +97 -0
- package/dist/http/Operation.d.ts.map +1 -0
- package/dist/http/Operation.js +146 -0
- package/dist/http/Operation.js.map +1 -0
- package/dist/http/Policy.d.ts +82 -0
- package/dist/http/Policy.d.ts.map +1 -0
- package/dist/http/Policy.js +76 -0
- package/dist/http/Policy.js.map +1 -0
- package/dist/http/Request.d.ts +324 -0
- package/dist/http/Request.d.ts.map +1 -0
- package/dist/http/Request.js +391 -0
- package/dist/http/Request.js.map +1 -0
- package/dist/http/Response.d.ts +126 -0
- package/dist/http/Response.d.ts.map +1 -0
- package/dist/http/Response.js +100 -0
- package/dist/http/Response.js.map +1 -0
- package/dist/http/Transport.d.ts +75 -0
- package/dist/http/Transport.d.ts.map +1 -0
- package/dist/http/Transport.js +91 -0
- package/dist/http/Transport.js.map +1 -0
- package/dist/http/UrlEncoding.d.ts +78 -0
- package/dist/http/UrlEncoding.d.ts.map +1 -0
- package/dist/http/UrlEncoding.js +64 -0
- package/dist/http/UrlEncoding.js.map +1 -0
- package/dist/http/internal/fields.d.ts +12 -0
- package/dist/http/internal/fields.d.ts.map +1 -0
- package/dist/http/internal/fields.js +21 -0
- package/dist/http/internal/fields.js.map +1 -0
- package/dist/main.d.ts +33 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +33 -0
- package/dist/main.js.map +1 -0
- package/package.json +125 -4
- package/src/concurrency/Interaction.ts +192 -0
- package/src/http/Body.ts +318 -0
- package/src/http/Operation.ts +300 -0
- package/src/http/Policy.ts +130 -0
- package/src/http/Request.ts +700 -0
- package/src/http/Response.ts +201 -0
- package/src/http/Transport.ts +143 -0
- package/src/http/UrlEncoding.ts +119 -0
- package/src/http/internal/fields.ts +46 -0
- package/src/main.ts +32 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Cause, Effect, Pipeable, type Scope } from "effect";
|
|
2
|
+
import { HttpClient, HttpClientError } from "effect/http";
|
|
3
|
+
import * as Policy from "./Policy.js";
|
|
4
|
+
import * as Request from "./Request.js";
|
|
5
|
+
import * as Response from "./Response.js";
|
|
6
|
+
/**
|
|
7
|
+
* An executable contract with typed input, output, failures and required services.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* `execute(input)` returns a cold Effect: work starts when it is evaluated, and each evaluation
|
|
11
|
+
* runs again. `Operation.make` supplies HTTP execution; `fromEffect` adapts an existing function.
|
|
12
|
+
* With `make`, the request bindings determine `I`. Extract an operation's input type with
|
|
13
|
+
* `Parameters<typeof operation.execute>[0]`.
|
|
14
|
+
*/
|
|
15
|
+
export interface Operation<in I, out A, out E = never, out R = never> extends Pipeable.Pipeable {
|
|
16
|
+
readonly execute: (input: I) => Effect.Effect<A, E, R>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Adapt an Effect-returning function to the operation interface.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* The function is suspended until evaluation. This adapter adds no HTTP dispatch, decoding,
|
|
23
|
+
* retry policy or scope management beyond what the supplied function already does.
|
|
24
|
+
*/
|
|
25
|
+
export declare const fromEffect: <I, A, E, R>(execute: (input: I) => Effect.Effect<A, E, R>) => Operation<I, A, E, R>;
|
|
26
|
+
/**
|
|
27
|
+
* Optional retry policy and per-attempt HTTP client transformation.
|
|
28
|
+
*/
|
|
29
|
+
export interface Options<E = HttpClientError.HttpClientError, R = never, EP = never, RP = never> {
|
|
30
|
+
/** Retry selection, budget and attempt timeout. Defaults to `Policy.none`. */
|
|
31
|
+
readonly policy?: Policy.Policy<EP, RP> | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Customize the HTTP client separately for each attempt.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Applied after request preparation, suitable for fresh credentials or URL-dependent signatures.
|
|
37
|
+
* Run a signer after other request edits it must cover. Native status filters can reject responses
|
|
38
|
+
* before the declared response contract sees them.
|
|
39
|
+
* Appending native URL parameters can reserialize the entire query as form data, changing bare keys and
|
|
40
|
+
* escapes. Prefer Request query combinators, or preserve the query text when editing the URL directly.
|
|
41
|
+
*/
|
|
42
|
+
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient.With<E, R>) | undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Combine a request and response contract into an executable HTTP operation.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { Effect } from "effect";
|
|
50
|
+
* import { FetchHttpClient } from "effect/http";
|
|
51
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
52
|
+
* const health = Operation.make(
|
|
53
|
+
* Request.get("https://api.example.test/health"),
|
|
54
|
+
* Response.status(200, Body.text),
|
|
55
|
+
* );
|
|
56
|
+
* const program = health.execute(undefined).pipe(Effect.provide(FetchHttpClient.layer));
|
|
57
|
+
* // Evaluate program only after choosing your real service URL.
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* Defaults to one attempt with no library-imposed timeout. The caller supplies Effect's
|
|
62
|
+
* `HttpClient` service and evaluates `execute(input)`.
|
|
63
|
+
*
|
|
64
|
+
* Each evaluation prepares input once. Each attempt then owns a scope covering client execution
|
|
65
|
+
* and body decoding; cleanup completes before classification or a retry delay. Decoder and
|
|
66
|
+
* client `Scope` requirements are supplied by the attempt. Interruption and defects never retry.
|
|
67
|
+
* An automatic `Accept` header is added only when the request has none.
|
|
68
|
+
*/
|
|
69
|
+
export declare const make: <I, E, R, H extends boolean, Q extends boolean, A, E2, R2, E3 = HttpClientError.HttpClientError, R3 = never, EP = never, RP = never>(request: Request.Request<I, E, R, H, Q>, response: Response.Response<A, E2, R2>, options?: Options<E3, R3, EP, RP>) => Operation<I, A, E | E3 | EP | Request.InvalidRequest | HttpClientError.HttpClientError | Response.UnexpectedResponse | Response.DecodeError<E2 | HttpClientError.HttpClientError> | Cause.TimeoutError, R | RP | Exclude<R2 | R3, Scope.Scope> | HttpClient.HttpClient>;
|
|
70
|
+
/**
|
|
71
|
+
* Return the Effect for an operation's input, in direct or pipeable form.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* Equivalent to `operation.execute(input)`. It does not itself evaluate the Effect.
|
|
75
|
+
*/
|
|
76
|
+
export declare const execute: {
|
|
77
|
+
<I>(input: I): <A, E, R>(self: Operation<I, A, E, R>) => Effect.Effect<A, E, R>;
|
|
78
|
+
<I, A, E, R>(self: Operation<I, A, E, R>, input: I): Effect.Effect<A, E, R>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Transform an operation's successful result with a pure function.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* Keeps the input, error and service types. Runs after the operation and its retry policy finish.
|
|
85
|
+
* Use {@link mapEffect} to return a typed failure or perform further effectful work.
|
|
86
|
+
*/
|
|
87
|
+
export declare const map: <A, B>(f: (value: A) => B) => <I, E, R>(self: Operation<I, A, E, R>) => Operation<I, B, E, R>;
|
|
88
|
+
/**
|
|
89
|
+
* Transform an operation's successful result with an Effect.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* Adds the transformation's errors and required services. Runs after the operation's retry
|
|
93
|
+
* policy completes, so failures here do not trigger that policy. Use it to turn a recognized
|
|
94
|
+
* response outcome into an application error.
|
|
95
|
+
*/
|
|
96
|
+
export declare const mapEffect: <A, B, E2, R2>(f: (value: A) => Effect.Effect<B, E2, R2>) => <I, E, R>(self: Operation<I, A, E, R>) => Operation<I, B, E | E2, R | R2>;
|
|
97
|
+
//# sourceMappingURL=Operation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Operation.d.ts","sourceRoot":"","sources":["../../src/http/Operation.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EAGL,MAAM,EAIN,QAAQ,EAGR,KAAK,KAAK,EACX,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,UAAU,EACV,eAAe,EAGhB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAC;AAG1C;;;;;;;;GAQG;AACH,MAAM,WAAW,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,CAClE,SAAQ,QAAQ,CAAC,QAAQ;IACzB,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;CACxD;AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACnC,SAAS,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,KAC5C,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAGrB,CAAC;AAEH;;GAEG;AACH,MAAM,WAAW,OAAO,CACtB,CAAC,GAAG,eAAe,CAAC,eAAe,EACnC,CAAC,GAAG,KAAK,EACT,EAAE,GAAG,KAAK,EACV,EAAE,G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Cause, Clock, Duration, Effect, Exit, Function, Option, Pipeable, Pull, Schedule, } from "effect";
|
|
2
|
+
import { HttpClient, HttpClientError, HttpClientRequest, } from "effect/http";
|
|
3
|
+
import * as Policy from "./Policy.js";
|
|
4
|
+
import * as Request from "./Request.js";
|
|
5
|
+
import * as Response from "./Response.js";
|
|
6
|
+
import * as Transport from "./Transport.js";
|
|
7
|
+
/**
|
|
8
|
+
* Adapt an Effect-returning function to the operation interface.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* The function is suspended until evaluation. This adapter adds no HTTP dispatch, decoding,
|
|
12
|
+
* retry policy or scope management beyond what the supplied function already does.
|
|
13
|
+
*/
|
|
14
|
+
export const fromEffect = (execute) => ({
|
|
15
|
+
...Pipeable.Prototype,
|
|
16
|
+
execute: (input) => Effect.suspend(() => execute(input)),
|
|
17
|
+
});
|
|
18
|
+
/** Outcome categories only: errors can contain credentials, response text or schema input. */
|
|
19
|
+
const outcome = (exit) => Exit.isSuccess(exit)
|
|
20
|
+
? "success"
|
|
21
|
+
: Cause.hasInterrupts(exit.cause)
|
|
22
|
+
? "interrupted"
|
|
23
|
+
: Cause.hasDies(exit.cause)
|
|
24
|
+
? "defect"
|
|
25
|
+
: "failure";
|
|
26
|
+
/**
|
|
27
|
+
* Combine a request and response contract into an executable HTTP operation.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { Effect } from "effect";
|
|
32
|
+
* import { FetchHttpClient } from "effect/http";
|
|
33
|
+
* import { Body, Operation, Request, Response } from "viell-effect-schema-http";
|
|
34
|
+
* const health = Operation.make(
|
|
35
|
+
* Request.get("https://api.example.test/health"),
|
|
36
|
+
* Response.status(200, Body.text),
|
|
37
|
+
* );
|
|
38
|
+
* const program = health.execute(undefined).pipe(Effect.provide(FetchHttpClient.layer));
|
|
39
|
+
* // Evaluate program only after choosing your real service URL.
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Defaults to one attempt with no library-imposed timeout. The caller supplies Effect's
|
|
44
|
+
* `HttpClient` service and evaluates `execute(input)`.
|
|
45
|
+
*
|
|
46
|
+
* Each evaluation prepares input once. Each attempt then owns a scope covering client execution
|
|
47
|
+
* and body decoding; cleanup completes before classification or a retry delay. Decoder and
|
|
48
|
+
* client `Scope` requirements are supplied by the attempt. Interruption and defects never retry.
|
|
49
|
+
* An automatic `Accept` header is added only when the request has none.
|
|
50
|
+
*/
|
|
51
|
+
export const make = (request, response, options) => {
|
|
52
|
+
const policy = options?.policy ?? Policy.none;
|
|
53
|
+
const accept = response.accepts.join(", ");
|
|
54
|
+
const execute = Effect.fnUntraced(function* (input) {
|
|
55
|
+
const compiled = yield* Request.compile(request, input);
|
|
56
|
+
const prepared = accept !== "" && compiled.request.headers.accept === undefined
|
|
57
|
+
? HttpClientRequest.setHeader(compiled.request, "accept", accept)
|
|
58
|
+
: compiled.request;
|
|
59
|
+
const client = yield* HttpClient.HttpClient;
|
|
60
|
+
const step = yield* Schedule.toStep(policy.schedule);
|
|
61
|
+
const method = prepared.method;
|
|
62
|
+
let attempt = 0;
|
|
63
|
+
while (true) {
|
|
64
|
+
attempt++;
|
|
65
|
+
yield* Effect.logDebug("HTTP attempt started").pipe(Effect.annotateLogs({
|
|
66
|
+
"http.method": method,
|
|
67
|
+
"http.attempt": attempt,
|
|
68
|
+
}));
|
|
69
|
+
let metadata;
|
|
70
|
+
const execution = Effect.scoped(Effect.gen(function* () {
|
|
71
|
+
const send = options?.transformClient === undefined
|
|
72
|
+
? HttpClient.withScope(client).execute
|
|
73
|
+
: HttpClient.withScope(options.transformClient(client)).execute;
|
|
74
|
+
// A fresh request identity keeps the scoped abort controller of this attempt separate.
|
|
75
|
+
const received = yield* send(HttpClientRequest.setHeaders(prepared, {})).pipe(Effect.provideService(Transport.HeaderLines, compiled.headerLines),
|
|
76
|
+
// Client middleware can reject a response, such as filterStatusOk; its metadata still classifies the attempt.
|
|
77
|
+
Effect.tapError((error) => Effect.sync(() => {
|
|
78
|
+
if (HttpClientError.isHttpClientError(error) &&
|
|
79
|
+
error.response !== undefined)
|
|
80
|
+
metadata = Response.metadata(error.response);
|
|
81
|
+
})));
|
|
82
|
+
metadata = Response.metadata(received);
|
|
83
|
+
return yield* Response.decode(response, received);
|
|
84
|
+
}));
|
|
85
|
+
const exit = yield* Effect.exit(policy.attemptTimeout === undefined
|
|
86
|
+
? execution
|
|
87
|
+
: Effect.timeout(execution, policy.attemptTimeout));
|
|
88
|
+
yield* Effect.logDebug("HTTP attempt completed").pipe(Effect.annotateLogs({
|
|
89
|
+
"http.method": method,
|
|
90
|
+
"http.attempt": attempt,
|
|
91
|
+
"http.outcome": outcome(exit),
|
|
92
|
+
...(metadata === undefined
|
|
93
|
+
? {}
|
|
94
|
+
: { "http.status": metadata.status }),
|
|
95
|
+
}));
|
|
96
|
+
if (Exit.isFailure(exit) &&
|
|
97
|
+
(Cause.hasInterrupts(exit.cause) || Cause.hasDies(exit.cause)))
|
|
98
|
+
return yield* exit;
|
|
99
|
+
const reason = Exit.isFailure(exit)
|
|
100
|
+
? { _tag: "Failure", cause: exit.cause, metadata }
|
|
101
|
+
: { _tag: "Response", metadata: metadata };
|
|
102
|
+
if (!policy.while(reason))
|
|
103
|
+
return yield* exit;
|
|
104
|
+
const now = yield* Clock.currentTimeMillis;
|
|
105
|
+
const delay = yield* Pull.matchEffect(step(now, reason), {
|
|
106
|
+
onSuccess: ([, scheduled]) => Effect.succeed(Option.some(scheduled)),
|
|
107
|
+
onFailure: Effect.failCause,
|
|
108
|
+
onDone: () => Effect.succeed(Option.none()),
|
|
109
|
+
});
|
|
110
|
+
if (Option.isNone(delay)) {
|
|
111
|
+
yield* Effect.logDebug("HTTP retry budget exhausted").pipe(Effect.annotateLogs({ "http.attempt": attempt }));
|
|
112
|
+
return yield* exit;
|
|
113
|
+
}
|
|
114
|
+
yield* Effect.sleep(policy.retryAfter && reason.metadata !== undefined
|
|
115
|
+
? Duration.max(delay.value, Policy.retryAfterDelay(reason.metadata, now))
|
|
116
|
+
: delay.value);
|
|
117
|
+
yield* Effect.logDebug("HTTP retrying").pipe(Effect.annotateLogs({ "http.attempt": attempt + 1 }));
|
|
118
|
+
}
|
|
119
|
+
}, Effect.onExit((exit) => Effect.logDebug("HTTP operation completed").pipe(Effect.annotateLogs({ "http.outcome": outcome(exit) }))), Effect.withLogSpan("schema-http.operation"));
|
|
120
|
+
return { ...Pipeable.Prototype, execute };
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Return the Effect for an operation's input, in direct or pipeable form.
|
|
124
|
+
*
|
|
125
|
+
* @remarks
|
|
126
|
+
* Equivalent to `operation.execute(input)`. It does not itself evaluate the Effect.
|
|
127
|
+
*/
|
|
128
|
+
export const execute = Function.dual(2, (self, input) => self.execute(input));
|
|
129
|
+
/**
|
|
130
|
+
* Transform an operation's successful result with a pure function.
|
|
131
|
+
*
|
|
132
|
+
* @remarks
|
|
133
|
+
* Keeps the input, error and service types. Runs after the operation and its retry policy finish.
|
|
134
|
+
* Use {@link mapEffect} to return a typed failure or perform further effectful work.
|
|
135
|
+
*/
|
|
136
|
+
export const map = (f) => (self) => fromEffect((input) => Effect.map(self.execute(input), f));
|
|
137
|
+
/**
|
|
138
|
+
* Transform an operation's successful result with an Effect.
|
|
139
|
+
*
|
|
140
|
+
* @remarks
|
|
141
|
+
* Adds the transformation's errors and required services. Runs after the operation's retry
|
|
142
|
+
* policy completes, so failures here do not trigger that policy. Use it to turn a recognized
|
|
143
|
+
* response outcome into an application error.
|
|
144
|
+
*/
|
|
145
|
+
export const mapEffect = (f) => (self) => fromEffect((input) => Effect.flatMap(self.execute(input), f));
|
|
146
|
+
//# sourceMappingURL=Operation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Operation.js","sourceRoot":"","sources":["../../src/http/Operation.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,KAAK,EACL,QAAQ,EAC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E,CAC9D,UAAU,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE9D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,SAAS,GACpB,CAAe,CAAyC,EAAE,EAAE,CAC5D,CAAU,IAA2B,EAAmC,EAAE,CACxE,UAAU,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Cause, Duration, Schedule } from "effect";
|
|
2
|
+
import type * as Response from "./Response.js";
|
|
3
|
+
/**
|
|
4
|
+
* The completed attempt's response metadata or failure cause, used to decide whether to retry.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Contains no live transport resource, request method or decoded result. A failure includes
|
|
8
|
+
* metadata when a response was available, such as a decoding failure or a middleware rejection.
|
|
9
|
+
* Capture the operation's method or repeat-safety decision in the classifier's closure.
|
|
10
|
+
*/
|
|
11
|
+
export type Reason = {
|
|
12
|
+
readonly _tag: "Response";
|
|
13
|
+
readonly metadata: Response.Metadata;
|
|
14
|
+
} | {
|
|
15
|
+
readonly _tag: "Failure";
|
|
16
|
+
readonly cause: Cause.Cause<unknown>;
|
|
17
|
+
/** Present when a response arrived before the failure: a decoding failure, or middleware rejecting it. */
|
|
18
|
+
readonly metadata?: Response.Metadata | undefined;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A retry schedule, outcome classifier and optional timeout for an operation.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* The classifier and schedule must both permit another attempt. One schedule owns the retry
|
|
25
|
+
* budget across all reasons for that evaluation.
|
|
26
|
+
*/
|
|
27
|
+
export interface Policy<out E = never, out R = never> {
|
|
28
|
+
/** Supplies each retry delay and the shared retry budget for one evaluation. */
|
|
29
|
+
readonly schedule: Schedule.Schedule<unknown, Reason, E, R>;
|
|
30
|
+
/** Classifies a completed outcome as eligible for retry; the schedule must also permit it. */
|
|
31
|
+
readonly while: (reason: Reason) => boolean;
|
|
32
|
+
/** Bounds client execution and body decoding per attempt. `undefined` imposes no timeout. */
|
|
33
|
+
readonly attemptTimeout: Duration.Input | undefined;
|
|
34
|
+
/** Whether `Retry-After` supplies a minimum delay in addition to the schedule. */
|
|
35
|
+
readonly retryAfter: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Recognize timeouts, transport failures and transient HTTP statuses.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* The statuses are 408, 429, 500, 502, 503 and 504, matching Effect's transient classification.
|
|
42
|
+
* A declared response with one of these statuses is eligible even when decoding succeeded.
|
|
43
|
+
* This does not check the method or whether repeating a remote action is safe.
|
|
44
|
+
*/
|
|
45
|
+
export declare const transient: (reason: Reason) => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Create a retry policy from an Effect schedule and optional settings.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { Schedule } from "effect";
|
|
52
|
+
* import { Policy } from "viell-effect-schema-http";
|
|
53
|
+
* const retryRead = Policy.make(Schedule.recurs(2), { attemptTimeout: "5 seconds" });
|
|
54
|
+
* // At most three attempts; repeat only operations your service permits you to repeat.
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* `Schedule.recurs(n)` permits up to `n` retries after the first attempt. Defaults to
|
|
59
|
+
* {@link transient}, honoring `Retry-After`, and no attempt timeout. Enable retries only when
|
|
60
|
+
* repeating the service action is acceptable; transport failure does not prove it was unapplied.
|
|
61
|
+
* The attempt timeout covers client execution and decoding, not initial request preparation or
|
|
62
|
+
* retry delays. Apply `Effect.timeout` around the operation for an overall limit.
|
|
63
|
+
*/
|
|
64
|
+
export declare const make: <Out, E = never, R = never>(schedule: Schedule.Schedule<Out, Reason, E, R>, options?: {
|
|
65
|
+
readonly while?: ((reason: Reason) => boolean) | undefined;
|
|
66
|
+
readonly attemptTimeout?: Duration.Input | undefined;
|
|
67
|
+
readonly retryAfter?: boolean | undefined;
|
|
68
|
+
}) => Policy<E, R>;
|
|
69
|
+
/**
|
|
70
|
+
* One attempt, with no retry and no library-imposed timeout.
|
|
71
|
+
*/
|
|
72
|
+
export declare const none: Policy;
|
|
73
|
+
/**
|
|
74
|
+
* Read the minimum retry delay requested by a response's `Retry-After` header.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* `now` is milliseconds since the Unix epoch. Accepts delay seconds or an HTTP date. Missing,
|
|
78
|
+
* malformed or unrepresentable values produce zero. An operation takes the larger of this delay
|
|
79
|
+
* and its scheduled delay; this helper does not decide whether another attempt is allowed.
|
|
80
|
+
*/
|
|
81
|
+
export declare const retryAfterDelay: (metadata: Response.Metadata, now: number) => Duration.Duration;
|
|
82
|
+
//# sourceMappingURL=Policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Policy.d.ts","sourceRoot":"","sources":["../../src/http/Policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAU,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAE3D,OAAO,KAAK,KAAK,QAAQ,MAAM,eAAe,CAAC;AAE/C;;;;;;;GAOG;AACH,MAAM,MAAM,MAAM,GACd;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAA;CAAE,GACnE;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACrC,0GAA0G;IAC1G,QAAQ,CAAC,QAAQ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Ae,GAC1B,UAAU,QAAQ,CAAC,QAAQ,EAC3B,KAAK,MAAM,KACV,QAAQ,CAAC,QAOX,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Cause, Duration, Result, Schedule } from "effect";
|
|
2
|
+
import { Headers, HttpClientError } from "effect/http";
|
|
3
|
+
const isTransientStatus = (status) => status === 408 ||
|
|
4
|
+
status === 429 ||
|
|
5
|
+
status === 500 ||
|
|
6
|
+
status === 502 ||
|
|
7
|
+
status === 503 ||
|
|
8
|
+
status === 504;
|
|
9
|
+
const isTransientError = (error) => Cause.isTimeoutError(error) ||
|
|
10
|
+
(HttpClientError.isHttpClientError(error) &&
|
|
11
|
+
(error.reason._tag === "TransportError" ||
|
|
12
|
+
(error.reason._tag === "StatusCodeError" &&
|
|
13
|
+
isTransientStatus(error.reason.response.status))));
|
|
14
|
+
/**
|
|
15
|
+
* Recognize timeouts, transport failures and transient HTTP statuses.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* The statuses are 408, 429, 500, 502, 503 and 504, matching Effect's transient classification.
|
|
19
|
+
* A declared response with one of these statuses is eligible even when decoding succeeded.
|
|
20
|
+
* This does not check the method or whether repeating a remote action is safe.
|
|
21
|
+
*/
|
|
22
|
+
export const transient = (reason) => {
|
|
23
|
+
if (reason.metadata !== undefined &&
|
|
24
|
+
isTransientStatus(reason.metadata.status))
|
|
25
|
+
return true;
|
|
26
|
+
if (reason._tag === "Response")
|
|
27
|
+
return false;
|
|
28
|
+
return reason.cause.reasons.some((failure) => Cause.isFailReason(failure) && isTransientError(failure.error));
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Create a retry policy from an Effect schedule and optional settings.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { Schedule } from "effect";
|
|
36
|
+
* import { Policy } from "viell-effect-schema-http";
|
|
37
|
+
* const retryRead = Policy.make(Schedule.recurs(2), { attemptTimeout: "5 seconds" });
|
|
38
|
+
* // At most three attempts; repeat only operations your service permits you to repeat.
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* `Schedule.recurs(n)` permits up to `n` retries after the first attempt. Defaults to
|
|
43
|
+
* {@link transient}, honoring `Retry-After`, and no attempt timeout. Enable retries only when
|
|
44
|
+
* repeating the service action is acceptable; transport failure does not prove it was unapplied.
|
|
45
|
+
* The attempt timeout covers client execution and decoding, not initial request preparation or
|
|
46
|
+
* retry delays. Apply `Effect.timeout` around the operation for an overall limit.
|
|
47
|
+
*/
|
|
48
|
+
export const make = (schedule, options) => ({
|
|
49
|
+
schedule,
|
|
50
|
+
while: options?.while ?? transient,
|
|
51
|
+
attemptTimeout: options?.attemptTimeout,
|
|
52
|
+
retryAfter: options?.retryAfter ?? true,
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* One attempt, with no retry and no library-imposed timeout.
|
|
56
|
+
*/
|
|
57
|
+
export const none = make(Schedule.recurs(0));
|
|
58
|
+
/**
|
|
59
|
+
* Read the minimum retry delay requested by a response's `Retry-After` header.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* `now` is milliseconds since the Unix epoch. Accepts delay seconds or an HTTP date. Missing,
|
|
63
|
+
* malformed or unrepresentable values produce zero. An operation takes the larger of this delay
|
|
64
|
+
* and its scheduled delay; this helper does not decide whether another attempt is allowed.
|
|
65
|
+
*/
|
|
66
|
+
export const retryAfterDelay = (metadata, now) => {
|
|
67
|
+
const value = metadata.headers["retry-after"];
|
|
68
|
+
if (value === undefined)
|
|
69
|
+
return Duration.zero;
|
|
70
|
+
const parsed = Headers.parseRetryAfter(value);
|
|
71
|
+
if (Result.isFailure(parsed))
|
|
72
|
+
return Duration.zero;
|
|
73
|
+
const delay = Headers.retryAfterDelay(parsed.success, now);
|
|
74
|
+
return Number.isFinite(Duration.toMillis(delay)) ? delay : Duration.zero;
|
|
75
|
+
};
|
|
76
|
+
//# s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