@gustavo-valsechi/node 1.0.44 → 1.0.45

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,6 +1,133 @@
1
1
  // src/database/index.ts
2
2
  import "dotenv/config";
3
3
  import { hostname } from "os";
4
+
5
+ // src/tools/response.ts
6
+ import { ZodError } from "zod";
7
+ import Utils from "@gustavo-valsechi/utils";
8
+ import _ from "lodash";
9
+
10
+ // src/tools/operators.ts
11
+ import {
12
+ Like,
13
+ MoreThan,
14
+ MoreThanOrEqual,
15
+ In,
16
+ Not,
17
+ LessThan,
18
+ Between,
19
+ Raw,
20
+ And,
21
+ LessThanOrEqual
22
+ } from "typeorm";
23
+ import _2 from "lodash";
24
+ var OPERATORS = {
25
+ $in: (value) => In(value),
26
+ $and: (value) => And(value),
27
+ $notIn: (value) => Not(In(value)),
28
+ $like: (value) => Like(value),
29
+ $notLike: (value) => Not(Like(value)),
30
+ $gt: (value) => MoreThan(value),
31
+ $gte: (value) => MoreThanOrEqual(value),
32
+ $lt: (value) => LessThan(value),
33
+ $lte: (value) => LessThanOrEqual(value),
34
+ $between: ([from, to]) => Between(from, to),
35
+ $not: (value) => Not(value),
36
+ $ne: (value) => Not(value),
37
+ $raw: (value) => Raw((alias) => _2.replace(value, "ALIAS", alias))
38
+ };
39
+ var existsOperator = (value) => {
40
+ let includeOp = false;
41
+ for (const op in OPERATORS) {
42
+ if (includeOp) continue;
43
+ includeOp = _2.includes(JSON.stringify(value), op);
44
+ }
45
+ return includeOp;
46
+ };
47
+ var converter = (operator) => {
48
+ operator = _2.isObject(operator) ? operator : JSON.parse(operator || "{}");
49
+ let convertion = operator;
50
+ for (const op in OPERATORS) {
51
+ if (!operator[op] && operator[op] !== 0) continue;
52
+ if (_2.isObject(operator[op]) && _2.isArray(operator[op])) {
53
+ _2.forEach(operator[op], (condition, index) => {
54
+ if (!existsOperator(condition)) return;
55
+ for (const opSecondary in OPERATORS) {
56
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
57
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
58
+ }
59
+ });
60
+ }
61
+ convertion = OPERATORS[op](operator[op]);
62
+ }
63
+ return convertion;
64
+ };
65
+ var refactorWhere = (where) => {
66
+ if (!where) return where;
67
+ for (const key in where) {
68
+ let includeOp = existsOperator(where[key]);
69
+ if (includeOp) {
70
+ where[key] = converter(where[key]);
71
+ if (_2.isObject(where[key]) && !_2.isArray(where[key])) {
72
+ for (const keyObject in where[key]) {
73
+ includeOp = existsOperator(where[key][keyObject]);
74
+ if (!includeOp) continue;
75
+ where[key][keyObject] = converter(where[key][keyObject]);
76
+ }
77
+ }
78
+ if (_2.isObject(where[key]) && _2.isArray(where[key])) {
79
+ _2.forEach(where[key], (condition, index) => {
80
+ includeOp = existsOperator(condition);
81
+ if (includeOp) {
82
+ condition = converter(condition);
83
+ if (_2.isObject(condition) && !_2.isArray(condition)) {
84
+ for (const keyObject in condition) {
85
+ includeOp = existsOperator(condition[keyObject]);
86
+ if (!includeOp) continue;
87
+ condition[keyObject] = converter(condition[keyObject]);
88
+ }
89
+ }
90
+ }
91
+ where[key][index] = condition;
92
+ });
93
+ }
94
+ }
95
+ }
96
+ return where;
97
+ };
98
+
99
+ // src/schemas/options.ts
100
+ import { z } from "zod";
101
+ import _3 from "lodash";
102
+ var parse = (json) => {
103
+ if (!json) return;
104
+ if (_3.isString(json)) return JSON.parse(json);
105
+ return json;
106
+ };
107
+ var OptionsSchema = z.object({
108
+ take: z.number().optional(),
109
+ page: z.number().default(0),
110
+ select: z.array(z.string()).optional(),
111
+ relations: z.array(z.string()).optional(),
112
+ where: z.any().transform((value) => refactorWhere(parse(value))).optional(),
113
+ order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
114
+ });
115
+
116
+ // src/tools/logger.ts
117
+ import Utils2 from "@gustavo-valsechi/utils";
118
+ import pino from "pino";
119
+ var logger = pino({
120
+ timestamp: () => `,"time":"${Utils2.moment().format("HH:mm:ss")}"`,
121
+ transport: {
122
+ target: "pino-pretty",
123
+ options: {
124
+ colorize: true,
125
+ ignore: "pid,hostname"
126
+ }
127
+ }
128
+ });
129
+
130
+ // src/database/index.ts
4
131
  import { DataSource } from "typeorm";
