qcobjects 2.4.72 → 2.4.73

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.4.72
1
+ 2.4.73
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qcobjects",
3
- "version": "2.4.72",
3
+ "version": "2.4.73",
4
4
  "description": "QCObjects is an Open-source framework that empowers full-stack developers to make micro-services and micro-frontends into an N-Tier architecture.",
5
5
  "main": "src/index.cjs",
6
6
  "module": "src/index.mjs",
package/src/QCObjects.js CHANGED
@@ -1004,21 +1004,24 @@
1004
1004
  _methods_(self.__definition).map(function (m) {
1005
1005
  self[m.name] = m.bind(self);
1006
1006
  });
1007
-
1008
- if (typeof self.__definition === "undefined" || (!Object.hasOwnProperty.call(self.__definition, "body")) || typeof self.__definition.body === "undefined") {
1009
- try {
1010
- if (isBrowser) {
1011
- self["body"] = _DOMCreateElement(self.__definition.__classType);
1012
- } else {
1007
+
1008
+ if (!!self["body"]){
1009
+ if (typeof self.__definition === "undefined" || (!Object.hasOwnProperty.call(self.__definition, "body")) || typeof self.__definition.body === "undefined") {
1010
+ try {
1011
+ if (isBrowser) {
1012
+ self["body"] = _DOMCreateElement(self.__definition.__classType);
1013
+ } else {
1014
+ self["body"] = {};
1015
+ }
1016
+ } catch (e) {
1013
1017
  self["body"] = {};
1014
1018
  }
1015
- } catch (e) {
1016
- self["body"] = {};
1017
- }
1018
- } else if (Object.hasOwnProperty.call(self.__definition, "body")) {
1019
- self["body"] = self.__definition.body;
1019
+ } else if (Object.hasOwnProperty.call(self.__definition, "body")) {
1020
+ self["body"] = self.__definition.body;
1021
+ }
1020
1022
  }
1021
1023
 
1024
+
1022
1025
  try {
1023
1026
  if (typeof self.__new__ === "function") {
1024
1027
  self.__new__.call(self, _o_);
@@ -3980,17 +3983,19 @@
3980
3983
  constructor ({
3981
3984
  domain= _domain_,
3982
3985
  basePath= _basePath_,
3983
- body= null,
3986
+ body = null,
3984
3987
  stream= null,
3985
3988
  request= null
3986
3989
  }){
3987
3990
  super(...arguments);
3988
-
3989
- var o = this;
3990
-
3991
- logger.debug("Executing BackendMicroservice ");
3991
+ logger.debug("Initializing BackendMicroservice...");
3992
3992
  let microservice = this;
3993
- microservice.body = null;
3993
+ if (typeof this.body === "undefined") {
3994
+ this.body = null;
3995
+ }
3996
+ if (typeof body !== "undefined"){
3997
+ this.body = body;
3998
+ }
3994
3999
  this.cors();
3995
4000
  microservice.stream = stream;
3996
4001
  stream.on("data", (data) => {
@@ -4025,6 +4030,7 @@
4025
4030
 
4026
4031
  cors() {
4027
4032
  if (this.route.cors) {
4033
+ logger.debug("Validating CORS...");
4028
4034
  let {
4029
4035
  allow_origins,
4030
4036
  allow_credentials,
@@ -4035,37 +4041,54 @@
4035
4041
  if (typeof microservice.headers !== "object") {
4036
4042
  microservice.headers = {};
4037
4043
  }
4044
+ if (typeof microservice.route.responseHeaders !== "object") {
4045
+ microservice.route.responseHeaders = {};
4046
+ }
4038
4047
  if (typeof allow_origins !== "undefined") {
4048
+ logger.debug("CORS: allow_origins available. Validating origins...");
4039
4049
  // an example of allow_origins is ['https://example.com','http://www.example.com']
4040
4050
  if (allow_origins === "*" || (typeof microservice.request.headers.origin === "undefined") || [...allow_origins].indexOf(microservice.request.headers.origin) !== -1) {
4041
4051
  // for compatibility with all browsers allways return a wildcard when the origin is allowed
4042
- microservice.headers["Access-Control-Allow-Origin"] = "*";
4052
+ logger.debug("CORS: Adding header Access-Control-Allow-Origin=*");
4053
+ microservice.route.responseHeaders["Access-Control-Allow-Origin"] = "*";
4043
4054
  } else {
4044
- logger.debug("Origin is not allowed: " + microservice.request.headers.origin);
4045
- logger.debug("Forcing to finish the response...");
4055
+ logger.debug("CORS: Origin is not allowed: " + microservice.request.headers.origin);
4056
+ logger.debug("CORS: Forcing to finish the response...");
4046
4057
  this.body = {};
4047
4058
  try {
4048
4059
  this.done();
4049
- } catch (e) {}
4060
+ } catch (e) {
4061
+ logger.debug(`It was not possible to finish the call to the microservice: ${e}`);
4062
+ }
4050
4063
  }
4051
4064
  } else {
4052
- microservice.headers["Access-Control-Allow-Origin"] = "*";
4065
+ logger.debug("CORS: no allow_origins available. Allowing all origins...");
4066
+ logger.debug("CORS: Adding header Access-Control-Allow-Origin=*");
4067
+ microservice.route.responseHeaders["Access-Control-Allow-Origin"] = "*";
4053
4068
  }
4054
4069
  if (typeof allow_credentials !== "undefined") {
4055
- microservice.headers["Access-Control-Allow-Credentials"] = allow_credentials.toString();
4070
+ logger.debug(`CORS: allow_credentials present. Allowing ${allow_credentials}...`);
4071
+ microservice.route.responseHeaders["Access-Control-Allow-Credentials"] = allow_credentials.toString();
4056
4072
  } else {
4057
- microservice.headers["Access-Control-Allow-Credentials"] = "true";
4073
+ logger.debug("CORS: No allow_credentials present. Allowing all credentials.");
4074
+ microservice.route.responseHeaders["Access-Control-Allow-Credentials"] = "true";
4058
4075
  }
4059
4076
  if (typeof allow_methods !== "undefined") {
4060
- microservice.headers["Access-Control-Allow-Methods"] = [...allow_methods].join(",");
4077
+ logger.debug(`CORS: allow_methods present. Allowing ${allow_methods}...`);
4078
+ microservice.route.responseHeaders["Access-Control-Allow-Methods"] = [...allow_methods].join(",");
4061
4079
  } else {
4062
- microservice.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS, POST";
4080
+ logger.debug("CORS: No allow_methods present. Allowing only GET, OPTIONS and POST");
4081
+ microservice.route.responseHeaders["Access-Control-Allow-Methods"] = "GET, OPTIONS, POST";
4063
4082
  }
4064
4083
  if (typeof allow_headers !== "undefined") {
4065
- microservice.headers["Access-Control-Allow-Headers"] = [...allow_headers].join(",");
4084
+ logger.debug(`CORS: allow_headers present. Allowing ${allow_headers}...`);
4085
+ microservice.route.responseHeaders["Access-Control-Allow-Headers"] = [...allow_headers].join(",");
4066
4086
  } else {
4067
- microservice.headers["Access-Control-Allow-Headers"] = "*";
4087
+ logger.debug(`CORS: No allow_headers present. Allowing all headers...`);
4088
+ microservice.route.responseHeaders["Access-Control-Allow-Headers"] = "*";
4068
4089
  }
4090
+ } else {
4091
+ logger.debug("No CORS validation available. You can specify cors in CONFIG.backend.routes[].cors");
4069
4092
  }
4070
4093
  }
4071
4094
 
@@ -4075,6 +4098,7 @@
4075
4098
  }
4076
4099
 
4077
4100
  get(formData) {
4101
+ logger.debug(`[BackendMicroservice.get] Data received: ${_DataStringify(formData)}`);
4078
4102
  this.done();
4079
4103
  }
4080
4104
 
@@ -4108,27 +4132,45 @@
4108
4132
 
4109
4133
  finishWithBody(stream) {
4110
4134
  try {
4111
- stream.write(_DataStringify(this.body));
4135
+ logger.debug("[BackendMicroservice.finishWithBody] Ending the stream...");
4136
+ logger.debug(`[BackendMicroservice.finishWithBody] type of body is: ${typeof this.body}`);
4137
+ if (typeof this.body !== "string"){
4138
+ this.body = _DataStringify(this.body);
4139
+ }
4140
+ logger.debug(`[BackendMicroservice.finishWithBody] \n body: ${this.body} `);
4141
+ stream.write(this.body);
4112
4142
  stream.end();
4143
+ logger.debug(`[BackendMicroservice.finishWithBody] Stream ended.`);
4113
4144
  } catch (e) {
4114
- logger.debug("Something wrong writing the response for microservice" + e.toString());
4145
+ logger.debug(`[BackendMicroservice.finishWithBody] Something went wrong ending the stream: ${e}`);
4115
4146
  }
4116
4147
  }
4117
4148
 
4118
4149
  done() {
4150
+ logger.debug(`[BackendMicroservice.done] Finalizing the response...`);
4119
4151
  var microservice = this;
4120
4152
  var stream = microservice.stream;
4121
4153
  try {
4122
- stream.respond(microservice.headers);
4154
+ logger.debug(`[BackendMicroservice.done] Sending response headers...`);
4155
+ if (microservice.route.responseHeaders){
4156
+ logger.debug(`[BackendMicroservice.done] Response headers present: ${Object.keys(microservice.route.responseHeaders)}`);
4157
+ stream.respond(microservice.route.responseHeaders);
4158
+ } else {
4159
+ throw Error(`[BackendMicroservice.done] No headers present.`);
4160
+ }
4123
4161
  } catch (e) {
4124
- logger.debug(e.toString());
4162
+ logger.debug(`[BackendMicroservice.done] Something went wrong sending response headers: ${e}`);
4125
4163
  }
4126
4164
  if (microservice.body !== null) {
4127
4165
  try {
4166
+ logger.debug(`[BackendMicroservice.done] A body of message is present. Finalizing the response...`);
4128
4167
  microservice.finishWithBody.call(microservice, stream);
4129
4168
  } catch (e) {
4130
- logger.debug(e.toString());
4169
+ logger.debug(`[BackendMicroservice.done] Something went wrong finalizing the response: ${e}`);
4131
4170
  }
4171
+ } else {
4172
+ logger.debug("[BackendMicroservice.done] No body present. Ending stream...");
4173
+ stream.end();
4132
4174
  }
4133
4175
  }
4134
4176