@effect-app/infra 4.0.0-beta.20 → 4.0.0-beta.201

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 (305) hide show
  1. package/CHANGELOG.md +1410 -0
  2. package/_check.sh +1 -1
  3. package/dist/CUPS.d.ts +15 -7
  4. package/dist/CUPS.d.ts.map +1 -1
  5. package/dist/CUPS.js +10 -12
  6. package/dist/Emailer/Sendgrid.d.ts +14 -14
  7. package/dist/Emailer/Sendgrid.d.ts.map +1 -1
  8. package/dist/Emailer/Sendgrid.js +16 -15
  9. package/dist/Emailer/fake.d.ts +1 -1
  10. package/dist/Emailer/service.d.ts +10 -4
  11. package/dist/Emailer/service.d.ts.map +1 -1
  12. package/dist/Emailer/service.js +3 -3
  13. package/dist/Emailer.d.ts +1 -1
  14. package/dist/MainFiberSet.d.ts +9 -9
  15. package/dist/MainFiberSet.d.ts.map +1 -1
  16. package/dist/MainFiberSet.js +3 -3
  17. package/dist/Model/Repository/Registry.d.ts +20 -0
  18. package/dist/Model/Repository/Registry.d.ts.map +1 -0
  19. package/dist/Model/Repository/Registry.js +17 -0
  20. package/dist/Model/Repository/ext.d.ts +33 -15
  21. package/dist/Model/Repository/ext.d.ts.map +1 -1
  22. package/dist/Model/Repository/ext.js +54 -2
  23. package/dist/Model/Repository/internal/internal.d.ts +6 -6
  24. package/dist/Model/Repository/internal/internal.d.ts.map +1 -1
  25. package/dist/Model/Repository/internal/internal.js +43 -32
  26. package/dist/Model/Repository/legacy.d.ts +1 -1
  27. package/dist/Model/Repository/makeRepo.d.ts +7 -6
  28. package/dist/Model/Repository/makeRepo.d.ts.map +1 -1
  29. package/dist/Model/Repository/makeRepo.js +5 -1
  30. package/dist/Model/Repository/service.d.ts +28 -23
  31. package/dist/Model/Repository/service.d.ts.map +1 -1
  32. package/dist/Model/Repository/validation.d.ts +142 -17
  33. package/dist/Model/Repository/validation.d.ts.map +1 -1
  34. package/dist/Model/Repository/validation.js +5 -5
  35. package/dist/Model/Repository.d.ts +2 -1
  36. package/dist/Model/Repository.d.ts.map +1 -1
  37. package/dist/Model/Repository.js +2 -1
  38. package/dist/Model/dsl.d.ts +4 -4
  39. package/dist/Model/dsl.d.ts.map +1 -1
  40. package/dist/Model/filter/filterApi.d.ts +5 -5
  41. package/dist/Model/filter/filterApi.d.ts.map +1 -1
  42. package/dist/Model/filter/types/errors.d.ts +1 -1
  43. package/dist/Model/filter/types/fields.d.ts +1 -1
  44. package/dist/Model/filter/types/path/common.d.ts +1 -1
  45. package/dist/Model/filter/types/path/eager.d.ts +1 -1
  46. package/dist/Model/filter/types/path/eager.d.ts.map +1 -1
  47. package/dist/Model/filter/types/path/eager.js +1 -1
  48. package/dist/Model/filter/types/path/index.d.ts +1 -1
  49. package/dist/Model/filter/types/utils.d.ts +1 -1
  50. package/dist/Model/filter/types/validator.d.ts +1 -1
  51. package/dist/Model/filter/types.d.ts +1 -1
  52. package/dist/Model/query/dsl.d.ts +16 -16
  53. package/dist/Model/query/dsl.d.ts.map +1 -1
  54. package/dist/Model/query/new-kid-interpreter.d.ts +6 -6
  55. package/dist/Model/query/new-kid-interpreter.d.ts.map +1 -1
  56. package/dist/Model/query/new-kid-interpreter.js +3 -3
  57. package/dist/Model/query.d.ts +1 -1
  58. package/dist/Model.d.ts +2 -1
  59. package/dist/Model.d.ts.map +1 -1
  60. package/dist/Model.js +2 -1
  61. package/dist/QueueMaker/SQLQueue.d.ts +5 -7
  62. package/dist/QueueMaker/SQLQueue.d.ts.map +1 -1
  63. package/dist/QueueMaker/SQLQueue.js +105 -114
  64. package/dist/QueueMaker/errors.d.ts +2 -2
  65. package/dist/QueueMaker/errors.d.ts.map +1 -1
  66. package/dist/QueueMaker/memQueue.d.ts +7 -4
  67. package/dist/QueueMaker/memQueue.d.ts.map +1 -1
  68. package/dist/QueueMaker/memQueue.js +51 -62
  69. package/dist/QueueMaker/sbqueue.d.ts +6 -3
  70. package/dist/QueueMaker/sbqueue.d.ts.map +1 -1
  71. package/dist/QueueMaker/sbqueue.js +37 -53
  72. package/dist/QueueMaker/service.d.ts +1 -1
  73. package/dist/RequestContext.d.ts +112 -26
  74. package/dist/RequestContext.d.ts.map +1 -1
  75. package/dist/RequestContext.js +7 -8
  76. package/dist/RequestFiberSet.d.ts +7 -7
  77. package/dist/RequestFiberSet.d.ts.map +1 -1
  78. package/dist/RequestFiberSet.js +5 -5
  79. package/dist/Store/ContextMapContainer.d.ts +19 -3
  80. package/dist/Store/ContextMapContainer.d.ts.map +1 -1
  81. package/dist/Store/ContextMapContainer.js +13 -3
  82. package/dist/Store/Cosmos/query.d.ts +1 -1
  83. package/dist/Store/Cosmos/query.d.ts.map +1 -1
  84. package/dist/Store/Cosmos/query.js +10 -12
  85. package/dist/Store/Cosmos.d.ts +1 -1
  86. package/dist/Store/Cosmos.d.ts.map +1 -1
  87. package/dist/Store/Cosmos.js +318 -240
  88. package/dist/Store/Disk.d.ts +2 -2
  89. package/dist/Store/Disk.d.ts.map +1 -1
  90. package/dist/Store/Disk.js +25 -22
  91. package/dist/Store/Memory.d.ts +4 -4
  92. package/dist/Store/Memory.d.ts.map +1 -1
  93. package/dist/Store/Memory.js +27 -22
  94. package/dist/Store/SQL/Pg.d.ts +4 -0
  95. package/dist/Store/SQL/Pg.d.ts.map +1 -0
  96. package/dist/Store/SQL/Pg.js +189 -0
  97. package/dist/Store/SQL/query.d.ts +38 -0
  98. package/dist/Store/SQL/query.d.ts.map +1 -0
  99. package/dist/Store/SQL/query.js +367 -0
  100. package/dist/Store/SQL.d.ts +20 -0
  101. package/dist/Store/SQL.d.ts.map +1 -0
  102. package/dist/Store/SQL.js +381 -0
  103. package/dist/Store/codeFilter.d.ts +1 -1
  104. package/dist/Store/codeFilter.d.ts.map +1 -1
  105. package/dist/Store/codeFilter.js +2 -1
  106. package/dist/Store/index.d.ts +5 -2
  107. package/dist/Store/index.d.ts.map +1 -1
  108. package/dist/Store/index.js +15 -3
  109. package/dist/Store/service.d.ts +18 -7
  110. package/dist/Store/service.d.ts.map +1 -1
  111. package/dist/Store/service.js +24 -6
  112. package/dist/Store/utils.d.ts +1 -1
  113. package/dist/Store/utils.d.ts.map +1 -1
  114. package/dist/Store/utils.js +3 -4
  115. package/dist/Store.d.ts +1 -1
  116. package/dist/adapters/SQL/Model.d.ts +28 -42
  117. package/dist/adapters/SQL/Model.d.ts.map +1 -1
  118. package/dist/adapters/SQL/Model.js +2 -2
  119. package/dist/adapters/SQL.d.ts +1 -1
  120. package/dist/adapters/ServiceBus.d.ts +11 -11
  121. package/dist/adapters/ServiceBus.d.ts.map +1 -1
  122. package/dist/adapters/ServiceBus.js +13 -15
  123. package/dist/adapters/cosmos-client.d.ts +3 -3
  124. package/dist/adapters/cosmos-client.d.ts.map +1 -1
  125. package/dist/adapters/cosmos-client.js +3 -3
  126. package/dist/adapters/index.d.ts +8 -2
  127. package/dist/adapters/index.d.ts.map +1 -1
  128. package/dist/adapters/index.js +8 -2
  129. package/dist/adapters/logger.d.ts +2 -2
  130. package/dist/adapters/logger.d.ts.map +1 -1
  131. package/dist/adapters/memQueue.d.ts +3 -3
  132. package/dist/adapters/memQueue.d.ts.map +1 -1
  133. package/dist/adapters/memQueue.js +3 -3
  134. package/dist/adapters/mongo-client.d.ts +3 -3
  135. package/dist/adapters/mongo-client.d.ts.map +1 -1
  136. package/dist/adapters/mongo-client.js +3 -3
  137. package/dist/adapters/redis-client.d.ts +3 -3
  138. package/dist/adapters/redis-client.d.ts.map +1 -1
  139. package/dist/adapters/redis-client.js +3 -3
  140. package/dist/api/ContextProvider.d.ts +8 -8
  141. package/dist/api/ContextProvider.d.ts.map +1 -1
  142. package/dist/api/ContextProvider.js +6 -6
  143. package/dist/api/codec.d.ts +1 -1
  144. package/dist/api/internal/RequestContextMiddleware.d.ts +2 -2
  145. package/dist/api/internal/RequestContextMiddleware.d.ts.map +1 -1
  146. package/dist/api/internal/RequestContextMiddleware.js +2 -2
  147. package/dist/api/internal/auth.d.ts +44 -6
  148. package/dist/api/internal/auth.d.ts.map +1 -1
  149. package/dist/api/internal/auth.js +160 -29
  150. package/dist/api/internal/events.d.ts +3 -3
  151. package/dist/api/internal/events.d.ts.map +1 -1
  152. package/dist/api/internal/events.js +9 -7
  153. package/dist/api/internal/health.d.ts +1 -1
  154. package/dist/api/layerUtils.d.ts +6 -6
  155. package/dist/api/layerUtils.d.ts.map +1 -1
  156. package/dist/api/layerUtils.js +5 -5
  157. package/dist/api/middlewares.d.ts +1 -1
  158. package/dist/api/reportError.d.ts +1 -1
  159. package/dist/api/routing/middleware/RouterMiddleware.d.ts +4 -4
  160. package/dist/api/routing/middleware/RouterMiddleware.d.ts.map +1 -1
  161. package/dist/api/routing/middleware/middleware.d.ts +39 -3
  162. package/dist/api/routing/middleware/middleware.d.ts.map +1 -1
  163. package/dist/api/routing/middleware/middleware.js +46 -14
  164. package/dist/api/routing/middleware.d.ts +1 -2
  165. package/dist/api/routing/middleware.d.ts.map +1 -1
  166. package/dist/api/routing/middleware.js +1 -2
  167. package/dist/api/routing/schema/jwt.d.ts +1 -1
  168. package/dist/api/routing/schema/jwt.d.ts.map +1 -1
  169. package/dist/api/routing/tsort.d.ts +1 -1
  170. package/dist/api/routing/tsort.d.ts.map +1 -1
  171. package/dist/api/routing/utils.d.ts +3 -3
  172. package/dist/api/routing/utils.d.ts.map +1 -1
  173. package/dist/api/routing.d.ts +25 -26
  174. package/dist/api/routing.d.ts.map +1 -1
  175. package/dist/api/routing.js +99 -35
  176. package/dist/api/setupRequest.d.ts +8 -5
  177. package/dist/api/setupRequest.d.ts.map +1 -1
  178. package/dist/api/setupRequest.js +12 -7
  179. package/dist/api/util.d.ts +1 -1
  180. package/dist/arbs.d.ts +1 -1
  181. package/dist/arbs.d.ts.map +1 -1
  182. package/dist/arbs.js +5 -3
  183. package/dist/errorReporter.d.ts +4 -4
  184. package/dist/errorReporter.d.ts.map +1 -1
  185. package/dist/errorReporter.js +20 -25
  186. package/dist/errors.d.ts +1 -1
  187. package/dist/fileUtil.d.ts +1 -1
  188. package/dist/fileUtil.d.ts.map +1 -1
  189. package/dist/index.d.ts +1 -1
  190. package/dist/logger/jsonLogger.d.ts +1 -1
  191. package/dist/logger/logFmtLogger.d.ts +1 -1
  192. package/dist/logger/shared.d.ts +1 -1
  193. package/dist/logger/shared.js +2 -2
  194. package/dist/logger.d.ts +1 -1
  195. package/dist/logger.d.ts.map +1 -1
  196. package/dist/rateLimit.d.ts +9 -3
  197. package/dist/rateLimit.d.ts.map +1 -1
  198. package/dist/rateLimit.js +5 -11
  199. package/dist/test.d.ts +2 -2
  200. package/dist/test.d.ts.map +1 -1
  201. package/dist/test.js +1 -1
  202. package/dist/vitest.d.ts +1 -1
  203. package/examples/query.ts +39 -35
  204. package/package.json +41 -37
  205. package/src/CUPS.ts +9 -11
  206. package/src/Emailer/Sendgrid.ts +17 -14
  207. package/src/Emailer/service.ts +9 -3
  208. package/src/MainFiberSet.ts +5 -6
  209. package/src/Model/Repository/Registry.ts +33 -0
  210. package/src/Model/Repository/ext.ts +96 -10
  211. package/src/Model/Repository/internal/internal.ts +97 -88
  212. package/src/Model/Repository/makeRepo.ts +12 -10
  213. package/src/Model/Repository/service.ts +31 -22
  214. package/src/Model/Repository/validation.ts +4 -4
  215. package/src/Model/Repository.ts +1 -0
  216. package/src/Model/dsl.ts +3 -3
  217. package/src/Model/filter/types/path/eager.ts +1 -2
  218. package/src/Model/query/dsl.ts +18 -18
  219. package/src/Model/query/new-kid-interpreter.ts +2 -2
  220. package/src/Model.ts +1 -0
  221. package/src/QueueMaker/SQLQueue.ts +121 -151
  222. package/src/QueueMaker/memQueue.ts +82 -103
  223. package/src/QueueMaker/sbqueue.ts +56 -86
  224. package/src/RequestContext.ts +8 -10
  225. package/src/RequestFiberSet.ts +4 -4
  226. package/src/Store/ContextMapContainer.ts +41 -2
  227. package/src/Store/Cosmos/query.ts +16 -20
  228. package/src/Store/Cosmos.ts +452 -342
  229. package/src/Store/Disk.ts +52 -49
  230. package/src/Store/Memory.ts +54 -48
  231. package/src/Store/SQL/Pg.ts +318 -0
  232. package/src/Store/SQL/query.ts +409 -0
  233. package/src/Store/SQL.ts +668 -0
  234. package/src/Store/codeFilter.ts +1 -0
  235. package/src/Store/index.ts +17 -2
  236. package/src/Store/service.ts +32 -8
  237. package/src/Store/utils.ts +23 -22
  238. package/src/adapters/SQL/Model.ts +10 -4
  239. package/src/adapters/ServiceBus.ts +112 -116
  240. package/src/adapters/cosmos-client.ts +2 -2
  241. package/src/adapters/index.ts +7 -0
  242. package/src/adapters/memQueue.ts +2 -2
  243. package/src/adapters/mongo-client.ts +2 -2
  244. package/src/adapters/redis-client.ts +2 -2
  245. package/src/api/ContextProvider.ts +12 -13
  246. package/src/api/internal/RequestContextMiddleware.ts +1 -1
  247. package/src/api/internal/auth.ts +246 -44
  248. package/src/api/internal/events.ts +12 -8
  249. package/src/api/layerUtils.ts +8 -8
  250. package/src/api/routing/middleware/RouterMiddleware.ts +4 -4
  251. package/src/api/routing/middleware/middleware.ts +53 -12
  252. package/src/api/routing/middleware.ts +0 -2
  253. package/src/api/routing.ts +173 -63
  254. package/src/api/setupRequest.ts +28 -8
  255. package/src/arbs.ts +4 -2
  256. package/src/errorReporter.ts +62 -74
  257. package/src/logger/shared.ts +1 -1
  258. package/src/rateLimit.ts +30 -22
  259. package/src/test.ts +1 -1
  260. package/test/auth.test.ts +101 -0
  261. package/test/contextProvider.test.ts +11 -11
  262. package/test/controller.test.ts +19 -17
  263. package/test/dist/auth.test.d.ts.map +1 -0
  264. package/test/dist/contextProvider.test.d.ts.map +1 -1
  265. package/test/dist/controller.test.d.ts.map +1 -1
  266. package/test/dist/date-query.test.d.ts.map +1 -0
  267. package/test/dist/fixtures.d.ts +26 -12
  268. package/test/dist/fixtures.d.ts.map +1 -1
  269. package/test/dist/fixtures.js +12 -10
  270. package/test/dist/query.test.d.ts.map +1 -1
  271. package/test/dist/rawQuery.test.d.ts.map +1 -1
  272. package/test/dist/repository-ext.test.d.ts.map +1 -0
  273. package/test/dist/requires.test.d.ts.map +1 -1
  274. package/test/dist/router-generator.test.d.ts.map +1 -0
  275. package/test/dist/routing-interruptibility.test.d.ts.map +1 -0
  276. package/test/dist/rpc-e2e-invalidation.test.d.ts.map +1 -0
  277. package/test/dist/rpc-multi-middleware.test.d.ts.map +1 -1
  278. package/test/dist/rpc-stream-fullstack.test.d.ts.map +1 -0
  279. package/test/dist/sql-store.test.d.ts.map +1 -0
  280. package/test/fixtures.ts +11 -9
  281. package/test/query.test.ts +216 -34
  282. package/test/rawQuery.test.ts +23 -19
  283. package/test/repository-ext.test.ts +60 -0
  284. package/test/requires.test.ts +6 -6
  285. package/test/router-generator.test.ts +183 -0
  286. package/test/routing-interruptibility.test.ts +63 -0
  287. package/test/rpc-e2e-invalidation.test.ts +249 -0
  288. package/test/rpc-multi-middleware.test.ts +78 -9
  289. package/test/rpc-stream-fullstack.test.ts +325 -0
  290. package/test/sql-store.test.ts +1064 -0
  291. package/test/validateSample.test.ts +15 -12
  292. package/tsconfig.examples.json +1 -1
  293. package/tsconfig.json +0 -1
  294. package/tsconfig.json.bak +2 -2
  295. package/tsconfig.src.json +35 -35
  296. package/tsconfig.test.json +2 -2
  297. package/dist/Operations.d.ts +0 -55
  298. package/dist/Operations.d.ts.map +0 -1
  299. package/dist/Operations.js +0 -102
  300. package/dist/OperationsRepo.d.ts +0 -41
  301. package/dist/OperationsRepo.d.ts.map +0 -1
  302. package/dist/OperationsRepo.js +0 -14
  303. package/eslint.config.mjs +0 -24
  304. package/src/Operations.ts +0 -235
  305. package/src/OperationsRepo.ts +0 -16
