-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionBase.js
59 lines (50 loc) · 1.59 KB
/
ExpressionBase.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
'use strict';
/**
* La classe base che permette di interagire con il middleware della policy
* Responsible for:
* 1. istanziare req, res, next, pluginContext, actionParams => OK
* 2. fornire un metodo log() che usa un logger custom => TBD
*/
class ExpressionBase {
constructor(req, res, next, pluginContext, actionParams) {
this.req = req
this.res = res
this.next = next
this.pluginContext = pluginContext
this.actionParams = actionParams
// i responsi in un array per avere un log sequenziale di come viene lavorato il responso
this.res.locals.customResponse = res.locals.customResponse || []
}
run() {
throw new Error('You have to implement the method run()');
}
setParam(param, value) {
this.res.locals[param] = value
}
getParam(param) {
return this.res.locals[param]
}
/**
* questo torna un oggetto chiave valore con i parametri richiesti in paramArray
*/
getParams(paramArray = []) {
let params = {}
paramArray.forEach(paramName => {
params[paramName] = this.getParam(paramName)
})
return params
}
getResponse(index) {
const length = this.res.locals.customResponse.length - 1
return (index <= length)
? this.res.locals.customResponse[index]
: this.res.locals.customResponse[length]
}
setResponse(payload) {
this.res.locals.customResponse.push(payload)
}
getAllResponse() {
return this.res.locals.customResponse
}
}
module.exports = ExpressionBase