Giter Site home page Giter Site logo

depinjection's Introduction

Depsin

npm version downloads download size

About

Depsin is a lightweight dependency injector for JavaScript/Typescript.

Installation

You can install depsin using npm or yarn:

$ npm install depsin
$ yarn add depsin

If you use Typescript and want to use the decorator @inject you muste add in your tsconfig.json

{
  "compilerOptions": {
    ...
    "experimentalDecorators": true,
    ...
  }
}

Examples

Typescript

import { createContainer, DEPS_SYMBOL, inject } from "depsin";

enum TYPES {
  Auth = "auth",
  Login = "login",
  App = "app",
  Url = "url"
}

const url = "http://myurl.web";

class Auth {
  private token: string;

  constructor(@inject(TYPES.Url) private url: string) {}

  login() {
    console.log(this.url);
    this.token = "I'm a token";
  }

  getToken() {
    return this.token;
  }
}

class LoginService {
  constructor(@inject(TYPES.Auth) private auth: Auth) {}

  login() {
    this.auth.login();
    return "You are logged";
  }
}

function App(auth, service) {
  return {
    makeLogin() {
      return service.login();
    },
    token() {
      return auth.getToken();
    }
  }
}
App[DEPS_SYMBOL] = [TYPES.Auth, TYPES.Login];

const container = createContainer({
  [TYPES.Login]: { asClass: LoginService },
  [TYPES.Url]: { asValue: url }
})
container.register(TYPES.App).asFunction(App);
container.register(TYPES.Auth).asClass(Auth).toSingleton();

const app = container.get<ReturnType<typeof App>>(TYPES.App);
console.log(app.token()); // undefined
console.log(app.makeLogin()); // http://myurl.web && You are logged
console.log(app.token()); // I'm a token

JavaScript

const { createContainer, DEPS_SYMBOL } = require("depsin");

const TYPES = {
  Auth: "auth",
  Login: "login",
  App: "app"
};

class Auth {
  login() {
    this.token = "I'm a token";
  }

  getToken() {
    return this.token;
  }
}

class LoginService {
  constructor(auth) {
    this.auth = auth;
  }

  login() {
    this.auth.login();
    return "You are logged";
  }
}

LoginService[DEPS_SYMBOL] = [TYPES.Auth];

function App(auth, service) {
  return {
    makeLogin() {
      return service.login();
    },
    token() {
      return auth.getToken();
    }
  }
}
App[DEPS_SYMBOL] = [TYPES.Auth, TYPES.Login];

const container = createContainer({
  [TYPES.Login]: { asClass: LoginService }
})
container.register(TYPES.App).asFunction(App);
container.register(TYPES.Auth).asClass(Auth).toSingleton();

const app = container.get(TYPES.App);
console.log(app.token()); // undefined
console.log(app.makeLogin()); // You are logged
console.log(app.token()); // I'm a token

References

depinjection's People

Contributors

evuz avatar dependabot[bot] avatar

Watchers

 avatar James Cloos avatar

depinjection's Issues

README

Create an awesomen README

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.