ismx-nexo-node-app 0.4.46 → 0.4.48

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.
@@ -31,8 +31,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
33
  };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
34
37
  Object.defineProperty(exports, "__esModule", { value: true });
35
38
  const node_fetch_1 = __importStar(require("node-fetch"));
39
+ const QueryUtils_1 = __importDefault(require("../repository/utils/QueryUtils"));
36
40
  class BusinessProxy {
37
41
  constructor() {
38
42
  this.modules = {};
@@ -42,36 +46,29 @@ class BusinessProxy {
42
46
  }
43
47
  call(tag, method, endpoint, request) {
44
48
  return __awaiter(this, void 0, void 0, function* () {
49
+ var _a, _b, _c;
45
50
  let module = this.modules[tag];
46
51
  if (!module)
47
52
  throw new Error(`Module ${tag} does not exist`);
48
- let headers = new node_fetch_1.Headers();
53
+ // Completa la información de las cabeceras con cabeceras comunes por llamada a API.
54
+ let headers = new node_fetch_1.Headers(request.headers);
49
55
  headers.set('Content-Type', 'application/json');
50
56
  headers.set('Cache-Control', 'no-cache');
51
57
  headers.set('Pragma', 'no-cache');
52
- for (let header in request.headers)
53
- headers.set(header, request.headers[header]);
54
- let response = null;
58
+ for (const header of Object.keys(headers))
59
+ headers.set(header, (_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a[header]) !== null && _b !== void 0 ? _b : "");
60
+ // Si el body especificado es un objeto, lo convierte a JSON.
61
+ let body = request.body;
62
+ if ((_c = headers.get('Content-Type')) === null || _c === void 0 ? void 0 : _c.startsWith('application/json'))
63
+ body = JSON.stringify(request.body);
64
+ // Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
55
65
  try {
56
- return yield (0, node_fetch_1.default)(`${module.host}${endpoint}${this.map(request.query)}`, {
57
- method: method,
58
- body: method !== "GET" ? JSON.stringify(request.body) : undefined,
59
- headers: headers,
60
- });
66
+ return (0, node_fetch_1.default)(module.host + endpoint + QueryUtils_1.default.map(request.query), Object.assign(Object.assign({}, request), { method, body: method !== "GET" ? body : undefined, headers }));
61
67
  }
62
68
  catch (error) {
63
69
  throw new Error(`Module ${tag} is not responding`);
64
70
  }
65
71
  });
66
72
  }
67
- map(query = {}) {
68
- let queryString = "?";
69
- for (const param in query) {
70
- if (query[param] === undefined)
71
- continue;
72
- queryString += `${param}=${query[param]}&`;
73
- }
74
- return queryString;
75
- }
76
73
  }
77
74
  exports.default = BusinessProxy;
@@ -83,6 +83,7 @@ class BusinessServer extends Business_1.default {
83
83
  case 'xml': return this.express.xml();
84
84
  case 'stream': return this.express.raw({ type: 'application/octet-stream', limit: '10mb' });
85
85
  case 'image': return this.express.raw({ type: 'image/*', limit: '10mb' });
86
+ case '*': return this.express.raw({ limit: '10mb' });
86
87
  default: return this.express.json();
87
88
  }
88
89
  })();
