@anthonylzq/simba.js 4.0.0 → 4.2.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/README.md CHANGED
@@ -136,9 +136,8 @@ Regardless of the option chosen, a new folder will be generated with the name of
136
136
  ┃ ┗ 📜index.ts
137
137
  ┣ 📂network
138
138
  ┃ ┣ 📂routes
139
- ┃ ┃ ┣ 📂schemas
140
- ┃ ┃ ┃ 📜index.ts
141
- ┃ ┃ ┃ ┗ 📜user.ts
139
+ ┃ ┃ ┣ 📂utils
140
+ ┃ ┃ ┃ 📜docs.ts
142
141
  ┃ ┃ ┣ 📜home.ts
143
142
  ┃ ┃ ┣ 📜index.ts
144
143
  ┃ ┃ ┗ 📜user.ts
@@ -146,6 +145,9 @@ Regardless of the option chosen, a new folder will be generated with the name of
146
145
  ┃ ┣ 📜response.ts
147
146
  ┃ ┣ 📜routes.ts
148
147
  ┃ ┗ 📜server.ts
148
+ ┣ 📂schemas
149
+ ┃ ┣ 📜index.ts
150
+ ┃ ┗ 📜user.ts
149
151
  ┣ 📂services
150
152
  ┃ ┣ 📂utils
151
153
  ┃ ┃ ┣ 📂messages
@@ -183,8 +185,6 @@ Regardless of the option chosen, a new folder will be generated with the name of
183
185
  📂node_modules
184
186
  📂src
185
187
  ┣ 📂@types
186
- ┃ ┣ 📂dto
187
- ┃ ┃ ┗ 📜user.d.ts
188
188
  ┃ ┣ 📂models
189
189
  ┃ ┃ ┗ 📜user.d.ts
190
190
  ┃ ┗ 📜index.d.ts
@@ -200,9 +200,8 @@ Regardless of the option chosen, a new folder will be generated with the name of
200
200
  ┃ ┗ 📜index.ts
201
201
  ┣ 📂network
202
202
  ┃ ┣ 📂routes
203
- ┃ ┃ ┣ 📂schemas
204
- ┃ ┃ ┃ 📜index.ts
205
- ┃ ┃ ┃ ┗ 📜user.ts
203
+ ┃ ┃ ┣ 📂utils
204
+ ┃ ┃ ┃ 📜docs.ts
206
205
  ┃ ┃ ┣ 📜docs.ts
207
206
  ┃ ┃ ┣ 📜home.ts
208
207
  ┃ ┃ ┣ 📜index.ts
@@ -211,6 +210,9 @@ Regardless of the option chosen, a new folder will be generated with the name of
211
210
  ┃ ┣ 📜response.ts
212
211
  ┃ ┣ 📜routes.ts
213
212
  ┃ ┗ 📜server.ts
213
+ ┣ 📂schemas
214
+ ┃ ┣ 📜index.ts
215
+ ┃ ┗ 📜user.ts
214
216
  ┣ 📂services
215
217
  ┃ ┣ 📂utils
216
218
  ┃ ┃ ┣ 📂messages
@@ -331,12 +333,14 @@ As `dependencies`:
331
333
  As `devDependencies`:
332
334
 
