-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuard.js
69 lines (66 loc) · 2.12 KB
/
Guard.js
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
class Guard {
constructor(res,errorCode=400){
this.errorCode = errorCode
this.errorList = []
this.failed = false
this.res = res;
this.hasWritten = false;
}
/**
* Adds a guard
* @param {boolean} failOn The logic that causes the test to fail if true
* @param {string=} errorMessage If given, the error message that is written if the test fails
*/
and(failOn,errorMessage){
if (failOn){
this.failed = true;
if(errorMessage){
this.errorList.push(errorMessage);
}
}
return this;
}
/**
* If any of the guards have failed, then this method writes the errors to the
* result stream. The method then returns if any of the guards failed.
* The method will however not write to the output stream if it is called a second time.
* @param {boolean} [end=true] If the stream should be closed after writing to it
*/
write(end=true){
if(!this.hasWritten){
this.hasWritten = true;
if(this.failed){
if(this.errorCode){
res.writeHead(errorCode)
}
for(const err of this.errorList){
res.write(err);
res.write("\n");
}
if(end){
res.end();
}
}
}
return this.failed;
}
}
/**
* @param {any} res Response stream to write to
* @param {number} [errorCode=400] Error code to use
*//**
* @param {any} res Response stream to write to
* @param {boolean} failOn The logic that causes the test to fail if true
* @param {string} errorMessage The error message to show if the test fails
* @param {number} [errorCode=400] Error code to use
*/
module.exports = function(res,failOn,errorMessage,errorCode){
if(errorCode === undefined && errorMessage === undefined){
return new Guard(res,failOn) //read failOn as errorcode
}
const g = new Guard(res,errorCode)
if(errorMessage !== undefined){
g.and(failOn,errorMessage)
}
return g;
}