@gustavo-valsechi/node 1.0.32 → 1.0.33

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.
package/dist/index.d.mts CHANGED
@@ -6,3 +6,5 @@ export { IMiddlewareContract, TenantService } from './src/services/index.mjs';
6
6
  import 'typeorm';
7
7
  import 'fastify';
8
8
  import 'http';
9
+ import './src/schemas/index.mjs';
10
+ import 'zod';
package/dist/index.d.ts CHANGED
@@ -6,3 +6,5 @@ export { IMiddlewareContract, TenantService } from './src/services/index.js';
6
6
  import 'typeorm';
7
7
  import 'fastify';
8
8
  import 'http';
9
+ import './src/schemas/index.js';
10
+ import 'zod';
package/dist/index.js CHANGED
@@ -148,6 +148,7 @@ var converter = (operator) => {
148
148
  return convertion;
149
149
  };
150
150
  var refactorWhere = (where) => {
151
+ if (!where) return where;
151
152
  for (const key in where) {
152
153
  let includeOp = existsOperator(where[key]);
153
154
  if (includeOp) {
@@ -180,26 +181,33 @@ var refactorWhere = (where) => {
180
181
  return where;
181
182
  };
182
183
 
183
- // src/tools/paginate.ts
184
+ // src/schemas/options.ts
185
+ var import_zod2 = require("zod");
184
186
  var import_lodash3 = __toESM(require("lodash"));
185
187
  var parse = (json) => {
186
188
  if (!json) return;
187
189
  if (import_lodash3.default.isString(json)) return JSON.parse(json);
188
190
  return json;
189
191
  };
192
+ var OptionsSchema = import_zod2.z.object({
193
+ take: import_zod2.z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
194
+ page: import_zod2.z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
195
+ select: import_zod2.z.array(import_zod2.z.string()).optional(),
196
+ relations: import_zod2.z.array(import_zod2.z.string()).optional(),
197
+ where: import_zod2.z.any().transform((value) => refactorWhere(parse(value))).optional(),
198
+ order: import_zod2.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
199
+ });
200
+
201
+ // src/tools/paginate.ts
190
202
  var paginate = async (repository, options) => {
191
- let where = parse(options.filters);
192
- if (where) where = refactorWhere(where);
203
+ var _a;
204
+ const params = await OptionsSchema.parseAsync(options);
193
205
  const [data, count] = await repository.findAndCount({
194
- select: options.fields,
195
- relations: options.relations,
196
- where,
197
- order: parse(options.order),
198
- take: options.limit,
199
- skip: Number(options.page) * (Number(options.limit) || 0) || 0
206
+ skip: Number(params.page) * (Number(params.take) || 0) || 0,
207
+ ...params
200
208
  });
201
209
  return {
202
- totalPage: Math.ceil(count / options.limit),
210
+ totalPage: options.take ? Math.ceil(count / options.take) : (_a = options.take) != null ? _a : 1,
203
211
  total: count,
204
212
  content: data
205
213
  };
package/dist/index.mjs CHANGED
@@ -118,6 +118,7 @@ var converter = (operator) => {
118
118
  return convertion;
119
119
  };
120
120
  var refactorWhere = (where) => {
121
+ if (!where) return where;
121
122
  for (const key in where) {
122
123
  let includeOp = existsOperator(where[key]);
123
124
  if (includeOp) {
@@ -150,26 +151,33 @@ var refactorWhere = (where) => {
150
151
  return where;
151
152
  };
152
153
 
153
- // src/tools/paginate.ts
154
+ // src/schemas/options.ts
155
+ import { z } from "zod";
154
156
  import _3 from "lodash";
155
157
  var parse = (json) => {
156
158
  if (!json) return;
157
159
  if (_3.isString(json)) return JSON.parse(json);
158
160
  return json;
159
161
  };
162
+ var OptionsSchema = z.object({
163
+ take: z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
164
+ page: z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
165
+ select: z.array(z.string()).optional(),
166
+ relations: z.array(z.string()).optional(),
167
+ where: z.any().transform((value) => refactorWhere(parse(value))).optional(),
168
+ order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
169
+ });
170
+
171
+ // src/tools/paginate.ts
160
172
  var paginate = async (repository, options) => {
161
- let where = parse(options.filters);
162
- if (where) where = refactorWhere(where);
173
+ var _a;
174
+ const params = await OptionsSchema.parseAsync(options);
163
175
  const [data, count] = await repository.findAndCount({
164
- select: options.fields,
165
- relations: options.relations,
166
- where,
167
- order: parse(options.order),
168
- take: options.limit,
169
- skip: Number(options.page) * (Number(options.limit) || 0) || 0
176
+ skip: Number(params.page) * (Number(params.take) || 0) || 0,
177
+ ...params
170
178
  });
171
179
  return {
172
- totalPage: Math.ceil(count / options.limit),
180
+ totalPage: options.take ? Math.ceil(count / options.take) : (_a = options.take) != null ? _a : 1,
173
181
  total: count,
174
182
  content: data
175
183
  };
@@ -60,9 +60,97 @@ var responseWrapper = async (res, promise) => {
60
60
  // src/tools/operators.ts
61
61
  var import_typeorm = require("typeorm");
62
62
  var import_lodash2 = __toESM(require("lodash"));
63
+ var OPERATORS = {
64
+ $in: (value) => (0, import_typeorm.In)(value),
65
+ $and: (value) => (0, import_typeorm.And)(value),
66
+ $notIn: (value) => (0, import_typeorm.Not)((0, import_typeorm.In)(value)),
67
+ $like: (value) => (0, import_typeorm.Like)(value),
68
+ $notLike: (value) => (0, import_typeorm.Not)((0, import_typeorm.Like)(value)),
69
+ $gt: (value) => (0, import_typeorm.MoreThan)(value),
70
+ $gte: (value) => (0, import_typeorm.MoreThanOrEqual)(value),
71
+ $lt: (value) => (0, import_typeorm.LessThan)(value),
72
+ $lte: (value) => (0, import_typeorm.LessThanOrEqual)(value),
73
+ $between: ([from, to]) => (0, import_typeorm.Between)(from, to),
74
+ $not: (value) => (0, import_typeorm.Not)(value),
75
+ $ne: (value) => (0, import_typeorm.Not)(value),
76
+ $raw: (value) => (0, import_typeorm.Raw)((alias) => import_lodash2.default.replace(value, "ALIAS", alias))
77
+ };
78
+ var existsOperator = (value) => {
79
+ let includeOp = false;
80
+ for (const op in OPERATORS) {
81
+ if (includeOp) continue;
82
+ includeOp = import_lodash2.default.includes(JSON.stringify(value), op);
83
+ }
84
+ return includeOp;
85
+ };
86
+ var converter = (operator) => {
87
+ operator = import_lodash2.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
88
+ let convertion = operator;
89
+ for (const op in OPERATORS) {
90
+ if (!operator[op] && operator[op] !== 0) continue;
91
+ if (import_lodash2.default.isObject(operator[op]) && import_lodash2.default.isArray(operator[op])) {
92
+ import_lodash2.default.forEach(operator[op], (condition, index) => {
93
+ if (!existsOperator(condition)) return;
94
+ for (const opSecondary in OPERATORS) {
95
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
96
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
97
+ }
98
+ });
99
+ }
100
+ convertion = OPERATORS[op](operator[op]);
101
+ }
102
+ return convertion;
103
+ };
104
+ var refactorWhere = (where) => {
105
+ if (!where) return where;
106
+ for (const key in where) {
107
+ let includeOp = existsOperator(where[key]);
108
+ if (includeOp) {
109
+ where[key] = converter(where[key]);
110
+ if (import_lodash2.default.isObject(where[key]) && !import_lodash2.default.isArray(where[key])) {
111
+ for (const keyObject in where[key]) {
112
+ includeOp = existsOperator(where[key][keyObject]);
113
+ if (!includeOp) continue;
114
+ where[key][keyObject] = converter(where[key][keyObject]);
115
+ }
116
+ }
117
+ if (import_lodash2.default.isObject(where[key]) && import_lodash2.default.isArray(where[key])) {
118
+ import_lodash2.default.forEach(where[key], (condition, index) => {
119
+ includeOp = existsOperator(condition);
120
+ if (includeOp) {
121
+ condition = converter(condition);
122
+ if (import_lodash2.default.isObject(condition) && !import_lodash2.default.isArray(condition)) {
123
+ for (const keyObject in condition) {
124
+ includeOp = existsOperator(condition[keyObject]);
125
+ if (!includeOp) continue;
126
+ condition[keyObject] = converter(condition[keyObject]);
127
+ }
128
+ }
129
+ }
130
+ where[key][index] = condition;
131
+ });
132
+ }
133
+ }
134
+ }
135
+ return where;
136
+ };
63
137
 
64
- // src/tools/paginate.ts
138
+ // src/schemas/options.ts
139
+ var import_zod2 = require("zod");
65
140
  var import_lodash3 = __toESM(require("lodash"));
141
+ var parse = (json) => {
142
+ if (!json) return;
143
+ if (import_lodash3.default.isString(json)) return JSON.parse(json);
144
+ return json;
145
+ };
146
+ var OptionsSchema = import_zod2.z.object({
147
+ take: import_zod2.z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
148
+ page: import_zod2.z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
149
+ select: import_zod2.z.array(import_zod2.z.string()).optional(),
150
+ relations: import_zod2.z.array(import_zod2.z.string()).optional(),
151
+ where: import_zod2.z.any().transform((value) => refactorWhere(parse(value))).optional(),
152
+ order: import_zod2.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
153
+ });
66
154
 
67
155
  // src/router/health.ts
68
156
  var healthRouter = (router, options, done) => {
@@ -34,9 +34,97 @@ import {
34
34
  LessThanOrEqual
35
35
  } from "typeorm";
36
36
  import _2 from "lodash";
37
+ var OPERATORS = {
38
+ $in: (value) => In(value),
39
+ $and: (value) => And(value),
40
+ $notIn: (value) => Not(In(value)),
41
+ $like: (value) => Like(value),
42
+ $notLike: (value) => Not(Like(value)),
43
+ $gt: (value) => MoreThan(value),
44
+ $gte: (value) => MoreThanOrEqual(value),
45
+ $lt: (value) => LessThan(value),
46
+ $lte: (value) => LessThanOrEqual(value),
47
+ $between: ([from, to]) => Between(from, to),
48
+ $not: (value) => Not(value),
49
+ $ne: (value) => Not(value),
50
+ $raw: (value) => Raw((alias) => _2.replace(value, "ALIAS", alias))
51
+ };
52
+ var existsOperator = (value) => {
53
+ let includeOp = false;
54
+ for (const op in OPERATORS) {
55
+ if (includeOp) continue;
56
+ includeOp = _2.includes(JSON.stringify(value), op);
57
+ }
58
+ return includeOp;
59
+ };
60
+ var converter = (operator) => {
61
+ operator = _2.isObject(operator) ? operator : JSON.parse(operator || "{}");
62
+ let convertion = operator;
63
+ for (const op in OPERATORS) {
64
+ if (!operator[op] && operator[op] !== 0) continue;
65
+ if (_2.isObject(operator[op]) && _2.isArray(operator[op])) {
66
+ _2.forEach(operator[op], (condition, index) => {
67
+ if (!existsOperator(condition)) return;
68
+ for (const opSecondary in OPERATORS) {
69
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
70
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
71
+ }
72
+ });
73
+ }
74
+ convertion = OPERATORS[op](operator[op]);
75
+ }
76
+ return convertion;
77
+ };
78
+ var refactorWhere = (where) => {
79
+ if (!where) return where;
80
+ for (const key in where) {
81
+ let includeOp = existsOperator(where[key]);
82
+ if (includeOp) {
83
+ where[key] = converter(where[key]);
84
+ if (_2.isObject(where[key]) && !_2.isArray(where[key])) {
85
+ for (const keyObject in where[key]) {
86
+ includeOp = existsOperator(where[key][keyObject]);
87
+ if (!includeOp) continue;
88
+ where[key][keyObject] = converter(where[key][keyObject]);
89
+ }
90
+ }
91
+ if (_2.isObject(where[key]) && _2.isArray(where[key])) {
92
+ _2.forEach(where[key], (condition, index) => {
93
+ includeOp = existsOperator(condition);
94
+ if (includeOp) {
95
+ condition = converter(condition);
96
+ if (_2.isObject(condition) && !_2.isArray(condition)) {
97
+ for (const keyObject in condition) {
98
+ includeOp = existsOperator(condition[keyObject]);
99
+ if (!includeOp) continue;
100
+ condition[keyObject] = converter(condition[keyObject]);
101
+ }
102
+ }
103
+ }
104
+ where[key][index] = condition;
105
+ });
106
+ }
107
+ }
108
+ }
109
+ return where;
110
+ };
37
111
 
38
- // src/tools/paginate.ts
112
+ // src/schemas/options.ts
113
+ import { z } from "zod";
39
114
  import _3 from "lodash";
115
+ var parse = (json) => {
116
+ if (!json) return;
117
+ if (_3.isString(json)) return JSON.parse(json);
118
+ return json;
119
+ };
120
+ var OptionsSchema = z.object({
121
+ take: z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
122
+ page: z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
123
+ select: z.array(z.string()).optional(),
124
+ relations: z.array(z.string()).optional(),
125
+ where: z.any().transform((value) => refactorWhere(parse(value))).optional(),
126
+ order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
127
+ });
40
128
 
41
129
  // src/router/health.ts
42
130
  var healthRouter = (router, options, done) => {
@@ -0,0 +1,12 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const OptionsSchema: z.ZodObject<{
4
+ take: z.ZodPipe<z.ZodDefault<z.ZodString>, z.ZodTransform<number | undefined, string>>;
5
+ page: z.ZodPipe<z.ZodDefault<z.ZodString>, z.ZodTransform<number, string>>;
6
+ select: z.ZodOptional<z.ZodArray<z.ZodString>>;
7
+ relations: z.ZodOptional<z.ZodArray<z.ZodString>>;
8
+ where: z.ZodOptional<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>>;
9
+ order: z.ZodPipe<z.ZodDefault<z.ZodAny>, z.ZodTransform<any, any>>;
10
+ }, z.core.$strip>;
11
+
12
+ export { OptionsSchema };
@@ -0,0 +1,12 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const OptionsSchema: z.ZodObject<{
4
+ take: z.ZodPipe<z.ZodDefault<z.ZodString>, z.ZodTransform<number | undefined, string>>;
5
+ page: z.ZodPipe<z.ZodDefault<z.ZodString>, z.ZodTransform<number, string>>;
6
+ select: z.ZodOptional<z.ZodArray<z.ZodString>>;
7
+ relations: z.ZodOptional<z.ZodArray<z.ZodString>>;
8
+ where: z.ZodOptional<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>>;
9
+ order: z.ZodPipe<z.ZodDefault<z.ZodAny>, z.ZodTransform<any, any>>;
10
+ }, z.core.$strip>;
11
+
12
+ export { OptionsSchema };
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/schemas/index.ts
31
+ var schemas_exports = {};
32
+ __export(schemas_exports, {
33
+ OptionsSchema: () => OptionsSchema
34
+ });
35
+ module.exports = __toCommonJS(schemas_exports);
36
+
37
+ // src/tools/operators.ts
38
+ var import_typeorm = require("typeorm");
39
+ var import_lodash = __toESM(require("lodash"));
40
+ var OPERATORS = {
41
+ $in: (value) => (0, import_typeorm.In)(value),
42
+ $and: (value) => (0, import_typeorm.And)(value),
43
+ $notIn: (value) => (0, import_typeorm.Not)((0, import_typeorm.In)(value)),
44
+ $like: (value) => (0, import_typeorm.Like)(value),
45
+ $notLike: (value) => (0, import_typeorm.Not)((0, import_typeorm.Like)(value)),
46
+ $gt: (value) => (0, import_typeorm.MoreThan)(value),
47
+ $gte: (value) => (0, import_typeorm.MoreThanOrEqual)(value),
48
+ $lt: (value) => (0, import_typeorm.LessThan)(value),
49
+ $lte: (value) => (0, import_typeorm.LessThanOrEqual)(value),
50
+ $between: ([from, to]) => (0, import_typeorm.Between)(from, to),
51
+ $not: (value) => (0, import_typeorm.Not)(value),
52
+ $ne: (value) => (0, import_typeorm.Not)(value),
53
+ $raw: (value) => (0, import_typeorm.Raw)((alias) => import_lodash.default.replace(value, "ALIAS", alias))
54
+ };
55
+ var existsOperator = (value) => {
56
+ let includeOp = false;
57
+ for (const op in OPERATORS) {
58
+ if (includeOp) continue;
59
+ includeOp = import_lodash.default.includes(JSON.stringify(value), op);
60
+ }
61
+ return includeOp;
62
+ };
63
+ var converter = (operator) => {
64
+ operator = import_lodash.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
65
+ let convertion = operator;
66
+ for (const op in OPERATORS) {
67
+ if (!operator[op] && operator[op] !== 0) continue;
68
+ if (import_lodash.default.isObject(operator[op]) && import_lodash.default.isArray(operator[op])) {
69
+ import_lodash.default.forEach(operator[op], (condition, index) => {
70
+ if (!existsOperator(condition)) return;
71
+ for (const opSecondary in OPERATORS) {
72
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
73
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
74
+ }
75
+ });
76
+ }
77
+ convertion = OPERATORS[op](operator[op]);
78
+ }
79
+ return convertion;
80
+ };
81
+ var refactorWhere = (where) => {
82
+ if (!where) return where;
83
+ for (const key in where) {
84
+ let includeOp = existsOperator(where[key]);
85
+ if (includeOp) {
86
+ where[key] = converter(where[key]);
87
+ if (import_lodash.default.isObject(where[key]) && !import_lodash.default.isArray(where[key])) {
88
+ for (const keyObject in where[key]) {
89
+ includeOp = existsOperator(where[key][keyObject]);
90
+ if (!includeOp) continue;
91
+ where[key][keyObject] = converter(where[key][keyObject]);
92
+ }
93
+ }
94
+ if (import_lodash.default.isObject(where[key]) && import_lodash.default.isArray(where[key])) {
95
+ import_lodash.default.forEach(where[key], (condition, index) => {
96
+ includeOp = existsOperator(condition);
97
+ if (includeOp) {
98
+ condition = converter(condition);
99
+ if (import_lodash.default.isObject(condition) && !import_lodash.default.isArray(condition)) {
100
+ for (const keyObject in condition) {
101
+ includeOp = existsOperator(condition[keyObject]);
102
+ if (!includeOp) continue;
103
+ condition[keyObject] = converter(condition[keyObject]);
104
+ }
105
+ }
106
+ }
107
+ where[key][index] = condition;
108
+ });
109
+ }
110
+ }
111
+ }
112
+ return where;
113
+ };
114
+
115
+ // src/schemas/options.ts
116
+ var import_zod = require("zod");
117
+ var import_lodash2 = __toESM(require("lodash"));
118
+ var parse = (json) => {
119
+ if (!json) return;
120
+ if (import_lodash2.default.isString(json)) return JSON.parse(json);
121
+ return json;
122
+ };
123
+ var OptionsSchema = import_zod.z.object({
124
+ take: import_zod.z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
125
+ page: import_zod.z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
126
+ select: import_zod.z.array(import_zod.z.string()).optional(),
127
+ relations: import_zod.z.array(import_zod.z.string()).optional(),
128
+ where: import_zod.z.any().transform((value) => refactorWhere(parse(value))).optional(),
129
+ order: import_zod.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
130
+ });
131
+ // Annotate the CommonJS export names for ESM import in node:
132
+ 0 && (module.exports = {
133
+ OptionsSchema
134
+ });
@@ -0,0 +1,108 @@
1
+ // src/tools/operators.ts
2
+ import {
3
+ Like,
4
+ MoreThan,
5
+ MoreThanOrEqual,
6
+ In,
7
+ Not,
8
+ LessThan,
9
+ Between,
10
+ Raw,
11
+ And,
12
+ LessThanOrEqual
13
+ } from "typeorm";
14
+ import _ from "lodash";
15
+ var OPERATORS = {
16
+ $in: (value) => In(value),
17
+ $and: (value) => And(value),
18
+ $notIn: (value) => Not(In(value)),
19
+ $like: (value) => Like(value),
20
+ $notLike: (value) => Not(Like(value)),
21
+ $gt: (value) => MoreThan(value),
22
+ $gte: (value) => MoreThanOrEqual(value),
23
+ $lt: (value) => LessThan(value),
24
+ $lte: (value) => LessThanOrEqual(value),
25
+ $between: ([from, to]) => Between(from, to),
26
+ $not: (value) => Not(value),
27
+ $ne: (value) => Not(value),
28
+ $raw: (value) => Raw((alias) => _.replace(value, "ALIAS", alias))
29
+ };
30
+ var existsOperator = (value) => {
31
+ let includeOp = false;
32
+ for (const op in OPERATORS) {
33
+ if (includeOp) continue;
34
+ includeOp = _.includes(JSON.stringify(value), op);
35
+ }
36
+ return includeOp;
37
+ };
38
+ var converter = (operator) => {
39
+ operator = _.isObject(operator) ? operator : JSON.parse(operator || "{}");
40
+ let convertion = operator;
41
+ for (const op in OPERATORS) {
42
+ if (!operator[op] && operator[op] !== 0) continue;
43
+ if (_.isObject(operator[op]) && _.isArray(operator[op])) {
44
+ _.forEach(operator[op], (condition, index) => {
45
+ if (!existsOperator(condition)) return;
46
+ for (const opSecondary in OPERATORS) {
47
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
48
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
49
+ }
50
+ });
51
+ }
52
+ convertion = OPERATORS[op](operator[op]);
53
+ }
54
+ return convertion;
55
+ };
56
+ var refactorWhere = (where) => {
57
+ if (!where) return where;
58
+ for (const key in where) {
59
+ let includeOp = existsOperator(where[key]);
60
+ if (includeOp) {
61
+ where[key] = converter(where[key]);
62
+ if (_.isObject(where[key]) && !_.isArray(where[key])) {
63
+ for (const keyObject in where[key]) {
64
+ includeOp = existsOperator(where[key][keyObject]);
65
+ if (!includeOp) continue;
66
+ where[key][keyObject] = converter(where[key][keyObject]);
67
+ }
68
+ }
69
+ if (_.isObject(where[key]) && _.isArray(where[key])) {
70
+ _.forEach(where[key], (condition, index) => {
71
+ includeOp = existsOperator(condition);
72
+ if (includeOp) {
73
+ condition = converter(condition);
74
+ if (_.isObject(condition) && !_.isArray(condition)) {
75
+ for (const keyObject in condition) {
76
+ includeOp = existsOperator(condition[keyObject]);
77
+ if (!includeOp) continue;
78
+ condition[keyObject] = converter(condition[keyObject]);
79
+ }
80
+ }
81
+ }
82
+ where[key][index] = condition;
83
+ });
84
+ }
85
+ }
86
+ }
87
+ return where;
88
+ };
89
+
90
+ // src/schemas/options.ts
91
+ import { z } from "zod";
92
+ import _2 from "lodash";
93
+ var parse = (json) => {
94
+ if (!json) return;
95
+ if (_2.isString(json)) return JSON.parse(json);
96
+ return json;
97
+ };
98
+ var OptionsSchema = z.object({
99
+ take: z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
100
+ page: z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
101
+ select: z.array(z.string()).optional(),
102
+ relations: z.array(z.string()).optional(),
103
+ where: z.any().transform((value) => refactorWhere(parse(value))).optional(),
104
+ order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
105
+ });
106
+ export {
107
+ OptionsSchema
108
+ };
@@ -61,9 +61,97 @@ var responseWrapper = async (res, promise) => {
61
61
  // src/tools/operators.ts
62
62
  var import_typeorm = require("typeorm");
63
63
  var import_lodash2 = __toESM(require("lodash"));
64
+ var OPERATORS = {
65
+ $in: (value) => (0, import_typeorm.In)(value),
66
+ $and: (value) => (0, import_typeorm.And)(value),
67
+ $notIn: (value) => (0, import_typeorm.Not)((0, import_typeorm.In)(value)),
68
+ $like: (value) => (0, import_typeorm.Like)(value),
69
+ $notLike: (value) => (0, import_typeorm.Not)((0, import_typeorm.Like)(value)),
70
+ $gt: (value) => (0, import_typeorm.MoreThan)(value),
71
+ $gte: (value) => (0, import_typeorm.MoreThanOrEqual)(value),
72
+ $lt: (value) => (0, import_typeorm.LessThan)(value),
73
+ $lte: (value) => (0, import_typeorm.LessThanOrEqual)(value),
74
+ $between: ([from, to]) => (0, import_typeorm.Between)(from, to),
75
+ $not: (value) => (0, import_typeorm.Not)(value),
76
+ $ne: (value) => (0, import_typeorm.Not)(value),
77
+ $raw: (value) => (0, import_typeorm.Raw)((alias) => import_lodash2.default.replace(value, "ALIAS", alias))
78
+ };
79
+ var existsOperator = (value) => {
80
+ let includeOp = false;
81
+ for (const op in OPERATORS) {
82
+ if (includeOp) continue;
83
+ includeOp = import_lodash2.default.includes(JSON.stringify(value), op);
84
+ }
85
+ return includeOp;
86
+ };
87
+ var converter = (operator) => {
88
+ operator = import_lodash2.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
89
+ let convertion = operator;
90
+ for (const op in OPERATORS) {
91
+ if (!operator[op] && operator[op] !== 0) continue;
92
+ if (import_lodash2.default.isObject(operator[op]) && import_lodash2.default.isArray(operator[op])) {
93
+ import_lodash2.default.forEach(operator[op], (condition, index) => {
94
+ if (!existsOperator(condition)) return;
95
+ for (const opSecondary in OPERATORS) {
96
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
97
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
98
+ }
99
+ });
100
+ }
101
+ convertion = OPERATORS[op](operator[op]);
102
+ }
103
+ return convertion;
104
+ };
105
+ var refactorWhere = (where) => {
106
+ if (!where) return where;
107
+ for (const key in where) {
108
+ let includeOp = existsOperator(where[key]);
109
+ if (includeOp) {
110
+ where[key] = converter(where[key]);
111
+ if (import_lodash2.default.isObject(where[key]) && !import_lodash2.default.isArray(where[key])) {
112
+ for (const keyObject in where[key]) {
113
+ includeOp = existsOperator(where[key][keyObject]);
114
+ if (!includeOp) continue;
115
+ where[key][keyObject] = converter(where[key][keyObject]);
116
+ }
117
+ }
118
+ if (import_lodash2.default.isObject(where[key]) && import_lodash2.default.isArray(where[key])) {
119
+ import_lodash2.default.forEach(where[key], (condition, index) => {
120
+ includeOp = existsOperator(condition);
121
+ if (includeOp) {
122
+ condition = converter(condition);
123
+ if (import_lodash2.default.isObject(condition) && !import_lodash2.default.isArray(condition)) {
124
+ for (const keyObject in condition) {
125
+ includeOp = existsOperator(condition[keyObject]);
126
+ if (!includeOp) continue;
127
+ condition[keyObject] = converter(condition[keyObject]);
128
+ }
129
+ }
130
+ }
131
+ where[key][index] = condition;
132
+ });
133
+ }
134
+ }
135
+ }
136
+ return where;
137
+ };
64
138
 
65
- // src/tools/paginate.ts
139
+ // src/schemas/options.ts
140
+ var import_zod2 = require("zod");
66
141
  var import_lodash3 = __toESM(require("lodash"));
142
+ var parse = (json) => {
143
+ if (!json) return;
144
+ if (import_lodash3.default.isString(json)) return JSON.parse(json);
145
+ return json;
146
+ };
147
+ var OptionsSchema = import_zod2.z.object({
148
+ take: import_zod2.z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
149
+ page: import_zod2.z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
150
+ select: import_zod2.z.array(import_zod2.z.string()).optional(),
151
+ relations: import_zod2.z.array(import_zod2.z.string()).optional(),
152
+ where: import_zod2.z.any().transform((value) => refactorWhere(parse(value))).optional(),
153
+ order: import_zod2.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
154
+ });
67
155
 
68
156
  // src/router/health.ts
69
157
  var healthRouter = (router, options, done) => {
@@ -37,9 +37,97 @@ import {
37
37
  LessThanOrEqual
38
38
  } from "typeorm";
39
39
  import _2 from "lodash";
40
+ var OPERATORS = {
41
+ $in: (value) => In(value),
42
+ $and: (value) => And(value),
43
+ $notIn: (value) => Not(In(value)),
44
+ $like: (value) => Like(value),
45
+ $notLike: (value) => Not(Like(value)),
46
+ $gt: (value) => MoreThan(value),
47
+ $gte: (value) => MoreThanOrEqual(value),
48
+ $lt: (value) => LessThan(value),
49
+ $lte: (value) => LessThanOrEqual(value),
50
+ $between: ([from, to]) => Between(from, to),
51
+ $not: (value) => Not(value),
52
+ $ne: (value) => Not(value),
53
+ $raw: (value) => Raw((alias) => _2.replace(value, "ALIAS", alias))
54
+ };
55
+ var existsOperator = (value) => {
56
+ let includeOp = false;
57
+ for (const op in OPERATORS) {
58
+ if (includeOp) continue;
59
+ includeOp = _2.includes(JSON.stringify(value), op);
60
+ }
61
+ return includeOp;
62
+ };
63
+ var converter = (operator) => {
64
+ operator = _2.isObject(operator) ? operator : JSON.parse(operator || "{}");
65
+ let convertion = operator;
66
+ for (const op in OPERATORS) {
67
+ if (!operator[op] && operator[op] !== 0) continue;
68
+ if (_2.isObject(operator[op]) && _2.isArray(operator[op])) {
69
+ _2.forEach(operator[op], (condition, index) => {
70
+ if (!existsOperator(condition)) return;
71
+ for (const opSecondary in OPERATORS) {
72
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
73
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
74
+ }
75
+ });
76
+ }
77
+ convertion = OPERATORS[op](operator[op]);
78
+ }
79
+ return convertion;
80
+ };
81
+ var refactorWhere = (where) => {
82
+ if (!where) return where;
83
+ for (const key in where) {
84
+ let includeOp = existsOperator(where[key]);
85
+ if (includeOp) {
86
+ where[key] = converter(where[key]);
87
+ if (_2.isObject(where[key]) && !_2.isArray(where[key])) {
88
+ for (const keyObject in where[key]) {
89
+ includeOp = existsOperator(where[key][keyObject]);
90
+ if (!includeOp) continue;
91
+ where[key][keyObject] = converter(where[key][keyObject]);
92
+ }
93
+ }
94
+ if (_2.isObject(where[key]) && _2.isArray(where[key])) {
95
+ _2.forEach(where[key], (condition, index) => {
96
+ includeOp = existsOperator(condition);
97
+ if (includeOp) {
98
+ condition = converter(condition);
99
+ if (_2.isObject(condition) && !_2.isArray(condition)) {
100
+ for (const keyObject in condition) {
101
+ includeOp = existsOperator(condition[keyObject]);
102
+ if (!includeOp) continue;
103
+ condition[keyObject] = converter(condition[keyObject]);
104
+ }
105
+ }
106
+ }
107
+ where[key][index] = condition;
108
+ });
109
+ }
110
+ }
111
+ }
112
+ return where;
113
+ };
40
114
 
41
- // src/tools/paginate.ts
115
+ // src/schemas/options.ts
116
+ import { z } from "zod";
42
117
  import _3 from "lodash";
118
+ var parse = (json) => {
119
+ if (!json) return;
120
+ if (_3.isString(json)) return JSON.parse(json);
121
+ return json;
122
+ };
123
+ var OptionsSchema = z.object({
124
+ take: z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
125
+ page: z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
126
+ select: z.array(z.string()).optional(),
127
+ relations: z.array(z.string()).optional(),
128
+ where: z.any().transform((value) => refactorWhere(parse(value))).optional(),
129
+ order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
130
+ });
43
131
 
44
132
  // src/router/health.ts
45
133
  var healthRouter = (router, options, done) => {
@@ -1,4 +1,6 @@
1
1
  import { FastifyReply } from 'fastify';
2
+ import { OptionsSchema } from '../schemas/index.mjs';
3
+ import { z } from 'zod';
2
4
 
3
5
  declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
4
6
 
@@ -7,18 +9,7 @@ declare const existsOperator: (value: any) => boolean;
7
9
  declare const converter: (operator: any) => any;
8
10
  declare const refactorWhere: (where: any) => any;
9
11
 
10
- declare const paginate: (repository: any, options: {
11
- fields: Array<string>;
12
- relations: Array<string>;
13
- filters: string | {
14
- [field: string]: any;
15
- };
16
- order: string | {
17
- [field: string]: "asc" | "desc";
18
- };
19
- limit: number;
20
- page: number;
21
- }) => Promise<{
12
+ declare const paginate: (repository: any, options: z.infer<typeof OptionsSchema>) => Promise<{
22
13
  totalPage: number;
23
14
  total: any;
24
15
  content: any;
@@ -1,4 +1,6 @@
1
1
  import { FastifyReply } from 'fastify';
2
+ import { OptionsSchema } from '../schemas/index.js';
3
+ import { z } from 'zod';
2
4
 
3
5
  declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
4
6
 
@@ -7,18 +9,7 @@ declare const existsOperator: (value: any) => boolean;
7
9
  declare const converter: (operator: any) => any;
8
10
  declare const refactorWhere: (where: any) => any;
9
11
 
10
- declare const paginate: (repository: any, options: {
11
- fields: Array<string>;
12
- relations: Array<string>;
13
- filters: string | {
14
- [field: string]: any;
15
- };
16
- order: string | {
17
- [field: string]: "asc" | "desc";
18
- };
19
- limit: number;
20
- page: number;
21
- }) => Promise<{
12
+ declare const paginate: (repository: any, options: z.infer<typeof OptionsSchema>) => Promise<{
22
13
  totalPage: number;
23
14
  total: any;
24
15
  content: any;
@@ -107,6 +107,7 @@ var converter = (operator) => {
107
107
  return convertion;
108
108
  };
109
109
  var refactorWhere = (where) => {
110
+ if (!where) return where;
110
111
  for (const key in where) {
111
112
  let includeOp = existsOperator(where[key]);
112
113
  if (includeOp) {
@@ -139,26 +140,33 @@ var refactorWhere = (where) => {
139
140
  return where;
140
141
  };
141
142
 
142
- // src/tools/paginate.ts
143
+ // src/schemas/options.ts
144
+ var import_zod2 = require("zod");
143
145
  var import_lodash3 = __toESM(require("lodash"));
144
146
  var parse = (json) => {
145
147
  if (!json) return;
146
148
  if (import_lodash3.default.isString(json)) return JSON.parse(json);
147
149
  return json;
148
150
  };
151
+ var OptionsSchema = import_zod2.z.object({
152
+ take: import_zod2.z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
153
+ page: import_zod2.z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
154
+ select: import_zod2.z.array(import_zod2.z.string()).optional(),
155
+ relations: import_zod2.z.array(import_zod2.z.string()).optional(),
156
+ where: import_zod2.z.any().transform((value) => refactorWhere(parse(value))).optional(),
157
+ order: import_zod2.z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
158
+ });
159
+
160
+ // src/tools/paginate.ts
149
161
  var paginate = async (repository, options) => {
150
- let where = parse(options.filters);
151
- if (where) where = refactorWhere(where);
162
+ var _a;
163
+ const params = await OptionsSchema.parseAsync(options);
152
164
  const [data, count] = await repository.findAndCount({
153
- select: options.fields,
154
- relations: options.relations,
155
- where,
156
- order: parse(options.order),
157
- take: options.limit,
158
- skip: Number(options.page) * (Number(options.limit) || 0) || 0
165
+ skip: Number(params.page) * (Number(params.take) || 0) || 0,
166
+ ...params
159
167
  });
160
168
  return {
161
- totalPage: Math.ceil(count / options.limit),
169
+ totalPage: options.take ? Math.ceil(count / options.take) : (_a = options.take) != null ? _a : 1,
162
170
  total: count,
163
171
  content: data
164
172
  };
@@ -83,6 +83,7 @@ var converter = (operator) => {
83
83
  return convertion;
84
84
  };
85
85
  var refactorWhere = (where) => {
86
+ if (!where) return where;
86
87
  for (const key in where) {
87
88
  let includeOp = existsOperator(where[key]);
88
89
  if (includeOp) {
@@ -115,26 +116,33 @@ var refactorWhere = (where) => {
115
116
  return where;
116
117
  };
117
118
 
118
- // src/tools/paginate.ts
119
+ // src/schemas/options.ts
120
+ import { z } from "zod";
119
121
  import _3 from "lodash";
120
122
  var parse = (json) => {
121
123
  if (!json) return;
122
124
  if (_3.isString(json)) return JSON.parse(json);
123
125
  return json;
124
126
  };
127
+ var OptionsSchema = z.object({
128
+ take: z.string().default("20").transform((value) => Number(value) ? Number(value) : value === "infinity" ? void 0 : 20),
129
+ page: z.string().default("0").transform((value) => Number(value) ? Number(value) : 0),
130
+ select: z.array(z.string()).optional(),
131
+ relations: z.array(z.string()).optional(),
132
+ where: z.any().transform((value) => refactorWhere(parse(value))).optional(),
133
+ order: z.any().default({ createdAt: "desc" }).transform((value) => parse(value))
134
+ });
135
+
136
+ // src/tools/paginate.ts
125
137
  var paginate = async (repository, options) => {
126
- let where = parse(options.filters);
127
- if (where) where = refactorWhere(where);
138
+ var _a;
139
+ const params = await OptionsSchema.parseAsync(options);
128
140
  const [data, count] = await repository.findAndCount({
129
- select: options.fields,
130
- relations: options.relations,
131
- where,
132
- order: parse(options.order),
133
- take: options.limit,
134
- skip: Number(options.page) * (Number(options.limit) || 0) || 0
141
+ skip: Number(params.page) * (Number(params.take) || 0) || 0,
142
+ ...params
135
143
  });
136
144
  return {
137
- totalPage: Math.ceil(count / options.limit),
145
+ totalPage: options.take ? Math.ceil(count / options.take) : (_a = options.take) != null ? _a : 1,
138
146
  total: count,
139
147
  content: data
140
148
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gustavo-valsechi/node",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -67,6 +67,11 @@
67
67
  "require": "./dist/src/router/index.js",
68
68
  "import": "./dist/src/router/index.js"
69
69
  },
70
+ "./schemas": {
71
+ "types": "./dist/src/schemas/index.d.ts",
72
+ "require": "./dist/src/schemas/index.js",
73
+ "import": "./dist/src/schemas/index.js"
74
+ },
70
75
  "./server": {
71
76
  "types": "./dist/src/server/index.d.ts",
72
77
  "require": "./dist/src/server/index.js",