@drax/crud-back 0.11.5 → 0.12.2
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/builders/CrudSchemaBuilder.js +349 -0
- package/dist/controllers/AbstractFastifyController.js +105 -176
- package/dist/index.js +15 -1
- package/dist/regexs/QueryFilterRegex.js +3 -0
- package/dist/repository/AbstractMongoRepository.js +114 -28
- package/dist/repository/AbstractSqliteRepository.js +131 -41
- package/dist/schemas/DeleteBodyResponseSchema.js +9 -0
- package/dist/schemas/ErrorBodyResponseSchema.js +16 -0
- package/dist/schemas/ExportBodyResponseSchema.js +9 -0
- package/dist/schemas/FindBySchema.js +6 -0
- package/dist/schemas/FindSchema.js +9 -0
- package/dist/schemas/IdParamSchema.js +6 -0
- package/dist/schemas/PaginateBodySchema.js +20 -0
- package/dist/schemas/PaginateSchema.js +17 -0
- package/dist/schemas/SearchSchema.js +5 -0
- package/dist/services/AbstractService.js +8 -5
- package/dist/zod/DeleteBodySchema.js +6 -0
- package/dist/zod/IdParamSchema.js +6 -0
- package/dist/zod/PaginateBodySchema.js +20 -0
- package/package.json +7 -6
- package/src/builders/CrudSchemaBuilder.ts +420 -0
- package/src/controllers/AbstractFastifyController.ts +149 -165
- package/src/index.ts +28 -0
- package/src/regexs/QueryFilterRegex.ts +4 -0
- package/src/repository/AbstractMongoRepository.ts +153 -40
- package/src/repository/AbstractSqliteRepository.ts +196 -72
- package/src/schemas/DeleteBodyResponseSchema.ts +12 -0
- package/src/schemas/ErrorBodyResponseSchema.ts +20 -0
- package/src/schemas/ExportBodyResponseSchema.ts +12 -0
- package/src/schemas/FindBySchema.ts +9 -0
- package/src/schemas/FindSchema.ts +13 -0
- package/src/schemas/IdParamSchema.ts +9 -0
- package/src/schemas/PaginateSchema.ts +21 -0
- package/src/schemas/SearchSchema.ts +8 -0
- package/src/services/AbstractService.ts +42 -33
- package/tsconfig.tsbuildinfo +1 -1
- package/types/builders/CrudSchemaBuilder.d.ts +2401 -0
- package/types/builders/CrudSchemaBuilder.d.ts.map +1 -0
- package/types/controllers/AbstractFastifyController.d.ts +6 -1
- package/types/controllers/AbstractFastifyController.d.ts.map +1 -1
- package/types/index.d.ts +10 -1
- package/types/index.d.ts.map +1 -1
- package/types/regexs/QueryFilterRegex.d.ts +4 -0
- package/types/regexs/QueryFilterRegex.d.ts.map +1 -0
- package/types/repository/AbstractMongoRepository.d.ts +6 -3
- package/types/repository/AbstractMongoRepository.d.ts.map +1 -1
- package/types/repository/AbstractSqliteRepository.d.ts +23 -8
- package/types/repository/AbstractSqliteRepository.d.ts.map +1 -1
- package/types/schemas/DeleteBodyResponseSchema.d.ts +20 -0
- package/types/schemas/DeleteBodyResponseSchema.d.ts.map +1 -0
- package/types/schemas/ErrorBodyResponseSchema.d.ts +60 -0
- package/types/schemas/ErrorBodyResponseSchema.d.ts.map +1 -0
- package/types/schemas/ExportBodyResponseSchema.d.ts +20 -0
- package/types/schemas/ExportBodyResponseSchema.d.ts.map +1 -0
- package/types/schemas/FindBySchema.d.ts +13 -0
- package/types/schemas/FindBySchema.d.ts.map +1 -0
- package/types/schemas/FindSchema.d.ts +19 -0
- package/types/schemas/FindSchema.d.ts.map +1 -0
- package/types/schemas/IdParamSchema.d.ts +11 -0
- package/types/schemas/IdParamSchema.d.ts.map +1 -0
- package/types/schemas/PaginateBodySchema.d.ts +61 -0
- package/types/schemas/PaginateBodySchema.d.ts.map +1 -0
- package/types/schemas/PaginateSchema.d.ts +41 -0
- package/types/schemas/PaginateSchema.d.ts.map +1 -0
- package/types/schemas/SearchSchema.d.ts +10 -0
- package/types/schemas/SearchSchema.d.ts.map +1 -0
- package/types/services/AbstractService.d.ts +5 -5
- package/types/services/AbstractService.d.ts.map +1 -1
- package/types/zod/DeleteBodySchema.d.ts +11 -0
- package/types/zod/DeleteBodySchema.d.ts.map +1 -0
- package/types/zod/IdParamSchema.d.ts +11 -0
- package/types/zod/IdParamSchema.d.ts.map +1 -0
- package/types/zod/PaginateBodySchema.d.ts +61 -0
- package/types/zod/PaginateBodySchema.d.ts.map +1 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { CommonConfig, DraxConfig, ValidationError } from "@drax/common-back";
|
|
1
|
+
import { CommonConfig, DraxConfig, ForbiddenError, InternalServerError, InvalidIdError, LimitError, NotFoundError, ValidationError, SecuritySensitiveError, UploadFileError, BadRequestError } from "@drax/common-back";
|
|
2
2
|
import { UnauthorizedError } from "@drax/common-back";
|
|
3
3
|
import { join } from "path";
|
|
4
|
+
import QueryFilterRegex from "../regexs/QueryFilterRegex.js";
|
|
4
5
|
const BASE_FILE_DIR = DraxConfig.getOrLoad(CommonConfig.FileDir) || 'files';
|
|
5
6
|
const BASE_URL = DraxConfig.getOrLoad(CommonConfig.BaseUrl) ? DraxConfig.get(CommonConfig.BaseUrl).replace(/\/$/, '') : '';
|
|
6
7
|
class AbstractFastifyController {
|
|
@@ -13,6 +14,8 @@ class AbstractFastifyController {
|
|
|
13
14
|
this.userSetter = false;
|
|
14
15
|
this.tenantAssert = false;
|
|
15
16
|
this.userAssert = false;
|
|
17
|
+
this.defaultLimit = 1000;
|
|
18
|
+
this.maximumLimit = 10000;
|
|
16
19
|
this.service = service;
|
|
17
20
|
this.permission = permission;
|
|
18
21
|
console.log("AbstractFastifyController created. Permissions", this.permission);
|
|
@@ -22,6 +25,9 @@ class AbstractFastifyController {
|
|
|
22
25
|
if (!stringFilters) {
|
|
23
26
|
return [];
|
|
24
27
|
}
|
|
28
|
+
if (!QueryFilterRegex.test(stringFilters)) {
|
|
29
|
+
throw new BadRequestError("Invalid filters format");
|
|
30
|
+
}
|
|
25
31
|
const filterArray = stringFilters.split("|");
|
|
26
32
|
const filters = [];
|
|
27
33
|
filterArray.forEach((filter) => {
|
|
@@ -31,7 +37,7 @@ class AbstractFastifyController {
|
|
|
31
37
|
return filters;
|
|
32
38
|
}
|
|
33
39
|
catch (e) {
|
|
34
|
-
console.error(e);
|
|
40
|
+
console.error("parseFilters error", e);
|
|
35
41
|
throw e;
|
|
36
42
|
}
|
|
37
43
|
}
|
|
@@ -43,6 +49,16 @@ class AbstractFastifyController {
|
|
|
43
49
|
filters.push({ field: this.userField, operator: 'eq', value: rbac.userId });
|
|
44
50
|
}
|
|
45
51
|
}
|
|
52
|
+
assertUserAndTenant(item, rbac) {
|
|
53
|
+
if (this.tenantAssert) {
|
|
54
|
+
const itemTenantId = item[this.tenantField] && item[this.tenantField]._id ? item[this.tenantField]._id : null;
|
|
55
|
+
rbac.assertTenantId(itemTenantId);
|
|
56
|
+
}
|
|
57
|
+
if (this.userAssert) {
|
|
58
|
+
const itemUserId = item[this.userField] && item[this.userField]._id ? item[this.userField]._id : null;
|
|
59
|
+
rbac.assertUserId(itemUserId);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
46
62
|
applyUserAndTenantSetters(payload, rbac) {
|
|
47
63
|
if (this.tenantSetter && rbac.tenantId) {
|
|
48
64
|
payload[this.tenantField] = rbac.tenantId;
|
|
@@ -51,6 +67,25 @@ class AbstractFastifyController {
|
|
|
51
67
|
payload[this.userField] = rbac.userId;
|
|
52
68
|
}
|
|
53
69
|
}
|
|
70
|
+
handleError(e, reply) {
|
|
71
|
+
console.error(e);
|
|
72
|
+
if (e instanceof ValidationError ||
|
|
73
|
+
e instanceof NotFoundError ||
|
|
74
|
+
e instanceof BadRequestError ||
|
|
75
|
+
e instanceof UnauthorizedError ||
|
|
76
|
+
e instanceof ForbiddenError ||
|
|
77
|
+
e instanceof InvalidIdError ||
|
|
78
|
+
e instanceof SecuritySensitiveError ||
|
|
79
|
+
e instanceof UploadFileError ||
|
|
80
|
+
e instanceof LimitError) {
|
|
81
|
+
reply.status(e.statusCode).send(e.body);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const serverError = new InternalServerError();
|
|
85
|
+
reply.statusCode = serverError.statusCode;
|
|
86
|
+
reply.status(500).send(serverError.body);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
54
89
|
async create(request, reply) {
|
|
55
90
|
try {
|
|
56
91
|
request.rbac.assertPermission(this.permission.Create);
|
|
@@ -60,19 +95,7 @@ class AbstractFastifyController {
|
|
|
60
95
|
return item;
|
|
61
96
|
}
|
|
62
97
|
catch (e) {
|
|
63
|
-
|
|
64
|
-
if (e instanceof ValidationError) {
|
|
65
|
-
reply.statusCode = e.statusCode;
|
|
66
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
67
|
-
}
|
|
68
|
-
else if (e instanceof UnauthorizedError) {
|
|
69
|
-
reply.statusCode = e.statusCode;
|
|
70
|
-
reply.send({ error: e.message });
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
reply.statusCode = 500;
|
|
74
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
75
|
-
}
|
|
98
|
+
this.handleError(e, reply);
|
|
76
99
|
}
|
|
77
100
|
}
|
|
78
101
|
async update(request, reply) {
|
|
@@ -86,22 +109,33 @@ class AbstractFastifyController {
|
|
|
86
109
|
const payload = request.body;
|
|
87
110
|
//this.applyUserAndTenantSetters(payload, request.rbac)
|
|
88
111
|
let item = await this.service.update(id, payload);
|
|
112
|
+
if (!item) {
|
|
113
|
+
throw new NotFoundError();
|
|
114
|
+
}
|
|
89
115
|
return item;
|
|
90
116
|
}
|
|
91
117
|
catch (e) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
reply.
|
|
118
|
+
this.handleError(e, reply);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async updatePartial(request, reply) {
|
|
122
|
+
try {
|
|
123
|
+
request.rbac.assertPermission(this.permission.Update);
|
|
124
|
+
if (!request.params.id) {
|
|
125
|
+
reply.statusCode = 400;
|
|
126
|
+
reply.send({ error: 'BAD REQUEST' });
|
|
100
127
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
128
|
+
const id = request.params.id;
|
|
129
|
+
const payload = request.body;
|
|
130
|
+
//this.applyUserAndTenantSetters(payload, request.rbac)
|
|
131
|
+
let item = await this.service.updatePartial(id, payload);
|
|
132
|
+
if (!item) {
|
|
133
|
+
throw new NotFoundError();
|
|
104
134
|
}
|
|
135
|
+
return item;
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
this.handleError(e, reply);
|
|
105
139
|
}
|
|
106
140
|
}
|
|
107
141
|
async delete(request, reply) {
|
|
@@ -118,25 +152,19 @@ class AbstractFastifyController {
|
|
|
118
152
|
reply.send({ error: 'NOT_FOUND' });
|
|
119
153
|
}
|
|
120
154
|
if (this.tenantAssert) {
|
|
121
|
-
|
|
155
|
+
const tenantId = item[this.tenantField] && item[this.tenantField]._id ? item[this.tenantField]._id : null;
|
|
156
|
+
request.rbac.assertTenantId(tenantId);
|
|
122
157
|
}
|
|
123
158
|
await this.service.delete(id);
|
|
124
|
-
reply.send({
|
|
159
|
+
reply.send({
|
|
160
|
+
id: id,
|
|
161
|
+
message: 'Item deleted successfully',
|
|
162
|
+
deleted: true,
|
|
163
|
+
deletedAt: new Date(),
|
|
164
|
+
});
|
|
125
165
|
}
|
|
126
166
|
catch (e) {
|
|
127
|
-
|
|
128
|
-
if (e instanceof ValidationError) {
|
|
129
|
-
reply.statusCode = e.statusCode;
|
|
130
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
131
|
-
}
|
|
132
|
-
else if (e instanceof UnauthorizedError) {
|
|
133
|
-
reply.statusCode = e.statusCode;
|
|
134
|
-
reply.send({ error: e.message });
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
reply.statusCode = 500;
|
|
138
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
139
|
-
}
|
|
167
|
+
this.handleError(e, reply);
|
|
140
168
|
}
|
|
141
169
|
}
|
|
142
170
|
async findById(request, reply) {
|
|
@@ -148,25 +176,17 @@ class AbstractFastifyController {
|
|
|
148
176
|
}
|
|
149
177
|
const id = request.params.id;
|
|
150
178
|
let item = await this.service.findById(id);
|
|
179
|
+
if (!item) {
|
|
180
|
+
throw new NotFoundError();
|
|
181
|
+
}
|
|
151
182
|
if (this.tenantAssert) {
|
|
152
|
-
|
|
183
|
+
const itemTenantId = item[this.tenantField] && item[this.tenantField]._id ? item[this.tenantField]._id : null;
|
|
184
|
+
request.rbac.assertTenantId(itemTenantId);
|
|
153
185
|
}
|
|
154
186
|
return item;
|
|
155
187
|
}
|
|
156
188
|
catch (e) {
|
|
157
|
-
|
|
158
|
-
if (e instanceof ValidationError) {
|
|
159
|
-
reply.statusCode = e.statusCode;
|
|
160
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
161
|
-
}
|
|
162
|
-
else if (e instanceof UnauthorizedError) {
|
|
163
|
-
reply.statusCode = e.statusCode;
|
|
164
|
-
reply.send({ error: e.message });
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
reply.statusCode = 500;
|
|
168
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
169
|
-
}
|
|
189
|
+
this.handleError(e, reply);
|
|
170
190
|
}
|
|
171
191
|
}
|
|
172
192
|
async findByIds(request, reply) {
|
|
@@ -178,51 +198,33 @@ class AbstractFastifyController {
|
|
|
178
198
|
}
|
|
179
199
|
const ids = request.params.ids.split(",");
|
|
180
200
|
let items = await this.service.findByIds(ids);
|
|
201
|
+
if (!items || items.length === 0) {
|
|
202
|
+
throw new NotFoundError();
|
|
203
|
+
}
|
|
181
204
|
return items;
|
|
182
205
|
}
|
|
183
206
|
catch (e) {
|
|
184
|
-
|
|
185
|
-
if (e instanceof ValidationError) {
|
|
186
|
-
reply.statusCode = e.statusCode;
|
|
187
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
188
|
-
}
|
|
189
|
-
else if (e instanceof UnauthorizedError) {
|
|
190
|
-
reply.statusCode = e.statusCode;
|
|
191
|
-
reply.send({ error: e.message });
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
reply.statusCode = 500;
|
|
195
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
196
|
-
}
|
|
207
|
+
this.handleError(e, reply);
|
|
197
208
|
}
|
|
198
209
|
}
|
|
199
210
|
async find(request, reply) {
|
|
200
211
|
var _a;
|
|
201
212
|
try {
|
|
202
213
|
request.rbac.assertPermission(this.permission.View);
|
|
214
|
+
if (request.query.limit > this.maximumLimit) {
|
|
215
|
+
throw new LimitError(this.maximumLimit, request.query.limit);
|
|
216
|
+
}
|
|
217
|
+
const limit = request.query.limit ? request.query.limit : this.defaultLimit;
|
|
218
|
+
const orderBy = request.query.orderBy;
|
|
219
|
+
const order = request.query.order;
|
|
203
220
|
const search = (_a = request.query).search ?? (_a.search = undefined);
|
|
204
221
|
const filters = this.parseFilters(request.query.filters);
|
|
205
222
|
this.applyUserAndTenantFilters(filters, request.rbac);
|
|
206
|
-
let items = await this.service.find({ search, filters });
|
|
207
|
-
if (this.tenantAssert) {
|
|
208
|
-
items = items.filter(item => request.rbac.tenantId === item[this.tenantField].id);
|
|
209
|
-
}
|
|
223
|
+
let items = await this.service.find({ search, filters, order, orderBy, limit });
|
|
210
224
|
return items;
|
|
211
225
|
}
|
|
212
226
|
catch (e) {
|
|
213
|
-
|
|
214
|
-
if (e instanceof ValidationError) {
|
|
215
|
-
reply.statusCode = e.statusCode;
|
|
216
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
217
|
-
}
|
|
218
|
-
else if (e instanceof UnauthorizedError) {
|
|
219
|
-
reply.statusCode = e.statusCode;
|
|
220
|
-
reply.send({ error: e.message });
|
|
221
|
-
}
|
|
222
|
-
else {
|
|
223
|
-
reply.statusCode = 500;
|
|
224
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
225
|
-
}
|
|
227
|
+
this.handleError(e, reply);
|
|
226
228
|
}
|
|
227
229
|
}
|
|
228
230
|
async findOne(request, reply) {
|
|
@@ -233,25 +235,10 @@ class AbstractFastifyController {
|
|
|
233
235
|
const filters = this.parseFilters(request.query.filters);
|
|
234
236
|
this.applyUserAndTenantFilters(filters, request.rbac);
|
|
235
237
|
let item = await this.service.findOne({ search, filters });
|
|
236
|
-
if (this.tenantAssert) {
|
|
237
|
-
request.rbac.assertTenantId(item[this.tenantField].id);
|
|
238
|
-
}
|
|
239
238
|
return item;
|
|
240
239
|
}
|
|
241
240
|
catch (e) {
|
|
242
|
-
|
|
243
|
-
if (e instanceof ValidationError) {
|
|
244
|
-
reply.statusCode = e.statusCode;
|
|
245
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
246
|
-
}
|
|
247
|
-
else if (e instanceof UnauthorizedError) {
|
|
248
|
-
reply.statusCode = e.statusCode;
|
|
249
|
-
reply.send({ error: e.message });
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
reply.statusCode = 500;
|
|
253
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
254
|
-
}
|
|
241
|
+
this.handleError(e, reply);
|
|
255
242
|
}
|
|
256
243
|
}
|
|
257
244
|
async findBy(request, reply) {
|
|
@@ -261,28 +248,17 @@ class AbstractFastifyController {
|
|
|
261
248
|
reply.statusCode = 400;
|
|
262
249
|
reply.send({ error: 'BAD REQUEST' });
|
|
263
250
|
}
|
|
251
|
+
const limit = this.defaultLimit;
|
|
264
252
|
const field = request.params.field;
|
|
265
253
|
const value = request.params.value;
|
|
266
|
-
let items = await this.service.findBy(field, value);
|
|
267
|
-
|
|
268
|
-
|
|
254
|
+
let items = await this.service.findBy(field, value, limit);
|
|
255
|
+
for (let item of items) {
|
|
256
|
+
this.assertUserAndTenant(item, request.rbac);
|
|
269
257
|
}
|
|
270
258
|
return items;
|
|
271
259
|
}
|
|
272
260
|
catch (e) {
|
|
273
|
-
|
|
274
|
-
if (e instanceof ValidationError) {
|
|
275
|
-
reply.statusCode = e.statusCode;
|
|
276
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
277
|
-
}
|
|
278
|
-
else if (e instanceof UnauthorizedError) {
|
|
279
|
-
reply.statusCode = e.statusCode;
|
|
280
|
-
reply.send({ error: e.message });
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
reply.statusCode = 500;
|
|
284
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
285
|
-
}
|
|
261
|
+
this.handleError(e, reply);
|
|
286
262
|
}
|
|
287
263
|
}
|
|
288
264
|
async findOneBy(request, reply) {
|
|
@@ -295,58 +271,35 @@ class AbstractFastifyController {
|
|
|
295
271
|
const field = request.params.field;
|
|
296
272
|
const value = request.params.value;
|
|
297
273
|
let item = await this.service.findOneBy(field, value);
|
|
298
|
-
|
|
299
|
-
request.rbac.assertTenantId(item[this.tenantField].id);
|
|
300
|
-
}
|
|
274
|
+
this.assertUserAndTenant(item, request.rbac);
|
|
301
275
|
return item;
|
|
302
276
|
}
|
|
303
277
|
catch (e) {
|
|
304
|
-
|
|
305
|
-
if (e instanceof ValidationError) {
|
|
306
|
-
reply.statusCode = e.statusCode;
|
|
307
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
308
|
-
}
|
|
309
|
-
else if (e instanceof UnauthorizedError) {
|
|
310
|
-
reply.statusCode = e.statusCode;
|
|
311
|
-
reply.send({ error: e.message });
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
reply.statusCode = 500;
|
|
315
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
316
|
-
}
|
|
278
|
+
this.handleError(e, reply);
|
|
317
279
|
}
|
|
318
280
|
}
|
|
319
281
|
async search(request, reply) {
|
|
320
282
|
try {
|
|
321
283
|
request.rbac.assertPermission(this.permission.View);
|
|
322
284
|
const search = request.query.search;
|
|
323
|
-
const
|
|
324
|
-
const
|
|
285
|
+
const filters = [];
|
|
286
|
+
const limit = this.defaultLimit;
|
|
325
287
|
this.applyUserAndTenantFilters(filters, request.rbac);
|
|
326
288
|
let item = await this.service.search(search, limit, filters);
|
|
327
289
|
return item;
|
|
328
290
|
}
|
|
329
291
|
catch (e) {
|
|
330
|
-
|
|
331
|
-
if (e instanceof ValidationError) {
|
|
332
|
-
reply.statusCode = e.statusCode;
|
|
333
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
334
|
-
}
|
|
335
|
-
else if (e instanceof UnauthorizedError) {
|
|
336
|
-
reply.statusCode = e.statusCode;
|
|
337
|
-
reply.send({ error: e.message });
|
|
338
|
-
}
|
|
339
|
-
else {
|
|
340
|
-
reply.statusCode = 500;
|
|
341
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
342
|
-
}
|
|
292
|
+
this.handleError(e, reply);
|
|
343
293
|
}
|
|
344
294
|
}
|
|
345
295
|
async paginate(request, reply) {
|
|
346
296
|
try {
|
|
347
297
|
request.rbac.assertPermission(this.permission.View);
|
|
348
|
-
|
|
349
|
-
|
|
298
|
+
if (request.query.limit > this.maximumLimit) {
|
|
299
|
+
throw new LimitError(this.maximumLimit, request.query.limit);
|
|
300
|
+
}
|
|
301
|
+
const page = request.query.page ? request.query.page : 1;
|
|
302
|
+
const limit = request.query.limit ? request.query.limit : 10;
|
|
350
303
|
const orderBy = request.query.orderBy;
|
|
351
304
|
const order = request.query.order;
|
|
352
305
|
const search = request.query.search;
|
|
@@ -357,19 +310,7 @@ class AbstractFastifyController {
|
|
|
357
310
|
return paginateResult;
|
|
358
311
|
}
|
|
359
312
|
catch (e) {
|
|
360
|
-
|
|
361
|
-
if (e instanceof ValidationError) {
|
|
362
|
-
reply.statusCode = e.statusCode;
|
|
363
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
364
|
-
}
|
|
365
|
-
else if (e instanceof UnauthorizedError) {
|
|
366
|
-
reply.statusCode = e.statusCode;
|
|
367
|
-
reply.send({ error: e.message });
|
|
368
|
-
}
|
|
369
|
-
else {
|
|
370
|
-
reply.statusCode = 500;
|
|
371
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
372
|
-
}
|
|
313
|
+
this.handleError(e, reply);
|
|
373
314
|
}
|
|
374
315
|
}
|
|
375
316
|
async export(request, reply) {
|
|
@@ -407,19 +348,7 @@ class AbstractFastifyController {
|
|
|
407
348
|
};
|
|
408
349
|
}
|
|
409
350
|
catch (e) {
|
|
410
|
-
|
|
411
|
-
if (e instanceof ValidationError) {
|
|
412
|
-
reply.statusCode = e.statusCode;
|
|
413
|
-
reply.send({ error: e.message, inputErrors: e.errors });
|
|
414
|
-
}
|
|
415
|
-
else if (e instanceof UnauthorizedError) {
|
|
416
|
-
reply.statusCode = e.statusCode;
|
|
417
|
-
reply.send({ error: e.message });
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
reply.statusCode = 500;
|
|
421
|
-
reply.send({ error: 'INTERNAL_SERVER_ERROR' });
|
|
422
|
-
}
|
|
351
|
+
this.handleError(e, reply);
|
|
423
352
|
}
|
|
424
353
|
}
|
|
425
354
|
}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,20 @@ import AbstractMongoRepository from "./repository/AbstractMongoRepository.js";
|
|
|
3
3
|
import AbstractSqliteRepository from "./repository/AbstractSqliteRepository.js";
|
|
4
4
|
import AbstractService from "./services/AbstractService.js";
|
|
5
5
|
import AbstractFastifyController from "./controllers/AbstractFastifyController.js";
|
|
6
|
+
//schemas
|
|
7
|
+
import { IdParamSchema } from "./schemas/IdParamSchema.js";
|
|
8
|
+
import { DeleteBodyResponseSchema } from "./schemas/DeleteBodyResponseSchema.js";
|
|
9
|
+
import { PaginateBodyResponseSchema, PaginateQuerySchema } from "./schemas/PaginateSchema.js";
|
|
10
|
+
import { FindQuerySchema } from "./schemas/FindSchema.js";
|
|
11
|
+
import { SearchQuerySchema } from "./schemas/SearchSchema.js";
|
|
12
|
+
import { FindByParamSchema } from "./schemas/FindBySchema.js";
|
|
13
|
+
import { ExportBodyResponseSchema } from "./schemas/ExportBodyResponseSchema.js";
|
|
14
|
+
import { ErrorBodyResponseSchema, ValidationErrorBodyResponseSchema } from "./schemas/ErrorBodyResponseSchema.js";
|
|
15
|
+
import { CrudSchemaBuilder } from "./builders/CrudSchemaBuilder.js";
|
|
6
16
|
export {
|
|
7
17
|
//CRUD
|
|
8
|
-
AbstractMongoRepository, AbstractSqliteRepository, AbstractService, AbstractFastifyController,
|
|
18
|
+
AbstractMongoRepository, AbstractSqliteRepository, AbstractService, AbstractFastifyController,
|
|
19
|
+
//Schemas
|
|
20
|
+
IdParamSchema, DeleteBodyResponseSchema, PaginateBodyResponseSchema, PaginateQuerySchema, FindQuerySchema, SearchQuerySchema, FindByParamSchema, ErrorBodyResponseSchema, ValidationErrorBodyResponseSchema, ExportBodyResponseSchema,
|
|
21
|
+
//Builder
|
|
22
|
+
CrudSchemaBuilder, };
|