@effect-app/infra 4.0.0-beta.22 → 4.0.0-beta.221

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 (337) hide show
  1. package/CHANGELOG.md +1648 -0
  2. package/_check.sh +1 -1
  3. package/dist/CUPS.d.ts +12 -7
  4. package/dist/CUPS.d.ts.map +1 -1
  5. package/dist/CUPS.js +16 -12
  6. package/dist/Emailer/Sendgrid.d.ts +15 -15
  7. package/dist/Emailer/Sendgrid.d.ts.map +1 -1
  8. package/dist/Emailer/Sendgrid.js +20 -16
  9. package/dist/Emailer/fake.d.ts +1 -1
  10. package/dist/Emailer/fake.js +2 -2
  11. package/dist/Emailer/service.d.ts +13 -4
  12. package/dist/Emailer/service.d.ts.map +1 -1
  13. package/dist/Emailer/service.js +4 -3
  14. package/dist/Emailer.d.ts +1 -1
  15. package/dist/MainFiberSet.d.ts +12 -9
  16. package/dist/MainFiberSet.d.ts.map +1 -1
  17. package/dist/MainFiberSet.js +7 -3
  18. package/dist/Model/Repository/Registry.d.ts +21 -0
  19. package/dist/Model/Repository/Registry.d.ts.map +1 -0
  20. package/dist/Model/Repository/Registry.js +18 -0
  21. package/dist/Model/Repository/ext.d.ts +35 -16
  22. package/dist/Model/Repository/ext.d.ts.map +1 -1
  23. package/dist/Model/Repository/ext.js +60 -3
  24. package/dist/Model/Repository/internal/internal.d.ts +9 -6
  25. package/dist/Model/Repository/internal/internal.d.ts.map +1 -1
  26. package/dist/Model/Repository/internal/internal.js +115 -51
  27. package/dist/Model/Repository/legacy.d.ts +4 -2
  28. package/dist/Model/Repository/legacy.d.ts.map +1 -1
  29. package/dist/Model/Repository/makeRepo.d.ts +10 -6
  30. package/dist/Model/Repository/makeRepo.d.ts.map +1 -1
  31. package/dist/Model/Repository/makeRepo.js +5 -2
  32. package/dist/Model/Repository/service.d.ts +32 -24
  33. package/dist/Model/Repository/service.d.ts.map +1 -1
  34. package/dist/Model/Repository/validation.d.ts +47 -18
  35. package/dist/Model/Repository/validation.d.ts.map +1 -1
  36. package/dist/Model/Repository/validation.js +6 -6
  37. package/dist/Model/Repository.d.ts +2 -1
  38. package/dist/Model/Repository.d.ts.map +1 -1
  39. package/dist/Model/Repository.js +2 -1
  40. package/dist/Model/dsl.d.ts +6 -5
  41. package/dist/Model/dsl.d.ts.map +1 -1
  42. package/dist/Model/dsl.js +2 -3
  43. package/dist/Model/filter/filterApi.d.ts +5 -5
  44. package/dist/Model/filter/filterApi.d.ts.map +1 -1
  45. package/dist/Model/filter/types/errors.d.ts +1 -1
  46. package/dist/Model/filter/types/fields.d.ts +1 -1
  47. package/dist/Model/filter/types/path/common.d.ts +1 -1
  48. package/dist/Model/filter/types/path/eager.d.ts +1 -1
  49. package/dist/Model/filter/types/path/eager.d.ts.map +1 -1
  50. package/dist/Model/filter/types/path/eager.js +1 -1
  51. package/dist/Model/filter/types/path/index.d.ts +1 -1
  52. package/dist/Model/filter/types/utils.d.ts +1 -1
  53. package/dist/Model/filter/types/validator.d.ts +1 -1
  54. package/dist/Model/filter/types.d.ts +1 -1
  55. package/dist/Model/query/dsl.d.ts +142 -17
  56. package/dist/Model/query/dsl.d.ts.map +1 -1
  57. package/dist/Model/query/dsl.js +190 -5
  58. package/dist/Model/query/new-kid-interpreter.d.ts +77 -8
  59. package/dist/Model/query/new-kid-interpreter.d.ts.map +1 -1
  60. package/dist/Model/query/new-kid-interpreter.js +127 -6
  61. package/dist/Model/query.d.ts +1 -1
  62. package/dist/Model.d.ts +2 -1
  63. package/dist/Model.d.ts.map +1 -1
  64. package/dist/Model.js +2 -1
  65. package/dist/QueueMaker/SQLQueue.d.ts +7 -8
  66. package/dist/QueueMaker/SQLQueue.d.ts.map +1 -1
  67. package/dist/QueueMaker/SQLQueue.js +135 -117
  68. package/dist/QueueMaker/errors.d.ts +5 -3
  69. package/dist/QueueMaker/errors.d.ts.map +1 -1
  70. package/dist/QueueMaker/errors.js +4 -2
  71. package/dist/QueueMaker/memQueue.d.ts +9 -5
  72. package/dist/QueueMaker/memQueue.d.ts.map +1 -1
  73. package/dist/QueueMaker/memQueue.js +81 -65
  74. package/dist/QueueMaker/sbqueue.d.ts +8 -4
  75. package/dist/QueueMaker/sbqueue.d.ts.map +1 -1
  76. package/dist/QueueMaker/sbqueue.js +57 -55
  77. package/dist/QueueMaker/service.d.ts +4 -2
  78. package/dist/QueueMaker/service.d.ts.map +1 -1
  79. package/dist/QueueMaker/service.js +1 -1
  80. package/dist/RequestContext.d.ts +75 -35
  81. package/dist/RequestContext.d.ts.map +1 -1
  82. package/dist/RequestContext.js +14 -14
  83. package/dist/RequestFiberSet.d.ts +10 -7
  84. package/dist/RequestFiberSet.d.ts.map +1 -1
  85. package/dist/RequestFiberSet.js +8 -3
  86. package/dist/Store/ContextMapContainer.d.ts +22 -3
  87. package/dist/Store/ContextMapContainer.d.ts.map +1 -1
  88. package/dist/Store/ContextMapContainer.js +17 -3
  89. package/dist/Store/Cosmos/query.d.ts +7 -2
  90. package/dist/Store/Cosmos/query.d.ts.map +1 -1
  91. package/dist/Store/Cosmos/query.js +115 -35
  92. package/dist/Store/Cosmos.d.ts +2 -2
  93. package/dist/Store/Cosmos.d.ts.map +1 -1
  94. package/dist/Store/Cosmos.js +343 -244
  95. package/dist/Store/Disk.d.ts +3 -3
  96. package/dist/Store/Disk.d.ts.map +1 -1
  97. package/dist/Store/Disk.js +76 -36
  98. package/dist/Store/Memory.d.ts +7 -4
  99. package/dist/Store/Memory.d.ts.map +1 -1
  100. package/dist/Store/Memory.js +251 -58
  101. package/dist/Store/SQL/Pg.d.ts +4 -0
  102. package/dist/Store/SQL/Pg.d.ts.map +1 -0
  103. package/dist/Store/SQL/Pg.js +233 -0
  104. package/dist/Store/SQL/query.d.ts +43 -0
  105. package/dist/Store/SQL/query.d.ts.map +1 -0
  106. package/dist/Store/SQL/query.js +478 -0
  107. package/dist/Store/SQL.d.ts +21 -0
  108. package/dist/Store/SQL.d.ts.map +1 -0
  109. package/dist/Store/SQL.js +450 -0
  110. package/dist/Store/codeFilter.d.ts +2 -2
  111. package/dist/Store/codeFilter.d.ts.map +1 -1
  112. package/dist/Store/codeFilter.js +6 -3
  113. package/dist/Store/index.d.ts +6 -3
  114. package/dist/Store/index.d.ts.map +1 -1
  115. package/dist/Store/index.js +18 -4
  116. package/dist/Store/service.d.ts +26 -8
  117. package/dist/Store/service.d.ts.map +1 -1
  118. package/dist/Store/service.js +25 -6
  119. package/dist/Store/utils.d.ts +3 -2
  120. package/dist/Store/utils.d.ts.map +1 -1
  121. package/dist/Store/utils.js +5 -5
  122. package/dist/Store.d.ts +1 -1
  123. package/dist/adapters/SQL/Model.d.ts +32 -43
  124. package/dist/adapters/SQL/Model.d.ts.map +1 -1
  125. package/dist/adapters/SQL/Model.js +30 -39
  126. package/dist/adapters/SQL.d.ts +1 -1
  127. package/dist/adapters/ServiceBus.d.ts +14 -11
  128. package/dist/adapters/ServiceBus.d.ts.map +1 -1
  129. package/dist/adapters/ServiceBus.js +30 -21
  130. package/dist/adapters/cosmos-client.d.ts +5 -3
  131. package/dist/adapters/cosmos-client.d.ts.map +1 -1
  132. package/dist/adapters/cosmos-client.js +5 -3
  133. package/dist/adapters/index.d.ts +8 -2
  134. package/dist/adapters/index.d.ts.map +1 -1
  135. package/dist/adapters/index.js +8 -2
  136. package/dist/adapters/logger.d.ts +2 -2
  137. package/dist/adapters/logger.d.ts.map +1 -1
  138. package/dist/adapters/memQueue.d.ts +5 -3
  139. package/dist/adapters/memQueue.d.ts.map +1 -1
  140. package/dist/adapters/memQueue.js +6 -5
  141. package/dist/adapters/mongo-client.d.ts +4 -3
  142. package/dist/adapters/mongo-client.d.ts.map +1 -1
  143. package/dist/adapters/mongo-client.js +5 -3
  144. package/dist/adapters/redis-client.d.ts +6 -3
  145. package/dist/adapters/redis-client.d.ts.map +1 -1
  146. package/dist/adapters/redis-client.js +7 -3
  147. package/dist/api/ContextProvider.d.ts +12 -8
  148. package/dist/api/ContextProvider.d.ts.map +1 -1
  149. package/dist/api/ContextProvider.js +9 -7
  150. package/dist/api/codec.d.ts +1 -1
  151. package/dist/api/internal/RequestContextMiddleware.d.ts +3 -3
  152. package/dist/api/internal/RequestContextMiddleware.d.ts.map +1 -1
  153. package/dist/api/internal/RequestContextMiddleware.js +10 -6
  154. package/dist/api/internal/auth.d.ts +45 -7
  155. package/dist/api/internal/auth.d.ts.map +1 -1
  156. package/dist/api/internal/auth.js +162 -29
  157. package/dist/api/internal/events.d.ts +6 -4
  158. package/dist/api/internal/events.d.ts.map +1 -1
  159. package/dist/api/internal/events.js +16 -9
  160. package/dist/api/internal/health.d.ts +1 -1
  161. package/dist/api/layerUtils.d.ts +10 -6
  162. package/dist/api/layerUtils.d.ts.map +1 -1
  163. package/dist/api/layerUtils.js +7 -6
  164. package/dist/api/middlewares.d.ts +1 -1
  165. package/dist/api/reportError.d.ts +2 -2
  166. package/dist/api/reportError.d.ts.map +1 -1
  167. package/dist/api/reportError.js +3 -2
  168. package/dist/api/routing/middleware/RouterMiddleware.d.ts +5 -4
  169. package/dist/api/routing/middleware/RouterMiddleware.d.ts.map +1 -1
  170. package/dist/api/routing/middleware/middleware.d.ts +42 -3
  171. package/dist/api/routing/middleware/middleware.d.ts.map +1 -1
  172. package/dist/api/routing/middleware/middleware.js +53 -17
  173. package/dist/api/routing/middleware.d.ts +1 -2
  174. package/dist/api/routing/middleware.d.ts.map +1 -1
  175. package/dist/api/routing/middleware.js +1 -2
  176. package/dist/api/routing/schema/jwt.d.ts +1 -1
  177. package/dist/api/routing/schema/jwt.d.ts.map +1 -1
  178. package/dist/api/routing/schema/jwt.js +3 -2
  179. package/dist/api/routing/tsort.d.ts +1 -1
  180. package/dist/api/routing/tsort.d.ts.map +1 -1
  181. package/dist/api/routing/utils.d.ts +4 -4
  182. package/dist/api/routing/utils.d.ts.map +1 -1
  183. package/dist/api/routing/utils.js +3 -2
  184. package/dist/api/routing.d.ts +84 -37
  185. package/dist/api/routing.d.ts.map +1 -1
  186. package/dist/api/routing.js +115 -45
  187. package/dist/api/setupRequest.d.ts +10 -6
  188. package/dist/api/setupRequest.d.ts.map +1 -1
  189. package/dist/api/setupRequest.js +15 -7
  190. package/dist/api/util.d.ts +1 -1
  191. package/dist/arbs.d.ts +2 -2
  192. package/dist/arbs.d.ts.map +1 -1
  193. package/dist/arbs.js +5 -3
  194. package/dist/errorReporter.d.ts +7 -5
  195. package/dist/errorReporter.d.ts.map +1 -1
  196. package/dist/errorReporter.js +22 -26
  197. package/dist/errors.d.ts +1 -1
  198. package/dist/fileUtil.d.ts +2 -2
  199. package/dist/fileUtil.d.ts.map +1 -1
  200. package/dist/fileUtil.js +2 -2
  201. package/dist/index.d.ts +1 -1
  202. package/dist/logger/jsonLogger.d.ts +2 -2
  203. package/dist/logger/jsonLogger.d.ts.map +1 -1
  204. package/dist/logger/jsonLogger.js +4 -2
  205. package/dist/logger/logFmtLogger.d.ts +2 -2
  206. package/dist/logger/logFmtLogger.d.ts.map +1 -1
  207. package/dist/logger/logFmtLogger.js +2 -2
  208. package/dist/logger/shared.d.ts +2 -2
  209. package/dist/logger/shared.d.ts.map +1 -1
  210. package/dist/logger/shared.js +3 -3
  211. package/dist/logger.d.ts +1 -1
  212. package/dist/logger.d.ts.map +1 -1
  213. package/dist/otel.d.ts +75 -0
  214. package/dist/otel.d.ts.map +1 -0
  215. package/dist/otel.js +65 -0
  216. package/dist/rateLimit.d.ts +12 -4
  217. package/dist/rateLimit.d.ts.map +1 -1
  218. package/dist/rateLimit.js +7 -12
  219. package/dist/test.d.ts +3 -3
  220. package/dist/test.d.ts.map +1 -1
  221. package/dist/test.js +2 -2
  222. package/dist/vitest.d.ts +1 -1
  223. package/examples/query.ts +46 -38
  224. package/package.json +46 -37
  225. package/src/CUPS.ts +15 -11
  226. package/src/Emailer/Sendgrid.ts +21 -15
  227. package/src/Emailer/fake.ts +1 -1
  228. package/src/Emailer/service.ts +13 -3
  229. package/src/MainFiberSet.ts +9 -6
  230. package/src/Model/Repository/Registry.ts +34 -0
  231. package/src/Model/Repository/ext.ts +103 -11
  232. package/src/Model/Repository/internal/internal.ts +231 -149
  233. package/src/Model/Repository/legacy.ts +3 -1
  234. package/src/Model/Repository/makeRepo.ts +15 -10
  235. package/src/Model/Repository/service.ts +35 -23
  236. package/src/Model/Repository/validation.ts +5 -5
  237. package/src/Model/Repository.ts +1 -0
  238. package/src/Model/dsl.ts +5 -4
  239. package/src/Model/filter/types/path/eager.ts +1 -2
  240. package/src/Model/query/dsl.ts +353 -19
  241. package/src/Model/query/new-kid-interpreter.ts +211 -6
  242. package/src/Model.ts +1 -0
  243. package/src/QueueMaker/SQLQueue.ts +150 -153
  244. package/src/QueueMaker/errors.ts +3 -1
  245. package/src/QueueMaker/memQueue.ts +111 -105
  246. package/src/QueueMaker/sbqueue.ts +76 -88
  247. package/src/QueueMaker/service.ts +3 -1
  248. package/src/RequestContext.ts +15 -16
  249. package/src/RequestFiberSet.ts +8 -2
  250. package/src/Store/ContextMapContainer.ts +45 -2
  251. package/src/Store/Cosmos/query.ts +143 -44
  252. package/src/Store/Cosmos.ts +491 -350
  253. package/src/Store/Disk.ts +106 -66
  254. package/src/Store/Memory.ts +285 -87
  255. package/src/Store/SQL/Pg.ts +364 -0
  256. package/src/Store/SQL/query.ts +540 -0
  257. package/src/Store/SQL.ts +736 -0
  258. package/src/Store/codeFilter.ts +5 -2
  259. package/src/Store/index.ts +20 -3
  260. package/src/Store/service.ts +45 -10
  261. package/src/Store/utils.ts +25 -23
  262. package/src/adapters/SQL/Model.ts +42 -41
  263. package/src/adapters/ServiceBus.ts +131 -121
  264. package/src/adapters/cosmos-client.ts +4 -2
  265. package/src/adapters/index.ts +7 -0
  266. package/src/adapters/memQueue.ts +5 -4
  267. package/src/adapters/mongo-client.ts +4 -2
  268. package/src/adapters/redis-client.ts +6 -2
  269. package/src/api/ContextProvider.ts +17 -13
  270. package/src/api/internal/RequestContextMiddleware.ts +16 -5
  271. package/src/api/internal/auth.ts +248 -44
  272. package/src/api/internal/events.ts +19 -10
  273. package/src/api/layerUtils.ts +12 -8
  274. package/src/api/reportError.ts +2 -1
  275. package/src/api/routing/middleware/RouterMiddleware.ts +5 -4
  276. package/src/api/routing/middleware/middleware.ts +60 -15
  277. package/src/api/routing/middleware.ts +0 -2
  278. package/src/api/routing/schema/jwt.ts +2 -1
  279. package/src/api/routing/utils.ts +2 -1
  280. package/src/api/routing.ts +304 -131
  281. package/src/api/setupRequest.ts +31 -8
  282. package/src/arbs.ts +5 -3
  283. package/src/errorReporter.ts +65 -75
  284. package/src/fileUtil.ts +1 -1
  285. package/src/logger/jsonLogger.ts +3 -1
  286. package/src/logger/logFmtLogger.ts +1 -1
  287. package/src/logger/shared.ts +3 -2
  288. package/src/otel.ts +152 -0
  289. package/src/rateLimit.ts +34 -23
  290. package/src/test.ts +2 -2
  291. package/test/auth.test.ts +101 -0
  292. package/test/contextProvider.test.ts +14 -11
  293. package/test/controller.test.ts +25 -29
  294. package/test/dist/auth.test.d.ts.map +1 -0
  295. package/test/dist/contextProvider.test.d.ts.map +1 -1
  296. package/test/dist/controller.test.d.ts.map +1 -1
  297. package/test/dist/date-query.test.d.ts.map +1 -0
  298. package/test/dist/fixtures.d.ts +30 -12
  299. package/test/dist/fixtures.d.ts.map +1 -1
  300. package/test/dist/fixtures.js +17 -10
  301. package/test/dist/query.test.d.ts.map +1 -1
  302. package/test/dist/rawQuery.test.d.ts.map +1 -1
  303. package/test/dist/repository-ext.test.d.ts.map +1 -0
  304. package/test/dist/requires.test.d.ts.map +1 -1
  305. package/test/dist/router-generator.test.d.ts.map +1 -0
  306. package/test/dist/routing-interruptibility.test.d.ts.map +1 -0
  307. package/test/dist/rpc-e2e-invalidation.test.d.ts.map +1 -0
  308. package/test/dist/rpc-multi-middleware.test.d.ts.map +1 -1
  309. package/test/dist/rpc-stream-fullstack.test.d.ts.map +1 -0
  310. package/test/dist/sql-store.test.d.ts.map +1 -0
  311. package/test/fixtures.ts +16 -9
  312. package/test/layerUtils.test.ts +1 -1
  313. package/test/query.test.ts +819 -38
  314. package/test/rawQuery.test.ts +312 -20
  315. package/test/repository-ext.test.ts +62 -0
  316. package/test/requires.test.ts +10 -5
  317. package/test/router-generator.test.ts +187 -0
  318. package/test/routing-interruptibility.test.ts +66 -0
  319. package/test/rpc-e2e-invalidation.test.ts +256 -0
  320. package/test/rpc-multi-middleware.test.ts +84 -9
  321. package/test/rpc-stream-fullstack.test.ts +304 -0
  322. package/test/sql-store.test.ts +1592 -0
  323. package/test/validateSample.test.ts +17 -12
  324. package/tsconfig.examples.json +1 -1
  325. package/tsconfig.json +0 -1
  326. package/tsconfig.json.bak +2 -2
  327. package/tsconfig.src.json +35 -35
  328. package/tsconfig.test.json +2 -2
  329. package/dist/Operations.d.ts +0 -55
  330. package/dist/Operations.d.ts.map +0 -1
  331. package/dist/Operations.js +0 -102
  332. package/dist/OperationsRepo.d.ts +0 -41
  333. package/dist/OperationsRepo.d.ts.map +0 -1
  334. package/dist/OperationsRepo.js +0 -14
  335. package/eslint.config.mjs +0 -24
  336. package/src/Operations.ts +0 -235
  337. package/src/OperationsRepo.ts +0 -16
