-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseContext.js
63 lines (58 loc) · 1.32 KB
/
ResponseContext.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
/**
* Represents response state.
* @prop {*} error - if not falsy will be passed to errorHandler
* @prop {RequestContext} request - processing request's requestContext instance.
* @prop {*} locals - the legal way to pass data between middlewares
*/
class ResponseContext {
/**
* Creates an instance of ResponseContext.
* @param {RequestContext} req
*/
constructor(req) {
this.error = null;
this.request = req;
this.locals = {};
}
/**
* Returns true if there is no error, otherwise false
*
* @returns {boolean}
* @memberof ResponseContext
*/
isOk() {
return !this.error;
}
/**
* Sets response error
*
* @param {*} error
* @returns {ResponseContext}
* @memberof ResponseContext
*/
setError(error) {
this.error = error;
return this;
}
/**
* Sets 'notfound' error, shorthand for setError('notfound')
*
* @returns {ResponseContext}
* @memberof ResponseContext
*/
notFound() {
this.setError('notfound');
return this;
}
/**
* Sets 'notallowed' error, shorthand for setError('notallowed')
*
* @returns {ResponseContext}
* @memberof ResponseContext
*/
notAllowed() {
this.setError('notallowed');
return this;
}
}
export default ResponseContext;