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

TypeError: Cannot call a class as a function #6586

Closed
clayrisser opened this issue Mar 7, 2021 · 5 comments
Closed

TypeError: Cannot call a class as a function #6586

clayrisser opened this issue Mar 7, 2021 · 5 comments
Labels
needs triage This issue has not been looked into

Comments

@clayrisser
Copy link

Bug Report

NestJS does not correctly detect class when babel transforms class to function.

Current behavior

throws following error

TypeError: Cannot call a class as a function

Input Code

.baberc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "corejs": 3,
        "spec": true,
        "useBuiltIns": "usage",
        "targets": {
          "node": "12"
        }
      }
    ],
    "@babel/preset-typescript"
  ],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
    "@babel/plugin-proposal-optional-chaining",
    "@babel/plugin-transform-runtime"
  ]
}
import { Injectable, Inject, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export default class SomeMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    console.log('some middleware');
    next();
  }
}

Expected behavior

Should log some middleware when api is called.

Possible Solution

Update isClass implementation to detect a class that was transformed to a function.

export function isClass(middleware: any): middleware is Type<any> {

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: v12.20.0
- Platform:  Debian Linux
- NestJS: 7.6.13
@clayrisser clayrisser added the needs triage This issue has not been looked into label Mar 7, 2021
@jmcdo29
Copy link
Member

jmcdo29 commented Mar 7, 2021

Please provide a minimum reproduction

@clayrisser
Copy link
Author

clayrisser commented Mar 7, 2021

This is worth reading regarding this issue. Basically it's only checking if it's a native class. It's not checking if it's a conventional class.

https://stackoverflow.com/a/49510834

Here is an example of a regex that checks for a conventional class. If we're worried that checking for a conventional class will trigger a false positive, we could also use duck typing to check if the use() method exists.

https://github.com/bevry/typechecker/blob/master/source/index.ts#L5

@clayrisser
Copy link
Author

clayrisser commented Mar 7, 2021

Please provide a minimum reproduction

docker run --rm -p 27017:27017 mongo
npm install -g pnpm
git clone https://github.com/clayrisser/example-graphback-nestjs
cd example-graphback-nestjs
make start
curl localhost:3000

clayrisser pushed a commit to clayrisser/nest that referenced this issue Mar 7, 2021
clayrisser pushed a commit to clayrisser/nest that referenced this issue Mar 7, 2021
clayrisser pushed a commit to clayrisser/nest that referenced this issue Mar 7, 2021
@clayrisser
Copy link
Author

I created a pull request to fix this. #6587

@kamilmysliwiec
Copy link
Member

Let's track this herehttps://github.com//pull/6587

kamilmysliwiec added a commit that referenced this issue Mar 12, 2021
…leware-class

fix(core): detect conventional middleware class #6586
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants