@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.20
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 +201 -0
- package/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/grpc.js +2 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +174 -18
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +4 -4
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +68 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +64 -72
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +403 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +12 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +76 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/pagination.d.ts +101 -24
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +10 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protobuf.d.ts +92 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +86 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +102 -23
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +120 -50
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- package/lib/retry.d.ts +48 -29
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +158 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +1 -0
- package/lib/schema-lite.d.ts +40 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +27 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/sensitive.d.ts +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +288 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/trait.d.ts +71 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1370 -200
- package/src/category.ts +76 -106
- package/src/chunks.ts +30 -0
- package/src/config.ts +245 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +75 -75
- package/src/graphql.ts +1778 -0
- package/src/graphql.types.ts +105 -0
- package/src/grpc-transport-cloudflare-http2.ts +494 -0
- package/src/grpc-transport-cloudflare-sockets.ts +299 -0
- package/src/grpc-transport-node-http2.ts +234 -0
- package/src/grpc-transport.ts +118 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/pagination.ts +368 -94
- package/src/plain-values.ts +41 -0
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +622 -322
- package/src/protocol-rest.ts +440 -227
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +7 -13
- package/src/retry.ts +96 -110
- package/src/schema-lite-impl.ts +550 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +44 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +832 -0
- package/src/trait.ts +106 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
package/src/category.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* ```ts
|
|
10
10
|
* import * as Category from "@distilled.cloud/core/category";
|
|
11
11
|
*
|
|
12
|
-
* export class Unauthorized extends Schema.
|
|
12
|
+
* export class Unauthorized extends Schema.TaggedError<Unauthorized>()(
|
|
13
13
|
* "Unauthorized",
|
|
14
14
|
* { message: Schema.String },
|
|
15
15
|
* ).pipe(Category.withAuthError) {}
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
21
|
import * as Effect from "effect/Effect";
|
|
22
|
+
import * as HttpClientError from "effect/http/HttpClientError";
|
|
22
23
|
import * as Predicate from "effect/Predicate";
|
|
23
|
-
import * as HttpClientError from "effect/unstable/http/HttpClientError";
|
|
24
24
|
|
|
25
25
|
// ============================================================================
|
|
26
26
|
// Error Category Constants
|
|
@@ -92,7 +92,7 @@ export interface RetryableInfo {
|
|
|
92
92
|
*
|
|
93
93
|
* @example
|
|
94
94
|
* ```ts
|
|
95
|
-
* export class MyError extends Schema.
|
|
95
|
+
* export class MyError extends Schema.TaggedError<MyError>()(
|
|
96
96
|
* "MyError",
|
|
97
97
|
* { message: Schema.String },
|
|
98
98
|
* ).pipe(Category.withCategory(Category.AuthError)) {}
|
|
@@ -103,9 +103,7 @@ export const withCategory =
|
|
|
103
103
|
<Args extends Array<any>, Ret, C extends { new (...args: Args): Ret }>(
|
|
104
104
|
C: C,
|
|
105
105
|
): C & {
|
|
106
|
-
new (
|
|
107
|
-
...args: Args
|
|
108
|
-
): Ret & { [categoriesKey]: { [Cat in Categories[number]]: true } };
|
|
106
|
+
new (...args: Args): Ret & { [categoriesKey]: { [Cat in Categories[number]]: true } };
|
|
109
107
|
} => {
|
|
110
108
|
for (const category of categories) {
|
|
111
109
|
if (!(categoriesKey in C.prototype)) {
|
|
@@ -123,13 +121,13 @@ export const withCategory =
|
|
|
123
121
|
* @example
|
|
124
122
|
* ```ts
|
|
125
123
|
* // Standard retryable error
|
|
126
|
-
* export class TransientError extends Schema.
|
|
124
|
+
* export class TransientError extends Schema.TaggedError<TransientError>()(
|
|
127
125
|
* "TransientError",
|
|
128
126
|
* { message: Schema.String },
|
|
129
127
|
* ).pipe(Category.withRetryable()) {}
|
|
130
128
|
*
|
|
131
129
|
* // Throttling error (uses longer backoff)
|
|
132
|
-
* export class RateLimitError extends Schema.
|
|
130
|
+
* export class RateLimitError extends Schema.TaggedError<RateLimitError>()(
|
|
133
131
|
* "RateLimitError",
|
|
134
132
|
* { message: Schema.String },
|
|
135
133
|
* ).pipe(Category.withRetryable({ throttling: true })) {}
|
|
@@ -150,24 +148,22 @@ export const withRetryable =
|
|
|
150
148
|
// Category Decorators (convenience functions for common categories)
|
|
151
149
|
// ============================================================================
|
|
152
150
|
|
|
153
|
-
export const withAuthError = withCategory(AuthError);
|
|
154
|
-
export const withBadRequestError = withCategory(BadRequestError);
|
|
155
|
-
export const withConflictError = withCategory(ConflictError);
|
|
156
|
-
export const withNotFoundError = withCategory(NotFoundError);
|
|
157
|
-
export const withQuotaError = withCategory(QuotaError);
|
|
158
|
-
export const withServerError = withCategory(ServerError);
|
|
159
|
-
export const withThrottlingError = withCategory(ThrottlingError);
|
|
160
|
-
export const withNetworkError = withCategory(NetworkError);
|
|
161
|
-
export const withParseError = withCategory(ParseError);
|
|
162
|
-
export const withConfigurationError = withCategory(ConfigurationError);
|
|
163
|
-
export const withTimeoutError = withCategory(TimeoutError);
|
|
164
|
-
export const withRetryableError = withCategory(RetryableError);
|
|
165
|
-
export const withLockedError = withCategory(LockedError);
|
|
166
|
-
export const withAbortedError = withCategory(AbortedError);
|
|
167
|
-
export const withAlreadyExistsError = withCategory(AlreadyExistsError);
|
|
168
|
-
export const withDependencyViolationError = withCategory(
|
|
169
|
-
DependencyViolationError,
|
|
170
|
-
);
|
|
151
|
+
export const withAuthError = /*@__PURE__*/ withCategory(AuthError);
|
|
152
|
+
export const withBadRequestError = /*@__PURE__*/ withCategory(BadRequestError);
|
|
153
|
+
export const withConflictError = /*@__PURE__*/ withCategory(ConflictError);
|
|
154
|
+
export const withNotFoundError = /*@__PURE__*/ withCategory(NotFoundError);
|
|
155
|
+
export const withQuotaError = /*@__PURE__*/ withCategory(QuotaError);
|
|
156
|
+
export const withServerError = /*@__PURE__*/ withCategory(ServerError);
|
|
157
|
+
export const withThrottlingError = /*@__PURE__*/ withCategory(ThrottlingError);
|
|
158
|
+
export const withNetworkError = /*@__PURE__*/ withCategory(NetworkError);
|
|
159
|
+
export const withParseError = /*@__PURE__*/ withCategory(ParseError);
|
|
160
|
+
export const withConfigurationError = /*@__PURE__*/ withCategory(ConfigurationError);
|
|
161
|
+
export const withTimeoutError = /*@__PURE__*/ withCategory(TimeoutError);
|
|
162
|
+
export const withRetryableError = /*@__PURE__*/ withCategory(RetryableError);
|
|
163
|
+
export const withLockedError = /*@__PURE__*/ withCategory(LockedError);
|
|
164
|
+
export const withAbortedError = /*@__PURE__*/ withCategory(AbortedError);
|
|
165
|
+
export const withAlreadyExistsError = /*@__PURE__*/ withCategory(AlreadyExistsError);
|
|
166
|
+
export const withDependencyViolationError = /*@__PURE__*/ withCategory(DependencyViolationError);
|
|
171
167
|
|
|
172
168
|
// ============================================================================
|
|
173
169
|
// Category Predicates
|
|
@@ -177,57 +173,41 @@ export const withDependencyViolationError = withCategory(
|
|
|
177
173
|
* Check if an error has a specific category.
|
|
178
174
|
*/
|
|
179
175
|
export const hasCategory = (error: unknown, category: Category): boolean => {
|
|
180
|
-
if (
|
|
181
|
-
Predicate.isObject(error) &&
|
|
182
|
-
Predicate.hasProperty(categoriesKey)(error)
|
|
183
|
-
) {
|
|
176
|
+
if (Predicate.isObject(error) && Predicate.hasProperty(categoriesKey)(error)) {
|
|
184
177
|
// @ts-expect-error - dynamic property access
|
|
185
178
|
return category in error[categoriesKey];
|
|
186
179
|
}
|
|
187
180
|
return false;
|
|
188
181
|
};
|
|
189
182
|
|
|
190
|
-
export const isAuthError = (error: unknown): boolean =>
|
|
191
|
-
hasCategory(error, AuthError);
|
|
183
|
+
export const isAuthError = (error: unknown): boolean => hasCategory(error, AuthError);
|
|
192
184
|
|
|
193
|
-
export const isBadRequestError = (error: unknown): boolean =>
|
|
194
|
-
hasCategory(error, BadRequestError);
|
|
185
|
+
export const isBadRequestError = (error: unknown): boolean => hasCategory(error, BadRequestError);
|
|
195
186
|
|
|
196
|
-
export const isConflictError = (error: unknown): boolean =>
|
|
197
|
-
hasCategory(error, ConflictError);
|
|
187
|
+
export const isConflictError = (error: unknown): boolean => hasCategory(error, ConflictError);
|
|
198
188
|
|
|
199
|
-
export const isNotFoundError = (error: unknown): boolean =>
|
|
200
|
-
hasCategory(error, NotFoundError);
|
|
189
|
+
export const isNotFoundError = (error: unknown): boolean => hasCategory(error, NotFoundError);
|
|
201
190
|
|
|
202
|
-
export const isQuotaError = (error: unknown): boolean =>
|
|
203
|
-
hasCategory(error, QuotaError);
|
|
191
|
+
export const isQuotaError = (error: unknown): boolean => hasCategory(error, QuotaError);
|
|
204
192
|
|
|
205
|
-
export const isServerError = (error: unknown): boolean =>
|
|
206
|
-
hasCategory(error, ServerError);
|
|
193
|
+
export const isServerError = (error: unknown): boolean => hasCategory(error, ServerError);
|
|
207
194
|
|
|
208
|
-
export const isThrottlingError = (error: unknown): boolean =>
|
|
209
|
-
hasCategory(error, ThrottlingError);
|
|
195
|
+
export const isThrottlingError = (error: unknown): boolean => hasCategory(error, ThrottlingError);
|
|
210
196
|
|
|
211
|
-
export const isNetworkError = (error: unknown): boolean =>
|
|
212
|
-
hasCategory(error, NetworkError);
|
|
197
|
+
export const isNetworkError = (error: unknown): boolean => hasCategory(error, NetworkError);
|
|
213
198
|
|
|
214
|
-
export const isParseError = (error: unknown): boolean =>
|
|
215
|
-
hasCategory(error, ParseError);
|
|
199
|
+
export const isParseError = (error: unknown): boolean => hasCategory(error, ParseError);
|
|
216
200
|
|
|
217
201
|
export const isConfigurationError = (error: unknown): boolean =>
|
|
218
202
|
hasCategory(error, ConfigurationError);
|
|
219
203
|
|
|
220
|
-
export const isTimeoutError = (error: unknown): boolean =>
|
|
221
|
-
hasCategory(error, TimeoutError);
|
|
204
|
+
export const isTimeoutError = (error: unknown): boolean => hasCategory(error, TimeoutError);
|
|
222
205
|
|
|
223
|
-
export const isRetryableError = (error: unknown): boolean =>
|
|
224
|
-
hasCategory(error, RetryableError);
|
|
206
|
+
export const isRetryableError = (error: unknown): boolean => hasCategory(error, RetryableError);
|
|
225
207
|
|
|
226
|
-
export const isLockedError = (error: unknown): boolean =>
|
|
227
|
-
hasCategory(error, LockedError);
|
|
208
|
+
export const isLockedError = (error: unknown): boolean => hasCategory(error, LockedError);
|
|
228
209
|
|
|
229
|
-
export const isAbortedError = (error: unknown): boolean =>
|
|
230
|
-
hasCategory(error, AbortedError);
|
|
210
|
+
export const isAbortedError = (error: unknown): boolean => hasCategory(error, AbortedError);
|
|
231
211
|
|
|
232
212
|
export const isAlreadyExistsError = (error: unknown): boolean =>
|
|
233
213
|
hasCategory(error, AlreadyExistsError);
|
|
@@ -264,15 +244,14 @@ export const isThrottling = (error: unknown): boolean => {
|
|
|
264
244
|
/**
|
|
265
245
|
* Check if an error is an Effect `HttpClientError` caused by a wire-level
|
|
266
246
|
* transport failure (undici `ConnectTimeoutError`, socket reset, DNS
|
|
267
|
-
* failure, premature stream close,
|
|
247
|
+
* failure, premature stream close, ...). The request never reached the
|
|
268
248
|
* server (or the response never finished) so retrying is safe.
|
|
269
249
|
*
|
|
270
250
|
* Status-code retries are intentionally left to the per-SDK API client,
|
|
271
251
|
* which knows whether a given response is genuinely retryable.
|
|
272
252
|
*/
|
|
273
253
|
const isHttpTransportError = (error: unknown): boolean =>
|
|
274
|
-
HttpClientError.isHttpClientError(error) &&
|
|
275
|
-
error.reason._tag === "TransportError";
|
|
254
|
+
HttpClientError.isHttpClientError(error) && error.reason._tag === "TransportError";
|
|
276
255
|
|
|
277
256
|
/**
|
|
278
257
|
* Check if an error is a transient error that should be automatically retried.
|
|
@@ -309,9 +288,7 @@ export const isTransientError = (error: unknown): boolean => {
|
|
|
309
288
|
// Category Type Utilities
|
|
310
289
|
// ============================================================================
|
|
311
290
|
|
|
312
|
-
export type AllKeys<E> = E extends { [categoriesKey]: infer Q }
|
|
313
|
-
? keyof Q
|
|
314
|
-
: never;
|
|
291
|
+
export type AllKeys<E> = E extends { [categoriesKey]: infer Q } ? keyof Q : never;
|
|
315
292
|
|
|
316
293
|
export type ExtractAll<E, Cats extends PropertyKey> = Cats extends any
|
|
317
294
|
? Extract<E, { [categoriesKey]: { [K in Cats]: any } }>
|
|
@@ -343,41 +320,39 @@ const makeCatcher =
|
|
|
343
320
|
* )
|
|
344
321
|
* ```
|
|
345
322
|
*/
|
|
346
|
-
export const catchErrors =
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
323
|
+
export const catchErrors = <Categories extends Category[], A2, E2, R2>(
|
|
324
|
+
...args: [...Categories, (err: any) => Effect.Effect<A2, E2, R2>]
|
|
325
|
+
) => {
|
|
326
|
+
// Split once, without mutating `args`, so one catcher can be reused.
|
|
327
|
+
const handler = args[args.length - 1] as (err: any) => Effect.Effect<A2, E2, R2>;
|
|
328
|
+
const categories = args.slice(0, -1) as unknown as Categories;
|
|
329
|
+
return <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
330
|
+
Effect.catchIf(
|
|
354
331
|
effect,
|
|
355
332
|
(e) => categories.some((cat) => hasCategory(e, cat)),
|
|
356
333
|
handler,
|
|
357
334
|
) as Effect.Effect<A | A2, E | E2, R | R2>;
|
|
358
|
-
|
|
335
|
+
};
|
|
359
336
|
|
|
360
337
|
// Alias for convenience
|
|
361
338
|
export { catchErrors as catch };
|
|
362
339
|
|
|
363
|
-
export const catchAuthError = makeCatcher(AuthError);
|
|
364
|
-
export const catchBadRequestError = makeCatcher(BadRequestError);
|
|
365
|
-
export const catchConflictError = makeCatcher(ConflictError);
|
|
366
|
-
export const catchNotFoundError = makeCatcher(NotFoundError);
|
|
367
|
-
export const catchQuotaError = makeCatcher(QuotaError);
|
|
368
|
-
export const catchServerError = makeCatcher(ServerError);
|
|
369
|
-
export const catchThrottlingError = makeCatcher(ThrottlingError);
|
|
370
|
-
export const catchNetworkError = makeCatcher(NetworkError);
|
|
371
|
-
export const catchParseError = makeCatcher(ParseError);
|
|
372
|
-
export const catchConfigurationError = makeCatcher(ConfigurationError);
|
|
373
|
-
export const catchTimeoutError = makeCatcher(TimeoutError);
|
|
374
|
-
export const catchRetryableError = makeCatcher(RetryableError);
|
|
375
|
-
export const catchLockedError = makeCatcher(LockedError);
|
|
376
|
-
export const catchAbortedError = makeCatcher(AbortedError);
|
|
377
|
-
export const catchAlreadyExistsError = makeCatcher(AlreadyExistsError);
|
|
378
|
-
export const catchDependencyViolationError = makeCatcher(
|
|
379
|
-
DependencyViolationError,
|
|
380
|
-
);
|
|
340
|
+
export const catchAuthError = /*@__PURE__*/ makeCatcher(AuthError);
|
|
341
|
+
export const catchBadRequestError = /*@__PURE__*/ makeCatcher(BadRequestError);
|
|
342
|
+
export const catchConflictError = /*@__PURE__*/ makeCatcher(ConflictError);
|
|
343
|
+
export const catchNotFoundError = /*@__PURE__*/ makeCatcher(NotFoundError);
|
|
344
|
+
export const catchQuotaError = /*@__PURE__*/ makeCatcher(QuotaError);
|
|
345
|
+
export const catchServerError = /*@__PURE__*/ makeCatcher(ServerError);
|
|
346
|
+
export const catchThrottlingError = /*@__PURE__*/ makeCatcher(ThrottlingError);
|
|
347
|
+
export const catchNetworkError = /*@__PURE__*/ makeCatcher(NetworkError);
|
|
348
|
+
export const catchParseError = /*@__PURE__*/ makeCatcher(ParseError);
|
|
349
|
+
export const catchConfigurationError = /*@__PURE__*/ makeCatcher(ConfigurationError);
|
|
350
|
+
export const catchTimeoutError = /*@__PURE__*/ makeCatcher(TimeoutError);
|
|
351
|
+
export const catchRetryableError = /*@__PURE__*/ makeCatcher(RetryableError);
|
|
352
|
+
export const catchLockedError = /*@__PURE__*/ makeCatcher(LockedError);
|
|
353
|
+
export const catchAbortedError = /*@__PURE__*/ makeCatcher(AbortedError);
|
|
354
|
+
export const catchAlreadyExistsError = /*@__PURE__*/ makeCatcher(AlreadyExistsError);
|
|
355
|
+
export const catchDependencyViolationError = /*@__PURE__*/ makeCatcher(DependencyViolationError);
|
|
381
356
|
|
|
382
357
|
/**
|
|
383
358
|
* Catch errors with specified categories with full type narrowing.
|
|
@@ -389,23 +364,18 @@ export const catchDependencyViolationError = makeCatcher(
|
|
|
389
364
|
* )
|
|
390
365
|
* ```
|
|
391
366
|
*/
|
|
392
|
-
export const catchCategory =
|
|
393
|
-
<E,
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
]
|
|
398
|
-
) =>
|
|
399
|
-
|
|
367
|
+
export const catchCategory = <E, const Categories extends Array<AllKeys<E>>, A2, E2, R2>(
|
|
368
|
+
...args: [...Categories, f: (err: ExtractAll<E, Categories[number]>) => Effect.Effect<A2, E2, R2>]
|
|
369
|
+
) => {
|
|
370
|
+
// Split once, without mutating `args`, so one catcher can be reused.
|
|
371
|
+
const f = args[args.length - 1] as (
|
|
372
|
+
err: ExtractAll<E, Categories[number]>,
|
|
373
|
+
) => Effect.Effect<A2, E2, R2>;
|
|
374
|
+
const categories = args.slice(0, -1) as unknown as Categories;
|
|
375
|
+
return <A, R>(
|
|
400
376
|
effect: Effect.Effect<A, E, R>,
|
|
401
|
-
): Effect.Effect<
|
|
402
|
-
|
|
403
|
-
E2 | Exclude<E, ExtractAll<E, Categories[number]>>,
|
|
404
|
-
R | R2
|
|
405
|
-
> => {
|
|
406
|
-
const f = args.pop()!;
|
|
407
|
-
const categories = args;
|
|
408
|
-
return Effect.catchIf(
|
|
377
|
+
): Effect.Effect<A | A2, E2 | Exclude<E, ExtractAll<E, Categories[number]>>, R | R2> =>
|
|
378
|
+
Effect.catchIf(
|
|
409
379
|
effect,
|
|
410
380
|
(e) => {
|
|
411
381
|
if (Predicate.isObject(e) && Predicate.hasProperty(categoriesKey)(e)) {
|
|
@@ -421,4 +391,4 @@ export const catchCategory =
|
|
|
421
391
|
// @ts-expect-error - type narrowing limitation
|
|
422
392
|
(e) => f(e),
|
|
423
393
|
) as any;
|
|
424
|
-
|
|
394
|
+
};
|
package/src/chunks.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated schema consts built from chunked builder functions.
|
|
3
|
+
*
|
|
4
|
+
* A generated service module declares thousands of schema consts. Evaluating
|
|
5
|
+
* each initializer at import allocated a thunk closure, its compiled-function
|
|
6
|
+
* record, a lazy wrapper and an annotation object -- about 330 bytes a
|
|
7
|
+
* schema. The generator now moves the initializers into builder functions
|
|
8
|
+
* of a few schemas each (`switch (j) { case 0: return S.Struct(...) }`), and
|
|
9
|
+
* every const is a small lazy schema holding its builder and index:
|
|
10
|
+
*
|
|
11
|
+
* const Customer = /*@__PURE__*\/ __L.at(__c12, 3) as any as S.Schema<Customer>;
|
|
12
|
+
* function __c12(j: number) { switch (j) { case 3: return S.suspend(...) } }
|
|
13
|
+
*
|
|
14
|
+
* The initializer runs on first use, exactly as written, so the schema is
|
|
15
|
+
* the same. V8 compiles a builder only when one of its schemas is first
|
|
16
|
+
* used, and the code text lives only in the module source.
|
|
17
|
+
*/
|
|
18
|
+
import type * as S from "effect/Schema";
|
|
19
|
+
import { lazyFrom, type LazyBuilder } from "./schema.ts";
|
|
20
|
+
|
|
21
|
+
const builders = /*@__PURE__*/ new WeakMap<(j: number) => unknown, LazyBuilder>();
|
|
22
|
+
|
|
23
|
+
/** Schema `j` of the chunk `build`, built on first use. */
|
|
24
|
+
// `any`: each const keeps the type it declares or casts to.
|
|
25
|
+
export const at = (build: (j: number) => unknown, j: number): any => {
|
|
26
|
+
let builder = builders.get(build);
|
|
27
|
+
if (!builder)
|
|
28
|
+
builders.set(build, (builder = { build: (data) => build(data as number) as S.Top }));
|
|
29
|
+
return lazyFrom(builder, j);
|
|
30
|
+
};
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment configuration for credentials, endpoints and regions: the
|
|
3
|
+
* small part of `effect/Config` that SDKs use, as plain Effects.
|
|
4
|
+
*
|
|
5
|
+
* Importing `effect/Config` costs about 280 KB of heap per process (it pulls
|
|
6
|
+
* in ConfigProvider, Schema-based parsing and their dependencies), and every
|
|
7
|
+
* SDK whose credentials read the environment paid it at import. These
|
|
8
|
+
* readers do the same lookups without it:
|
|
9
|
+
*
|
|
10
|
+
* - When the caller provided a `ConfigProvider` (found by its context key),
|
|
11
|
+
* the value comes from that provider, so `ConfigProvider.fromUnknown(...)`
|
|
12
|
+
* in tests and custom providers on Workers keep working.
|
|
13
|
+
* - Otherwise the value comes from `process.env`, which is what Effect's
|
|
14
|
+
* default provider reads.
|
|
15
|
+
*
|
|
16
|
+
* A missing variable fails with {@link ConfigError} (with its `key`), which `withDefault`, `option` and `orElse` handle. A provided
|
|
17
|
+
* `ConfigProvider` that cannot read its source fails with its `SourceError`,
|
|
18
|
+
* which those helpers pass through.
|
|
19
|
+
*
|
|
20
|
+
* Credentials read these on every request, so without a provider a whole
|
|
21
|
+
* reader tree (`all`, `withDefault`, `orElse`, ...) runs as one synchronous
|
|
22
|
+
* read of `process.env` in a single fiber step: each reader carries a plain
|
|
23
|
+
* `read` function next to its effect, and the combinators compose those.
|
|
24
|
+
* A missing value inside the tree is a plain marker; the tagged error is
|
|
25
|
+
* only built if the whole tree fails.
|
|
26
|
+
*/
|
|
27
|
+
import type { SourceError } from "effect/ConfigProvider";
|
|
28
|
+
import * as Context from "effect/Context";
|
|
29
|
+
import * as Effect from "effect/Effect";
|
|
30
|
+
import * as Option from "effect/Option";
|
|
31
|
+
import * as Redact from "effect/Redacted";
|
|
32
|
+
import * as Category from "./category.ts";
|
|
33
|
+
import * as Schema from "./schema-lite-typed.ts";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Configuration error - missing or invalid configuration. `key` is the
|
|
37
|
+
* configuration value (e.g. an environment variable) when one is missing.
|
|
38
|
+
*/
|
|
39
|
+
export class ConfigError
|
|
40
|
+
extends /*@__PURE__*/ Schema.TaggedError<ConfigError>()("ConfigError", {
|
|
41
|
+
message: Schema.String,
|
|
42
|
+
key: /*@__PURE__*/ Schema.optional(Schema.String),
|
|
43
|
+
}).pipe(Category.withConfigurationError) {}
|
|
44
|
+
|
|
45
|
+
const CONFIG_PROVIDER_KEY = "effect/ConfigProvider";
|
|
46
|
+
|
|
47
|
+
interface ProvidedConfigProvider {
|
|
48
|
+
readonly load: (
|
|
49
|
+
path: ReadonlyArray<string>,
|
|
50
|
+
) => Effect.Effect<{ readonly _tag: string; readonly value?: string } | undefined, SourceError>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const fromEnv = (name: string): string | undefined =>
|
|
54
|
+
(globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env?.[name];
|
|
55
|
+
|
|
56
|
+
/** A required value that is missing, inside a synchronous read. */
|
|
57
|
+
class Missing {
|
|
58
|
+
constructor(readonly name: string) {}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A synchronous read that cannot finish on its own (an `orElse` fallback
|
|
63
|
+
* that is an arbitrary effect): the outermost reader runs its effect path.
|
|
64
|
+
*/
|
|
65
|
+
const DEFER: unique symbol = /*@__PURE__*/ Symbol("defer");
|
|
66
|
+
type Read<A> = A | Missing | typeof DEFER;
|
|
67
|
+
|
|
68
|
+
const readSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/config/read");
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A reader: an effect, plus the same read as a plain function of
|
|
72
|
+
* `process.env` (used when no `ConfigProvider` is in the context) and the
|
|
73
|
+
* effect-based read used when one is (or when the read defers).
|
|
74
|
+
*/
|
|
75
|
+
interface Reader<A> {
|
|
76
|
+
readonly read: () => Read<A>;
|
|
77
|
+
readonly viaEffect: () => Effect.Effect<A, any, any>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const readerOf = <A, E, R>(self: Effect.Effect<A, E, R>): Reader<A> | undefined =>
|
|
81
|
+
(self as unknown as { [readSymbol]?: Reader<A> })[readSymbol];
|
|
82
|
+
|
|
83
|
+
// Looked up by its string key so `effect/ConfigProvider` stays out of bundles.
|
|
84
|
+
const providerKey = { key: CONFIG_PROVIDER_KEY } as Context.Key<never, ProvidedConfigProvider>;
|
|
85
|
+
|
|
86
|
+
const hasProvider = (context: Context.Context<never>): boolean =>
|
|
87
|
+
Context.getOrUndefined(context, providerKey) !== undefined;
|
|
88
|
+
|
|
89
|
+
const missingError = (m: Missing) =>
|
|
90
|
+
new ConfigError({ key: m.name, message: `Expected ${m.name} to be set` });
|
|
91
|
+
|
|
92
|
+
const fromReader = <A>(reader: Reader<A>): Effect.Effect<A, any, any> => {
|
|
93
|
+
const effect = Effect.withFiber((fiber) => {
|
|
94
|
+
if (hasProvider(fiber.context as Context.Context<never>)) return reader.viaEffect();
|
|
95
|
+
const value = reader.read();
|
|
96
|
+
if (value === DEFER) return reader.viaEffect();
|
|
97
|
+
return value instanceof Missing ? Effect.fail(missingError(value)) : Effect.succeed(value);
|
|
98
|
+
});
|
|
99
|
+
(effect as unknown as { [readSymbol]: Reader<A> })[readSymbol] = reader;
|
|
100
|
+
return effect;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The raw value of `name` from the provided `ConfigProvider`, or undefined
|
|
105
|
+
* when unset. A provider that cannot read its source fails with `SourceError`.
|
|
106
|
+
*/
|
|
107
|
+
const lookup = (name: string): Effect.Effect<string | undefined, SourceError> =>
|
|
108
|
+
Effect.flatMap(Effect.context<never>(), (context) => {
|
|
109
|
+
const provider = context.mapUnsafe.get(CONFIG_PROVIDER_KEY) as
|
|
110
|
+
| ProvidedConfigProvider
|
|
111
|
+
| undefined;
|
|
112
|
+
if (!provider) return Effect.succeed(fromEnv(name));
|
|
113
|
+
return Effect.map(provider.load([name]), (node) =>
|
|
114
|
+
node?._tag === "Value" ? node.value : undefined,
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const required = (name: string): Effect.Effect<string, ConfigError | SourceError> =>
|
|
119
|
+
fromReader({
|
|
120
|
+
read: () => fromEnv(name) ?? new Missing(name),
|
|
121
|
+
viaEffect: () =>
|
|
122
|
+
Effect.flatMap(lookup(name), (value) =>
|
|
123
|
+
value === undefined ? Effect.fail(missingError(new Missing(name))) : Effect.succeed(value),
|
|
124
|
+
),
|
|
125
|
+
}) as Effect.Effect<string, ConfigError | SourceError>;
|
|
126
|
+
|
|
127
|
+
/** The string value of environment variable `name`. */
|
|
128
|
+
export const String = (name: string): Effect.Effect<string, ConfigError | SourceError> =>
|
|
129
|
+
required(name);
|
|
130
|
+
|
|
131
|
+
/** The value of `name`, redacted. */
|
|
132
|
+
export const Redacted = (
|
|
133
|
+
name: string,
|
|
134
|
+
): Effect.Effect<Redact.Redacted<string>, ConfigError | SourceError> =>
|
|
135
|
+
map((value: string) => Redact.make(value))(required(name));
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The failure of a missing value: {@link ConfigError}, matched by its tag
|
|
139
|
+
* (Effect's own config errors share it). `option`, `withDefault` and `orElse` handle
|
|
140
|
+
* only this failure; any other failure of `self` propagates.
|
|
141
|
+
*/
|
|
142
|
+
type Missed = { readonly _tag: "ConfigError" };
|
|
143
|
+
|
|
144
|
+
const isMissed = (error: unknown): boolean =>
|
|
145
|
+
typeof error === "object" && error !== null && (error as Missed)._tag === "ConfigError";
|
|
146
|
+
|
|
147
|
+
const catchMissed = <A, E, R, A2, E2, R2>(
|
|
148
|
+
self: Effect.Effect<A, E, R>,
|
|
149
|
+
f: () => Effect.Effect<A2, E2, R2>,
|
|
150
|
+
): Effect.Effect<A | A2, Exclude<E, Missed> | E2, R | R2> =>
|
|
151
|
+
Effect.catchIf(self, isMissed, f) as Effect.Effect<A | A2, Exclude<E, Missed> | E2, R | R2>;
|
|
152
|
+
|
|
153
|
+
/** The value as an `Option`: `None` when missing. */
|
|
154
|
+
export const option = <A, E, R>(
|
|
155
|
+
self: Effect.Effect<A, E, R>,
|
|
156
|
+
): Effect.Effect<Option.Option<A>, Exclude<E, Missed>, R> => {
|
|
157
|
+
const viaEffect = () =>
|
|
158
|
+
catchMissed(Effect.map(self, Option.some), () => Effect.succeed(Option.none<A>()));
|
|
159
|
+
const reader = readerOf(self);
|
|
160
|
+
if (!reader) return viaEffect();
|
|
161
|
+
return fromReader({
|
|
162
|
+
read: () => {
|
|
163
|
+
const value = reader.read();
|
|
164
|
+
if (value === DEFER) return DEFER;
|
|
165
|
+
return value instanceof Missing ? Option.none<A>() : Option.some(value);
|
|
166
|
+
},
|
|
167
|
+
viaEffect,
|
|
168
|
+
}) as Effect.Effect<Option.Option<A>, Exclude<E, Missed>, R>;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/** Use `value` when missing. */
|
|
172
|
+
export const withDefault =
|
|
173
|
+
<B>(value: B) =>
|
|
174
|
+
<A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A | B, Exclude<E, Missed>, R> => {
|
|
175
|
+
const viaEffect = () => catchMissed(self, () => Effect.succeed(value));
|
|
176
|
+
const reader = readerOf(self);
|
|
177
|
+
if (!reader) return viaEffect();
|
|
178
|
+
return fromReader<A | B>({
|
|
179
|
+
read: () => {
|
|
180
|
+
const v = reader.read();
|
|
181
|
+
return v instanceof Missing ? value : v;
|
|
182
|
+
},
|
|
183
|
+
viaEffect,
|
|
184
|
+
}) as Effect.Effect<A | B, Exclude<E, Missed>, R>;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/** Try `that` when missing. */
|
|
188
|
+
export const orElse =
|
|
189
|
+
<A2, E2, R2>(that: () => Effect.Effect<A2, E2, R2>) =>
|
|
190
|
+
<A, E, R>(
|
|
191
|
+
self: Effect.Effect<A, E, R>,
|
|
192
|
+
): Effect.Effect<A | A2, Exclude<E, Missed> | E2, R | R2> => {
|
|
193
|
+
const viaEffect = () => catchMissed(self, that);
|
|
194
|
+
const reader = readerOf(self);
|
|
195
|
+
if (!reader) return viaEffect();
|
|
196
|
+
return fromReader<A | A2>({
|
|
197
|
+
read: () => {
|
|
198
|
+
const v = reader.read();
|
|
199
|
+
if (!(v instanceof Missing)) return v;
|
|
200
|
+
const fallback = readerOf(that());
|
|
201
|
+
return fallback ? fallback.read() : DEFER;
|
|
202
|
+
},
|
|
203
|
+
viaEffect,
|
|
204
|
+
}) as Effect.Effect<A | A2, Exclude<E, Missed> | E2, R | R2>;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export const map =
|
|
208
|
+
<A, B>(f: (a: A) => B) =>
|
|
209
|
+
<E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<B, E, R> => {
|
|
210
|
+
const reader = readerOf(self);
|
|
211
|
+
if (!reader) return Effect.map(self, f);
|
|
212
|
+
return fromReader({
|
|
213
|
+
read: () => {
|
|
214
|
+
const v = reader.read();
|
|
215
|
+
return v === DEFER || v instanceof Missing ? v : f(v);
|
|
216
|
+
},
|
|
217
|
+
viaEffect: () => Effect.map(reader.viaEffect(), f),
|
|
218
|
+
}) as Effect.Effect<B, E, R>;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/** Read every value of a struct of readers. */
|
|
222
|
+
export const all = <const T extends Record<string, Effect.Effect<any, any, any>>>(
|
|
223
|
+
readers: T,
|
|
224
|
+
): Effect.Effect<
|
|
225
|
+
{ -readonly [K in keyof T]: Effect.Success<T[K]> },
|
|
226
|
+
Effect.Error<T[keyof T]>,
|
|
227
|
+
Effect.Services<T[keyof T]>
|
|
228
|
+
> => {
|
|
229
|
+
const viaEffect = () => Effect.all(readers) as any;
|
|
230
|
+
const keys = Object.keys(readers);
|
|
231
|
+
const parts = keys.map((k) => readerOf(readers[k]!));
|
|
232
|
+
if (parts.some((r) => r === undefined)) return viaEffect();
|
|
233
|
+
return fromReader({
|
|
234
|
+
read: () => {
|
|
235
|
+
const out: Record<string, unknown> = {};
|
|
236
|
+
for (let i = 0; i < keys.length; i++) {
|
|
237
|
+
const v = parts[i]!.read();
|
|
238
|
+
if (v === DEFER || v instanceof Missing) return v;
|
|
239
|
+
out[keys[i]!] = v;
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
},
|
|
243
|
+
viaEffect: viaEffect,
|
|
244
|
+
}) as any;
|
|
245
|
+
};
|