c2-http 1.0.105 → 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;
@@ -13,22 +13,43 @@ class MiddlewareBody {
13
13
  expressApplication.use(body_parser_1.default.text({ limit: "5mb" }));
14
14
  expressApplication.use((request, response, next) => {
15
15
  if (request.body && typeof request.body === "object") {
16
- this.convertDates(request);
16
+ this.convertDates(request.body, request.headers);
17
17
  }
18
18
  next();
19
19
  });
20
20
  }
21
- convertDates(request) {
22
- for (const key in request.body) {
23
- if (request.body.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];
24
47
  if (key.endsWith("Date") || key.endsWith("DateTime")) {
25
- const tz = request.headers["timezone"] || "America/Sao_Paulo"; // fallback padrão
26
- // interpreta como GMT-3
27
- request.body[key] = moment_timezone_1.default.tz(request.body[key], "YYYY-MM-DD", tz);
28
- // obj[key] = moment.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();
29
50
  }
30
- else if (typeof request.body[key] === "object" && request.body[key] !== null) {
31
- this.convertDates(request.body[key]);
51
+ else {
52
+ this.convertDates(value, headers); // recursivo
32
53
  }
33
54
  }
34
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-http",
3
- "version": "1.0.105",
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>",