Last active
April 3, 2022 13:56
-
-
Save Romakita/22cc1c7f3a0e9dce322c929f28861304 to your computer and use it in GitHub Desktop.
Custom controller loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export abstract class BaseEntityController { | |
readonly entity: string; | |
@Get('/') | |
list() { | |
return [] | |
} | |
/// etc | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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