@@ -1,9 +1,21 @@
1
- import { Effect, Layer, Tracer } from "effect-app"
1
+ import * as Effect from "effect-app/Effect"
2
+ import * as Layer from "effect-app/Layer"
3
+ import * as Option from "effect-app/Option"
2
4
  import { NonEmptyString255 } from "effect-app/Schema"
5
+ import * as Tracer from "effect/Tracer"
6
+ import { SqlClient } from "effect/unstable/sql"
3
7
  import { LocaleRef, RequestContext, spanAttributes } from "../RequestContext.js"
4
8
  import { ContextMapContainer } from "../Store/ContextMapContainer.js"
5
9
  import { storeId } from "../Store/Memory.js"
6
10
 
11
+ const withSqlTransaction = <R, E, A>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
12
+ Effect.serviceOption(SqlClient.SqlClient).pipe(
13
+ Effect.flatMap(Option.match({
14
+ onNone: () => self,
15
+ onSome: (sql) => sql.withTransaction(self).pipe(Effect.orDie)
16
+ }))
17
+ )
18
+
7
19
  export const getRequestContext = Effect
8
20
  .all({
9
21
  span: Effect.currentSpan.pipe(Effect.orDie),
@@ -12,7 +24,7 @@ export const getRequestContext = Effect
12
24
  })
