c2-http 1.0.104 → 1.0.106

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.
@@ -1,7 +1,7 @@
1
1
  import express from "express";
2
2
  declare class MiddlewareBody {
3
3
  config(expressApplication: express.Application): void;
4
- private convertDates;
4
+ convertDates(obj: any, headers: any): void;
5
5
  }
6
6
  declare const _default: MiddlewareBody;
7
7
  export default _default;
@@ -4,7 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const body_parser_1 = __importDefault(require("body-parser"));
7
- const moment_1 = __importDefault(require("moment"));
7
+ // import moment from "moment";
8
+ const moment_timezone_1 = __importDefault(require("moment-timezone"));
8
9
  class MiddlewareBody {
9
10
  config(expressApplication) {
10
11
  expressApplication.use(body_parser_1.default.json({ limit: "5mb" }));
@@ -12,19 +13,43 @@ class MiddlewareBody {
12
13
  expressApplication.use(body_parser_1.default.text({ limit: "5mb" }));
13
14
  expressApplication.use((request, response, next) => {
14
15
  if (request.body && typeof request.body === "object") {
15
- this.convertDates(request.body);
16
+ this.convertDates(request.body, request.headers);
16
17
  }
17
18
  next();
18
19
  });
19
20
  }
20
- convertDates(obj) {
21
- for (const key in obj) {
22
- if (obj.hasOwnProperty(key)) {
21
+ // private convertDates(request: Request): void {
22
+ // for (const key in request.body) {
23
+ // if (request.body.hasOwnProperty(key)) {
24
+ // if (key.endsWith("Date") || key.endsWith("DateTime")) {
25
+ // const tz = request.headers["timezone"] as string || "America/Sao_Paulo" as string; // fallback padrão
26
+ // // interpreta como GMT-3
27
+ // request.body[key] = moment.tz(request.body[key], "YYYY-MM-DD", tz);
28
+ // // obj[key] = moment.utc(obj[key]);
29
+ // } else if (typeof request.body[key] === "object" && request.body[key] !== null) {
30
+ // this.convertDates(request.body[key]);
31
+ // }
32
+ // }
33
+ // }
34
+ // }
35
+ convertDates(obj, headers) {
36
+ if (Array.isArray(obj)) {
37
+ // percorre arrays também
38
+ for (let i = 0; i < obj.length; i++) {
39
+ this.convertDates(obj[i], headers);
40
+ }
41
+ }
42
+ else if (typeof obj === "object" && obj !== null) {
43
+ for (const key in obj) {
44
+ if (!obj.hasOwnProperty(key))
45
+ continue;
46
+ const value = obj[key];
23
47
  if (key.endsWith("Date") || key.endsWith("DateTime")) {
24
- obj[key] = moment_1.default.utc(obj[key]);
48
+ const tz = headers["timezone"] || "America/Sao_Paulo"; // fallback padrão
49
+ obj[key] = moment_timezone_1.default.tz(value, "YYYY-MM-DD", tz).utc();
25
50
  }
26
- else if (typeof obj[key] === "object" && obj[key] !== null) {
27
- this.convertDates(obj[key]);
51
+ else {
52
+ this.convertDates(value, headers); // recursivo
28
53
  }
29
54
  }
30
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-http",
3
- "version": "1.0.104",
3
+ "version": "1.0.106",
4
4
  "description": "Biblioteca Typescript para API NodeJS",
5
5
  "repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
6
6
  "author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",
@@ -34,6 +34,7 @@
34
34
  "http-status": "^1.7.4",
35
35
  "jsonwebtoken": "^9.0.0",
36
36
  "moment": "^2.30.1",
37
+ "moment-timezone": "^0.6.0",
37
38
  "qs": "^6.13.0",
38
39
  "swagger-autogen": "^2.23.7",
39
40
  "swagger-ui-express": "^5.0.0",