Skip to content

defineHooks

utils
Source Code | Documentation

TypeScript helper that provides full type inference and autocompletion when defining service hooks. It is an identity function that simply returns its input, but enables your IDE to infer the correct hook context types.

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

Example

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

export const userHooks = defineHooks({
  before: { create: [validateUser()] },
  after: { all: [sanitizeResult()] }
})

Type declaration

Show Type Declarations
ts
/**
 * TypeScript helper that provides full type inference and autocompletion when defining
 * service hooks. It is an identity function that simply returns its input,
 * but enables your IDE to infer the correct hook context types.
 *
 * @example
 * ```ts
 *
 *
 * export const userHooks = defineHooks({
 *   before: { create: [validateUser()] },
 *   after: { all: [sanitizeResult()] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/utils/define-hooks.html
 */
export declare function defineHooks<
  A extends Application = Application,
  S = {
    find: any
    get: any
    create: any
    update: any
    patch: any
    remove: any
  },
  Options = HookOptions<A, S>,
>(hooks: Options): Options
ArgumentTypeDescription
hooksOptions

When you define hooks in feathers like this:

ts
export default {
  before: {
    // ...
  },
  // ...
};

you don't have autocompletion. Instead, you can use defineHooks to define type-safe hooks:

ts
import { defineHooks } from "feathers-utils/utils";
export default defineHooks({
  before: {
    // ...
  },
  // ...
});

Released under the MIT License.