333
335
  - [`@types/express`](https://www.npmjs.com/package/@types/express)
336
+ - [`@types/cors`](https://www.npmjs.com/package/@types/cors)
334
337
  - [`@types/morgan`](https://www.npmjs.com/package/@types/morgan)
335
338
  - [`@types/swagger-ui-express`](https://www.npmjs.com/package/@types/swagger-ui-express)
336
339
 
337
340
  As `dependencies`:
338
341
 
339
- - [`express`](https://expressjs.com/)
342
+ - [`cors`](https://www.npmjs.com/package/cors)
343
+ - [`express`](https://www.npmjs.com/package/express)
340
344
  - [`morgan`](https://www.npmjs.com/package/morgan)
341
345
  - [`swagger-ui-express`](https://www.npmjs.com/package/swagger-ui-express)
342
346
 
@@ -345,6 +349,7 @@ As `dependencies`:
345
349
  As `dependencies`:
346
350
 
347
351
  - [`fastify`](https://www.npmjs.com/package/fastify)
352
+ - [`fastify-cors`](https://www.npmjs.com/package/fastify-cors)
348
353
  - [`fastify-swagger`](https://www.npmjs.com/package/fastify-swagger)
349
354
 
350
355
  Feel free to contribute to this project. Every contribution will be appreciated.
@@ -121,17 +121,6 @@ export {}
121
121
  file: `${projectName}/src/@types/index.d.ts`
122
122
  }
123
123
  },
124
- '@types/dto': {
125
- user: {
126
- content: `interface UserDTO {
127
- id: string
128
- lastName: string
129
- name: string
130
- }
131
- `,
132
- file: `${projectName}/src/@types/dto/user.d.ts`
133
- }
134
- },
135
124
  '@types/models': {
136
125
  user: {
137
126
  content: `interface UserDBO {
@@ -661,26 +650,27 @@ export { applyRoutes }
661
650
  content: `import express from 'express'
662
651
  import mongoose from 'mongoose'
663
652
  import morgan from 'morgan'
653
+ import cors from 'cors'
664
654
 
665
655
  import { applyRoutes } from './router'
666
656
 
667
657
  const PORT = (process.env.PORT as string) || '1996'
668
658
 
669
659
  class Server {
670
- private _app: express.Application
671
- private _connection: mongoose.Connection | undefined
660
+ #app: express.Application
661
+ #connection: mongoose.Connection | undefined
672
662
 
673
663
  constructor() {
674
- this._app = express()
675
- this._config()
664
+ this.#app = express()
665
+ this.#config()
676
666
  }
677
667
 
678
- private _config() {
679
- this._app.set('port', PORT)
680
- this._app.use(morgan('dev'))
681
- this._app.use(express.json())
682
- this._app.use(express.urlencoded({ extended: false }))
683
- this._app.use(
668
+ #config() {
669
+ this.#app.use(cors())
670
+ this.#app.use(morgan('dev'))
671
+ this.#app.use(express.json())
672
+ this.#app.use(express.urlencoded({ extended: false }))
673
+ this.#app.use(
684
674
  (
685
675
  req: express.Request,
686
676
  res: express.Response,
@@ -696,23 +686,23 @@ class Server {
696
686
  }
697
687
  )
698
688
 
699
- applyRoutes(this._app)
689
+ applyRoutes(this.#app)
700
690
  }
701
691
 
702
- private async _mongo(): Promise<void> {
703
- this._connection = mongoose.connection
692
+ async #mongo(): Promise<void> {
693
+ this.#connection = mongoose.connection
704
694
  const connection = {
705
695
  keepAlive: true,
706
696
  useNewUrlParser: true,
707
697
  useUnifiedTopology: true
708
698
  }
709
- this._connection.on('connected', () => {
699
+ this.#connection.on('connected', () => {
710
700
  console.log('Mongo connection established.')
711
701
  })
712
- this._connection.on('reconnected', () => {
702
+ this.#connection.on('reconnected', () => {
713
703
  console.log('Mongo connection reestablished')
714
704
  })
715
- this._connection.on('disconnected', () => {
705
+ this.#connection.on('disconnected', () => {
716
706
  console.log('Mongo connection disconnected')
717
707
  console.log('Trying to reconnected to Mongo...')
718
708
  setTimeout(() => {
@@ -723,10 +713,10 @@ class Server {
723
713
  })
724
714
  }, 3000)
725
715
  })
726
- this._connection.on('close', () => {
716
+ this.#connection.on('close', () => {
727
717
  console.log('Mongo connection closed')
728
718
  })
729
- this._connection.on('error', (e: Error) => {
719
+ this.#connection.on('error', (e: Error) => {
730
720
  console.log('Mongo connection error:')
731
721
  console.error(e)
732
722
  })
@@ -734,12 +724,12 @@ class Server {
734
724
  }
735
725
 
736
726
  public start(): void {
737
- this._app.listen(PORT, () => {
727
+ this.#app.listen(PORT, () => {
738
728
  console.log(\`Server running at port \${PORT}\`)
739
729
  })
740
730
 
741
731
  try {
742
- this._mongo()
732
+ this.#mongo()
743
733
  } catch (e) {
744
734
  console.error(e)
745
735
  }
@@ -1405,16 +1395,17 @@ import { applyRoutes } from './router'
1405
1395
  const PORT = process.env.PORT ?? '1996'
1406
1396
 
1407
1397
  class Server {
1408
- private _app: FastifyInstance
1409
- private _connection: mongoose.Connection | undefined
1398
+ #app: FastifyInstance
1399
+ #connection: mongoose.Connection | undefined
1410
1400
 
1411
1401
  constructor() {
1412
- this._app = Fastify({ logger: true })
1413
- this._config()
1402
+ this.#app = Fastify({ logger: true })
1403
+ this.#config()
1414
1404
  }
1415
1405
 
1416
- private _config() {
1417
- this._app.addHook('preHandler', (req, reply, done) => {
1406
+ #config() {
1407
+ this.#app.register(require('fastify-cors'), {})
1408
+ this.#app.addHook('preHandler', (req, reply, done) => {
1418
1409
  reply.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE')
1419
1410
  reply.header('Access-Control-Allow-Origin', '*')
1420
1411
  reply.header(
@@ -1423,25 +1414,25 @@ class Server {
1423
1414
  )
1424
1415
  done()
1425
1416
  })
1426
- applyRoutes(this._app)
1417
+ applyRoutes(this.#app)
1427
1418
  }
1428
1419
 
1429
- private async _mongo(): Promise<void> {
1430
- this._connection = mongoose.connection
1420
+ async #mongo(): Promise<void> {
1421
+ this.#connection = mongoose.connection
1431
1422
  const connection = {
1432
1423
  keepAlive: true,
1433
1424
  useNewUrlParser: true,
1434
1425
  useUnifiedTopology: true
1435
1426
  }
1436
- this._connection.on('connected', () => {
1437
- this._app.log.info('Mongo connection established.')
1427
+ this.#connection.on('connected', () => {
1428
+ this.#app.log.info('Mongo connection established.')
1438
1429
  })
1439
- this._connection.on('reconnected', () => {
1440
- this._app.log.info('Mongo connection reestablished')
1430
+ this.#connection.on('reconnected', () => {
1431
+ this.#app.log.info('Mongo connection reestablished')
1441
1432
  })
1442
- this._connection.on('disconnected', () => {
1443
- this._app.log.info('Mongo connection disconnected')
1444
- this._app.log.info('Trying to reconnected to Mongo...')
1433
+ this.#connection.on('disconnected', () => {
1434
+ this.#app.log.info('Mongo connection disconnected')
1435
+ this.#app.log.info('Trying to reconnected to Mongo...')
1445
1436
  setTimeout(() => {
1446
1437
  mongoose.connect(process.env.MONGO_URI as string, {
1447
1438
  ...connection,
@@ -1450,20 +1441,20 @@ class Server {
1450
1441
  })
1451
1442
  }, 3000)
1452
1443
  })
1453
- this._connection.on('close', () => {
1454
- this._app.log.info('Mongo connection closed')
1444
+ this.#connection.on('close', () => {
1445
+ this.#app.log.info('Mongo connection closed')
1455
1446
  })
1456
- this._connection.on('error', (e: Error) => {
1457
- this._app.log.info('Mongo connection error:')
1458
- this._app.log.error(e)
1447
+ this.#connection.on('error', (e: Error) => {
1448
+ this.#app.log.info('Mongo connection error:')
1449
+ this.#app.log.error(e)
1459
1450
  })
1460
1451
  await mongoose.connect(process.env.MONGO_URI as string, connection)
1461
1452
  }
1462
1453
 
1463
1454
  public async start(): Promise<void> {
1464
1455
  try {
1465
- await this._app.listen(PORT)
1466
- this._mongo()
1456
+ await this.#app.listen(PORT)
1457
+ this.#mongo()
1467
1458
  } catch (e) {
1468
1459
  console.error(e)
1469
1460
  }
@@ -1811,7 +1802,6 @@ ${projectName}/src/@types/custom`
1811
1802
 
1812
1803
  const createFoldersCommands = `mkdir ${projectName}/src \
1813
1804
  ${projectName}/src/@types \
1814
- ${projectName}/src/@types/dto \
1815
1805
  ${projectName}/src/@types/models \
1816
1806
  ${projectName}/src/database \
1817
1807
  ${projectName}/src/database/mongo \
@@ -1834,9 +1824,6 @@ ${fastify ? '' : `${expressFolders}`}
1834
1824
  // /@types
1835
1825
  await writeFile(data['@types'].index.file, data['@types'].index.content)
1836
1826
 
1837
- // /@types/dto
1838
- await writeFile(data['@types/dto'].user.file, data['@types/dto'].user.content)
1839
-
1840
1827
  // /@types/models
1841
1828
  await writeFile(
1842
1829
  data['@types/models'].user.file,
package/lib/src/index.js CHANGED
@@ -57,13 +57,13 @@ module.exports = async ({
57
57
  cliProgress.Presets.shades_classic
58
58
  )
59
59
 
60
- const expressProdPackages = 'express morgan swagger-ui-express'
61
- const fastifyProdPackages = 'fastify fastify-swagger'
60
+ const expressProdPackages = 'express morgan swagger-ui-express cors'
61
+ const fastifyProdPackages = 'fastify fastify-swagger fastify-cors'
62
62
  const prodPackages = `${manager} http-errors mongoose @sinclair/typebox ajv@^6 ${
63
63
  fastify ? fastifyProdPackages : expressProdPackages
64
64
  }`
65
65
 
66
- const expressDevPackages = `@types/express @types/morgan @types/swagger-ui-express`
66
+ const expressDevPackages = `@types/express @types/morgan @types/swagger-ui-express @types/cors`
67
67
  const fastifyDevPackages = ''
68
68
  const devPackages = `${manager} -D \
69
69
  @types/http-errors \
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonylzq/simba.js",
3
- "version": "4.0.0",
3
+ "version": "4.2.0",
4
4
  "description": "set up a modern backend app by running one command",
5
5
  "main": "lib/index.js",
6
6
  "directories": {