@@ -37,10 +37,11 @@ class RepositoryRest extends Repository_1.default {
37
37
  headers.set('Pragma', 'no-cache');
38
38
  for (const header of Object.keys(headers))
39
39
  headers.set(header, (_e = (_d = request.headers) === null || _d === void 0 ? void 0 : _d[header]) !== null && _e !== void 0 ? _e : "");
40
- // Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
40
+ // Si el body especificado es un objeto, lo convierte a JSON.
41
41
  let body = request.body;
42
42
  if ((_f = headers.get('Content-Type')) === null || _f === void 0 ? void 0 : _f.startsWith('application/json'))
43
43
  body = JSON.stringify(request.body);
44
+ // Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
44
45
  let call = fetch(this.baseUrl + endpoint + QueryUtils_1.default.map(request.query), Object.assign(Object.assign({}, request), { body: method !== "GET" ? body : undefined, headers }));
45
46
  // Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
46
47
  return call.then((res) => __awaiter(this, void 0, void 0, function* () {
@@ -24,7 +24,7 @@ export declare class HttpResponse<T = any> {
24
24
  static ok<T = any>(content: T): HttpResponse<any>;
25
25
  static ko<T = any>(httpCode: number, content: T): HttpResponse<any>;
26
26
  }
27
- export type ServiceType = "json" | "xml" | 'stream' | 'image';
27
+ export type ServiceType = "json" | "xml" | 'stream' | 'image' | '*';
28
28
  export default abstract class Service<Req, Res> {
29
29
  static readonly services: Service<any, any>[];
30
30
  readonly method: string;
@@ -11,5 +11,4 @@ export default class BusinessProxy {
11
11
  [key: string]: Module;
12
12
  }): void;
13
13
  call<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<fetch.Response>;
14
- private map;
15
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.46",
3
+ "version": "0.4.48",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -29,7 +29,7 @@ export class HttpResponse<T = any> {
29
29
  }
30
30
  }
31
31
 
32
- export type ServiceType = "json" | "xml" | 'stream' | 'image';
32
+ export type ServiceType = "json" | "xml" | 'stream' | 'image' | '*';
33
33
  export default abstract class Service<Req, Res>
34
34
  {
35
35
  public static readonly services: Service<any, any>[] = []
@@ -1,6 +1,7 @@
1
1
  import { HttpResponse, HttpRequest } from "../api/Service"
2
2
  import fetch, {Body, Headers} from 'node-fetch';
3
3
  import {Wrapper} from "../api/ServiceRestFormal";
4
+ import QueryUtils from "../repository/utils/QueryUtils";
4
5
 
5
6
  export interface Module {
6
7
  id: string;
@@ -20,29 +21,26 @@ export default class BusinessProxy
20
21
  let module = this.modules[tag];
21
22
  if (!module) throw new Error(`Module ${tag} does not exist`);
22
23
 
23
- let headers = new Headers();
24
+
25
+ // Completa la información de las cabeceras con cabeceras comunes por llamada a API.
26
+ let headers = new Headers(request.headers);
24
27
  headers.set('Content-Type', 'application/json');
25
28
  headers.set('Cache-Control', 'no-cache');
26
29
  headers.set('Pragma', 'no-cache');
27
- for (let header in request.headers)
28
- headers.set(header, request.headers[header]);
30
+ for (const header of Object.keys(headers))
31
+ headers.set(header, request.headers?.[header] ?? "");
32
+
33
+ // Si el body especificado es un objeto, lo convierte a JSON.
34
+ let body: string = request.body as string;
35
+ if (headers.get('Content-Type')?.startsWith('application/json'))
36
+ body = JSON.stringify(request.body);
29
37
 
30
- let response = null;
38
+ // Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
31
39
  try {
32
- return await fetch(`${module.host}${endpoint}${this.map(request.query)}`, {
33
- method: method,
34
- body: method !== "GET" ? JSON.stringify(request.body) : undefined,
35
- headers: headers,
40
+ return fetch(module.host + endpoint + QueryUtils.map(request.query), {
41
+ ...request, method, body: method !== "GET" ? body : undefined, headers
36
42
  });
37
- } catch (error) { throw new Error(`Module ${tag} is not responding`); }
38
- }
39
43
 
40
- private map(query: { [key:string]: string } = {}) {
41
- let queryString = "?"
42
- for (const param in query) {
43
- if (query[param] === undefined) continue;
44
- queryString += `${param}=${query[param]}&`
45
- }
46
- return queryString;
44
+ } catch (error) { throw new Error(`Module ${tag} is not responding`); }
47
45
  }
48
46
  }
@@ -65,6 +65,7 @@ export default class BusinessServer extends Business
65
65
  case 'xml': return this.express.xml();
66
66
  case 'stream': return this.express.raw({ type: 'application/octet-stream', limit: '10mb' });
67
67
  case 'image': return this.express.raw({ type: 'image/*', limit: '10mb' });
68
+ case '*': return this.express.raw({ limit: '10mb' });
68
69
  default: return this.express.json();
69
70
  }})();
70
71
 
@@ -46,11 +46,12 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
46
46
  for (const header of Object.keys(headers))
47
47
  headers.set(header, request.headers?.[header] ?? "");
48
48
 
49
- // Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
49
+ // Si el body especificado es un objeto, lo convierte a JSON.
50
50
  let body: string = request.body as string;
51
51
  if (headers.get('Content-Type')?.startsWith('application/json'))
52
52
  body = JSON.stringify(request.body);
53
53
 
54
+ // Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
54
55
  let call = fetch(this.baseUrl + endpoint + QueryUtils.map(request.query), {
55
56
  ...request, body: method !== "GET" ? body : undefined, headers
56
57
  });