Skip to content

isContext

Category
Tags
Export size
min 0.31 kB · gzip 0.19 kB

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), method (find/get/create/update/patch/remove) and/or id (the record a get/update/patch/remove addresses, null for the multi variants).

ts
  import { 
isContext
} from 'feathers-utils/predicates';

Examples

Example 1

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

app.service('users').hooks({
  before: { all: [iff(isContext({ method: 'create', type: 'before' }), validateHook())] }
})

Example 2

ts
// a single record, or every multi call
isContext({ method: 'patch', id: 1 })
isContext({ method: ['patch', 'remove'], id: null })

Hooks for predicates

HookDescription
iff

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.

iffElse

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.

skippable

Wraps a hook so it can be conditionally skipped based on a predicate. When the predicate returns true, the wrapped hook is skipped entirely. Commonly used with shouldSkip and addSkip for runtime hook control.

throwIf

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.

unless

Executes a series of hooks when the predicate is falsy --- the inverse of iff. The predicate can be a boolean or a sync/async function. Useful for applying hooks to all contexts except those matching a condition.

Type declaration

Show Type Declarations
ts
export type IsContextOptions<H extends HookContext = HookContext> = {
  path?: MaybeArray<H["path"]>
  type?: MaybeArray<H["type"]>
  method?: MaybeArray<H["method"]>
  /**
   * The `id` of a `get`, `update`, `patch` or `remove` call. `null` matches the
   * multi variant of those, which is what Feathers puts there when several
   * records are addressed at once.
   *
   * Ids are compared strictly: `3` does not match `'3'`.
   */
  id?: MaybeArray<NonNullable<H["id"]> | null>
}
/**
 * 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),
 * `method` (find/get/create/update/patch/remove) and/or `id` (the record a
 * `get`/`update`/`patch`/`remove` addresses, `null` for the multi variants).
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: { all: [iff(isContext({ method: 'create', type: 'before' }), validateHook())] }
 * })
 * ```
 *
 * @example
 * ```ts
 * // a single record, or every multi call
 * isContext({ method: 'patch', id: 1 })
 * isContext({ method: ['patch', 'remove'], id: null })
 * ```
 *
 * @see https://utils.feathersjs.com/predicates/is-context.html
 */
export declare const isContext: <H extends HookContext = HookContext>(
  options: IsContextOptions<H>,
) => (context: any) => boolean
ArgumentTypeDescription
optionsIsContextOptions<H>

Released under the MIT License.