Skip to content

iffElse

hooks
Source Code | Documentation

See also: hooks/iff hooks/unless predicates

Executes one array of hooks when the predicate is truthy, or another array when it is falsy. The predicate can be a boolean or a sync/async function. Unlike iff, both branches are provided upfront without chaining.

ts
  import { 
iffElse
} from 'feathers-utils/hooks';

Example

ts
import { iffElse, isProvider } from 'feathers-utils/predicates'

app.service('users').hooks({
  before: {
    find: [iffElse(isProvider('external'), [hook1()], [hook2()])]
  }
})

Type declaration

Show Type Declarations
ts
/**
 * Executes one array of hooks when the predicate is truthy, or another array when it is falsy.
 * The predicate can be a boolean or a sync/async function.
 * Unlike `iff`, both branches are provided upfront without chaining.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: {
 *     find: [iffElse(isProvider('external'), [hook1()], [hook2()])]
 *   }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/iff-else.html
 */
export declare function iffElse<H extends HookContext = HookContext>(
  predicate: boolean | PredicateFn<H>,
  trueHook: HookFunction<H> | HookFunction<H>[] | undefined,
  falseHook?: HookFunction<H> | HookFunction<H>[] | undefined,
): (this: any, ctx: H) => any
ArgumentTypeDescription
predicateboolean | PredicateFn<H>
trueHookHookFunction<H> | HookFunction<H>[] | undefined
falseHookHookFunction<H> | HookFunction<H>[] | undefined
typemethodsmulti
before, afterallyes

Predicates

'feathers-utils' provides a set of predicates that can be used with this hook. These predicates can be used to conditionally execute hooks based on the result of a predicate function.

PredicateDescription
every

Returns a predicate that is true only when all given predicates are true (logical AND). Supports both sync and async predicates. Short-circuits on the first false result. Undefined predicates in the list are skipped.

isContext

Returns a predicate that checks whether the hook context matches the given criteria. You can filter by path (service name), type (before/after/around/error), and/or method (find/get/create/update/patch/remove).

isMulti

Checks if the current hook context represents a multi operation. Returns true for find, for create with array data, and for patch/remove with id === null. Returns false for get and update.

isPaginated

Checks if the current find operation is paginated by inspecting params.paginate and the service's pagination options via getPaginate. Returns false for all methods other than find or when pagination is disabled.

isProvider

Returns a predicate that checks the transport provider of the service call. Matches against 'rest', 'socketio', 'external' (any external provider), or 'server' (internal call without a provider).

not

Negates a sync or async predicate function, inverting its boolean result. Useful for composing conditions like "not external" or "not multi".

shouldSkip

Returns a predicate that checks params.skipHooks to determine if a hook should be skipped. Matches by hook name, hook type (e.g. 'before'), prefixed name (e.g. 'before:myHook'), or 'all' to skip everything. Designed to be used with skippable and addSkip.

some

Returns a predicate that is true when any of the given predicates is true (logical OR). Supports both sync and async predicates. Short-circuits on the first true result. Undefined predicates in the list are skipped.

Released under the MIT License.