How to use the @tsed/di.ProviderScope.SINGLETON function in @tsed/di

To help you get started, we’ve selected a few @tsed/di examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TypedProject / ts-express-decorators / packages / common / src / mvc / services / ControllerService.ts View on Github external
import {Deprecated, ProxyMap, Type} from "@tsed/core";
import {Configuration, Injectable, InjectorService, ProviderScope, ProviderType} from "@tsed/di";
import {ControllerProvider} from "../models/ControllerProvider";
import {ControllerRegistry} from "../registries/ControllerRegistry";
import {IRouteController, RouteService} from "./RouteService";

/**
 * @private
 * @deprecated
 */
@Injectable({
  scope: ProviderScope.SINGLETON,
  global: true
})
export class ControllerService extends ProxyMap | any, ControllerProvider> {
  constructor(
    private injectorService: InjectorService,
    @Configuration() private settings: Configuration,
    private routeService: RouteService
  ) {
    super(injectorService as any, {filter: {type: ProviderType.CONTROLLER}});
  }

  /**
   * @deprecated
   */

  /* istanbul ignore next */
github TypedProject / ts-express-decorators / packages / common / src / config / services / ServerSettingsService.ts View on Github external
const rootDir = process.cwd();
/**
 * @deprecated
 */
export let globalServerSettings: ServerSettingsService;
/**
 * @deprecated
 */
// tslint:disable-next-line: variable-name
export let GlobalServerSettings: ServerSettingsService;

/**
 * `ServerSettingsService` contains all information about [ServerLoader](/api/common/server/components/ServerLoader.md) configuration.
 */
@Injectable({
  scope: ProviderScope.SINGLETON,
  global: true
})
export class ServerSettingsService implements IServerSettings, IDISettings {
  protected map = new Map();

  constructor() {
    this.rootDir = rootDir;
    this.env = (process.env.NODE_ENV as Env) || Env.DEV;
    this.port = 8080;
    this.httpsPort = 8000;
    this.version = "1.0.0";
    this.uploadDir = "${rootDir}/uploads";
    this.controllerScope = ProviderScope.SINGLETON;
    this.logger = {
      debug: false,
      level: "info",
github TypedProject / ts-express-decorators / packages / common / src / config / services / ServerSettingsService.ts View on Github external
constructor() {
    this.rootDir = rootDir;
    this.env = (process.env.NODE_ENV as Env) || Env.DEV;
    this.port = 8080;
    this.httpsPort = 8000;
    this.version = "1.0.0";
    this.uploadDir = "${rootDir}/uploads";
    this.controllerScope = ProviderScope.SINGLETON;
    this.logger = {
      debug: false,
      level: "info",
      logRequest: true,
      jsonIndentation: this.env === Env.PROD ? 0 : 2
    };
    this.errors = {
      headerName: "errors"
    };

    this.mount = {
      "/rest": "${rootDir}/controllers/**/*.ts"
    };

    this.exclude = ["**/*.spec.ts", "**/*.spec.js"];
github TypedProject / ts-express-decorators / packages / common / src / server / utils / createHttpsServer.ts View on Github external
import {InjectorService, ProviderScope, registerProvider} from "@tsed/di";
import * as Https from "https";
import {ServerSettingsService} from "../../config";
import {ExpressApplication} from "../decorators/expressApplication";
import {HttpsServer} from "../decorators/httpsServer";

export function createHttpsServer(injector: InjectorService): void {
  injector.forkProvider(HttpsServer);
}

registerProvider({
  provide: HttpsServer,
  deps: [ExpressApplication, ServerSettingsService],
  scope: ProviderScope.SINGLETON,
  global: true,
  useFactory(expressApplication: ExpressApplication, settings: ServerSettingsService) {
    const options = settings.httpsOptions;

    return Https.createServer(options, expressApplication);
  }
});
github TypedProject / ts-express-decorators / packages / common / src / server / utils / createHttpServer.ts View on Github external
import {InjectorService, ProviderScope, registerProvider} from "@tsed/di";
import * as Http from "http";
import {ExpressApplication} from "../decorators/expressApplication";
import {HttpServer} from "../decorators/httpServer";

export function createHttpServer(injector: InjectorService): void {
  injector.forkProvider(HttpServer);
}

registerProvider({
  provide: HttpServer,
  deps: [ExpressApplication],
  scope: ProviderScope.SINGLETON,
  global: true,
  useFactory(expressApplication: ExpressApplication) {
    return Http.createServer(expressApplication);
  }
});
github TypedProject / ts-express-decorators / packages / common / src / server / utils / createExpressApplication.ts View on Github external
import {InjectorService, ProviderScope, registerProvider} from "@tsed/di";
import * as Express from "express";
import {HandlerBuilder} from "../../mvc";
import {ExpressApplication} from "../decorators/expressApplication";

export function createExpressApplication(injector: InjectorService): void {
  injector.forkProvider(ExpressApplication);
}

registerProvider({
  provide: ExpressApplication,
  deps: [InjectorService],
  scope: ProviderScope.SINGLETON,
  global: true,
  useFactory(injector: InjectorService) {
    const expressApp = Express();
    const originalUse = expressApp.use;

    expressApp.use = function(...args: any[]) {
      args = args.map(arg => {
        if (injector.has(arg)) {
          arg = HandlerBuilder.from(arg).build(injector);
        }

        return arg;
      });

      return originalUse.call(this, ...args);
    };