Skip to content

Predicates

Predicates are functions that return a boolean value based on the context of a Feathers service call. They can be used to conditionally execute hooks or logic based on the state of the request, such as whether the user is authenticated, if the data is an array, or any other custom condition you define.

Hooks that are meant to be used with predicates:

  • iff: Executes a hook if the predicate returns true.
  • iffElse: Executes one hook if the predicate returns true, and another if it returns false.
  • unless: Executes a series of hooks if the predicate returns false.
  • skippable: Allows you to skip the execution of a hook based on a predicate.
  • throwIf: Throws an error if the predicate returns true.
every

Return the and of a series of sync or async predicate functions.

isContext

Check if the context matches the given options.

isMulti

util to check if a hook is a multi hook:

  • find: true
  • get: false
  • create: context.data is an array
  • update: false
  • patch: context.id == null
  • remove: context.id == null
isPaginated

util to check if a hook is a paginated hook using getPaginate

isProvider

Check which transport provided the service call.

not

Negate a predicate function.

shouldSkip

Util to detect if a hook should be skipped

Checks the params.skipHooks array for the hook name, type, or 'all'.

some

Return the or of a series of sync or async predicate functions.

Custom Predicates

You can easily create custom predicates like:

ts
const isSingleData = (context) => !Array.isArray(context.data);

or

ts
const isAuthenticated = (context) => !!context.params?.authenticated;

Released under the MIT License.