You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Elysia with Eden to build my API, and I'm facing an issue with TypeScript type inference when applying a custom response wrapping plugin.
I have a mapResponse plugin that wraps all my API responses in a custom structure to maintain a consistent response format across all endpoints. The structure looks like this:
Similarly, for errors, the inferred type does not match the wrapped error structure.
My Question:
What's the best way to handle this situation?
I want to avoid modifying each handler to return the wrapped response directly, as it duplicates the logic in the mapResponse plugin. I'm looking for a solution that allows me to keep my handlers clean and have the type inference correctly reflect the wrapped response structure.
I'm eager to hear your suggestions and best practices for this scenario.
Thank you!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everyone,
I'm using Elysia with Eden to build my API, and I'm facing an issue with TypeScript type inference when applying a custom response wrapping plugin.
I have a mapResponse plugin that wraps all my API responses in a custom structure to maintain a consistent response format across all endpoints. The structure looks like this:
Here is a simplified version of the mapResponse plugin:
For example, I have a simple health check route:
TypeScript infers the response types based on the handler's return value (unwrapped data), leading to type mismatches in my client code.
In the images, you can see that the inferred type for data is { health: string; }, but due to the response wrapping, the actual response is:
Similarly, for errors, the inferred type does not match the wrapped error structure.
My Question:
What's the best way to handle this situation?
I want to avoid modifying each handler to return the wrapped response directly, as it duplicates the logic in the mapResponse plugin. I'm looking for a solution that allows me to keep my handlers clean and have the type inference correctly reflect the wrapped response structure.
I'm eager to hear your suggestions and best practices for this scenario.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions