ismx-nexo-node-app 0.4.25 → 0.4.27

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.
@@ -68,6 +68,29 @@ class DateUtils {
68
68
  result += ":" + `${final.getSeconds()}`.padStart(2, "0");
69
69
  return result;
70
70
  }
71
+ static isIsoDate(value) {
72
+ return typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(value);
73
+ }
74
+ static reviveDate(value) {
75
+ return DateUtils.isIsoDate(value) ? new Date(value) : value;
76
+ }
77
+ static reviveDates(obj) {
78
+ const isIsoDate = (str) => typeof str === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(str);
79
+ const recursivelyRevive = (input) => {
80
+ if (Array.isArray(input))
81
+ return input.map(recursivelyRevive);
82
+ if (input && typeof input === 'object') {
83
+ for (const key in input) {
84
+ if (isIsoDate(input[key]))
85
+ input[key] = new Date(input[key]);
86
+ else
87
+ input[key] = recursivelyRevive(input[key]);
88
+ }
89
+ }
90
+ return input;
91
+ };
92
+ return recursivelyRevive(obj);
93
+ }
71
94
  }
72
95
  DateUtils.MILLIS = 1;
73
96
  DateUtils.SECONDS = 1000 * DateUtils.MILLIS;
@@ -14,17 +14,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const Repository_1 = __importDefault(require("./Repository"));
16
16
  const QueryUtils_1 = __importDefault(require("./utils/QueryUtils"));
17
+ const DateUtils_1 = __importDefault(require("../business/utils/DateUtils"));
17
18
  class RepositoryRest extends Repository_1.default {
18
19
  constructor(baseUrl, options = {}) {
19
20
  super();
20
21
  this.baseUrl = baseUrl;
21
22
  this.options = options;
23
+ this.reviver = this.reviver.bind(this);
22
24
  }
23
25
  call() {
24
26
  return __awaiter(this, arguments, void 0, function* (method = 'GET', endpoint = '/', request = {}) {
25
27
  var _a, _b, _c, _d, _e;
26
28
  // Especifica el método de la llamada HTTP al recurso.
27
29
  request.method = method;
30
+ request.endpoint = endpoint;
28
31
  // Notifica que una petición REST va a ser realizada
29
32
  request = (_c = (_b = (_a = this.options).interceptor) === null || _b === void 0 ? void 0 : _b.call(_a, endpoint, request)) !== null && _c !== void 0 ? _c : request;
30
33
  // Completa la información de las cabeceras con cabeceras comunes por llamada a API.
@@ -45,7 +48,7 @@ class RepositoryRest extends Repository_1.default {
45
48
  (_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, request, rawResponse);
46
49
  let response = {
47
50
  httpCode: res.status,
48
- content: JSON.parse(rawResponse),
51
+ content: JSON.parse(rawResponse, this.reviver),
49
52
  headers
50
53
  };
51
54
  (_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.onResponse) === null || _d === void 0 ? void 0 : _d.call(_c, request, response);
@@ -57,5 +60,14 @@ class RepositoryRest extends Repository_1.default {
57
60
  });
58
61
  });
59
62
  }
63
+ reviver(key, value) {
64
+ var _a, _b;
65
+ let revived = (_b = (_a = this.options).jsonReviver) === null || _b === void 0 ? void 0 : _b.call(_a, key, value);
66
+ if (revived)
67
+ return revived;
68
+ if (DateUtils_1.default.isIsoDate(value))
69
+ return new Date(value);
70
+ return value;
71
+ }
60
72
  }
61
73
  exports.default = RepositoryRest;
@@ -14,4 +14,7 @@ export default abstract class DateUtils {
14
14
  static toIsoDate(date: Date): string;
15
15
  static sum(date: Date, value: number, component: number): Date;
16
16
  static sumTime(date: string, value: number, component: number): string;
17
+ static isIsoDate(value: string): boolean;
18
+ static reviveDate(value: string): string | Date;
19
+ static reviveDates<T>(obj: T): T;
17
20
  }
@@ -2,6 +2,7 @@ import Repository from "./Repository";
2
2
  import { HttpRequest, HttpResponse } from "../api/Service";
3
3
  export interface RestOptions {
4
4
  interceptor?: (endpoint: string, request: HttpRequest) => HttpRequest | undefined;
5
+ jsonReviver?: (key: string, value: string) => any;
5
6
  onResponse?: (request: HttpRequest, response: HttpResponse) => void;
6
7
  onRawResponse?: (request: HttpRequest, response: string) => void;
7
8
  onError?: (request: HttpRequest, error: Error) => void;
@@ -11,4 +12,5 @@ export default class RepositoryRest<Body = any, Res = any> extends Repository {
11
12
  private readonly options;
12
13
  constructor(baseUrl: string, options?: RestOptions);
13
14
  call<B = Body, E = Res>(method?: string, endpoint?: string, request?: HttpRequest<B>): Promise<HttpResponse<E>>;
15
+ private reviver;
14
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.25",
3
+ "version": "0.4.27",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -90,4 +90,29 @@ export default abstract class DateUtils {
90
90
  return result;
91
91
  }
92
92
 
93
- }
93
+ static isIsoDate(value: string) {
94
+ return typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(value);
95
+ }
96
+
97
+ static reviveDate(value: string): string | Date {
98
+ return DateUtils.isIsoDate(value) ? new Date(value) : value;
99
+ }
100
+
101
+ static reviveDates<T>(obj: T): T
102
+ {
103
+ const isIsoDate = (str: any) => typeof str === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(str);
104
+
105
+ const recursivelyRevive = (input: any): any => {
106
+ if (Array.isArray(input)) return input.map(recursivelyRevive);
107
+ if (input && typeof input === 'object') {
108
+ for (const key in input) {
109
+ if (isIsoDate(input[key])) input[key] = new Date(input[key]);
110
+ else input[key] = recursivelyRevive(input[key]);
111
+ }
112
+ }
113
+ return input;
114
+ };
115
+
116
+ return recursivelyRevive(obj);
117
+ }
118
+ }
@@ -1,9 +1,11 @@
1
1
  import Repository from "./Repository";
2
2
  import {HttpRequest, HttpResponse} from "../api/Service";
3
3
  import QueryUtils from "./utils/QueryUtils";
4
+ import DateUtils from "../business/utils/DateUtils";
4
5
 
5
6
  export interface RestOptions {
6
7
  interceptor?: (endpoint: string, request: HttpRequest) => HttpRequest | undefined
8
+ jsonReviver?: (key: string, value: string) => any
7
9
  onResponse?: (request: HttpRequest, response: HttpResponse) => void;
8
10
  onRawResponse?: (request: HttpRequest, response: string) => void;
9
11
  onError?: (request: HttpRequest, error: Error) => void
@@ -19,6 +21,8 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
19
21
  super();
20
22
  this.baseUrl = baseUrl;
21
23
  this.options = options;
24
+
25
+ this.reviver = this.reviver.bind(this);
22
26
  }
23
27
 
24
28
  async call<B=Body, E = Res>(
@@ -29,6 +33,7 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
29
33
  {
30
34
  // Especifica el método de la llamada HTTP al recurso.
31
35
  request.method = method;
36
+ request.endpoint = endpoint;
32
37
 
33
38
  // Notifica que una petición REST va a ser realizada
34
39
  request = this.options.interceptor?.(endpoint, request) ?? request
@@ -55,7 +60,7 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
55
60
  this.options.onRawResponse?.(request, rawResponse);
56
61
  let response: HttpResponse<E> = {
57
62
  httpCode: res.status,
58
- content: JSON.parse(rawResponse) as E,
63
+ content: JSON.parse(rawResponse, this.reviver) as E,
59
64
  headers
60
65
  };
61
66
  this.options?.onResponse?.(request, response);
@@ -66,5 +71,13 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
66
71
  });
67
72
  }
68
73
 
74
+ private reviver(key: string, value: string) {
75
+ let revived = this.options.jsonReviver?.(key, value);
76
+ if (revived) return revived;
77
+
78
+ if (DateUtils.isIsoDate(value)) return new Date(value);
79
+ return value;
80
+ }
81
+
69
82
 
70
83
  }