-
Looks like the base type is Here's an example of me trying to handle const robotsResultHandler = new ResultHandler({
positive: { statusCode: 200, mimeType: 'text/plain', schema: z.string() },
negative: { statusCode: 404, mimeType: null, schema: z.never() },
handler: ({ output, response }) => {
response.set('Cache-Control', 'max-age=2592000');
response.status(200);
response.send(output);
},
});
export const robotsHandler = new EndpointsFactory(robotsResultHandler)
.build({
output: z.string(),
handler: async () => 'User-agent: *\nDisallow: /\n\nUser-agent: Googlebot\nDisallow:\n'
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If the answer is to use an object, should I expect the const robotsResultHandler = new ResultHandler({
positive: (data) => ({
schema: z.object({ data }),
mimeType: 'text/plain',
}),
negative: { statusCode: 404, mimeType: null, schema: z.never() },
handler: ({ output, response }) => {
response.type('text/plain');
response.set('Cache-Control', 'max-age=2592000'); // 1 month
response.status(200);
response.send((output as any).disallow);
},
});
export const robotsHandler = new EndpointsFactory(robotsResultHandler).build({
output: z.object({
disallow: z.string(),
}),
handler: async () => ({
disallow: 'User-agent: *\nDisallow: /\n\nUser-agent: Googlebot\nDisallow:\n'
})
}); |
Beta Was this translation helpful? Give feedback.
-
https://ez.robintail.cz/v24.7.1/non-json-response Your implementation of const robotsResultHandler = new ResultHandler({
positive: { statusCode: 200, mimeType: 'text/plain', schema: z.string() },
negative: { statusCode: 404, mimeType: null, schema: z.never() },
handler: ({ output, response }) => {
response.set('Cache-Control', 'max-age=2592000');
response.status(200);
response.send(output.toString()); // somehow
},
});
This type is for I/O: Therefore, your
This is fine, but I suggest if (output && "disallow" in output) response.send(output.disallow);
// else — handle it |
Beta Was this translation helpful? Give feedback.
ResuiltHandler
can respond anything, @JoshElias , including strings with a corresponding MIME type.https://ez.robintail.cz/v24.7.1/non-json-response
Your implementation of
ResultHandler
is looking good, but it should make a string out of object-basedoutput
This type is for I/O:
input
and