tRPCttRPC
Powered by
isitayushI
tRPC•3y ago•
2 replies
isitayush

awaiting for procedure & logging the response.

Hi, I was wondering if there is a way to handle the return object via the post-middleware's? I know we could do something like,
const logMiddleware = t.middleware(async ({ ctx, next }) => {
   const res = await next();
   // insert logging here
   return res;
);
const logMiddleware = t.middleware(async ({ ctx, next }) => {
   const res = await next();
   // insert logging here
   return res;
);
However, I want to access the returned result with an event name & log it. This event name could be returned by the procedure itself as one of the key's of
res
res
. The problem is I can't access the
res
res
object as it's of type
MiddlewareResult
MiddlewareResult
& is the whole request itself that is returned from a procedure's query/mutation. How should I go about solving this? Is there a way I could destruct the data returned from
next()
next()
and wrap it in a defined type say something similar to,
type Response = {
  event: "requested_data"; // res.response.type
  request: { ip: res.req.headers.forwarded, ua: ...},
  time: 18214931949,
  data: { type: "query/mutation", path: "procedure path", ctx: res.ctx, input: res.input, response: res.response.message  
 },
}
type Response = {
  event: "requested_data"; // res.response.type
  request: { ip: res.req.headers.forwarded, ua: ...},
  time: 18214931949,
  data: { type: "query/mutation", path: "procedure path", ctx: res.ctx, input: res.input, response: res.response.message  
 },
}
& fill in the fields with my
res
res
object. log the response to my db & return
res.reponse
res.reponse
to my client.
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

[v11] Change responseMeta in procedure
SongkeysSSongkeys / ❓-help
2y ago
How to compress procedure response
MugetsuMMugetsu / ❓-help
5mo ago
Finding the Unused TRPC procedure...
0xtz00xtz / ❓-help
11mo ago