1.0.0 • Published 3 years ago

controlled v1.0.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
3 years ago

Controlled

Build Status

Minimal toolkit to use Express with DI. Powered by Containor.

  • Use controllers in Express with ease.
  • Lazy instantiate controllers.
  • Inject services using DI.
  • Use middleware with DI.

📖 Documentation

Getting started

Setting up Controlled is very simple, we need to have Express and Containor (for our dependency injection) too and we are good to go!

Installation

Controlled (and Containor) can be installed by using any package manager using the npm repository.

npm install express containor controlled

With yarn:

yarn add express containor controlled

Controlled ships with Typescript types included, these do not have to be installed separately.

Basic usage

import express, { Request, Response } from "express";
import { createContainer, token } from "containor";
import { createControlled } from "controlled";

const app = express();
const container = createContainer();
const controlled = createControlled(container);

const tokens = {
  healthController: token<HealthController>("healthController"),
};

class HealthController {
  get(req: Request, res: Response): void {
    res.sendStatus(200);
  }
}

container.add(tokens.healthController, HealthController);

app.get("/health", controlled(tokens.healthController, "get"));

app.listen(8080);

As you can see, Controlled doesn't get in the way of your regular Express code, the only difference is the request handler is now created by Controlled!