generated from teqfw/repo-tmpl-mod
/
Context.mjs
148 lines (133 loc) · 3.75 KB
/
Context.mjs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Model to represent context for plugins services.
* All plugins services use this context to interchange data with HTTP-server (and, through the server, with clients).
*
* @namespace TeqFw_Web_Back_Api_Service_Context
*/
// MODULE'S IMPORT
import {constants as H2} from 'http2';
// MODULE'S VARS
const NS = 'TeqFw_Web_Back_Api_Service_Context';
export default class TeqFw_Web_Back_Api_Service_Context {
/** @type {Object} */
inData;
/** @type {Object} */
outData;
/** @type {Object<string, string>} */
outHeaders;
/** @type {TeqFw_Web_Back_Api_Request_IContext} */
requestContext;
/** @type {{string, string}} */
routeParams;
/**
* Get object that is shared between all handlers from request context.
* TODO: should we return prop of the shared object for method param (getHandlersShare(name) => shared[name])?
* @return {Object}
*/
getHandlersShare() {
return this.requestContext.getHandlersShare();
}
/**
* Get service input data extracted from the POSTed JSON.
* @return {Object}
*/
getInData() {
return this.inData;
}
/**
* Get service output data to be send as JSON in the response.
* @return {Object}
*/
getOutData() {
return this.outData;
}
/**
* Get service output headers to be send in response.
* @return {Object<string, string>}
*/
getOutHeaders() {
return this.outHeaders;
}
/**
* Get context for current request.
* @return TeqFw_Web_Back_Api_Request_IContext
*/
getRequestContext() {
return this.requestContext;
}
/**
* Get params for route (/post/:postId/comment/:commentId).
* @return {{string, string}} data
*/
getRouteParams() {
return this.routeParams;
}
/**
* Set service input data extracted from the POSTed JSON.
* @param {Object} data
*/
setInData(data) {
this.inData = data;
}
/**
* Set service output data to be send as JSON in the response.
* @param {Object} data
*/
setOutData(data) {
this.outData = data;
}
/**
* Add/replace one header to be send in the response.
* @param {string} key
* @param {string} value
*/
setOutHeader(key, value) {
if (key === H2.HTTP2_HEADER_SET_COOKIE) {
if (this.outHeaders[key]) {
// merge cookies
this.outHeaders[key] += `;${value}`;
} else {
// add cookie
this.outHeaders[key] = value;
}
} else {
this.outHeaders[key] = value;
}
}
/**
* Set context to current request.
* @param {TeqFw_Web_Back_Api_Request_IContext} data
*/
setRequestContext(data) {
this.requestContext = data;
}
/**
* Set params for route (/post/:postId/comment/:commentId).
* @param {{string, string}} data
*/
setRouteParams(data) {
this.routeParams = data;
}
}
/**
* Factory to create new instances.
* @memberOf TeqFw_Web_Back_Api_Service_Context
*/
export class Factory {
/**
* @param {TeqFw_Web_Back_Api_Service_Context|null} data
* @return {TeqFw_Web_Back_Api_Service_Context}
*/
create(data = null) {
const res = new TeqFw_Web_Back_Api_Service_Context();
res.inData = data?.inData;
res.outData = data?.outData;
res.outHeaders = data?.outHeaders ?? {};
res.requestContext = data?.requestContext;
res.routeParams = data?.routeParams ?? {};
return res;
}
}
// finalize code components for this es6-module
Object.freeze(TeqFw_Web_Back_Api_Service_Context);
Object.defineProperty(Factory, 'name', {value: `${NS}.${Factory.constructor.name}`});