flamewow / nestjs-asyncapi

NestJS AsyncAPI module - generate documentation of your event-based services using decorators
MIT License
181 stars 24 forks source link

Running AsyncApiModule.setup logs "Warning: Invalid hook call." and kills the process #551

Open JohnICB opened 5 months ago

JohnICB commented 5 months ago

Describe the bug Running AsyncApiModule.setup causes React error to appear in logs and stops the process.

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

To Reproduce Steps to reproduce the behavior:

  1. run npm i --save nestjs-asyncapi

  2. main.ts

    const app = await NestFactory.create(AppModule);
    
    const asyncApiServer: AsyncServerObject = {
        url: `ws://localhost:${port}`,
        protocol: 'socket.io',
        protocolVersion: '4',
        description:
            'Allows you to connect using the websocket protocol to our Socket.io server.',
        variables: {
            port: {
                description: 'Secure connection (TLS) is available through port 443.',
                default: '443',
            },
        },
        bindings: {},
    };
    
    const asyncApiOptions = new AsyncApiDocumentBuilder()
        .setTitle('Feline')
        .setDescription('Feline server description here')
        .setVersion('1.0')
        .setDefaultContentType('application/json')
        .build();
    
    await AsyncApiModule.setup('/async-api', app, AsyncApiModule.createDocument(app, asyncApiOptions));
    
    // ...
  3. See error in logs and the process exists

Expected behavior The error to not appear in the logs and to start the dashboard.

Environment

    "@nestjs/axios": "^3.0.1",
    "@nestjs/cache-manager": "^2.2.0",
    "@nestjs/common": "^10.3.1",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.3.1",
    "@nestjs/event-emitter": "^2.0.3",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/mapped-types": "^2.0.4",
    "@nestjs/microservices": "^10.3.1",
    "@nestjs/passport": "^10.0.3",
    "@nestjs/platform-express": "^10.3.1",
    "@nestjs/platform-socket.io": "^10.3.1",
    "@nestjs/schedule": "^4.0.0",
    "@nestjs/swagger": "^7.2.0",
    "@nestjs/terminus": "^10.2.1",
    "@nestjs/websockets": "^10.3.1",
    ...
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "express": "^4.18.2",
    ...
    "nestjs-asyncapi": "^1.2.2",

Node v20.10.0 TS: 4.7.2 Edit: Upgraded to 5.3.3, same result Docker version 24.0.7, build afdd53b

For Tooling issues:

Others:

Edit: using an AppModule empty (no modules imported) raises the same issue I thought it was from the models, some are more complicated. I guess this rules it out Edit: Upgraded tsc version

github-actions[bot] commented 5 months ago

Hello! Thank you for filing an issue.

If this is a bug report, please include relevant logs to help us debug the problem.

flamewow commented 5 months ago

Hey @JohnICB , thanks for reporting an issue.

Can you please move your setup into a min reproduction repo? I've quickly tried in an empty project and with latest nest version and can't reproduce your issue

JohnICB commented 5 months ago

Hey! The issue probably is the fact that I have multiple dependencies to react and other libraries that has a dependency to react (such as tanstack query and tanstack router) that are of different versions Must be the mix of 18.2.0 and 17.0.2 versions of react that don't play nicely. Removing all other dependencies fixes the issue, even though async-api has dependencies to both versions of react, it somehow does not want to work with my other dependencies image ^ this is working now Installing it while having all these dependencies does not work anymore: image I'll try to setup a repo when I have the time to make it more easily to reproduce, but I think the issue is with the react-generator-sdk since it was the only dependency to react 17.0.2 at that time

danyalutsevich commented 4 months ago

I had the same problem I had a monorepo with react app and nestjs app (i used npm workspaces for monorepo) I removed nestjs app from the monorepo and at this point it have separate node_modules and the async-api started to work