New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@travisk/practice-lib

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@travisk/practice-lib

--- ![Build Status](https://github.com/Travis-Kirton/practice-lib/actions/workflows/ci.yml/badge.svg)

  • 0.0.7
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

IoC / DI Container


Build Status

This is a minimal IoC / DI Container that supports injection of dependencies for TypeScript.

Feature Set:

  • Object injection
  • constructor injection
  • Singleton / Transient services for class instantiation
  • Can support multiple containers but initial @Service are registered on each container created

Installation

Install the package via npm

npm install @travisk/practice-lib

Ensure you install reflect-metadata as this library requires it

npm install reflect-metadata

Import reflect-metadata when using the decorators such as @Service

import 'reflect-metadata';

Enable decorators and metadata within your tsconfig.json

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

Usage

Injecting constructor dependencies (Transient by default)

  • Use @Service above the class declaration to allow constructor injection
import 'reflect-metadata';
import { Service } from "./decorators";
import { Container } from './container';

class UserRepository {

    getAllUserIds(): number[] {
        return [1, 2];
    }

}


@Service()
class UserService {

    constructor(private userRepo: UserRepository) {}

    getUserData = (): number[] => {
        return this.userRepo.getAllUserIds();
    }
}

const container = new Container();
const userService = container.get(UserService);

const userIds = userService.getUserData(); // returns [1, 2]

Injecting Singleton dependencies

import 'reflect-metadata';
import { Service } from "./decorators";
import { Container } from './container';

class UuidGenerator {
    private uuid;
    constructor(){
        this.uuid = new randomUuid(); // generate uuid
    }
    getUuid(): string {
        return this.uuid;
    }

}


@Service('Singleton')
class TestClass {

    constructor(private uuidGen: UuidGenerator) {}

    getUserId = (): string => {
        return this.uuidGen.getUuid();
    }
}

const container = new Container();
const service1 = container.get(TestClass);
const service2 = container.get(TestClass);

service1.getUserId() === service2.getUserId(); // returns true

Injecting Objects

import 'reflect-metadata';
import { Container } from './container';


const container = new Container();
container.register('token', {hello: 'world'});
const obj = container.get('token'); // returns { hello: 'world' }

Roadmap / Missing features

  • Include injection of functions, constants etc
  • Allow for Scoped lifecycle dependencies
  • Allow injected dependencies to be swapped with 'Fake' dependencies for testing

For Maintainors / Contributors

  • make sure to run lint via npm run lint
  • This library is published via changesets
    • You can run npx changeset to initialise a version bump (patch, minor, major)
    • Once committed it will create PR to be merged in
    • When the PR is merged, it will publish the new version to npm and update the CHANGELOG.md

FAQs

Package last updated on 16 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc