effect 4.0.0-beta.104 → 4.0.0-beta.106
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/AGENTS.md +381 -0
- package/CLAUDE.md +381 -0
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +39 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +5 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +165 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +110 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +116 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +91 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +12 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +71 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +62 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +136 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +24 -0
- package/dist/Brand.d.ts +3 -3
- package/dist/Brand.d.ts.map +1 -1
- package/dist/Brand.js +4 -3
- package/dist/Brand.js.map +1 -1
- package/dist/Config.d.ts.map +1 -1
- package/dist/Config.js +5 -4
- package/dist/Config.js.map +1 -1
- package/dist/ConfigProvider.d.ts +35 -3
- package/dist/ConfigProvider.d.ts.map +1 -1
- package/dist/ConfigProvider.js +41 -8
- package/dist/ConfigProvider.js.map +1 -1
- package/dist/Context.d.ts +0 -1
- package/dist/Context.d.ts.map +1 -1
- package/dist/Context.js +1 -22
- package/dist/Context.js.map +1 -1
- package/dist/Cron.d.ts +31 -0
- package/dist/Cron.d.ts.map +1 -1
- package/dist/Cron.js +58 -2
- package/dist/Cron.js.map +1 -1
- package/dist/Fiber.d.ts +1 -1
- package/dist/Fiber.d.ts.map +1 -1
- package/dist/Function.d.ts +26 -0
- package/dist/Function.d.ts.map +1 -1
- package/dist/Function.js +36 -0
- package/dist/Function.js.map +1 -1
- package/dist/JsonSchema.d.ts.map +1 -1
- package/dist/JsonSchema.js +34 -17
- package/dist/JsonSchema.js.map +1 -1
- package/dist/Optic.d.ts +35 -21
- package/dist/Optic.d.ts.map +1 -1
- package/dist/Optic.js +33 -34
- package/dist/Optic.js.map +1 -1
- package/dist/Schema.d.ts +105 -109
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +141 -104
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaAST.d.ts +32 -17
- package/dist/SchemaAST.d.ts.map +1 -1
- package/dist/SchemaAST.js +79 -60
- package/dist/SchemaAST.js.map +1 -1
- package/dist/SchemaError.d.ts +8 -8
- package/dist/SchemaError.d.ts.map +1 -1
- package/dist/SchemaError.js +7 -6
- package/dist/SchemaError.js.map +1 -1
- package/dist/SchemaGetter.d.ts +8 -5
- package/dist/SchemaGetter.d.ts.map +1 -1
- package/dist/SchemaGetter.js +41 -37
- package/dist/SchemaGetter.js.map +1 -1
- package/dist/SchemaIssue.d.ts +195 -38
- package/dist/SchemaIssue.d.ts.map +1 -1
- package/dist/SchemaIssue.js +243 -68
- package/dist/SchemaIssue.js.map +1 -1
- package/dist/SchemaParser.d.ts +30 -0
- package/dist/SchemaParser.d.ts.map +1 -1
- package/dist/SchemaParser.js +39 -9
- package/dist/SchemaParser.js.map +1 -1
- package/dist/SchemaRepresentation.d.ts +15 -3
- package/dist/SchemaRepresentation.d.ts.map +1 -1
- package/dist/SchemaRepresentation.js +8 -2
- package/dist/SchemaRepresentation.js.map +1 -1
- package/dist/SchemaTransformation.d.ts +2 -2
- package/dist/SchemaTransformation.d.ts.map +1 -1
- package/dist/SchemaTransformation.js +26 -26
- package/dist/SchemaTransformation.js.map +1 -1
- package/dist/ScopedRef.d.ts.map +1 -1
- package/dist/ScopedRef.js +1 -1
- package/dist/ScopedRef.js.map +1 -1
- package/dist/Stdio.d.ts +17 -4
- package/dist/Stdio.d.ts.map +1 -1
- package/dist/Stdio.js +6 -3
- package/dist/Stdio.js.map +1 -1
- package/dist/TxQueue.js +1 -1
- package/dist/TxQueue.js.map +1 -1
- package/dist/internal/effect.js +2 -4
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/executionPlan.js +3 -2
- package/dist/internal/executionPlan.js.map +1 -1
- package/dist/internal/rcRef.js +18 -13
- package/dist/internal/rcRef.js.map +1 -1
- package/dist/internal/schema/fromJsonSchemaDocument.js +10 -6
- package/dist/internal/schema/fromJsonSchemaDocument.js.map +1 -1
- package/dist/internal/schema/schema.js +1 -9
- package/dist/internal/schema/schema.js.map +1 -1
- package/dist/internal/schema/toArbitrary.d.ts +1 -4
- package/dist/internal/schema/toArbitrary.d.ts.map +1 -1
- package/dist/internal/schema/toArbitrary.js +0 -74
- package/dist/internal/schema/toArbitrary.js.map +1 -1
- package/dist/internal/schema/toJsonSchemaDocument.js +18 -4
- package/dist/internal/schema/toJsonSchemaDocument.js.map +1 -1
- package/dist/internal/schema/toRepresentation.js +45 -43
- package/dist/internal/schema/toRepresentation.js.map +1 -1
- package/dist/testing/TestSchema.d.ts +1 -1
- package/dist/testing/TestSchema.d.ts.map +1 -1
- package/dist/testing/TestSchema.js +10 -9
- package/dist/testing/TestSchema.js.map +1 -1
- package/dist/unstable/ai/McpSchema.d.ts +110 -110
- package/dist/unstable/ai/McpSchema.d.ts.map +1 -1
- package/dist/unstable/ai/McpServer.d.ts.map +1 -1
- package/dist/unstable/ai/McpServer.js +23 -11
- package/dist/unstable/ai/McpServer.js.map +1 -1
- package/dist/unstable/ai/Prompt.d.ts +4 -3
- package/dist/unstable/ai/Prompt.d.ts.map +1 -1
- package/dist/unstable/ai/Prompt.js +42 -17
- package/dist/unstable/ai/Prompt.js.map +1 -1
- package/dist/unstable/cluster/Entity.d.ts +2 -2
- package/dist/unstable/cluster/Entity.d.ts.map +1 -1
- package/dist/unstable/cluster/Entity.js.map +1 -1
- package/dist/unstable/cluster/EntityProxy.d.ts +3 -3
- package/dist/unstable/cluster/EntityProxy.d.ts.map +1 -1
- package/dist/unstable/cluster/EntityProxy.js +4 -3
- package/dist/unstable/cluster/EntityProxy.js.map +1 -1
- package/dist/unstable/cluster/Reply.js +4 -4
- package/dist/unstable/cluster/Reply.js.map +1 -1
- package/dist/unstable/cluster/Sharding.d.ts +2 -2
- package/dist/unstable/cluster/Sharding.d.ts.map +1 -1
- package/dist/unstable/cluster/Sharding.js +14 -5
- package/dist/unstable/cluster/Sharding.js.map +1 -1
- package/dist/unstable/encoding/Msgpack.d.ts.map +1 -1
- package/dist/unstable/encoding/Msgpack.js +6 -6
- package/dist/unstable/encoding/Msgpack.js.map +1 -1
- package/dist/unstable/eventlog/EventLogEncryption.d.ts +6 -5
- package/dist/unstable/eventlog/EventLogEncryption.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventLogEncryption.js +14 -12
- package/dist/unstable/eventlog/EventLogEncryption.js.map +1 -1
- package/dist/unstable/eventlog/EventLogMessage.d.ts +3 -3
- package/dist/unstable/eventlog/EventLogMessage.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventLogMessage.js +2 -3
- package/dist/unstable/eventlog/EventLogMessage.js.map +1 -1
- package/dist/unstable/eventlog/EventLogRemote.js +4 -4
- package/dist/unstable/eventlog/EventLogRemote.js.map +1 -1
- package/dist/unstable/eventlog/EventLogServerEncrypted.js +3 -2
- package/dist/unstable/eventlog/EventLogServerEncrypted.js.map +1 -1
- package/dist/unstable/http/HttpClient.d.ts +59 -3
- package/dist/unstable/http/HttpClient.d.ts.map +1 -1
- package/dist/unstable/http/HttpClient.js +33 -19
- package/dist/unstable/http/HttpClient.js.map +1 -1
- package/dist/unstable/http/HttpServerRequest.d.ts.map +1 -1
- package/dist/unstable/http/HttpServerRequest.js +7 -1
- package/dist/unstable/http/HttpServerRequest.js.map +1 -1
- package/dist/unstable/http/MultipartParser/internal/multipart.d.ts.map +1 -1
- package/dist/unstable/http/MultipartParser/internal/multipart.js +18 -6
- package/dist/unstable/http/MultipartParser/internal/multipart.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.js +10 -10
- package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.js +9 -9
- package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
- package/dist/unstable/httpapi/OpenApi.d.ts.map +1 -1
- package/dist/unstable/httpapi/OpenApi.js +1 -1
- package/dist/unstable/httpapi/OpenApi.js.map +1 -1
- package/dist/unstable/reactivity/AsyncResult.d.ts.map +1 -1
- package/dist/unstable/reactivity/AsyncResult.js +4 -4
- package/dist/unstable/reactivity/AsyncResult.js.map +1 -1
- package/dist/unstable/reactivity/Atom.d.ts +26 -11
- package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
- package/dist/unstable/reactivity/Atom.js +9 -22
- package/dist/unstable/reactivity/Atom.js.map +1 -1
- package/dist/unstable/rpc/RpcClient.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcClient.js +8 -1
- package/dist/unstable/rpc/RpcClient.js.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +3 -2
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/sql/SqlResolver.d.ts.map +1 -1
- package/dist/unstable/sql/SqlResolver.js +20 -16
- package/dist/unstable/sql/SqlResolver.js.map +1 -1
- package/dist/unstable/workers/Worker.d.ts.map +1 -1
- package/dist/unstable/workers/Worker.js +11 -12
- package/dist/unstable/workers/Worker.js.map +1 -1
- package/dist/unstable/workflow/Workflow.d.ts.map +1 -1
- package/dist/unstable/workflow/Workflow.js +2 -2
- package/dist/unstable/workflow/Workflow.js.map +1 -1
- package/package.json +5 -2
- package/src/Brand.ts +4 -4
- package/src/Config.ts +6 -4
- package/src/ConfigProvider.ts +43 -8
- package/src/Context.ts +1 -23
- package/src/Cron.ts +65 -2
- package/src/Fiber.ts +1 -1
- package/src/Function.ts +37 -0
- package/src/JsonSchema.ts +43 -42
- package/src/Optic.ts +59 -58
- package/src/Schema.ts +213 -217
- package/src/SchemaAST.ts +135 -78
- package/src/SchemaError.ts +9 -9
- package/src/SchemaGetter.ts +89 -30
- package/src/SchemaIssue.ts +285 -80
- package/src/SchemaParser.ts +51 -17
- package/src/SchemaRepresentation.ts +15 -3
- package/src/SchemaTransformation.ts +66 -18
- package/src/ScopedRef.ts +3 -1
- package/src/Stdio.ts +23 -4
- package/src/TxQueue.ts +1 -1
- package/src/internal/effect.ts +3 -6
- package/src/internal/executionPlan.ts +3 -2
- package/src/internal/rcRef.ts +24 -16
- package/src/internal/schema/fromJsonSchemaDocument.ts +19 -14
- package/src/internal/schema/schema.ts +1 -17
- package/src/internal/schema/toArbitrary.ts +0 -72
- package/src/internal/schema/toJsonSchemaDocument.ts +24 -4
- package/src/internal/schema/toRepresentation.ts +66 -48
- package/src/testing/TestSchema.ts +10 -10
- package/src/unstable/ai/McpServer.ts +23 -11
- package/src/unstable/ai/Prompt.ts +51 -19
- package/src/unstable/cluster/Entity.ts +7 -2
- package/src/unstable/cluster/EntityProxy.ts +12 -5
- package/src/unstable/cluster/Reply.ts +4 -4
- package/src/unstable/cluster/Sharding.ts +29 -17
- package/src/unstable/encoding/Msgpack.ts +12 -8
- package/src/unstable/eventlog/EventLogEncryption.ts +15 -16
- package/src/unstable/eventlog/EventLogMessage.ts +2 -3
- package/src/unstable/eventlog/EventLogRemote.ts +4 -4
- package/src/unstable/eventlog/EventLogServerEncrypted.ts +2 -2
- package/src/unstable/http/HttpClient.ts +132 -23
- package/src/unstable/http/HttpServerRequest.ts +8 -3
- package/src/unstable/http/MultipartParser/internal/multipart.ts +19 -6
- package/src/unstable/httpapi/HttpApiBuilder.ts +32 -13
- package/src/unstable/httpapi/HttpApiClient.ts +26 -8
- package/src/unstable/httpapi/OpenApi.ts +4 -1
- package/src/unstable/reactivity/AsyncResult.ts +11 -6
- package/src/unstable/reactivity/Atom.ts +44 -25
- package/src/unstable/rpc/RpcClient.ts +9 -1
- package/src/unstable/rpc/RpcServer.ts +4 -2
- package/src/unstable/sql/SqlResolver.ts +28 -20
- package/src/unstable/workers/Worker.ts +13 -14
- package/src/unstable/workflow/Workflow.ts +2 -5
package/src/Optic.ts
CHANGED
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
* @since 4.0.0
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
import { format } from "./Formatter.ts"
|
|
16
15
|
import { identity } from "./Function.ts"
|
|
17
16
|
import * as InternalRecord from "./internal/record.ts"
|
|
18
17
|
import * as Option from "./Option.ts"
|
|
@@ -20,7 +19,7 @@ import * as Predicate from "./Predicate.ts"
|
|
|
20
19
|
import * as Result from "./Result.ts"
|
|
21
20
|
import type * as Schema from "./Schema.ts"
|
|
22
21
|
import * as SchemaAST from "./SchemaAST.ts"
|
|
23
|
-
import
|
|
22
|
+
import * as SchemaIssue from "./SchemaIssue.ts"
|
|
24
23
|
import * as Struct from "./Struct.ts"
|
|
25
24
|
import type { IsUnion } from "./Types.ts"
|
|
26
25
|
|
|
@@ -192,7 +191,7 @@ export function makeLens<S, A>(get: (s: S) => A, replace: (a: A, s: S) => S): Le
|
|
|
192
191
|
* **Details**
|
|
193
192
|
*
|
|
194
193
|
* - `getResult(s)` returns `Result.Success<A>` when the focus matches, or
|
|
195
|
-
* `Result.Failure<
|
|
194
|
+
* `Result.Failure<SchemaIssue.Issue>` with a structured issue.
|
|
196
195
|
* - `set(a)` always succeeds and returns a new `S`.
|
|
197
196
|
* - Extends {@link Optional}.
|
|
198
197
|
* - Composing two Prisms produces a Prism; composing a Prism with a
|
|
@@ -235,17 +234,20 @@ export interface Prism<in out S, in out A> extends Optional<S, A> {
|
|
|
235
234
|
*
|
|
236
235
|
* **Details**
|
|
237
236
|
*
|
|
238
|
-
* - `getResult` should return `Result.fail(
|
|
237
|
+
* - `getResult` should return `Result.fail(issue)` on mismatch.
|
|
238
|
+
* - Issues are not formatted automatically; callers choose how to render them.
|
|
239
239
|
*
|
|
240
240
|
* **Example** (Parsing a string to a number)
|
|
241
241
|
*
|
|
242
242
|
* ```ts import.meta.vitest
|
|
243
|
-
* import { Optic, Result } from "effect"
|
|
243
|
+
* import { Optic, Result, SchemaIssue } from "effect"
|
|
244
244
|
*
|
|
245
245
|
* const numeric = Optic.makePrism<string, number>(
|
|
246
246
|
* (s) => {
|
|
247
247
|
* const n = Number(s)
|
|
248
|
-
* return Number.isNaN(n)
|
|
248
|
+
* return Number.isNaN(n)
|
|
249
|
+
* ? Result.fail(new SchemaIssue.InvalidValue({ message: "not a number" }))
|
|
250
|
+
* : Result.succeed(n)
|
|
249
251
|
* },
|
|
250
252
|
* String
|
|
251
253
|
* )
|
|
@@ -261,7 +263,10 @@ export interface Prism<in out S, in out A> extends Optional<S, A> {
|
|
|
261
263
|
* @category constructors
|
|
262
264
|
* @since 4.0.0
|
|
263
265
|
*/
|
|
264
|
-
export function makePrism<S, A>(
|
|
266
|
+
export function makePrism<S, A>(
|
|
267
|
+
getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>,
|
|
268
|
+
set: (a: A) => S
|
|
269
|
+
): Prism<S, A> {
|
|
265
270
|
return make(primitiveNode("Prism", getResult, set))
|
|
266
271
|
}
|
|
267
272
|
|
|
@@ -276,7 +281,7 @@ export function makePrism<S, A>(getResult: (s: S) => Result.Result<A, string>, s
|
|
|
276
281
|
*
|
|
277
282
|
* **Details**
|
|
278
283
|
*
|
|
279
|
-
* - `getResult` runs all checks
|
|
284
|
+
* - `getResult` runs all checks and preserves their structured issues when
|
|
280
285
|
* any check fails.
|
|
281
286
|
* - `set` is identity — the value passes through unchanged.
|
|
282
287
|
*
|
|
@@ -366,12 +371,12 @@ class CheckNode<T> {
|
|
|
366
371
|
readonly _tag = "CheckNode"
|
|
367
372
|
readonly kind = "Prism"
|
|
368
373
|
readonly checks: readonly [SchemaAST.Check<T>, ...Array<SchemaAST.Check<T>>]
|
|
369
|
-
readonly get: (s: T) => Result.Result<T,
|
|
374
|
+
readonly get: (s: T) => Result.Result<T, SchemaIssue.Issue>
|
|
370
375
|
readonly set = identity
|
|
371
376
|
|
|
372
377
|
constructor(checks: readonly [SchemaAST.Check<T>, ...Array<SchemaAST.Check<T>>]) {
|
|
373
378
|
this.checks = checks
|
|
374
|
-
this.get = (s) =>
|
|
379
|
+
this.get = (s) => SchemaAST.runChecks(checks, s)
|
|
375
380
|
}
|
|
376
381
|
}
|
|
377
382
|
|
|
@@ -407,9 +412,9 @@ type ForbidUnion<A, Message extends string> = IsUnion<A> extends true ? [Message
|
|
|
407
412
|
*
|
|
408
413
|
* **Details**
|
|
409
414
|
*
|
|
410
|
-
* - `getResult(s)` returns `Result.Success<A>` or `Result.Failure<
|
|
415
|
+
* - `getResult(s)` returns `Result.Success<A>` or `Result.Failure<SchemaIssue.Issue>`.
|
|
411
416
|
* - `replaceResult(a, s)` returns `Result.Success<S>` or
|
|
412
|
-
* `Result.Failure<
|
|
417
|
+
* `Result.Failure<SchemaIssue.Issue>`.
|
|
413
418
|
* - `replace(a, s)` returns the original `s` on failure (never throws).
|
|
414
419
|
* - `modify(f)` returns the original `s` on failure (never throws).
|
|
415
420
|
* - All operations are pure; inputs are never mutated.
|
|
@@ -441,9 +446,9 @@ export interface Optional<in out S, in out A> {
|
|
|
441
446
|
/**
|
|
442
447
|
* Attempts to read the focus `A` from the whole `S`. Returns
|
|
443
448
|
* `Result.Success<A>` when the focus exists, or
|
|
444
|
-
* `Result.Failure<
|
|
449
|
+
* `Result.Failure<SchemaIssue.Issue>` with a structured issue otherwise.
|
|
445
450
|
*/
|
|
446
|
-
readonly getResult: (s: S) => Result.Result<A,
|
|
451
|
+
readonly getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>
|
|
447
452
|
/**
|
|
448
453
|
* Replaces the focus in `S` with a new `A`. Returns the original `s`
|
|
449
454
|
* unchanged when the optic cannot focus (never throws).
|
|
@@ -453,7 +458,7 @@ export interface Optional<in out S, in out A> {
|
|
|
453
458
|
* Like {@link replace}, but returns an explicit `Result` so callers can
|
|
454
459
|
* detect and handle failure.
|
|
455
460
|
*/
|
|
456
|
-
readonly replaceResult: (a: A, s: S) => Result.Result<S,
|
|
461
|
+
readonly replaceResult: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
|
|
457
462
|
/**
|
|
458
463
|
* Composes this optic with another. The result type is the weakest of
|
|
459
464
|
* the two: Iso + Iso = Iso, Lens + Prism = Optional, etc.
|
|
@@ -635,6 +640,8 @@ export interface Optional<in out S, in out A> {
|
|
|
635
640
|
* - On a {@link Prism}, returns a Prism.
|
|
636
641
|
* - On an {@link Optional}, returns an Optional.
|
|
637
642
|
* - Shorthand for `.refine(s => s._tag === tag)`.
|
|
643
|
+
* - A non-matching value fails with {@link SchemaIssue.InvalidValue} whose
|
|
644
|
+
* `expected` annotation is `"<tag> tag"`.
|
|
638
645
|
*
|
|
639
646
|
* **Example** (Focusing a tagged variant)
|
|
640
647
|
*
|
|
@@ -674,6 +681,9 @@ export interface Optional<in out S, in out A> {
|
|
|
674
681
|
*
|
|
675
682
|
* - Always returns an {@link Optional}.
|
|
676
683
|
* - Does **not** work on union types (compile error).
|
|
684
|
+
* - A missing key fails with a {@link SchemaIssue.Pointer} at that key whose
|
|
685
|
+
* inner issue is {@link SchemaIssue.MissingKey}, for both `getResult` and
|
|
686
|
+
* `replaceResult`.
|
|
677
687
|
*
|
|
678
688
|
* **Example** (Accessing records safely)
|
|
679
689
|
*
|
|
@@ -800,7 +810,9 @@ export interface Optional<in out S, in out A> {
|
|
|
800
810
|
* element. Non-focusable elements are skipped.
|
|
801
811
|
* - **replaceResult** expects exactly as many values as were collected by
|
|
802
812
|
* `getResult` and writes them back in order. Fails with a
|
|
803
|
-
*
|
|
813
|
+
* {@link SchemaIssue.InvalidValue} if counts differ. If an inner replacement
|
|
814
|
+
* fails, its issue is wrapped in a {@link SchemaIssue.Pointer} at the element
|
|
815
|
+
* index.
|
|
804
816
|
*
|
|
805
817
|
* **Example** (Incrementing liked posts)
|
|
806
818
|
*
|
|
@@ -871,26 +883,29 @@ export interface Optional<in out S, in out A> {
|
|
|
871
883
|
*
|
|
872
884
|
* **Details**
|
|
873
885
|
*
|
|
874
|
-
* - `getResult` should return `Result.fail(
|
|
875
|
-
* - `set` should return `Result.fail(
|
|
886
|
+
* - `getResult` should return `Result.fail(issue)` on mismatch.
|
|
887
|
+
* - `set` should return `Result.fail(issue)` when the update cannot be
|
|
876
888
|
* applied.
|
|
889
|
+
* - Issues are not formatted automatically; callers choose how to render them.
|
|
877
890
|
*
|
|
878
891
|
* **Example** (Accessing record keys safely)
|
|
879
892
|
*
|
|
880
893
|
* ```ts import.meta.vitest
|
|
881
|
-
* import { Optic, Result } from "effect"
|
|
894
|
+
* import { Optic, Result, SchemaIssue } from "effect"
|
|
882
895
|
*
|
|
883
|
-
* const atKey = (key: string) =>
|
|
884
|
-
*
|
|
896
|
+
* const atKey = (key: string) => {
|
|
897
|
+
* const issue = new SchemaIssue.Pointer([key], new SchemaIssue.MissingKey(undefined))
|
|
898
|
+
* return Optic.makeOptional<Record<string, number>, number>(
|
|
885
899
|
* (s) =>
|
|
886
900
|
* Object.hasOwn(s, key)
|
|
887
901
|
* ? Result.succeed(s[key])
|
|
888
|
-
* : Result.fail(
|
|
902
|
+
* : Result.fail(issue),
|
|
889
903
|
* (a, s) =>
|
|
890
904
|
* Object.hasOwn(s, key)
|
|
891
905
|
* ? Result.succeed({ ...s, [key]: a })
|
|
892
|
-
* : Result.fail(
|
|
906
|
+
* : Result.fail(issue)
|
|
893
907
|
* )
|
|
908
|
+
* }
|
|
894
909
|
*
|
|
895
910
|
* atKey("x").getResult({ x: 1 }) // => Result.succeed(1)
|
|
896
911
|
* ```
|
|
@@ -903,8 +918,8 @@ export interface Optional<in out S, in out A> {
|
|
|
903
918
|
* @since 4.0.0
|
|
904
919
|
*/
|
|
905
920
|
export function makeOptional<S, A>(
|
|
906
|
-
getResult: (s: S) => Result.Result<A,
|
|
907
|
-
set: (a: A, s: S) => Result.Result<S,
|
|
921
|
+
getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>,
|
|
922
|
+
set: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
|
|
908
923
|
): Optional<S, A> {
|
|
909
924
|
return make(primitiveNode("Optional", getResult, set))
|
|
910
925
|
}
|
|
@@ -953,12 +968,12 @@ export interface Traversal<in out S, in out A> extends Optional<S, ReadonlyArray
|
|
|
953
968
|
class OptionalImpl<S, A> implements Optional<S, A> {
|
|
954
969
|
/** @internal */
|
|
955
970
|
readonly node: Node
|
|
956
|
-
readonly getResult: (s: S) => Result.Result<A,
|
|
957
|
-
readonly replaceResult: (a: A, s: S) => Result.Result<S,
|
|
971
|
+
readonly getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>
|
|
972
|
+
readonly replaceResult: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
|
|
958
973
|
constructor(
|
|
959
974
|
node: Node,
|
|
960
|
-
getResult: (s: S) => Result.Result<A,
|
|
961
|
-
replaceResult: (a: A, s: S) => Result.Result<S,
|
|
975
|
+
getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>,
|
|
976
|
+
replaceResult: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
|
|
962
977
|
) {
|
|
963
978
|
this.node = node
|
|
964
979
|
this.getResult = getResult
|
|
@@ -1007,22 +1022,22 @@ class OptionalImpl<S, A> implements Optional<S, A> {
|
|
|
1007
1022
|
return make(compose(this.node, [new CheckNode([SchemaAST.makeFilterByGuard(refinement, annotations)])]))
|
|
1008
1023
|
}
|
|
1009
1024
|
tag(tag: string): any {
|
|
1025
|
+
const err = Result.fail(new SchemaIssue.InvalidValue({ expected: `${JSON.stringify(tag)} tag` }))
|
|
1010
1026
|
return make(
|
|
1011
1027
|
compose(
|
|
1012
1028
|
this.node,
|
|
1013
1029
|
primitiveNode(
|
|
1014
1030
|
"Prism",
|
|
1015
|
-
(s) =>
|
|
1016
|
-
s._tag === tag
|
|
1017
|
-
? Result.succeed(s)
|
|
1018
|
-
: Result.fail(`Expected ${format(tag)} tag, got ${format(s._tag)}`),
|
|
1031
|
+
(s) => s._tag === tag ? Result.succeed(s) : err,
|
|
1019
1032
|
identity
|
|
1020
1033
|
)
|
|
1021
1034
|
)
|
|
1022
1035
|
)
|
|
1023
1036
|
}
|
|
1024
1037
|
at(key: PropertyKey, ..._rest: Array<any>): any {
|
|
1025
|
-
const err = Result.fail(
|
|
1038
|
+
const err = Result.fail(
|
|
1039
|
+
new SchemaIssue.Pointer([key], new SchemaIssue.MissingKey(undefined))
|
|
1040
|
+
)
|
|
1026
1041
|
return make(
|
|
1027
1042
|
compose(
|
|
1028
1043
|
this.node,
|
|
@@ -1076,7 +1091,9 @@ class OptionalImpl<S, A> implements Optional<S, A> {
|
|
|
1076
1091
|
// 2) arity check
|
|
1077
1092
|
if (bs.length !== idxs.length) {
|
|
1078
1093
|
return Result.fail(
|
|
1079
|
-
|
|
1094
|
+
new SchemaIssue.InvalidValue({
|
|
1095
|
+
message: `each: replacement length mismatch: ${bs.length} !== ${idxs.length}`
|
|
1096
|
+
})
|
|
1080
1097
|
)
|
|
1081
1098
|
}
|
|
1082
1099
|
|
|
@@ -1086,7 +1103,7 @@ class OptionalImpl<S, A> implements Optional<S, A> {
|
|
|
1086
1103
|
const i = idxs[k]
|
|
1087
1104
|
const r = inner.replaceResult(bs[k], as[i])
|
|
1088
1105
|
if (Result.isFailure(r)) {
|
|
1089
|
-
return Result.fail(
|
|
1106
|
+
return Result.fail(new SchemaIssue.Pointer([i], r.failure))
|
|
1090
1107
|
}
|
|
1091
1108
|
out[i] = r.success
|
|
1092
1109
|
}
|
|
@@ -1133,7 +1150,7 @@ class LensImpl<S, A> extends OptionalImpl<S, A> implements Lens<S, A> {
|
|
|
1133
1150
|
|
|
1134
1151
|
class PrismImpl<S, A> extends OptionalImpl<S, A> implements Prism<S, A> {
|
|
1135
1152
|
readonly set: (a: A) => S
|
|
1136
|
-
constructor(node: Node, getResult: (s: S) => Result.Result<A,
|
|
1153
|
+
constructor(node: Node, getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>, set: (a: A) => S) {
|
|
1137
1154
|
super(node, getResult, (a, _) => Result.succeed(set(a)))
|
|
1138
1155
|
this.set = set
|
|
1139
1156
|
}
|
|
@@ -1390,7 +1407,7 @@ export function entries<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [str
|
|
|
1390
1407
|
*
|
|
1391
1408
|
* **Details**
|
|
1392
1409
|
*
|
|
1393
|
-
* - `getResult` fails with
|
|
1410
|
+
* - `getResult` fails with a structured issue when the option is `None`.
|
|
1394
1411
|
* - `set(a)` wraps `a` in `Option.some(a)`.
|
|
1395
1412
|
*
|
|
1396
1413
|
* **Example** (Accessing Some value)
|
|
@@ -1416,11 +1433,7 @@ export function entries<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [str
|
|
|
1416
1433
|
export function some<A>(): Prism<Option.Option<A>, A> {
|
|
1417
1434
|
const run = runRefinement(Option.isSome, { expected: "a Some value" })
|
|
1418
1435
|
return makePrism(
|
|
1419
|
-
(s) =>
|
|
1420
|
-
Result.mapBoth(run(s), {
|
|
1421
|
-
onFailure: String,
|
|
1422
|
-
onSuccess: (s) => s.value
|
|
1423
|
-
}),
|
|
1436
|
+
(s) => Result.map(run(s), (s) => s.value),
|
|
1424
1437
|
Option.some
|
|
1425
1438
|
)
|
|
1426
1439
|
}
|
|
@@ -1459,11 +1472,7 @@ export function some<A>(): Prism<Option.Option<A>, A> {
|
|
|
1459
1472
|
export function none<A>(): Prism<Option.Option<A>, undefined> {
|
|
1460
1473
|
const run = runRefinement(Option.isNone, { expected: "a None value" })
|
|
1461
1474
|
return makePrism(
|
|
1462
|
-
(s) =>
|
|
1463
|
-
Result.mapBoth(run(s), {
|
|
1464
|
-
onFailure: String,
|
|
1465
|
-
onSuccess: () => undefined
|
|
1466
|
-
}),
|
|
1475
|
+
(s) => Result.map(run(s), () => undefined),
|
|
1467
1476
|
() => Option.none()
|
|
1468
1477
|
)
|
|
1469
1478
|
}
|
|
@@ -1502,11 +1511,7 @@ export function none<A>(): Prism<Option.Option<A>, undefined> {
|
|
|
1502
1511
|
export function success<A, E>(): Prism<Result.Result<A, E>, A> {
|
|
1503
1512
|
const run = runRefinement(Result.isSuccess, { expected: "a Result.Success value" })
|
|
1504
1513
|
return makePrism(
|
|
1505
|
-
(s) =>
|
|
1506
|
-
Result.mapBoth(run(s), {
|
|
1507
|
-
onFailure: String,
|
|
1508
|
-
onSuccess: (s) => s.success
|
|
1509
|
-
}),
|
|
1514
|
+
(s) => Result.map(run(s), (s) => s.success),
|
|
1510
1515
|
Result.succeed
|
|
1511
1516
|
)
|
|
1512
1517
|
}
|
|
@@ -1545,11 +1550,7 @@ export function success<A, E>(): Prism<Result.Result<A, E>, A> {
|
|
|
1545
1550
|
export function failure<A, E>(): Prism<Result.Result<A, E>, E> {
|
|
1546
1551
|
const run = runRefinement(Result.isFailure, { expected: "a Result.Failure value" })
|
|
1547
1552
|
return makePrism(
|
|
1548
|
-
(s) =>
|
|
1549
|
-
Result.mapBoth(run(s), {
|
|
1550
|
-
onFailure: String,
|
|
1551
|
-
onSuccess: (s) => s.failure
|
|
1552
|
-
}),
|
|
1553
|
+
(s) => Result.map(run(s), (s) => s.failure),
|
|
1553
1554
|
Result.fail
|
|
1554
1555
|
)
|
|
1555
1556
|
}
|