1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/middlewares/RenderingMWs.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error";
import {Utils} from "../../common/Utils";
import {Message} from "../../common/entities/Message";
export class RenderingMWs {
2016-05-09 23:04:56 +08:00
public static renderResult(req:Request, res:Response, next:NextFunction) {
if (!req.resultPipe)
return next();
2016-05-09 23:04:56 +08:00
return RenderingMWs.renderMessage(res, req.resultPipe);
}
2016-05-09 23:04:56 +08:00
public static renderSessionUser(req:Request, res:Response, next:NextFunction) {
if (!(req.session.user)) {
return next(new Error(ErrorCodes.GENERAL_ERROR));
}
let user = Utils.clone(req.session.user);
delete user.password;
2016-05-09 23:04:56 +08:00
RenderingMWs.renderMessage(res, user);
}
2016-05-09 23:04:56 +08:00
public static renderFile(req:Request, res:Response, next:NextFunction) {
if (!req.resultPipe)
return next();
return res.sendFile(req.resultPipe);
}
2016-05-09 23:04:56 +08:00
public static renderOK(req:Request, res:Response, next:NextFunction) {
let message = new Message<string>(null, "ok");
res.json(message);
}
2016-05-09 23:04:56 +08:00
public static renderError(err:any, req:Request, res:Response, next:NextFunction):any {
if (err instanceof Error) {
let message = new Message<any>(err, null);
return res.json(message);
}
return next(err);
}
2016-05-09 23:04:56 +08:00
protected static renderMessage<T>(res:Response, content:T) {
let message = new Message<T>(null, content);
res.json(message);
}
}