@@ -1,8 +1,7 @@
1
1
  import { describe, expect, expectTypeOf, it } from "@effect/vitest"
2
- import { Effect, Layer, Result, S, ServiceMap } from "effect-app"
2
+ import { Context, Effect, Layer, Result, RpcX, S } from "effect-app"
3
3
  import { NotLoggedInError, UnauthorizedError } from "effect-app/client"
4
4
  import { HttpHeaders } from "effect-app/http"
5
- import * as RpcX from "effect-app/rpc"
6
5
  import { MiddlewareMaker } from "effect-app/rpc"
7
6
  import type { unhandled } from "effect-app/Types"
8
7
  import { Rpc } from "effect/unstable/rpc"
@@ -63,11 +62,12 @@ const testSuite = (_mw: typeof middleware3) =>
63
62
  "works",
64
63
  Effect.fn(function*() {
65
64
  const defaultOpts = {
65
+ client: null as any, // TODO?
66
66
  headers: HttpHeaders.fromRecordUnsafe({}),
67
67
  payload: { _tag: "Test" },
68
68
  clientId: 0,
69
69
  requestId: "test-id" as any,
70
- rpc: { ...TestRpc, annotations: ServiceMap.make(_mw.requestContext, {}) }
70
+ rpc: { ...TestRpc, annotations: Context.make(_mw.requestContext, {}) }
71
71
  }
72
72
  const next = Effect.void as unknown as Effect.Effect<SuccessValue, unhandled, never>
73
73
  const layer = _mw.layer.pipe(
@@ -89,7 +89,7 @@ const testSuite = (_mw: typeof middleware3) =>
89
89
  headers: HttpHeaders.fromRecordUnsafe({ "x-user": "test-user", "x-is-manager": "true" }),
90
90
  rpc: {
91
91
  ...defaultOpts.rpc,
92
- annotations: ServiceMap.make(_mw.requestContext, { requireRoles: ["manager"] })
92
+ annotations: Context.make(_mw.requestContext, { requireRoles: ["manager"] })
93
93
  }
94
94
  })
95
95
  )
@@ -127,7 +127,7 @@ const testSuite = (_mw: typeof middleware3) =>
127
127
  Object.assign({ ...defaultOpts }, {
128
128
  rpc: {
129
129
  ...defaultOpts.rpc,
130
- annotations: ServiceMap.make(_mw.requestContext, { requireRoles: ["manager"] })
130
+ annotations: Context.make(_mw.requestContext, { requireRoles: ["manager"] })
131
131
  }
132
132
  })
133
133
  )
@@ -153,7 +153,7 @@ const testSuite = (_mw: typeof middleware3) =>
153
153
  {
154
154
  rpc: {
155
155
  ...defaultOpts.rpc,
156
- annotations: ServiceMap.make(_mw.requestContext, { requireRoles: ["manager"] })
156
+ annotations: Context.make(_mw.requestContext, { requireRoles: ["manager"] })
157
157
  }
158
158
  }
159
159
  )
@@ -0,0 +1,183 @@
1
+ import { type MakeContext, type MakeErrors, makeRouter } from "@effect-app/infra/api/routing"
2
+ import { makeAllDSL, makeOneDSL } from "@effect-app/infra/Model"
3
+ import { expectTypeOf, it } from "@effect/vitest"
4
+ import { Context, Effect, Layer, RpcX, S } from "effect-app"
5
+ import { InvalidStateError, makeRpcClient, UnauthorizedError } from "effect-app/client"
6
+ import { DefaultGenericMiddlewares } from "effect-app/middleware"
7
+ import { type FixEnv } from "effect-app/Pure"
8
+ import { MiddlewareMaker } from "effect-app/rpc"
9
+ import { type TypeTestId } from "effect-app/TypeTest"
10
+ import { type ConfigError } from "effect/Config"
11
+ import { type RpcSerialization } from "effect/unstable/rpc/RpcSerialization"
12
+ import { DefaultGenericMiddlewaresLive, DevModeMiddlewareLive } from "../src/api/routing/middleware.js"
13
+ import { AllowAnonymous, AllowAnonymousLive, RequestContextMap, RequireRoles, RequireRolesLive, Some, SomeElse, SomeService, Test, TestLive } from "./fixtures.js"
14
+
15
+ // Inline minimal context provider (provides `Some`)
16
+ class CtxProvider extends RpcX.RpcMiddleware.Tag<CtxProvider, { provides: Some }>()("CtxProvider") {
17
+ static Default = Layer.make(this, {
18
+ *make() {
19
+ return Effect.fnUntraced(function*(effect) {
20
+ return yield* Effect.provideService(effect, Some, Some.of({ a: 1 }))
21
+ })
22
+ }
23
+ })
24
+ }
25
+
26
+ // Provides `SomeElse` so AllowAnonymous's requirement is met.
27
+ class SomeElseProvider extends RpcX.RpcMiddleware.Tag<SomeElseProvider, { provides: SomeElse }>()("SomeElseProvider") {
28
+ static Default = Layer.make(this, {
29
+ *make() {
30
+ return Effect.fnUntraced(function*(effect) {
31
+ return yield* Effect.provideService(effect, SomeElse, SomeElse.of({ b: 2 }))
32
+ })
33
+ }
34
+ })
35
+ }
36
+
37
+ class mw extends MiddlewareMaker
38
+ .Tag<mw>()("mw", RequestContextMap)
39
+ .middleware(RequireRoles, Test)
40
+ .middleware(AllowAnonymous)
41
+ .middleware(CtxProvider)
42
+ .middleware(...DefaultGenericMiddlewares, SomeElseProvider)
43
+ {
44
+ static Default = this.layer.pipe(
45
+ Layer.provide([
46
+ RequireRolesLive,
47
+ TestLive,
48
+ AllowAnonymousLive,
49
+ CtxProvider.Default,
50
+ SomeElseProvider.Default,
51
+ DefaultGenericMiddlewaresLive,
52
+ DevModeMiddlewareLive,
53
+ SomeService.Default
54
+ ])
55
+ )
56
+ }
57
+
58
+ const { TaggedRequestFor } = makeRpcClient(RequestContextMap)
59
+ const Req = TaggedRequestFor("GenRouter")
60
+
61
+ class GetThing extends Req.Query<GetThing>()("GetThing", { id: S.String }, {
62
+ success: S.String,
63
+ error: UnauthorizedError
64
+ }) {}
65
+ class DoThing extends Req.Command<DoThing>()("DoThing", { id: S.String }, { success: S.Void }) {}
66
+
67
+ const Resource = { GetThing, DoThing }
68
+
69
+ const { Router, matchAll } = makeRouter(mw)
70
+
71
+ class ThingRepo extends Context.Service<ThingRepo>()("ThingRepo", {
72
+ make: Effect.succeed({ get: (id: string) => Effect.succeed(id + "!") })
73
+ }) {
74
+ static Default = Layer.effect(this, this.make)
75
+ }
76
+
77
+ // Case under test:
78
+ // `match({})` is given handlers as **shorthand generator methods** (`*GetThing(req) { ... }`).
79
+ // tsgo (>= 7 dev) infers `TNext = unknown` for these shorthand generators while TS6 infers `never`.
80
+ // `HandlerWithInputGen` in routing.ts must accept both — see the structural fix.
81
+ const router = Router(Resource)({
82
+ dependencies: [ThingRepo.Default],
83
+ *effect(match) {
84
+ const repo = yield* ThingRepo
85
+
86
+ if (Math.random() > 0.5) return yield* new InvalidStateError("nope")
87
+
88
+ return match({
89
+ *GetThing(req) {
90
+ const some = yield* Some
91
+ if (req.id === "boom") {
92
+ return yield* Effect.fail(new UnauthorizedError())
93
+ }
94
+ return yield* repo.get(req.id + String(some.a))
95
+ },
96
+ *DoThing(_req) {
97
+ yield* Effect.succeed(1)
98
+ }
99
+ })
100
+ }
101
+ })
102
+
103
+ // Same scenario but using the `raw:` variant — exercises the `raw` path of `HandlerWithInputGen`.
104
+ const routerRaw = Router({ GetThing })({
105
+ *effect(match) {
106
+ return match({
107
+ GetThing: {
108
+ *raw(req) {
109
+ const some = yield* Some
110
+ return yield* Effect.succeed(req.id + String(some.a))
111
+ }
112
+ }
113
+ })
114
+ }
115
+ })
116
+
117
+ it("router with generator-method handlers compiles", () => {
118
+ expectTypeOf(router).toMatchTypeOf<
119
+ Layer.Layer<never, ConfigError | InvalidStateError, SomeService | RpcSerialization>
120
+ >()
121
+ expectTypeOf(routerRaw).toMatchTypeOf<Layer.Layer<never, ConfigError, SomeService | RpcSerialization>>()
122
+ })
123
+
124
+ // Type-level assertions: verify generator yields propagate to MakeErrors / MakeContext
125
+ type Errors = MakeErrors<typeof router[TypeTestId]>
126
+ type Ctx = MakeContext<typeof router[TypeTestId]>
127
+ expectTypeOf<Errors>().toEqualTypeOf<InvalidStateError>()
128
+ expectTypeOf<Ctx>().toEqualTypeOf<ThingRepo>()
129
+
130
+ const matched = matchAll({ router })
131
+ expectTypeOf(matched).toMatchTypeOf<
132
+ Layer.Layer<never, ConfigError | InvalidStateError, SomeService | RpcSerialization>
133
+ >()
134
+
135
+ // ---------------------------------------------------------------------------
136
+ // DSL R-inference regression
137
+ // ---------------------------------------------------------------------------
138
+ // `OneDSL`/`OneDSLExt.update`/`.modify` previously annotated the callback's
139
+ // effect R as `FixEnv<R, Evt, S1, S2>`. That deadlocked inference of `R`
140
+ // (TS6 → `never`, tsgo → `unknown`), causing yielded effects to leak
141
+ // `unknown` in the R slot when consumed by generator handlers.
142
+ // The fix uses bare `R` in the callback and `FixEnv<R, …>` only on the return.
143
+ class Item extends S.Class<Item>("Item")({ id: S.String, label: S.String }) {}
144
+ class Dep extends Context.Service<Dep>()("Dep", { make: Effect.succeed({ tag: "dep" as const }) }) {}
145
+
146
+ type Evt = { _tag: "Updated"; id: string }
147
+
148
+ const Items$ = makeAllDSL<Item, Evt>()
149
+ const Item$ = makeOneDSL<Item, Evt>()
150
+
151
+ // Callback body uses generator syntax (TNext = unknown under tsgo) and yields
152
+ // a service-dependent effect — R must be inferred as `Dep` (plus the
153
+ // canonical PureEnvEnv contributed by FixEnv on the return).
154
+ const oneUpdate = Item$.update((item) =>
155
+ Effect.gen(function*() {
156
+ const dep = yield* Dep
157
+ return new Item({ id: item.id, label: item.label + dep.tag })
158
+ })
159
+ )
160
+
161
+ const allUpdate = Items$.update((items) =>
162
+ Effect.gen(function*() {
163
+ const dep = yield* Dep
164
+ return items.map((_) => new Item({ id: _.id, label: _.label + dep.tag }))
165
+ })
166
+ )
167
+
168
+ const oneModify = Item$.modify((item, _dsl) =>
169
+ Effect.gen(function*() {
170
+ const dep = yield* Dep
171
+ return { ...item, tag: dep.tag }
172
+ })
173
+ )
174
+
175
+ // `R` should be `FixEnv<Dep, Evt, …>` — never collapsed to `unknown`/`never`.
176
+ // The regression manifested as `unknown` here, breaking `Dep` assignability.
177
+ expectTypeOf(oneUpdate).toMatchTypeOf<Effect.Effect<Item, never, FixEnv<Dep, Evt, Item, Item>>>()
178
+ expectTypeOf(allUpdate).toMatchTypeOf<
179
+ Effect.Effect<readonly Item[], never, FixEnv<Dep, Evt, readonly Item[], readonly Item[]>>
180
+ >()
181
+ expectTypeOf(oneModify).toMatchTypeOf<
182
+ Effect.Effect<{ tag: "dep"; id: string; label: string }, never, FixEnv<Dep, Evt, Item, Item>>
183
+ >()
@@ -0,0 +1,63 @@
1
+ import { describe, expect, it } from "@effect/vitest"
2
+ import { Effect, Fiber, Layer, Ref } from "effect"
3
+ import { S } from "effect-app"
4
+ import { ConfigureInterruptibilityMiddleware } from "effect-app/middleware"
5
+ import { Rpc, RpcGroup, RpcTest } from "effect/unstable/rpc"
6
+ import { applyRequestTypeInterruptibility } from "../src/api/routing.js"
7
+ import { ConfigureInterruptibilityMiddlewareLive, RequestType } from "../src/api/routing/middleware.js"
8
+
9
+ const InterruptibilityRpcs = RpcGroup.make(
10
+ Rpc
11
+ .make("doCommand", { success: S.Void })
12
+ .annotate(RequestType, "command")
13
+ .middleware(ConfigureInterruptibilityMiddleware),
14
+ Rpc
15
+ .make("doQuery", { success: S.Void })
16
+ .annotate(RequestType, "query")
17
+ .middleware(ConfigureInterruptibilityMiddleware)
18
+ )
19
+
20
+ const makeImplLayer = (commandDone: Ref.Ref<boolean>, queryDone: Ref.Ref<boolean>) =>
21
+ InterruptibilityRpcs.toLayer({
22
+ doCommand: () =>
23
+ applyRequestTypeInterruptibility(
24
+ "command",
25
+ Effect.sleep("120 millis").pipe(Effect.andThen(Ref.set(commandDone, true)))
26
+ ),
27
+ doQuery: () =>
28
+ applyRequestTypeInterruptibility(
29
+ "query",
30
+ Effect.sleep("120 millis").pipe(Effect.andThen(Ref.set(queryDone, true)))
31
+ )
32
+ })
33
+
34
+ describe("routing interruptibility", () => {
35
+ it.live(
36
+ "e2e: command continues after client interrupt, query does not",
37
+ () =>
38
+ Effect.gen(function*() {
39
+ const commandDone = yield* Ref.make(false)
40
+ const queryDone = yield* Ref.make(false)
41
+
42
+ const client = yield* RpcTest
43
+ .makeClient(InterruptibilityRpcs)
44
+ .pipe(
45
+ Effect.provide(
46
+ Layer.mergeAll(makeImplLayer(commandDone, queryDone), ConfigureInterruptibilityMiddlewareLive)
47
+ )
48
+ )
49
+
50
+ const commandFiber = yield* Effect.forkDetach(client.doCommand())
51
+ yield* Effect.sleep("20 millis")
52
+ yield* Fiber.interrupt(commandFiber)
53
+ yield* Effect.sleep("180 millis")
54
+ expect(yield* Ref.get(commandDone)).toBe(true)
55
+
56
+ const queryFiber = yield* Effect.forkDetach(client.doQuery())
57
+ yield* Effect.sleep("20 millis")
58
+ yield* Fiber.interrupt(queryFiber)
59
+ yield* Effect.sleep("180 millis")
60
+ expect(yield* Ref.get(queryDone)).toBe(false)
61
+ })
62
+ )
63
+ })
@@ -0,0 +1,249 @@
1
+ /**
2
+ * E2E tests for the invalidation key flow exercised end-to-end via the
3
+ * production wrap/unwrap path:
4
+ *
5
+ * server: routing.ts wraps command success with `CommandResponseWithMetaData`
6
+ * and handler-thrown failure with `CommandFailureWithMetaData`;
7
+ * routing wraps stream values into `{_tag:"value"|"metadata"|"done"}`
8
+ * chunks. `InvalidationSet.use(...)` inside a handler accumulates keys.
9
+ * client: apiClientFactory unwraps both envelopes and forwards keys to
10
+ * `InvalidationKeysFromServer`.
11
+ *
12
+ * Transport is real HTTP (NodeHttpServer on a loopback port) so the wire
13
+ * encoding is exercised too.
14
+ */
15
+ import { NodeHttpServer } from "@effect/platform-node"
16
+ import { expect, it } from "@effect/vitest"
17
+ import { Effect, Exit, Layer, Option, Ref, Stream } from "effect"
18
+ import { S } from "effect-app"
19
+ import { ApiClientFactory, InvalidationKeysFromServer, makeInvalidationKeysService, makeRpcClient } from "effect-app/client"
20
+ import { HttpRouter, HttpServer } from "effect-app/http"
21
+ import { DefaultGenericMiddlewares } from "effect-app/middleware"
22
+ import { Invalidation, MiddlewareMaker } from "effect-app/rpc"
23
+ import { TaggedErrorClass } from "effect-app/Schema"
24
+ import { FetchHttpClient } from "effect/unstable/http"
25
+ import { RpcSerialization } from "effect/unstable/rpc"
26
+ import { createServer } from "http"
27
+ import { makeRouter } from "../src/api/routing.js"
28
+ import { DefaultGenericMiddlewaresLive } from "../src/api/routing/middleware.js"
29
+ import { AllowAnonymous, AllowAnonymousLive, RequestContextMap, RequireRoles, RequireRolesLive, SomeElseMiddleware, SomeElseMiddlewareLive, SomeService, Test, TestLive } from "./fixtures.js"
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // Middleware (mirrors AppMiddleware shape — same composite as other e2e tests).
33
+ // ---------------------------------------------------------------------------
34
+
35
+ class AppMiddleware extends MiddlewareMaker
36
+ .Tag<AppMiddleware>()("AppMiddleware", RequestContextMap)
37
+ .middleware(RequireRoles, Test)
38
+ .middleware(AllowAnonymous)
39
+ .middleware(SomeElseMiddleware)
40
+ .middleware(...DefaultGenericMiddlewares)
41
+ {
42
+ static Default = this.layer.pipe(
43
+ Layer.provide([
44
+ RequireRolesLive.pipe(Layer.provide(SomeService.Default)),
45
+ AllowAnonymousLive,
46
+ TestLive,
47
+ SomeElseMiddlewareLive,
48
+ DefaultGenericMiddlewaresLive
49
+ ])
50
+ )
51
+ }
52
+
53
+ const { Router, matchAll } = makeRouter(AppMiddleware)
54
+
55
+ // ---------------------------------------------------------------------------
56
+ // Resources
57
+ // ---------------------------------------------------------------------------
58
+
59
+ const DynamicKey: Invalidation.InvalidationKey = ["dynamic", "key"]
60
+ const ExtraKey: Invalidation.InvalidationKey = ["extra", "key"]
61
+ const StreamKey: Invalidation.InvalidationKey = ["stream", "key"]
62
+
63
+ const { TaggedRequestFor } = makeRpcClient(RequestContextMap)
64
+ const Req = TaggedRequestFor("Inv")
65
+
66
+ class CmdBoom extends TaggedErrorClass<CmdBoom>()("CmdBoom", { reason: S.String }) {}
67
+
68
+ class DoNothing extends Req.Command<DoNothing>()("DoNothing", {}, {
69
+ allowAnonymous: true,
70
+ success: S.Void
71
+ }) {}
72
+
73
+ class DoWithDynamicKey extends Req.Command<DoWithDynamicKey>()("DoWithDynamicKey", {}, {
74
+ allowAnonymous: true,
75
+ success: S.String
76
+ }) {}
77
+
78
+ class DoWithBothKeys extends Req.Command<DoWithBothKeys>()("DoWithBothKeys", {}, {
79
+ allowAnonymous: true,
80
+ success: S.Number
81
+ }) {}
82
+
83
+ class DoAndFail extends Req.Command<DoAndFail>()("DoAndFail", {}, {
84
+ allowAnonymous: true,
85
+ success: S.Void,
86
+ error: CmdBoom
87
+ }) {}
88
+
89
+ class StreamWithKey extends Req.Command<StreamWithKey>()("StreamWithKey", {}, {
90
+ stream: true,
91
+ allowAnonymous: true,
92
+ success: S.Number
93
+ }) {}
94
+
95
+ const InvRsc = { DoNothing, DoWithDynamicKey, DoWithBothKeys, DoAndFail, StreamWithKey }
96
+
97
+ // ---------------------------------------------------------------------------
98
+ // Controllers / router
99
+ // ---------------------------------------------------------------------------
100
+
101
+ const router = Router(InvRsc)({
102
+ *effect(match) {
103
+ return match({
104
+ DoNothing: () => Effect.void,
105
+ DoWithDynamicKey: Effect.fnUntraced(function*() {
106
+ yield* Invalidation.InvalidationSet.use((_) => _.add(DynamicKey))
107
+ return "done"
108
+ }),
109
+ DoWithBothKeys: Effect.fnUntraced(function*() {
110
+ yield* Invalidation.InvalidationSet.use((_) => _.add(DynamicKey))
111
+ yield* Invalidation.InvalidationSet.use((_) => _.add(ExtraKey))
112
+ return 99
113
+ }),
114
+ DoAndFail: Effect.fnUntraced(function*() {
115
+ yield* Invalidation.InvalidationSet.use((_) => _.add(DynamicKey))
116
+ return yield* Effect.fail(new CmdBoom({ reason: "intentional failure" }))
117
+ }),
118
+ StreamWithKey: () =>
119
+ Stream.fromIterable([1, 2, 3]).pipe(
120
+ Stream.tap(() => Invalidation.InvalidationSet.use((_) => _.add(StreamKey)))
121
+ )
122
+ })
123
+ }
124
+ })
125
+
126
+ const RpcRouterLayer = matchAll({ router })
127
+
128
+ // ---------------------------------------------------------------------------
129
+ // HTTP wiring — fresh server on loopback per `it.live`.
130
+ // ---------------------------------------------------------------------------
131
+
132
+ const NodeServerLayer = NodeHttpServer.layer(() => createServer(), { port: 0 })
133
+
134
+ const ServerLayer = HttpRouter
135
+ .serve(RpcRouterLayer)
136
+ .pipe(
137
+ Layer.provide(NodeServerLayer),
138
+ Layer.provide(RpcSerialization.layerNdjson)
139
+ )
140
+
141
+ const ClientLayer = Layer
142
+ .unwrap(
143
+ Effect.gen(function*() {
144
+ const server = yield* HttpServer.HttpServer
145
+ const addr = server.address
146
+ if (addr._tag !== "TcpAddress") return yield* Effect.die(new Error("expected TcpAddress"))
147
+ const host = addr.hostname === "0.0.0.0" ? "127.0.0.1" : addr.hostname
148
+ const url = `http://${host}:${addr.port}`
149
+ return ApiClientFactory
150
+ .layer({ url, headers: Option.none() })
151
+ .pipe(Layer.provide(FetchHttpClient.layer))
152
+ })
153
+ )
154
+ .pipe(Layer.provide(NodeServerLayer))
155
+
156
+ const TestLayer = Layer.mergeAll(ServerLayer, ClientLayer)
157
+
158
+ // Helper: provide a fresh `InvalidationKeysFromServer` and capture forwarded keys.
159
+ const withCapture = <A, E, R>(eff: Effect.Effect<A, E, R>) =>
160
+ Effect.gen(function*() {
161
+ const ref = yield* Ref.make<ReadonlyArray<Invalidation.InvalidationKey>>([])
162
+ const svc = makeInvalidationKeysService(ref)
163
+ const result = yield* eff.pipe(Effect.provideService(InvalidationKeysFromServer, svc), Effect.exit)
164
+ return { result, keys: yield* Ref.get(ref) }
165
+ })
166
+
167
+ // ---------------------------------------------------------------------------
168
+ // Tests
169
+ // ---------------------------------------------------------------------------
170
+
171
+ it.live(
172
+ "command with no invalidation keys: caller sees raw payload, no keys forwarded",
173
+ Effect.fnUntraced(function*() {
174
+ const client = yield* ApiClientFactory.makeFor(Layer.empty, { middleware: AppMiddleware })(InvRsc)
175
+ const { result, keys } = yield* withCapture(client.DoNothing.handler())
176
+ expect(Exit.isSuccess(result)).toBe(true)
177
+ expect(keys).toStrictEqual([])
178
+ }, Effect.provide(TestLayer)),
179
+ { timeout: 10_000 }
180
+ )
181
+
182
+ it.live(
183
+ "command with dynamic InvalidationSet.use: payload + key forwarded",
184
+ Effect.fnUntraced(function*() {
185
+ const client = yield* ApiClientFactory.makeFor(Layer.empty, { middleware: AppMiddleware })(InvRsc)
186
+ const { result, keys } = yield* withCapture(client.DoWithDynamicKey.handler())
187
+ expect(Exit.isSuccess(result) && result.value).toBe("done")
188
+ expect(keys).toStrictEqual([DynamicKey])
189
+ }, Effect.provide(TestLayer)),
190
+ { timeout: 10_000 }
191
+ )
192
+
193
+ it.live(
194
+ "command accumulating multiple dynamic keys: all keys forwarded in order",
195
+ Effect.fnUntraced(function*() {
196
+ const client = yield* ApiClientFactory.makeFor(Layer.empty, { middleware: AppMiddleware })(InvRsc)
197
+ const { result, keys } = yield* withCapture(client.DoWithBothKeys.handler())
198
+ expect(Exit.isSuccess(result) && result.value).toBe(99)
199
+ expect(keys).toStrictEqual([DynamicKey, ExtraKey])
200
+ }, Effect.provide(TestLayer)),
201
+ { timeout: 10_000 }
202
+ )
203
+
204
+ it.live(
205
+ "per-request isolation: each command call starts with a fresh InvalidationSet",
206
+ Effect.fnUntraced(function*() {
207
+ const client = yield* ApiClientFactory.makeFor(Layer.empty, { middleware: AppMiddleware })(InvRsc)
208
+ const r1 = yield* withCapture(client.DoWithDynamicKey.handler())
209
+ const r2 = yield* withCapture(client.DoWithDynamicKey.handler())
210
+ // Each call must have exactly one key — no accumulation across calls
211
+ expect(r1.keys).toStrictEqual([DynamicKey])
212
+ expect(r2.keys).toStrictEqual([DynamicKey])
213
+ }, Effect.provide(TestLayer)),
214
+ { timeout: 10_000 }
215
+ )
216
+
217
+ it.live(
218
+ "command failure (V2): keys accumulated before fail still reach the client; original error re-thrown",
219
+ Effect.fnUntraced(function*() {
220
+ const client = yield* ApiClientFactory.makeFor(Layer.empty, { middleware: AppMiddleware })(InvRsc)
221
+ const { result, keys } = yield* withCapture(client.DoAndFail.handler())
222
+ expect(Exit.isFailure(result)).toBe(true)
223
+ if (Exit.isFailure(result)) {
224
+ const failures = (result.cause as any).reasons as ReadonlyArray<{ _tag: "Fail"; error: any }>
225
+ expect(failures[0]?.error?._tag).toBe("CmdBoom")
226
+ expect(failures[0]?.error?.reason).toBe("intentional failure")
227
+ }
228
+ expect(keys).toStrictEqual([DynamicKey])
229
+ }, Effect.provide(TestLayer)),
230
+ { timeout: 10_000 }
231
+ )
232
+
233
+ it.live(
234
+ "stream: per-chunk metadata drains keys mid-stream",
235
+ Effect.fnUntraced(function*() {
236
+ const client = yield* ApiClientFactory.makeFor(Layer.empty, { middleware: AppMiddleware })(InvRsc)
237
+ const ref = yield* Ref.make<ReadonlyArray<Invalidation.InvalidationKey>>([])
238
+ const svc = makeInvalidationKeysService(ref)
239
+ const values = yield* Stream.runCollect(client.StreamWithKey.handler()).pipe(
240
+ Effect.provideService(InvalidationKeysFromServer, svc)
241
+ )
242
+ const keys = yield* Ref.get(ref)
243
+ expect(values).toStrictEqual([1, 2, 3])
244
+ // Handler taps `InvalidationSet.use` once per emitted value; routing's V3 mid-stream
245
+ // metadata drain forwards each batch as it arrives.
246
+ expect(keys).toStrictEqual([StreamKey, StreamKey, StreamKey])
247
+ }, Effect.provide(TestLayer)),
248
+ { timeout: 10_000 }
249
+ )
@@ -1,19 +1,18 @@
1
1
  import { NodeHttpServer } from "@effect/platform-node"
