-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mvc-express): Make
MvcExpressModule
act like a normal module
BREAKING CHANGE: removes defaults helpers in favor of overriding with providers supports #24
- Loading branch information
Daniel Schaffer
committed
Mar 1, 2019
1 parent
32d6742
commit bf46773
Showing
1 changed file
with
11 additions
and
46 deletions.
There are no files selected for viewing
57 changes: 11 additions & 46 deletions
57
packages/dandi-contrib/mvc-express/src/mvc-express.module.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,25 @@ | ||
import { Constructor } from '@dandi/common' | ||
import { Bootstrapper, ModuleBuilder, Provider, Registerable } from '@dandi/core' | ||
import { Route, RouteExecutor, RouteGenerator, RouteHandler, RouteInitializer, RouteMapper } from '@dandi/mvc' | ||
import { Express } from 'express' | ||
import { ModuleBuilder, Registerable } from '@dandi/core' | ||
import { MvcModule } from '@dandi/mvc' | ||
|
||
import { DEFAULT_EXPRESS_PROVIDER } from './default.express.provider' | ||
import { ExpressMvcApplication } from './express.mvc.application' | ||
import { ExpressMvcConfig } from './express.mvc.config' | ||
import { ExpressMvcRouteMapper } from './express.mvc.route.mapper' | ||
import { PKG } from './local.token' | ||
|
||
export interface ExpressMvcApplicationConfig { | ||
expressInstanceProvider: Provider<Express>; | ||
routeExecutor: Constructor<RouteExecutor>; | ||
routeGenerator: Constructor<RouteGenerator>; | ||
routeHandler: Constructor<RouteHandler>; | ||
routeInitializer: Constructor<RouteInitializer>; | ||
routeMapper: Constructor<RouteMapper>; | ||
bootstrap: Constructor<Bootstrapper>; | ||
routesProvider: Provider<Route[]>; | ||
} | ||
export type ExpressMvcApplicationOptions = { | ||
[P in keyof ExpressMvcApplicationConfig]?: ExpressMvcApplicationConfig[P] | ||
} | ||
|
||
export class MvcExpressModuleBuilder extends ModuleBuilder<MvcExpressModuleBuilder> { | ||
constructor(...entries: Registerable[]) { | ||
super(MvcExpressModuleBuilder, PKG, ...entries) | ||
} | ||
|
||
private getConfiguredRegisterables(options: ExpressMvcApplicationOptions) { | ||
const config: ExpressMvcApplicationConfig = { | ||
expressInstanceProvider: | ||
options.expressInstanceProvider || require('./default.express.provider').DEFAULT_EXPRESS_PROVIDER, | ||
routeExecutor: options.routeExecutor || require('@dandi/mvc').DefaultRouteExecutor, | ||
routeGenerator: options.routeGenerator || require('@dandi/mvc').DecoratorRouteGenerator, | ||
routeHandler: options.routeHandler || require('@dandi/mvc').DefaultRouteHandler, | ||
routeInitializer: options.routeInitializer || require('@dandi/mvc').DefaultRouteInitializer, | ||
routeMapper: options.routeMapper || require('./express.mvc.route.mapper').ExpressMvcRouteMapper, | ||
bootstrap: options.bootstrap || require('./express.mvc.application').ExpressMvcApplication, | ||
routesProvider: options.routesProvider || require('@dandi/mvc').ROUTES_PROVIDER, | ||
} | ||
return Object.values(config).filter((v) => v) | ||
} | ||
|
||
public providers(options: ExpressMvcApplicationOptions = {}): this { | ||
return this.add(...this.getConfiguredRegisterables(options)) | ||
} | ||
|
||
public config(mvcConfig: ExpressMvcConfig): this { | ||
return this.add({ provide: ExpressMvcConfig, useValue: mvcConfig }) | ||
} | ||
} | ||
|
||
export class MvcExpressModule { | ||
public static withProviders(options: ExpressMvcApplicationOptions): MvcExpressModuleBuilder { | ||
return new MvcExpressModuleBuilder().providers(options) | ||
} | ||
|
||
public static withDefaults(): MvcExpressModuleBuilder { | ||
return new MvcExpressModuleBuilder().providers() | ||
} | ||
} | ||
export const MvcExpressModule = new MvcExpressModuleBuilder( | ||
MvcModule, | ||
DEFAULT_EXPRESS_PROVIDER, | ||
ExpressMvcApplication, | ||
ExpressMvcRouteMapper, | ||
) |