effect 4.0.0-rc.113 → 4.0.0-rc.115

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.
Files changed (76) hide show
  1. package/dist/Effect.d.ts +268 -7
  2. package/dist/Effect.d.ts.map +1 -1
  3. package/dist/Effect.js +33 -2
  4. package/dist/Effect.js.map +1 -1
  5. package/dist/Effectable.d.ts +1 -2
  6. package/dist/Effectable.d.ts.map +1 -1
  7. package/dist/Effectable.js.map +1 -1
  8. package/dist/FileSystem.d.ts +3 -4
  9. package/dist/FileSystem.d.ts.map +1 -1
  10. package/dist/FileSystem.js.map +1 -1
  11. package/dist/Match.d.ts +1 -14
  12. package/dist/Match.d.ts.map +1 -1
  13. package/dist/Schema.d.ts +4 -4
  14. package/dist/Schema.d.ts.map +1 -1
  15. package/dist/SchemaAST.d.ts.map +1 -1
  16. package/dist/SchemaAST.js +43 -23
  17. package/dist/SchemaAST.js.map +1 -1
  18. package/dist/SchemaParser.d.ts.map +1 -1
  19. package/dist/SchemaParser.js +3 -2
  20. package/dist/SchemaParser.js.map +1 -1
  21. package/dist/SchemaRepresentation.d.ts +39 -24
  22. package/dist/SchemaRepresentation.d.ts.map +1 -1
  23. package/dist/SchemaRepresentation.js +37 -23
  24. package/dist/SchemaRepresentation.js.map +1 -1
  25. package/dist/internal/arbitrary/schema.js.map +1 -1
  26. package/dist/internal/effect.js +16 -8
  27. package/dist/internal/effect.js.map +1 -1
  28. package/dist/internal/schema/fromJsonSchemaDocument.js +221 -230
  29. package/dist/internal/schema/fromJsonSchemaDocument.js.map +1 -1
  30. package/dist/internal/schema/make.js +8 -3
  31. package/dist/internal/schema/make.js.map +1 -1
  32. package/dist/internal/schema/toCodeDocument.js +4 -4
  33. package/dist/internal/schema/toCodeDocument.js.map +1 -1
  34. package/dist/internal/schema/toEquivalence.js +2 -2
  35. package/dist/internal/schema/toEquivalence.js.map +1 -1
  36. package/dist/internal/schema/toFormatter.js +2 -2
  37. package/dist/internal/schema/toFormatter.js.map +1 -1
  38. package/dist/unstable/cli/Prompt.d.ts +6 -2
  39. package/dist/unstable/cli/Prompt.d.ts.map +1 -1
  40. package/dist/unstable/cli/Prompt.js +8 -7
  41. package/dist/unstable/cli/Prompt.js.map +1 -1
  42. package/dist/unstable/cli/internal/command.d.ts +1 -6
  43. package/dist/unstable/cli/internal/command.d.ts.map +1 -1
  44. package/dist/unstable/cli/internal/command.js +2 -0
  45. package/dist/unstable/cli/internal/command.js.map +1 -1
  46. package/dist/unstable/http/HttpEffect.d.ts.map +1 -1
  47. package/dist/unstable/http/HttpEffect.js +6 -2
  48. package/dist/unstable/http/HttpEffect.js.map +1 -1
  49. package/dist/unstable/http/HttpServerResponse.d.ts +13 -2
  50. package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
  51. package/dist/unstable/http/HttpServerResponse.js +19 -5
  52. package/dist/unstable/http/HttpServerResponse.js.map +1 -1
  53. package/dist/unstable/persistence/Persistence.d.ts.map +1 -1
  54. package/dist/unstable/persistence/Persistence.js +2 -10
  55. package/dist/unstable/persistence/Persistence.js.map +1 -1
  56. package/package.json +2 -2
  57. package/src/Effect.ts +271 -7
  58. package/src/Effectable.ts +2 -2
  59. package/src/FileSystem.ts +3 -4
  60. package/src/Match.ts +1 -1
  61. package/src/Schema.ts +4 -7
  62. package/src/SchemaAST.ts +48 -25
  63. package/src/SchemaParser.ts +3 -2
  64. package/src/SchemaRepresentation.ts +39 -24
  65. package/src/internal/arbitrary/schema.ts +16 -16
  66. package/src/internal/effect.ts +34 -11
  67. package/src/internal/schema/fromJsonSchemaDocument.ts +294 -268
  68. package/src/internal/schema/make.ts +9 -4
  69. package/src/internal/schema/toCodeDocument.ts +4 -4
  70. package/src/internal/schema/toEquivalence.ts +2 -2
  71. package/src/internal/schema/toFormatter.ts +2 -2
  72. package/src/unstable/cli/Prompt.ts +22 -11
  73. package/src/unstable/cli/internal/command.ts +2 -0
  74. package/src/unstable/http/HttpEffect.ts +5 -2
  75. package/src/unstable/http/HttpServerResponse.ts +21 -5
  76. package/src/unstable/persistence/Persistence.ts +6 -16
