heimdall-api-platform 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/lib/clients/http-client.js +296 -0
- package/lib/commons-cache.js +185 -0
- package/lib/commons-const.js +203 -0
- package/lib/commons-elasticsearch.js +49 -0
- package/lib/commons-errors.js +278 -0
- package/lib/commons-opensearch.js +37 -0
- package/lib/commons-splunk.js +105 -0
- package/lib/commons-util.js +669 -0
- package/lib/default-routes-docs.js +141 -0
- package/lib/default-routes-pos.js +111 -0
- package/lib/default-routes-pre.js +151 -0
- package/lib/environment.js +81 -0
- package/lib/factory/api-gateway.js +12 -0
- package/lib/factory/client-factory.js +41 -0
- package/lib/factory/function-factory.js +40 -0
- package/lib/factory/operation-flow-factory.js +64 -0
- package/lib/factory/server-factory.js +15 -0
- package/lib/factory/transformation-function-factory.js +47 -0
- package/lib/handle-route.js +472 -0
- package/lib/index.js +50 -0
- package/lib/jwt-util.js +38 -0
- package/lib/license/license-service.js +27 -0
- package/lib/models/base-context.js +77 -0
- package/lib/models/elastic-index-data.js +76 -0
- package/lib/models/flow-context.js +58 -0
- package/lib/models/flow-indexed.js +62 -0
- package/lib/models/operation-function-indexed.js +22 -0
- package/lib/models/operation-function-transformation-indexed.js +23 -0
- package/lib/models/operation-http-indexed.js +38 -0
- package/lib/models/operation-mock-indexed.js +22 -0
- package/lib/models/route-context.js +69 -0
- package/lib/models/security-route.js +41 -0
- package/lib/models/service-context.js +65 -0
- package/lib/models/service-group.js +15 -0
- package/lib/models/service-route.js +23 -0
- package/lib/models/splunk-data.js +70 -0
- package/lib/operations/abstract-operation.js +73 -0
- package/lib/operations/function.js +143 -0
- package/lib/operations/http.js +286 -0
- package/lib/operations/mock.js +34 -0
- package/lib/operations/monitor-check.js +151 -0
- package/lib/orchestration-flow.js +323 -0
- package/lib/public/redoc.html +152 -0
- package/lib/public/swagger.html +143 -0
- package/lib/router.js +29 -0
- package/lib/security-validation.js +46 -0
- package/lib/services/server.js +211 -0
- package/lib/services/template-monitorcheck-route.js +61 -0
- package/package.json +90 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const _ = require("underscore");
|
4
|
+
const Const = require("../commons-const");
|
5
|
+
const Util = require("../commons-util");
|
6
|
+
const { Errors } = require("../commons-errors");
|
7
|
+
const BaseContext = require("../models/base-context");
|
8
|
+
|
9
|
+
class AbstractOperation extends BaseContext {
|
10
|
+
constructor(uuid, context) {
|
11
|
+
super(uuid, context);
|
12
|
+
}
|
13
|
+
|
14
|
+
execute(flowStepToUpdate, context, callbackFromOrchestrationFlow) {
|
15
|
+
this.init();
|
16
|
+
this.doExecute({ context: context, flowStep: flowStepToUpdate }, (error, result) => {
|
17
|
+
this.end();
|
18
|
+
this.loggingResponse();
|
19
|
+
this.handleResponse(error, result, flowStepToUpdate, callbackFromOrchestrationFlow);
|
20
|
+
});
|
21
|
+
}
|
22
|
+
|
23
|
+
executeFunction(inputData, context, callbackStepOperation) {
|
24
|
+
this.init();
|
25
|
+
this.inputData = _.clone(inputData); //OBRIGAR A RETORNAR O OBJETO ALTERDO
|
26
|
+
this.doExecuteFunction(inputData, context, (error, result) => {
|
27
|
+
this.end();
|
28
|
+
this.loggingResponse();
|
29
|
+
callbackStepOperation(error, result);
|
30
|
+
});
|
31
|
+
}
|
32
|
+
|
33
|
+
handleResponse(error, result, flowStepToUpdate, callbackFromOrchestrationFlow) {
|
34
|
+
flowStepToUpdate.responseHeaders = this.responseHeaders;
|
35
|
+
|
36
|
+
if (error) {
|
37
|
+
let objectError = Errors.handleError(Util.parseJSON(error || {}));
|
38
|
+
flowStepToUpdate.statusCode = objectError.responseCode;
|
39
|
+
flowStepToUpdate.error = true;
|
40
|
+
if (flowStepToUpdate.operation.type === "function") {
|
41
|
+
flowStepToUpdate.setResult(Util.tryParseJSON(objectError));
|
42
|
+
} else {
|
43
|
+
flowStepToUpdate.setResult(
|
44
|
+
Util.tryParseJSON(flowStepToUpdate.operation.encapsuleError || true ? objectError : error)
|
45
|
+
);
|
46
|
+
}
|
47
|
+
callbackFromOrchestrationFlow(objectError, null);
|
48
|
+
} else {
|
49
|
+
flowStepToUpdate.statusCode = Const.HTTP_STATUS.OK;
|
50
|
+
flowStepToUpdate.error = false;
|
51
|
+
flowStepToUpdate.setResult(Util.tryParseJSON(result));
|
52
|
+
callbackFromOrchestrationFlow(null, Util.tryParseJSON(result));
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
loggingResponse() {
|
57
|
+
Util.info(
|
58
|
+
`stage=end method=AbstractOperation.loggingResponse operation function flow(${this.type.toUpperCase()}) name=${this.name}`,
|
59
|
+
_.extend(this.getDefaultResponse(), { result: Util.obfuscationJSON(this.getResult()) })
|
60
|
+
);
|
61
|
+
}
|
62
|
+
|
63
|
+
getDefaultResponse() {
|
64
|
+
return {
|
65
|
+
flowStepUuid: this.uuid,
|
66
|
+
contextUuid: this.context.uuid,
|
67
|
+
functionName: this.name,
|
68
|
+
duration: this.timers.duration,
|
69
|
+
};
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
module.exports = AbstractOperation;
|
@@ -0,0 +1,143 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const FunctionFactory = require("../factory/function-factory");
|
4
|
+
const AbstractOperation = require("./abstract-operation");
|
5
|
+
const Const = require("../commons-const");
|
6
|
+
const { CreateFunctionError } = require("../commons-errors");
|
7
|
+
const Util = require("../commons-util");
|
8
|
+
const { v4: uuidv4 } = require("uuid");
|
9
|
+
|
10
|
+
class Function extends AbstractOperation {
|
11
|
+
constructor(options, context) {
|
12
|
+
super(uuidv4(), context);
|
13
|
+
this.type = options.type;
|
14
|
+
this.name = options.functionName;
|
15
|
+
this.dir = options.dir;
|
16
|
+
this.function = FunctionFactory.createFunctionByName(options);
|
17
|
+
this.sourceCode = this.function.handle.toString();
|
18
|
+
}
|
19
|
+
|
20
|
+
//Flow
|
21
|
+
doExecute(options, callbackAbstractOperation) {
|
22
|
+
Util.info(`stage=init method=Function.doExecute operation function name=${this.name}`, {
|
23
|
+
uuid: this.uuid,
|
24
|
+
flowStepUuid: options.flowStep.uuid,
|
25
|
+
contextUuid: options.context.uuid,
|
26
|
+
sourceCode: Util.stringifyInfo(this.sourceCode),
|
27
|
+
});
|
28
|
+
|
29
|
+
try {
|
30
|
+
this.handleFunctionFlow(options.context, callbackAbstractOperation);
|
31
|
+
} catch (error) {
|
32
|
+
Util.error(`stage=error method=Function.doExecute operation function flow name=${this.name}`, error);
|
33
|
+
this.handleFunctionError(error, callbackAbstractOperation);
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
//Function part of Flow
|
38
|
+
doExecuteFunction(options, context, callbackAbstractOperation) {
|
39
|
+
Util.info(`stage=init method=Function.doExecuteFunction operation function name=${this.name}`, {
|
40
|
+
uuid: this.uuid,
|
41
|
+
contextUuid: context.uuid,
|
42
|
+
sourceCode: Util.stringifyInfo(this.sourceCode),
|
43
|
+
});
|
44
|
+
|
45
|
+
try {
|
46
|
+
this.handleFunction(options, context, callbackAbstractOperation);
|
47
|
+
} catch (error) {
|
48
|
+
Util.error(`stage=error method=Function.doExecuteFunction operation function name=${this.name}`, error);
|
49
|
+
this.handleFunctionError(error, callbackAbstractOperation);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
//Function part of Flow
|
54
|
+
doExecuteFunctionWithoutCallback(options, context) {
|
55
|
+
Util.info(`stage=init method=Function.doExecuteFunctionWithoutCallback operation function name=${this.name}`, {
|
56
|
+
uuid: this.uuid,
|
57
|
+
contextUuid: context.uuid,
|
58
|
+
sourceCode: Util.stringifyInfo(this.sourceCode),
|
59
|
+
});
|
60
|
+
|
61
|
+
try {
|
62
|
+
this.function.handle(options, context);
|
63
|
+
} catch (error) {
|
64
|
+
Util.error(`stage=error method=Function.doExecuteFunctionWithoutCallback operation function name=${this.name}`, error);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
handleFunctionFlow(context, callbackAbstractOperation) {
|
69
|
+
let result = this.function.handle(context);
|
70
|
+
|
71
|
+
this.validateResponse(result, callbackAbstractOperation);
|
72
|
+
}
|
73
|
+
|
74
|
+
handleFunction(options, context, callbackAbstractOperation) {
|
75
|
+
let result = this.function.handle(options, context);
|
76
|
+
|
77
|
+
this.validateResponse(result, callbackAbstractOperation);
|
78
|
+
}
|
79
|
+
|
80
|
+
validateResult(result) {
|
81
|
+
let resultValidated = result || "";
|
82
|
+
|
83
|
+
if (!typeof resultValidated === "string") {
|
84
|
+
if (!resultValidated.hasOwnProperty("statusCode")) {
|
85
|
+
resultValidated.statusCode = Const.HTTP_STATUS.OK;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
return resultValidated;
|
90
|
+
}
|
91
|
+
|
92
|
+
validateResponse(result, callbackAbstractOperation) {
|
93
|
+
let resultValidated = this.validateResult(result);
|
94
|
+
|
95
|
+
switch (resultValidated.constructor) {
|
96
|
+
case Promise:
|
97
|
+
result
|
98
|
+
.then((resultPromises) => {
|
99
|
+
this.statusCode = (resultPromises || {}).statusCode || Const.HTTP_STATUS.OK;
|
100
|
+
this.error = false;
|
101
|
+
if ((resultPromises || {}).statusCode !== undefined) {
|
102
|
+
delete resultPromises["statusCode"];
|
103
|
+
}
|
104
|
+
this.setResult(resultPromises || null);
|
105
|
+
callbackAbstractOperation(null, resultPromises || null);
|
106
|
+
})
|
107
|
+
.catch((errorPromises) => {
|
108
|
+
let objectError = new CreateFunctionError(errorPromises);
|
109
|
+
this.statusCode = objectError.responseCode;
|
110
|
+
this.error = true;
|
111
|
+
this.setResult(Util.tryParseJSON(objectError));
|
112
|
+
callbackAbstractOperation(errorPromises, null);
|
113
|
+
});
|
114
|
+
break;
|
115
|
+
case Object:
|
116
|
+
this.statusCode = result.statusCode || Const.HTTP_STATUS.OK;
|
117
|
+
this.error = false;
|
118
|
+
delete result["statusCode"];
|
119
|
+
this.setResult(result || {});
|
120
|
+
callbackAbstractOperation(null, result);
|
121
|
+
break;
|
122
|
+
case String:
|
123
|
+
this.statusCode = Const.HTTP_STATUS.OK;
|
124
|
+
this.error = false;
|
125
|
+
this.setResult(result || "");
|
126
|
+
callbackAbstractOperation(null, result);
|
127
|
+
break;
|
128
|
+
default:
|
129
|
+
callbackAbstractOperation(null, result);
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
handleFunctionError(error, callbackAbstractOperation) {
|
135
|
+
let objectError = new CreateFunctionError(error);
|
136
|
+
this.statusCode = objectError.responseCode;
|
137
|
+
this.error = true;
|
138
|
+
this.setResult(Util.tryParseJSON(objectError));
|
139
|
+
callbackAbstractOperation(objectError, null);
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
module.exports = Function;
|
@@ -0,0 +1,286 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const Util = require("../commons-util");
|
4
|
+
const Env = require("../environment").application;
|
5
|
+
const Step = require("step");
|
6
|
+
const _ = require("underscore");
|
7
|
+
|
8
|
+
const ClientFactory = require("../factory/client-factory");
|
9
|
+
const Const = require("../commons-const");
|
10
|
+
const { Errors } = require("../commons-errors");
|
11
|
+
const AbstractOperation = require("./abstract-operation");
|
12
|
+
const { v4: uuidv4 } = require("uuid");
|
13
|
+
const TransformationFunctionFactory = require("../factory/transformation-function-factory");
|
14
|
+
|
15
|
+
_.templateSettings = {
|
16
|
+
interpolate: /\{\{(.+?)\}\}/g,
|
17
|
+
};
|
18
|
+
|
19
|
+
class Http extends AbstractOperation {
|
20
|
+
constructor(options, flowContext) {
|
21
|
+
super(uuidv4(), flowContext);
|
22
|
+
|
23
|
+
this.type = options.type || Const.FLOW_OPERATION_TYPE.HTTP;
|
24
|
+
this.path = options.path || "";
|
25
|
+
this.method = options.method || Const.HTTP_METHOD.GET;
|
26
|
+
this.body = options.body || "";
|
27
|
+
this.mergedHeaders = options.mergedHeaders == undefined ? false : options.mergedHeaders;
|
28
|
+
this.encapsuleError = options.encapsuleError == undefined ? true : options.encapsuleError;
|
29
|
+
this.httpClient = ClientFactory.createByName(options.clientName);
|
30
|
+
this.headers = options.headers || {};
|
31
|
+
this.name = options.clientName;
|
32
|
+
this.headersTransformation = TransformationFunctionFactory.create(options.headersTransformation, this);
|
33
|
+
this.requestTransformation = TransformationFunctionFactory.create(options.requestTransformation, this);
|
34
|
+
this.responseTransformation = TransformationFunctionFactory.create(options.responseTransformation, this);
|
35
|
+
this.ifResponseError = TransformationFunctionFactory.create(options.ifResponseError, this);
|
36
|
+
this.onError = TransformationFunctionFactory.create(options.onError, this);
|
37
|
+
this.onSuccess = TransformationFunctionFactory.create(options.onSuccess, this);
|
38
|
+
}
|
39
|
+
|
40
|
+
doExecute(options, callbackAbstractOperation) {
|
41
|
+
|
42
|
+
let self = this;
|
43
|
+
|
44
|
+
let context = options.context;
|
45
|
+
|
46
|
+
let optionsInvoke = {
|
47
|
+
path: this.compilePath(this.path, context),
|
48
|
+
method: this.method,
|
49
|
+
query: context.query,
|
50
|
+
body: this.compileBody(context.body, context),
|
51
|
+
params: context.params,
|
52
|
+
headers: this.headers,
|
53
|
+
uuid: this.uuid,
|
54
|
+
flowStepUuid: options.flowStep.uuid,
|
55
|
+
contextUuid: context.uuid,
|
56
|
+
name: this.name,
|
57
|
+
type: this.type.toUpperCase(),
|
58
|
+
request: context.request,
|
59
|
+
service: options.flowStep.service,
|
60
|
+
};
|
61
|
+
|
62
|
+
Util.info(
|
63
|
+
`stage=init method=Http.doExecute Options (Before Transformation) operation flow(${optionsInvoke.type}) name=${optionsInvoke.name}`,
|
64
|
+
{
|
65
|
+
uuid: optionsInvoke.uuid,
|
66
|
+
flowStepUuid: optionsInvoke.flowStepUuid,
|
67
|
+
contextUuid: optionsInvoke.contextUuid,
|
68
|
+
method: optionsInvoke.method,
|
69
|
+
path: optionsInvoke.path,
|
70
|
+
body: Util.isJSONValid(optionsInvoke.body) ? Util.obfuscationJSON(optionsInvoke.body) : {},
|
71
|
+
form: optionsInvoke.form || {},
|
72
|
+
query: optionsInvoke.qs || {},
|
73
|
+
headers: Util.obfuscationJSON(optionsInvoke.headers || {}),
|
74
|
+
}
|
75
|
+
);
|
76
|
+
|
77
|
+
Step(
|
78
|
+
function () {
|
79
|
+
self.prepareHeaders(optionsInvoke, context, this);
|
80
|
+
},
|
81
|
+
function (error) {
|
82
|
+
Util.throwErrorIfItExists(error);
|
83
|
+
self.headersTransformation.doExecuteFunction(optionsInvoke.headers, context, this);
|
84
|
+
},
|
85
|
+
function (error, headersTransformed) {
|
86
|
+
Util.throwErrorIfItExists(error);
|
87
|
+
//CLONAR PARA OBRIGAR A FUNCTION RETORNAR O VALOR
|
88
|
+
optionsInvoke.headers = headersTransformed || optionsInvoke.headers;
|
89
|
+
self.requestTransformation.doExecuteFunction(_.clone(optionsInvoke), context, this);
|
90
|
+
},
|
91
|
+
function (error, optionsInvokeTransformed) {
|
92
|
+
Util.throwErrorIfItExists(error);
|
93
|
+
self.invokeService(optionsInvokeTransformed || optionsInvoke, this);
|
94
|
+
},
|
95
|
+
function (error, response) {
|
96
|
+
self.handleHttpResponse(error, response, this);
|
97
|
+
},
|
98
|
+
function (error, result) {
|
99
|
+
if (error) {
|
100
|
+
self.onError.doExecuteFunctionWithoutCallback(error, context);
|
101
|
+
|
102
|
+
if (self.context.skipErrors) {
|
103
|
+
self.responseTransformation.doExecuteFunction(error, context, (error, result) => {
|
104
|
+
this(null, error || result);
|
105
|
+
});
|
106
|
+
} else {
|
107
|
+
self.responseTransformation.doExecuteFunction(error, context, (error, result) => {
|
108
|
+
this(error || result, null);
|
109
|
+
});
|
110
|
+
}
|
111
|
+
} else {
|
112
|
+
self.onSuccess.doExecuteFunctionWithoutCallback(result, context);
|
113
|
+
self.responseTransformation.doExecuteFunction(result, context, this);
|
114
|
+
}
|
115
|
+
},
|
116
|
+
function (error, result) {
|
117
|
+
callbackAbstractOperation(error, result);
|
118
|
+
}
|
119
|
+
);
|
120
|
+
}
|
121
|
+
|
122
|
+
invokeService(optionsInvoke, callbackStep) {
|
123
|
+
Util.info(
|
124
|
+
`stage=init method=Http.invokeService Options (Before Transformation) operation flow(${optionsInvoke.type}) name=${optionsInvoke.name}`,
|
125
|
+
{
|
126
|
+
uuid: optionsInvoke.uuid,
|
127
|
+
flowStepUuid: optionsInvoke.flowStepUuid,
|
128
|
+
contextUuid: optionsInvoke.contextUuid,
|
129
|
+
method: optionsInvoke.method,
|
130
|
+
path: optionsInvoke.path,
|
131
|
+
body: Util.isJSONValid(optionsInvoke.body) ? Util.obfuscationJSON(optionsInvoke.body) : {},
|
132
|
+
form: optionsInvoke.form || {},
|
133
|
+
query: optionsInvoke.qs || {},
|
134
|
+
headers: Util.obfuscationJSON(optionsInvoke.headers || {}),
|
135
|
+
}
|
136
|
+
);
|
137
|
+
|
138
|
+
this.httpClient.execute(optionsInvoke, callbackStep);
|
139
|
+
}
|
140
|
+
|
141
|
+
handleHttpResponse(error, response, callbackStep) {
|
142
|
+
let responseHeaders = response && response.hasOwnProperty("headers") ? response.headers : {};
|
143
|
+
|
144
|
+
if (error) {
|
145
|
+
let objectError = Errors.handleError(Util.parseJSON(error || {}));
|
146
|
+
this.statusCode = objectError.responseCode;
|
147
|
+
this.responseHeaders = responseHeaders;
|
148
|
+
this.error = true;
|
149
|
+
this.setResult(Util.tryParseJSON(error));
|
150
|
+
callbackStep(error, null);
|
151
|
+
} else {
|
152
|
+
this.statusCode = response.status;
|
153
|
+
this.error = false;
|
154
|
+
this.responseHeaders = responseHeaders;
|
155
|
+
this.setResult(Util.tryParseJSON(response.data));
|
156
|
+
callbackStep(null, this.getResult());
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
//Override from AbstractOperation handleResponse
|
161
|
+
//Update flowStep from operation
|
162
|
+
handleResponse(error, result, flowStep, callbackFromOrchestrationFlow) {
|
163
|
+
flowStep.statusCode = this.statusCode;
|
164
|
+
flowStep.error = this.error;
|
165
|
+
flowStep.responseHeaders = this.responseHeaders;
|
166
|
+
|
167
|
+
if (error) {
|
168
|
+
let objectError = Errors.handleError(Util.parseJSON(error || {}));
|
169
|
+
flowStep.setResult(Util.tryParseJSON(flowStep.operation.encapsuleError ? objectError : error));
|
170
|
+
callbackFromOrchestrationFlow(objectError, null);
|
171
|
+
} else {
|
172
|
+
flowStep.setResult(Util.tryParseJSON(result));
|
173
|
+
callbackFromOrchestrationFlow(null, Util.tryParseJSON(result));
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
prepareHeaders(optionsInvoke, context, callbackStep) {
|
178
|
+
try {
|
179
|
+
optionsInvoke.headers = this.compileHeaders(optionsInvoke.headers, context);
|
180
|
+
|
181
|
+
//ADD ROUTE CONTEXT UUID
|
182
|
+
optionsInvoke.headers = _.extend(optionsInvoke.headers, {
|
183
|
+
[Const.HEADERS.X_REQUEST_UUID_NAME]: context.uuid,
|
184
|
+
});
|
185
|
+
|
186
|
+
//ADD WHITE LABEL UUID
|
187
|
+
if (context.headers[Const.HEADERS.X_WHITE_LABEL_UUID_NAME]) {
|
188
|
+
optionsInvoke.headers = _.extend(optionsInvoke.headers, {
|
189
|
+
[Const.HEADERS.X_WHITE_LABEL_UUID_NAME]: context.headers[Const.HEADERS.X_WHITE_LABEL_UUID_NAME],
|
190
|
+
});
|
191
|
+
}
|
192
|
+
|
193
|
+
//TODO: ADICIONAR ESSA LINHA EM UMA TRANSFORMACAO DE HEADER
|
194
|
+
if (context.security.customerUUID) {
|
195
|
+
optionsInvoke.headers = _.extend(optionsInvoke.headers, {
|
196
|
+
[Const.HEADERS.X_CUSTOMER_UUID_NAME]: context.security.customerUUID,
|
197
|
+
});
|
198
|
+
}
|
199
|
+
|
200
|
+
callbackStep();
|
201
|
+
} catch (error) {
|
202
|
+
Util.error(`stage=error method=Http.prepareHeaders headers=${optionsInvoke.headers} error`, error);
|
203
|
+
callbackStep(error);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
compileBody(body, context) {
|
208
|
+
let bodyCompiled = Util.underscore().clone(body);
|
209
|
+
|
210
|
+
if (this.body.indexOf("{{") !== -1) {
|
211
|
+
try {
|
212
|
+
let compiled = _.template(this.body);
|
213
|
+
bodyCompiled = compiled(this.getCompileContext(context));
|
214
|
+
Util.info("stage=info method=Http.compileBody result", {
|
215
|
+
body: body,
|
216
|
+
bodyCompiled: bodyCompiled,
|
217
|
+
});
|
218
|
+
} catch (error) {
|
219
|
+
Util.error(`stage=error method=Http.compileBody body=${body} error`, error);
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
return bodyCompiled;
|
224
|
+
}
|
225
|
+
|
226
|
+
compilePath(path, context) {
|
227
|
+
let pathCompiled = _.clone(path);
|
228
|
+
|
229
|
+
if (path.indexOf("{{") !== -1) {
|
230
|
+
try {
|
231
|
+
let compiled = _.template(path);
|
232
|
+
pathCompiled = compiled(this.getCompileContext(context));
|
233
|
+
Util.info("stage=info method=Http.compilePath result", {
|
234
|
+
path: path,
|
235
|
+
pathCompiled: pathCompiled,
|
236
|
+
});
|
237
|
+
} catch (error) {
|
238
|
+
Util.error(`stage=error method=Http.compilePath path=${path}, error=${error}`);
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
return pathCompiled;
|
243
|
+
}
|
244
|
+
|
245
|
+
getCompileContext(context) {
|
246
|
+
return _.extend(context, {
|
247
|
+
env: Env,
|
248
|
+
});
|
249
|
+
}
|
250
|
+
|
251
|
+
compileHeaders(headers, context) {
|
252
|
+
|
253
|
+
let compiledHeaders = _.clone(headers);
|
254
|
+
|
255
|
+
for (let index in headers) {
|
256
|
+
let header = headers[index];
|
257
|
+
if (header && header.indexOf("{{") !== -1) {
|
258
|
+
try {
|
259
|
+
let compiled = _.template(header);
|
260
|
+
compiledHeaders[index] = compiled(this.getCompileContext(context));
|
261
|
+
Util.info("stage=info method=Http.compileHeaders header", {
|
262
|
+
header: header,
|
263
|
+
compiledHeader: compiledHeaders[index],
|
264
|
+
});
|
265
|
+
} catch (error) {
|
266
|
+
Util.error(`stage=error method=Http.compileHeaders header=${header}, error=${error}`);
|
267
|
+
return compiledHeaders;
|
268
|
+
}
|
269
|
+
}
|
270
|
+
}
|
271
|
+
|
272
|
+
if (this.mergedHeaders === true) compiledHeaders = _.extend(compiledHeaders, context.headers);
|
273
|
+
|
274
|
+
let requestCookie = context.headers[Const.HEADERS.COOKIE.toLocaleLowerCase()] || {};
|
275
|
+
|
276
|
+
if (Util.nonNull(requestCookie)) {
|
277
|
+
compiledHeaders = _.extend(compiledHeaders, {
|
278
|
+
[Const.HEADERS.COOKIE.toLocaleLowerCase()]: requestCookie,
|
279
|
+
});
|
280
|
+
}
|
281
|
+
|
282
|
+
return compiledHeaders;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
|
286
|
+
module.exports = Http;
|
@@ -0,0 +1,34 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const fs = require("fs");
|
4
|
+
const _ = require("underscore");
|
5
|
+
const AbstractOperation = require("./abstract-operation");
|
6
|
+
const { v4: uuidv4 } = require("uuid");
|
7
|
+
const Util = require("../commons-util");
|
8
|
+
|
9
|
+
class Mock extends AbstractOperation {
|
10
|
+
constructor(options, flowContext) {
|
11
|
+
super(uuidv4(), flowContext);
|
12
|
+
|
13
|
+
if (!options.file) throw "The attribute 'file' is not defined in mock operation";
|
14
|
+
|
15
|
+
this.type = options.type;
|
16
|
+
this.description = options.description || "";
|
17
|
+
this.name = options.file;
|
18
|
+
this.file = `${process.cwd()}/mocks/${options.file}`;
|
19
|
+
}
|
20
|
+
|
21
|
+
doExecute(options, callback) {
|
22
|
+
Util.info(`stage=init method=Mock.doExecute file=${this.name} options`, {
|
23
|
+
uuid: this.uuid,
|
24
|
+
flowStepUuid: options.flowStep.uuid,
|
25
|
+
contextUuid: options.context.uuid,
|
26
|
+
});
|
27
|
+
|
28
|
+
fs.readFile(_.template(this.file)(options.context), (error, result) => {
|
29
|
+
callback(error, JSON.parse(result));
|
30
|
+
});
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
module.exports = Mock;
|
@@ -0,0 +1,151 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const Util = require("../commons-util");
|
4
|
+
const Step = require("step");
|
5
|
+
const _ = require("underscore");
|
6
|
+
const ClientFactory = require("../factory/client-factory");
|
7
|
+
const Const = require("../commons-const");
|
8
|
+
const { Errors } = require("../commons-errors");
|
9
|
+
const AbstractOperation = require("./abstract-operation");
|
10
|
+
const { v4: uuidv4 } = require("uuid");
|
11
|
+
|
12
|
+
_.templateSettings = {
|
13
|
+
interpolate: /\{\{(.+?)\}\}/g,
|
14
|
+
};
|
15
|
+
|
16
|
+
class MonitorCheck extends AbstractOperation {
|
17
|
+
constructor(options, flowContext) {
|
18
|
+
super(uuidv4(), flowContext);
|
19
|
+
|
20
|
+
this.type = options.type;
|
21
|
+
this.path = options.path || "";
|
22
|
+
this.method = options.method || Const.HTTP_METHOD.GET;
|
23
|
+
this.httpClient = ClientFactory.createByName(options.clientName);
|
24
|
+
this.headers = options.headers || {};
|
25
|
+
this.name = options.clientName;
|
26
|
+
}
|
27
|
+
|
28
|
+
doExecute(options, callbackAbstractOperation) {
|
29
|
+
let self = this;
|
30
|
+
|
31
|
+
let context = options.context;
|
32
|
+
|
33
|
+
let optionsInvoke = {
|
34
|
+
path: this.compilePath(this.path, context),
|
35
|
+
method: this.method,
|
36
|
+
query: context.query,
|
37
|
+
body: context.body,
|
38
|
+
params: context.params,
|
39
|
+
headers: this.compileHeaders(this.headers, context),
|
40
|
+
monitor: true,
|
41
|
+
};
|
42
|
+
|
43
|
+
Step(
|
44
|
+
function () {
|
45
|
+
self.invokeService(optionsInvoke, options, this);
|
46
|
+
},
|
47
|
+
function (error, response) {
|
48
|
+
self.handleHttpResponse(error, response, this);
|
49
|
+
},
|
50
|
+
function (error, result) {
|
51
|
+
callbackAbstractOperation(error, result);
|
52
|
+
}
|
53
|
+
);
|
54
|
+
}
|
55
|
+
|
56
|
+
invokeService(optionsInvoke, options, callbackStep) {
|
57
|
+
Util.info(`Request operation flow(${this.type.toUpperCase()}) name=${this.name}`, {
|
58
|
+
flowStepUuid: options.flowStep.uuid,
|
59
|
+
contextUuid: options.context.uuid,
|
60
|
+
method: optionsInvoke.method,
|
61
|
+
path: optionsInvoke.path,
|
62
|
+
body: Util.isJSONValid(optionsInvoke.body) ? Util.obfuscationJSON(optionsInvoke.body) : {},
|
63
|
+
form: optionsInvoke.form || {},
|
64
|
+
query: optionsInvoke.qs || {},
|
65
|
+
headers: Util.obfuscationJSON(optionsInvoke.headers),
|
66
|
+
timeout: optionsInvoke.timeout,
|
67
|
+
});
|
68
|
+
|
69
|
+
this.httpClient.execute(optionsInvoke, callbackStep);
|
70
|
+
}
|
71
|
+
|
72
|
+
handleHttpResponse(error, response, callbackStep) {
|
73
|
+
let responseHeaders = response && response.hasOwnProperty("headers") ? response.headers : {};
|
74
|
+
|
75
|
+
if (error) {
|
76
|
+
let objectError = Errors.handleError(Util.parseJSON(error || {}));
|
77
|
+
this.statusCode = objectError.responseCode;
|
78
|
+
this.responseHeaders = responseHeaders;
|
79
|
+
this.error = true;
|
80
|
+
this.setResult(Util.tryParseJSON(objectError));
|
81
|
+
callbackStep(error, null);
|
82
|
+
} else {
|
83
|
+
this.statusCode = response.statusCode;
|
84
|
+
this.error = false;
|
85
|
+
this.responseHeaders = responseHeaders;
|
86
|
+
this.setResult(Util.tryParseJSON(response.data));
|
87
|
+
callbackStep(null, this.getResult());
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
handleResponse(error, result, flowStep, callbackFromOrchestrationFlow) {
|
92
|
+
if (error) {
|
93
|
+
let objectError = Errors.handleError(Util.parseJSON(error || {}));
|
94
|
+
flowStep.statusCode = objectError.responseCode;
|
95
|
+
flowStep.error = true;
|
96
|
+
flowStep.responseHeaders = this.responseHeaders;
|
97
|
+
flowStep.setResult(Util.tryParseJSON(objectError));
|
98
|
+
callbackFromOrchestrationFlow(objectError, null);
|
99
|
+
} else {
|
100
|
+
flowStep.statusCode = Const.HTTP_STATUS.OK;
|
101
|
+
flowStep.error = false;
|
102
|
+
flowStep.responseHeaders = this.responseHeaders;
|
103
|
+
flowStep.setResult(Util.tryParseJSON(result));
|
104
|
+
callbackFromOrchestrationFlow(null, Util.tryParseJSON(result));
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
compilePath(path, context) {
|
109
|
+
let pathCompiled = _.clone(path);
|
110
|
+
|
111
|
+
if (path.indexOf("{{") !== -1) {
|
112
|
+
try {
|
113
|
+
let compiled = _.template(path);
|
114
|
+
pathCompiled = compiled(context);
|
115
|
+
Util.info("Compile path", {
|
116
|
+
path: path,
|
117
|
+
pathCompiled: pathCompiled,
|
118
|
+
});
|
119
|
+
} catch (error) {
|
120
|
+
Util.error(`Error to compile path=${path}, error=${error}`);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
return pathCompiled;
|
125
|
+
}
|
126
|
+
|
127
|
+
compileHeaders(headers, context) {
|
128
|
+
let compiledHeaders = _.clone(headers);
|
129
|
+
|
130
|
+
for (let index in headers) {
|
131
|
+
let header = headers[index];
|
132
|
+
if (header && header.indexOf("{{") !== -1) {
|
133
|
+
try {
|
134
|
+
let compiled = _.template(header);
|
135
|
+
compiledHeaders[index] = compiled(context);
|
136
|
+
Util.info("Compile header", {
|
137
|
+
header: header,
|
138
|
+
compiledHeader: compiledHeaders[index],
|
139
|
+
});
|
140
|
+
} catch (error) {
|
141
|
+
Util.error(`Error to compile header=${header}, error=${error}`);
|
142
|
+
return compiledHeaders;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
return compiledHeaders;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
module.exports = MonitorCheck;
|