How return array of objects in response? #361
-
I don't want structure { data: [{}, {}] } because it's not compatible with already exist REST Api and blocked migration need return simple [{}, {}] express-zod-api source code express-zod-api/src/common-helpers.ts export type IOSchema = |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 5 replies
-
Hello @HardCoreQual . Thank you for the question.
Please let me know if you need more information on the topic. |
Beta Was this translation helpful? Give feedback.
-
Here is an example implementation, @HardCoreQual . export const arrayResultHandler = createResultHandler({
getPositiveResponse: <OUT extends IOSchema>(output: OUT) =>
createApiResponse(
z.array(z.any()),
),
getNegativeResponse: () => createApiResponse(z.string()),
handler: ({ error, input, output, request, response, logger }) => {
if (error) {
response.status(getStatusCodeFromError(error)).send(error.message);
return;
}
if ("array" in output && Array.isArray(output.array)) {
response.status(200).json(output.array);
} else {
response.status(400).send("Array is missing");
}
},
}); |
Beta Was this translation helpful? Give feedback.
-
Regarding your suggestion on including
I initially did not plan to do this in principle and do not want to, because I consider it contrary to best practices and hindering the development of the endpoint preserving its backwards compatibility. The temptation to return just an array of objects is very strong, however, returning arrays once raises a question of limitations, the answer to which is pagination. Therefore, as your product evolves, you will soon realize that you would also like to return additional data or metadata, such as Therefore, I would like this library to encourage more provident architectural decisions. |
Beta Was this translation helpful? Give feedback.
-
createApiResponse is deprecated, can you help me to achieve the same effect? I am trying: getPositiveResponse: (output: IOSchema | z.ZodArray<z.ZodObject<any>>) =>
z.object({
statusCode: z.number().default(200),
data: output,
}), but the |
Beta Was this translation helpful? Give feedback.
-
Yeah, it is no longer needed and has been removed, @gmazoni https://github.com/RobinTail/express-zod-api/blob/master/src/api-response.ts Here you can also find an example of creating a custom ResultHandler: https://github.com/RobinTail/express-zod-api/blob/master/example/factories.ts |
Beta Was this translation helpful? Give feedback.
-
Also, @gmazoni you wanted the ResultHandler to respond with array, so your schema should most likely be: const arrayRespondingFactory = new EndpointsFactory({
config,
resultHandler: createResultHandler({
getPositiveResponse: (output) =>
"shape" in output ? output.shape.array : z.array(z.any()),
getNegativeResponse: () => z.string(),
handler: ({ response, output }) => {
if ("array" in output && Array.isArray(output.array)) {
response.status(200).json(output.array);
} else {
response.status(500).send("Array is missing");
}
},
}),
}); meaning that the endpoints you create on this Factory should result in object having |
Beta Was this translation helpful? Give feedback.
-
🚀 Good news, array lovers and legacy API migrators: |
Beta Was this translation helpful? Give feedback.
🚀 Good news, array lovers and legacy API migrators:
the new feature has been just released in version 11.7.0 that should help you to achieve the desired response easier:
arrayResultHandler
andarrayEndpointsFactory
.The details in Readme and Changelog.