@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.
- package/CHANGELOG.md +1648 -0
- package/_check.sh +1 -1
- package/dist/CUPS.d.ts +12 -7
- package/dist/CUPS.d.ts.map +1 -1
- package/dist/CUPS.js +16 -12
- package/dist/Emailer/Sendgrid.d.ts +15 -15
- package/dist/Emailer/Sendgrid.d.ts.map +1 -1
- package/dist/Emailer/Sendgrid.js +20 -16
- package/dist/Emailer/fake.d.ts +1 -1
- package/dist/Emailer/fake.js +2 -2
- package/dist/Emailer/service.d.ts +13 -4
- package/dist/Emailer/service.d.ts.map +1 -1
- package/dist/Emailer/service.js +4 -3
- package/dist/Emailer.d.ts +1 -1
- package/dist/MainFiberSet.d.ts +12 -9
- package/dist/MainFiberSet.d.ts.map +1 -1
- package/dist/MainFiberSet.js +7 -3
- package/dist/Model/Repository/Registry.d.ts +21 -0
- package/dist/Model/Repository/Registry.d.ts.map +1 -0
- package/dist/Model/Repository/Registry.js +18 -0
- package/dist/Model/Repository/ext.d.ts +35 -16
- package/dist/Model/Repository/ext.d.ts.map +1 -1
- package/dist/Model/Repository/ext.js +60 -3
- package/dist/Model/Repository/internal/internal.d.ts +9 -6
- package/dist/Model/Repository/internal/internal.d.ts.map +1 -1
- package/dist/Model/Repository/internal/internal.js +115 -51
- package/dist/Model/Repository/legacy.d.ts +4 -2
- package/dist/Model/Repository/legacy.d.ts.map +1 -1
- package/dist/Model/Repository/makeRepo.d.ts +10 -6
- package/dist/Model/Repository/makeRepo.d.ts.map +1 -1
- package/dist/Model/Repository/makeRepo.js +5 -2
- package/dist/Model/Repository/service.d.ts +32 -24
- package/dist/Model/Repository/service.d.ts.map +1 -1
- package/dist/Model/Repository/validation.d.ts +47 -18
- package/dist/Model/Repository/validation.d.ts.map +1 -1
- package/dist/Model/Repository/validation.js +6 -6
- package/dist/Model/Repository.d.ts +2 -1
- package/dist/Model/Repository.d.ts.map +1 -1
- package/dist/Model/Repository.js +2 -1
- package/dist/Model/dsl.d.ts +6 -5
- package/dist/Model/dsl.d.ts.map +1 -1
- package/dist/Model/dsl.js +2 -3
- package/dist/Model/filter/filterApi.d.ts +5 -5
- package/dist/Model/filter/filterApi.d.ts.map +1 -1
- package/dist/Model/filter/types/errors.d.ts +1 -1
- package/dist/Model/filter/types/fields.d.ts +1 -1
- package/dist/Model/filter/types/path/common.d.ts +1 -1
- package/dist/Model/filter/types/path/eager.d.ts +1 -1
- package/dist/Model/filter/types/path/eager.d.ts.map +1 -1
- package/dist/Model/filter/types/path/eager.js +1 -1
- package/dist/Model/filter/types/path/index.d.ts +1 -1
- package/dist/Model/filter/types/utils.d.ts +1 -1
- package/dist/Model/filter/types/validator.d.ts +1 -1
- package/dist/Model/filter/types.d.ts +1 -1
- package/dist/Model/query/dsl.d.ts +142 -17
- package/dist/Model/query/dsl.d.ts.map +1 -1
- package/dist/Model/query/dsl.js +190 -5
- package/dist/Model/query/new-kid-interpreter.d.ts +77 -8
- package/dist/Model/query/new-kid-interpreter.d.ts.map +1 -1
- package/dist/Model/query/new-kid-interpreter.js +127 -6
- package/dist/Model/query.d.ts +1 -1
- package/dist/Model.d.ts +2 -1
- package/dist/Model.d.ts.map +1 -1
- package/dist/Model.js +2 -1
- package/dist/QueueMaker/SQLQueue.d.ts +7 -8
- package/dist/QueueMaker/SQLQueue.d.ts.map +1 -1
- package/dist/QueueMaker/SQLQueue.js +135 -117
- package/dist/QueueMaker/errors.d.ts +5 -3
- package/dist/QueueMaker/errors.d.ts.map +1 -1
- package/dist/QueueMaker/errors.js +4 -2
- package/dist/QueueMaker/memQueue.d.ts +9 -5
- package/dist/QueueMaker/memQueue.d.ts.map +1 -1
- package/dist/QueueMaker/memQueue.js +81 -65
- package/dist/QueueMaker/sbqueue.d.ts +8 -4
- package/dist/QueueMaker/sbqueue.d.ts.map +1 -1
- package/dist/QueueMaker/sbqueue.js +57 -55
- package/dist/QueueMaker/service.d.ts +4 -2
- package/dist/QueueMaker/service.d.ts.map +1 -1
- package/dist/QueueMaker/service.js +1 -1
- package/dist/RequestContext.d.ts +75 -35
- package/dist/RequestContext.d.ts.map +1 -1
- package/dist/RequestContext.js +14 -14
- package/dist/RequestFiberSet.d.ts +10 -7
- package/dist/RequestFiberSet.d.ts.map +1 -1
- package/dist/RequestFiberSet.js +8 -3
- package/dist/Store/ContextMapContainer.d.ts +22 -3
- package/dist/Store/ContextMapContainer.d.ts.map +1 -1
- package/dist/Store/ContextMapContainer.js +17 -3
- package/dist/Store/Cosmos/query.d.ts +7 -2
- package/dist/Store/Cosmos/query.d.ts.map +1 -1
- package/dist/Store/Cosmos/query.js +115 -35
- package/dist/Store/Cosmos.d.ts +2 -2
- package/dist/Store/Cosmos.d.ts.map +1 -1
- package/dist/Store/Cosmos.js +343 -244
- package/dist/Store/Disk.d.ts +3 -3
- package/dist/Store/Disk.d.ts.map +1 -1
- package/dist/Store/Disk.js +76 -36
- package/dist/Store/Memory.d.ts +7 -4
- package/dist/Store/Memory.d.ts.map +1 -1
- package/dist/Store/Memory.js +251 -58
- package/dist/Store/SQL/Pg.d.ts +4 -0
- package/dist/Store/SQL/Pg.d.ts.map +1 -0
- package/dist/Store/SQL/Pg.js +233 -0
- package/dist/Store/SQL/query.d.ts +43 -0
- package/dist/Store/SQL/query.d.ts.map +1 -0
- package/dist/Store/SQL/query.js +478 -0
- package/dist/Store/SQL.d.ts +21 -0
- package/dist/Store/SQL.d.ts.map +1 -0
- package/dist/Store/SQL.js +450 -0
- package/dist/Store/codeFilter.d.ts +2 -2
- package/dist/Store/codeFilter.d.ts.map +1 -1
- package/dist/Store/codeFilter.js +6 -3
- package/dist/Store/index.d.ts +6 -3
- package/dist/Store/index.d.ts.map +1 -1
- package/dist/Store/index.js +18 -4
- package/dist/Store/service.d.ts +26 -8
- package/dist/Store/service.d.ts.map +1 -1
- package/dist/Store/service.js +25 -6
- package/dist/Store/utils.d.ts +3 -2
- package/dist/Store/utils.d.ts.map +1 -1
- package/dist/Store/utils.js +5 -5
- package/dist/Store.d.ts +1 -1
- package/dist/adapters/SQL/Model.d.ts +32 -43
- package/dist/adapters/SQL/Model.d.ts.map +1 -1
- package/dist/adapters/SQL/Model.js +30 -39
- package/dist/adapters/SQL.d.ts +1 -1
- package/dist/adapters/ServiceBus.d.ts +14 -11
- package/dist/adapters/ServiceBus.d.ts.map +1 -1
- package/dist/adapters/ServiceBus.js +30 -21
- package/dist/adapters/cosmos-client.d.ts +5 -3
- package/dist/adapters/cosmos-client.d.ts.map +1 -1
- package/dist/adapters/cosmos-client.js +5 -3
- package/dist/adapters/index.d.ts +8 -2
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +8 -2
- package/dist/adapters/logger.d.ts +2 -2
- package/dist/adapters/logger.d.ts.map +1 -1
- package/dist/adapters/memQueue.d.ts +5 -3
- package/dist/adapters/memQueue.d.ts.map +1 -1
- package/dist/adapters/memQueue.js +6 -5
- package/dist/adapters/mongo-client.d.ts +4 -3
- package/dist/adapters/mongo-client.d.ts.map +1 -1
- package/dist/adapters/mongo-client.js +5 -3
- package/dist/adapters/redis-client.d.ts +6 -3
- package/dist/adapters/redis-client.d.ts.map +1 -1
- package/dist/adapters/redis-client.js +7 -3
- package/dist/api/ContextProvider.d.ts +12 -8
- package/dist/api/ContextProvider.d.ts.map +1 -1
- package/dist/api/ContextProvider.js +9 -7
- package/dist/api/codec.d.ts +1 -1
- package/dist/api/internal/RequestContextMiddleware.d.ts +3 -3
- package/dist/api/internal/RequestContextMiddleware.d.ts.map +1 -1
- package/dist/api/internal/RequestContextMiddleware.js +10 -6
- package/dist/api/internal/auth.d.ts +45 -7
- package/dist/api/internal/auth.d.ts.map +1 -1
- package/dist/api/internal/auth.js +162 -29
- package/dist/api/internal/events.d.ts +6 -4
- package/dist/api/internal/events.d.ts.map +1 -1
- package/dist/api/internal/events.js +16 -9
- package/dist/api/internal/health.d.ts +1 -1
- package/dist/api/layerUtils.d.ts +10 -6
- package/dist/api/layerUtils.d.ts.map +1 -1
- package/dist/api/layerUtils.js +7 -6
- package/dist/api/middlewares.d.ts +1 -1
- package/dist/api/reportError.d.ts +2 -2
- package/dist/api/reportError.d.ts.map +1 -1
- package/dist/api/reportError.js +3 -2
- package/dist/api/routing/middleware/RouterMiddleware.d.ts +5 -4
- package/dist/api/routing/middleware/RouterMiddleware.d.ts.map +1 -1
- package/dist/api/routing/middleware/middleware.d.ts +42 -3
- package/dist/api/routing/middleware/middleware.d.ts.map +1 -1
- package/dist/api/routing/middleware/middleware.js +53 -17
- package/dist/api/routing/middleware.d.ts +1 -2
- package/dist/api/routing/middleware.d.ts.map +1 -1
- package/dist/api/routing/middleware.js +1 -2
- package/dist/api/routing/schema/jwt.d.ts +1 -1
- package/dist/api/routing/schema/jwt.d.ts.map +1 -1
- package/dist/api/routing/schema/jwt.js +3 -2
- package/dist/api/routing/tsort.d.ts +1 -1
- package/dist/api/routing/tsort.d.ts.map +1 -1
- package/dist/api/routing/utils.d.ts +4 -4
- package/dist/api/routing/utils.d.ts.map +1 -1
- package/dist/api/routing/utils.js +3 -2
- package/dist/api/routing.d.ts +84 -37
- package/dist/api/routing.d.ts.map +1 -1
- package/dist/api/routing.js +115 -45
- package/dist/api/setupRequest.d.ts +10 -6
- package/dist/api/setupRequest.d.ts.map +1 -1
- package/dist/api/setupRequest.js +15 -7
- package/dist/api/util.d.ts +1 -1
- package/dist/arbs.d.ts +2 -2
- package/dist/arbs.d.ts.map +1 -1
- package/dist/arbs.js +5 -3
- package/dist/errorReporter.d.ts +7 -5
- package/dist/errorReporter.d.ts.map +1 -1
- package/dist/errorReporter.js +22 -26
- package/dist/errors.d.ts +1 -1
- package/dist/fileUtil.d.ts +2 -2
- package/dist/fileUtil.d.ts.map +1 -1
- package/dist/fileUtil.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/logger/jsonLogger.d.ts +2 -2
- package/dist/logger/jsonLogger.d.ts.map +1 -1
- package/dist/logger/jsonLogger.js +4 -2
- package/dist/logger/logFmtLogger.d.ts +2 -2
- package/dist/logger/logFmtLogger.d.ts.map +1 -1
- package/dist/logger/logFmtLogger.js +2 -2
- package/dist/logger/shared.d.ts +2 -2
- package/dist/logger/shared.d.ts.map +1 -1
- package/dist/logger/shared.js +3 -3
- package/dist/logger.d.ts +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/otel.d.ts +75 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +65 -0
- package/dist/rateLimit.d.ts +12 -4
- package/dist/rateLimit.d.ts.map +1 -1
- package/dist/rateLimit.js +7 -12
- package/dist/test.d.ts +3 -3
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js +2 -2
- package/dist/vitest.d.ts +1 -1
- package/examples/query.ts +46 -38
- package/package.json +46 -37
- package/src/CUPS.ts +15 -11
- package/src/Emailer/Sendgrid.ts +21 -15
- package/src/Emailer/fake.ts +1 -1
- package/src/Emailer/service.ts +13 -3
- package/src/MainFiberSet.ts +9 -6
- package/src/Model/Repository/Registry.ts +34 -0
- package/src/Model/Repository/ext.ts +103 -11
- package/src/Model/Repository/internal/internal.ts +231 -149
- package/src/Model/Repository/legacy.ts +3 -1
- package/src/Model/Repository/makeRepo.ts +15 -10
- package/src/Model/Repository/service.ts +35 -23
- package/src/Model/Repository/validation.ts +5 -5
- package/src/Model/Repository.ts +1 -0
- package/src/Model/dsl.ts +5 -4
- package/src/Model/filter/types/path/eager.ts +1 -2
- package/src/Model/query/dsl.ts +353 -19
- package/src/Model/query/new-kid-interpreter.ts +211 -6
- package/src/Model.ts +1 -0
- package/src/QueueMaker/SQLQueue.ts +150 -153
- package/src/QueueMaker/errors.ts +3 -1
- package/src/QueueMaker/memQueue.ts +111 -105
- package/src/QueueMaker/sbqueue.ts +76 -88
- package/src/QueueMaker/service.ts +3 -1
- package/src/RequestContext.ts +15 -16
- package/src/RequestFiberSet.ts +8 -2
- package/src/Store/ContextMapContainer.ts +45 -2
- package/src/Store/Cosmos/query.ts +143 -44
- package/src/Store/Cosmos.ts +491 -350
- package/src/Store/Disk.ts +106 -66
- package/src/Store/Memory.ts +285 -87
- package/src/Store/SQL/Pg.ts +364 -0
- package/src/Store/SQL/query.ts +540 -0
- package/src/Store/SQL.ts +736 -0
- package/src/Store/codeFilter.ts +5 -2
- package/src/Store/index.ts +20 -3
- package/src/Store/service.ts +45 -10
- package/src/Store/utils.ts +25 -23
- package/src/adapters/SQL/Model.ts +42 -41
- package/src/adapters/ServiceBus.ts +131 -121
- package/src/adapters/cosmos-client.ts +4 -2
- package/src/adapters/index.ts +7 -0
- package/src/adapters/memQueue.ts +5 -4
- package/src/adapters/mongo-client.ts +4 -2
- package/src/adapters/redis-client.ts +6 -2
- package/src/api/ContextProvider.ts +17 -13
- package/src/api/internal/RequestContextMiddleware.ts +16 -5
- package/src/api/internal/auth.ts +248 -44
- package/src/api/internal/events.ts +19 -10
- package/src/api/layerUtils.ts +12 -8
- package/src/api/reportError.ts +2 -1
- package/src/api/routing/middleware/RouterMiddleware.ts +5 -4
- package/src/api/routing/middleware/middleware.ts +60 -15
- package/src/api/routing/middleware.ts +0 -2
- package/src/api/routing/schema/jwt.ts +2 -1
- package/src/api/routing/utils.ts +2 -1
- package/src/api/routing.ts +304 -131
- package/src/api/setupRequest.ts +31 -8
- package/src/arbs.ts +5 -3
- package/src/errorReporter.ts +65 -75
- package/src/fileUtil.ts +1 -1
- package/src/logger/jsonLogger.ts +3 -1
- package/src/logger/logFmtLogger.ts +1 -1
- package/src/logger/shared.ts +3 -2
- package/src/otel.ts +152 -0
- package/src/rateLimit.ts +34 -23
- package/src/test.ts +2 -2
- package/test/auth.test.ts +101 -0
- package/test/contextProvider.test.ts +14 -11
- package/test/controller.test.ts +25 -29
- package/test/dist/auth.test.d.ts.map +1 -0
- package/test/dist/contextProvider.test.d.ts.map +1 -1
- package/test/dist/controller.test.d.ts.map +1 -1
- package/test/dist/date-query.test.d.ts.map +1 -0
- package/test/dist/fixtures.d.ts +30 -12
- package/test/dist/fixtures.d.ts.map +1 -1
- package/test/dist/fixtures.js +17 -10
- package/test/dist/query.test.d.ts.map +1 -1
- package/test/dist/rawQuery.test.d.ts.map +1 -1
- package/test/dist/repository-ext.test.d.ts.map +1 -0
- package/test/dist/requires.test.d.ts.map +1 -1
- package/test/dist/router-generator.test.d.ts.map +1 -0
- package/test/dist/routing-interruptibility.test.d.ts.map +1 -0
- package/test/dist/rpc-e2e-invalidation.test.d.ts.map +1 -0
- package/test/dist/rpc-multi-middleware.test.d.ts.map +1 -1
- package/test/dist/rpc-stream-fullstack.test.d.ts.map +1 -0
- package/test/dist/sql-store.test.d.ts.map +1 -0
- package/test/fixtures.ts +16 -9
- package/test/layerUtils.test.ts +1 -1
- package/test/query.test.ts +819 -38
- package/test/rawQuery.test.ts +312 -20
- package/test/repository-ext.test.ts +62 -0
- package/test/requires.test.ts +10 -5
- package/test/router-generator.test.ts +187 -0
- package/test/routing-interruptibility.test.ts +66 -0
- package/test/rpc-e2e-invalidation.test.ts +256 -0
- package/test/rpc-multi-middleware.test.ts +84 -9
- package/test/rpc-stream-fullstack.test.ts +304 -0
- package/test/sql-store.test.ts +1592 -0
- package/test/validateSample.test.ts +17 -12
- package/tsconfig.examples.json +1 -1
- package/tsconfig.json +0 -1
- package/tsconfig.json.bak +2 -2
- package/tsconfig.src.json +35 -35
- package/tsconfig.test.json +2 -2
- package/dist/Operations.d.ts +0 -55
- package/dist/Operations.d.ts.map +0 -1
- package/dist/Operations.js +0 -102
- package/dist/OperationsRepo.d.ts +0 -41
- package/dist/OperationsRepo.d.ts.map +0 -1
- package/dist/OperationsRepo.js +0 -14
- package/eslint.config.mjs +0 -24
- package/src/Operations.ts +0 -235
- package/src/OperationsRepo.ts +0 -16
package/src/api/internal/auth.ts
CHANGED
|
@@ -1,42 +1,246 @@
|
|
|
1
|
-
|
|
2
|
-
/* eslint-disable unused-imports/no-unused-vars */
|
|
3
|
-
import { Data, Effect } from "effect-app"
|
|
1
|
+
import * as Effect from "effect-app/Effect"
|
|
4
2
|
import { HttpHeaders, HttpMiddleware, HttpServerRequest, HttpServerResponse } from "effect-app/http"
|
|
5
|
-
import
|
|
3
|
+
import * as Option from "effect-app/Option"
|
|
4
|
+
import * as Data from "effect/Data"
|
|
5
|
+
import { createRemoteJWKSet, jwtVerify } from "jose"
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const getHeaders = (error: string, description: string, scopes?: ReadonlyArray<string>) => ({
|
|
8
|
+
"WWW-Authenticate": `Bearer realm="api", error="${error}", error_description="${description.replace(/"/g, "'")}"${
|
|
9
|
+
scopes ? `, scope="${scopes.join(" ")}"` : ""
|
|
10
|
+
}`
|
|
11
|
+
})
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
13
|
+
export class UnauthorizedError extends Error {
|
|
14
|
+
readonly status: number = 401
|
|
15
|
+
readonly statusCode: number = 401
|
|
16
|
+
headers = { "WWW-Authenticate": "Bearer realm=\"api\"" }
|
|
17
|
+
|
|
18
|
+
constructor(message = "Unauthorized") {
|
|
19
|
+
super(message)
|
|
20
|
+
this.name = this.constructor.name
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class InvalidRequestError extends UnauthorizedError {
|
|
25
|
+
readonly code: string
|
|
26
|
+
override readonly status = 400
|
|
27
|
+
override readonly statusCode = 400
|
|
28
|
+
|
|
29
|
+
constructor(message = "Invalid Request", useErrorCode = true) {
|
|
30
|
+
super(message)
|
|
31
|
+
this.code = useErrorCode ? "invalid_request" : ""
|
|
32
|
+
if (useErrorCode) {
|
|
33
|
+
this.headers = getHeaders(this.code, this.message)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class InvalidTokenError extends UnauthorizedError {
|
|
39
|
+
readonly code = "invalid_token"
|
|
40
|
+
|
|
41
|
+
constructor(message = "Invalid Token") {
|
|
42
|
+
super(message)
|
|
43
|
+
this.headers = getHeaders(this.code, this.message)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class InsufficientScopeError extends UnauthorizedError {
|
|
48
|
+
readonly code = "insufficient_scope"
|
|
49
|
+
override readonly status = 403
|
|
50
|
+
override readonly statusCode = 403
|
|
51
|
+
|
|
52
|
+
constructor(scopes?: ReadonlyArray<string>, message = "Insufficient Scope") {
|
|
53
|
+
super(message)
|
|
54
|
+
this.headers = getHeaders(this.code, this.message, scopes)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface JwtVerifierOptions {
|
|
59
|
+
readonly audience?: string | Array<string> | ReadonlyArray<string>
|
|
60
|
+
readonly clockTolerance?: number
|
|
61
|
+
readonly issuer?: string
|
|
62
|
+
readonly issuerBaseURL?: string
|
|
63
|
+
readonly jwksUri?: string
|
|
64
|
+
readonly maxTokenAge?: number
|
|
65
|
+
readonly secret?: string
|
|
66
|
+
readonly strict?: boolean
|
|
67
|
+
readonly tokenSigningAlg?: string
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface AuthOptions extends JwtVerifierOptions {
|
|
71
|
+
readonly authRequired?: boolean
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type Config = AuthOptions
|
|
75
|
+
|
|
76
|
+
type JwtError = InsufficientScopeError | InvalidRequestError | InvalidTokenError | UnauthorizedError
|
|
77
|
+
|
|
78
|
+
type ResolvedConfigBase = {
|
|
79
|
+
readonly audience: string | Array<string> | undefined
|
|
80
|
+
readonly clockTolerance: number
|
|
81
|
+
readonly issuer: string | undefined
|
|
82
|
+
readonly maxTokenAge: number | undefined
|
|
83
|
+
readonly strict: boolean
|
|
84
|
+
readonly tokenSigningAlg: string | undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type ResolvedConfig =
|
|
88
|
+
& ResolvedConfigBase
|
|
89
|
+
& (
|
|
90
|
+
| {
|
|
91
|
+
readonly key: ReturnType<typeof createRemoteJWKSet>
|
|
92
|
+
readonly keyType: "jwks"
|
|
93
|
+
}
|
|
94
|
+
| {
|
|
95
|
+
readonly key: Uint8Array
|
|
96
|
+
readonly keyType: "secret"
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null
|
|
101
|
+
|
|
102
|
+
const getErrorMessage = (error: unknown) => error instanceof Error ? error.message : String(error)
|
|
103
|
+
|
|
104
|
+
const normalizeAudience = (audience: Config["audience"]): string | Array<string> | undefined =>
|
|
105
|
+
Array.isArray(audience) ? Array.from(audience) : audience as string | undefined
|
|
106
|
+
|
|
107
|
+
const buildDiscoveryUrl = (issuerBaseURL: string) => {
|
|
108
|
+
const url = new URL(issuerBaseURL)
|
|
109
|
+
if (!url.pathname.includes("/.well-known/")) {
|
|
110
|
+
url.pathname = url.pathname.endsWith("/")
|
|
111
|
+
? `${url.pathname}.well-known/openid-configuration`
|
|
112
|
+
: `${url.pathname}/.well-known/openid-configuration`
|
|
113
|
+
}
|
|
114
|
+
url.search = ""
|
|
115
|
+
url.hash = ""
|
|
116
|
+
return url
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const fetchDiscoveryDocumentPromise = async (issuerBaseURL: string) => {
|
|
120
|
+
const response = await fetch(buildDiscoveryUrl(issuerBaseURL))
|
|
121
|
+
if (!response.ok) {
|
|
122
|
+
throw new Error(`Failed to fetch authorization server metadata: ${response.status}`)
|
|
123
|
+
}
|
|
124
|
+
const json = await response.json()
|
|
125
|
+
if (!isRecord(json) || typeof json["issuer"] !== "string" || typeof json["jwks_uri"] !== "string") {
|
|
126
|
+
throw new Error("Invalid authorization server metadata")
|
|
127
|
+
}
|
|
128
|
+
return { issuer: json["issuer"], jwksUri: json["jwks_uri"] }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const getAuthorizationToken = (headers: HttpHeaders.Headers, authRequired: boolean) => {
|
|
132
|
+
const authorization = HttpHeaders.get(headers, "authorization")
|
|
133
|
+
if (Option.isNone(authorization)) {
|
|
134
|
+
return authRequired ? Effect.fail(new UnauthorizedError()) : Effect.succeed(Option.none<string>())
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const [scheme, token] = authorization.value.split(" ")
|
|
138
|
+
if (!scheme || !token || scheme.toLowerCase() !== "bearer") {
|
|
139
|
+
return Effect.fail(new InvalidRequestError("", false))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return Effect.succeed(Option.some(token))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const makeResolveConfig = (config: Config) => {
|
|
146
|
+
let cached: Promise<ResolvedConfig> | undefined
|
|
147
|
+
|
|
148
|
+
return Effect.tryPromise({
|
|
149
|
+
try: () => {
|
|
150
|
+
if (!cached) {
|
|
151
|
+
cached = (async (): Promise<ResolvedConfig> => {
|
|
152
|
+
const discovery = config.issuerBaseURL
|
|
153
|
+
? await fetchDiscoveryDocumentPromise(config.issuerBaseURL)
|
|
154
|
+
: undefined
|
|
155
|
+
|
|
156
|
+
const issuer = config.issuer ?? discovery?.issuer
|
|
157
|
+
const jwksUri = config.jwksUri ?? discovery?.jwksUri
|
|
158
|
+
const secret = config.secret
|
|
159
|
+
const base = {
|
|
160
|
+
audience: normalizeAudience(config.audience),
|
|
161
|
+
clockTolerance: config.clockTolerance ?? 5,
|
|
162
|
+
issuer,
|
|
163
|
+
maxTokenAge: config.maxTokenAge,
|
|
164
|
+
strict: config.strict ?? false,
|
|
165
|
+
tokenSigningAlg: config.tokenSigningAlg
|
|
166
|
+
} satisfies ResolvedConfigBase
|
|
167
|
+
|
|
168
|
+
if (!issuer && !secret) {
|
|
169
|
+
throw new InvalidRequestError("JWT config requires 'issuer', 'issuerBaseURL', or 'secret'")
|
|
29
170
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
171
|
+
|
|
172
|
+
if (!secret) {
|
|
173
|
+
if (!jwksUri) {
|
|
174
|
+
throw new InvalidRequestError("JWT config requires 'jwksUri', 'issuerBaseURL', or 'secret'")
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
...base,
|
|
179
|
+
key: createRemoteJWKSet(new URL(jwksUri)),
|
|
180
|
+
keyType: "jwks"
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
...base,
|
|
186
|
+
key: new TextEncoder().encode(secret),
|
|
187
|
+
keyType: "secret"
|
|
188
|
+
}
|
|
189
|
+
})()
|
|
38
190
|
}
|
|
191
|
+
|
|
192
|
+
return cached
|
|
193
|
+
},
|
|
194
|
+
catch: (error) =>
|
|
195
|
+
error instanceof InvalidRequestError || error instanceof InvalidTokenError
|
|
196
|
+
? error
|
|
197
|
+
: new InvalidTokenError(getErrorMessage(error))
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const verifyToken =
|
|
202
|
+
(resolveConfig: Effect.Effect<ResolvedConfig, InvalidRequestError | InvalidTokenError>) => (token: string) =>
|
|
203
|
+
resolveConfig.pipe(
|
|
204
|
+
Effect.flatMap((config) => {
|
|
205
|
+
const options = {
|
|
206
|
+
clockTolerance: config.clockTolerance,
|
|
207
|
+
...(config.tokenSigningAlg ? { algorithms: [config.tokenSigningAlg] } : {}),
|
|
208
|
+
...(config.audience !== undefined ? { audience: config.audience } : {}),
|
|
209
|
+
...(config.issuer !== undefined ? { issuer: config.issuer } : {}),
|
|
210
|
+
...(config.maxTokenAge !== undefined ? { maxTokenAge: config.maxTokenAge } : {})
|
|
211
|
+
}
|
|
212
|
+
const verified = config.keyType === "jwks"
|
|
213
|
+
? Effect.tryPromise({
|
|
214
|
+
try: () => jwtVerify(token, config.key, options).then(({ protectedHeader }) => ({ protectedHeader })),
|
|
215
|
+
catch: (error) => new InvalidTokenError(getErrorMessage(error))
|
|
216
|
+
})
|
|
217
|
+
: Effect.tryPromise({
|
|
218
|
+
try: () => jwtVerify(token, config.key, options).then(({ protectedHeader }) => ({ protectedHeader })),
|
|
219
|
+
catch: (error) => new InvalidTokenError(getErrorMessage(error))
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
return verified.pipe(
|
|
223
|
+
Effect.flatMap(({ protectedHeader }) => {
|
|
224
|
+
const typ = protectedHeader.typ?.toLowerCase().replace(/^application\//, "")
|
|
225
|
+
return config.strict && typ !== "at+jwt"
|
|
226
|
+
? Effect.fail(new InvalidTokenError("Unexpected 'typ' value"))
|
|
227
|
+
: Effect.void
|
|
228
|
+
})
|
|
229
|
+
)
|
|
230
|
+
})
|
|
39
231
|
)
|
|
232
|
+
|
|
233
|
+
export const checkJWTI = (config: Config) => {
|
|
234
|
+
const resolveConfig = makeResolveConfig(config)
|
|
235
|
+
const verify = verifyToken(resolveConfig)
|
|
236
|
+
|
|
237
|
+
return Effect.fnUntraced(function*(headers: HttpHeaders.Headers) {
|
|
238
|
+
const token = yield* getAuthorizationToken(headers, config.authRequired !== false)
|
|
239
|
+
if (Option.isNone(token)) {
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
yield* verify(token.value)
|
|
40
244
|
})
|
|
41
245
|
}
|
|
42
246
|
|
|
@@ -45,24 +249,24 @@ export const checkJwt = (config: Config) => {
|
|
|
45
249
|
return HttpMiddleware.make((app) =>
|
|
46
250
|
Effect.gen(function*() {
|
|
47
251
|
const req = yield* HttpServerRequest.HttpServerRequest
|
|
48
|
-
const response = yield* check(req.headers).pipe(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
252
|
+
const response = yield* check(req.headers).pipe(
|
|
253
|
+
Effect.catch((error: JwtError) =>
|
|
254
|
+
HttpServerResponse.json({ message: error.message }, {
|
|
255
|
+
status: error.status,
|
|
256
|
+
headers: HttpHeaders.fromInput(error.headers)
|
|
257
|
+
})
|
|
258
|
+
)
|
|
259
|
+
)
|
|
260
|
+
|
|
54
261
|
if (response) {
|
|
55
262
|
return response
|
|
56
263
|
}
|
|
264
|
+
|
|
57
265
|
return yield* app
|
|
58
266
|
})
|
|
59
267
|
)
|
|
60
268
|
}
|
|
61
269
|
|
|
62
270
|
export class JWTError extends Data.TaggedClass("JWTError")<{
|
|
63
|
-
error:
|
|
64
|
-
| InsufficientScopeError
|
|
65
|
-
| InvalidRequestError
|
|
66
|
-
| InvalidTokenError
|
|
67
|
-
| UnauthorizedError
|
|
271
|
+
error: JwtError
|
|
68
272
|
}> {}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Effect from "effect-app/Effect"
|
|
2
2
|
import { HttpHeaders, HttpServerResponse } from "effect-app/http"
|
|
3
|
+
import * as S from "effect-app/Schema"
|
|
4
|
+
import * as Duration from "effect/Duration"
|
|
5
|
+
import { pipe } from "effect/Function"
|
|
6
|
+
import * as Schedule from "effect/Schedule"
|
|
7
|
+
import * as Stream from "effect/Stream"
|
|
3
8
|
import { reportError } from "../../errorReporter.js"
|
|
9
|
+
import { storeId } from "../../Store/Memory.js"
|
|
4
10
|
import { setupRequestContextFromCurrent } from "../setupRequest.js"
|
|
5
11
|
|
|
6
12
|
// Tell the client to retry every 10 seconds if connectivity is lost
|
|
@@ -9,29 +15,32 @@ const keepAlive = Stream.fromEffectSchedule(Effect.succeed(":keep-alive"), Sched
|
|
|
9
15
|
|
|
10
16
|
let connId = BigInt(0)
|
|
11
17
|
|
|
12
|
-
export const makeSSE = <A extends { id: any }, SI,
|
|
13
|
-
schema: S.Codec<A, SI,
|
|
18
|
+
export const makeSSE = <A extends { id: any }, SI, SRD, SRE>(
|
|
19
|
+
schema: S.Codec<A, SI, SRD, SRE>
|
|
14
20
|
) =>
|
|
15
21
|
<E, R>(events: Stream.Stream<{ evt: A; namespace: string }, E, R>) =>
|
|
16
22
|
Effect
|
|
17
23
|
.gen(function*() {
|
|
18
24
|
const id = connId++
|
|
19
|
-
const ctx = yield* Effect.
|
|
25
|
+
const ctx = yield* Effect.context<R | SRD | SRE>()
|
|
20
26
|
const res = HttpServerResponse.stream(
|
|
21
27
|
// workaround for different scoped behaviour for streams in Bun
|
|
22
28
|
// https://discord.com/channels/795981131316985866/1098177242598756412/1389646879675125861
|
|
23
29
|
Effect
|
|
24
30
|
.gen(function*() {
|
|
25
|
-
|
|
26
|
-
yield* Effect.
|
|
27
|
-
yield* Effect.
|
|
31
|
+
const ns = yield* storeId
|
|
32
|
+
yield* Effect.annotateCurrentSpan({ "network.connection.id": id.toString() })
|
|
33
|
+
yield* Effect.logInfo("$ start listening to events, id: " + id.toString() + ", ns: " + ns)
|
|
34
|
+
yield* Effect.addFinalizer(() =>
|
|
35
|
+
Effect.logInfo("$ end listening to events, id: " + id.toString() + ", ns: " + ns)
|
|
36
|
+
)
|
|
28
37
|
|
|
29
38
|
const enc = new TextEncoder()
|
|
30
39
|
|
|
31
|
-
const encode = S.encodeEffect(S.fromJsonString(schema))
|
|
40
|
+
const encode = S.encodeEffect(S.fromJsonString(S.toCodecJson(schema)))
|
|
32
41
|
|
|
33
42
|
const eventStream = Stream.mapEffect(
|
|
34
|
-
events,
|
|
43
|
+
Stream.filter(events, (_) => _.namespace === ns),
|
|
35
44
|
(_) =>
|
|
36
45
|
encode(_.evt)
|
|
37
46
|
.pipe(Effect.map((data) => `id: ${_.evt.id}\ndata: ${data}`))
|
|
@@ -42,7 +51,7 @@ export const makeSSE = <A extends { id: any }, SI, SR>(
|
|
|
42
51
|
Stream.merge(keepAlive),
|
|
43
52
|
// Keep this unary so pipe receives a function, not a Stream value.
|
|
44
53
|
(self) => Stream.merge(self, eventStream, { haltStrategy: "either" }),
|
|
45
|
-
Stream.tapCause((cause) => Effect.logError("SSE error", cause)),
|
|
54
|
+
Stream.tapCause((cause) => Effect.logError("SSE error, id: " + id.toString() + ", ns: " + ns, cause)),
|
|
46
55
|
Stream.map((_) => enc.encode(_ + "\n\n"))
|
|
47
56
|
)
|
|
48
57
|
|
package/src/api/layerUtils.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import
|
|
2
|
+
import type { NonEmptyReadonlyArray } from "effect-app/Array"
|
|
3
|
+
import * as Context from "effect-app/Context"
|
|
4
|
+
import * as Effect from "effect-app/Effect"
|
|
5
|
+
import type * as Layer from "effect-app/Layer"
|
|
6
|
+
import * as Option from "effect-app/Option"
|
|
3
7
|
import { InfraLogger } from "../logger.js"
|
|
4
8
|
|
|
5
9
|
// TODO: These LayerUtils are flaky, like in dependencies as a readonly array, it breaks when there are two entries
|
|
@@ -27,7 +31,7 @@ export namespace LayerUtils {
|
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
export type ContextTagWithDefault<Id, A, LayerE, LayerR> =
|
|
30
|
-
&
|
|
34
|
+
& Context.Service<Id, A>
|
|
31
35
|
& {
|
|
32
36
|
Default: Layer.Layer<Id, LayerE, LayerR>
|
|
33
37
|
}
|
|
@@ -36,29 +40,29 @@ export namespace ContextTagWithDefault {
|
|
|
36
40
|
export type Base<A> = ContextTagWithDefault<any, A, any, any>
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
export type GetContext<T> = T extends
|
|
43
|
+
export type GetContext<T> = T extends Context.Context<infer Y> ? Y : never
|
|
40
44
|
|
|
41
45
|
export const mergeContexts = Effect.fnUntraced(
|
|
42
46
|
function*<
|
|
43
47
|
T extends readonly {
|
|
44
48
|
maker: any
|
|
45
|
-
handle: Effect.Effect<
|
|
49
|
+
handle: Effect.Effect<Context.Context<any> | Option.Option<Context.Context<any>>>
|
|
46
50
|
}[]
|
|
47
51
|
>(
|
|
48
52
|
makers: T
|
|
49
53
|
) {
|
|
50
|
-
let context =
|
|
54
|
+
let context = Context.empty()
|
|
51
55
|
for (const mw of makers) {
|
|
52
56
|
const ctx = yield* mw.handle.pipe(Effect.provide(context))
|
|
53
|
-
const moreContext =
|
|
57
|
+
const moreContext = Context.isContext(ctx) ? Option.some(ctx) : ctx
|
|
54
58
|
yield* InfraLogger.logDebug(
|
|
55
59
|
"Built dynamic context for middleware" + (mw.maker.key ?? mw.maker),
|
|
56
60
|
Option.map(moreContext, (c) => (c as any).toJSON().services)
|
|
57
61
|
)
|
|
58
62
|
if (moreContext.value) {
|
|
59
|
-
context =
|
|
63
|
+
context = Context.merge(context, moreContext.value)
|
|
60
64
|
}
|
|
61
65
|
}
|
|
62
|
-
return context as
|
|
66
|
+
return context as Context.Context<Effect.Success<T[number]["handle"]>>
|
|
63
67
|
}
|
|
64
68
|
)
|
package/src/api/reportError.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
-
import
|
|
4
|
+
import type * as Context from "effect-app/Context"
|
|
5
|
+
import type * as Layer from "effect-app/Layer"
|
|
5
6
|
import { type GetContextConfig, type RpcContextMap } from "effect-app/rpc/RpcContextMap"
|
|
6
7
|
import { type RpcMiddlewareV4 } from "effect-app/rpc/RpcMiddleware"
|
|
7
8
|
// module:
|
|
8
9
|
//
|
|
9
10
|
|
|
10
|
-
// v4: middleware tags are
|
|
11
|
+
// v4: middleware tags are Context.Service (not Effect) — they carry the RpcMiddlewareV4 as their service Shape
|
|
11
12
|
export type RouterMiddleware<
|
|
12
13
|
Self,
|
|
13
14
|
RequestContextMap extends Record<string, RpcContextMap.Any>, // what services will the middlware provide dynamically to the next, or raise errors.
|
|
@@ -18,9 +19,9 @@ export type RouterMiddleware<
|
|
|
18
19
|
_ContextProviderR, // what the context provider requires
|
|
19
20
|
RequestContextId
|
|
20
21
|
> =
|
|
21
|
-
&
|
|
22
|
+
& Context.Service<Self, RpcMiddlewareV4<ContextProviderA, ContextProviderE, never>>
|
|
22
23
|
& {
|
|
23
24
|
readonly Default: Layer.Layer<Self, MakeMiddlewareE, MakeMiddlewareR>
|
|
24
|
-
readonly requestContext:
|
|
25
|
+
readonly requestContext: Context.Service<RequestContextId, GetContextConfig<RequestContextMap>>
|
|
25
26
|
readonly requestContextMap: RequestContextMap
|
|
26
27
|
}
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { Cause, Config, Effect, Layer, Schema } from "effect"
|
|
3
2
|
import { ConfigureInterruptibilityMiddleware, DevMode, DevModeMiddleware, LoggerMiddleware, RequestCacheMiddleware } from "effect-app/middleware"
|
|
3
|
+
import { RpcContextMap, type RpcMiddleware } from "effect-app/rpc"
|
|
4
4
|
import { pretty } from "effect-app/utils"
|
|
5
|
+
import * as Array from "effect/Array"
|
|
6
|
+
import * as Cause from "effect/Cause"
|
|
7
|
+
import * as Config from "effect/Config"
|
|
8
|
+
import * as Context from "effect/Context"
|
|
9
|
+
import * as Effect from "effect/Effect"
|
|
10
|
+
import * as Layer from "effect/Layer"
|
|
11
|
+
import * as Schema from "effect/Schema"
|
|
12
|
+
import { type Rpc } from "effect/unstable/rpc"
|
|
5
13
|
import { logError, reportError } from "../../../errorReporter.js"
|
|
6
14
|
import { InfraLogger } from "../../../logger.js"
|
|
7
|
-
import {
|
|
15
|
+
import { WithNsTransaction } from "../../../Store/SQL.js"
|
|
8
16
|
|
|
9
17
|
const logRequestError = logError("Request")
|
|
10
18
|
const reportRequestError = reportError("Request")
|
|
@@ -26,22 +34,20 @@ export const RequestCacheMiddlewareLive = Layer.succeed(
|
|
|
26
34
|
const isOptimisticConcurrencyException = (input: unknown) =>
|
|
27
35
|
typeof input === "object" && input !== null && "_tag" in input && input._tag === "OptimisticConcurrencyException"
|
|
28
36
|
|
|
37
|
+
export const RequestType = Context.Reference<"command" | "query">(
|
|
38
|
+
"@effect-app/infra/api/routing/RequestType",
|
|
39
|
+
{ defaultValue: () => "query" }
|
|
40
|
+
)
|
|
41
|
+
|
|
29
42
|
export const ConfigureInterruptibilityMiddlewareLive = Layer.effect(
|
|
30
43
|
ConfigureInterruptibilityMiddleware,
|
|
31
44
|
Effect.gen(function*() {
|
|
32
|
-
const cache = new Map()
|
|
33
|
-
const getCached = (key: string, schema: Schema.Top) => {
|
|
34
|
-
const existing = cache.get(key)
|
|
35
|
-
if (existing) return existing
|
|
36
|
-
const n = determineMethod(key, schema)
|
|
37
|
-
cache.set(key, n)
|
|
38
|
-
return n
|
|
39
|
-
}
|
|
40
45
|
return (effect, { rpc }) => {
|
|
41
|
-
const
|
|
46
|
+
const requestType = Context.get(rpc.annotations, RequestType)
|
|
47
|
+
const isCommand = requestType === "command"
|
|
42
48
|
|
|
43
|
-
effect = isCommand
|
|
44
|
-
? Effect.retry(
|
|
49
|
+
effect = isCommand
|
|
50
|
+
? Effect.retry(effect, { times: 1, while: isOptimisticConcurrencyException })
|
|
45
51
|
: Effect.interruptible(effect)
|
|
46
52
|
|
|
47
53
|
return effect
|
|
@@ -57,8 +63,8 @@ export const LoggerMiddlewareLive = Layer
|
|
|
57
63
|
return (effect, { headers, payload, rpc }) =>
|
|
58
64
|
Effect
|
|
59
65
|
.annotateCurrentSpan({
|
|
60
|
-
"
|
|
61
|
-
"
|
|
66
|
+
"rpc.method": rpc._tag,
|
|
67
|
+
"rpc.request.payload": typeof payload === "object" && payload !== null
|
|
62
68
|
? Object.entries(payload).reduce((prev, [key, value]: [string, unknown]) => {
|
|
63
69
|
prev[key] = key === "password"
|
|
64
70
|
? "<redacted>"
|
|
@@ -126,3 +132,42 @@ export const DefaultGenericMiddlewaresLive = Layer.mergeAll(
|
|
|
126
132
|
LoggerMiddlewareLive,
|
|
127
133
|
DevModeMiddlewareLive
|
|
128
134
|
)
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Config entry for `RequestContextMap` that controls per-RPC transaction wrapping.
|
|
138
|
+
* Defaults to `false` (no transaction). Set `requiresTransaction: true` on a route to enable.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* class RequestContextMap extends RpcContextMap.makeMap({
|
|
143
|
+
* requiresTransaction: requiresTransactionConfig,
|
|
144
|
+
* // ...
|
|
145
|
+
* }) {}
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export const requiresTransactionConfig = RpcContextMap.makeCustom()(Schema.Never, false)
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Creates the middleware Effect for SQL transaction wrapping.
|
|
152
|
+
* Requires `WithNsTransaction` service.
|
|
153
|
+
* Reads `requiresTransaction` from the RPC config; defaults to `false`.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const SqlTransactionMiddlewareLive = Layer.effect(
|
|
158
|
+
* SqlTransactionMiddleware,
|
|
159
|
+
* makeSqlTransactionMiddleware(RequestContextMap)
|
|
160
|
+
* )
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export const makeSqlTransactionMiddleware = Effect.fnUntraced(function*(
|
|
164
|
+
rcm: { getConfig: (rpc: Rpc.AnyWithProps) => { readonly requiresTransaction?: boolean } }
|
|
165
|
+
) {
|
|
166
|
+
const withTx = yield* WithNsTransaction
|
|
167
|
+
const mw: RpcMiddleware.RpcMiddlewareV4<never, never, never> = (effect, { rpc }) => {
|
|
168
|
+
const { requiresTransaction } = rcm.getConfig(rpc)
|
|
169
|
+
if (requiresTransaction !== true) return effect
|
|
170
|
+
return withTx(effect)
|
|
171
|
+
}
|
|
172
|
+
return mw
|
|
173
|
+
})
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
import { Effect, Option } from "effect"
|
|
4
3
|
import * as S from "effect-app/Schema"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Option from "effect/Option"
|
|
5
6
|
import { jwtDecode, type JwtDecodeOptions } from "jwt-decode"
|
|
6
7
|
|
|
7
8
|
export const parseJwt = <Sch extends S.Top>(
|
package/src/api/routing/utils.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as S from "effect-app/Schema"
|
|
2
2
|
import type { AST } from "effect-app/Schema"
|
|
3
|
+
import * as SchemaAST from "effect/SchemaAST"
|
|
3
4
|
|
|
4
5
|
const get = ["Get", "Index", "List", "All", "Find", "Search"]
|
|
5
6
|
const del = ["Delete", "Remove", "Destroy"]
|