Skip to content

throwIf

hooks
Source Code | Documentation

Throws a BadRequest error when the given predicate function returns true. The predicate receives the hook context and can be async. Useful for validating conditions before proceeding with a request.

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

Example

ts
import { throwIf } from 'feathers-utils/hooks'

app.service('users').hooks({
  before: { remove: [throwIf((context) => context.id === context.params.user?.id)] }
})

Type declaration

Show Type Declarations
ts
export type ThrowIfOptions = {
  /**
   * Customize the error that is thrown if the predicate returns true.
   *
   * If not provided, throws a `BadRequest` error with 'Invalid operation'.
   */
  error?: (context: HookContext) => FeathersError
}
/**
 * Throws a `BadRequest` error when the given predicate function returns `true`.
 * The predicate receives the hook context and can be async.
 * Useful for validating conditions before proceeding with a request.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: { remove: [throwIf((context) => context.id === context.params.user?.id)] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/throw-if.html
 */
export declare const throwIf: <H extends HookContext = HookContext>(
  predicate: PredicateFn,
  options?: ThrowIfOptions,
) => (context: H, next?: NextFunction) => Promise<any>
ArgumentTypeDescription
predicatePredicateFn
optionsThrowIfOptions
typemethodsmulti
before, after, aroundcreate, update, patchyes

Released under the MIT License.