Next.js Discord

Discord Forum

Research around decorator and api route

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
Hello, i'm porting a project from NextJS for the frontend + NestJS for the API to a full NextJS.
This not hard to do, but i really love the decorator provided by NestJS, after a little bit of research i found next-api-decorator, but unfortunately it dont work with the new Route Handler.

So i started to dig deeper, and creating a set of decorators for the router.
But now i encounter an issue with the class decorator.

I just started with decorator and metadata, so I'm looking for some that can helps / advises to produce a set of open source usable decorator for the new route handler and possibly fork and update the next-api-decorator repo.

I created a gist with what i have so far https://gist.github.com/SuperstrongBE/2d4e6d1aac89797270745f22c9ba047b

The idea is to create a class with an @ApiService class decorator, and @Post, @Get, @Patch, @Delete method decorators to wrap targeted function around NextJS handler mecanism.
// in api/test/route.ts

// See the gist at apiservice.decorator.ts
@ApiService()
class TestService {
  @Post() //Wrap the handlePost inside a new function named POST in the decorated class.
  // See the gist at post.decorator.ts
  async handlePost(req:NextRequest){
    ...
    return {result:'stuff'}
  }
}

and finally
export {POST,GET,PATCH,DELETE} from new TestService()

But the last part (the export of method) don't work because i loose the this in the class decorator, that make the metadata loosing his target ([see line 32 of apiService](https://gist.github.com/SuperstrongBE/2d4e6d1aac89797270745f22c9ba047b#file-apiservice-decorator-ts-L32)).
If someone know how i can tame the issue, i'm aware of solution!
Thank for reading !

0 Replies