Skip to content

Instantly share code, notes, and snippets.

@Romakita
Last active April 3, 2022 13:56
Show Gist options
  • Save Romakita/22cc1c7f3a0e9dce322c929f28861304 to your computer and use it in GitHub Desktop.
Save Romakita/22cc1c7f3a0e9dce322c929f28861304 to your computer and use it in GitHub Desktop.
Custom controller loader
export abstract class BaseEntityController {
readonly entity: string;
@Get('/')
list() {
return []
 }
/// etc
}
import {Platform, createRouter, InjectorService, registerController} from "@tsed/common";
@Module()
export class CmsModule {
@Inject()
protected plaform: Platform;
@Inject()
protected injector: InjectorService;
$onRoutesInit() {
const entities = ['products', 'product_categories'];
entities.map((entity) => {
const token = class extends BaseEntityController {
readonly entity: string = entity;
}
registerController({
provide: token
});
// attach a new router to the controller. this method return an instance of PlatformRouter
createRouter(this.injector, token);
this.platform.addRoute('/cms/' + entity, controller)
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment