Easy Customisation of Response Code #1193
-
Hi, I have been looking for a way to change the status code to a 201, to make best use of HTTP semantics. I have seen that the way to do this is by defining a custom ResultHandler. I have made what I think is pretty much the defaultResultHandler, and changed the success code to 201. I want all the default functionality, apart from the status code. It looks like this:
I just wanted to ask whether this is the easiest way to do this. I would like the generated documentation to reflect the code which this does thankfully. But if I want a 202 handler as well, I will need to do this again and it seems a bit verbose! This library is great, and I just wanted to see if I was understanding it properly :) Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Hello, @danclaytondev . Well, currently
Instead of encoding such details as "created" and "accepted" within the status code, consider having a property in the payload with a usual status code const customResultHandler = createResultHandler({
getPositiveResponse: (output: IOSchema) => z.object({
status: z.literal("success"),
detail: z.literal("created").or(z.literal("accepted"))
data: output,
}), // ...
}) However, in case you really want to have those status codes, but you don't want to repeat yourself, consider making a creator-function, that can produce result handlers for you, like this: const makeResultHandler = (statusCode: number) => createResultHandler({
getPositiveResponse: (output: IOSchema) => ({
schema: z.object({
status: z.literal("success"),
data: output,
}),
statusCode, // <—— comes from the higher context
}), // ...
}) ); |
Beta Was this translation helpful? Give feedback.
-
@danclaytondev , would you like to help me in testing the feature #1431 ? |
Beta Was this translation helpful? Give feedback.
-
For those looking for a solution to the same problem, |
Beta Was this translation helpful? Give feedback.
For those looking for a solution to the same problem,
the feature is available in version 16.2.0 🚀 — see Changelog for details.