Handler functions
SafeFn has two types of handler functions, handler()
and safeHandler()
.
Args
Both handler()
and safeHandler()
take in an object with the following properties:
input
: The result of parsing the input schema if you defined one, otherwiseundefined
.unsafeRawInput
: The unparsed input of your function. Either inferred throughinput()
or set throughunparsedInput()
, and merged with theunsafeRawInput
of the parent SafeFn if one is defined.undefined
if there's no input schema, nounparsedInput()
set, and no parent is set or a parent without input is used.ctx
: TheOk
return value of the parent handler function or undefined if no parent handler function is set.ctxInput
: An array of all the parsed input of the parent handler functions. This could be handy in a pinch, altho ideally you'd return the data from the parent function directly and access it throughctx
.
When chaining the type for unsafeRawInput
can lie
if you use this SafeFn as a parent for another! Since the input is not parsed
and the child SafeFn might require additional input that will be passed in at
runtime, the parent function will be called with properties that only appear
in the type of the child function!
Handler
handler()
takes in a function that will be called when your SafeFn is executed. This function should return a Result
which can be created through Neverthrow's ok()
and err()
functions. If you defined an output schema, the Ok
return must match the shape of the input of that schema.
Safe Handler
Instead of using handler()
, you can also use safeHandler()
. This offers the same functionality as NeverThrow's safeTry()
and takes in an async generator function.
This is probably the route you want to take if the rest of your codebase is built with NeverThrow, as it allows ergonomic "return if error" handling.
Consider the following example:
This can be rewritten using safeHandler()
as follows: