iff
Source Code | DocumentationSee 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.
import { iff } from 'feathers-utils/hooks';Example
import { iff, isProvider } from 'feathers-utils/predicates'
app.service('users').hooks({
before: {
find: [iff(isProvider('external'), authenticate('jwt'))]
}
})Type declaration
Show Type Declarations
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 }| Argument | Type | Description |
|---|---|---|
| predicate | boolean | PredicateFn<H> | |
| hooks | HookFunction<H>[] |
| type | methods | multi |
|---|---|---|
| before, after | all | yes |
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.
| Predicate | Description |
|---|---|
every | Returns a predicate that is |
isContext | Returns a predicate that checks whether the hook context matches the given criteria.
You can filter by |
isMulti | Checks if the current hook context represents a multi operation.
Returns |
isPaginated | Checks if the current |
isProvider | Returns a predicate that checks the transport provider of the service call.
Matches against |
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 |
some | Returns a predicate that is |
