ifex
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

ifex

NPM

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'.

import {
    HttpInterfaces,
    HttpController,
    controller,
    httpDelete,
    httpGet,
    httpPost,
    body,
    pathParam,
    queryParam
} from 'ifex';
import IActionResult = HttpInterfaces.IActionResult;
 
@controller('/foo')
export class FooController extends HttpController {
 
    @httpGet('/:id')
    private index(@pathParam('id') id: string): IActionResult {
        return this.json([]);
    }
 
    @httpGet('/')
    private list(@queryParam('skip') skip: number, @queryParam('limit') limit: number): IActionResult {
        return this.json([]);
    }
 
    @httpPost('/')
    private create(@body() data: {}): IActionResult {
        return this.json({
            created: true
        });
    }
 
    @httpDelete('/:id')
    private delete(@pathParam('id') id: string): IActionResult {
        return this.json({
            deleted: true
        });
    }
}
 

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.

import {Server, App} from 'ifex';
const server = new Server(App.container, {
    rootPath: '/'
});
server.build().listen(3000);

Server

A wrapper for an express Application.

.build()

Attaches all registered controllers and middleware to the express application. Returns the application instance.

// ...
let server = new Server(container);
server
    .build()
    .listen(3000, '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.

Package Sidebar

Install

npm i ifex

Weekly Downloads

4

Version

0.1.0

License

MIT

Unpacked Size

75.8 kB

Total Files

42

Last publish

Collaborators

  • uybv