How to use the @tsed/common.Middleware function in @tsed/common

To help you get started, we’ve selected a few @tsed/common 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 / docs / docs / snippets / middlewares / endpoint-middleware.ts View on Github external
import {EndpointInfo, IMiddleware, Middleware, Req} from "@tsed/common";
import {NotAcceptable} from "ts-httpexceptions";

@Middleware()
export class AcceptMimesMiddleware implements IMiddleware {
  use(@Req() request: Req, @EndpointInfo() endpoint: EndpointInfo) {

    // get the parameters stored for the current endpoint or on the controller.
    const mimes = endpoint.get(AcceptMimesMiddleware) || [];

    mimes.forEach((mime: string) => {
      if (!request.accepts(mime)) {
        throw new NotAcceptable(mime);
      }
    });
  }
}
github TypedProject / ts-express-decorators / docs / docs / snippets / authentication / auth-middleware.ts View on Github external
import {EndpointInfo, IMiddleware, Middleware, Req} from "@tsed/common";
import {Forbidden, Unauthorized} from "ts-httpexceptions";

@Middleware()
export class CustomAuthMiddleware implements IMiddleware {
  public use(@Req() request: Express.Request, @EndpointInfo() endpoint: EndpointInfo) {
    // retrieve options given to the @UseAuth decorator
    const options = endpoint.get(CustomAuthMiddleware) || {};

    if (!request.isAuthenticated()) { // passport.js method to check auth
      throw new Unauthorized("Unauthorized");
    }

    if (request.user.role !== options.role) {
      throw new Forbidden("Forbidden");
    }
  }
}
github TypedProject / ts-express-decorators / test / integration / app / middlewares / acceptmime.ts View on Github external
import * as Express from "express";
import {NotAcceptable} from "ts-httpexceptions";
import {IMiddleware, Middleware, Next, Request} from "@tsed/common";

@Middleware()
export default class TestAcceptMimeMiddleware implements IMiddleware {
  private mimes = ["application/json"];

  public use(@Request() request: Express.Request, @Next() next: Function) {
    this.mimes.forEach(mime => {
      if (!request.accepts(mime)) {
        throw new NotAcceptable(mime);
      }
    });

    next();
  }
}
github TypedProject / ts-express-decorators / test / integration / app / middlewares / middleware.ts View on Github external
import {IMiddleware, Middleware} from "@tsed/common";
import * as Express from "express";

@Middleware()
export class TestMiddleware implements IMiddleware {
  constructor() {}

  use(request: Express.Request, response: Express.Response): Promise {
    request.params.test = "testMiddleware";

    return Promise.resolve();
  }
}

@Middleware()
export class Test2Middleware implements IMiddleware {
  constructor() {}

  use(request: Express.Request, response: Express.Response, next: Express.NextFunction): void {
    request.params.test = "test2Middleware";
github TypedProject / ts-express-decorators / docs / docs / snippets / middlewares / global-middleware.ts View on Github external
import {Constant, IMiddleware, Middleware, Req} from "@tsed/common";
import {NotAcceptable} from "ts-httpexceptions";

@Middleware()
export default class GlobalAcceptMimesMiddleware implements IMiddleware {
  @Constant("acceptMimes")
  acceptMimes: string[];

  use(@Req() request: Req) {
    this.acceptMimes
      .forEach((mime) => {
        if (!request.accepts(mime)) {
          throw new NotAcceptable(mime);
        }
      });
  }
}
github TypedProject / ts-express-decorators / packages / multipartfiles / src / middlewares / MultipartFileMiddleware.ts View on Github external
import {Configuration, EndpointInfo, IMiddleware, Middleware, Req, Res} from "@tsed/common";
import * as multer from "multer";
import {BadRequest} from "ts-httpexceptions";
import {promisify} from "util";

/**
 * @middleware
 */
@Middleware()
export class MultipartFileMiddleware implements IMiddleware {
  private multer: any = multer;

  constructor(@Configuration() private configuration: Configuration) {}

  async use(@EndpointInfo() endpoint: EndpointInfo, @Req() request: Req, @Res() response: Res) {
    try {
      const endpointConfiguration = endpoint.get(MultipartFileMiddleware);

      return await promisify(this.invoke(endpointConfiguration))(request, response);
    } catch (er) {
      throw er.code ? new BadRequest(`${er.message} ${er.field || ""}`.trim()) : er;
    }
  }

  invoke(conf: any) {
github TypedProject / ts-express-decorators / packages / mongoose / src / middlewares / ValidationErrorMiddleware.ts View on Github external
import {Err, Middleware} from "@tsed/common";
import {getClass, nameOf} from "@tsed/core";
import {BadRequest} from "ts-httpexceptions";

/**
 * @middleware
 */
@Middleware()
export class ValidationErrorMiddleware {
  use(@Err() error: any) {
    if (error && nameOf(getClass(error)) === "MongooseError") {
      const err = new BadRequest(error.message);
      err.stack = error.stack;
      throw err;
    }

    throw error;
  }
}
github TypedProject / ts-express-decorators / examples / mongoose / src / middlewares / calendars / CheckCalendarId.ts View on Github external
import {Middleware, PathParams, Required} from "@tsed/common";
import {NotFound} from "ts-httpexceptions";
import {CalendarsService} from "../../services/calendars/CalendarsService";

@Middleware()
export class CheckCalendarIdMiddleware {
    constructor(private calendarService: CalendarsService) {

    }

    async use(@Required() @PathParams("calendarId") calendarId: string) {

        try {
            await this.calendarService.find(calendarId);
        } catch (er) {
            throw new NotFound("CalendarDTO not found");
        }

    }
}
github TypedProject / ts-express-decorators / packages / integration / src / middlewares / NotFoundMiddleware.ts View on Github external
import {Middleware, Res} from "@tsed/common";
import * as Express from "express";

@Middleware()
export class NotFoundMiddleware {
  use(@Res() response: Express.Response) {
    response.status(404).render("404.html");
  }
}
github TypedProject / ts-express-decorators / examples / getting-started / src / middlewares / CheckCalendarIdMiddleware.ts View on Github external
import {Middleware, PathParams, Required} from "@tsed/common";
import {NotFound} from "ts-httpexceptions";
import {CalendarsService} from "../services/calendars/CalendarsService";

@Middleware()
export class CheckCalendarIdMiddleware {
  constructor(private calendarService: CalendarsService) {
  }

  async use(@Required() @PathParams("calendarId") calendarId: string) {
    const calendar = await this.calendarService.find(calendarId);

    if (!calendar) {
      throw new NotFound("Calendar not found");
    }
  }
}