Callback functions
While these functions are guaranteed to finish execution before your function
returns (to prevent issues in serverless environments) and will be called in
their logical order (onStart
-> onSuccess
| onError
-> onComplete
),
they are not guaranteed to finish execution in that order (e.g., onComplete
can finish execution before onStart
if onStart
makes a long async
request).
These functions must return Promise<void>
. All thrown errors are automatically caught, except for framework related ones like Next's Redirect
.
The examples bellow use the following SafeFn
onStart()
Starts execution right when you run your SafeFn and takes in the following parameters:
unsafeRawInput
: the raw input passed when running your SafeFn. Keep in mind this can contain additional properties when using one SafeFn as the parent of another!
onSuccess()
Starts execution after output parsing has completed if you defined a schema, otherwise after your handler()
or safeHandler()
finished execution. Takes in the following parameters:
unsafeRawInput
: the raw input passed when running your SafeFn. Keep in mind this can contain additional properties when using one SafeFn as the parent of another!input
: the results of parsingunsafeRawInput
through your input schema if you defined one, otherwiseundefined
ctx
: theOk
value of your parent safe-fn if you defined one, otherwise undefined.ctxInput
: an array of all the parsed input of parent SafeFns.value
: theOk
value of the safe-fn this callback is defined on.
onError()
Starts execution after the first encountered Err
return while executing your SafeFn. Takes in the following parameters:
unsafeRawInput
: the raw input passed when running your SafeFn. Keep in mind this can contain additional properties when using one SafeFn as the parent of another!input
: the results of parsingunsafeRawInput
through your input schema if you defined one and parsing was successful, otherwiseundefined
.ctx
: theOk
value of your parent safe-fn if you defined one and execution was successful, otherwise undefinedctxInput
: an array of all the parsed input of parent SafeFns. (Note this is not properly typed yet, the value of this depends on where your function errors)error
: theErr
error that caused your safe-fn to fail.
onComplete()
Starts execution after either onSuccess()
or onError()
is called (not finished). Takes in the following parameters:
-
unsafeRawInput
: the raw input passed when running your SafeFn. Keep in mind this can contain additional properties when using one SafeFn as the parent of another! -
input
: the results of parsingunsafeRawInput
through your input schema if you defined one and parsing was successful, otherwiseundefined
. -
ctx
: theOk
value of your parent safe-fn if you defined one and execution was successful, otherwise undefined -
ctxInput
: an array of all the parsed input of parent SafeFns. (Note this is not properly typed yet for the error case, the value of this depends on where your function errors) -
result
: aResult<T,E>
with either theOk
value of the safe-fn this callback was defined on if execution was successful, or theErr
error.