Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using @Param or @Query causes handler to throw error due to missing Reflect metadata for "design:paramtypes" #320

Closed
sannajammeh opened this issue Dec 30, 2021 · 2 comments

Comments

@sannajammeh
Copy link

sannajammeh commented Dec 30, 2021

Describe the bug
When using the @param or @query decorator, a TypeError occurs because const parameterTypes in internals/handler.ts is undefined. This line specifically seems to create the issue:

const parameterTypes: ClassConstructor<any>[] = Reflect.getMetadata('design:paramtypes', target, propertyKey); // Line 70 in handler.ts

To Reproduce
Steps to reproduce the behavior:

  1. run npx create-next-app --ts and enable typescript decorator support
  2. Add this library to an api route with path-to-regexp installed
  3. Use the @param(":someId") decorator.
  4. See error
{
  "statusCode": 500,
  "message": "An unknown error occurred.",
  "errors": [
    "An unknown error occurred."
  ],
  "stack": "TypeError: Cannot read property 'length' of undefined\n    at /Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/@storyofams/next-api-decorators/dist/internals/handler.js:45:50\n    at Array.map (<anonymous>)\n    at StudioController.runMainLayer (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/@storyofams/next-api-decorators/dist/internals/handler.js:41:62)\n    at StudioController.descriptor.value (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/@storyofams/next-api-decorators/dist/internals/handler.js:105:32)\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async Object.apiResolver (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/next/dist/server/api-utils.js:102:9)\n    at async DevServer.handleApiRequest (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/next/dist/server/next-server.js:1064:9)\n    at async Object.fn (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/next/dist/server/next-server.js:951:37)\n    at async Router.execute (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/next/dist/server/router.js:222:32)\n    at async DevServer.run (/Users/sannajammeh/code/Cartable/CourseExposure/platform/node_modules/next/dist/server/next-server.js:1135:29)"
}
  1. Enter node_modules folder and change line 45 in @storyofams/next-api-decorators/dist/internals/handler.js
-         const paramType = index < parameterTypes.length &&
+        const paramType = index < parameterTypes?.length &&
  1. Observe that the API route now works.

Expected behavior
The parameterTypes should not be undefined.

Desktop (please complete the following information):

  • OS: Mac OS 11.5.2
  • Server: Next JS 12 compiled with SWC with decorators enabled
  • Version 1.6.0

image

Additional context
Everything works without using the Param or Query decorators.

@mitchelvanbever
Copy link
Member

mitchelvanbever commented Dec 31, 2021

Have you setup babel or are you using SWC for compilation? I believe the package still relies on the usage of babel to provide the decorators API (legacy)

See: https://next-api-decorators.vercel.app/docs/

@sannajammeh
Copy link
Author

sannajammeh commented Dec 31, 2021

Have you setup babel or are you using SWC for compilation? I believe the package still relies on the usage of babel to provide the decorators API (legacy)

See: https://next-api-decorators.vercel.app/docs/

I believe this can be closed. Submitted a PR to Next.JS which supports decoratorMetadata in SWC.

vercel/next.js#32914

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants