ifex
Some utilities for the development of express applications with Inversify & Fibers.
Installation
You can install ifex
using npm:
npm install ifex --save
The ifex
type definitions are included in the npm module and require TypeScript 3.9.
Please refer to the InversifyJS documentation to learn more about the installation process.
The Basics
Step 1: Decorate your controllers
To use a class as a "controller" for your express app, simply add the @controller
decorator to the class. Similarly, decorate methods of the class to serve as request handlers.
The following example will declare a controller that responds to `GET /foo'.
;;
Step 2: Configure container and server
Configure the inversify container in your composition root as usual.
Then, pass the container to the Server constructor. This will allow it to register all controllers and their dependencies from your container and attach them to the express app. Then just call server.build() to prepare your app.
In order for the Server to find your controllers, you must bind them to the HttpConstants.Type.Controller
service identifier and tag the binding with the controller's name.
The Controller
interface exported by ifex is empty and solely for convenience, so feel free to implement your own if you want.
;;server.build.listen3000;
Server
A wrapper for an express Application.
.build()
Attaches all registered controllers and middleware to the express application. Returns the application instance.
// ...;server .build .listen3000, 'localhost', callback;
Decorators
@controller(path, [middleware, ...])
Registers the decorated class as a controller with a root path, and optionally registers any global middleware for this controller.
@httpMethod(method, path, [middleware, ...])
Registers the decorated controller method as a request handler for a particular path and method, where the method name is a valid express routing method.
@SHORTCUT(path, [middleware, ...])
Shortcut decorators which are simply wrappers for @httpMethod
. Right now these include @httpGet
, @httpPost
, @httpPut
, @httpPatch
, @httpHead
, @httpDelete
, and @All
. For anything more obscure, use @httpMethod
(Or make a PR 😄).
@request()
Binds a method parameter to the request object.
@response()
Binds a method parameter to the response object.
@body()
Binds a method parameter to the request.body. If the bodyParser middleware is not used on the express app, this will bind the method parameter to the express request object.
@pathParam(name: string)
Binds a method parameter to request.params object or to a specific parameter if a name is passed.
@queryParam(name: string)
Binds a method parameter to request.query or to a specific query parameter if a name is passed.
@headerParam(name: string)
Binds a method parameter to the request headers.
@cookieParam(name: string)
Binds a method parameter to the request cookies.
@next()
Binds a method parameter to the next() function.
License
MIT License
Copyright (c) 2019 Uy Bui
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.