SafeFn
Creating a SafeFn

Chaining

You can create procedure chains by using .use(). This is a function that takes in a SafeFn.

const authedFn = createSafeFn()
  .safeHandler(async () => {
    const user = await getUser();
    if (!user) {
      return err({
        code: "UNAUTHORIZED",
      });
      return user;
    }
  })
  .catch((e) => ({
    code: "AUTH_ERROR",
    cause: e,
  }));
 
const getUserTodos = createSafeFn().use(authedFn). ...

This parent SafeFn will run before the child it's defined on (including all callbacks). If it returns an Err, the function is short circuited and input parsing/the handler on the child will not be executed. As such, the return type of the child safe-fn now includes the possible Err values of the parent.

Setting a parent SafeFn also changes the available arguments for handler and callback functions, as outlined on their pages.

On this page

No Headings