How to use the @tsed/common.UseBefore 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 / packages / multipartfiles / src / decorators / multipartFile.ts View on Github external
name = (typeof name === "object" ? undefined : name)!;

        // create endpoint metadata
        store.merge("consumes", ["multipart/form-data"]).set("multipartAdded", true);
        store
          .merge("responses", {
            "400": {
              description: `<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field>  [fieldName]
                            Example: File too long file1`
            }
          })
          .set("multipartAdded", true);

        if (!added) {
          // middleware is added
          UseBefore(MultipartFileMiddleware)(target, propertyKey, descriptorOf(target, propertyKey));
        }

        if (name === undefined) {
          store.merge(MultipartFileMiddleware, {
            options,
            any: true
          });

          UseFilter(multiple ? MultipartFilesFilter : MultipartFileFilter, {
            useConverter: false,
            paramType: ParamTypes.FORM_DATA
          })(target, propertyKey, index);
        } else {
          const expression = multiple ? (name as string) : name + ".0";

          store.merge(MultipartFileMiddleware, {
github TypedProject / ts-express-decorators / examples / passport-azure-ad / packages / server / src / decorators / OAuthHead.ts View on Github external
export function OAuthHead() {
  return UseBefore((request, response, next) => {
    const {scopes = []} = request.ctx.endpoint.get(OAuthBearerOptions) || {};
    response.set("Access-Control-Expose-Headers", "scopes");
    response.set("scopes", JSON.stringify(scopes));

    if (request.method.toUpperCase() === "HEAD") { // prevent head request and skip endpoint
      return response.send();
    }

    next();
  })
}
github TypedProject / ts-express-decorators / src / multipartfiles / decorators / multipartFile.ts View on Github external
name = (typeof name === "object" ? undefined : name)!;

        // create endpoint metadata
        store.merge("consumes", ["multipart/form-data"]).set("multipartAdded", true);
        store
          .merge("responses", {
            "400": {
              description: `<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field>  [fieldName]
                            Example: File too long file1`
            }
          })
          .set("multipartAdded", true);

        if (!added) {
          // middleware is added
          UseBefore(MultipartFileMiddleware)(target, propertyKey, descriptorOf(target, propertyKey));
        }

        if (name === undefined) {
          store.merge(MultipartFileMiddleware, {
            options,
            any: true
          });

          ParamRegistry.useFilter(multiple ? MultipartFilesFilter : MultipartFileFilter, {
            propertyKey,
            parameterIndex,
            target,
            useConverter: false,
            paramType: ParamTypes.FORM_DATA
          });
        } else {
github TypedProject / ts-express-decorators / test / integration / app / controllers / calendars / TaskCtrl.ts View on Github external
import {Authenticated, Controller, Get, MergeParams, PathParams, QueryParams, UseBefore} from "@tsed/common";
import {Hidden} from "../../../../../packages/swagger/src";
import {Test2Middleware} from "../../middlewares/middleware";

@Controller("/:eventId/tasks")
@MergeParams()
@UseBefore(Test2Middleware)
@Authenticated({options: "options"})
export class TaskCtrl {
  @Get("/")
  async get(@PathParams("test") value: string, @PathParams("eventId") id: string) {
    return {value, id};
  }

  @Get("/hidden")
  @Hidden()
  async getHidden() {
    return {};
  }

  @Get("/hiddenparam")
  async getHiddenParams(
    @Hidden()
github TypedProject / ts-express-decorators / docs / docs / snippets / middlewares / call-sequences.ts View on Github external
import {Controller, Get, Next, Use, UseAfter, UseBefore, UseBeforeEach} from "@tsed/common";

@Controller("/")
@UseAfter(MdlwCtrlAfter)
@UseBefore(MdlwCtrlBefore)
@UseBeforeEach(MdlwCtrlBeforeEach)
@Use(MdlwCtrl)
export class MyCtrl {

  @Get("/")
  @UseBefore(MdlwBefore)
  @Use(Mdlw)
  @UseAfter(MdlwAfter)
  endpointA(@Next() next: Next) {
    console.log("EndpointA");
    next();
  }

  @Get("/")
  endpointB() {
    console.log("EndpointB");

    return {};
  }
}
github TypedProject / ts-express-decorators / docs / docs / snippets / middlewares / call-sequences.ts View on Github external
import {Controller, Get, Next, Use, UseAfter, UseBefore, UseBeforeEach} from "@tsed/common";

@Controller("/")
@UseAfter(MdlwCtrlAfter)
@UseBefore(MdlwCtrlBefore)
@UseBeforeEach(MdlwCtrlBeforeEach)
@Use(MdlwCtrl)
export class MyCtrl {

  @Get("/")
  @UseBefore(MdlwBefore)
  @Use(Mdlw)
  @UseAfter(MdlwAfter)
  endpointA(@Next() next: Next) {
    console.log("EndpointA");
    next();
  }

  @Get("/")
  endpointB() {
    console.log("EndpointB");
github TypedProject / ts-express-decorators / examples / getting-started / src / controllers / events / EventsCtrl.ts View on Github external
MergeParams,
  PathParams,
  Post,
  Put,
  Required,
  Status,
  UseBefore
} from "@tsed/common";
import {NotFound} from "ts-httpexceptions";
import {CheckCalendarIdMiddleware} from "../../middlewares/CheckCalendarIdMiddleware";
import {Event} from "../../interfaces/Event";
import {Task} from "../../interfaces/Task";

@Controller("/:calendarId/events")
@MergeParams(true)
@UseBefore(CheckCalendarIdMiddleware)
export class EventsCtrl {
  private AUTO_INC = 5;
  private events: Event[] = require("../../../resources/events.json");

  @Get("/:id")
  async get(@Required() @PathParams("calendarId") calendarId: string,
            @PathParams("id") id: string): Promise<Event> {

    const event = this.events.find(event => event.id === id && event.calendarId === calendarId);

    if (event) {
      return event;
    }

    throw new NotFound("event not found");
  }
github TypedProject / ts-express-decorators / examples / mongoose / src / controllers / events / EventsCtrl.ts View on Github external
@Status(200, {description: "Success"})
  async get(@Description("The event id")
            @PathParams("id") id: string): Promise<CalendarEvent> {
    return this.calendarEventsService
      .find(id)
      .catch((err) => {
        throw new NotFound("Event not found");
      });
  }

  /**
   *
   * @returns {null}
   */
  @Put("/")
  @UseBefore(CheckCalendarIdMiddleware)
  @Summary("Create an event")
  @Status(201, {description: "Created"})
  async save(@Description("The calendar id of the event")
             @Required() @PathParams("calendarId") calendarId: string,
             @BodyParams() calendarEvent: CalendarEvent): Promise<CalendarEvent> {

    calendarEvent.calendarId = calendarId;

    return this.calendarEventsService.save(calendarEvent);
  }

  /**
   *
   * @returns {null}
   */
  @Post("/:id")
github TypedProject / ts-express-decorators / examples / mongoose / src / controllers / events / EventsCtrl.ts View on Github external
import {CalendarEvent} from "../../models/events/CalendarEvent";
import {CalendarEventsService} from "../../services/calendars/CalendarEventsService";

@Controller("/:calendarId/events")
@MergeParams(true)
export class EventsCtrl {
  constructor(private calendarEventsService: CalendarEventsService) {

  }

  /**
   *
   * @returns {null}
   */
  @Get("/:id")
  @UseBefore(CheckCalendarIdMiddleware)
  @Summary("Get an event from his ID")
  @Status(200, {description: "Success"})
  async get(@Description("The event id")
            @PathParams("id") id: string): Promise<CalendarEvent> {
    return this.calendarEventsService
      .find(id)
      .catch((err) => {
        throw new NotFound("Event not found");
      });
  }

  /**
   *
   * @returns {null}
   */
  @Put("/")
github TypedProject / ts-express-decorators / examples / mongoose / src / controllers / events / EventsCtrl.ts View on Github external
@Status(201, {description: "Created"})
  async save(@Description("The calendar id of the event")
             @Required() @PathParams("calendarId") calendarId: string,
             @BodyParams() calendarEvent: CalendarEvent): Promise<CalendarEvent> {

    calendarEvent.calendarId = calendarId;

    return this.calendarEventsService.save(calendarEvent);
  }

  /**
   *
   * @returns {null}
   */
  @Post("/:id")
  @UseBefore(CheckCalendarIdMiddleware)
  @Summary("Update event information")
  @Status(200, {description: "Success"})
  async update(@Description("The calendar id of the event")
               @Required()
               @PathParams("calendarId") calendarId: string,
               @Description("The event id")
               @PathParams("id") id: string,
               @BodyParams() calendarEvent: CalendarEvent): Promise<CalendarEvent> {

    return this
      .calendarEventsService
      .find(id)
      .then(() => this.calendarEventsService.save(calendarEvent))
      .catch((err) => {
        throw new NotFound("Calendar id not found");
      });