Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions docs/v6_docs/guides/custom-hooks.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Custom Hooks
# Custom Hooks (Updated)

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document title includes an editorial marker "(Updated)". Please keep the H1 as the stable guide title (e.g., just "Custom Hooks") and avoid versioning/status notes in the heading.

Suggested change
# Custom Hooks (Updated)
# Custom Hooks

Copilot uses AI. Check for mistakes.

Hooks are the primary way to add cross-cutting logic in FeathersJS — think
validation, authorization, logging, and data transformation.
Expand All @@ -8,7 +8,6 @@ validation, authorization, logging, and data transformation.
```typescript
import type { HookContext } from '../declarations';

// A simple before-create hook that stamps a createdAt field
export const addTimestamp = async (context: HookContext) => {
context.data = {
...context.data,
Expand All @@ -29,19 +28,28 @@ app.service('messages').hooks({
});
```

## Around hooks (v6+)
## Around hooks (v6+ — NEW SECTION)

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section header includes "— NEW SECTION", which reads like an internal note. Please remove this so the published heading is just the topic (e.g., "Around hooks (v6+)").

Suggested change
## Around hooks (v6+ — NEW SECTION)
## Around hooks (v6+)

Copilot uses AI. Check for mistakes.

Around hooks wrap the entire method call, giving you control of both
the before and after phases in one function:
Around hooks are the recommended pattern in v6. They wrap the entire method
call, giving you control of both the before and after phases:

```typescript
export const logDuration = async (context: HookContext, next: Function) => {

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The around hook example types next as Function. Elsewhere in the v6 docs the recommended type is NextFunction (for correct typing and to document the intended contract). Update the example signature accordingly and ensure the type is imported in the snippet.

Suggested change
export const logDuration = async (context: HookContext, next: Function) => {
import type { HookContext, NextFunction } from '../declarations';
export const logDuration = async (context: HookContext, next: NextFunction) => {

Copilot uses AI. Check for mistakes.
const start = Date.now();
await next();
console.log(`${context.method} took ${Date.now() - start} ms`);
const ms = Date.now() - start;
console.log(`${context.method} on ${context.path} took ${ms}ms`);
};
```

## Composing multiple hooks

```typescript
import { hooks } from '@feathersjs/hooks';

const composed = hooks([validateData, addTimestamp, logDuration]);
Comment on lines +46 to +50

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Composing multiple hooks" snippet introduces hooks imported from @feathersjs/hooks, but there’s no surrounding explanation of what this helper is or how composed is then registered on a service. Consider aligning this with the existing docs patterns (composing via arrays passed to app.service(...).hooks(...)) or explicitly explain the dependency/API and show how to apply composed in a hooks registration.

Suggested change
```typescript
import { hooks } from '@feathersjs/hooks';
const composed = hooks([validateData, addTimestamp, logDuration]);
Using the `hooks` helper from `@feathersjs/hooks`, you can compose several hooks
into a single reusable around hook and then register it like any other hook:
```typescript
import { hooks } from '@feathersjs/hooks';
// Compose multiple hooks into a single reusable around hook
const composed = hooks([validateData, addTimestamp, logDuration]);
// Register the composed hook on a service
app.service('messages').hooks({
around: {
all: [composed],
},
});

Copilot uses AI. Check for mistakes.
```

## Unit-testing a hook

```typescript
Expand All @@ -53,4 +61,3 @@ test('stamps createdAt', async () => {
expect(ctx.data.createdAt).toBeDefined();
});
```
I am adding this just to make our lives easier
Loading