-
I understand that the value Looking at the code, it seems that I cannot narrow the type of My use case is this: I feel I'm misusing the lib tho, but I've tried to figure out by checking the docs and examples and couldn't spot what I'm missing. Thank you! Love the library! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @andremw , the purpose of ResultHandler is responding consistently for every Endpoint produced on EndpointsFactory having that ResultHandler attached to. Therefore, However, you can still narrow it down to whatever you want by implementing Typescript conditions like this: new ResultHandler({
handler: ({ output }) => {
if ("_type" in output && typeof output._type === "string") {
// inside this block `output` has type { _type: string }
}
}
}); I must also say, that you should not return errors as a valid output from your Endpoint, like you mentioned:
this is the job of ResultHandler. I recommend to throw from Endpoint, so that ResultHandler will receieve the factory.build({
handler: async () => {
if (something) {
throw createHttpError(400, "WorkflowError ...");
}
}
});
new ResultHandler({
handler: (output, error) => {
if (error) {
// handle error here
return;
}
// handle no error here
}
}) I hope it helps. |
Beta Was this translation helpful? Give feedback.
Hello @andremw ,
the purpose of ResultHandler is responding consistently for every Endpoint produced on EndpointsFactory having that ResultHandler attached to. Therefore,
output
can not have certain type, except the fact that we know that it is an object, because everyoutput
schema of Endpoint is required to be based onz.object()
.However, you can still narrow it down to whatever you want by implementing Typescript conditions like this:
I must also say, that you should not return errors as a valid …