5
132
  var databaseInit = (entities, options) => {
6
133
  const type = process.env.DB_TYPE;
@@ -30,7 +157,7 @@ var databaseInit = (entities, options) => {
30
157
  applicationName: hostname(),
31
158
  entities
32
159
  });
33
- DB.initialize().then(() => console.log("\u{1F680} Data Source has been initialized!")).catch((err) => console.error("\u{1F534} Error during Data Source initialization", err));
160
+ DB.initialize().then(() => logger.info("\u{1F680} Data Source has been initialized!")).catch((err) => logger.error({ message: "\u{1F534} Error during Data Source initialization", error: err }));
34
161
  return DB;
35
162
  };
36
163
  export {
@@ -39,22 +39,42 @@ module.exports = __toCommonJS(router_exports);
39
39
  var import_zod = require("zod");
40
40
  var import_utils = __toESM(require("@gustavo-valsechi/utils"));
41
41
  var import_lodash = __toESM(require("lodash"));
42
- var responseWrapper = async (res, promise) => {
43
- return promise.then(async (data) => {
44
- res.status(200).send(await import_utils.default.cryptor.encrypt(data));
45
- }).catch((error) => {
42
+ var Response = class {
43
+ constructor(response) {
44
+ this.response = response;
45
+ }
46
+ async wrapper(promise) {
47
+ return promise.then(async (data) => {
48
+ return this.response.status(200).send(await import_utils.default.cryptor.encrypt(data));
49
+ }).catch((error) => {
50
+ logger.error(JSON.stringify(this.setError(error)));
51
+ return this.response.status(error.statusCode || 400).send(this.setError(error));
52
+ });
53
+ }
54
+ setError(error) {
46
55
  var _a;
47
- const status = error.statusCode || 500;
48
- let response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message) || error;
49
- if (error instanceof import_zod.ZodError) {
50
- response = import_lodash.default.transform(JSON.parse(response || "[]"), (acc, item) => {
51
- var _a2;
52
- acc[(_a2 = item == null ? void 0 : item.path) == null ? void 0 : _a2.join(",")] = (item == null ? void 0 : item.message) || "";
53
- }, {});
56
+ switch (true) {
57
+ case error instanceof import_zod.ZodError:
58
+ return this.setZodError(error);
59
+ default:
60
+ const response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message);
61
+ return {
62
+ statusCode: error.statusCode || 400,
63
+ code: error.code,
64
+ response: (error == null ? void 0 : error.isAxiosError) && !response ? "Bad Request" : response || error
65
+ };
54
66
  }
55
- console.error("\u{1F534} Request error", (error == null ? void 0 : error.response) || response);
56
- return res.status(status).send({ statusCode: status, code: error.code, response });
57
- });
67
+ }
68
+ setZodError(error) {
69
+ return {
70
+ statusCode: 400,
71
+ code: "ZOD_VALIDATION",
72
+ response: import_lodash.default.transform(JSON.parse(error || "[]"), (acc, item) => {
73
+ var _a;
74
+ acc[(_a = item == null ? void 0 : item.path) == null ? void 0 : _a.join(",")] = (item == null ? void 0 : item.message) || "";
75
+ }, {})
76
+ };
77
+ }
58
78
  };
59
79
 
60
80
  // src/tools/operators.ts
@@ -152,11 +172,24 @@ var OptionsSchema = import_zod2.z.object({
152
172
  order: import_zod2.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
153
173
  });
154
174
 
175
+ // src/tools/logger.ts
176
+ var import_utils2 = __toESM(require("@gustavo-valsechi/utils"));
177
+ var import_pino = __toESM(require("pino"));
178
+ var logger = (0, import_pino.default)({
179
+ timestamp: () => `,"time":"${import_utils2.default.moment().format("HH:mm:ss")}"`,
180
+ transport: {
181
+ target: "pino-pretty",
182
+ options: {
183
+ colorize: true,
184
+ ignore: "pid,hostname"
185
+ }
186
+ }
187
+ });
188
+
155
189
  // src/router/health.ts
156
190
  var healthRouter = (router, options, done) => {
157
191
  router.get("/", async (req, res) => {
158
- return responseWrapper(
159
- res,
192
+ return new Response(res).wrapper(
160
193
  new Promise((resolve) => {
161
194
  resolve({ message: "OK" });
162
195
  })
@@ -2,22 +2,42 @@
2
2
  import { ZodError } from "zod";
3
3
  import Utils from "@gustavo-valsechi/utils";
4
4
  import _ from "lodash";
5
- var responseWrapper = async (res, promise) => {
6
- return promise.then(async (data) => {
7
- res.status(200).send(await Utils.cryptor.encrypt(data));
8
- }).catch((error) => {
5
+ var Response = class {
6
+ constructor(response) {
7
+ this.response = response;
8
+ }
9
+ async wrapper(promise) {
10
+ return promise.then(async (data) => {
11
+ return this.response.status(200).send(await Utils.cryptor.encrypt(data));
12
+ }).catch((error) => {
13
+ logger.error(JSON.stringify(this.setError(error)));
14
+ return this.response.status(error.statusCode || 400).send(this.setError(error));
15
+ });
16
+ }
17
+ setError(error) {
9
18
  var _a;
10
- const status = error.statusCode || 500;
11
- let response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message) || error;
12
- if (error instanceof ZodError) {
13
- response = _.transform(JSON.parse(response || "[]"), (acc, item) => {
14
- var _a2;
15
- acc[(_a2 = item == null ? void 0 : item.path) == null ? void 0 : _a2.join(",")] = (item == null ? void 0 : item.message) || "";
16
- }, {});
19
+ switch (true) {
20
+ case error instanceof ZodError:
21
+ return this.setZodError(error);
22
+ default:
23
+ const response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message);
24
+ return {
25
+ statusCode: error.statusCode || 400,
26
+ code: error.code,
27
+ response: (error == null ? void 0 : error.isAxiosError) && !response ? "Bad Request" : response || error
28
+ };
17
29
  }
18
- console.error("\u{1F534} Request error", (error == null ? void 0 : error.response) || response);
19
- return res.status(status).send({ statusCode: status, code: error.code, response });
20
- });
30
+ }
31
+ setZodError(error) {
32
+ return {
33
+ statusCode: 400,
34
+ code: "ZOD_VALIDATION",
35
+ response: _.transform(JSON.parse(error || "[]"), (acc, item) => {
36
+ var _a;
37
+ acc[(_a = item == null ? void 0 : item.path) == null ? void 0 : _a.join(",")] = (item == null ? void 0 : item.message) || "";
38
+ }, {})
39
+ };
40
+ }
21
41
  };
22
42
 
23
43
  // src/tools/operators.ts
@@ -126,11 +146,24 @@ var OptionsSchema = z.object({
126
146
  order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
127
147
  });
128
148
 
149
+ // src/tools/logger.ts
150
+ import Utils2 from "@gustavo-valsechi/utils";
151
+ import pino from "pino";
152
+ var logger = pino({
153
+ timestamp: () => `,"time":"${Utils2.moment().format("HH:mm:ss")}"`,
154
+ transport: {
155
+ target: "pino-pretty",
156
+ options: {
157
+ colorize: true,
158
+ ignore: "pid,hostname"
159
+ }
160
+ }
161
+ });
162
+
129
163
  // src/router/health.ts
130
164
  var healthRouter = (router, options, done) => {
131
165
  router.get("/", async (req, res) => {
132
- return responseWrapper(
133
- res,
166
+ return new Response(res).wrapper(
134
167
  new Promise((resolve) => {
135
168
  resolve({ message: "OK" });
136
169
  })
@@ -40,22 +40,42 @@ var import_fastify = __toESM(require("fastify"));
40
40
  var import_zod = require("zod");
41
41
  var import_utils = __toESM(require("@gustavo-valsechi/utils"));
42
42
  var import_lodash = __toESM(require("lodash"));
43
- var responseWrapper = async (res, promise) => {
44
- return promise.then(async (data) => {
45
- res.status(200).send(await import_utils.default.cryptor.encrypt(data));
46
- }).catch((error) => {
43
+ var Response = class {
44
+ constructor(response) {
45
+ this.response = response;
46
+ }
47
+ async wrapper(promise) {
48
+ return promise.then(async (data) => {
49
+ return this.response.status(200).send(await import_utils.default.cryptor.encrypt(data));
50
+ }).catch((error) => {
51
+ logger.error(JSON.stringify(this.setError(error)));
52
+ return this.response.status(error.statusCode || 400).send(this.setError(error));
53
+ });
54
+ }
55
+ setError(error) {
47
56
  var _a;
48
- const status = error.statusCode || 500;
49
- let response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message) || error;
50
- if (error instanceof import_zod.ZodError) {
51
- response = import_lodash.default.transform(JSON.parse(response || "[]"), (acc, item) => {
52
- var _a2;
53
- acc[(_a2 = item == null ? void 0 : item.path) == null ? void 0 : _a2.join(",")] = (item == null ? void 0 : item.message) || "";
54
- }, {});
57
+ switch (true) {
58
+ case error instanceof import_zod.ZodError:
59
+ return this.setZodError(error);
60
+ default:
61
+ const response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message);
62
+ return {
63
+ statusCode: error.statusCode || 400,
64
+ code: error.code,
65
+ response: (error == null ? void 0 : error.isAxiosError) && !response ? "Bad Request" : response || error
66
+ };
55
67
  }
56
- console.error("\u{1F534} Request error", (error == null ? void 0 : error.response) || response);
57
- return res.status(status).send({ statusCode: status, code: error.code, response });
58
- });
68
+ }
69
+ setZodError(error) {
70
+ return {
71
+ statusCode: 400,
72
+ code: "ZOD_VALIDATION",
73
+ response: import_lodash.default.transform(JSON.parse(error || "[]"), (acc, item) => {
74
+ var _a;
75
+ acc[(_a = item == null ? void 0 : item.path) == null ? void 0 : _a.join(",")] = (item == null ? void 0 : item.message) || "";
76
+ }, {})
77
+ };
78
+ }
59
79
  };
60
80
 
61
81
  // src/tools/operators.ts
@@ -153,11 +173,24 @@ var OptionsSchema = import_zod2.z.object({
153
173
  order: import_zod2.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
154
174
  });
155
175
 
176
+ // src/tools/logger.ts
177
+ var import_utils2 = __toESM(require("@gustavo-valsechi/utils"));
178
+ var import_pino = __toESM(require("pino"));
179
+ var logger = (0, import_pino.default)({
180
+ timestamp: () => `,"time":"${import_utils2.default.moment().format("HH:mm:ss")}"`,
181
+ transport: {
182
+ target: "pino-pretty",
183
+ options: {
184
+ colorize: true,
185
+ ignore: "pid,hostname"
186
+ }
187
+ }
188
+ });
189
+
156
190
  // src/router/health.ts
157
191
  var healthRouter = (router, options, done) => {
158
192
  router.get("/", async (req, res) => {
159
- return responseWrapper(
160
- res,
193
+ return new Response(res).wrapper(
161
194
  new Promise((resolve) => {
162
195
  resolve({ message: "OK" });
163
196
  })
@@ -201,7 +234,7 @@ var serverInit = (appConfig) => {
201
234
  instance.addHook(
202
235
  "onResponse",
203
236
  (req, res, done) => {
204
- console.log(`${res.statusCode < 400 ? "\u2705" : "\u{1F534}"} ${res.statusCode} [${req.method}] - ${req.url}`);
237
+ logger[res.statusCode < 400 ? "info" : "error"](`${res.statusCode} [${req.method}] - ${req.url}`);
205
238
  done();
206
239
  }
207
240
  );
@@ -218,10 +251,10 @@ var serverInit = (appConfig) => {
218
251
  };
219
252
  app.listen(listenOptions, (err, address) => {
220
253
  if (err) {
221
- console.error("\u{1F534} Error in server initialization!", err);
254
+ logger.error({ message: "Error in server initialization!", error: err });
222
255
  process.exit(1);
223
256
  }
224
- console.log(`\u{1F680} Server has been started in ${address}...`);
257
+ logger.info(`\u{1F680} Server has been started in ${address}...`);
225
258
  });
226
259
  return app;
227
260
  };
@@ -5,22 +5,42 @@ import fastify from "fastify";
5
5
  import { ZodError } from "zod";
6
6
  import Utils from "@gustavo-valsechi/utils";
7
7
  import _ from "lodash";
8
- var responseWrapper = async (res, promise) => {
9
- return promise.then(async (data) => {
10
- res.status(200).send(await Utils.cryptor.encrypt(data));
11
- }).catch((error) => {
8
+ var Response = class {
9
+ constructor(response) {
10
+ this.response = response;
11
+ }
12
+ async wrapper(promise) {
13
+ return promise.then(async (data) => {
14
+ return this.response.status(200).send(await Utils.cryptor.encrypt(data));
15
+ }).catch((error) => {
16
+ logger.error(JSON.stringify(this.setError(error)));
17
+ return this.response.status(error.statusCode || 400).send(this.setError(error));
18
+ });
19
+ }
20
+ setError(error) {
12
21
  var _a;
13
- const status = error.statusCode || 500;
14
- let response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message) || error;
15
- if (error instanceof ZodError) {
16
- response = _.transform(JSON.parse(response || "[]"), (acc, item) => {
17
- var _a2;
18
- acc[(_a2 = item == null ? void 0 : item.path) == null ? void 0 : _a2.join(",")] = (item == null ? void 0 : item.message) || "";
19
- }, {});
22
+ switch (true) {
23
+ case error instanceof ZodError:
24
+ return this.setZodError(error);
25
+ default:
26
+ const response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message);
27
+ return {
28
+ statusCode: error.statusCode || 400,
29
+ code: error.code,
30
+ response: (error == null ? void 0 : error.isAxiosError) && !response ? "Bad Request" : response || error
31
+ };
20
32
  }
21
- console.error("\u{1F534} Request error", (error == null ? void 0 : error.response) || response);
22
- return res.status(status).send({ statusCode: status, code: error.code, response });
23
- });
33
+ }
34
+ setZodError(error) {
35
+ return {
36
+ statusCode: 400,
37
+ code: "ZOD_VALIDATION",
38
+ response: _.transform(JSON.parse(error || "[]"), (acc, item) => {
39
+ var _a;
40
+ acc[(_a = item == null ? void 0 : item.path) == null ? void 0 : _a.join(",")] = (item == null ? void 0 : item.message) || "";
41
+ }, {})
42
+ };
43
+ }
24
44
  };
25
45
 
26
46
  // src/tools/operators.ts
@@ -129,11 +149,24 @@ var OptionsSchema = z.object({
129
149
  order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
130
150
  });
131
151
 
152
+ // src/tools/logger.ts
153
+ import Utils2 from "@gustavo-valsechi/utils";
154
+ import pino from "pino";
155
+ var logger = pino({
156
+ timestamp: () => `,"time":"${Utils2.moment().format("HH:mm:ss")}"`,
157
+ transport: {
158
+ target: "pino-pretty",
159
+ options: {
160
+ colorize: true,
161
+ ignore: "pid,hostname"
162
+ }
163
+ }
164
+ });
165
+
132
166
  // src/router/health.ts
133
167
  var healthRouter = (router, options, done) => {
134
168
  router.get("/", async (req, res) => {
135
- return responseWrapper(
136
- res,
169
+ return new Response(res).wrapper(
137
170
  new Promise((resolve) => {
138
171
  resolve({ message: "OK" });
139
172
  })
@@ -177,7 +210,7 @@ var serverInit = (appConfig) => {
177
210
  instance.addHook(
178
211
  "onResponse",
179
212
  (req, res, done) => {
180
- console.log(`${res.statusCode < 400 ? "\u2705" : "\u{1F534}"} ${res.statusCode} [${req.method}] - ${req.url}`);
213
+ logger[res.statusCode < 400 ? "info" : "error"](`${res.statusCode} [${req.method}] - ${req.url}`);
181
214
  done();
182
215
  }
183
216
  );
@@ -194,10 +227,10 @@ var serverInit = (appConfig) => {
194
227
  };
195
228
  app.listen(listenOptions, (err, address) => {
196
229
  if (err) {
197
- console.error("\u{1F534} Error in server initialization!", err);
230
+ logger.error({ message: "Error in server initialization!", error: err });
198
231
  process.exit(1);
199
232
  }
200
- console.log(`\u{1F680} Server has been started in ${address}...`);
233
+ logger.info(`\u{1F680} Server has been started in ${address}...`);
201
234
  });
202
235
  return app;
203
236
  };
@@ -1,8 +1,15 @@
1
1
  import { FastifyReply } from 'fastify';
2
2
  import { OptionsSchema } from '../schemas/index.mjs';
3
3
  import { z } from 'zod';
4
+ import pino from 'pino';
4
5
 
5
- declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
6
+ declare class Response {
7
+ readonly response: FastifyReply;
8
+ constructor(response: FastifyReply);
9
+ wrapper(promise: Promise<any>): Promise<never>;
10
+ private setError;
11
+ private setZodError;
12
+ }
6
13
 
7
14
  declare const OPERATORS: any;
8
15
  declare const existsOperator: (value: any) => boolean;
@@ -17,4 +24,6 @@ declare const paginate: (repository: any, options: z.infer<typeof OptionsSchema>
17
24
 
18
25
  declare const moduler: () => Record<string, any>;
19
26
 
20
- export { OPERATORS, converter, existsOperator, moduler, paginate, refactorWhere, responseWrapper };
27
+ declare const logger: pino.Logger<never, boolean>;
28
+
29
+ export { OPERATORS, Response, converter, existsOperator, logger, moduler, paginate, refactorWhere };
@@ -1,8 +1,15 @@
1
1
  import { FastifyReply } from 'fastify';
2
2
  import { OptionsSchema } from '../schemas/index.js';
3
3
  import { z } from 'zod';
4
+ import pino from 'pino';
4
5
 
5
- declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
6
+ declare class Response {
7
+ readonly response: FastifyReply;
8
+ constructor(response: FastifyReply);
9
+ wrapper(promise: Promise<any>): Promise<never>;
10
+ private setError;
11
+ private setZodError;
12
+ }
6
13
 
7
14
  declare const OPERATORS: any;
8
15
  declare const existsOperator: (value: any) => boolean;
@@ -17,4 +24,6 @@ declare const paginate: (repository: any, options: z.infer<typeof OptionsSchema>
17
24
 
18
25
  declare const moduler: () => Record<string, any>;
19
26
 
20
- export { OPERATORS, converter, existsOperator, moduler, paginate, refactorWhere, responseWrapper };
27
+ declare const logger: pino.Logger<never, boolean>;
28
+
29
+ export { OPERATORS, Response, converter, existsOperator, logger, moduler, paginate, refactorWhere };
@@ -31,12 +31,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var tools_exports = {};
32
32
  __export(tools_exports, {
33
33
  OPERATORS: () => OPERATORS,
34
+ Response: () => Response,
34
35
  converter: () => converter,
35
36
  existsOperator: () => existsOperator,
37
+ logger: () => logger,
36
38
  moduler: () => moduler,
37
39
  paginate: () => paginate,
38
- refactorWhere: () => refactorWhere,
39
- responseWrapper: () => responseWrapper
40
+ refactorWhere: () => refactorWhere
40
41
  });
41
42
  module.exports = __toCommonJS(tools_exports);
42
43
 
@@ -44,22 +45,42 @@ module.exports = __toCommonJS(tools_exports);
44
45
  var import_zod = require("zod");
45
46
  var import_utils = __toESM(require("@gustavo-valsechi/utils"));
46
47
  var import_lodash = __toESM(require("lodash"));
47
- var responseWrapper = async (res, promise) => {
48
- return promise.then(async (data) => {
49
- res.status(200).send(await import_utils.default.cryptor.encrypt(data));
50
- }).catch((error) => {
48
+ var Response = class {
49
+ constructor(response) {
50
+ this.response = response;
51
+ }
52
+ async wrapper(promise) {
53
+ return promise.then(async (data) => {
54
+ return this.response.status(200).send(await import_utils.default.cryptor.encrypt(data));
55
+ }).catch((error) => {
56
+ logger.error(JSON.stringify(this.setError(error)));
57
+ return this.response.status(error.statusCode || 400).send(this.setError(error));
58
+ });
59
+ }
60
+ setError(error) {
51
61
  var _a;
52
- const status = error.statusCode || 500;
53
- let response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message) || error;
54
- if (error instanceof import_zod.ZodError) {
55
- response = import_lodash.default.transform(JSON.parse(response || "[]"), (acc, item) => {
56
- var _a2;
57
- acc[(_a2 = item == null ? void 0 : item.path) == null ? void 0 : _a2.join(",")] = (item == null ? void 0 : item.message) || "";
58
- }, {});
62
+ switch (true) {
63
+ case error instanceof import_zod.ZodError:
64
+ return this.setZodError(error);
65
+ default:
66
+ const response = ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) || (error == null ? void 0 : error.message);
67
+ return {
68
+ statusCode: error.statusCode || 400,
69
+ code: error.code,
70
+ response: (error == null ? void 0 : error.isAxiosError) && !response ? "Bad Request" : response || error
71
+ };
59
72
  }
60
- console.error("\u{1F534} Request error", (error == null ? void 0 : error.response) || response);
61
- return res.status(status).send({ statusCode: status, code: error.code, response });
62
- });
73
+ }
74
+ setZodError(error) {
75
+ return {
76
+ statusCode: 400,
77
+ code: "ZOD_VALIDATION",
78
+ response: import_lodash.default.transform(JSON.parse(error || "[]"), (acc, item) => {
79
+ var _a;
80
+ acc[(_a = item == null ? void 0 : item.path) == null ? void 0 : _a.join(",")] = (item == null ? void 0 : item.message) || "";
81
+ }, {})
82
+ };
83
+ }
63
84
  };
64
85
 
65
86
  // src/tools/operators.ts
@@ -185,13 +206,28 @@ var moduler = () => {
185
206
  });
186
207
  return content;
187
208
  };
209
+
210
+ // src/tools/logger.ts
211
+ var import_utils2 = __toESM(require("@gustavo-valsechi/utils"));
212
+ var import_pino = __toESM(require("pino"));
213
+ var logger = (0, import_pino.default)({
214
+ timestamp: () => `,"time":"${import_utils2.default.moment().format("HH:mm:ss")}"`,
215
+ transport: {
216
+ target: "pino-pretty",
217
+ options: {
218
+ colorize: true,
219
+ ignore: "pid,hostname"
220
+ }
221
+ }
222
+ });
188
223
  // Annotate the CommonJS export names for ESM import in node:
189
224
  0 && (module.exports = {
190
225
  OPERATORS,
226
+ Response,
191
227
  converter,
192
228
  existsOperator,
229
+ logger,
193
230
  moduler,
194
231
  paginate,
195
- refactorWhere,
196
- responseWrapper
232
+ refactorWhere
197
233
  });