Overview
Thefeathers generate hook command creates a new hook file with the appropriate structure for your chosen hook type. Hooks are middleware functions that can be registered before, after, or on errors of service methods.
Usage
Options
string
The name of the hook. Will be used for the file name and function name.The name will be converted to camelCase for the function name and kebab-case for the file name.
string
The type of hook to generate.Choices:
around- Around hooks wrap service methodsregular- Before, After, or Error hooks
Interactive Prompts
Hook Name
Prompt: “What is the name of the hook?” The name will be transformed:- camelCase for function names (e.g.,
logRequest) - kebab-case for file names (e.g.,
log-request.ts)
Hook Type
Prompt: “What kind of hook is it?” Choices:- Around - Wrap service methods with custom logic before and after execution
- Before, After or Error - Run at a specific point in the service call lifecycle
Hook Types Explained
Around Hooks
Around hooks wrap the service method execution and can:- Run code before the method executes
- Run code after the method executes
- Modify the context before it reaches the method
- Modify the result after the method completes
- Handle errors from the method
- Skip method execution entirely
- Logging and timing
- Caching
- Transaction management
- Performance monitoring
Regular Hooks
Regular hooks run at specific points:- Before hooks - Run before the service method
- After hooks - Run after successful service method execution
- Error hooks - Run when an error occurs
- Validation
- Data transformation
- Populating related data
- Sending notifications
- Error handling
Generated Files
For a hook named “log-request”:Generated Code Examples
Around Hook (TypeScript)
Regular Hook (TypeScript)
Hook Context
All hooks receive acontext object with the following properties:
Application
The Feathers application instance
Service
The service this hook is running on
string
The service path (e.g., “messages”)
string
The service method name (e.g., “create”, “find”, “get”, “update”, “patch”, “remove”)
string
The hook type: “before”, “after”, or “error”
Params
Service call parameters, including query, user, authentication info
any
Data submitted by the client (create, update, patch methods only)
string | number
The resource ID (get, update, patch, remove methods only)
any
The service method result (after hooks only)
Error
The error object (error hooks only)
Using Generated Hooks
After generating a hook, import and register it in your service:Around Hook Usage
Regular Hook Usage
Terminal Output Example
Common Hook Patterns
Validation Hook
Populate Hook
Authentication Hook
Around Hook with Timing
Caching Hook
Hook Registration Levels
Hooks can be registered at different levels:Application-Level Hooks
Run for all services:Service-Level Hooks
Run only for a specific service:Method-Specific Hooks
Run only for specific methods:Next Steps
Service documentation
Learn more about Feathers services
Authentication
Add authentication to your application