import type { HookContext } from '@feathersjs/feathers' import type { MaybeArray } from '../../internal.utils.js' import { toArray } from '../../internal.utils.js' export type IsContextOptions = { path?: MaybeArray type?: MaybeArray method?: MaybeArray } /** * 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), * and/or `method` (find/get/create/update/patch/remove). * * @example * ```ts * import { iff, isContext } from 'feathers-utils/predicates' * * app.service('users').hooks({ * before: { all: [iff(isContext({ method: 'create', type: 'before' }), validateHook())] } * }) * ``` * * @see https://utils.feathersjs.com/predicates/is-context.html */ export const isContext = ( options: IsContextOptions, ) => { const path = options.path != null ? toArray(options.path) : undefined const type = options.type != null ? toArray(options.type) : undefined const method = options.method != null ? toArray(options.method) : undefined return (context: any): boolean => { if (path && !path.some((x) => context.path === x)) { return false } if (type && !type.some((x) => context.type === x)) { return false } if (method && !method.some((x) => context.method === x)) { return false } return true } }