2
2
  import { expect, expectTypeOf, it } from "@effect/vitest"
3
- import { Console, Effect, Layer, Result } from "effect"
4
- import { S } from "effect-app"
3
+ import { Console, Effect, Layer, Ref, Result } from "effect"
4
+ import { Context, RpcX, S } from "effect-app"
5
5
  import { NotLoggedInError } from "effect-app/client"
6
6
  import { HttpRouter } from "effect-app/http"
7
7
  import { DefaultGenericMiddlewares } from "effect-app/middleware"
8
- import { MiddlewareMaker } from "effect-app/rpc"
9
- import { middlewareGroup } from "effect-app/rpc/MiddlewareMaker"
10
8
  import { FetchHttpClient } from "effect/unstable/http"
11
- import { RpcClient, RpcGroup, RpcSerialization, RpcServer, RpcTest } from "effect/unstable/rpc"
9
+ import { Rpc, RpcClient, RpcGroup, RpcSerialization, RpcServer, RpcTest } from "effect/unstable/rpc"
12
10
  import { createServer } from "http"
13
11
  import { DefaultGenericMiddlewaresLive } from "../src/api/routing.js"
14
12
  import { AllowAnonymous, AllowAnonymousLive, RequestContextMap, RequireRoles, RequireRolesLive, Some, SomeElseMiddleware, SomeElseMiddlewareLive, SomeMiddleware, SomeMiddlewareLive, SomeService, Test, TestLive, UserProfile } from "./fixtures.js"
