forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreq.ts
More file actions
36 lines (31 loc) · 1.34 KB
/
req.ts
File metadata and controls
36 lines (31 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { NotLoggedInError, UnauthorizedError } from "@effect-app/infra/errors"
import type { Role } from "Domain/User.js"
import { Duration, Layer, Request } from "effect-app"
import type { RPCContextMap } from "effect-app/client"
import { makeRpcClient } from "effect-app/client"
import { makeClientFor } from "effect-app/client/clientFor"
type CTXMap = {
// we put `never`, because we can't access this service here in the client, and we also don't need to
// TODO: a base map for client, that the server extends
allowAnonymous: RPCContextMap.Inverted<"userProfile", never, typeof NotLoggedInError>
// TODO: not boolean but `string[]`
requireRoles: RPCContextMap.Custom<"", void, typeof UnauthorizedError, Array<string>>
}
export type RequestConfig = {
/** Disable authentication requirement */
allowAnonymous?: true
/** Control the roles that are required to access the resource */
allowRoles?: readonly Role[]
}
export const { TaggedRequest: Req } = makeRpcClient<RequestConfig, CTXMap>({
allowAnonymous: NotLoggedInError,
requireRoles: UnauthorizedError
})
export const RequestCacheLayers = Layer.mergeAll(
Layer.setRequestCache(
Request.makeCache({ capacity: 500, timeToLive: Duration.hours(8) })
),
Layer.setRequestCaching(true),
Layer.setRequestBatching(true)
)
export const clientFor = makeClientFor(RequestCacheLayers)