13
25
  .pipe(
14
26
  Effect.map(({ locale, namespace, span }) =>
15
- new RequestContext({
27
+ RequestContext.make({
16
28
  span: Tracer.externalSpan(span),
17
29
  locale,
18
30
  namespace,
@@ -43,16 +55,25 @@ const withRequestSpan = (name = "request", options?: Tracer.SpanOptions) => <R,
43
55
  )
44
56
  )
45
57
 
58
+ export interface SetupRequestOptions {
59
+ readonly withTransaction?: boolean
60
+ }
61
+
46
62
  export const setupRequestContextFromCurrent =
47
- (name = "request", options?: Tracer.SpanOptions) => <R, E, A>(self: Effect.Effect<A, E, R>) =>
63
+ (name = "request", options?: Tracer.SpanOptions & SetupRequestOptions) => <R, E, A>(self: Effect.Effect<A, E, R>) =>
48
64
  self
49
65
  .pipe(
66
+ options?.withTransaction === true ? withSqlTransaction : (_) => _,
50
67
  withRequestSpan(name, options),
51
- Effect.provide(ContextMapContainer.layer)
68
+ Effect.provide(ContextMapContainer.layer, { local: true })
52
69
  )
53
70
 
54
71
  // TODO: consider integrating Effect.withParentSpan
55
- export function setupRequestContext<R, E, A>(self: Effect.Effect<A, E, R>, requestContext: RequestContext) {
72
+ export function setupRequestContext<R, E, A>(
73
+ self: Effect.Effect<A, E, R>,
74
+ requestContext: RequestContext,
75
+ options?: SetupRequestOptions
76
+ ) {
56
77
  const layer = Layer.mergeAll(
57
78
  ContextMapContainer.layer,
58
79
  Layer.succeed(LocaleRef, requestContext.locale),
@@ -60,8 +81,9 @@ export function setupRequestContext<R, E, A>(self: Effect.Effect<A, E, R>, reque
60
81
  )
61
82
  return self
62
83
  .pipe(
84
+ options?.withTransaction === true ? withSqlTransaction : (_) => _,
63
85
  withRequestSpan(requestContext.name),
64
- Effect.provide(layer)
86
+ Effect.provide(layer, { local: true })
65
87
  )
66
88
  }
67
89
 
@@ -69,7 +91,7 @@ export function setupRequestContextWithCustomSpan<R, E, A>(
69
91
  self: Effect.Effect<A, E, R>,
70
92
  requestContext: RequestContext,
71
93
  name: string,
72
- options?: Tracer.SpanOptions
94
+ options?: Tracer.SpanOptions & SetupRequestOptions
73
95
  ) {
74
96
  const layer = Layer.mergeAll(
75
97
  ContextMapContainer.layer,
@@ -78,7 +100,8 @@ export function setupRequestContextWithCustomSpan<R, E, A>(
78
100
  )
79
101
  return self
80
102
  .pipe(
103
+ options?.withTransaction === true ? withSqlTransaction : (_) => _,
81
104
  withRequestSpan(name, options),
82
- Effect.provide(layer)
105
+ Effect.provide(layer, { local: true })
83
106
  )
84
107
  }
package/src/arbs.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  // Do not import to frontend
2
2
 
3
3
  import { faker } from "@faker-js/faker"
4
- import { type S } from "effect-app"
5
4
  import { setFaker } from "effect-app/faker"
5
+ import type * as S from "effect-app/Schema"
6
6
  import * as FastCheck from "effect/testing/FastCheck"
7
7
  import { Random } from "fast-check"
8
- import * as rand from "pure-rand"
8
+ import { congruential32 } from "pure-rand/generator/congruential32"
9
9
 
10
- const rnd = new Random(rand.congruential32(5))
10
+ const seed = 5
11
+ const rng = congruential32(seed)
12
+ const rnd = new Random(rng)
11
13
 
12
14
  setFaker(faker)
13
15
 
@@ -1,6 +1,8 @@
1
1
  import * as Sentry from "@sentry/node"
2
- import { Cause, Effect, type LogLevel } from "effect-app"
2
+ import * as Effect from "effect-app/Effect"
3
3
  import { dropUndefined, LogLevelToSentry } from "effect-app/utils"
4
+ import * as Cause from "effect/Cause"
5
+ import type * as LogLevel from "effect/LogLevel"
4
6
  import { getRC } from "./api/setupRequest.js"
5
7
  import { CauseException, tryToJson, tryToReport } from "./errors.js"
6
8
  import { InfraLogger } from "./logger.js"
@@ -13,47 +15,41 @@ const tryCauseException = <E>(cause: Cause.Cause<E>, name: string): CauseExcepti
13
15
  }
14
16
  }
15
17
 
16
- export function reportError(
17
- name: string
18
- ) {
19
- return (
20
- cause: Cause.Cause<unknown>,
21
- extras?: Record<string, unknown>,
22
- level: LogLevel.Severity = "Error"
23
- ) =>
24
- Effect
25
- .gen(function*() {
26
- if (Cause.hasInterruptsOnly(cause)) {
27
- yield* InfraLogger.logDebug("Interrupted").pipe(Effect.annotateLogs("extras", JSON.stringify(extras ?? {})))
28
- return
29
- }
30
- const error = tryCauseException(cause, name)
31
-
32
- yield* reportSentry(error, extras, LogLevelToSentry(level))
33
- yield* InfraLogger
34
- .logWithLevel(level, "Reporting error", cause)
35
- .pipe(
36
- Effect.annotateLogs(dropUndefined({
37
- extras,
38
- error: tryToReport(error),
39
- cause: tryToJson(cause),
40
- __error_name__: name
41
- }))
42
- )
43
- .pipe(
44
- Effect.catchCause((cause) => InfraLogger.logWarning("Failed to log error", cause)),
45
- Effect.catchCause(() => InfraLogger.logFatal("Failed to log error cause"))
46
- )
18
+ export function reportError(name: string) {
19
+ return Effect.fnUntraced(
20
+ function*(
21
+ cause: Cause.Cause<unknown>,
22
+ extras?: Record<string, unknown>,
23
+ level: LogLevel.Severity = "Error"
24
+ ) {
25
+ if (Cause.hasInterruptsOnly(cause)) {
26
+ yield* InfraLogger.logDebug("Interrupted").pipe(Effect.annotateLogs("extras", JSON.stringify(extras ?? {})))
27
+ return
28
+ }
29
+ const error = tryCauseException(cause, name)
47
30
 
48
- return error
49
- })
50
- .pipe(
51
- Effect.tapCause((cause) =>
52
- InfraLogger.logError("Failed to report error", cause).pipe(
53
- Effect.tapCause(() => InfraLogger.logFatal("Failed to log error cause"))
54
- )
31
+ yield* reportSentry(error, extras, LogLevelToSentry(level))
32
+ yield* InfraLogger
33
+ .logWithLevel(level, "Reporting error", cause)
34
+ .pipe(
35
+ Effect.annotateLogs(dropUndefined({
36
+ extras,
37
+ error: tryToReport(error),
38
+ cause: tryToJson(cause),
39
+ __error_name__: name
40
+ })),
41
+ Effect.catchCause((cause) => InfraLogger.logWarning("Failed to log error", cause)),
42
+ Effect.catchCause(() => InfraLogger.logFatal("Failed to log error cause"))
55
43
  )
56
- )
44
+
45
+ return error
46
+ },
47
+ (effect) =>
48
+ Effect.tapCause(effect, (cause) =>
49
+ InfraLogger.logError("Failed to report error", cause).pipe(
50
+ Effect.tapCause(() => InfraLogger.logFatal("Failed to log error cause"))
51
+ ))
52
+ )
57
53
  }
58
54
 
59
55
  function reportSentry(
@@ -66,45 +62,39 @@ function reportSentry(
66
62
  scope.setLevel(level)
67
63
  if (context) scope.setContext("context", { ...context })
68
64
  if (extras) scope.setContext("extras", extras)
69
- scope.setContext("error", { data: tryToReport(error) })
70
- scope.setContext("cause", { data: tryToJson(error.originalCause) })
65
+ const squashed = Cause.squash(error.originalCause)
66
+ scope.setContext("mainError", tryToJson(squashed))
67
+ scope.setContext("error", tryToReport(error))
68
+ scope.setContext("cause", tryToJson(error.originalCause))
71
69
  Sentry.captureException(error, scope)
72
70
  }))
73
71
  }
74
72
 
75
- export function logError<E>(
76
- name: string
77
- ) {
78
- return (cause: Cause.Cause<E>, extras?: Record<string, unknown>) =>
79
- Effect
80
- .gen(function*() {
81
- if (Cause.hasInterruptsOnly(cause)) {
82
- yield* InfraLogger.logDebug("Interrupted").pipe(Effect.annotateLogs(dropUndefined({ extras })))
83
- return
84
- }
85
- yield* InfraLogger
86
- .logWarning("Logging error", cause)
87
- .pipe(
88
- Effect.annotateLogs(dropUndefined({
89
- extras,
90
- cause: tryToJson(cause),
91
- __error_name__: name
92
- }))
93
- )
94
- })
95
- .pipe(
96
- Effect.tapCause(() => InfraLogger.logFatal("Failed to log error cause"))
97
- )
73
+ export function logError<E>(name: string) {
74
+ return Effect.fnUntraced(
75
+ function*(cause: Cause.Cause<E>, extras?: Record<string, unknown>) {
76
+ if (Cause.hasInterruptsOnly(cause)) {
77
+ yield* InfraLogger.logDebug("Interrupted").pipe(Effect.annotateLogs(dropUndefined({ extras })))
78
+ return
79
+ }
80
+ yield* InfraLogger
81
+ .logWarning("Logging error", cause)
82
+ .pipe(Effect.annotateLogs(dropUndefined({
83
+ extras,
84
+ cause: tryToJson(cause),
85
+ __error_name__: name
86
+ })))
87
+ },
88
+ (effect) => Effect.tapCause(effect, () => InfraLogger.logFatal("Failed to log error cause"))
89
+ )
98
90
  }
99
91
 
100
- export function reportMessage(message: string, extras?: Record<string, unknown>) {
101
- return Effect.gen(function*() {
102
- const context = yield* getRC
103
- const scope = new Sentry.Scope()
104
- if (context) scope.setContext("context", { ...context })
105
- if (extras) scope.setContext("extras", extras)
106
- Sentry.captureMessage(message, scope)
92
+ export const reportMessage = Effect.fnUntraced(function*(message: string, extras?: Record<string, unknown>) {
93
+ const context = yield* getRC
94
+ const scope = new Sentry.Scope()
95
+ if (context) scope.setContext("context", { ...context })
96
+ if (extras) scope.setContext("extras", extras)
97
+ Sentry.captureMessage(message, scope)
107
98
 
108
- console.warn(message, extras)
109
- })
110
- }
99
+ console.warn(message, extras)
100
+ })
package/src/fileUtil.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import crypto from "crypto"
2
- import { Effect } from "effect-app"
2
+ import * as Effect from "effect-app/Effect"
3
3
  import type { Abortable } from "events"
4
4
  import type { Mode, ObjectEncodingOptions, OpenMode } from "fs"
5
5
  import fs from "fs/promises"
@@ -1,4 +1,6 @@
1
- import { Array, Cause, Logger } from "effect-app"
1
+ import * as Array from "effect-app/Array"
2
+ import * as Cause from "effect/Cause"
3
+ import * as Logger from "effect/Logger"
2
4
  import { CurrentLogAnnotations, CurrentLogSpans } from "effect/References"
3
5
  import { spanAttributes } from "../RequestContext.js"
4
6
  import { getRequestContextFromFiber } from "./shared.js"
@@ -1,4 +1,4 @@
1
- import { Logger } from "effect-app"
1
+ import * as Logger from "effect/Logger"
2
2
  import { spanAttributes } from "../RequestContext.js"
3
3
  import { getRequestContextFromFiber } from "./shared.js"
4
4
 
@@ -1,5 +1,6 @@
1
- import { type Fiber, Option } from "effect-app"
1
+ import * as Option from "effect-app/Option"
2
2
  import { NonEmptyString255 } from "effect-app/Schema"
3
+ import type * as Fiber from "effect/Fiber"
3
4
  import { LocaleRef, RequestContext } from "../RequestContext.js"
4
5
  import { storeId } from "../Store/Memory.js"
5
6
 
@@ -7,7 +8,7 @@ export function getRequestContextFromFiber(fiber: Fiber.Fiber<unknown, unknown>)
7
8
  const span = Option.fromNullishOr(fiber.currentSpan)
8
9
  const locale = fiber.getRef(LocaleRef)
9
10
  const namespace = fiber.getRef(storeId)
10
- return new RequestContext({
11
+ return RequestContext.make({
11
12
  span: Option.map(span, (s) => ({ spanId: s.spanId, traceId: s.traceId, sampled: s.sampled })).pipe(
12
13
  Option.getOrElse(() => ({ spanId: "bogus", sampled: true, traceId: "bogus" }))
13
14
  ),
package/src/otel.ts ADDED
@@ -0,0 +1,152 @@
1
+ /**
2
+ * OpenTelemetry semantic-convention helpers for span attributes.
3
+ *
4
+ * Aligns repository / queue / cache adapters with stable OTel semconv keys so
5
+ * downstream collectors and dashboards work without per-adapter mappings.
6
+ *
7
+ * - Database: https://opentelemetry.io/docs/specs/semconv/database/
8
+ * - Messaging: https://opentelemetry.io/docs/specs/semconv/messaging/
9
+ * - Cosmos DB: https://opentelemetry.io/docs/specs/semconv/database/cosmosdb/
10
+ */
11
+
12
+ import * as Effect from "effect-app/Effect"
13
+
14
+ export type DbSystem =
15
+ | "postgresql"
16
+ | "sqlite"
17
+ | "cosmosdb"
18
+ | "mongodb"
19
+ | "redis"
20
+ | "other_sql"
21
+ | "memory"
22
+ | "disk"
23
+
24
+ export interface DbSpanOptions {
25
+ /** OTel `db.operation.name` (e.g. `find`, `all`, `filter`, `set`). */
26
+ readonly operation: string
27
+ readonly system: DbSystem
28
+ /** Logical collection / table / container name. */
29
+ readonly collection: string
30
+ /** Tenant / namespace / database name. */
31
+ readonly namespace?: string | undefined
32
+ /** Application-level entity / model name (custom: `app.entity`). */
33
+ readonly entity?: string | undefined
34
+ /** Sanitized / parameterized query text. Never include bound values. */
35
+ readonly query?: string | undefined
36
+ /** Optional fragments merged into final attributes (e.g. id, partition). */
37
+ readonly extra?: Record<string, unknown> | undefined
38
+ }
39
+
40
+ const dbAttributes = (a: DbSpanOptions): Record<string, unknown> => ({
41
+ "db.system.name": a.system,
42
+ "db.operation.name": a.operation,
43
+ "db.collection.name": a.collection,
44
+ ...(a.namespace !== undefined && { "db.namespace": a.namespace }),
45
+ ...(a.query !== undefined && { "db.query.text": a.query }),
46
+ ...(a.entity !== undefined && { "app.entity": a.entity }),
47
+ ...a.extra
48
+ })
49
+
50
+ /**
51
+ * Wrap an effect with an OTel-semconv database span.
52
+ *
53
+ * Span name follows the low-cardinality convention: `<operation> <collection>`.
54
+ */
55
+ export const withDbSpan = (a: DbSpanOptions) =>
56
+ Effect.withSpan(
57
+ `${a.operation} ${a.collection}`,
58
+ { attributes: dbAttributes(a), kind: "client" as const },
59
+ { captureStackTrace: false }
60
+ )
61
+
62
+ /**
63
+ * Annotate the current span with OTel-semconv database attributes.
64
+ *
65
+ * Use when the caller already owns the span (e.g. a repository) and the
66
+ * adapter should only contribute db.* semconv attrs without opening a child.
67
+ * Annotates before running so attrs persist even on failure.
68
+ * No-op if there is no current span.
69
+ */
70
+ export const annotateDb = (a: DbSpanOptions) => <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
71
+ Effect.flatMap(Effect.annotateCurrentSpan(dbAttributes(a)), () => self)
72
+
73
+ /** Annotate the current span with response metrics from a DB call. */
74
+ export const annotateDbResponse = (m: {
75
+ readonly returnedRows?: number | undefined
76
+ readonly responseBytes?: number | undefined
77
+ }) =>
78
+ Effect.annotateCurrentSpan({
79
+ ...(m.returnedRows !== undefined && { "db.response.returned_rows": m.returnedRows }),
80
+ ...(m.responseBytes !== undefined && { "db.response.body.size": m.responseBytes })
81
+ })
82
+
83
+ /** Cosmos-specific response annotations. */
84
+ export const annotateCosmosResponse = (m: {
85
+ readonly requestCharge?: number | undefined
86
+ readonly returnedRows?: number | undefined
87
+ readonly responseBytes?: number | undefined
88
+ readonly statusCode?: number | undefined
89
+ }) =>
90
+ Effect.annotateCurrentSpan({
91
+ ...(m.requestCharge !== undefined && { "azure.cosmosdb.operation.request_charge": m.requestCharge }),
92
+ ...(m.statusCode !== undefined && { "db.response.status_code": String(m.statusCode) }),
93
+ ...(m.returnedRows !== undefined && { "db.response.returned_rows": m.returnedRows }),
94
+ ...(m.responseBytes !== undefined && { "db.response.body.size": m.responseBytes })
95
+ })
96
+
97
+ export type MessagingSystem =
98
+ | "servicebus"
99
+ | "rabbitmq"
100
+ | "kafka"
101
+ | "memory"
102
+ | "sql"
103
+
104
+ export type MessagingOperation =
105
+ | "publish"
106
+ | "create"
107
+ | "receive"
108
+ | "process"
109
+ | "settle"
110
+
111
+ export interface MessagingSpanOptions {
112
+ readonly operation: MessagingOperation
113
+ readonly system: MessagingSystem
114
+ /** Queue / topic name. */
115
+ readonly destination: string
116
+ readonly messageId?: string | undefined
117
+ readonly conversationId?: string | undefined
118
+ readonly bodySize?: number | undefined
119
+ readonly extra?: Record<string, unknown> | undefined
120
+ }
121
+
122
+ const messagingAttributes = (a: MessagingSpanOptions): Record<string, unknown> => ({
123
+ "messaging.system": a.system,
124
+ "messaging.operation.name": a.operation,
125
+ "messaging.destination.name": a.destination,
126
+ ...(a.messageId !== undefined && { "messaging.message.id": a.messageId }),
127
+ ...(a.conversationId !== undefined && { "messaging.message.conversation_id": a.conversationId }),
128
+ ...(a.bodySize !== undefined && { "messaging.message.body.size": a.bodySize }),
129
+ ...a.extra
130
+ })
131
+
132
+ /** Wrap an effect with an OTel-semconv messaging span. */
133
+ export const withMessagingSpan = (
134
+ a: MessagingSpanOptions,
135
+ kind: "producer" | "consumer"
136
+ ) =>
137
+ Effect.withSpan(
138
+ `${a.operation} ${a.destination}`,
139
+ { kind, attributes: messagingAttributes(a) },
140
+ { captureStackTrace: false }
141
+ )
142
+
143
+ /** Build messaging span options without wrapping (for Effect.fn / setupRequestContextWithCustomSpan). */
144
+ export const messagingSpanArgs = (
145
+ a: MessagingSpanOptions,
146
+ kind: "producer" | "consumer"
147
+ ) =>
148
+ ({
149
+ name: `${a.operation} ${a.destination}`,
150
+ kind,
151
+ attributes: messagingAttributes(a)
152
+ }) as const
package/src/rateLimit.ts CHANGED
@@ -20,8 +20,13 @@
20
20
  // }
21
21
  // }
22
22
 
23
- import { Array, type Duration, Effect, type NonEmptyArray } from "effect-app"
23
+ import * as Array from "effect-app/Array"
24
+ import type { NonEmptyArray } from "effect-app/Array"
25
+ import * as Effect from "effect-app/Effect"
26
+ import { dual } from "effect-app/Function"
27
+ import type * as Duration from "effect/Duration"
24
28
  import type { Semaphore } from "effect/Semaphore"
29
+ import type { Concurrency } from "effect/Types"
25
30
 
26
31
  /**
27
32
  * Executes the specified effect, acquiring the specified number of permits
@@ -45,36 +50,42 @@ export function SEM_withPermitsDuration(permits: number, duration: Duration.Dura
45
50
  }
46
51
  }
47
52
 
48
- export function batchPar<R, E, A, R2, E2, A2, T>(
49
- n: number,
50
- forEachItem: (item: T, iWithinBatch: number, batchI: number) => Effect.Effect<A, E, R>,
51
- forEachBatch: (a: NonEmptyArray<A>, i: number) => Effect.Effect<A2, E2, R2>
52
- ) {
53
- return (items: Iterable<T>) =>
54
- Effect.forEach(
55
- Array.chunksOf(items, n),
56
- (_, i) =>
57
- Effect
58
- .forEach(_, (_, j) => forEachItem(_, j, i), { concurrency: "inherit" })
59
- .pipe(Effect.flatMap((_) => forEachBatch(_, i))),
60
- { concurrency: "inherit" }
61
- )
53
+ export interface BatchOptions {
54
+ readonly concurrency?: Concurrency | undefined
62
55
  }
63
56
 
64
- export function batch<R, E, A, R2, E2, A2, T>(
65
- n: number,
66
- forEachItem: (item: T, iWithinBatch: number, batchI: number) => Effect.Effect<A, E, R>,
67
- forEachBatch: (a: NonEmptyArray<A>, i: number) => Effect.Effect<A2, E2, R2>
68
- ) {
69
- return (items: Iterable<T>) =>
57
+ export const batch: {
58
+ <T, A, E, R, A2, E2, R2>(
59
+ n: number,
60
+ forEachItem: (item: T, iWithinBatch: number, batchI: number) => Effect.Effect<A, E, R>,
61
+ forEachBatch: (a: NonEmptyArray<A>, i: number) => Effect.Effect<A2, E2, R2>,
62
+ options?: BatchOptions
63
+ ): (items: Iterable<T>) => Effect.Effect<Array<A2>, E | E2, R | R2>
64
+ <T, A, E, R, A2, E2, R2>(
65
+ items: Iterable<T>,
66
+ n: number,
67
+ forEachItem: (item: T, iWithinBatch: number, batchI: number) => Effect.Effect<A, E, R>,
68
+ forEachBatch: (a: NonEmptyArray<A>, i: number) => Effect.Effect<A2, E2, R2>,
69
+ options?: BatchOptions
70
+ ): Effect.Effect<Array<A2>, E | E2, R | R2>
71
+ } = dual(
72
+ (args) => typeof args[0] !== "number",
73
+ <T, A, E, R, A2, E2, R2>(
74
+ items: Iterable<T>,
75
+ n: number,
76
+ forEachItem: (item: T, iWithinBatch: number, batchI: number) => Effect.Effect<A, E, R>,
77
+ forEachBatch: (a: NonEmptyArray<A>, i: number) => Effect.Effect<A2, E2, R2>,
78
+ options?: BatchOptions
79
+ ) =>
70
80
  Effect.forEach(
71
81
  Array.chunksOf(items, n),
72
82
  (_, i) =>
73
83
  Effect
74
84
  .forEach(_, (_, j) => forEachItem(_, j, i), { concurrency: "inherit" })
75
- .pipe(Effect.flatMap((_) => forEachBatch(_, i)))
85
+ .pipe(Effect.flatMap((_) => forEachBatch(_, i))),
86
+ { concurrency: options?.concurrency }
76
87
  )
77
- }
88
+ )
78
89
 
79
90
  // export function rateLimit(
80
91
  // n: number,
package/src/test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { S } from "effect-app"
1
+ import * as S from "effect-app/Schema"
2
2
  import { copy } from "effect-app/utils"
3
3
  import { generate } from "./arbs.js"
4
4
 
@@ -16,7 +16,7 @@ export const createRandomInstance = <A extends object, I, R>(s: S.Codec<A, I, R>
16
16
  /**
17
17
  * Like `createRandomInstance`, but takes encoded values rather than decoded ones.
18
18
  */
19
- export const createRandomInstanceI = <A extends object, I>(s: S.Codec<A, I, never> & { fields: S.Struct.Fields }) => {
19
+ export const createRandomInstanceI = <A extends object, I>(s: S.Codec<A, I> & { fields: S.Struct.Fields }) => {
20
20
  const gen = generate(S.toArbitrary(s))
21
21
  const encode = S.encodeSync(s)
22
22
  const decode = S.decodeSync(s)
@@ -0,0 +1,101 @@
1
+ import { describe, expect, it } from "@effect/vitest"
2
+ import * as Effect from "effect-app/Effect"
3
+ import { HttpHeaders } from "effect-app/http"
4
+ import { SignJWT } from "jose"
5
+ import { checkJWTI, InvalidRequestError, InvalidTokenError, UnauthorizedError } from "../src/api/internal/auth.js"
6
+
7
+ const issuer = "https://issuer.example.com/"
8
+ const audience = "effect-app"
9
+ const secret = "test-secret-test-secret-test-secret"
10
+
11
+ const makeToken = () =>
12
+ new SignJWT({ scope: "read:all" })
13
+ .setProtectedHeader({ alg: "HS256", typ: "at+jwt" })
14
+ .setIssuer(issuer)
15
+ .setAudience(audience)
16
+ .setIssuedAt()
17
+ .setExpirationTime("10m")
18
+ .sign(new TextEncoder().encode(secret))
19
+
20
+ describe("checkJWTI", () => {
21
+ it.effect(
22
+ "validates a bearer token from headers",
23
+ Effect.fnUntraced(function*() {
24
+ const token = yield* Effect.promise(() => makeToken())
25
+
26
+ yield* checkJWTI({
27
+ audience,
28
+ issuer,
29
+ secret,
30
+ strict: true,
31
+ tokenSigningAlg: "HS256"
32
+ })(HttpHeaders.fromRecordUnsafe({ authorization: `Bearer ${token}` }))
33
+ })
34
+ )
35
+
36
+ it.effect(
37
+ "fails on malformed authorization headers",
38
+ Effect.fnUntraced(function*() {
39
+ const error = yield* Effect.flip(
40
+ checkJWTI({
41
+ audience,
42
+ issuer,
43
+ secret,
44
+ tokenSigningAlg: "HS256"
45
+ })(HttpHeaders.fromRecordUnsafe({ authorization: "Basic abc" }))
46
+ )
47
+
48
+ expect(error).toBeInstanceOf(InvalidRequestError)
49
+ expect(error.status).toBe(400)
50
+ })
51
+ )
52
+
53
+ it.effect(
54
+ "fails when the token is missing",
55
+ Effect.fnUntraced(function*() {
56
+ const error = yield* Effect.flip(
57
+ checkJWTI({
58
+ audience,
59
+ issuer,
60
+ secret,
61
+ tokenSigningAlg: "HS256"
62
+ })(HttpHeaders.empty)
63
+ )
64
+
65
+ expect(error).toBeInstanceOf(UnauthorizedError)
66
+ expect(error.status).toBe(401)
67
+ })
68
+ )
69
+
70
+ it.effect(
71
+ "allows missing tokens when auth is optional",
72
+ Effect.fnUntraced(function*() {
73
+ yield* checkJWTI({
74
+ audience,
75
+ authRequired: false,
76
+ issuer,
77
+ secret,
78
+ tokenSigningAlg: "HS256"
79
+ })(HttpHeaders.empty)
80
+ })
81
+ )
82
+
83
+ it.effect(
84
+ "fails when the token signature is invalid",
85
+ Effect.fnUntraced(function*() {
86
+ const token = yield* Effect.promise(() => makeToken())
87
+
88
+ const error = yield* Effect.flip(
89
+ checkJWTI({
90
+ audience,
91
+ issuer,
92
+ secret: "wrong-secret-wrong-secret-wrong-secret",
93
+ tokenSigningAlg: "HS256"
94
+ })(HttpHeaders.fromRecordUnsafe({ authorization: `Bearer ${token}` }))
95
+ )
96
+
97
+ expect(error).toBeInstanceOf(InvalidTokenError)
98
+ expect(error.status).toBe(401)
99
+ })
100
+ )
101
+ })