lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.45k stars 494 forks source link

Help with tsoa implementation clean architecture express and typescript #1478

Closed IamCocoDev closed 10 months ago

IamCocoDev commented 1 year ago

Sorting

Hi people i have a problem with using tsoa with typescript and express

{
  "entryFile": "src/index.ts",
  "noImplicitAdditionalProperties": "throw-on-extras",
  "controllerPathGlobs": ["src/**/*.ts"],
  "spec": {
    "outputDirectory": "public",
    "specVersion": 3
  }
}

Thas my folder structure

image

I have this problem

image

This the code iam trying to use tsoa but cant work when run tsoa spec

import { Router } from 'express';
import { IWebService, IWebServiceParams } from 'shared/ports';
import { Controller, Get, Response, Route } from 'tsoa';

@Route('Status')
export class StatusServiceExpress extends Controller implements IWebService {
  private route: Router = Router();

  constructor(public params: IWebServiceParams) {
    super();
    this.getService();
  }

  @Get()
  @Response(200, 'Up and running')
  getService() {
    this.route.get('/', (req, res) => {
      res.send('Up and running');
    });

    return this.route;
  }
}

And the lastone code its express server

import express from 'express';
import { morganConfig } from 'config/morgan';
import { IWebServer, IWebService } from 'shared/ports';
import swaggerUi from 'swagger-ui-express';

export class ExpressWebServer implements IWebServer {
  init(options: { port: number; services: IWebService[] }): void {
    this.app.use(morganConfig)
    this.app.listen(options.port, () => {
      console.log(`🧩 Bff-template up and running. Port: ${options.port} enjoy and brake it`);
    });
    this.app.use(express.json());
    this.app.use(express.urlencoded({ extended: true }));
    this.app.use(express.static('public'));
    this.app.use(
      "/docs",
      swaggerUi.serve,
      swaggerUi.setup(undefined, {
        swaggerOptions: {
          url: "/swagger.json",
        },
      })
    )

    options.services.forEach((service) => {
      this.app.use(service.params.prefixUrl, service.getService());
    });
  }

  private app = express();
}
github-actions[bot] commented 1 year ago

Hello there IamCocoDev 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

IamCocoDev commented 1 year ago

I discovered what line of code fails it

import { Router } from 'express';
private route: Router = Router();

If I quit the Router type like that code works fine!

import { Router } from 'express';
private route: any = Router();
ArkasDev commented 1 year ago

You need to add the following code. To generate the required files just run tsoa spec-and-routes.

import {RegisterRoutes} from "../build/routes";
RegisterRoutes(app);
IamCocoDev commented 1 year ago

You need to add the following code. To generate the required files just run tsoa spec-and-routes.

import {RegisterRoutes} from "../build/routes";
RegisterRoutes(app);

I can't use that code he suggests because it edits the Swagger configuration and causes Nodemon to enter an infinite loop

Alexander-Welsh commented 11 months ago

@IamCocoDev you can add an ignore array to the nodemon.json file, which should avoid that infinite loop issue.

IamCocoDev commented 11 months ago

Thanks, @Alexander-Welsh, I have a question about how to work RegisterRoutes(app); because I can do all TSOA without this line!

github-actions[bot] commented 10 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days