Skip to content

iff

hooks
Source Code | Documentation

See also: hooks/iffElse hooks/unless predicates

Conditionally executes a series of hooks when the predicate is truthy. The predicate can be a boolean value or a sync/async function. Supports an .else(...) chain for the falsy branch. Also exported as when.

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

Example

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

app.service('users').hooks({
  before: {
    find: [iff(isProvider('external'), authenticate('jwt'))]
  }
})

Type declaration

Show Type Declarations
ts
export interface IffHook<
  H extends HookContext = HookContext,
> extends HookFunction<H> {
  else(...hooks: HookFunction<H>[]): HookFunction<H>
}
/**
 * Conditionally executes a series of hooks when the predicate is truthy.
 * The predicate can be a boolean value or a sync/async function.
 * Supports an `.else(...)` chain for the falsy branch. Also exported as `when`.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: {
 *     find: [iff(isProvider('external'), authenticate('jwt'))]
 *   }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/iff.html
 */
export declare function iff<H extends HookContext = HookContext>(
  predicate: boolean | PredicateFn<H>,
  ...hooks: HookFunction<H>[]
): IffHook<H>
export { iff as when }
ArgumentTypeDescription
predicateboolean | PredicateFn<H>
hooksHookFunction<H>[]
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.