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,192 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Context,
|
|
3
|
+
Data,
|
|
4
|
+
Deferred,
|
|
5
|
+
Effect,
|
|
6
|
+
Equal,
|
|
7
|
+
type Equivalence,
|
|
8
|
+
Exit,
|
|
9
|
+
type Fiber,
|
|
10
|
+
Scope,
|
|
11
|
+
Semaphore,
|
|
12
|
+
} from "effect";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A call tried to start work after the interaction had permanently closed.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* The error's `_tag` is `"InteractionClosed"`.
|
|
19
|
+
*/
|
|
20
|
+
export class Closed extends Data.TaggedError("InteractionClosed") {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for one call to an interaction.
|
|
24
|
+
*/
|
|
25
|
+
export interface RunOptions {
|
|
26
|
+
/** Start fresh work, superseding active work and bypassing the retained result. */
|
|
27
|
+
readonly force?: boolean | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Coordinate one active execution and one retained successful result.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* Use for one user-facing activity, such as a search field. Equal input shares work or reuses a
|
|
35
|
+
* success; different input supersedes active work. This is not a cache of all previous inputs.
|
|
36
|
+
*/
|
|
37
|
+
export interface Interaction<in I, out A, out E = never> {
|
|
38
|
+
/** Equal input joins active work or reuses the retained success; different input supersedes active work. */
|
|
39
|
+
readonly run: (
|
|
40
|
+
input: I,
|
|
41
|
+
options?: RunOptions,
|
|
42
|
+
) => Effect.Effect<A, E | Closed>;
|
|
43
|
+
/** Permanent and idempotent. Every caller waits for all owned finalizers, including superseded work. */
|
|
44
|
+
readonly close: Effect.Effect<void>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Choose how inputs compare and which successful results are retained.
|
|
49
|
+
*/
|
|
50
|
+
export interface Options<I, A> {
|
|
51
|
+
/** Defaults to `Equal.equals`, which compares plain objects and arrays structurally. */
|
|
52
|
+
readonly equivalence?: Equivalence.Equivalence<I> | undefined;
|
|
53
|
+
/** Whether to retain a success. Defaults to retaining; the result is returned either way. */
|
|
54
|
+
readonly retain?: ((value: A, input: I) => boolean) | undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface Flight<I, A, E> {
|
|
58
|
+
readonly input: I;
|
|
59
|
+
readonly result: Deferred.Deferred<A, E>;
|
|
60
|
+
readonly fiber: Fiber.Fiber<void>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Acquire a scoped interaction around an Effect-returning function.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { Effect } from "effect";
|
|
69
|
+
* import { Interaction } from "viell-effect-schema-http";
|
|
70
|
+
* const program = Effect.scoped(Effect.gen(function* () {
|
|
71
|
+
* const lookup = yield* Interaction.make((id: number) => Effect.succeed({ id }));
|
|
72
|
+
* const first = yield* lookup.run(42);
|
|
73
|
+
* const reused = yield* lookup.run(42);
|
|
74
|
+
* return [first, reused];
|
|
75
|
+
* }));
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* Keep the acquiring scope alive while calls should be accepted. Required services are captured
|
|
80
|
+
* at acquisition. Each execution belongs to the interaction in its own child scope; cancelling
|
|
81
|
+
* one waiter does not cancel shared work. Closing the acquiring scope closes the interaction
|
|
82
|
+
* and waits for all owned cleanup.
|
|
83
|
+
*/
|
|
84
|
+
export const make = Effect.fnUntraced(function* <I, A, E, R>(
|
|
85
|
+
execute: (input: I) => Effect.Effect<A, E, R>,
|
|
86
|
+
options?: Options<I, A>,
|
|
87
|
+
): Effect.fn.Return<Interaction<I, A, E>, never, R | Scope.Scope> {
|
|
88
|
+
const context = yield* Effect.context<R>();
|
|
89
|
+
const owned = yield* Scope.make("parallel");
|
|
90
|
+
const lock = yield* Semaphore.make(1);
|
|
91
|
+
const closing = yield* Deferred.make<void>();
|
|
92
|
+
const equivalent = options?.equivalence ?? Equal.equals;
|
|
93
|
+
let closed = false;
|
|
94
|
+
let flight: Flight<I, A, E> | undefined;
|
|
95
|
+
let retained: { readonly input: I; readonly value: A } | undefined;
|
|
96
|
+
|
|
97
|
+
const close = Effect.uninterruptible(
|
|
98
|
+
Effect.gen(function* () {
|
|
99
|
+
const first = yield* lock.withPermit(
|
|
100
|
+
Effect.sync(() => {
|
|
101
|
+
if (closed) return false;
|
|
102
|
+
closed = true;
|
|
103
|
+
flight = undefined;
|
|
104
|
+
retained = undefined;
|
|
105
|
+
return true;
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
if (first)
|
|
109
|
+
yield* Deferred.done(
|
|
110
|
+
closing,
|
|
111
|
+
yield* Effect.exit(Scope.close(owned, Exit.void)),
|
|
112
|
+
);
|
|
113
|
+
yield* Deferred.await(closing);
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
116
|
+
yield* Effect.addFinalizer(() => close);
|
|
117
|
+
|
|
118
|
+
// State transitions hold the lock briefly; it is never held while waiting for work or finalizers.
|
|
119
|
+
const publish = (
|
|
120
|
+
input: I,
|
|
121
|
+
shared: Deferred.Deferred<A, E>,
|
|
122
|
+
exit: Exit.Exit<A, E>,
|
|
123
|
+
) =>
|
|
124
|
+
lock.withPermit(
|
|
125
|
+
Effect.sync(() => {
|
|
126
|
+
if (flight?.result !== shared) return;
|
|
127
|
+
try {
|
|
128
|
+
if (Exit.isSuccess(exit)) {
|
|
129
|
+
if (options?.retain?.(exit.value, input) ?? true)
|
|
130
|
+
retained = { input, value: exit.value };
|
|
131
|
+
else if (
|
|
132
|
+
retained !== undefined &&
|
|
133
|
+
equivalent(retained.input, input)
|
|
134
|
+
)
|
|
135
|
+
retained = undefined;
|
|
136
|
+
}
|
|
137
|
+
} finally {
|
|
138
|
+
flight = undefined;
|
|
139
|
+
}
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// Forked uninterruptibly so that publication always happens, even when superseded before starting.
|
|
144
|
+
const worker = Effect.fnUntraced(function* (
|
|
145
|
+
input: I,
|
|
146
|
+
shared: Deferred.Deferred<A, E>,
|
|
147
|
+
) {
|
|
148
|
+
const exit = yield* Effect.exit(
|
|
149
|
+
Effect.interruptible(
|
|
150
|
+
Effect.scopedWith((scope) =>
|
|
151
|
+
Effect.suspend(() => execute(input)).pipe(
|
|
152
|
+
Effect.provideContext(Context.add(context, Scope.Scope, scope)),
|
|
153
|
+
),
|
|
154
|
+
),
|
|
155
|
+
),
|
|
156
|
+
);
|
|
157
|
+
// A retention predicate defect still releases every waiter and clears the active flight.
|
|
158
|
+
const publication = yield* Effect.exit(publish(input, shared, exit));
|
|
159
|
+
yield* Deferred.done(
|
|
160
|
+
shared,
|
|
161
|
+
Exit.isFailure(publication) ? Exit.failCause(publication.cause) : exit,
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const admit = Effect.fnUntraced(function* (input: I, force: boolean) {
|
|
166
|
+
if (closed) return yield* new Closed();
|
|
167
|
+
if (!force && flight !== undefined && equivalent(flight.input, input))
|
|
168
|
+
return Deferred.await(flight.result);
|
|
169
|
+
if (flight !== undefined) {
|
|
170
|
+
flight.fiber.interruptUnsafe();
|
|
171
|
+
flight = undefined;
|
|
172
|
+
}
|
|
173
|
+
if (!force && retained !== undefined && equivalent(retained.input, input))
|
|
174
|
+
return Effect.succeed(retained.value);
|
|
175
|
+
const shared = yield* Deferred.make<A, E>();
|
|
176
|
+
const fiber = yield* Effect.forkIn(worker(input, shared), owned, {
|
|
177
|
+
uninterruptible: true,
|
|
178
|
+
});
|
|
179
|
+
flight = { input, result: shared, fiber };
|
|
180
|
+
return Deferred.await(shared);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const run = (input: I, settings?: RunOptions): Effect.Effect<A, E | Closed> =>
|
|
184
|
+
Effect.uninterruptibleMask((restore) =>
|
|
185
|
+
Effect.flatMap(
|
|
186
|
+
lock.withPermit(admit(input, settings?.force === true)),
|
|
187
|
+
(result) => restore(result),
|
|
188
|
+
),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return { run, close };
|
|
192
|
+
});
|
package/src/http/Body.ts
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { Effect, Pipeable, Schema, Stream } from "effect";
|
|
2
|
+
import {
|
|
3
|
+
HttpBody,
|
|
4
|
+
HttpClientError,
|
|
5
|
+
type HttpClientResponse,
|
|
6
|
+
type UrlParams,
|
|
7
|
+
} from "effect/http";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Turn an application value into a finite outgoing HTTP body.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* Use with `Request.body`. Encoding occurs once per operation evaluation, before dispatch.
|
|
14
|
+
*/
|
|
15
|
+
export interface Encoder<in A, out E = never, out R = never>
|
|
16
|
+
extends Pipeable.Pipeable {
|
|
17
|
+
readonly encode: (value: A) => Effect.Effect<HttpBody.HttpBody, E, R>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Read a finite response body into an application value.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Use with `Response.status` or `Response.when`. Read the body within the returned Effect;
|
|
25
|
+
* `Operation.make` closes the attempt's resources before returning the decoded result.
|
|
26
|
+
*/
|
|
27
|
+
export interface Decoder<out A, out E = never, out R = never>
|
|
28
|
+
extends Pipeable.Pipeable {
|
|
29
|
+
readonly decode: (
|
|
30
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
31
|
+
) => Effect.Effect<A, E, R>;
|
|
32
|
+
/** Contributes to automatic `Accept`; it does not validate response Content-Type. */
|
|
33
|
+
readonly mediaType: string | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An encoder and decoder for the same application value.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* Encoding and decoding errors and required services are tracked separately.
|
|
41
|
+
*/
|
|
42
|
+
export interface Codec<
|
|
43
|
+
A,
|
|
44
|
+
EncodeError = never,
|
|
45
|
+
DecodeError = never,
|
|
46
|
+
EncodeServices = never,
|
|
47
|
+
DecodeServices = never,
|
|
48
|
+
>
|
|
49
|
+
extends
|
|
50
|
+
Encoder<A, EncodeError, EncodeServices>,
|
|
51
|
+
Decoder<A, DecodeError, DecodeServices> {}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Create a custom outgoing body encoder from an Effect-returning function.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* Return an Effect `HttpBody` with the representation and content metadata your service needs.
|
|
58
|
+
*/
|
|
59
|
+
export const encoder = <A, E = never, R = never>(
|
|
60
|
+
encode: (value: A) => Effect.Effect<HttpBody.HttpBody, E, R>,
|
|
61
|
+
): Encoder<A, E, R> => ({
|
|
62
|
+
...Pipeable.Prototype,
|
|
63
|
+
encode,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create a custom response-body decoder from an Effect-returning function.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* `mediaType` contributes to automatic `Accept`; it does not validate response Content-Type.
|
|
71
|
+
* Return typed failures through the Effect error channel; thrown exceptions are defects.
|
|
72
|
+
*/
|
|
73
|
+
export const decoder = <A, E = never, R = never>(
|
|
74
|
+
decode: (
|
|
75
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
76
|
+
) => Effect.Effect<A, E, R>,
|
|
77
|
+
mediaType?: string,
|
|
78
|
+
): Decoder<A, E, R> => ({ ...Pipeable.Prototype, decode, mediaType });
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Combine custom encoding and decoding functions into one body codec.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* `mediaType` describes the accepted response representation. Outgoing content metadata comes
|
|
85
|
+
* from the `HttpBody` returned by `encode`.
|
|
86
|
+
*/
|
|
87
|
+
export const make = <
|
|
88
|
+
A,
|
|
89
|
+
EncodeError = never,
|
|
90
|
+
DecodeError = never,
|
|
91
|
+
EncodeServices = never,
|
|
92
|
+
DecodeServices = never,
|
|
93
|
+
>(options: {
|
|
94
|
+
readonly encode: (
|
|
95
|
+
value: A,
|
|
96
|
+
) => Effect.Effect<HttpBody.HttpBody, EncodeError, EncodeServices>;
|
|
97
|
+
readonly decode: (
|
|
98
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
99
|
+
) => Effect.Effect<A, DecodeError, DecodeServices>;
|
|
100
|
+
readonly mediaType?: string | undefined;
|
|
101
|
+
}): Codec<A, EncodeError, DecodeError, EncodeServices, DecodeServices> => ({
|
|
102
|
+
...Pipeable.Prototype,
|
|
103
|
+
encode: options.encode,
|
|
104
|
+
decode: options.decode,
|
|
105
|
+
mediaType: options.mediaType,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const decodeFailure = (
|
|
109
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
110
|
+
cause: unknown,
|
|
111
|
+
) =>
|
|
112
|
+
new HttpClientError.HttpClientError({
|
|
113
|
+
reason: new HttpClientError.DecodeError({
|
|
114
|
+
request: response.request,
|
|
115
|
+
response,
|
|
116
|
+
cause,
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Read and write JSON without schema validation.
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* Encoding uses Effect's `HttpBody.json`. Values omitted by `JSON.stringify`, such as
|
|
125
|
+
* `undefined`, produce an empty outgoing body. An empty incoming body is a decoding failure,
|
|
126
|
+
* not `null`. Use {@link jsonSchema} to validate and transform application values.
|
|
127
|
+
*/
|
|
128
|
+
export const json: Codec<
|
|
129
|
+
unknown,
|
|
130
|
+
HttpBody.HttpBodyError,
|
|
131
|
+
HttpClientError.HttpClientError
|
|
132
|
+
> = make({
|
|
133
|
+
encode: (value: unknown) => HttpBody.json(value),
|
|
134
|
+
decode: (response) =>
|
|
135
|
+
Effect.flatMap(response.text, (text) =>
|
|
136
|
+
Effect.try({
|
|
137
|
+
try: (): unknown => JSON.parse(text),
|
|
138
|
+
catch: (cause) => decodeFailure(response, cause),
|
|
139
|
+
}),
|
|
140
|
+
),
|
|
141
|
+
mediaType: "application/json",
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Read and write text, with `text/plain` as the media type.
|
|
146
|
+
*
|
|
147
|
+
* @remarks
|
|
148
|
+
* Outgoing text is UTF-8. Incoming decoding delegates to the provider's `response.text`.
|
|
149
|
+
* Fetch-backed providers ignore charset parameters, strip a leading UTF-8 BOM, and replace
|
|
150
|
+
* malformed bytes with U+FFFD. Use {@link bytes} for strict or non-UTF-8 decoding.
|
|
151
|
+
*/
|
|
152
|
+
export const text: Codec<string, never, HttpClientError.HttpClientError> = make(
|
|
153
|
+
{
|
|
154
|
+
encode: (value: string) => Effect.sync(() => HttpBody.text(value)),
|
|
155
|
+
decode: (response) => response.text,
|
|
156
|
+
mediaType: "text/plain",
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Read and write `Uint8Array` bodies as `application/octet-stream`.
|
|
162
|
+
*
|
|
163
|
+
* @remarks
|
|
164
|
+
* Incoming bytes are buffered. Use a schema or custom decoder to interpret another encoding.
|
|
165
|
+
*/
|
|
166
|
+
export const bytes: Codec<Uint8Array, never, HttpClientError.HttpClientError> =
|
|
167
|
+
make<Uint8Array, never, HttpClientError.HttpClientError>({
|
|
168
|
+
encode: (value: Uint8Array) =>
|
|
169
|
+
Effect.sync(() => HttpBody.uint8Array(value)),
|
|
170
|
+
decode: (response) =>
|
|
171
|
+
Effect.map(response.arrayBuffer, (buffer) => new Uint8Array(buffer)),
|
|
172
|
+
mediaType: "application/octet-stream",
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Read and write Web `FormData` as multipart form data.
|
|
177
|
+
*
|
|
178
|
+
* @remarks
|
|
179
|
+
* The transport supplies the outgoing multipart boundary and its Content-Type. Do not set a
|
|
180
|
+
* boundary-less Content-Type manually. Decoding delegates to the HTTP provider.
|
|
181
|
+
*/
|
|
182
|
+
export const formData: Codec<FormData, never, HttpClientError.HttpClientError> =
|
|
183
|
+
make({
|
|
184
|
+
encode: (value: FormData) => Effect.sync(() => HttpBody.formData(value)),
|
|
185
|
+
decode: (response) => response.formData,
|
|
186
|
+
mediaType: "multipart/form-data",
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Read and write form bodies using Effect's `UrlParams.UrlParams`.
|
|
191
|
+
*
|
|
192
|
+
* @remarks
|
|
193
|
+
* Construct values with `UrlParams.fromInput` from `effect/http`. The media type is
|
|
194
|
+
* `application/x-www-form-urlencoded`.
|
|
195
|
+
*/
|
|
196
|
+
export const urlEncoded: Codec<
|
|
197
|
+
UrlParams.UrlParams,
|
|
198
|
+
never,
|
|
199
|
+
HttpClientError.HttpClientError
|
|
200
|
+
> = make({
|
|
201
|
+
encode: (value: UrlParams.UrlParams) =>
|
|
202
|
+
Effect.sync(() => HttpBody.urlParams(value)),
|
|
203
|
+
decode: (response) => response.urlParamsBody,
|
|
204
|
+
mediaType: "application/x-www-form-urlencoded",
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const isEmptyBody = (error: HttpClientError.HttpClientError) =>
|
|
208
|
+
error.reason._tag === "EmptyBodyError";
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Send no body, or discard an incoming body when its content is not needed.
|
|
212
|
+
*
|
|
213
|
+
* @remarks
|
|
214
|
+
* Decoding drains even unexpected incoming bytes without buffering them. Draining has no
|
|
215
|
+
* intrinsic timeout; use an attempt or operation timeout for an unbounded response.
|
|
216
|
+
*/
|
|
217
|
+
export const empty: Codec<void, never, HttpClientError.HttpClientError> = make({
|
|
218
|
+
encode: () => Effect.succeed(HttpBody.empty),
|
|
219
|
+
decode: (response) =>
|
|
220
|
+
Stream.runDrain(response.stream).pipe(
|
|
221
|
+
Effect.catchIf(isEmptyBody, () => Effect.void),
|
|
222
|
+
),
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Transform a decoded value with a pure function, preserving its media type.
|
|
227
|
+
*
|
|
228
|
+
* @remarks
|
|
229
|
+
* Returns a decoder only. Use {@link mapEffect} if the transformation may fail or need services.
|
|
230
|
+
*/
|
|
231
|
+
export const map =
|
|
232
|
+
<A, B>(f: (value: A) => B) =>
|
|
233
|
+
<E, R>(self: Decoder<A, E, R>): Decoder<B, E, R> =>
|
|
234
|
+
decoder((response) => Effect.map(self.decode(response), f), self.mediaType);
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Transform a decoded value with an Effect, adding its errors and required services.
|
|
238
|
+
*
|
|
239
|
+
* @remarks
|
|
240
|
+
* Returns a decoder only and preserves the media type. When used in a response case, this work
|
|
241
|
+
* is part of that attempt's decoding and is covered by its timeout.
|
|
242
|
+
*/
|
|
243
|
+
export const mapEffect =
|
|
244
|
+
<A, B, E2, R2>(f: (value: A) => Effect.Effect<B, E2, R2>) =>
|
|
245
|
+
<E, R>(self: Decoder<A, E, R>): Decoder<B, E | E2, R | R2> =>
|
|
246
|
+
decoder(
|
|
247
|
+
(response) => Effect.flatMap(self.decode(response), f),
|
|
248
|
+
self.mediaType,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Validate and transform a decoded value with an Effect schema.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* import { Schema } from "effect";
|
|
257
|
+
* import { Body } from "viell-effect-schema-http";
|
|
258
|
+
* const numberFromText = Body.text.pipe(Body.schema(Schema.NumberFromString));
|
|
259
|
+
* // Decodes the text "42" to the number 42.
|
|
260
|
+
* ```
|
|
261
|
+
*
|
|
262
|
+
* @remarks
|
|
263
|
+
* Returns a decoder with the schema's application type. A failure in a selected response case
|
|
264
|
+
* is final for that attempt; it does not try a later response case.
|
|
265
|
+
*/
|
|
266
|
+
export const schema = <S extends Schema.Constraint>(contract: S) => {
|
|
267
|
+
const decode = Schema.decodeUnknownEffect(contract);
|
|
268
|
+
return <A, E, R>(
|
|
269
|
+
self: Decoder<A, E, R>,
|
|
270
|
+
): Decoder<S["Type"], E | Schema.SchemaError, R | S["DecodingServices"]> =>
|
|
271
|
+
self.pipe(mapEffect((value) => decode(value)));
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Validate and encode application input before passing it to a body encoder.
|
|
276
|
+
*
|
|
277
|
+
* @remarks
|
|
278
|
+
* The schema's encoded type must match the underlying encoder's input. Returns an encoder only;
|
|
279
|
+
* use {@link schema} for the decoding direction.
|
|
280
|
+
*/
|
|
281
|
+
export const encodeSchema = <S extends Schema.Constraint>(contract: S) => {
|
|
282
|
+
const encode = Schema.encodeEffect(contract);
|
|
283
|
+
return <E, R>(
|
|
284
|
+
self: Encoder<S["Encoded"], E, R>,
|
|
285
|
+
): Encoder<S["Type"], E | Schema.SchemaError, R | S["EncodingServices"]> =>
|
|
286
|
+
encoder((value: S["Type"]) => Effect.flatMap(encode(value), self.encode));
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Read and write schema-validated JSON using the schema's application type.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* import { Schema } from "effect";
|
|
295
|
+
* import { Body } from "viell-effect-schema-http";
|
|
296
|
+
* const user = Body.jsonSchema(Schema.Struct({ id: Schema.Number, name: Schema.String }));
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* @remarks
|
|
300
|
+
* Uses `Schema.toCodecJson` to obtain the schema's canonical JSON codec. Encoding failures occur
|
|
301
|
+
* before dispatch; an operation wraps response decoding failures in `Response.DecodeError`.
|
|
302
|
+
*/
|
|
303
|
+
export const jsonSchema = <S extends Schema.Constraint>(
|
|
304
|
+
contract: S,
|
|
305
|
+
): Codec<
|
|
306
|
+
S["Type"],
|
|
307
|
+
Schema.SchemaError | HttpBody.HttpBodyError,
|
|
308
|
+
Schema.SchemaError | HttpClientError.HttpClientError,
|
|
309
|
+
S["EncodingServices"],
|
|
310
|
+
S["DecodingServices"]
|
|
311
|
+
> => {
|
|
312
|
+
const codec = Schema.toCodecJson(contract);
|
|
313
|
+
return make({
|
|
314
|
+
encode: encodeSchema(codec)(json).encode,
|
|
315
|
+
decode: json.pipe(schema(codec)).decode,
|
|
316
|
+
mediaType: json.mediaType,
|
|
317
|
+
});
|
|
318
|
+
};
|