Skip to content

checkMulti ​

Category
Tags
Export size
min 7.15 kB · gzip 2.91 kB
See also

Throws a MethodNotAllowed error for a multi call (create with an array, patch/remove with id: null) that the service's multi option - or the hook's own multi - doesn't allow, before the following hooks run. Useful to guard against accidental bulk create, patch, or remove calls.

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

Example ​

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

app.service('users').hooks({
  before: { create: [checkMulti()] }
})

Options ​

multiMulti

Which methods allow multi calls: false refuses every multi call, an array allows them only for the listed methods.

Defaults to the multi option of the service. A service that declares none allows every multi call.

Example
ts
import { checkMulti, iff } from 'feathers-utils/hooks'
import { isProvider } from 'feathers-utils/predicates'

// refuses multi calls although the service allows them
checkMulti({ multi: false })

// only for calls from clients - the server may still change many items
iff(isProvider('external'), checkMulti({ multi: false }))
error(context: HookContext) => FeathersError

Customize the error that is thrown if the service does not allow multi operations.

If not provided, throws a MethodNotAllowed error with a message indicating the operation.

Type declaration ​

Show Type Declarations
ts
export type CheckMultiOptions = {
  /**
   * Which methods allow multi calls: `false` refuses every multi call, an
   * array allows them only for the listed methods.
   *
   * Defaults to the `multi` option of the service. A service that declares
   * none allows every multi call.
   *
   * @example
   * ```ts
   *
   *
   *
   * // refuses multi calls although the service allows them
   * checkMulti({ multi: false })
   *
   * // only for calls from clients - the server may still change many items
   * iff(isProvider('external'), checkMulti({ multi: false }))
   * ```
   */
  multi?: Multi
  /**
   * Customize the error that is thrown if the service does not allow multi operations.
   *
   * If not provided, throws a `MethodNotAllowed` error with a message indicating the operation.
   */
  error?: (context: HookContext) => FeathersError
}
/**
 * Throws a `MethodNotAllowed` error for a multi call (`create` with an array,
 * `patch`/`remove` with `id: null`) that the service's `multi` option - or the
 * hook's own `multi` - doesn't allow, before the following hooks run.
 * Useful to guard against accidental bulk `create`, `patch`, or `remove` calls.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: { create: [checkMulti()] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/check-multi.html
 */
export declare function checkMulti<H extends HookContext = HookContext>(
  options?: CheckMultiOptions,
): {
  (context: H): void
  (context: H, next: NextFunction): Promise<void>
}
ArgumentTypeDescription
optionsCheckMultiOptions
typemethodsmulti
before, aroundcreate, patch, removeyes

Released under the MIT License.