import type { HookContext, NextFunction } from '@feathersjs/feathers' import { iffElse } from '../iff-else/iff-else.hook.js' import type { HookFunction, PredicateFn } from '../../types.js' export interface IffHook< H extends HookContext = HookContext, > extends HookFunction { else(...hooks: HookFunction[]): HookFunction } /** * 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 * import { iff, isProvider } from 'feathers-utils/predicates' * * app.service('users').hooks({ * before: { * find: [iff(isProvider('external'), authenticate('jwt'))] * } * }) * ``` * * @see https://utils.feathersjs.com/hooks/iff.html */ export function iff( predicate: boolean | PredicateFn, ...hooks: HookFunction[] ): IffHook { if (hooks.length && Array.isArray(hooks[0])) { hooks = hooks[0] } const iffWithoutElse = function (context: H, next?: NextFunction) { return iffElse(predicate, hooks.slice())(context, next) } iffWithoutElse.else = (...falseHooks: any[]) => (context: H, next?: NextFunction) => iffElse(predicate, hooks.slice(), falseHooks.slice())(context, next) return iffWithoutElse as IffHook } export { iff as when }