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

Help with tsoa implementation clean architecture express and typescript #1478

Closed
2 of 4 tasks
IamCocoDev opened this issue Sep 5, 2023 · 7 comments
Closed
2 of 4 tasks
Labels

Comments

@IamCocoDev
Copy link

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

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
Copy link

github-actions bot commented Sep 5, 2023

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
Copy link
Author

IamCocoDev commented Sep 5, 2023

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
Copy link

ArkasDev commented Sep 10, 2023

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
Copy link
Author

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
Copy link

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

@IamCocoDev
Copy link
Author

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

@github-actions
Copy link

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

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

No branches or pull requests

3 participants