Skip to content

Commit 7133df1

Browse files
committed
fix documentation
1 parent c27eb9b commit 7133df1

File tree

4 files changed

+23
-50
lines changed

4 files changed

+23
-50
lines changed

README.md

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,9 @@ return { data: 'value' };
122122

123123
## Advanced Usage
124124

125-
### Metadata
125+
### Static Parameters with Metadata
126126

127-
You can add metadata to your route handler with the `defineMetadata` method. Metadata is optional and can be used to add additional information to your route handler.
128-
129-
```ts
130-
const metadataSchema = z.object({
131-
permission: z.string(),
132-
role: z.enum(['admin', 'user']),
133-
});
134-
135-
export const GET = createZodRoute()
136-
.defineMetadata(metadataSchema)
137-
.handler((request, context) => {
138-
// Access metadata from context.metadata
139-
const { permission, role } = context.metadata!;
140-
141-
return Response.json({ permission, role });
142-
});
143-
```
144-
145-
When calling the route, you can pass metadata as part of the context:
146-
147-
```ts
148-
// In your Next.js page/component
149-
const response = await GET(request, {
150-
params: Promise.resolve({}),
151-
metadata: { permission: 'read', role: 'admin' },
152-
});
153-
```
154-
155-
Metadata is optional by default. If you define a metadata schema but don't provide metadata in the context, the handler will still work. If you provide metadata, it will be validated against the schema.
156-
157-
### Permission Checking with Metadata
127+
Metadata enable you to add **static parameters** to the route, for example to give permissions list to our application.
158128

159129
One powerful use case for metadata is defining required permissions for routes and checking them in middleware. This allows you to:
160130

docs/migrations/01-middleware-next.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ Version 0.2.0 introduces a completely revamped middleware system that provides m
1616
1. Middleware must now accept an object with:
1717

1818
- `request`: The incoming request
19-
- `context`: Current context from previous middleware
19+
- `ctx`: Current context from previous middleware
2020
- `metadata`: Optional route metadata (type-safe)
2121
- `next`: Function to continue the chain
2222

23-
2. Context is now passed explicitly via `next({ context: {...} })`
23+
2. Context is now passed explicitly via `next({ ctx: {...} })`
2424

2525
3. Middleware can return:
2626
- A Response object to short-circuit
@@ -39,8 +39,8 @@ const authMiddleware = async () => {
3939

4040
const route = createZodRoute()
4141
.use(authMiddleware)
42-
.handler((req, ctx) => {
43-
const { user } = ctx.data;
42+
.handler((req, { ctx }) => {
43+
const { user } = ctx;
4444
return { data: user.id };
4545
});
4646
```
@@ -50,15 +50,15 @@ const route = createZodRoute()
5050
```typescript
5151
const authMiddleware = async ({ next }) => {
5252
const response = await next({
53-
context: { user: { id: 'user-123' } },
53+
ctx: { user: { id: 'user-123' } },
5454
});
5555
return response;
5656
};
5757

5858
const route = createZodRoute()
5959
.use(authMiddleware)
60-
.handler((req, ctx) => {
61-
const { user } = ctx.data;
60+
.handler((req, { ctx }) => {
61+
const { user } = ctx;
6262
return { data: user.id };
6363
});
6464
```
@@ -102,7 +102,7 @@ const headerMiddleware = async ({ next }) => {
102102
```typescript
103103
const middleware1 = async ({ next }) => {
104104
const response = await next({
105-
context: { value1: 'first' },
105+
ctx: { value1: 'first' },
106106
});
107107
return response;
108108
};
@@ -112,7 +112,7 @@ const middleware2 = async ({ context, next }) => {
112112
console.log(context.value1); // 'first'
113113

114114
const response = await next({
115-
context: { value2: 'second' },
115+
ctx: { value2: 'second' },
116116
});
117117
return response;
118118
};
@@ -153,7 +153,7 @@ const permissionCheckMiddleware = async ({ next, metadata, request }) => {
153153

154154
// If no required permissions in metadata, allow access
155155
if (!metadata?.requiredPermissions || metadata.requiredPermissions.length === 0) {
156-
return next({ context: { authorized: true } });
156+
return next({ ctx: { authorized: true } });
157157
}
158158

159159
// Check if user has all required permissions
@@ -174,7 +174,7 @@ const permissionCheckMiddleware = async ({ next, metadata, request }) => {
174174
}
175175

176176
// Continue with authorized context
177-
return next({ context: { authorized: true } });
177+
return next({ ctx: { authorized: true } });
178178
};
179179

180180
// Use in your route handlers
@@ -208,7 +208,7 @@ const middleware = async () => {
208208
// After
209209
const middleware = async ({ request, next }) => {
210210
const token = request.headers.get('authorization');
211-
return next({ context: { token } });
211+
return next({ ctx: { token } });
212212
};
213213
```
214214

@@ -221,11 +221,11 @@ const middleware2 = async () => ({ value2: 'second' });
221221

222222
// After
223223
const middleware1 = async ({ next }) => {
224-
return next({ context: { value1: 'first' } });
224+
return next({ ctx: { value1: 'first' } });
225225
};
226226
const middleware2 = async ({ context, next }) => {
227227
return next({
228-
context: {
228+
ctx: {
229229
...context,
230230
value2: 'second'
231231
}
@@ -243,7 +243,7 @@ const middleware = async () => {
243243

244244
// After
245245
const middleware = async ({ next }) => {
246-
const response = await next({ context: { transform: true } });
246+
const response = await next({ ctx: { transform: true } });
247247

248248
// Now you can transform the response
249249
const data = await response.json();

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-zod-route",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "A zod way to define route handlers in Next.js",
55
"keywords": [
66
"next",
@@ -41,9 +41,7 @@
4141
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
4242
"lint": "tsc --noEmit && eslint .",
4343
"lint-staged": "lint-staged",
44-
"pre:publish": "pnpm build",
4544
"prepare": "husky",
46-
"publish": "pnpm publish",
4745
"release": "release-it",
4846
"test": "vitest run",
4947
"test:coverage": "vitest run --coverage",

src/routeHandlerBuilder.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export class RouteHandlerBuilder<
121121
});
122122
}
123123

124+
/**
125+
* Set the metadata value for the route handler
126+
* @param value - The metadata value that will be passed to middlewares
127+
* @returns A new instance of the RouteHandlerBuilder
128+
*/
124129
metadata(value: z.infer<TMetadata>) {
125130
return new RouteHandlerBuilder<TParams, TQuery, TBody, TContext, TMetadata>({
126131
...this,

0 commit comments

Comments
 (0)