Adding Cache-Control Headers to an RPC Response in Effect Typescript
How would you add headers (e.g.
Cache-ControlCache-Control) to an HTTP response for an RPC / Group? I tried to modify the response based of the README example and @effect/platform@effect/platform README but to no availconst myHeaderSetter = HttpMiddleware.make((app) =>
Effect.gen(function* () {
console.log("LOGGED");
const res = yield* app;
return res.pipe(HttpServerResponse.setHeader("FOO", "BAR"));
})
);
// Create the RPC server layer
const RpcLayer = RpcServer.layer(UserRpcs).pipe(Layer.provide(UsersLive));
// Choose the protocol and serialization format
const HttpProtocol = RpcServer.layerProtocolHttp({
path: "/rpc",
}).pipe(Layer.provide(RpcSerialization.layerJson));
// Create the main server layer
const Main = HttpRouter.Default.serve(myHeaderSetter).pipe(
Layer.provide(RpcLayer),
Layer.provide(HttpProtocol),
);
listen(Main, 3000);const myHeaderSetter = HttpMiddleware.make((app) =>
Effect.gen(function* () {
console.log("LOGGED");
const res = yield* app;
return res.pipe(HttpServerResponse.setHeader("FOO", "BAR"));
})
);
// Create the RPC server layer
const RpcLayer = RpcServer.layer(UserRpcs).pipe(Layer.provide(UsersLive));
// Choose the protocol and serialization format
const HttpProtocol = RpcServer.layerProtocolHttp({
path: "/rpc",
}).pipe(Layer.provide(RpcSerialization.layerJson));
// Create the main server layer
const Main = HttpRouter.Default.serve(myHeaderSetter).pipe(
Layer.provide(RpcLayer),
Layer.provide(HttpProtocol),
);
listen(Main, 3000);➜ effect-playgroun git:(main) ✗ curl -X POST http://localhost:3000/rpc -v \
-H "Content-Type: application/json" \
-d $'{"_tag": "Request", "id": "123", "tag": "UserList", "payload": {}, "traceId": "traceId", "spanId": "spanId", "sampled": true, "headers": {} }\n'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /rpc HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 142
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-type: application/json
< content-length: 157
< vary: Accept-Encoding
< date: Tue, 13 May 2025 15:34:52 GMT
<
* Connection #0 to host localhost left intact
[{"_tag":"Chunk","requestId":"123","values":[{"id":"1","name":"Alice"},{"id":"2","name":"Bob"}]},{"_tag":"Exit","requestId":"123","exit":{"_tag":"Success"}}]➜ effect-playgroun git:(main) ✗ curl -X POST http://localhost:3000/rpc -v \
-H "Content-Type: application/json" \
-d $'{"_tag": "Request", "id": "123", "tag": "UserList", "payload": {}, "traceId": "traceId", "spanId": "spanId", "sampled": true, "headers": {} }\n'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /rpc HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 142
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-type: application/json
< content-length: 157
< vary: Accept-Encoding
< date: Tue, 13 May 2025 15:34:52 GMT
<
* Connection #0 to host localhost left intact
[{"_tag":"Chunk","requestId":"123","values":[{"id":"1","name":"Alice"},{"id":"2","name":"Bob"}]},{"_tag":"Exit","requestId":"123","exit":{"_tag":"Success"}}]