Skip to content

[discussion] should we add an optional path param to the route decorators? #34

@leeuwis

Description

@leeuwis

Started yesterday on an example for next-api-decorators and stumbled on the following issue structure wise.

So for the example I wanted to create a simple CRUD example, with the following routes:

  • GET /api/users (list)
  • POST /api/users (create)
  • GET /api/users/:id (read)
  • PUT /api/users/:id (update)
  • DELETE /api/users/:id (delete)

Because we have 2 GET routes, this would normally be split in 2 files:

├── api
│   ├── users
│   │   ├── [id].ts
│   │   ├── index.ts

// or

├── api
│   ├── users.ts
│   ├── users
│       ├── [id].ts

But it feels weird to split up the handler into multiple files, you would expect to have one User class that handles all the user related routes. If we wouldn’t have the list route, it could be done with one class by using an id query parameter instead of the path parameter.

If we want to tackle this problem, it could be a solution to add an optional path param to the decorators like:

@GET('/:id')

The above syntax should be possible if we use this structure (which catches both /users and /users/:id):

├── api
│   ├── users
│   │   ├── [[...id]].ts

And I say “If we want” because it might just not be the right use case for next-api-decorators, maybe it should only be used for simple routes and not to create a complete CRUD API.

Any other thoughts on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions