Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
});
}
}
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");
}
}
}
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();
}
}
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";
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);
}
});
}
}
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) {
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;
}
}
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");
}
}
}
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");
}
}
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");
}
}
}