15
13
 
16
- const incomplete = MiddlewareMaker
14
+ const incomplete = RpcX
15
+ .MiddlewareMaker
17
16
  .Tag<middleware>()("MiddlewareMaker", RequestContextMap)
18
17
  .middleware(RequireRoles)
19
18
  .middleware(AllowAnonymous, Test)
@@ -21,7 +20,8 @@ const incomplete = MiddlewareMaker
21
20
  // this extension is allowed otherwise the error is quite obscure
22
21
  export class incompleteMiddleware extends incomplete {}
23
22
 
24
- class middleware extends MiddlewareMaker
23
+ class middleware extends RpcX
24
+ .MiddlewareMaker
25
25
  .Tag<middleware>()("MiddlewareMaker", RequestContextMap)
26
26
  .middleware(RequireRoles)
27
27
  .middleware(AllowAnonymous, Test)
@@ -29,7 +29,7 @@ class middleware extends MiddlewareMaker
29
29
  .middleware(...DefaultGenericMiddlewares)
30
30
  {}
31
31
 
32
- const UserRpcs = middlewareGroup(middleware)(
32
+ const UserRpcs = RpcX.MiddlewareMaker.middlewareGroup(middleware)(
33
33
  RpcGroup.make(
34
34
  middleware.rpc("getUser", {
35
35
  success: S.Literal("awesome")
@@ -56,7 +56,7 @@ const impl = UserRpcs
56
56
 
57
57
  expectTypeOf<Layer.Services<typeof impl>>().toEqualTypeOf<never>()
58
58
 
59
- const UserRpcsBad = middlewareGroup(middleware)(
59
+ const UserRpcsBad = RpcX.MiddlewareMaker.middlewareGroup(middleware)(
60
60
  RpcGroup.make(
61
61
  middleware.rpc("doSomethingElse", {
62
62
  success: S.Literal("also-awesome2"),
@@ -136,3 +136,72 @@ it.live(
136
136
  Effect.provide(RpcTestLayer)
137
137
  )
138
138
  )
139
+
140
+ // Per-request service isolation test
141
+
142
+ class PerRequestCounter extends Context.Service<PerRequestCounter>()(
143
+ "PerRequestCounter",
144
+ { make: Effect.sync(() => ({ a: 0 })) }
145
+ ) {
146
+ static Default = Layer.effect(this, this.make)
147
+ }
148
+
149
+ class GlobalCounter extends Context.Service<GlobalCounter, {
150
+ readonly ref: Ref.Ref<number>
151
+ }>()("GlobalCounter") {}
152
+
153
+ const CounterRpcs = RpcGroup.make(
154
+ Rpc.make("incrementA", {
155
+ success: S.Number
156
+ }),
157
+ Rpc.make("incrementB", {
158
+ success: S.Number
159
+ })
160
+ )
161
+
162
+ const counterImpl = CounterRpcs
163
+ .toLayer({
164
+ incrementA: Effect.fn(function*() {
165
+ const counter = yield* PerRequestCounter
166
+ counter.a++
167
+ const global = yield* GlobalCounter
168
+ yield* Ref.update(global.ref, (n) => n + 1)
169
+ return counter.a
170
+ }, Effect.provide(PerRequestCounter.Default)),
171
+ incrementB: Effect.fn(function*() {
172
+ const counter = yield* PerRequestCounter
173
+ counter.a++
174
+ const global = yield* GlobalCounter
175
+ yield* Ref.update(global.ref, (n) => n + 1)
176
+ return counter.a
177
+ }, Effect.provide(PerRequestCounter.Default))
178
+ })
179
+
180
+ const GlobalCounterLive = Layer.effect(
181
+ GlobalCounter,
182
+ Ref.make(0).pipe(Effect.map((ref) => ({ ref })))
183
+ )
184
+
185
+ const CounterTestLayer = counterImpl.pipe(Layer.provideMerge(GlobalCounterLive))
186
+
187
+ it.live(
188
+ "per-request service isolation with shared global counter",
189
+ Effect.fnUntraced(
190
+ function*() {
191
+ const client = yield* RpcTest.makeClient(CounterRpcs)
192
+ const global = yield* GlobalCounter
193
+
194
+ const r1 = yield* client.incrementA()
195
+ const r2 = yield* client.incrementB()
196
+
197
+ // per-request counter is fresh each time → both return 1
198
+ expect(r1).toBe(1)
199
+ expect(r2).toBe(1)
200
+
201
+ // global counter is shared across requests → accumulates to 2
202
+ const globalCount = yield* Ref.get(global.ref)
203
+ expect(globalCount).toBe(2)
204
+ },
205
+ Effect.provide(CounterTestLayer)
206
+ )
207
+ )