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
4941class 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+ ```
0 commit comments