effect-app 4.0.0-beta.27 → 4.0.0-beta.272

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 (342) hide show
  1. package/CHANGELOG.md +1275 -0
  2. package/dist/Array.d.ts +2 -1
  3. package/dist/Array.d.ts.map +1 -1
  4. package/dist/Array.js +4 -4
  5. package/dist/Chunk.d.ts.map +1 -1
  6. package/dist/Config/SecretURL.d.ts +3 -1
  7. package/dist/Config/SecretURL.d.ts.map +1 -1
  8. package/dist/Config/SecretURL.js +3 -6
  9. package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
  10. package/dist/Config/internal/configSecretURL.js +2 -2
  11. package/dist/Config.d.ts +2 -0
  12. package/dist/Config.d.ts.map +1 -0
  13. package/dist/Config.js +8 -0
  14. package/dist/ConfigProvider.d.ts +2 -0
  15. package/dist/ConfigProvider.d.ts.map +1 -0
  16. package/dist/ConfigProvider.js +6 -0
  17. package/dist/{ServiceMap.d.ts → Context.d.ts} +18 -20
  18. package/dist/Context.d.ts.map +1 -0
  19. package/dist/Context.js +67 -0
  20. package/dist/Effect.d.ts +10 -9
  21. package/dist/Effect.d.ts.map +1 -1
  22. package/dist/Effect.js +5 -8
  23. package/dist/Emailer.d.ts +51 -0
  24. package/dist/Emailer.d.ts.map +1 -0
  25. package/dist/Emailer.js +7 -0
  26. package/dist/Function.d.ts.map +1 -1
  27. package/dist/Layer.d.ts +10 -6
  28. package/dist/Layer.d.ts.map +1 -1
  29. package/dist/Layer.js +3 -2
  30. package/dist/Model/Repository/Registry.d.ts +22 -0
  31. package/dist/Model/Repository/Registry.d.ts.map +1 -0
  32. package/dist/Model/Repository/Registry.js +18 -0
  33. package/dist/Model/Repository/ext.d.ts +60 -0
  34. package/dist/Model/Repository/ext.d.ts.map +1 -0
  35. package/dist/Model/Repository/ext.js +122 -0
  36. package/dist/Model/Repository/internal/internal.d.ts +63 -0
  37. package/dist/Model/Repository/internal/internal.d.ts.map +1 -0
  38. package/dist/Model/Repository/internal/internal.js +430 -0
  39. package/dist/Model/Repository/legacy.d.ts +21 -0
  40. package/dist/Model/Repository/legacy.d.ts.map +1 -0
  41. package/dist/Model/Repository/legacy.js +2 -0
  42. package/dist/Model/Repository/makeRepo.d.ts +54 -0
  43. package/dist/Model/Repository/makeRepo.d.ts.map +1 -0
  44. package/dist/Model/Repository/makeRepo.js +27 -0
  45. package/dist/Model/Repository/service.d.ts +121 -0
  46. package/dist/Model/Repository/service.d.ts.map +1 -0
  47. package/dist/Model/Repository/service.js +2 -0
  48. package/dist/Model/Repository/validation.d.ts +58 -0
  49. package/dist/Model/Repository/validation.d.ts.map +1 -0
  50. package/dist/Model/Repository/validation.js +32 -0
  51. package/dist/Model/Repository.d.ts +7 -0
  52. package/dist/Model/Repository.d.ts.map +1 -0
  53. package/dist/Model/Repository.js +7 -0
  54. package/dist/Model/dsl.d.ts +33 -0
  55. package/dist/Model/dsl.d.ts.map +1 -0
  56. package/dist/Model/dsl.js +43 -0
  57. package/dist/Model/filter/filterApi.d.ts +30 -0
  58. package/dist/Model/filter/filterApi.d.ts.map +1 -0
  59. package/dist/Model/filter/filterApi.js +2 -0
  60. package/dist/Model/filter/types/errors.d.ts +29 -0
  61. package/dist/Model/filter/types/errors.d.ts.map +1 -0
  62. package/dist/Model/filter/types/errors.js +2 -0
  63. package/dist/Model/filter/types/fields.d.ts +15 -0
  64. package/dist/Model/filter/types/fields.d.ts.map +1 -0
  65. package/dist/Model/filter/types/fields.js +2 -0
  66. package/dist/Model/filter/types/path/common.d.ts +316 -0
  67. package/dist/Model/filter/types/path/common.d.ts.map +1 -0
  68. package/dist/Model/filter/types/path/common.js +2 -0
  69. package/dist/Model/filter/types/path/eager.d.ts +94 -0
  70. package/dist/Model/filter/types/path/eager.d.ts.map +1 -0
  71. package/dist/Model/filter/types/path/eager.js +36 -0
  72. package/dist/Model/filter/types/path/index.d.ts +4 -0
  73. package/dist/Model/filter/types/path/index.d.ts.map +1 -0
  74. package/dist/Model/filter/types/path/index.js +3 -0
  75. package/dist/Model/filter/types/utils.d.ts +79 -0
  76. package/dist/Model/filter/types/utils.d.ts.map +1 -0
  77. package/dist/Model/filter/types/utils.js +2 -0
  78. package/dist/Model/filter/types/validator.d.ts +30 -0
  79. package/dist/Model/filter/types/validator.d.ts.map +1 -0
  80. package/dist/Model/filter/types/validator.js +2 -0
  81. package/dist/Model/filter/types.d.ts +5 -0
  82. package/dist/Model/filter/types.d.ts.map +1 -0
  83. package/dist/Model/filter/types.js +7 -0
  84. package/dist/Model/query/dsl.d.ts +493 -0
  85. package/dist/Model/query/dsl.d.ts.map +1 -0
  86. package/dist/Model/query/dsl.js +376 -0
  87. package/dist/Model/query/new-kid-interpreter.d.ts +136 -0
  88. package/dist/Model/query/new-kid-interpreter.d.ts.map +1 -0
  89. package/dist/Model/query/new-kid-interpreter.js +336 -0
  90. package/dist/Model/query.d.ts +15 -0
  91. package/dist/Model/query.d.ts.map +1 -0
  92. package/dist/Model/query.js +3 -0
  93. package/dist/Model.d.ts +5 -0
  94. package/dist/Model.d.ts.map +1 -0
  95. package/dist/Model.js +5 -0
  96. package/dist/NonEmptySet.d.ts +3 -1
  97. package/dist/NonEmptySet.d.ts.map +1 -1
  98. package/dist/NonEmptySet.js +2 -2
  99. package/dist/Option.d.ts +1 -0
  100. package/dist/Option.d.ts.map +1 -1
  101. package/dist/Option.js +3 -1
  102. package/dist/Pure.d.ts +7 -5
  103. package/dist/Pure.d.ts.map +1 -1
  104. package/dist/Pure.js +17 -14
  105. package/dist/QueueMaker.d.ts +13 -0
  106. package/dist/QueueMaker.d.ts.map +1 -0
  107. package/dist/QueueMaker.js +4 -0
  108. package/dist/RequestContext.d.ts +154 -0
  109. package/dist/RequestContext.d.ts.map +1 -0
  110. package/dist/RequestContext.js +54 -0
  111. package/dist/Schema/Class.d.ts +160 -19
  112. package/dist/Schema/Class.d.ts.map +1 -1
  113. package/dist/Schema/Class.js +260 -17
  114. package/dist/Schema/FastCheck.d.ts.map +1 -1
  115. package/dist/Schema/SchemaParser.d.ts +5 -0
  116. package/dist/Schema/SchemaParser.d.ts.map +1 -0
  117. package/dist/Schema/SchemaParser.js +6 -0
  118. package/dist/Schema/SpecialJsonSchema.d.ts +34 -0
  119. package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
  120. package/dist/Schema/SpecialJsonSchema.js +118 -0
  121. package/dist/Schema/SpecialOpenApi.d.ts +32 -0
  122. package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
  123. package/dist/Schema/SpecialOpenApi.js +123 -0
  124. package/dist/Schema/brand.d.ts +4 -2
  125. package/dist/Schema/brand.d.ts.map +1 -1
  126. package/dist/Schema/brand.js +3 -1
  127. package/dist/Schema/email.d.ts.map +1 -1
  128. package/dist/Schema/email.js +7 -4
  129. package/dist/Schema/ext.d.ts +338 -55
  130. package/dist/Schema/ext.d.ts.map +1 -1
  131. package/dist/Schema/ext.js +358 -53
  132. package/dist/Schema/moreStrings.d.ts +82 -36
  133. package/dist/Schema/moreStrings.d.ts.map +1 -1
  134. package/dist/Schema/moreStrings.js +49 -42
  135. package/dist/Schema/numbers.d.ts +34 -21
  136. package/dist/Schema/numbers.d.ts.map +1 -1
  137. package/dist/Schema/numbers.js +55 -12
  138. package/dist/Schema/phoneNumber.d.ts.map +1 -1
  139. package/dist/Schema/phoneNumber.js +6 -3
  140. package/dist/Schema/strings.d.ts +18 -4
  141. package/dist/Schema/strings.d.ts.map +1 -1
  142. package/dist/Schema/strings.js +1 -5
  143. package/dist/Schema.d.ts +213 -7
  144. package/dist/Schema.d.ts.map +1 -1
  145. package/dist/Schema.js +190 -11
  146. package/dist/Set.d.ts +4 -1
  147. package/dist/Set.d.ts.map +1 -1
  148. package/dist/Set.js +3 -2
  149. package/dist/Store.d.ts +170 -0
  150. package/dist/Store.d.ts.map +1 -0
  151. package/dist/Store.js +121 -0
  152. package/dist/_ext/Array.d.ts +1 -1
  153. package/dist/_ext/Array.d.ts.map +1 -1
  154. package/dist/_ext/Array.js +4 -2
  155. package/dist/_ext/misc.d.ts +4 -1
  156. package/dist/_ext/misc.d.ts.map +1 -1
  157. package/dist/_ext/misc.js +4 -2
  158. package/dist/_ext/ord.ext.d.ts +2 -1
  159. package/dist/_ext/ord.ext.d.ts.map +1 -1
  160. package/dist/_ext/ord.ext.js +2 -2
  161. package/dist/client/InvalidationKeys.d.ts +29 -0
  162. package/dist/client/InvalidationKeys.d.ts.map +1 -0
  163. package/dist/client/InvalidationKeys.js +33 -0
  164. package/dist/client/apiClientFactory.d.ts +19 -31
  165. package/dist/client/apiClientFactory.d.ts.map +1 -1
  166. package/dist/client/apiClientFactory.js +104 -34
  167. package/dist/client/clientFor.d.ts +52 -18
  168. package/dist/client/clientFor.d.ts.map +1 -1
  169. package/dist/client/clientFor.js +9 -1
  170. package/dist/client/errors.d.ts +82 -27
  171. package/dist/client/errors.d.ts.map +1 -1
  172. package/dist/client/errors.js +75 -19
  173. package/dist/client/makeClient.d.ts +494 -32
  174. package/dist/client/makeClient.d.ts.map +1 -1
  175. package/dist/client/makeClient.js +66 -24
  176. package/dist/client.d.ts +1 -0
  177. package/dist/client.d.ts.map +1 -1
  178. package/dist/client.js +2 -1
  179. package/dist/faker.d.ts.map +1 -1
  180. package/dist/http/Request.d.ts +1 -1
  181. package/dist/http/Request.d.ts.map +1 -1
  182. package/dist/http/Request.js +2 -2
  183. package/dist/ids.d.ts +42 -14
  184. package/dist/ids.d.ts.map +1 -1
  185. package/dist/ids.js +30 -5
  186. package/dist/index.d.ts +6 -7
  187. package/dist/index.d.ts.map +1 -1
  188. package/dist/index.js +8 -8
  189. package/dist/middleware.d.ts +13 -7
  190. package/dist/middleware.d.ts.map +1 -1
  191. package/dist/middleware.js +14 -8
  192. package/dist/rpc/Invalidation.d.ts +420 -0
  193. package/dist/rpc/Invalidation.d.ts.map +1 -0
  194. package/dist/rpc/Invalidation.js +168 -0
  195. package/dist/rpc/MiddlewareMaker.d.ts +11 -7
  196. package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
  197. package/dist/rpc/MiddlewareMaker.js +59 -38
  198. package/dist/rpc/RpcContextMap.d.ts +3 -3
  199. package/dist/rpc/RpcContextMap.d.ts.map +1 -1
  200. package/dist/rpc/RpcContextMap.js +4 -4
  201. package/dist/rpc/RpcMiddleware.d.ts +14 -10
  202. package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
  203. package/dist/rpc/RpcMiddleware.js +1 -1
  204. package/dist/rpc.d.ts +1 -1
  205. package/dist/rpc.d.ts.map +1 -1
  206. package/dist/rpc.js +2 -2
  207. package/dist/runtime.d.ts +19 -0
  208. package/dist/runtime.d.ts.map +1 -0
  209. package/dist/runtime.js +40 -0
  210. package/dist/setupRequest.d.ts +19 -0
  211. package/dist/setupRequest.d.ts.map +1 -0
  212. package/dist/setupRequest.js +69 -0
  213. package/dist/toast.d.ts +51 -0
  214. package/dist/toast.d.ts.map +1 -0
  215. package/dist/toast.js +34 -0
  216. package/dist/transform.d.ts +1 -1
  217. package/dist/transform.d.ts.map +1 -1
  218. package/dist/transform.js +4 -5
  219. package/dist/utils/effectify.d.ts +1 -1
  220. package/dist/utils/effectify.d.ts.map +1 -1
  221. package/dist/utils/effectify.js +2 -2
  222. package/dist/utils/extend.d.ts.map +1 -1
  223. package/dist/utils/gen.d.ts +4 -4
  224. package/dist/utils/gen.d.ts.map +1 -1
  225. package/dist/utils/logLevel.d.ts +2 -2
  226. package/dist/utils/logLevel.d.ts.map +1 -1
  227. package/dist/utils/logger.d.ts +4 -3
  228. package/dist/utils/logger.d.ts.map +1 -1
  229. package/dist/utils/logger.js +4 -4
  230. package/dist/utils.d.ts +34 -39
  231. package/dist/utils.d.ts.map +1 -1
  232. package/dist/utils.js +33 -37
  233. package/dist/validation/validators.d.ts.map +1 -1
  234. package/dist/validation.d.ts.map +1 -1
  235. package/dist/withToast.d.ts +30 -0
  236. package/dist/withToast.d.ts.map +1 -0
  237. package/dist/withToast.js +64 -0
  238. package/package.json +22 -247
  239. package/src/Array.ts +5 -5
  240. package/src/Chunk.ts +3 -3
  241. package/src/Config/SecretURL.ts +6 -3
  242. package/src/Config/internal/configSecretURL.ts +2 -2
  243. package/src/Config.ts +7 -0
  244. package/src/ConfigProvider.ts +5 -0
  245. package/src/{ServiceMap.ts → Context.ts} +56 -63
  246. package/src/Effect.ts +14 -16
  247. package/src/Emailer.ts +51 -0
  248. package/src/Inputify.type.ts +1 -1
  249. package/src/Layer.ts +11 -7
  250. package/src/Model/Repository/Registry.ts +35 -0
  251. package/src/Model/Repository/ext.ts +375 -0
  252. package/src/Model/Repository/internal/internal.ts +741 -0
  253. package/src/Model/Repository/legacy.ts +29 -0
  254. package/src/Model/Repository/makeRepo.ts +145 -0
  255. package/src/Model/Repository/service.ts +676 -0
  256. package/src/Model/Repository/validation.ts +31 -0
  257. package/src/Model/Repository.ts +6 -0
  258. package/src/Model/dsl.ts +129 -0
  259. package/src/Model/filter/filterApi.ts +60 -0
  260. package/src/Model/filter/types/errors.ts +47 -0
  261. package/src/Model/filter/types/fields.ts +50 -0
  262. package/src/Model/filter/types/path/common.ts +404 -0
  263. package/src/Model/filter/types/path/eager.ts +329 -0
  264. package/src/Model/filter/types/path/index.ts +4 -0
  265. package/src/Model/filter/types/utils.ts +128 -0
  266. package/src/Model/filter/types/validator.ts +46 -0
  267. package/src/Model/filter/types.ts +6 -0
  268. package/src/Model/query/dsl.ts +2694 -0
  269. package/src/Model/query/new-kid-interpreter.ts +484 -0
  270. package/src/Model/query.ts +13 -0
  271. package/src/Model.ts +4 -0
  272. package/src/NonEmptySet.ts +6 -4
  273. package/src/Option.ts +2 -0
  274. package/src/Pure.ts +22 -20
  275. package/src/QueueMaker.ts +19 -0
  276. package/src/RequestContext.ts +95 -0
  277. package/src/Schema/Class.ts +593 -59
  278. package/src/Schema/SchemaParser.ts +12 -0
  279. package/src/Schema/SpecialJsonSchema.ts +139 -0
  280. package/src/Schema/SpecialOpenApi.ts +130 -0
  281. package/src/Schema/brand.ts +22 -2
  282. package/src/Schema/email.ts +9 -4
  283. package/src/Schema/ext.ts +446 -91
  284. package/src/Schema/moreStrings.ts +147 -68
  285. package/src/Schema/numbers.ts +97 -28
  286. package/src/Schema/phoneNumber.ts +9 -5
  287. package/src/Schema/strings.ts +23 -14
  288. package/src/Schema.ts +389 -25
  289. package/src/Set.ts +6 -2
  290. package/src/Store.ts +277 -0
  291. package/src/_ext/Array.ts +4 -2
  292. package/src/_ext/misc.ts +4 -1
  293. package/src/_ext/ord.ext.ts +2 -1
  294. package/src/client/InvalidationKeys.ts +50 -0
  295. package/src/client/apiClientFactory.ts +234 -135
  296. package/src/client/clientFor.ts +105 -34
  297. package/src/client/errors.ts +100 -29
  298. package/src/client/makeClient.ts +594 -73
  299. package/src/client.ts +5 -4
  300. package/src/http/Request.ts +3 -3
  301. package/src/http.ts +1 -1
  302. package/src/ids.ts +33 -6
  303. package/src/index.ts +20 -23
  304. package/src/middleware.ts +13 -9
  305. package/src/rpc/Invalidation.ts +261 -0
  306. package/src/rpc/MiddlewareMaker.ts +88 -80
  307. package/src/rpc/README.md +2 -2
  308. package/src/rpc/RpcContextMap.ts +7 -6
  309. package/src/rpc/RpcMiddleware.ts +19 -13
  310. package/src/rpc.ts +4 -4
  311. package/src/runtime.ts +56 -0
  312. package/src/setupRequest.ts +134 -0
  313. package/src/toast.ts +54 -0
  314. package/src/transform.ts +4 -4
  315. package/src/utils/effectify.ts +1 -1
  316. package/src/utils/gen.ts +8 -8
  317. package/src/utils/logLevel.ts +1 -1
  318. package/src/utils/logger.ts +4 -3
  319. package/src/utils.ts +85 -158
  320. package/src/validation.ts +2 -2
  321. package/src/withToast.ts +133 -0
  322. package/test/dist/rpc.test.d.ts.map +1 -1
  323. package/test/dist/secretURL.test.d.ts.map +1 -0
  324. package/test/dist/special.test.d.ts.map +1 -0
  325. package/test/moreStrings.test.ts +1 -1
  326. package/test/rpc.test.ts +46 -6
  327. package/test/schema.test.ts +459 -30
  328. package/test/secretURL.test.ts +160 -0
  329. package/test/special.test.ts +1258 -0
  330. package/test/utils.test.ts +7 -7
  331. package/tsconfig.base.json +6 -5
  332. package/tsconfig.json +3 -1
  333. package/tsconfig.json.bak +2 -2
  334. package/tsconfig.src.json +29 -29
  335. package/tsconfig.test.json +2 -2
  336. package/dist/Operations.d.ts +0 -123
  337. package/dist/Operations.d.ts.map +0 -1
  338. package/dist/Operations.js +0 -29
  339. package/dist/ServiceMap.d.ts.map +0 -1
  340. package/dist/ServiceMap.js +0 -91
  341. package/eslint.config.mjs +0 -26
  342. package/src/Operations.ts +0 -55
