Skip to content

checkContext

utils
Source Code | Documentation

Validates that the hook context matches the expected type(s) and method(s). Throws an error if the context is invalid, preventing hooks from running in unsupported configurations. Typically used internally by other hooks.

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

Example

ts
import { checkContext } from 'feathers-utils/utils'

const myHook = (context) => {
  checkContext(context, ['before', 'around'], ['create', 'patch'], 'myHook')
  // or with options object:
  checkContext(context, { type: ['before', 'around'], method: ['create', 'patch'], label: 'myHook' })
  // ... hook logic
}

Type declaration

Show Type Declarations
ts
export type CheckContextOptions<H extends HookContext = HookContext> =
  IsContextOptions<H> & {
    label?: string
  }
/**
 * Validates that the hook context matches the expected type(s) and method(s).
 * Throws an error if the context is invalid, preventing hooks from running in
 * unsupported configurations. Typically used internally by other hooks.
 *
 * @example
 * ```ts
 *
 *
 * const myHook = (context) => {
 *   checkContext(context, ['before', 'around'], ['create', 'patch'], 'myHook')
 *   // or with options object:
 *   checkContext(context, { type: ['before', 'around'], method: ['create', 'patch'], label: 'myHook' })
 *   // ... hook logic
 * }
 * ```
 *
 * @see https://utils.feathersjs.com/utils/check-context.html
 */
export declare function checkContext<H extends HookContext = HookContext>(
  context: H,
  options: CheckContextOptions<NoInfer<H>>,
): void
export declare function checkContext<H extends HookContext = HookContext>(
  context: H,
  type?: HookType | HookType[] | null,
  methods?: MethodName | MethodName[] | null,
  label?: string,
): void
ArgumentTypeDescription
contextH
optionsCheckContextOptions<NoInfer<H>>
contextH
typeHookType | HookType[] | null
methodsMethodName | MethodName[] | null
labelstring
contextH
typeOrOptions| HookType | HookType[] | CheckContextOptions<NoInfer<H>> | null
methodsMethodName | MethodName[] | null
labelany

Released under the MIT License.