ismx-nexo-node-app 0.4.24 → 0.4.26

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,6 +14,7 @@ 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();
@@ -42,20 +43,29 @@ class RepositoryRest extends Repository_1.default {
42
43
  let headers = {};
43
44
  res.headers.forEach((value, key) => headers[key] = value);
44
45
  let rawResponse = yield res.text();
45
- (_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, rawResponse);
46
+ (_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, request, rawResponse);
46
47
  let response = {
47
48
  httpCode: res.status,
48
- content: JSON.stringify(rawResponse),
49
+ content: JSON.parse(rawResponse, this.reviver),
49
50
  headers
50
51
  };
51
- (_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.onResponse) === null || _d === void 0 ? void 0 : _d.call(_c, response);
52
+ (_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.onResponse) === null || _d === void 0 ? void 0 : _d.call(_c, request, response);
52
53
  return response;
53
54
  })).catch((error) => {
54
55
  var _a, _b;
55
- (_b = (_a = this.options).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
56
+ (_b = (_a = this.options).onError) === null || _b === void 0 ? void 0 : _b.call(_a, request, error);
56
57
  throw error;
57
58
  });
58
59
  });
59
60
  }
61
+ reviver(key, value) {
62
+ var _a, _b;
63
+ let revived = (_b = (_a = this.options).jsonReviver) === null || _b === void 0 ? void 0 : _b.call(_a, key, value);
64
+ if (revived)
65
+ return revived;
66
+ if (DateUtils_1.default.isIsoDate(value))
67
+ return new Date(value);
68
+ return value;
69
+ }
60
70
  }
61
71
  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,13 +2,15 @@ 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
- onResponse?: (response: HttpResponse) => void;
6
- onRawResponse?: (response: string) => void;
7
- onError?: (error: Error) => void;
5
+ jsonReviver?: (key: string, value: string) => any;
6
+ onResponse?: (request: HttpRequest, response: HttpResponse) => void;
7
+ onRawResponse?: (request: HttpRequest, response: string) => void;
8
+ onError?: (request: HttpRequest, error: Error) => void;
8
9
  }
9
10
  export default class RepositoryRest<Body = any, Res = any> extends Repository {
10
11
  private readonly baseUrl;
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.24",
3
+ "version": "0.4.26",
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,12 +1,14 @@
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
7
- onResponse?: (response: HttpResponse) => void;
8
- onRawResponse?: (response: string) => void;
9
- onError?: (error: Error) => void
8
+ jsonReviver?: (key: string, value: string) => any
9
+ onResponse?: (request: HttpRequest, response: HttpResponse) => void;
10
+ onRawResponse?: (request: HttpRequest, response: string) => void;
11
+ onError?: (request: HttpRequest, error: Error) => void
10
12
  }
11
13
 
12
14
  export default class RepositoryRest<Body=any,Res=any> extends Repository
@@ -52,19 +54,27 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
52
54
  return call.then(async (res: Response) => {
53
55
  let headers: any = {}; res.headers.forEach((value, key) => headers[key] = value);
54
56
  let rawResponse = await res.text();
55
- this.options.onRawResponse?.(rawResponse);
57
+ this.options.onRawResponse?.(request, rawResponse);
56
58
  let response: HttpResponse<E> = {
57
59
  httpCode: res.status,
58
- content: JSON.stringify(rawResponse) as E,
60
+ content: JSON.parse(rawResponse, this.reviver) as E,
59
61
  headers
60
62
  };
61
- this.options?.onResponse?.(response);
63
+ this.options?.onResponse?.(request, response);
62
64
  return response;
63
65
  }).catch((error) => {
64
- this.options.onError?.(error);
66
+ this.options.onError?.(request, error);
65
67
  throw error;
66
68
  });
67
69
  }
68
70
 
71
+ private reviver(key: string, value: string) {
72
+ let revived = this.options.jsonReviver?.(key, value);
73
+ if (revived) return revived;
74
+
75
+ if (DateUtils.isIsoDate(value)) return new Date(value);
76
+ return value;
77
+ }
78
+
69
79
 
70
80
  }