Skip to content

checkContext

Category
Tags
Export size
min 1.29 kB · gzip 0.59 kB

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. Also narrows the context type based on the passed options.

The options form takes the same criteria as isContexttype, method, path and id — and reports every one of them that did not match. Only type, method and path narrow the context type.

ts
  import { 
checkContext
} 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' })
  // context.type is now 'before' | 'around', context.method is now 'create' | 'patch'
}

Type declaration

Show Type Declarations
ts
type NarrowedContext<H extends HookContext, O> = H &
  (O extends {
    path: infer P
  }
    ? [P] extends [undefined | null]
      ? unknown
      : {
          path: UnpackMaybeArray<P>
        }
    : unknown) &
  (O extends {
    type: infer T
  }
    ? [T] extends [undefined | null]
      ? unknown
      : {
          type: UnpackMaybeArray<T>
        }
    : unknown) &
  (O extends {
    method: infer M
  }
    ? [M] extends [undefined | null]
      ? unknown
      : {
          method: UnpackMaybeArray<M>
        }
    : unknown)
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.
 * Also narrows the context type based on the passed options.
 *
 * The options form takes the same criteria as `isContext` — `type`, `method`,
 * `path` and `id` — and reports every one of them that did not match. Only
 * `type`, `method` and `path` narrow the context type.
 *
 * @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' })
 *   // context.type is now 'before' | 'around', context.method is now 'create' | 'patch'
 * }
 * ```
 *
 * @see https://utils.feathersjs.com/utils/check-context.html
 */
export declare function checkContext<
  H extends HookContext,
  const O extends CheckContextOptions<NoInfer<H>>,
>(context: H, options: O): asserts context is NarrowedContext<H, O>
export declare function checkContext<
  H extends HookContext,
  const T extends HookType | HookType[] | null | undefined = undefined,
  const M extends MethodName | MethodName[] | null | undefined = undefined,
>(
  context: H,
  type?: T,
  methods?: M,
  label?: string,
): asserts context is NarrowedContext<
  H,
  {
    type: T
    method: M
  }
>
ArgumentTypeDescription
contextH
optionsO
contextH
typeT
methodsM
labelstring
contextH
typeOrOptionsHookType | HookType[] | CheckContextOptions<NoInfer<H>> | null
methodsMethodName | MethodName[] | null
labelany

Released under the MIT License.