seanpmaxwell / overnight

TypeScript decorators for the ExpressJS Server.
MIT License
878 stars 40 forks source link

Set prefix in all controllers #53

Closed ivanguimam closed 4 years ago

ivanguimam commented 4 years ago

I have the same problem this issue https://github.com/seanpmaxwell/overnight/issues/48.

Only I'm using inversify to inject the services into my controllers, so I can't use a parent controller.

ControllerExample.ts

import { Controller, Get, Middleware } from '@overnightjs/core'
import { injectable, inject } from 'inversify'

import TYPES from '@src/ioc/types'

import BannerService from './BannerService'

@injectable()
@Controller('banner')
class BannerController {
  private bannerService: BannerService

  constructor(@inject(TYPES.BannerService) bannerService: BannerService) {
    this.bannerService = bannerService
  }
}

export BannerController

Server.ts

import DIContainer from './ioc'
import TYPES from './ioc/types'
import BannerController from './modules/banner/BannerController'
...
const bannerController = DIContainer.get<BannerController>(TYPES.BannerController)
super.addControllers([bannerController])
ivanguimam commented 4 years ago

I managed to solve part of my problem with this snippet of code:

const bannerController = DIContainer.get<BannerController>(TYPES.BannerController)
const termsController = DIContainer.get<TermsController>(TYPES.TermsController)
super.addControllers([bannerController, termsController])

this.app.use('/app', this.app._router)

But before I had a route /banner, now I have two routes, one /banner and another /app/banner

AnandChowdhary commented 4 years ago

Just my two cents: @seanpmaxwell told me that his goal with OvernightJS is to add first-class TypeScript support to Express, not add additional features like prefixes.

In my project staart/api (Express/TypeScript starter for SaaS), I built on top of OvernightJS and added more opinionated features like prefix injection, async return support, etc. I think that's the best way for you too. 😉

seanpmaxwell commented 4 years ago

Can I close this issue in that case

seanpmaxwell commented 4 years ago

Closing issue