package/src/toast.ts ADDED
@@ -0,0 +1,54 @@
1
+ import * as Context from "./Context.ts"
2
+ import { accessEffectFn } from "./Context.ts"
3
+ import * as Effect from "./Effect.ts"
4
+ import * as Option from "./Option.ts"
5
+
6
+ export type ToastId = string | number
7
+ export type ToastOpts = { id?: ToastId; timeout?: number; groupId?: string; requestId?: string }
8
+ export type ToastOptsInternal = { id?: ToastId | null; timeout?: number; groupId?: string; requestId?: string }
9
+
10
+ export type UseToast = () => {
11
+ error: (this: void, message: string, options?: ToastOpts) => ToastId
12
+ warning: (this: void, message: string, options?: ToastOpts) => ToastId
13
+ success: (this: void, message: string, options?: ToastOpts) => ToastId
14
+ info: (this: void, message: string, options?: ToastOpts) => ToastId
15
+ dismiss: (this: void, id: ToastId) => void
16
+ }
17
+
18
+ export class CurrentToastId extends Context.Opaque<CurrentToastId, { toastId: ToastId }>()("CurrentToastId") {}
19
+
20
+ /** fallback to CurrentToastId when available unless id is explicitly set to a value or null */
21
+ export const wrap = (toast: ReturnType<UseToast>) => {
22
+ const wrap = (toastHandler: (message: string, options?: ToastOpts) => ToastId) => {
23
+ return (message: string, options?: ToastOptsInternal) =>
24
+ Effect.serviceOption(CurrentToastId).pipe(
25
+ Effect.flatMap((currentToast) =>
26
+ Effect.sync(() => {
27
+ const { id: _id, ...rest } = options ?? {}
28
+ const id = _id !== undefined
29
+ ? _id ?? undefined
30
+ : Option.getOrUndefined(Option.map(currentToast, (_) => _.toastId))
31
+ // when id is undefined, we may end up with no toast at all..
32
+ return toastHandler(message, id !== undefined ? { ...rest, id } : rest)
33
+ })
34
+ )
35
+ )
36
+ }
37
+ return {
38
+ error: wrap(toast.error),
39
+ info: wrap(toast.info),
40
+ success: wrap(toast.success),
41
+ warning: wrap(toast.warning),
42
+ dismiss: (toastId: ToastId) => Effect.sync(() => toast.dismiss(toastId))
43
+ }
44
+ }
45
+
46
+ type ToastShape = ReturnType<typeof wrap>
47
+
48
+ export class Toast extends Context.Opaque<Toast, ToastShape>()("Toast") {
49
+ static readonly error = accessEffectFn(this, "error")
50
+ static readonly info = accessEffectFn(this, "info")
51
+ static readonly success = accessEffectFn(this, "success")
52
+ static readonly warning = accessEffectFn(this, "warning")
53
+ static readonly dismiss = accessEffectFn(this, "dismiss")
54
+ }
package/src/transform.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { Option } from "effect"
3
2
  import type { NonEmptyReadonlyArray } from "effect/Array"