package/dist/Effect.d.ts CHANGED
@@ -12949,8 +12949,8 @@ export declare const onExitFilter: {
12949
12949
  */
12950
12950
  export declare const cached: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E, R>>;
12951
12951
  /**
12952
- * Returns an effect that caches its result for a specified `Duration`,
12953
- * known as "timeToLive" (TTL).
12952
+ * Returns an effect that caches its result for a fixed duration or a duration
12953
+ * computed from its `Exit`, known as "timeToLive" (TTL).
12954
12954
  *
12955
12955
  * **When to use**
12956
12956
  *
@@ -12969,6 +12969,17 @@ export declare const cached: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A
12969
12969
  * After the specified duration has passed, the cache expires, and the effect
12970
12970
  * will be recomputed upon the next evaluation.
12971
12971
  *
12972
+ * `timeToLive` accepts a `Duration.Input` or a function from `Exit<A, E>` to
12973
+ * `Duration.Input`. The function runs once after each fresh computation,
12974
+ * including failures, so successes and failures can have different TTLs. It
12975
+ * does not run when the cache is created or when a cached result is reused.
12976
+ * The callback also receives interruption exits, which are cached for the
12977
+ * returned duration.
12978
+ *
12979
+ * The TTL starts when the computation completes. Concurrent callers share the
12980
+ * pending computation. A zero TTL expires immediately, and an infinite TTL
12981
+ * keeps the result indefinitely.
12982
+ *
12972
12983
  * **Example** (Memoizing an effect with TTL)
12973
12984
  *
12974
12985
  * ```ts import.meta.vitest
@@ -12993,6 +13004,26 @@ export declare const cached: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A
12993
13004
  * output // => ["expensive task...", "result 1", "result 1", "result 1"]
12994
13005
  * ```
12995
13006
  *
13007
+ * **Example** (Caching successes while retrying failures)
13008
+ *
13009
+ * ```ts import.meta.vitest
13010
+ * import { Effect, Exit } from "effect"
13011
+ *
13012
+ * let attempts = 0
13013
+ * const task = Effect.suspend(() =>
13014
+ * ++attempts === 1 ? Effect.fail("temporary failure") : Effect.succeed(42)
13015
+ * )
13016
+ * const program = Effect.gen(function*() {
13017
+ * const cached = yield* task.pipe(
13018
+ * Effect.cachedWithTTL((exit) => Exit.isSuccess(exit) ? "1 hour" : 0)
13019
+ * )
13020
+ * yield* Effect.exit(cached)
13021
+ * return yield* cached
13022
+ * })
13023
+ *
13024
+ * Effect.runSync(program) // => 42
13025
+ * ```
13026
+ *
12996
13027
  * @see {@link cached} for a similar function that caches the result
12997
13028
  * indefinitely.
12998
13029
  * @see {@link cachedInvalidateWithTTL} for a similar function that includes an
@@ -13002,8 +13033,8 @@ export declare const cached: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A
13002
13033
  */
13003
13034
  export declare const cachedWithTTL: {
13004
13035
  /**
13005
- * Returns an effect that caches its result for a specified `Duration`,
13006
- * known as "timeToLive" (TTL).
13036
+ * Returns an effect that caches its result for a fixed duration or a duration
13037
+ * computed from its `Exit`, known as "timeToLive" (TTL).
13007
13038
  *
13008
13039
  * **When to use**
13009
13040
  *
@@ -13022,6 +13053,17 @@ export declare const cachedWithTTL: {
13022
13053
  * After the specified duration has passed, the cache expires, and the effect
13023
13054
  * will be recomputed upon the next evaluation.
13024
13055
  *
13056
+ * `timeToLive` accepts a `Duration.Input` or a function from `Exit<A, E>` to
13057
+ * `Duration.Input`. The function runs once after each fresh computation,
13058
+ * including failures, so successes and failures can have different TTLs. It
13059
+ * does not run when the cache is created or when a cached result is reused.
13060
+ * The callback also receives interruption exits, which are cached for the
13061
+ * returned duration.
13062
+ *
13063
+ * The TTL starts when the computation completes. Concurrent callers share the
13064
+ * pending computation. A zero TTL expires immediately, and an infinite TTL
13065
+ * keeps the result indefinitely.
13066
+ *
13025
13067
  * **Example** (Memoizing an effect with TTL)
13026
13068
  *
13027
13069
  * ```ts import.meta.vitest
@@ -13046,6 +13088,110 @@ export declare const cachedWithTTL: {
13046
13088
  * output // => ["expensive task...", "result 1", "result 1", "result 1"]
13047
13089
  * ```
13048
13090
  *
13091
+ * **Example** (Caching successes while retrying failures)
13092
+ *
13093
+ * ```ts import.meta.vitest
13094
+ * import { Effect, Exit } from "effect"
13095
+ *
13096
+ * let attempts = 0
13097
+ * const task = Effect.suspend(() =>
13098
+ * ++attempts === 1 ? Effect.fail("temporary failure") : Effect.succeed(42)
13099
+ * )
13100
+ * const program = Effect.gen(function*() {
13101
+ * const cached = yield* task.pipe(
13102
+ * Effect.cachedWithTTL((exit) => Exit.isSuccess(exit) ? "1 hour" : 0)
13103
+ * )
13104
+ * yield* Effect.exit(cached)
13105
+ * return yield* cached
13106
+ * })
13107
+ *
13108
+ * Effect.runSync(program) // => 42
13109
+ * ```
13110
+ *
13111
+ * @see {@link cached} for a similar function that caches the result
13112
+ * indefinitely.
13113
+ * @see {@link cachedInvalidateWithTTL} for a similar function that includes an
13114
+ * additional effect for manually invalidating the cached value.
13115
+ * @category caching
13116
+ * @since 2.0.0
13117
+ */
13118
+ <A, E>(timeToLive: (exit: Exit.Exit<A, E>) => Duration.Input): <R>(self: Effect<A, E, R>) => Effect<Effect<A, E, R>>;
13119
+ /**
13120
+ * Returns an effect that caches its result for a fixed duration or a duration
13121
+ * computed from its `Exit`, known as "timeToLive" (TTL).
13122
+ *
13123
+ * **When to use**
13124
+ *
13125
+ * Use when you need a costly effect result to be reused for a bounded duration
13126
+ * before being recomputed.
13127
+ *
13128
+ * **Details**
13129
+ *
13130
+ * This function is used to cache the result of an effect for a specified amount
13131
+ * of time. This means that the first time the effect is evaluated, its result
13132
+ * is computed and stored.
13133
+ *
13134
+ * If the effect is evaluated again within the specified `timeToLive`, the
13135
+ * cached result will be used, avoiding recomputation.
13136
+ *
13137
+ * After the specified duration has passed, the cache expires, and the effect
13138
+ * will be recomputed upon the next evaluation.
13139
+ *
13140
+ * `timeToLive` accepts a `Duration.Input` or a function from `Exit<A, E>` to
13141
+ * `Duration.Input`. The function runs once after each fresh computation,
13142
+ * including failures, so successes and failures can have different TTLs. It
13143
+ * does not run when the cache is created or when a cached result is reused.
13144
+ * The callback also receives interruption exits, which are cached for the
13145
+ * returned duration.
13146
+ *
13147
+ * The TTL starts when the computation completes. Concurrent callers share the
13148
+ * pending computation. A zero TTL expires immediately, and an infinite TTL
13149
+ * keeps the result indefinitely.
13150
+ *
13151
+ * **Example** (Memoizing an effect with TTL)
13152
+ *
13153
+ * ```ts import.meta.vitest
13154
+ * import { Effect } from "effect"
13155
+ * const output: Array<unknown> = []
13156
+ * const record = (value: unknown) => Effect.sync(() => { output.push(value) })
13157
+ *
13158
+ * let i = 1
13159
+ * const expensiveTask = Effect.sync(() => {
13160
+ * void output.push("expensive task...")
13161
+ * return `result ${i++}`
13162
+ * })
13163
+ *
13164
+ * const program = Effect.gen(function*() {
13165
+ * const cached = yield* Effect.cachedWithTTL(expensiveTask, "1 hour")
13166
+ * yield* cached.pipe(Effect.andThen(record))
13167
+ * yield* cached.pipe(Effect.andThen(record))
13168
+ * yield* cached.pipe(Effect.andThen(record))
13169
+ * })
13170
+ *
13171
+ * Effect.runSync(program)
13172
+ * output // => ["expensive task...", "result 1", "result 1", "result 1"]
13173
+ * ```
13174
+ *
13175
+ * **Example** (Caching successes while retrying failures)
13176
+ *
13177
+ * ```ts import.meta.vitest
13178
+ * import { Effect, Exit } from "effect"
13179
+ *
13180
+ * let attempts = 0
13181
+ * const task = Effect.suspend(() =>
13182
+ * ++attempts === 1 ? Effect.fail("temporary failure") : Effect.succeed(42)
13183
+ * )
13184
+ * const program = Effect.gen(function*() {
13185
+ * const cached = yield* task.pipe(
13186
+ * Effect.cachedWithTTL((exit) => Exit.isSuccess(exit) ? "1 hour" : 0)
13187
+ * )
13188
+ * yield* Effect.exit(cached)
13189
+ * return yield* cached
13190
+ * })
13191
+ *
13192
+ * Effect.runSync(program) // => 42
13193
+ * ```
13194
+ *
13049
13195
  * @see {@link cached} for a similar function that caches the result
13050
13196
  * indefinitely.
13051
13197
  * @see {@link cachedInvalidateWithTTL} for a similar function that includes an
@@ -13055,8 +13201,8 @@ export declare const cachedWithTTL: {
13055
13201
  */
13056
13202
  (timeToLive: Duration.Input): <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E, R>>;
13057
13203
  /**
13058
- * Returns an effect that caches its result for a specified `Duration`,
13059
- * known as "timeToLive" (TTL).
13204
+ * Returns an effect that caches its result for a fixed duration or a duration
13205
+ * computed from its `Exit`, known as "timeToLive" (TTL).
13060
13206
  *
13061
13207
  * **When to use**
13062
13208
  *
@@ -13075,6 +13221,17 @@ export declare const cachedWithTTL: {
13075
13221
  * After the specified duration has passed, the cache expires, and the effect
13076
13222
  * will be recomputed upon the next evaluation.
13077
13223
  *
13224
+ * `timeToLive` accepts a `Duration.Input` or a function from `Exit<A, E>` to
13225
+ * `Duration.Input`. The function runs once after each fresh computation,
13226
+ * including failures, so successes and failures can have different TTLs. It
13227
+ * does not run when the cache is created or when a cached result is reused.
13228
+ * The callback also receives interruption exits, which are cached for the
13229
+ * returned duration.
13230
+ *
13231
+ * The TTL starts when the computation completes. Concurrent callers share the
13232
+ * pending computation. A zero TTL expires immediately, and an infinite TTL
13233
+ * keeps the result indefinitely.
13234
+ *
13078
13235
  * **Example** (Memoizing an effect with TTL)
13079
13236
  *
13080
13237
  * ```ts import.meta.vitest
@@ -13099,6 +13256,110 @@ export declare const cachedWithTTL: {
13099
13256
  * output // => ["expensive task...", "result 1", "result 1", "result 1"]
13100
13257
  * ```
13101
13258
  *
13259
+ * **Example** (Caching successes while retrying failures)
13260
+ *
13261
+ * ```ts import.meta.vitest
13262
+ * import { Effect, Exit } from "effect"
13263
+ *
13264
+ * let attempts = 0
13265
+ * const task = Effect.suspend(() =>
13266
+ * ++attempts === 1 ? Effect.fail("temporary failure") : Effect.succeed(42)
13267
+ * )
13268
+ * const program = Effect.gen(function*() {
13269
+ * const cached = yield* task.pipe(
13270
+ * Effect.cachedWithTTL((exit) => Exit.isSuccess(exit) ? "1 hour" : 0)
13271
+ * )
13272
+ * yield* Effect.exit(cached)
13273
+ * return yield* cached
13274
+ * })
13275
+ *
13276
+ * Effect.runSync(program) // => 42
13277
+ * ```
13278
+ *
13279
+ * @see {@link cached} for a similar function that caches the result
13280
+ * indefinitely.
13281
+ * @see {@link cachedInvalidateWithTTL} for a similar function that includes an
13282
+ * additional effect for manually invalidating the cached value.
13283
+ * @category caching
13284
+ * @since 2.0.0
13285
+ */
13286
+ <A, E>(timeToLive: Duration.Input | ((exit: Exit.Exit<A, E>) => Duration.Input)): <R>(self: Effect<A, E, R>) => Effect<Effect<A, E, R>>;
13287
+ /**
13288
+ * Returns an effect that caches its result for a fixed duration or a duration
13289
+ * computed from its `Exit`, known as "timeToLive" (TTL).
13290
+ *
13291
+ * **When to use**
13292
+ *
13293
+ * Use when you need a costly effect result to be reused for a bounded duration
13294
+ * before being recomputed.
13295
+ *
13296
+ * **Details**
13297
+ *
13298
+ * This function is used to cache the result of an effect for a specified amount
13299
+ * of time. This means that the first time the effect is evaluated, its result
13300
+ * is computed and stored.
13301
+ *
13302
+ * If the effect is evaluated again within the specified `timeToLive`, the
13303
+ * cached result will be used, avoiding recomputation.
13304
+ *
13305
+ * After the specified duration has passed, the cache expires, and the effect
13306
+ * will be recomputed upon the next evaluation.
13307
+ *
13308
+ * `timeToLive` accepts a `Duration.Input` or a function from `Exit<A, E>` to
13309
+ * `Duration.Input`. The function runs once after each fresh computation,
13310
+ * including failures, so successes and failures can have different TTLs. It
13311
+ * does not run when the cache is created or when a cached result is reused.
13312
+ * The callback also receives interruption exits, which are cached for the
13313
+ * returned duration.
13314
+ *
13315
+ * The TTL starts when the computation completes. Concurrent callers share the
13316
+ * pending computation. A zero TTL expires immediately, and an infinite TTL
13317
+ * keeps the result indefinitely.
13318
+ *
13319
+ * **Example** (Memoizing an effect with TTL)
13320
+ *
13321
+ * ```ts import.meta.vitest
13322
+ * import { Effect } from "effect"
13323
+ * const output: Array<unknown> = []
13324
+ * const record = (value: unknown) => Effect.sync(() => { output.push(value) })
13325
+ *
13326
+ * let i = 1
13327
+ * const expensiveTask = Effect.sync(() => {
13328
+ * void output.push("expensive task...")
13329
+ * return `result ${i++}`
13330
+ * })
13331
+ *
13332
+ * const program = Effect.gen(function*() {
13333
+ * const cached = yield* Effect.cachedWithTTL(expensiveTask, "1 hour")
13334
+ * yield* cached.pipe(Effect.andThen(record))
13335
+ * yield* cached.pipe(Effect.andThen(record))
13336
+ * yield* cached.pipe(Effect.andThen(record))
13337
+ * })
13338
+ *
13339
+ * Effect.runSync(program)
13340
+ * output // => ["expensive task...", "result 1", "result 1", "result 1"]
13341
+ * ```
13342
+ *
13343
+ * **Example** (Caching successes while retrying failures)
13344
+ *
13345
+ * ```ts import.meta.vitest
13346
+ * import { Effect, Exit } from "effect"
13347
+ *
13348
+ * let attempts = 0
13349
+ * const task = Effect.suspend(() =>
13350
+ * ++attempts === 1 ? Effect.fail("temporary failure") : Effect.succeed(42)
13351
+ * )
13352
+ * const program = Effect.gen(function*() {
13353
+ * const cached = yield* task.pipe(
13354
+ * Effect.cachedWithTTL((exit) => Exit.isSuccess(exit) ? "1 hour" : 0)
13355
+ * )
13356
+ * yield* Effect.exit(cached)
13357
+ * return yield* cached
13358
+ * })
13359
+ *
13360
+ * Effect.runSync(program) // => 42
13361
+ * ```
13362
+ *
13102
13363
  * @see {@link cached} for a similar function that caches the result
13103
13364
  * indefinitely.
13104
13365
  * @see {@link cachedInvalidateWithTTL} for a similar function that includes an
@@ -13106,7 +13367,7 @@ export declare const cachedWithTTL: {
13106
13367
  * @category caching
13107
13368
  * @since 2.0.0
13108
13369
  */
13109
- <A, E, R>(self: Effect<A, E, R>, timeToLive: Duration.Input): Effect<Effect<A, E, R>>;
13370
+ <A, E, R>(self: Effect<A, E, R>, timeToLive: Duration.Input | ((exit: Exit.Exit<A, E>) => Duration.Input)): Effect<Effect<A, E, R>>;
13110
13371
  };
13111
13372
  /**
13112
13373
  * Creates a cached effect result for a specified duration and allows manual