This repository was archived by the owner on Jan 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
120 lines (97 loc) · 3.26 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as express from "express";
import {isNullOrUndefined} from "util";
import {User} from "./model/user";
import {MongoStore} from "./model";
import {Collection} from "mongodb";
import * as Q from 'q'
import * as debug from 'debug';
require('dotenv').config();
const Log = debug('wbb:utils');
export function sendError(res: express.Response, msg: string, err?: Error, code: number = 500) {
var errObj: any = {
error: msg
}
if (!isNullOrUndefined(err)) {
errObj.detail = msg;
if (err instanceof Error) {
errObj.originalMessage = err.message;
errObj.originalName = err.name;
Log('sendError: ' + msg + ' <' + err.name + '> ' + err.message);
}
if (err instanceof WbbError) {
code = err.errorCode;
}
}
res.status(code).send(errObj);
}
export interface WbbRequest extends express.Request {
user: User
}
export interface WbbResponse extends express.Response {
sendPromise(promise: PromiseLike<any>): any
}
export class WbbError extends Error {
public errorCode: number;
public constructor(detail: string, errCode: number = 500) {
super(detail);
this.errorCode = errCode;
}
}
interface RouteHandler {
(req: WbbRequest, res: WbbResponse, next: express.NextFunction): any
}
export class WbbRouter {
protected router: express.Router;
public store: MongoStore;
public config: any;
public constructor(store: MongoStore, router?: express.Router) {
this.router = isNullOrUndefined(router) ? express.Router() : router;
this.store = store;
}
public getRouter(): express.Router {
return this.router;
}
public mongo(collectionName: string): Collection {
return this.store.collection(collectionName);
}
protected wrap(handler: RouteHandler): express.RequestHandler {
return function (req: express.Request, res: express.Response, next: express.NextFunction) {
// request is modified to conform to Wbb elsewhere
(<WbbResponse> res).sendPromise = function (promise: PromiseLike<any>) {
Q(promise).then(val => {
res.send(val);
}).catch(err => {
sendError(res, "Generic Error", err);
});
};
handler(<WbbRequest> req, <WbbResponse> res, next);
}
}
public get(path: string, handler: RouteHandler) {
this.router.get(path, this.wrap(handler));
}
public post(path: string, handler: RouteHandler) {
this.router.post(path, this.wrap(handler));
}
public put(path: string, handler: RouteHandler) {
this.router.put(path, this.wrap(handler));
}
public delete(path: string, handler: RouteHandler) {
this.router.delete(path, this.wrap(handler));
}
public use(path: string, handler: RouteHandler) {
this.router.use(path, this.wrap(handler));
}
}
export function requireInput(field, errMsg: string) {
if (isNullOrUndefined(field)) {
throw new ValidationError(errMsg, 400);
}
}
export function requireArray(field, errMsg: string) {
if (!Array.isArray(field)) {
throw new ValidationError(errMsg, 400);
}
}
export class ValidationError extends WbbError {
}