@drax/crud-back 3.13.0 → 3.15.0

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.
@@ -204,9 +204,21 @@ class AbstractFastifyController extends CommonController {
204
204
  }
205
205
  return 'Unknown import error';
206
206
  }
207
+ assertCreatePermission(request) {
208
+ request.rbac.assertPermission(this.permission.Create);
209
+ }
210
+ assertReadPermission(request) {
211
+ request.rbac.assertPermission(this.permission.View);
212
+ }
213
+ assertUpdatePermission(request) {
214
+ request.rbac.assertPermission(this.permission.Update);
215
+ }
216
+ assertDeletePermission(request) {
217
+ request.rbac.assertPermission(this.permission.Delete);
218
+ }
207
219
  async create(request, reply) {
208
220
  try {
209
- request.rbac.assertPermission(this.permission.Create);
221
+ this.assertCreatePermission(request);
210
222
  const payload = await this.prepareCreatePayload(request, request.body);
211
223
  let item = await this.service.create(payload);
212
224
  this.onCreated(request, item);
@@ -225,7 +237,7 @@ class AbstractFastifyController extends CommonController {
225
237
  }
226
238
  async update(request, reply) {
227
239
  try {
228
- request.rbac.assertPermission(this.permission.Update);
240
+ this.assertUpdatePermission(request);
229
241
  if (!request.params.id) {
230
242
  reply.statusCode = 400;
231
243
  reply.send({ error: 'BAD REQUEST' });
@@ -298,7 +310,7 @@ class AbstractFastifyController extends CommonController {
298
310
  }
299
311
  async updatePartial(request, reply) {
300
312
  try {
301
- request.rbac.assertPermission(this.permission.Update);
313
+ this.assertUpdatePermission(request);
302
314
  if (!request.params.id) {
303
315
  reply.statusCode = 400;
304
316
  reply.send({ error: 'BAD REQUEST' });
@@ -345,7 +357,7 @@ class AbstractFastifyController extends CommonController {
345
357
  }
346
358
  async delete(request, reply) {
347
359
  try {
348
- request.rbac.assertPermission(this.permission.Delete);
360
+ this.assertDeletePermission(request);
349
361
  if (!request.params.id) {
350
362
  reply.statusCode = 400;
351
363
  reply.send({ error: 'BAD REQUEST' });
@@ -377,7 +389,7 @@ class AbstractFastifyController extends CommonController {
377
389
  }
378
390
  async findById(request, reply) {
379
391
  try {
380
- request.rbac.assertPermission(this.permission.View);
392
+ this.assertReadPermission(request);
381
393
  if (!request.params.id) {
382
394
  reply.statusCode = 400;
383
395
  reply.send({ error: 'BAD REQUEST' });
@@ -400,7 +412,7 @@ class AbstractFastifyController extends CommonController {
400
412
  }
401
413
  async findByIds(request, reply) {
402
414
  try {
403
- request.rbac.assertPermission(this.permission.View);
415
+ this.assertReadPermission(request);
404
416
  if (!request.params.ids) {
405
417
  reply.statusCode = 400;
406
418
  reply.send({ error: 'BAD REQUEST' });
@@ -420,7 +432,7 @@ class AbstractFastifyController extends CommonController {
420
432
  async find(request, reply) {
421
433
  var _a;
422
434
  try {
423
- request.rbac.assertPermission(this.permission.View);
435
+ this.assertReadPermission(request);
424
436
  if (request.query.limit > this.maximumLimit) {
425
437
  throw new LimitError(this.maximumLimit, request.query.limit);
426
438
  }
@@ -442,7 +454,7 @@ class AbstractFastifyController extends CommonController {
442
454
  async findOne(request, reply) {
443
455
  var _a;
444
456
  try {
445
- request.rbac.assertPermission(this.permission.View);
457
+ this.assertReadPermission(request);
446
458
  const search = (_a = request.query).search ?? (_a.search = undefined);
447
459
  let filters = this.parseFilters(request.query.filters);
448
460
  filters = await this.preRead(request, filters);
@@ -457,7 +469,7 @@ class AbstractFastifyController extends CommonController {
457
469
  }
458
470
  async findBy(request, reply) {
459
471
  try {
460
- request.rbac.assertPermission(this.permission.View);
472
+ this.assertReadPermission(request);
461
473
  if (!request.params.field || !request.params.value) {
462
474
  reply.statusCode = 400;
463
475
  reply.send({ error: 'BAD REQUEST' });
@@ -478,7 +490,7 @@ class AbstractFastifyController extends CommonController {
478
490
  }
479
491
  async findOneBy(request, reply) {
480
492
  try {
481
- request.rbac.assertPermission(this.permission.View);
493
+ this.assertReadPermission(request);
482
494
  if (!request.params.field || !request.params.value) {
483
495
  reply.statusCode = 400;
484
496
  reply.send({ error: 'BAD REQUEST' });
@@ -498,7 +510,7 @@ class AbstractFastifyController extends CommonController {
498
510
  }
499
511
  async search(request, reply) {
500
512
  try {
501
- request.rbac.assertPermission(this.permission.View);
513
+ this.assertReadPermission(request);
502
514
  const search = request.query.search;
503
515
  let filters = [];
504
516
  const limit = this.defaultLimit;
@@ -514,7 +526,7 @@ class AbstractFastifyController extends CommonController {
514
526
  }
515
527
  async paginate(request, reply) {
516
528
  try {
517
- request.rbac.assertPermission(this.permission.View);
529
+ this.assertReadPermission(request);
518
530
  if (request.query.limit > this.maximumLimit) {
519
531
  throw new LimitError(this.maximumLimit, request.query.limit);
520
532
  }
@@ -536,7 +548,7 @@ class AbstractFastifyController extends CommonController {
536
548
  }
537
549
  async export(request, reply) {
538
550
  try {
539
- request.rbac.assertPermission(this.permission.View);
551
+ this.assertReadPermission(request);
540
552
  const format = request.query.format || 'JSON';
541
553
  const headers = request.query.headers ? request.query.headers.split(",") : [];
542
554
  const headersTranslate = request.query.headersTranslate ? request.query.headersTranslate.split(",") : [];
@@ -581,7 +593,7 @@ class AbstractFastifyController extends CommonController {
581
593
  }
582
594
  async import(request, reply) {
583
595
  try {
584
- request.rbac.assertPermission(this.permission.Create);
596
+ this.assertCreatePermission(request);
585
597
  const data = await request.file();
586
598
  if (!data) {
587
599
  throw new BadRequestError('Import file is required');
@@ -676,7 +688,7 @@ class AbstractFastifyController extends CommonController {
676
688
  }
677
689
  async groupBy(request, reply) {
678
690
  try {
679
- request.rbac.assertPermission(this.permission.View);
691
+ this.assertReadPermission(request);
680
692
  const fields = request.query.fields ?
681
693
  request.query.fields.split(',').map(f => f.trim()).filter(f => f.length > 0) :
682
694
  [];
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "3.13.0",
6
+ "version": "3.15.0",
7
7
  "description": "Crud utils across modules",
8
8
  "main": "dist/index.js",
9
9
  "types": "types/index.d.ts",
@@ -22,10 +22,10 @@
22
22
  "author": "Cristian Incarnato & Drax Team",
23
23
  "license": "ISC",
24
24
  "dependencies": {
25
- "@drax/common-back": "^3.10.0",
25
+ "@drax/common-back": "^3.14.0",
26
26
  "@drax/common-share": "^3.0.0",
27
- "@drax/identity-share": "^3.0.0",
28
- "@drax/media-back": "^3.13.0",
27
+ "@drax/identity-share": "^3.15.0",
28
+ "@drax/media-back": "^3.15.0",
29
29
  "@graphql-tools/load-files": "^7.0.0",
30
30
  "@graphql-tools/merge": "^9.0.4",
31
31
  "mongoose": "^8.23.0",
@@ -47,5 +47,5 @@
47
47
  "typescript": "^5.9.3",
48
48
  "vitest": "^3.2.4"
49
49
  },
50
- "gitHead": "5f68eefbcb01c876471e387a815fee1040489c2c"
50
+ "gitHead": "a3bd3419f580b111b26da9e6be2e1cc4c75a056e"
51
51
  }
@@ -300,9 +300,25 @@ class AbstractFastifyController<T, C, U> extends CommonController {
300
300
  return 'Unknown import error'
301
301
  }
302
302
 
303
+ protected assertCreatePermission(request: CustomRequest) {
304
+ request.rbac.assertPermission(this.permission.Create)
305
+ }
306
+
307
+ protected assertReadPermission(request: CustomRequest) {
308
+ request.rbac.assertPermission(this.permission.View)
309
+ }
310
+
311
+ protected assertUpdatePermission(request: CustomRequest) {
312
+ request.rbac.assertPermission(this.permission.Update)
313
+ }
314
+
315
+ protected assertDeletePermission(request: CustomRequest) {
316
+ request.rbac.assertPermission(this.permission.Delete)
317
+ }
318
+
303
319
  async create(request: CustomRequest, reply: FastifyReply) {
304
320
  try {
305
- request.rbac.assertPermission(this.permission.Create)
321
+ this.assertCreatePermission(request)
306
322
  const payload = await this.prepareCreatePayload(request, request.body)
307
323
  let item = await this.service.create(payload as C)
308
324
  this.onCreated(request, item)
@@ -323,7 +339,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
323
339
 
324
340
  async update(request: CustomRequest, reply: FastifyReply) {
325
341
  try {
326
- request.rbac.assertPermission(this.permission.Update)
342
+ this.assertUpdatePermission(request)
327
343
  if (!request.params.id) {
328
344
  reply.statusCode = 400
329
345
  reply.send({error: 'BAD REQUEST'})
@@ -418,7 +434,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
418
434
 
419
435
  async updatePartial(request: CustomRequest, reply: FastifyReply) {
420
436
  try {
421
- request.rbac.assertPermission(this.permission.Update)
437
+ this.assertUpdatePermission(request)
422
438
  if (!request.params.id) {
423
439
  reply.statusCode = 400
424
440
  reply.send({error: 'BAD REQUEST'})
@@ -477,7 +493,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
477
493
 
478
494
  async delete(request: CustomRequest, reply: FastifyReply) {
479
495
  try {
480
- request.rbac.assertPermission(this.permission.Delete)
496
+ this.assertDeletePermission(request)
481
497
  if (!request.params.id) {
482
498
  reply.statusCode = 400
483
499
  reply.send({error: 'BAD REQUEST'})
@@ -519,7 +535,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
519
535
 
520
536
  async findById(request: CustomRequest, reply: FastifyReply): Promise<T> {
521
537
  try {
522
- request.rbac.assertPermission(this.permission.View)
538
+ this.assertReadPermission(request)
523
539
  if (!request.params.id) {
524
540
  reply.statusCode = 400
525
541
  reply.send({error: 'BAD REQUEST'})
@@ -549,7 +565,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
549
565
 
550
566
  async findByIds(request: CustomRequest, reply: FastifyReply): Promise<T[]> {
551
567
  try {
552
- request.rbac.assertPermission(this.permission.View)
568
+ this.assertReadPermission(request)
553
569
  if (!request.params.ids) {
554
570
  reply.statusCode = 400
555
571
  reply.send({error: 'BAD REQUEST'})
@@ -571,7 +587,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
571
587
 
572
588
  async find(request: CustomRequest, reply: FastifyReply): Promise<T[]> {
573
589
  try {
574
- request.rbac.assertPermission(this.permission.View)
590
+ this.assertReadPermission(request)
575
591
 
576
592
  if (request.query.limit > this.maximumLimit) {
577
593
  throw new LimitError(this.maximumLimit, request.query.limit)
@@ -597,7 +613,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
597
613
 
598
614
  async findOne(request: CustomRequest, reply: FastifyReply): Promise<T> {
599
615
  try {
600
- request.rbac.assertPermission(this.permission.View)
616
+ this.assertReadPermission(request)
601
617
 
602
618
  const search = request.query.search ??= undefined
603
619
  let filters = this.parseFilters(request.query.filters)
@@ -617,7 +633,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
617
633
 
618
634
  async findBy(request: CustomRequest, reply: FastifyReply): Promise<T[]> {
619
635
  try {
620
- request.rbac.assertPermission(this.permission.View)
636
+ this.assertReadPermission(request)
621
637
  if (!request.params.field || !request.params.value) {
622
638
  reply.statusCode = 400
623
639
  reply.send({error: 'BAD REQUEST'})
@@ -644,7 +660,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
644
660
 
645
661
  async findOneBy(request: CustomRequest, reply: FastifyReply): Promise<T> {
646
662
  try {
647
- request.rbac.assertPermission(this.permission.View)
663
+ this.assertReadPermission(request)
648
664
  if (!request.params.field || !request.params.value) {
649
665
  reply.statusCode = 400
650
666
  reply.send({error: 'BAD REQUEST'})
@@ -671,7 +687,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
671
687
 
672
688
  async search(request: CustomRequest, reply: FastifyReply) {
673
689
  try {
674
- request.rbac.assertPermission(this.permission.View)
690
+ this.assertReadPermission(request)
675
691
  const search = request.query.search
676
692
  let filters = []
677
693
  const limit = this.defaultLimit
@@ -689,7 +705,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
689
705
 
690
706
  async paginate(request: CustomRequest, reply: FastifyReply) {
691
707
  try {
692
- request.rbac.assertPermission(this.permission.View)
708
+ this.assertReadPermission(request)
693
709
 
694
710
 
695
711
  if (request.query.limit > this.maximumLimit) {
@@ -716,7 +732,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
716
732
 
717
733
  async export(request: CustomRequest, reply: FastifyReply) {
718
734
  try {
719
- request.rbac.assertPermission(this.permission.View)
735
+ this.assertReadPermission(request)
720
736
 
721
737
  const format = request.query.format as 'CSV' | 'JSON' || 'JSON'
722
738
  const headers = request.query.headers ? request.query.headers.split(",") : []
@@ -773,7 +789,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
773
789
 
774
790
  async import(request: CustomRequest, reply: FastifyReply) {
775
791
  try {
776
- request.rbac.assertPermission(this.permission.Create)
792
+ this.assertCreatePermission(request)
777
793
 
778
794
  const data = await (request as any).file()
779
795
  if (!data) {
@@ -879,7 +895,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
879
895
 
880
896
  async groupBy(request: CustomRequest, reply: FastifyReply) {
881
897
  try {
882
- request.rbac.assertPermission(this.permission.View)
898
+ this.assertReadPermission(request)
883
899
 
884
900
  const fields: string[] = request.query.fields ?
885
901
  request.query.fields.split(',').map(f => f.trim()).filter(f => f.length > 0) :