Skip to content

Commit 3f4e9b9

Browse files
committed
Merge branch 'master' into beta
2 parents e2965ff + 7136acd commit 3f4e9b9

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

README.md

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<h1 align="center">@storyofams/next-api-decorators</h1>
66
</p>
77

8-
<p align="center">Collection of decorators to create structured API routes with Next.js.</p>
8+
<p align="center">Collection of decorators to create typed Next.js API routes, with easy request validation and transformation.</p>
99

1010
---
1111

@@ -36,21 +36,13 @@ Your `tsconfig.json` needs the following flags:
3636

3737
```ts
3838
// pages/api/user.ts
39-
import {
40-
createHandler,
41-
Get,
42-
Post,
43-
HttpCode,
44-
Query,
45-
Body,
46-
NotFoundException
47-
} from '@storyofams/next-api-decorators';
39+
import { createHandler, Get, Post, Query, Body, NotFoundException } from '@storyofams/next-api-decorators';
4840

4941
class User {
5042
// GET /api/user
5143
@Get()
5244
public async fetchUser(@Query('id') id: string) {
53-
const user = await User.findById(id);
45+
const user = await DB.findUserById(id);
5446

5547
if (!user) {
5648
throw new NotFoundException('User not found.');
@@ -61,9 +53,8 @@ class User {
6153

6254
// POST /api/user
6355
@Post()
64-
@HttpCode(201)
6556
public createUser(@Body() body: any) {
66-
return User.create(body);
57+
return DB.createUser(body);
6758
}
6859
}
6960

@@ -106,20 +97,20 @@ export default createHandler(User);
10697

10798
### Class decorators
10899

109-
| | Description |
110-
| ----------- | -------------------------------------------------------------------------- |
111-
| `SetHeader` | Sets a header value into the response for all routes defined in the class. |
100+
| | Description |
101+
| ----------------------------------------- | -------------------------------------------------------------- |
102+
| `@SetHeader(name: string, value: string)` | Sets a header name/value into all routes defined in the class. |
112103

113104
### Method decorators
114105

115-
| | Description |
116-
| ----------- | --------------------------------------------------------- |
117-
| `Get` | Marks the method as `GET` handler. |
118-
| `Post` | Marks the method as `POST` handler. |
119-
| `Put` | Marks the method as `PUT` handler. |
120-
| `Delete` | Marks the method as `DELETE` handler. |
121-
| `SetHeader` | Sets a header name/value into the response for the route. |
122-
| `HttpCode` | Sets the http code the route response. |
106+
| | Description |
107+
| ----------------------------------------- | -------------------------------------------------- |
108+
| `@Get()` | Marks the method as `GET` handler. |
109+
| `@Post()` | Marks the method as `POST` handler. |
110+
| `@Put()` | Marks the method as `PUT` handler. |
111+
| `@Delete()` | Marks the method as `DELETE` handler. |
112+
| `@SetHeader(name: string, value: string)` | Sets a header name/value into the route response. |
113+
| `@HttpCode(code: number)` | Sets the http code in the route response. |
123114

124115
### Parameter decorators
125116

@@ -179,4 +170,4 @@ class Events {
179170
throw new ForbiddenException();
180171
}
181172
}
182-
```
173+
```

lib/e2e.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class TestHandler {
5353
return { contentType, id, step, redirect, test: this.testField };
5454
}
5555

56-
@Post()
5756
@HttpCode(201)
57+
@Post()
5858
@SetHeader('X-Method', 'create')
5959
public create(@Header('Content-Type') contentType: string, @Body() body: CreateDto) {
6060
return { contentType, receivedBody: body, test: this.testField, instanceOf: body instanceof CreateDto };

lib/internals/handler.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@ export function Handler(method?: HttpVerb): MethodDecorator {
4242
}
4343

4444
return function (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) {
45-
const httpCode: number | undefined = Reflect.getMetadata(HTTP_CODE_TOKEN, target.constructor, propertyKey);
46-
const metaParameters: Array<MetaParameter> = (
47-
Reflect.getMetadata(PARAMETER_TOKEN, target.constructor, propertyKey) ?? []
48-
).sort((a: MetaParameter, b: MetaParameter) => a.index - b.index);
49-
5045
const originalHandler = descriptor.value;
5146
descriptor.value = async function (req: NextApiRequest, res: NextApiResponse) {
5247
if (req.method !== method) {
5348
return notFound(req, res);
5449
}
5550

51+
const httpCode: number | undefined = Reflect.getMetadata(HTTP_CODE_TOKEN, target.constructor, propertyKey);
52+
const metaParameters: Array<MetaParameter> = (
53+
Reflect.getMetadata(PARAMETER_TOKEN, target.constructor, propertyKey) ?? []
54+
).sort((a: MetaParameter, b: MetaParameter) => a.index - b.index);
5655
const classHeaders: Map<string, string> | undefined = Reflect.getMetadata(HEADER_TOKEN, target.constructor);
5756
const methodHeaders: Map<string, string> | undefined = Reflect.getMetadata(
5857
HEADER_TOKEN,

0 commit comments

Comments
 (0)