3
+ import * as Option from "effect/Option"
4
4
  import type { Misc, Union } from "ts-toolbelt"
5
- import type * as SET from "./Set.js"
5
+ import type * as SET from "./Set.ts"
6
6
 
7
7
  // type SomeObject = {
8
8
  // 0: Option.Option<string>
@@ -51,8 +51,8 @@ const encodeOptsAsNullable_ = (value: any, cacheMap: Map<any, any>): any => {
51
51
 
52
52
  if (
53
53
  value instanceof Date
54
- || value instanceof Function
55
- || value instanceof Promise
54
+ || typeof value === "function"
55
+ || (typeof value === "object" && value !== null && "then" in value && typeof value.then === "function")
56
56
  ) {
57
57
  return value
58
58
  }
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  /* eslint-disable @typescript-eslint/no-unsafe-assignment */
3
3
 
4
- import { Effect } from "effect"
4
+ import * as Effect from "effect/Effect"
5
5
 
6
6
  interface Callback<E, A> {
7
7
  (err: E, a?: A): void
package/src/utils/gen.ts CHANGED
@@ -1,24 +1,24 @@
1
- import { type Effect, type Yieldable } from "effect/Effect"
1
+ import { type Effect } from "effect/Effect"
2
2
 
3
3
  export namespace EffectGenUtils {
4
4
  export type Success<EG> = EG extends Effect<infer A, infer _E, infer _R> ? A
5
5
  // there could be a case where the generator function does not yield anything, so we need to handle that
6
6
  : EG extends (..._: infer _3) => Generator<never, infer A, infer _2> ? A
7
- // v4: generators can yield Yieldable (Effect, Service, etc.), all have asEffect()
8
- : EG extends (..._: infer _3) => Generator<Yieldable<any, infer _, infer _E, infer _R>, infer A, infer _2> ? A
7
+ // generators yield Effect values
8
+ : EG extends (..._: infer _3) => Generator<Effect<any, infer _E, infer _R>, infer A, infer _2> ? A
9
9
  : never
10
10
 
11
11
  export type Error<EG> = EG extends Effect<infer _A, infer E, infer _R> ? E
12
12
  // there could be a case where the generator function does not yield anything, so we need to handle that
13
13
  : EG extends (..._: infer _3) => Generator<never, infer _A, infer _2> ? never
14
- // v4: generators can yield Yieldable (Effect, Service, etc.), all have asEffect()
15
- : EG extends (..._: infer _3) => Generator<Yieldable<any, infer _, infer E, infer _R>, infer _A, infer _2> ? E
14
+ // generators yield Effect values
15
+ : EG extends (..._: infer _3) => Generator<Effect<any, infer E, infer _R>, infer _A, infer _2> ? E
16
16
  : never
17
17
 
18
- export type ServiceMap<EG> = EG extends Effect<infer _A, infer _E, infer R> ? R
18
+ export type Context<EG> = EG extends Effect<infer _A, infer _E, infer R> ? R
19
19
  // there could be a case where the generator function does not yield anything, so we need to handle that
20
20
  : EG extends (..._: infer _3) => Generator<never, infer _A, infer _2> ? never
21
- // v4: generators can yield Yieldable (Effect, Service, etc.), all have asEffect()
22
- : EG extends (..._: infer _3) => Generator<Yieldable<any, infer _, infer _E, infer R>, infer _A, infer _2> ? R
21
+ // generators yield Effect values
22
+ : EG extends (..._: infer _3) => Generator<Effect<any, infer _E, infer R>, infer _A, infer _2> ? R
23
23
  : never
24
24
  }
@@ -1,4 +1,4 @@
1
- import { type LogLevel } from "effect"
1
+ import type * as LogLevel from "effect/LogLevel"
2
2
 
3
3
  export const LogLevelToSentry = (level: LogLevel.LogLevel) => {
4
4
  switch (level) {
@@ -1,12 +1,13 @@
1
1
  /* eslint-disable @typescript-eslint/no-unsafe-argument */
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
 
4
- import { Effect, type LogLevel } from "effect"
5
- import * as ServiceMap from "../ServiceMap.js"
4
+ import * as Effect from "effect/Effect"
5
+ import type * as LogLevel from "effect/LogLevel"
6
+ import * as Context from "../Context.ts"
6
7
 
7
8
  type Levels = "info" | "debug" | "warn" | "error"
8
9
 
9
- export class LogLevels extends ServiceMap.Reference("LogLevels", {
10
+ export class LogLevels extends Context.Reference("LogLevels", {
10
11
  defaultValue: () => new Map<string, Levels>()
11
12
  }) {}
12
13
 
package/src/utils.ts CHANGED
@@ -1,20 +1,24 @@
1
1
  /* eslint-disable @typescript-eslint/no-unsafe-function-type */
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
  /* eslint-disable @typescript-eslint/no-redundant-type-constituents */
4
- import { Cause, Effect, Exit, Fiber, Option, Record } from "effect"
4
+ import * as Cause from "effect/Cause"
5
+ import * as Effect from "effect/Effect"
6
+ import * as Exit from "effect/Exit"
7
+ import * as Fiber from "effect/Fiber"
5
8
  import { dual } from "effect/Function"
6
- import { isFunction, isObject } from "effect/Predicate"
9
+ import * as Option from "effect/Option"
10
+ import { isFunction } from "effect/Predicate"
11
+ import * as Record from "effect/Record"
7
12
  import * as Result from "effect/Result"
8
- import type { GetFieldType, NumericDictionary, PropertyPath } from "lodash"
9
- import { identity, pipe } from "./Function.js"
10
- import type { DeepMutable, Equals, Mutable } from "./Types.js"
13
+ import { identity, pipe } from "./Function.ts"
14
+ import type { DeepMutable, Equals, Mutable } from "./Types.ts"
11
15
 
12
16
  // codegen:start {preset: barrel, include: ./utils/*.ts, nodir: false }
13
- export * from "./utils/effectify.js"
14
- export * from "./utils/extend.js"
15
- export * from "./utils/gen.js"
16
- export * from "./utils/logger.js"
17
- export * from "./utils/logLevel.js"
17
+ export * from "./utils/effectify.ts"
18
+ export * from "./utils/extend.ts"
19
+ export * from "./utils/gen.ts"
20
+ export * from "./utils/logger.ts"
21
+ export * from "./utils/logLevel.ts"
18
22
  // codegen:end
19
23
 
20
24
  export * from "effect/Utils"
@@ -24,121 +28,6 @@ export interface Clone {
24
28
  [cloneTrait](this: this, that: any): this
25
29
  }
26
30
 
27
- function get<TObject extends object, TKey extends keyof TObject>(object: TObject, path: TKey | [TKey]): TObject[TKey]
28
- function get<TObject extends object, TKey extends keyof TObject>(
29
- object: TObject | null | undefined,
30
- path: TKey | [TKey]
31
- ): TObject[TKey] | undefined
32
- function get<TObject extends object, TKey extends keyof TObject, TDefault>(
33
- object: TObject | null | undefined,
34
- path: TKey | [TKey],
35
- defaultValue: TDefault
36
- ): Exclude<TObject[TKey], undefined> | TDefault
37
- function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1]>(
38
- object: TObject,
39
- path: [TKey1, TKey2]
40
- ): TObject[TKey1][TKey2]
41
- function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1]>(
42
- object: TObject | null | undefined,
43
- path: [TKey1, TKey2]
44
- ): TObject[TKey1][TKey2] | undefined
45
- function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1], TDefault>(
46
- object: TObject | null | undefined,
47
- path: [TKey1, TKey2],
48
- defaultValue: TDefault
49
- ): Exclude<TObject[TKey1][TKey2], undefined> | TDefault
50
- function get<
51
- TObject extends object,
52
- TKey1 extends keyof TObject,
53
- TKey2 extends keyof TObject[TKey1],
54
- TKey3 extends keyof TObject[TKey1][TKey2]
55
- >(object: TObject, path: [TKey1, TKey2, TKey3]): TObject[TKey1][TKey2][TKey3]
56
- function get<
57
- TObject extends object,
58
- TKey1 extends keyof TObject,
59
- TKey2 extends keyof TObject[TKey1],
60
- TKey3 extends keyof TObject[TKey1][TKey2]
61
- >(object: TObject | null | undefined, path: [TKey1, TKey2, TKey3]): TObject[TKey1][TKey2][TKey3] | undefined
62
- function get<
63
- TObject extends object,
64
- TKey1 extends keyof TObject,
65
- TKey2 extends keyof TObject[TKey1],
66
- TKey3 extends keyof TObject[TKey1][TKey2],
67
- TDefault
68
- >(
69
- object: TObject | null | undefined,
70
- path: [TKey1, TKey2, TKey3],
71
- defaultValue: TDefault
72
- ): Exclude<TObject[TKey1][TKey2][TKey3], undefined> | TDefault
73
- function get<
74
- TObject extends object,
75
- TKey1 extends keyof TObject,
76
- TKey2 extends keyof TObject[TKey1],
77
- TKey3 extends keyof TObject[TKey1][TKey2],
78
- TKey4 extends keyof TObject[TKey1][TKey2][TKey3]
79
- >(object: TObject, path: [TKey1, TKey2, TKey3, TKey4]): TObject[TKey1][TKey2][TKey3][TKey4]
80
- function get<
81
- TObject extends object,
82
- TKey1 extends keyof TObject,
83
- TKey2 extends keyof TObject[TKey1],
84
- TKey3 extends keyof TObject[TKey1][TKey2],
85
- TKey4 extends keyof TObject[TKey1][TKey2][TKey3]
86
- >(
87
- object: TObject | null | undefined,
88
- path: [TKey1, TKey2, TKey3, TKey4]
89
- ): TObject[TKey1][TKey2][TKey3][TKey4] | undefined
90
- function get<
91
- TObject extends object,
92
- TKey1 extends keyof TObject,
93
- TKey2 extends keyof TObject[TKey1],
94
- TKey3 extends keyof TObject[TKey1][TKey2],
95
- TKey4 extends keyof TObject[TKey1][TKey2][TKey3],
96
- TDefault
97
- >(
98
- object: TObject | null | undefined,
99
- path: [TKey1, TKey2, TKey3, TKey4],
100
- defaultValue: TDefault
101
- ): Exclude<TObject[TKey1][TKey2][TKey3][TKey4], undefined> | TDefault
102
- function get<T>(object: NumericDictionary<T>, path: number): T
103
- function get<T>(object: NumericDictionary<T> | null | undefined, path: number): T | undefined
104
- function get<T, TDefault>(
105
- object: NumericDictionary<T> | null | undefined,
106
- path: number,
107
- defaultValue: TDefault
108
- ): T | TDefault
109
- function get<TDefault>(object: null | undefined, path: PropertyPath, defaultValue: TDefault): TDefault
110
- function get(object: null | undefined, path: PropertyPath): undefined
111
- function get<TObject, TPath extends string>(
112
- data: TObject,
113
- path: TPath
114
- ): string extends TPath ? any : GetFieldType<TObject, TPath>
115
- function get<TObject, TPath extends string, TDefault = GetFieldType<TObject, TPath>>(
116
- data: TObject,
117
- path: TPath,
118
- defaultValue: TDefault
119
- ): Exclude<GetFieldType<TObject, TPath>, null | undefined> | TDefault
120
- function get(object: any, path: PropertyPath, defaultValue?: any): any
121
- function get(obj: any, path: any, defaultValue = undefined) {
122
- // https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore?tab=readme-ov-file#_get
123
- const travel = (regexp: any) =>
124
- String
125
- .prototype
126
- .split
127
- .call(path, regexp)
128
- .filter(Boolean)
129
- .reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj)
130
- const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/)
131
- return result === undefined || result === obj ? defaultValue : result
132
- }
133
-
134
- // codegen:start {preset: barrel, include: ./utils/*.ts }
135
- export * from "./utils/effectify.js"
136
- export * from "./utils/extend.js"
137
- export * from "./utils/gen.js"
138
- export * from "./utils/logger.js"
139
- export * from "./utils/logLevel.js"
140
- // codegen:end
141
-
142
31
  export const unsafeRight = <E, A>(ei: Result.Result<A, E>) => {
143
32
  if (Result.isFailure(ei)) {
144
33
  console.error(ei.failure)
@@ -269,7 +158,7 @@ export type EnforceNonEmptyRecord<R> = keyof R extends never ? never : R
269
158
  export function intersect<AS extends unknown[] & { 0: unknown }>(
270
159
  ...as: AS
271
160
  ): UnionToIntersection<{ [k in keyof AS]: AS[k] }[number]> {
272
- return as.reduce((a: any, b: any) => ({ ...a, ...b })) as any
161
+ return as.reduce((a: any, b: any) => Object.assign(a, b), {}) as any
273
162
  }
274
163
 
275
164
  export type IsEqualTo<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
@@ -280,8 +169,7 @@ export type IsEqualTo<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
280
169
  export const unifyIndex = Symbol()
281
170
  export type unifyIndex = typeof unifyIndex
282
171
 
283
- // @ts-expect-error abc
284
- export interface UnifiableIndexed<X> {}
172
+ export interface UnifiableIndexed<_X> {}
285
173
  export type UnifiableIndexedURI = keyof UnifiableIndexed<any>
286
174
 
287
175
  export interface Unifiable<X> {
@@ -313,9 +201,7 @@ function decorateNew(
313
201
  if (out.descriptor) {
314
202
  out.descriptor = Object.assign({}, out.descriptor)
315
203
  }
316
- const actualDesc: PropertyDescriptor = <any> (
317
- out.descriptor || /* istanbul ignore next */ out
318
- )
204
+ const actualDesc: PropertyDescriptor = out.descriptor || /* istanbul ignore next */ out
319
205
 
320
206
  const originalMethod = validateAndExtractMethodFromDescriptor(actualDesc)
321
207
  const isStatic = inp.placement === "static"
@@ -342,7 +228,7 @@ function decorateNew(
342
228
  function decorateLegacy(
343
229
  target: any,
344
230
  key: PropertyKey,
345
- descriptor: PropertyDescriptor,
231
+ descriptor: PropertyDescriptor | undefined,
346
232
  setProto: boolean,
347
233
  makeNonConfigurable: boolean,
348
234
  // tslint:enable:bool-param-default
@@ -350,11 +236,12 @@ function decorateLegacy(
350
236
  ): PropertyDescriptor {
351
237
  /* istanbul ignore if */
352
238
  if (!descriptor) {
353
- descriptor = <any> Object.getOwnPropertyDescriptor(target, key)
239
+ descriptor = Object.getOwnPropertyDescriptor(target, key)
354
240
  if (!descriptor) {
355
- const e = new Error("@LazyGetter is unable to determine the property descriptor")
356
- ;(<any> e).$target = target
357
- ;(<any> e).$key = key
241
+ const e = Object.assign(new Error("@LazyGetter is unable to determine the property descriptor"), {
242
+ $key: key,
243
+ $target: target
244
+ })
358
245
  throw e
359
246
  }
360
247
  }
@@ -409,7 +296,7 @@ function defaultFilter(): boolean {
409
296
 
410
297
  function validateAndExtractMethodFromDescriptor(desc: PropertyDescriptor): Function {
411
298
  // eslint-disable-next-line @typescript-eslint/unbound-method
412
- const originalMethod = <Function> desc.get
299
+ const originalMethod = desc.get
413
300
 
414
301
  if (!originalMethod) {
415
302
  throw new Error("@LazyGetter can only decorate getters!")
@@ -432,7 +319,7 @@ function getterCommon( // tslint:disable-line:parameters-max-number
432
319
  makeNonConfigurable: boolean,
433
320
  resultSelector: ResultSelectorFn
434
321
  ): any {
435
- const value = originalMethod.apply(thisArg, <any> args)
322
+ const value = originalMethod.apply(thisArg, args)
436
323
 
437
324
  if (resultSelector(value)) {
438
325
  const newDescriptor: PropertyDescriptor = {
@@ -467,23 +354,23 @@ export function LazyGetter(
467
354
  ): MethodDecorator & ResettableDescriptor {
468
355
  let desc: PropertyDescriptor
469
356
  let prop: PropertyKey
470
- let args: IArguments = <any> null
357
+ let args: [targetOrDesc: any, key: PropertyKey | undefined, descriptor: PropertyDescriptor | undefined] | undefined
471
358
  let isLegacy: boolean
472
359
 
473
360
  function decorator(
474
361
  targetOrDesc: any,
475
- key: PropertyKey,
476
- descriptor: PropertyDescriptor
362
+ key: PropertyKey | undefined,
363
+ descriptor: PropertyDescriptor | undefined
477
364
  ): DecoratorReturn {
478
- // eslint-disable-next-line prefer-rest-params
479
- args = arguments
365
+ args = [targetOrDesc, key, descriptor]
480
366
  if (key === undefined) {
481
367
  if (typeof desc === "undefined") {
368
+ const newDescriptor = targetOrDesc as NewDescriptor
482
369
  isLegacy = false
483
- prop = (<NewDescriptor> targetOrDesc).key
370
+ prop = newDescriptor.key
484
371
  desc = Object.assign(
485
372
  {},
486
- (<NewDescriptor> targetOrDesc).descriptor
373
+ newDescriptor.descriptor
487
374
  /* istanbul ignore next */ || targetOrDesc
488
375
  )
489
376
  }
@@ -525,11 +412,14 @@ export function LazyGetter(
525
412
  "Unable to restore descriptor. Did you remember to apply your decorator to a method?"
526
413
  )
527
414
  }
415
+ if (!args) {
416
+ throw new Error("Unable to restore descriptor before applying the decorator.")
417
+ }
528
418
  // Restore descriptor to its original state
529
419
  Object.defineProperty(on, prop, desc)
530
- // eslint-disable-next-line prefer-spread
531
- const ret: any = decorator.apply(null, <any> args)
532
- Object.defineProperty(on, prop, isLegacy ? ret : ret.descriptor || ret)
420
+ const ret = decorator.apply(null, args)
421
+ const restoredDescriptor = isLegacy ? ret : "descriptor" in ret && ret.descriptor ? ret.descriptor : ret
422
+ Object.defineProperty(on, prop, restoredDescriptor)
533
423
  }
534
424
 
535
425
  return decorator
@@ -684,7 +574,7 @@ export type OptPromise<T extends () => any> = (
684
574
  ) => Promise<ReturnType<T>> | ReturnType<T>
685
575
 
686
576
  export function access<T extends string, T2>(t: Record<T, T2>) {
687
- return (key: T) => t[key] as T2
577
+ return (key: T) => t[key]
688
578
  }
689
579
 
690
580
  export function todayAtUTCNoon() {
@@ -736,25 +626,64 @@ export const copy = dual<
736
626
  }
737
627
  >(2, <A>(self: A, f: Partial<A> | ((a: A) => Partial<A>)) => clone(self, { ...self, ...(isFunction(f) ? f(self) : f) }))
738
628
 
739
- type CopyOriginU<U, Ctor extends new(...args: any[]) => any> =
629
+ export type CopyOriginU<U, Ctor extends new(...args: any[]) => any> =
740
630
  & {
741
631
  [K in keyof U & keyof InstanceType<Ctor>]?: U[K]
742
632
  }
743
633
  & {}
744
634
 
745
- type CopyOriginRet<A, U> =
635
+ export type CopyOriginRet<A, U> =
746
636
  & {
747
637
  [K in keyof A | keyof U]: K extends keyof U ? U[K] : A[K & keyof A]
748
638
  }
749
639
  & {}
750
640
 
751
- type CopyOriginSelf<A, U> = Equals<{}, U> extends true
641
+ export type CopyOriginSelf<A, U> = Equals<{}, U> extends true
752
642
  ? Equals<keyof {}, keyof U> extends true ? `updates argument is empty or contains only extra properties`
753
643
  : A
754
644
  : A
755
645
 
646
+ export interface StructuralCopyOrigin<Self extends object> {
647
+ <A extends Self, U extends Partial<Self>>(
648
+ f: (a: A) =>
649
+ & {
650
+ [K in keyof U & keyof Self]?: U[K]
651
+ }
652
+ & {}
653
+ ): (self: CopyOriginSelf<A, U>) => CopyOriginRet<A, U>
654
+ <A extends Self, U extends Partial<Self>>(
655
+ updates:
656
+ & {
657
+ [K in keyof U & keyof Self]?: U[K]
658
+ }
659
+ & {}
660
+ ): (self: CopyOriginSelf<A, U>) => CopyOriginRet<A, U>
661
+ <A extends Self, U extends Partial<Self>>(
662
+ self: CopyOriginSelf<A, U>,
663
+ f: (a: A) =>
664
+ & {
665
+ [K in keyof U & keyof Self]?: U[K]
666
+ }
667
+ & {}
668
+ ): CopyOriginRet<A, U>
669
+ <A extends Self, U extends Partial<Self>>(
670
+ self: CopyOriginSelf<A, U>,
671
+ updates:
672
+ & {
673
+ [K in keyof U & keyof Self]?: U[K]
674
+ }
675
+ & {}
676
+ ): CopyOriginRet<A, U>
677
+ }
678
+
756
679
  // just one input param: the convention is that the ctor takes an object
757
- // containing the properties of the class (I can't put object there as type because of contravariance)
680
+ // containing the properties of the value (I can't put object there as type because of contravariance)
681
+ /**
682
+ * By design this does not return `Self` directly.
683
+ *
684
+ * The return type is computed from `Self` and the update payload so callers can
685
+ * expose an explicit structural return type that remains assignable to `Self`.
686
+ */
758
687
  export const copyOrigin = <Ctor extends new(_: any) => any>(ctor: Ctor) =>
759
688
  dual<
760
689
  {
@@ -883,8 +812,6 @@ export function setMoveElDropUndefined<T>(el: T, newIndex: number) {
883
812
  pipe([...arrInput], arMoveElDropUndefined(el, newIndex), Option.map((ar) => new Set(ar)))
884
813
  }
885
814
 
886
- export { get }
887
-
888
815
  type RemoveNonArray<T> = T extends readonly any[] ? T : never
889
816
  export function isNativeTuple<A>(a: A): a is RemoveNonArray<A> {
890
817
  return Array.isArray(a)
@@ -956,7 +883,7 @@ const genConstructor = (function*() {}).constructor
956
883
  /**
957
884
  * @example
958
885
  * ```ts
959
- * import { Utils } from "effect"
886
+ * import * as Utils from "effect/Utils"
960
887
  *
961
888
  * function* generatorFn() {
962
889
  * yield 1
@@ -970,5 +897,5 @@ const genConstructor = (function*() {}).constructor
970
897
  * @category predicates
971
898
  * @since 3.11.0
972
899
  */
973
- export const isGeneratorFunction = (u: unknown): u is (...args: Array<any>) => Generator<any, any, any> =>
974
- isObject(u) && u.constructor === genConstructor
900
+ export const isGeneratorFunction = (u: unknown): u is (...args: Array<any>) => Generator<any> =>
901
+ isFunction(u) && u.constructor === genConstructor
package/src/validation.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Predicate } from "./Function.js"
1
+ import type { Predicate } from "./Function.ts"
2
2
 
3
3
  export const all_ = <T>(v: T, ...a: Predicate<T>[]) => !a.some((x) => !x(v))
4
4
  export const all = <T>(...a: Predicate<T>[]) => (v: T) => all_(v, ...a)
@@ -15,4 +15,4 @@ export const min = (min: number) => {
15
15
  return (v: { length: number }) => f(v.length)
16
16
  }
17
17
 
18
- export * from "./validation/validators.js"
18
+ export * from "./validation/validators.ts"
@@ -0,0 +1,133 @@
1
+ import * as Cause from "effect/Cause"
2
+ import * as Fiber from "effect/Fiber"
3
+ import * as Context from "./Context.ts"
4
+ import * as Effect from "./Effect.ts"
5
+ import * as Layer from "./Layer.ts"
6
+ import type * as Option from "./Option.ts"
7
+ import * as S from "./Schema.ts"
8
+ import { CurrentToastId, Toast, type ToastId } from "./toast.ts"
9
+ import { wrapEffect } from "./utils.ts"
10
+
11
+ export interface ToastOptions<A, E, Args extends ReadonlyArray<unknown>, WaiR, SucR, ErrR> {
12
+ stableToastId?: undefined | string | ((...args: Args) => string | undefined)
13
+ timeout?: number
14
+ showSpanInfo?: false
15
+ groupId?: string
16
+ onWaiting:
17
+ | string
18
+ | ((...args: Args) => string | null)
19
+ | null
20
+ | ((
21
+ ...args: Args
22
+ ) => Effect.Effect<string | null, never, WaiR>)
23
+ onSuccess:
24
+ | string
25
+ | ((a: A, ...args: Args) => string | null)
26
+ | null
27
+ | ((
28
+ a: A,
29
+ ...args: Args
30
+ ) => Effect.Effect<string | null, never, SucR>)
31
+ onFailure:
32
+ | string
33
+ | ((
34
+ error: Option.Option<E>,
35
+ ...args: Args
36
+ ) => string | { level: "warn" | "error"; message: string })
37
+ | ((
38
+ error: Option.Option<E>,
39
+ ...args: Args
40
+ ) => Effect.Effect<string | { level: "warn" | "error"; message: string }, never, ErrR>)
41
+ }
42
+
43
+ // @effect-diagnostics-next-line missingEffectServiceDependency:off
44
+ export class WithToast extends Context.Service<WithToast>()("WithToast", {
45
+ make: Effect.gen(function*() {
46
+ const toast = yield* Toast
47
+ return <A, E, Args extends readonly unknown[], R, WaiR = never, SucR = never, ErrR = never>(
48
+ options: ToastOptions<A, E, Args, WaiR, SucR, ErrR>
49
+ ) =>
50
+ Effect.fnUntraced(function*(self: Effect.Effect<A, E, R>, ...args: Args) {
51
+ const baseTimeout = options.timeout ?? 3_000
52
+
53
+ const stableToastId = typeof options.stableToastId === "function"
54
+ ? options.stableToastId(...args)
55
+ : options.stableToastId
56
+
57
+ const requestId: string = yield* Effect.currentSpan.pipe(
58
+ Effect.map((span) => span.traceId),
59
+ Effect.orElseSucceed(() => S.StringId.make())
60
+ )
61
+ const groupId = options.groupId
62
+ const meta = { ...(groupId !== undefined ? { groupId } : {}), requestId }
63
+
64
+ const t = yield* wrapEffect(options.onWaiting)(...args)
65
+ const toastId: ToastId | undefined = t === null
66
+ ? stableToastId
67
+ : stableToastId ?? `wait-${Math.random().toString(36).slice(2)}`
68
+
69
+ const waitingFiber = t === null ? undefined : yield* Effect.forkChild(
70
+ Effect.sleep("1 seconds").pipe(
71
+ Effect.andThen(toast.info(t, { id: toastId!, timeout: Infinity, ...meta }))
72
+ )
73
+ )
74
+ const interruptWaiting = waitingFiber ? Fiber.interrupt(waitingFiber) : Effect.void
75
+
76
+ return yield* self.pipe(
77
+ Effect.tap(Effect.fnUntraced(function*(a) {
78
+ yield* interruptWaiting
79
+ const t = yield* wrapEffect(options.onSuccess)(a, ...args)
80
+ if (t === null) {
81
+ return
82
+ }
83
+ yield* toast.success(
84
+ t,
85
+ toastId !== undefined
86
+ ? { id: toastId, timeout: baseTimeout, ...meta }
87
+ : { timeout: baseTimeout, ...meta }
88
+ )
89
+ })),
90
+ Effect.tapCause(Effect.fnUntraced(function*(cause) {
91
+ yield* interruptWaiting
92
+ yield* Effect.logDebug(
93
+ "WithToast - caught error cause: " + Cause.squash(cause),
94
+ Cause.hasInterruptsOnly(cause),
95
+ cause
96
+ )
97
+
98
+ if (Cause.hasInterruptsOnly(cause)) {
99
+ if (toastId) yield* toast.dismiss(toastId)
100
+ return
101
+ }
102
+
103
+ const spanInfo = options.showSpanInfo !== false
104
+ ? yield* Effect.currentSpan.pipe(
105
+ Effect.map((span) => `\nTrace: ${span.traceId}\nSpan: ${span.spanId}`),
106
+ Effect.orElseSucceed(() => "")
107
+ )
108
+ : ""
109
+
110
+ const t = yield* wrapEffect(options.onFailure)(Cause.findErrorOption(cause), ...args)
111
+ const opts = { timeout: baseTimeout * 2, ...meta }
112
+
113
+ if (typeof t === "object") {
114
+ const message = t.message + spanInfo
115
+ return t.level === "warn"
116
+ ? yield* toast.warning(message, toastId !== undefined ? { ...opts, id: toastId } : opts)
117
+ : yield* toast.error(message, toastId !== undefined ? { ...opts, id: toastId } : opts)
118
+ }
119
+ yield* toast.error(t + spanInfo, toastId !== undefined ? { ...opts, id: toastId } : opts)
120
+ }, Effect.uninterruptible)),
121
+ toastId !== undefined ? Effect.provideService(CurrentToastId, CurrentToastId.of({ toastId })) : (_) => _
122
+ )
123
+ })
124
+ })
125
+ }) {
126
+ static readonly DefaultWithoutDependencies = Layer.effect(this, this.make)
127
+ static readonly Default = this.DefaultWithoutDependencies
128
+
129
+ static readonly handle = <A, E, Args extends Array<unknown>, R, WaiR = never, SucR = never, ErrR = never>(
130
+ options: ToastOptions<A, E, Args, WaiR, SucR, ErrR>
131
+ ): (self: Effect.Effect<A, E, R>, ...args: Args) => Effect.Effect<A, E, R | WaiR | SucR | ErrR | WithToast> =>
132
+ (self, ...args) => this.use((_) => _<A, E, Args, R, WaiR, SucR, ErrR>(options)(self, ...args))
133
+ }