instantcommerce / next-api-decorators

Collection of decorators to create typed Next.js API routes, with easy request validation and transformation.
https://next-api-decorators.vercel.app
MIT License
409 stars 29 forks source link

Routing - path param not matching #554

Closed crossinghoods closed 1 year ago

crossinghoods commented 1 year ago

Trying to do some very simple REST calls, but decorators don't seem to be working :(

GET http://localhost:3000/api/cart (Works) GET http://localhost:3000/api/cart/123 (Caught by findManyHandler instead of findOneHandler) POST http://localhost:3000/api/cart (Works) PATCH http://localhost:3000/api/cart/123 (404 - Not Found) DELETE http://localhost:3000/api/cart/123 (404 - Not Found)

Next version : 12.3.4,

File name: /api/cart/[[...params].ts File contents

class CartAPI {
  @Get()
  public async findManyHandler(@Request() req: IReq, @Query() query: Record<string, any> = {}) {
    console.log("get many >>>", query); /* outputs { params: [ '123' ] } */
    return [];
  }

  @Get("/:id")
  public async findOneHandler(@Request() req: IReq, @Param("id") id: number) {
    console.log("get one >>>", id);
    return [{ test: 1 }];
  }

  @Post()
  @HttpCode(201)
  public async createHandler(@Request() req: IReq, @Body() body: Record<string, any> = {}) {
    console.log("create >>>", body);
  }

  @Patch("/:id")
  @HttpCode(204)
  public async updateHandler(@Request() req: IReq, @Param("id") id: string, @Body() body: Record<string, any> = {}) {
    console.log("update >>>", id, body);
  }

  @Delete("/:id")
  @HttpCode(204)
  protected async deleteHandler(@Request() req: IReq, @Param("id") id: string) {
    console.log("delete >>>", id);
  }
}

export default createHandler(CartAPI);
crossinghoods commented 1 year ago

Got latest and installed these packages, seemed to have fixed the issue for me pnpm i path-to-regexp reflect-metadata class-validator class-transformer