@anthonylzq/simba.js 1.9.1 → 1.10.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.
@@ -5,7 +5,7 @@ const writeFile = require('../utils/writeFile')
5
5
  /*
6
6
  * src
7
7
  * |- @types:
8
- * | |- global: content, file
8
+ * | |- index: content, file
9
9
  * |- controllers:
10
10
  * | |- utils:
11
11
  * | | |- messages:
@@ -56,15 +56,14 @@ import { Request, Response } from 'express'
56
56
  import { DtoUser } from 'dto-interfaces'
57
57
 
58
58
  declare global {
59
- // This variable is global, so it will be available everywhere in the code
60
- var response = (
61
- error: boolean,
62
- message: unknown,
63
- res: Response,
59
+ interface ResponseProps {
60
+ error: boolean
61
+ message: unknown
62
+ res: Response
64
63
  status: number
65
- ): void => {
66
- res.status(status).send({ error, message })
67
64
  }
65
+ // This variable is global, so it will be available everywhere in the code
66
+ var response: ({ error, message, res, status }: ResponseProps) => void
68
67
 
69
68
  // We can personalize the response and request objects in case we need it by
70
69
  // adding new optional attributes to this interface
@@ -163,7 +162,10 @@ class User {
163
162
  try {
164
163
  const usersDeleted = await UserModel.deleteMany({})
165
164
 
166
- if (usersDeleted.acknowledged) return MFU.ALL_USERS_DELETED
165
+ if (usersDeleted.deletedCount >= 1) return MFU.ALL_USERS_DELETED
166
+
167
+ if (usersDeleted.deletedCount === 0)
168
+ throw new httpErrors.BadRequest(EFU.NOTHING_TO_DELETE)
167
169
 
168
170
  throw new httpErrors.InternalServerError(GE.INTERNAL_SERVER_ERROR)
169
171
  } catch (e) {
@@ -257,7 +259,8 @@ export { EFU, MFU, GenericErrors as GE }
257
259
  },
258
260
  user: {
259
261
  content: `enum ErrorForUser {
260
- NOT_FOUND = 'The requested user does not exists'
262
+ NOT_FOUND = 'The requested user does not exists',
263
+ NOTHING_TO_DELETE = 'There is no user to be deleted'
261
264
  }
262
265
 
263
266
  enum MessageForUser {
@@ -373,7 +376,12 @@ const applyRoutes = (app: Application): void => {
373
376
  res: Response,
374
377
  next: NextFunction
375
378
  ) => {
376
- response(true, error.message, res, error.status)
379
+ response({
380
+ error: true,
381
+ message: error.message,
382
+ res,
383
+ status: error.status
384
+ })
377
385
  next()
378
386
  }
379
387
  )
@@ -421,6 +429,17 @@ class Server {
421
429
  next()
422
430
  }
423
431
  )
432
+
433
+ // setting up the global response
434
+ global.response = ({
435
+ error,
436
+ message,
437
+ res,
438
+ status
439
+ }: ResponseProps): void => {
440
+ res.status(status).send({ error, message })
441
+ }
442
+
424
443
  applyRoutes(this._app)
425
444
  }
426
445
 
@@ -493,7 +512,12 @@ export { applyRoutes, Server }
493
512
  const Home = Router()
494
513
 
495
514
  Home.route('').get((req: Request, res: Response) => {
496
- response(false, 'Welcome to your Express Backend!', res, 200)
515
+ response({
516
+ error: false,
517
+ message: 'Welcome to your Express Backend!',
518
+ res,
519
+ status: 200
520
+ })
497
521
  })
498
522
 
499
523
  export { Home }
@@ -533,7 +557,7 @@ User.route('/users')
533
557
 
534
558
  try {
535
559
  const result = await u.process({ type: 'store' })
536
- response(false, result, res, 201)
560
+ response({ error: false, message: result, res, status: 201 })
537
561
  } catch (e) {
538
562
  next(e)
539
563
  }
@@ -549,7 +573,7 @@ User.route('/users')
549
573
 
550
574
  try {
551
575
  const result = await u.process({ type: 'getAll' })
552
- response(false, result, res, 200)
576
+ response({ error: false, message: result, res, status: 200 })
553
577
  } catch (e) {
554
578
  next(e)
555
579
  }
@@ -565,7 +589,7 @@ User.route('/users')
565
589
 
566
590
  try {
567
591
  const result = await u.process({ type: 'deleteAll' })
568
- response(false, result, res, 200)
592
+ response({ error: false, message: result, res, status: 200 })
569
593
  } catch (e) {
570
594
  next(e)
571
595
  }
@@ -587,7 +611,7 @@ User.route('/user/:id')
587
611
  await idSchema.validateAsync(id)
588
612
  const u = new UserC({ id } as DtoUser)
589
613
  const result = await u.process({ type: 'getOne' })
590
- response(false, result, res, 200)
614
+ response({ error: false, message: result, res, status: 200 })
591
615
  } catch (e) {
592
616
  if (e instanceof ValidationError)
593
617
  return next(new httpErrors.UnprocessableEntity(e.message))
@@ -615,7 +639,7 @@ User.route('/user/:id')
615
639
  await userSchema.validateAsync(user)
616
640
  const u = new UserC(user)
617
641
  const result = await u.process({ type: 'update' })
618
- response(false, result, res, 200)
642
+ response({ error: false, message: result, res, status: 200 })
619
643
  } catch (e) {
620
644
  if (e instanceof ValidationError)
621
645
  return next(new httpErrors.UnprocessableEntity(e.message))
@@ -638,7 +662,7 @@ User.route('/user/:id')
638
662
  await idSchema.validateAsync(id)
639
663
  const u = new UserC({ id } as DtoUser)
640
664
  const result = await u.process({ type: 'delete' })
641
- response(false, result, res, 200)
665
+ response({ error: false, message: result, res, status: 200 })
642
666
  } catch (e) {
643
667
  if (e instanceof ValidationError)
644
668
  return next(new httpErrors.UnprocessableEntity(e.message))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonylzq/simba.js",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "description": "set up a modern backend app by running one command",
5
5
  "main": "lib/index.js",
6
6
  "directories": {
@@ -33,11 +33,11 @@
33
33
  "cli-progress": "^3.9.1",
34
34
  "colors": "^1.4.0",
35
35
  "readline-sync": "^1.4.10",
36
- "underscore": "^1.13.1",
36
+ "underscore": "^1.13.2",
37
37
  "yargs": "^17.3.0"
38
38
  },
39
39
  "devDependencies": {
40
- "eslint": "^8.4.1",
40
+ "eslint": "^8.5.0",
41
41
  "eslint-config-prettier": "^8.3.0",
42
42
  "eslint-config-standard": "^16.0.3",
43
43
  "eslint-plugin-import": "^2.25.3",
@@ -52,5 +52,9 @@
52
52
  "bugs": {
53
53
  "url": "https://github.com/AnthonyLzq/simba.js/issues"
54
54
  },
55
- "homepage": "https://github.com/AnthonyLzq/simba.js#readme"
55
+ "homepage": "https://github.com/AnthonyLzq/simba.js#readme",
56
+ "files": [
57
+ "lib",
58
+ "bin"
59
+ ]
56
60
  }
package/CHANGELOG.md DELETED
@@ -1,124 +0,0 @@
1
- # Changelog
2
-
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
-
5
- ### [1.9.1](https://github.com/AnthonyLzq/simba.js/compare/v1.9.0...v1.9.1) (2021-12-14)
6
-
7
-
8
- ### Features
9
-
10
- * updated packages in example folder and .eslint rules ([b7bed74](https://github.com/AnthonyLzq/simba.js/commit/b7bed74fcd58f89abdffb7a06f87e4d808059af8))
11
-
12
- ## [1.9.0](https://github.com/AnthonyLzq/simba.js/compare/v1.8.0...v1.9.0) (2021-12-13)
13
-
14
-
15
- ### Features
16
-
17
- * updated packages and example folder to include license and heroku config ([7267e7a](https://github.com/AnthonyLzq/simba.js/commit/7267e7ab40bf7333d566d7e90d85ad4f4357b731))
18
-
19
-
20
- ### Bug Fixes
21
-
22
- * linted eslint.js file ([a47ea79](https://github.com/AnthonyLzq/simba.js/commit/a47ea795e2f6cf4363bd82ed05fc2c31b4fa8a26))
23
-
24
- ## [1.8.0](https://github.com/AnthonyLzq/simba.js/compare/v1.7.0...v1.8.0) (2021-12-13)
25
-
26
-
27
- ### Features
28
-
29
- * implemented baseUrl config with ts and webpack to avoid "../../.. ..." ([9b9e9d2](https://github.com/AnthonyLzq/simba.js/commit/9b9e9d20cae8b4bbdaef58364220512514dac1a4))
30
- * updated docker file and ignored git folder to be generated manually by docker ([33c1b0d](https://github.com/AnthonyLzq/simba.js/commit/33c1b0df9cfac4e686ece884aa3102e9ad49e6ab))
31
- * updated docs ([ba72b64](https://github.com/AnthonyLzq/simba.js/commit/ba72b643ea45af7ce7dd7a9adaf8b4f27eacd266))
32
-
33
-
34
- ### Bug Fixes
35
-
36
- * fixed eslint ignore ([1405884](https://github.com/AnthonyLzq/simba.js/commit/1405884ab10cf869d6811fbf588ba0b0efb04de2))
37
- * fixed mongo connection ([57f8580](https://github.com/AnthonyLzq/simba.js/commit/57f85803701253be2308b1c45f537db5b59c92d4))
38
-
39
- ## [1.7.0](https://github.com/AnthonyLzq/simba.js/compare/v1.6.0...v1.7.0) (2021-12-12)
40
-
41
-
42
- ### Features
43
-
44
- * updated docs and minor changes ([e6b32ca](https://github.com/AnthonyLzq/simba.js/commit/e6b32ca309603544e3506c9d18717f0e45ef0457))
45
-
46
- ## [1.6.0](https://github.com/AnthonyLzq/simba.js/compare/v1.5.0...v1.6.0) (2021-12-12)
47
-
48
-
49
- ### Features
50
-
51
- * implemented default mongo connection to local database ([1e66e98](https://github.com/AnthonyLzq/simba.js/commit/1e66e982b03eca5c2c3846a10ad827150683da21))
52
-
53
- ## [1.5.0](https://github.com/AnthonyLzq/simba.js/compare/v1.3.1...v1.5.0) (2021-12-12)
54
-
55
-
56
- ### Features
57
-
58
- * moving response function to the global variables and cleaning user route ([52f44aa](https://github.com/AnthonyLzq/simba.js/commit/52f44aadad180816d710b1e03dcc1c809b2be424))
59
- * simplified the code ([dde8f2e](https://github.com/AnthonyLzq/simba.js/commit/dde8f2e27f86f60321af2f12b53546227d0ffe64))
60
-
61
- ## [1.4.0](https://github.com/AnthonyLzq/simba.js/compare/v1.3.1...v1.4.0) (2021-12-12)
62
-
63
-
64
- ### Features
65
-
66
- * simplified the code ([dde8f2e](https://github.com/AnthonyLzq/simba.js/commit/dde8f2e27f86f60321af2f12b53546227d0ffe64))
67
-
68
- ### [1.3.1](https://github.com/AnthonyLzq/simba.js/compare/v1.3.0...v1.3.1) (2021-12-12)
69
-
70
-
71
- ### Features
72
-
73
- * updated docs ([809afaa](https://github.com/AnthonyLzq/simba.js/commit/809afaad2c1c3046fab509842118064ec6a61d5f))
74
-
75
- ## [1.3.0](https://github.com/AnthonyLzq/simba.js/compare/v1.2.0...v1.3.0) (2021-12-12)
76
-
77
-
78
- ### Features
79
-
80
- * implemented eslint rules ([748fb91](https://github.com/AnthonyLzq/simba.js/commit/748fb91a031102855dd7ba08344cc1ec3e181a4f))
81
- * implemented support for global variables (Node.js v16), linted all the code, fixed unlicensed project bug and simplified project structure ([ae4876f](https://github.com/AnthonyLzq/simba.js/commit/ae4876f249ff7dfd16fd4af7e41b3388c7de0f6a))
82
-
83
- ## [1.2.0](https://github.com/AnthonyLzq/simba.js/compare/v1.1.3...v1.2.0) (2021-10-18)
84
-
85
-
86
- ### Features
87
-
88
- * implemented support for project names with more than a word and updated dependencies ([2600413](https://github.com/AnthonyLzq/simba.js/commit/2600413bf7a5ef92e554738066e9bc4aea892f6f))
89
-
90
- ### [1.1.3](https://github.com/AnthonyLzq/simba.js/compare/v1.1.2...v1.1.3) (2021-10-17)
91
-
92
-
93
- ### Bug Fixes
94
-
95
- * fixed bin path ([bf194ff](https://github.com/AnthonyLzq/simba.js/commit/bf194ff21e6aaf63dd1a80c104b8a3567f0887b1))
96
-
97
- ### [1.1.2](https://github.com/AnthonyLzq/simba.js/compare/v1.1.1...v1.1.2) (2021-10-17)
98
-
99
-
100
- ### Bug Fixes
101
-
102
- * updated eslint package (eslint-config-airbnb -> eslint-config-airbnb-base) ([06547bc](https://github.com/AnthonyLzq/simba.js/commit/06547bc618e79f9f6e47e3b0fbd188358befe408))
103
-
104
- ### [1.1.1](https://github.com/AnthonyLzq/simba.js/compare/v1.0.1...v1.1.1) (2021-10-17)
105
-
106
-
107
- ### Features
108
-
109
- * added new eslint package and some new rules ([d79426c](https://github.com/AnthonyLzq/simba.js/commit/d79426c23fc899128d68c8b2e79ed7c5c0e4b18b))
110
-
111
- ### [1.0.1](https://github.com/AnthonyLzq/simba.js/compare/v1.0.0...v1.0.1) (2021-10-01)
112
-
113
-
114
- ### Bug Fixes
115
-
116
- * flag for unlicensed ([6b20a5b](https://github.com/AnthonyLzq/simba.js/commit/6b20a5b8a9ad60e5278b38252849fe5c3b2d54a4))
117
-
118
- ## 1.0.0 (2021-09-28)
119
-
120
-
121
- ### Features
122
-
123
- * first simba.js release ([d10fa01](https://github.com/AnthonyLzq/simba.js/commit/d10fa0199a8bff941da186c33fc16b512295a037))
124
- * updated package name and added standard-version command ([d0d0906](https://github.com/AnthonyLzq/simba.js/commit/d0d09064587a814f97d7a63b865b28a6f05030ad))