1.0.5 • Published 2 years ago

@thunderbird-js/container v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@thunderbird-js/container

Thunderbird container is a fast, lightweight and friendly API dependency injection container for TypeScript and JavaScript apps.

Installation

npm install @thunderbird-js/container --save

Philosophy

The main idea of this container is that the developer knows best what the application should look like, and the tools that will be used should be flexible and correspond to what is required of them, and nothing more.

Features

  • Lightweight: You get required minimum of dependency injection container wich can be used like you want
  • Developer friendly: The container is built to be very simple and help the developer implement they ideas without spending hours of reading API documentation.

Container

has(key)

container.addTransient(SomeClass, SomeClass)
container.has(SomeClass) // true

tryAddScoped(key, value)

Method which register the service in context scope only if there isn't already an implementation registered.

container.tryAddScoped(SomeClass, SomeClass)
container.tryAddScoped(SomeClass, (app) => new SomeClass(app.config.property))
container.tryAddScoped(SomeClass, (app, parameters) => new SomeClass(...parameters))

tryAddSingleton(key, value)

Method which register the service in singleton scope only if there isn't already an implementation registered

container.tryAddSingleton(SomeClass, SomeClass)
container.tryAddSingleton(SomeClass, (app) => new SomeClass(app.config.property))
container.tryAddSingleton(SomeClass, (app, parameters) => new SomeClass(...parameters))

tryAddTransient(key, value)

Method which register the service in transient scope only if there isn't already an implementation registered

container.tryAddTransient(SomeClass, SomeClass)
container.tryAddTransient(SomeClass, (app) => new SomeClass(app.config.property))
container.tryAddTransient(SomeClass, (app, parameters) => new SomeClass(...parameters))

addScoped(key, value)

Method which register the service in context scope if there is already an implementation registered it will rebinded.

container.addScoped(SomeClass, SomeClass)
container.addScoped(SomeClass, (app) => new SomeClass(app.config.property))
container.addScoped(SomeClass, (app, parameters) => new SomeClass(...parameters))

addSingleton(key, value)

Method which register the service in singleton scope if there is already an implementation registered it will rebinded.

container.addSingleton(SomeClass, SomeClass)
container.addSingleton(SomeClass, (app) => new SomeClass(app.config.property))
container.addSingleton(SomeClass, (app, parameters) => new SomeClass(...parameters))

addTransient(key, value)

Method which register the service in transient scope if there is already an implementation registered it will rebinded.

container.addTransient(SomeClass, SomeClass)
container.addTransient(SomeClass, (app) => new SomeClass(app.config.property))
container.addTransient(SomeClass, (app, parameters) => new SomeClass(...parameters))

make(key)

Method which resolve a class instance from the container.

container.make(SomeClass)

makeWith(key, ...params)

Method which resolve a class instance from the container with given parameters.

container.makeWith(SomeClass, ...params)

remove(key)

Method which remove implementation from container with given key.

container.remove(SomeClass)

makeProviders(...providers)

Method which will passed resolve service providers.

import { ContainerServiceProvider } from "@thunderbird-js/container"

class Provider extends ContainerServiceProvider {
	load() {
		this.container.addSingletone(SomeClass, (app, params) => new SomeClass(...params))
		this.container.addSingletone(SomeOtherClass, () => new SomeOtherClass(this.container.makeWith(SomeClass, ...params)))
	}
}
const provider = new Provider()

container.makeProviders(provider)

makeProvidersAsync(...asyncProviders)

Method which will async resolve resolve service providers.

import { AsyncContainerServiceProvider } from "@thunderbird-js/container"

class Provider extends AsyncContainerServiceProvider {

	async load() {

			this.container.addSingletone(SomeClass, (app, params) => new SomeClass(...params))
			this.container.addSingletone(SomeOtherClass, () => new SomeOtherClass(this.container.makeWith(SomeClass, ...params)))
	}
}

const provider = new Provider()

await container.makeProviders(provider)

createContext(cb)

Method which creates a new async function that, accept parameters and when called, shall execute callback function in new context.

await container.createContext(async (params) => {
	// creates new context where can be used context binded implementation
}})(params)
app.get("route", container.createContext(async (req, res) => {
	// per request scope })))

License

MIT License Copyright (c) 2022 thunderbird-js

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.

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago