@anthonylzq/simba.js 1.4.0 → 1.5.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.5.0](https://github.com/AnthonyLzq/simba.js/compare/v1.3.1...v1.5.0) (2021-12-12)
6
+
7
+
8
+ ### Features
9
+
10
+ * moving response function to the global variables and cleaning user route ([52f44aa](https://github.com/AnthonyLzq/simba.js/commit/52f44aadad180816d710b1e03dcc1c809b2be424))
11
+ * simplified the code ([dde8f2e](https://github.com/AnthonyLzq/simba.js/commit/dde8f2e27f86f60321af2f12b53546227d0ffe64))
12
+
5
13
  ## [1.4.0](https://github.com/AnthonyLzq/simba.js/compare/v1.3.1...v1.4.0) (2021-12-12)
6
14
 
7
15
 
@@ -58,7 +58,14 @@ import { DtoUser } from '../dto-interfaces'
58
58
 
59
59
  declare global {
60
60
  // This variable is global, so it will be available everywhere in the code
61
- var myGlobalVariable: unknown
61
+ var response = (
62
+ error: boolean,
63
+ message: unknown,
64
+ res: Response,
65
+ status: number
66
+ ): void => {
67
+ res.status(status).send({ error, message })
68
+ }
62
69
 
63
70
  // We can personalize the response and request objects in case we need it by
64
71
  // adding new optional attributes to this interface
@@ -347,7 +354,7 @@ import swaggerUi from 'swagger-ui-express'
347
354
  import httpErrors from 'http-errors'
348
355
 
349
356
  import { Home, User } from '../routes'
350
- import { response, docs } from '../utils'
357
+ import { docs } from '../utils'
351
358
 
352
359
  const routers = [User]
353
360
 
@@ -459,11 +466,11 @@ class Server {
459
466
  console.log(\`Server running at port \${PORT}\`)
460
467
  })
461
468
 
462
- try {
463
- this._mongo()
464
- } catch (e) {
465
- console.error(e)
466
- }
469
+ // try {
470
+ // this._mongo()
471
+ // } catch (e) {
472
+ // console.error(e)
473
+ // }
467
474
  }
468
475
  }
469
476
 
@@ -485,7 +492,6 @@ export { applyRoutes, Server }
485
492
  routes: {
486
493
  home: {
487
494
  content: `import { Response, Request, Router } from 'express'
488
- import { response } from '../utils'
489
495
 
490
496
  const Home = Router()
491
497
 
@@ -499,7 +505,7 @@ export { Home }
499
505
  },
500
506
  index: {
501
507
  content: `import { Home } from './home'
502
- import { User } from './users'
508
+ import { User } from './user'
503
509
 
504
510
  export { Home, User }
505
511
  `,
@@ -507,9 +513,9 @@ export { Home, User }
507
513
  },
508
514
  user: {
509
515
  content: `import { Router, NextFunction } from 'express'
516
+ import httpErrors from 'http-errors'
517
+ import { ValidationError } from 'joi'
510
518
 
511
- import { Response, Request } from '../custom'
512
- import { response } from '../utils'
513
519
  import { User as UserC } from '../controllers/user'
514
520
  import { DtoUser } from '../dto-interfaces'
515
521
  import { idSchema, userSchema } from '../schemas'
@@ -518,7 +524,11 @@ const User = Router()
518
524
 
519
525
  User.route('/users')
520
526
  .post(
521
- async (req: Request, res: Response, next: NextFunction): Promise<void> => {
527
+ async (
528
+ req: CustomRequest,
529
+ res: CustomResponse,
530
+ next: NextFunction
531
+ ): Promise<void> => {
522
532
  const {
523
533
  body: { args }
524
534
  } = req
@@ -533,7 +543,11 @@ User.route('/users')
533
543
  }
534
544
  )
535
545
  .get(
536
- async (req: Request, res: Response, next: NextFunction): Promise<void> => {
546
+ async (
547
+ req: CustomRequest,
548
+ res: CustomResponse,
549
+ next: NextFunction
550
+ ): Promise<void> => {
537
551
  const u = new UserC()
538
552
 
539
553
  try {
@@ -545,7 +559,11 @@ User.route('/users')
545
559
  }
546
560
  )
547
561
  .delete(
548
- async (req: Request, res: Response, next: NextFunction): Promise<void> => {
562
+ async (
563
+ req: CustomRequest,
564
+ res: CustomResponse,
565
+ next: NextFunction
566
+ ): Promise<void> => {
549
567
  const u = new UserC()
550
568
 
551
569
  try {
@@ -559,7 +577,11 @@ User.route('/users')
559
577
 
560
578
  User.route('/user/:id')
561
579
  .get(
562
- async (req: Request, res: Response, next: NextFunction): Promise<void> => {
580
+ async (
581
+ req: CustomRequest,
582
+ res: CustomResponse,
583
+ next: NextFunction
584
+ ): Promise<void> => {
563
585
  const {
564
586
  params: { id }
565
587
  } = req
@@ -570,13 +592,19 @@ User.route('/user/:id')
570
592
  const result = await u.process({ type: 'getOne' })
571
593
  response(false, result, res, 200)
572
594
  } catch (e) {
573
- if (e.isJoi) e.status = 422
595
+ if (e instanceof ValidationError)
596
+ return next(new httpErrors.UnprocessableEntity(e.message))
597
+
574
598
  next(e)
575
599
  }
576
600
  }
577
601
  )
578
602
  .patch(
579
- async (req: Request, res: Response, next: NextFunction): Promise<void> => {
603
+ async (
604
+ req: CustomRequest,
605
+ res: CustomResponse,
606
+ next: NextFunction
607
+ ): Promise<void> => {
580
608
  const {
581
609
  body: { args },
582
610
  params: { id }
@@ -592,13 +620,19 @@ User.route('/user/:id')
592
620
  const result = await u.process({ type: 'update' })
593
621
  response(false, result, res, 200)
594
622
  } catch (e) {
595
- if (e.isJoi) e.status = 422
623
+ if (e instanceof ValidationError)
624
+ return next(new httpErrors.UnprocessableEntity(e.message))
625
+
596
626
  next(e)
597
627
  }
598
628
  }
599
629
  )
600
630
  .delete(
601
- async (req: Request, res: Response, next: NextFunction): Promise<void> => {
631
+ async (
632
+ req: CustomRequest,
633
+ res: CustomResponse,
634
+ next: NextFunction
635
+ ): Promise<void> => {
602
636
  const {
603
637
  params: { id }
604
638
  } = req
@@ -609,7 +643,9 @@ User.route('/user/:id')
609
643
  const result = await u.process({ type: 'delete' })
610
644
  response(false, result, res, 200)
611
645
  } catch (e) {
612
- if (e.isJoi) e.status = 422
646
+ if (e instanceof ValidationError)
647
+ return next(new httpErrors.UnprocessableEntity(e.message))
648
+
613
649
  next(e)
614
650
  }
615
651
  }
@@ -617,7 +653,7 @@ User.route('/user/:id')
617
653
 
618
654
  export { User }
619
655
  `,
620
- file: `${projectName}/src/routes/users.ts`
656
+ file: `${projectName}/src/routes/user.ts`
621
657
  }
622
658
  },
623
659
  schemas: {
@@ -1108,9 +1144,8 @@ DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
1108
1144
  },
1109
1145
  index: {
1110
1146
  content: `import docs from './docs.json'
1111
- import { response } from './response'
1112
1147
 
1113
- export { docs, response }
1148
+ export { docs }
1114
1149
  `,
1115
1150
  file: `${projectName}/src/utils/index.ts`
1116
1151
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonylzq/simba.js",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "set up a modern backend app by running one command",
5
5
  "main": "lib/index.js",
6
6
  "directories": {