Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tsed/di

Package Overview
Dependencies
Maintainers
1
Versions
1029
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tsed/di

DI module for Ts.ED Framework

  • 6.133.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
decreased by-18.3%
Maintainers
1
Weekly downloads
 
Created
Source

Ts.ED logo

@tsed/di

Build & Release PR Welcome Coverage Status npm version semantic-release code style: prettier github opencollective

Website   •   Getting started   •   Slack   •   Twitter

A package of Ts.ED framework. See website: https://tsed.io/

Installation

You can get the latest release and the type definitions using npm:

npm install --save @tsed/di

Important! TsExpressDecorators requires Node >= 6, Express >= 4, TypeScript >= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.

{
  "compilerOptions": {
    "target": "es2016",
    "lib": ["es2016"],
    "typeRoots": ["./node_modules/@types"],
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true
  },
  "exclude": ["node_modules"]
}

Introduction

Basically, almost everything may be considered as a provider – service, factory, intereceptors, and so on. All of them can inject dependencies, meaning, they can create various relationships with each other. But in fact, a provider is nothing else than just a simple class annotated with an @Injectable() decorator.

Usage

Here is a basic usage to declare an injectable service to another one:

import {Injectable} from "@tsed/di";
import {Calendar} from "./models/calendar";

@Injectable()
export class CalendarsService {
  private readonly calendars: Calendar[] = [];

  create(calendar: Calendar) {
    this.calendars.push(calendar);
  }

  findAll(): Calendar[] {
    return this.calendars;
  }
}

Here's a CalendarsService, a basic class with one property and two methods. The only new trait is that it uses the @Injectable() decorator. The @Injectable() attaches the metadata, thereby Ts.ED knows that this class is a provider.

Now we have the service class already done, let's use it inside a controller:

import {Controller, Post, Body, Get} from "@tsed/common";
import {CalendarsService} from "./CalendarsService";
import {Calendar} from "./models/Calendar";

@Controller("/calendars")
export class CalendarCtrl {
  constructor(private readonly calendarsService: CalendarsService) {}

  @Post()
  async create(@Body() calendar: Calendar) {
    this.calendarsService.create(calendar);
  }

  @Get()
  async findAll(): Promise<Calendar[]> {
    return this.calendarsService.findAll();
  }
}

Note: Controller isn't a part of @tsed/di. @Controller decorator is exposed by @tsed/common package because it's a specific provider used by the Ts.ED framework. Ts.ED DI allow you to define your own Provider and decorator.

Finally, we can load the injector and use:

import {InjectorService} from "@tsed/di";
import {CalendarCtrl} from "./CalendarCtrl";

async function bootstrap() {
  const injector = new InjectorService();

  // Load all providers registered via @Injectable decorator
  await injector.load();

  const calendarController = injector.get<CalendarCtrl>();

  await calendarController.create(new Calendar());

  // And finally destroy injector and his instances (see injector hooks)
  await injector.destroy();
}

bootstrap();

Custom providers

To organize your code Ts.ED DI provide different kind of providers:

  • Provider can be declared with @Injectable,
  • Service can be declared with @Service,
  • Interceptor can be declared with @Interceptor,
  • Factory and Value can be declared with registerFactory and registerValue.

See more details on our documentation https://tsed.io/providers.html

Contributors

Please read contributing guidelines here

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2022 Romain Lenzotti

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.

FAQs

Package last updated on 27 Sep 2022

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