@subversify/express-utils
The @subversify/express-utils
package provides a customized decorator and
module hook for working with inversify-express-utils
controllers in a
Subversify application.
It is generally intended for working with existing inversify-express-utils
controllers as part of a progressive modularisation strategy, to overcome the
global registration approach of inversify-express-utils
. Suitable
alternatives—if any—depend on your specific situation too much for
any recommendations to be made here.
Installation
Usage
Defining/Migrating Controllers
When creating a new controller or migrating an existing controller within a
Subversify module, use this package's @controller
decorator instead of the one
exported by inversify-express-utils
.
Other decorators such as httpGet
etc. should continue to be imported from
inversify-express-utils
.
user.controller.ts | |
---|---|
Internally, the @controller
decorator proxies all arguments to the
@controller
decorator from inversify-express-utils
, but removes the
automatic global registration of the controller.
Registering Controllers
In your application module root,
register the Controllers
hook from this package. The hook is a factory
function, with zero parameters:
app.module.ts | |
---|---|
Now, in your module(s) you can register your controller by explicitly including
it in a controllers
class property. If you do not include it in a
controllers
property, it will not be registered in the Inversify Express
Server.
For static modules, you can implement WithControllers
for better type safety:
user.module.ts | |
---|---|
For module factories, you will need to use WithControllers
as a generic type
parameter to tell the base Module
constructor about the new controllers
property:
That's it. Building your Inversify Express Server is the same as the
inversify-express-utils
package documentation.
Gotchas
Moving your controller into a Subversify module means it can only access dependencies defined by that module, it's imports, or global module bindings.
If your controller currently depends on services defined in your root container, some work may be required to refactor those into modules that can be imported.