How to use the @tsed/di.Service 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 / filters / services / FilterService.ts View on Github external
import {Deprecated, ProxyMap, Type} from "@tsed/core";
import {Provider, Service, ProviderType, InjectorService} from "@tsed/di";
import {UnknowFilterError} from "../errors/UnknowFilterError";
import {IFilter} from "../interfaces";
import {FilterRegistry} from "../registries/FilterRegistry";

/**
 * @deprecated This service will be removed in a future release. Use injectorService directly.
 */
@Service()
export class FilterService extends ProxyMap | any, Provider> {
  constructor(private injectorService: InjectorService) {
    super(injectorService, {filter: {type: ProviderType.FILTER}});
  }

  /**
   *
   * @param target
   * @returns {ControllerProvider}
   */
  @Deprecated("static FilterService.get(). Removed feature.")
  /* istanbul ignore next */
  static get(target: Type): Provider | undefined {
    return FilterRegistry.get(target);
  }
github TypedProject / ts-express-decorators / packages / common / src / mvc / services / MiddlewareService.ts View on Github external
import {Deprecated, ProxyMap, Type} from "@tsed/core";
import {Provider, Service, ProviderType, ProviderRegistry, InjectorService} from "@tsed/di";
import {UnknowMiddlewareError} from "../errors/UnknowMiddlewareError";
import {IMiddleware} from "../interfaces";

/**
 * @deprecated This service will be removed in a future release. Use injectorService directly.
 */
@Service()
export class MiddlewareService extends ProxyMap | any, Provider> {
  constructor(private injectorService: InjectorService) {
    super(injectorService, {filter: {type: ProviderType.MIDDLEWARE}});
  }

  /**
   *
   * @param target
   * @returns {Provider}
   * @deprecated
   */
  @Deprecated("static MiddlewareService.get(). Removed feature.")
  /* istanbul ignore next */
  static get(target: Type): Provider | undefined {
    return ProviderRegistry.get(target);
  }
github TypedProject / ts-express-decorators / packages / common / src / server / services / ServeStaticService.ts View on Github external
import {Service} from "@tsed/di";
import * as Express from "express";
import {IServerMountDirectories} from "../../config";
import {ExpressApplication} from "../decorators/expressApplication";

@Service()
export class ServeStaticService {
  constructor(@ExpressApplication private expressApp: Express.Application) {}

  statics(statics: IServerMountDirectories) {
    /* istanbul ignore else */
    Object.keys(statics).forEach(path => {
      [].concat(statics[path] as any).forEach((directory: string) => this.mount(path, directory));
    });
  }

  mount(path: string, directory: string) {
    const middleware = Express.static(directory);
    this.expressApp.use(path, (request: any, response: any, next: any) => {
      if (!response.headersSent) {
        middleware(request, response, next);
      } else {
github TypedProject / ts-express-decorators / examples / passport-azure-ad / packages / server / src / services / auth / AuthService.ts View on Github external
import {Service} from "@tsed/di";
import {ITokenPayload} from "passport-azure-ad";
import {$log} from "@tsed/common";
import {TenantIdError} from "./errors/TenantIdError";
import {ClientIdError} from "./errors/ClientIdError";
import {InsufficientScopePermissions} from "./errors/InsufficientScopePermissions";

require("dotenv").config();

/**
 * Customise this as required.  Remember that Azure handles most user authentication/authorization so what
 * happens here is only to provide functional benefits to the application.  The Azure auth happens in the
 * protocols / BearerStrategy class for passport-azure-ad.
 */
@Service()
export class AuthService {
  owner = null;
  scopes = process.env.Scopes ? process.env.Scopes.split(",") : [];

  static getClientId(): string {
    return process.env.clientId;
  }

  static getTenantId(): string {
    return process.env.tenantId;
  }

  add(token: ITokenPayload) {
    this.owner = token.oid;
  }
github TypedProject / ts-express-decorators / packages / common / src / mvc / services / ValidationService.ts View on Github external
import {Service} from "@tsed/di";

@Service()
export class ValidationService {
  public validate(obj: any, targetType: any, baseType?: any): boolean {
    return true;
  }
}
github TypedProject / ts-express-decorators / packages / common / src / jsonschema / services / JsonSchemesService.ts View on Github external
import {JSONSchema6} from "json-schema";
import {ProxyRegistry, Type} from "@tsed/core";
import {Service} from "@tsed/di";
import {JsonSchema} from "../class/JsonSchema";
import {JsonSchemesRegistry} from "../registries/JsonSchemesRegistry";

@Service()
export class JsonSchemesService extends ProxyRegistry {
  private cache: Map, JSONSchema6> = new Map();

  constructor() {
    super(JsonSchemesRegistry);
  }

  /**
   *
   * @param {Type} target
   * @returns {JSONSchema4}
   */
  getSchemaDefinition(target: Type): JSONSchema6 | undefined {
    if (!this.cache.has(target)) {
      this.cache.set(target, JsonSchemesRegistry.getSchemaDefinition(target));
    }
github TypedProject / ts-express-decorators / packages / common / src / mvc / services / RouteService.ts View on Github external
import {Constant, InjectorService, Service} from "@tsed/di";
import {$log} from "ts-log-debug";
import {colorize} from "ts-log-debug/lib/layouts/utils/colorizeUtils";
import {AfterRoutesInit} from "../../server/interfaces/AfterRoutesInit";
import {ControllerProvider} from "../class/ControllerProvider";
import {EndpointMetadata} from "../class/EndpointMetadata";
import {IControllerRoute} from "../interfaces";

/**
 * `RouteService` is used to provide all routes collected by annotation `@Controller`.
 */
@Service()
export class RouteService implements AfterRoutesInit {
  /**
   *
   */
  @Constant("logger.disableRoutesSummary", false)
  disableRoutesSummary: boolean;

  private readonly _routes: {route: string; provider: any}[] = [];

  constructor(private injector: InjectorService) {}

  /**
   *
   * @returns {{route: string; provider: any}[]}
   */
  get routes(): {route: string; provider: any}[] {
github TypedProject / ts-express-decorators / packages / common / src / mvc / services / ParseService.ts View on Github external
import {getValue, isEmpty} from "@tsed/core";
import {Service} from "@tsed/di";

/**
 *
 */
@Service()
export class ParseService {
  constructor() {}

  /**
   * Clone an object.
   * @param src
   */
  static clone = (src: any): any => JSON.parse(JSON.stringify(src));

  /**
   * Eval an expression with a scope context and return value.
   * @param expression
   * @param scope
   * @param clone
   * @returns {any}
   */
github TypedProject / ts-express-decorators / packages / common / src / mvc / services / RouteService.ts View on Github external
import {InjectorService, Service, TokenProvider} from "@tsed/di";
import {ExpressApplication} from "../../server/decorators/expressApplication";
import {IControllerRoute} from "../interfaces";
import {ControllerProvider} from "../models/ControllerProvider";
import {EndpointMetadata} from "../models/EndpointMetadata";

export interface IRouteProvider {
  route: string;
  provider: ControllerProvider;
}

/**
 * `RouteService` is used to provide all routes collected by annotation `@Controller`.
 */
@Service()
export class RouteService {
  private readonly _routes: IRouteProvider[] = [];

  constructor(private injector: InjectorService, @ExpressApplication private expressApplication: ExpressApplication) {}

  get routes(): IRouteProvider[] {
    return this._routes || [];
  }

  /**
   * Add a new route in the route registry
   * @param endpoint
   * @param token
   */
  public addRoute(endpoint: string, token: TokenProvider) {
    if (this.injector.hasProvider(token)) {