Decorator-based service container for TypeScript

Overview

Capsule

Lightweight TypeScript service container.

Note: This is just quick preview of the documentation, not even alpha quality. For more detailed stuff, please check the tests as they contain more examples.

Installation

Deno

Capsule is built and tested for Deno. You can install it simply by importing:

import { container } from 'https://deno.land/x/capsule_sc/mod.ts';

Usage

The idea behind Capsule is to have super declarative and explicit service container. Thanks to this approach we managed to create Capsule without any reflection libraries.

class UserRepository {
  username() {
    return "@capsule";
  }
}

class UserService {
  constructor(
    @Inject(UserRepository) public repo: UserRepository,
  ) {}
}

Now we can construct the UserService using get.

container.get<UserService>(UserService).repo.username();

// "@capsule"

Binding to interfaces

In TypeScript interfaces are wiped during the compilation, so we can't use them as a values. As replacement, Capsule lets you bind using symbols. This is quite effective replacement for binds to interface. Let's see how it works.

The first part is as you would expect. Having a class that implements a interface.

interface Repository {
  username(): string;
}

class UserRepository implements Repository {
  username() {
    return "@capsule";
  }
}

Now, as identifier we use Symbol.

const repositoryInterface = Symbol();

Let's bind a property and as identifier use repositoryInterface.

container.set(repository, UserService, () => new UserRepository(), {
  position: 0,
  property: "constructor",
});

... and instead of @Inject we make use of @InjectSymbol:

class UserService {
  constructor(
    @InjectSymbol(repository) public repo: Repository
  ) {}
}

Notice how type is interface, not specific class.

container.get<UserService>(UserService).repo.username();

// "@capsule"

Licence

The MIT License (MIT). Please see licence file for more information.

You might also like...

ContainerMenu is an API for BDSX that allows you to create fake interactive container menus !

ContainerMenu is an API for BDSX that allows you to create fake interactive container menus !

ContainerMenu - A BDSX API ContainerMenu is an API for BDSX that allows you to create fake interactive container menus ! Features Multiple containers

Oct 28, 2022

Jugglr is a tool for managing test data and running tests with a dedicated database running in a Docker container.

Jugglr Jugglr is a tool for managing test data and running tests with a lightweight, dedicated database. Jugglr enables developers, testers, and CI/CD

Aug 20, 2022

A docker container with a wide variety of tools for debugging and setting up micro-services

Frame One Software Placeholder There are numerous times during the dev ops deployments, that a placeholder container is needed. In the past, Frame One

May 29, 2022

Pin any element within a container

jQuery.pin Ever wanted to pin something to the side of a text? Ever needed a subtle sticky element to quietly hang around as you scroll down? Jquery.P

Nov 30, 2022

Public repository of assets used during editions of AWS LATAM Container Roadshow event.

LATAM Containers Roadshow This is the official repository of assets related to LATAM Containers Roadshow, an all-day customer-facing event to highligh

Dec 6, 2022

Make text fit container, prevent overflow and underflow.

Make text fit container, prevent overflow and underflow.

AutoTextSize Make text fit container, prevent overflow and underflow. The font size of the text is adjusted so that it precisely fills its container.

Dec 30, 2022

Moject is a IoC container and an app factory built around the modules idea of Angular and NestJs.

Moject Moject is an IoC container and an app factory package built around the modules idea of Angular and NestJs. Usage npm install moject Use @Mo

Dec 4, 2022

A jQuery plugin allowing you to scroll an image within a container element

A jQuery plugin allowing you to scroll an image within a container element

jQuery Scroll Image Inside v0.1 A jQuery plugin allowing you to scroll an image within a container element Usage div id="window" img src="reall

Apr 11, 2021

The app's backend is written in Python (Flask) and for search it uses Elasticsearch. I used this app as candidate application for learning out how to build, run and deploy a multi-container environment (docker-compose).

foodtrucks-app-docker-compose The app's backend is written in Python (Flask) and for search it uses Elasticsearch. I used this app as candidate applic

Oct 24, 2022
Releases(v0.1.0-alpha)
Owner
Benjamin Beganović
Benjamin Beganović
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Open Horizon service container demonstrating Node-RED Object Detection

service-node-red-object-detection Open Horizon service container demonstrating Node-RED Object Detection. This is an Open Horizon configuration to dep

null 4 Dec 25, 2022
TypeScript plugin for service-to-service (aka. "functionless") cloud integrations.

Functionless λ< Functionless is a TypeScript plugin that transforms TypeScript code into Service-to-Service (aka. "functionless") integrations, such a

sam 303 Jan 2, 2023
The invoker based on event model provides an elegant way to call your methods in another container via promisify functions

The invoker based on event model provides an elegant way to call your methods in another container via promisify functions. (like child-processes, iframe, web worker etc).

尹挚 7 Dec 29, 2022
RESTful service to provide API linting as-a-service

API Linting Service Prerequisites / general idea General idea behind this API implementation is to provide an API as a service based on the awesome sp

Schwarz IT 6 Mar 14, 2022
A container-friendly alternative to os.cpus().length. Both cgroups v1 and cgroups v2 are supported.

node-cpu-count A container-friendly alternative to os.cpus().length. Both cgroups v1 and cgroups v2 are supported. Installation $ npm install node-cpu

Jiahao Lu 2 Jan 17, 2022
Container Image Signing & Verifying on Ethereum [Testnet]

cosigneth An experimental decentralized application for storing and verifying container image signatures as an NFT on Ethereum cosigneth, is a decentr

Furkan Türkal 17 Jul 4, 2022
Mailbox is the predictable states & transitions container for actors.

Mailbox (turns XState Machine into a REAL Actor) Mailbox is an NPM module built on top of the XState machine, by adding a message queue to the XState

Huan (李卓桓) 40 Aug 24, 2022