exp-node-server 1.1.7 → 2.1.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.
Files changed (183) hide show
  1. package/README.md +162 -20
  2. package/dist/cli.js +613 -0
  3. package/package.json +29 -9
  4. package/templates/mongoose/.dockerignore +10 -0
  5. package/templates/mongoose/.env.example +56 -7
  6. package/templates/mongoose/.prettierignore +4 -0
  7. package/templates/mongoose/.prettierrc +7 -0
  8. package/templates/mongoose/Dockerfile +33 -0
  9. package/templates/mongoose/_gitignore +35 -0
  10. package/templates/mongoose/docker-compose.yml +21 -0
  11. package/templates/mongoose/eslint.config.js +35 -0
  12. package/templates/mongoose/package-lock.json +6366 -0
  13. package/templates/mongoose/package.json +49 -11
  14. package/templates/mongoose/readme.md +88 -0
  15. package/templates/mongoose/src/app/configs/index.ts +178 -26
  16. package/templates/mongoose/src/app/errors/zodError.ts +16 -16
  17. package/templates/mongoose/src/app/lib/db.ts +18 -0
  18. package/templates/mongoose/src/app/lib/logger.ts +12 -0
  19. package/templates/mongoose/src/app/middlewares/auth.ts +48 -44
  20. package/templates/mongoose/src/app/middlewares/global_error_handler.ts +59 -65
  21. package/templates/mongoose/src/app/middlewares/not_found_api.ts +7 -7
  22. package/templates/mongoose/src/app/middlewares/rate_limiter.ts +153 -0
  23. package/templates/mongoose/src/app/middlewares/request_validator.ts +11 -10
  24. package/templates/mongoose/src/app/middlewares/uploader.ts +17 -13
  25. package/templates/mongoose/src/app/modules/account/account.controller.ts +156 -0
  26. package/templates/mongoose/src/app/modules/account/account.interface.ts +65 -0
  27. package/templates/mongoose/src/app/modules/account/account.route.ts +101 -0
  28. package/templates/mongoose/src/app/modules/account/account.schema.ts +97 -0
  29. package/templates/mongoose/src/app/modules/account/account.service.ts +422 -0
  30. package/templates/mongoose/src/app/modules/account/account.swagger.ts +278 -0
  31. package/templates/mongoose/src/app/modules/account/account.validation.ts +63 -0
  32. package/templates/mongoose/src/app/modules/profile/profile.controller.ts +17 -0
  33. package/templates/mongoose/src/app/modules/profile/profile.interface.ts +7 -0
  34. package/templates/mongoose/src/app/modules/profile/profile.route.ts +28 -0
  35. package/templates/mongoose/src/app/modules/profile/profile.schema.ts +13 -0
  36. package/templates/mongoose/src/app/modules/profile/profile.service.ts +28 -0
  37. package/templates/mongoose/src/app/modules/profile/profile.swagger.ts +34 -0
  38. package/templates/mongoose/src/app/modules/profile/profile.validation.ts +8 -0
  39. package/templates/mongoose/src/app/passport-js/index.ts +87 -0
  40. package/templates/mongoose/src/app/passport-js/oauth-state.ts +150 -0
  41. package/templates/mongoose/src/app/passport-js/providers.ts +258 -0
  42. package/templates/mongoose/src/app/passport-js/social-account.ts +141 -0
  43. package/templates/mongoose/src/app/queues/agenda.ts +36 -0
  44. package/templates/mongoose/src/app/queues/email.queue.ts +81 -0
  45. package/templates/mongoose/src/app/queues/index.ts +17 -0
  46. package/templates/mongoose/src/app/templates/otpEmailTemplate.ts +185 -0
  47. package/templates/mongoose/src/app/templates/resetPasswordEmailTemplate.ts +157 -0
  48. package/templates/mongoose/src/app/types/error.d.ts +7 -7
  49. package/templates/mongoose/src/app/types/express.d.ts +11 -0
  50. package/templates/mongoose/src/app/types/passport-oauth1.d.ts +51 -0
  51. package/templates/mongoose/src/app/utils/JWT.ts +38 -18
  52. package/templates/mongoose/src/app/utils/app_error.ts +10 -10
  53. package/templates/mongoose/src/app/utils/auth_cookie.ts +27 -0
  54. package/templates/mongoose/src/app/utils/catch_async.ts +8 -8
  55. package/templates/mongoose/src/app/utils/cloudinary.ts +26 -59
  56. package/templates/mongoose/src/app/utils/mail_sender.ts +26 -92
  57. package/templates/mongoose/src/app/utils/manage_response.ts +19 -19
  58. package/templates/mongoose/src/app/utils/otp.ts +20 -0
  59. package/templates/mongoose/src/app/utils/session.ts +55 -0
  60. package/templates/mongoose/src/app.ts +73 -42
  61. package/templates/mongoose/src/routes.ts +13 -13
  62. package/templates/mongoose/src/server.ts +38 -18
  63. package/templates/mongoose/src/swaggerOptions.ts +23 -23
  64. package/templates/mongoose/tests/app.test.ts +97 -0
  65. package/templates/mongoose/tests/global-setup.ts +15 -0
  66. package/templates/mongoose/tests/helpers/fixtures.ts +42 -0
  67. package/templates/mongoose/tests/queues/email.queue.test.ts +137 -0
  68. package/templates/mongoose/tests/routes/account.test.ts +614 -0
  69. package/templates/mongoose/tests/routes/profile.test.ts +95 -0
  70. package/templates/mongoose/tests/routes/social.test.ts +310 -0
  71. package/templates/mongoose/tests/setup.ts +25 -0
  72. package/templates/mongoose/tsconfig.json +16 -20
  73. package/templates/mongoose/vitest.config.ts +43 -0
  74. package/templates/prisma/.dockerignore +2 -0
  75. package/templates/prisma/.env.example +62 -6
  76. package/templates/prisma/.prettierignore +4 -0
  77. package/templates/prisma/.prettierrc +7 -0
  78. package/templates/prisma/.projectrc.json +1 -1
  79. package/templates/prisma/Dockerfile +21 -7
  80. package/templates/prisma/_gitignore +38 -0
  81. package/templates/prisma/docker-compose.yml +8 -7
  82. package/templates/prisma/eslint.config.js +35 -0
  83. package/templates/prisma/package-lock.json +7316 -0
  84. package/templates/prisma/package.json +47 -10
  85. package/templates/prisma/prisma/schema/account.schema.prisma +92 -10
  86. package/templates/prisma/prisma/schema/profile.schema.prisma +8 -3
  87. package/templates/prisma/prisma/schema/schema.prisma +1 -2
  88. package/templates/prisma/prisma.config.ts +5 -5
  89. package/templates/prisma/readme.md +90 -0
  90. package/templates/prisma/src/app/configs/index.ts +175 -26
  91. package/templates/prisma/src/app/errors/zodError.ts +16 -16
  92. package/templates/prisma/src/app/lib/logger.ts +12 -0
  93. package/templates/prisma/src/app/lib/prisma.ts +4 -6
  94. package/templates/prisma/src/app/middlewares/auth.ts +37 -16
  95. package/templates/prisma/src/app/middlewares/global_error_handler.ts +42 -35
  96. package/templates/prisma/src/app/middlewares/not_found_api.ts +7 -7
  97. package/templates/prisma/src/app/middlewares/rate_limiter.ts +124 -0
  98. package/templates/prisma/src/app/middlewares/request_validator.ts +4 -3
  99. package/templates/prisma/src/app/middlewares/uploader.ts +17 -13
  100. package/templates/prisma/src/app/modules/account/account.controller.ts +74 -34
  101. package/templates/prisma/src/app/modules/account/account.route.ts +68 -22
  102. package/templates/prisma/src/app/modules/account/account.service.ts +329 -309
  103. package/templates/prisma/src/app/modules/account/account.swagger.ts +167 -76
  104. package/templates/prisma/src/app/modules/account/account.validation.ts +41 -19
  105. package/templates/prisma/src/app/modules/profile/profile.controller.ts +4 -4
  106. package/templates/prisma/src/app/modules/profile/profile.route.ts +18 -12
  107. package/templates/prisma/src/app/modules/profile/profile.service.ts +11 -18
  108. package/templates/prisma/src/app/modules/profile/profile.swagger.ts +11 -11
  109. package/templates/prisma/src/app/modules/profile/profile.validation.ts +2 -2
  110. package/templates/prisma/src/app/passport-js/index.ts +87 -0
  111. package/templates/prisma/src/app/passport-js/oauth-state.ts +150 -0
  112. package/templates/prisma/src/app/passport-js/providers.ts +263 -0
  113. package/templates/prisma/src/app/passport-js/social-account.ts +147 -0
  114. package/templates/prisma/src/app/queues/boss.ts +33 -0
  115. package/templates/prisma/src/app/queues/email.queue.ts +71 -0
  116. package/templates/prisma/src/app/queues/index.ts +23 -0
  117. package/templates/prisma/src/app/queues/maintenance.queue.ts +30 -0
  118. package/templates/prisma/src/app/templates/otpEmailTemplate.ts +185 -0
  119. package/templates/prisma/src/app/templates/resetPasswordEmailTemplate.ts +157 -0
  120. package/templates/prisma/src/app/types/error.d.ts +7 -7
  121. package/templates/prisma/src/app/types/express.d.ts +11 -0
  122. package/templates/prisma/src/app/types/passport-oauth1.d.ts +51 -0
  123. package/templates/prisma/src/app/utils/JWT.ts +36 -17
  124. package/templates/prisma/src/app/utils/app_error.ts +10 -10
  125. package/templates/prisma/src/app/utils/auth_cookie.ts +27 -0
  126. package/templates/prisma/src/app/utils/catch_async.ts +8 -8
  127. package/templates/prisma/src/app/utils/cloudinary.ts +26 -59
  128. package/templates/prisma/src/app/utils/mail_sender.ts +26 -92
  129. package/templates/prisma/src/app/utils/manage_response.ts +19 -19
  130. package/templates/prisma/src/app/utils/otp.ts +20 -0
  131. package/templates/prisma/src/app/utils/session.ts +57 -0
  132. package/templates/prisma/src/app.ts +73 -42
  133. package/templates/prisma/src/routes.ts +12 -10
  134. package/templates/prisma/src/server.ts +37 -19
  135. package/templates/prisma/src/swaggerOptions.ts +22 -26
  136. package/templates/prisma/tests/app.test.ts +86 -0
  137. package/templates/prisma/tests/helpers/fixtures.ts +35 -0
  138. package/templates/prisma/tests/helpers/prisma.ts +32 -0
  139. package/templates/prisma/tests/queues/email.queue.test.ts +162 -0
  140. package/templates/prisma/tests/routes/account.test.ts +627 -0
  141. package/templates/prisma/tests/routes/profile.test.ts +91 -0
  142. package/templates/prisma/tests/routes/social.test.ts +317 -0
  143. package/templates/prisma/tests/setup.ts +10 -0
  144. package/templates/prisma/tsconfig.json +16 -23
  145. package/templates/prisma/vitest.config.ts +38 -0
  146. package/bin/cli.js +0 -41
  147. package/src/createProject.js +0 -61
  148. package/src/generateModule.js +0 -93
  149. package/src/utils/getProjectType.js +0 -13
  150. package/src/utils/mongoose/controllerGenerator.js +0 -70
  151. package/src/utils/mongoose/routeGenerator.js +0 -27
  152. package/src/utils/mongoose/schemaGenerator.js +0 -11
  153. package/src/utils/mongoose/serviceGenerator.js +0 -49
  154. package/src/utils/mongoose/swaggerGenerator.js +0 -112
  155. package/src/utils/mongoose/validationGenerator.js +0 -13
  156. package/src/utils/prisma/prismaSchemaGenerator.js +0 -10
  157. package/src/utils/prisma/prismaServiceGenerator.js +0 -47
  158. package/src/utils/routeInjector.js +0 -40
  159. package/src/utils/swaggerInjector.js +0 -52
  160. package/src/utils/validateModuleName.js +0 -17
  161. package/src/utils/validateProjectName.js +0 -15
  162. package/src/utils.js +0 -26
  163. package/templates/mongoose/src/app/errors/castError.ts +0 -23
  164. package/templates/mongoose/src/app/errors/duplicateError.ts +0 -26
  165. package/templates/mongoose/src/app/errors/validationError.ts +0 -25
  166. package/templates/mongoose/src/app/modules/auth/auth.controller.ts +0 -127
  167. package/templates/mongoose/src/app/modules/auth/auth.interface.ts +0 -24
  168. package/templates/mongoose/src/app/modules/auth/auth.route.ts +0 -46
  169. package/templates/mongoose/src/app/modules/auth/auth.schema.ts +0 -19
  170. package/templates/mongoose/src/app/modules/auth/auth.service.ts +0 -324
  171. package/templates/mongoose/src/app/modules/auth/auth.swagger.ts +0 -214
  172. package/templates/mongoose/src/app/modules/auth/auth.validation.ts +0 -37
  173. package/templates/mongoose/src/app/modules/user/user.controller.ts +0 -17
  174. package/templates/mongoose/src/app/modules/user/user.interface.ts +0 -15
  175. package/templates/mongoose/src/app/modules/user/user.route.ts +0 -20
  176. package/templates/mongoose/src/app/modules/user/user.schema.ts +0 -22
  177. package/templates/mongoose/src/app/modules/user/user.service.ts +0 -22
  178. package/templates/mongoose/src/app/modules/user/user.swagger.ts +0 -75
  179. package/templates/mongoose/src/app/modules/user/user.validation.ts +0 -18
  180. package/templates/mongoose/src/app/types/index.d.ts +0 -12
  181. package/templates/mongoose/src/app/utils/isAccountExist.ts +0 -31
  182. package/templates/prisma/src/app/types/index.d.ts +0 -12
  183. package/templates/prisma/src/app/utils/otpGenerator.ts +0 -29
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![npm downloads](https://img.shields.io/npm/dw/exp-node-server.svg)](https://www.npmjs.com/package/exp-node-server)
5
5
  [![npm total downloads](https://img.shields.io/npm/dt/exp-node-server.svg)](https://www.npmjs.com/package/exp-node-server)
6
6
  [![Made with TypeScript](https://img.shields.io/badge/Made%20with-TypeScript-blue.svg)](https://www.typescriptlang.org/)
7
- [![Node.js](https://img.shields.io/badge/Node.js-18+-green.svg)](https://nodejs.org/)
7
+ [![Node.js](https://img.shields.io/badge/Node.js-20.17+-green.svg)](https://nodejs.org/)
8
8
 
9
9
  ---
10
10
 
@@ -18,7 +18,6 @@ This tool helps developers quickly scaffold `clean`, `modular` `Express` APIs wi
18
18
 
19
19
  - ⚡ Generate complete `Express` + `TypeScript` modules
20
20
  - 🧩 Built-in `Mongoose` / `Prisma` support
21
- - 📘 Adds Swagger documentation automatically
22
21
  - 📘 Automatic `Swagger` documentation
23
22
  - 🔐 `Zod` validation ready
24
23
  - 🏗️ `Modular` clean architecture
@@ -26,23 +25,82 @@ This tool helps developers quickly scaffold `clean`, `modular` `Express` APIs wi
26
25
  - 🔄 Add modules anytime
27
26
  - 📦 Zero boilerplate setup
28
27
 
28
+ ## 📋 Requirements
29
+
30
+ - Node.js **20.17+** to run the CLI; generated projects need Node.js **22.12+**
31
+ - Docker (or your own database): PostgreSQL for Prisma, MongoDB for Mongoose
32
+
33
+ ## 📥 Installation
34
+
35
+ No install needed: `npx` always runs the latest version (recommended).
36
+
37
+ ```bash
38
+ npx exp-node-server --help
39
+ ```
40
+
41
+ Or install it globally to use `exp-node-server` directly:
42
+
43
+ ```bash
44
+ npm install -g exp-node-server
45
+ exp-node-server --version
46
+ ```
47
+
29
48
  ## 📦 Quick Start
30
49
 
31
50
  Run directly using npx (recommended):
32
51
 
33
- ```Bash
52
+ ```bash
34
53
  npx exp-node-server -c my-api
35
54
  ```
36
55
 
37
- This will:
56
+ You'll be asked to pick a template (Mongoose or Prisma). To skip the prompt:
38
57
 
39
- - Create an Express starter project
40
- - Install dependencies
41
- - Prepare the project for development
58
+ ```bash
59
+ npx exp-node-server create my-api --template prisma
60
+ ```
61
+
62
+ Then:
63
+
64
+ ```bash
65
+ cd my-api
66
+ npm install
67
+ cp .env.example .env # fill in the secrets (openssl rand -hex 32)
68
+ docker compose up -d # starts PostgreSQL / MongoDB
69
+ npm run db:migrate # Prisma only
70
+ npm run dev # http://localhost:5000, docs at /docs
71
+ npm test
72
+ ```
73
+
74
+ ### What you get
75
+
76
+ ```text
77
+ my-api/
78
+ ├── src/
79
+ │ ├── app.ts # express app: helmet, cors, rate limit, routes
80
+ │ ├── server.ts # connects the database, starts queues, graceful shutdown
81
+ │ ├── routes.ts # module routers (generated modules are added here)
82
+ │ ├── swaggerOptions.ts # API docs (generated modules are added here)
83
+ │ └── app/
84
+ │ ├── configs/ # env vars, validated with zod on startup
85
+ │ ├── middlewares/ # auth, validation, rate limiting, uploads, errors
86
+ │ ├── modules/
87
+ │ │ ├── account/ # sign up/in, OTP, refresh tokens, password reset
88
+ │ │ └── profile/ # profile + photo upload (Cloudinary)
89
+ │ ├── passport-js/ # social login (Google, Facebook, GitHub, X, LinkedIn, Discord)
90
+ │ ├── queues/ # background email jobs (pg-boss / Agenda, no Redis)
91
+ │ ├── templates/ # email templates
92
+ │ └── utils/
93
+ ├── tests/
94
+ │ ├── routes/ # supertest suites, one per module
95
+ │ └── helpers/
96
+ ├── prisma/schema/ # Prisma only: one .prisma file per model
97
+ ├── docker-compose.yml
98
+ └── .env.example
99
+ ```
42
100
 
43
101
  ## 🧩 Generate a Module
44
102
 
45
- Inside your project run:
103
+ Inside your project root run:
46
104
 
47
105
  ```bash
48
106
  npx exp-node-server -g <module-name>
@@ -52,33 +110,100 @@ Example: `npx exp-node-server -g order`
52
110
 
53
111
  Output:
54
112
 
55
- ```Bash
56
- ✔ order.interface.ts created
57
- ✔ order.schema.ts created
58
- ✔ order.validation.ts created
59
- ✔ order.route.ts created
60
- ✔ order.controller.ts created
61
- ✔ order.service.ts created
62
- ✔ order.swagger.ts created
113
+ ```bash
114
+ ✔ src/app/modules/order/order.controller.ts created
115
+ ✔ src/app/modules/order/order.route.ts created
116
+ ✔ src/app/modules/order/order.validation.ts created
117
+ ✔ src/app/modules/order/order.swagger.ts created
118
+ ✔ src/app/modules/order/order.interface.ts created
119
+ ✔ src/app/modules/order/order.schema.ts created
120
+ ✔ src/app/modules/order/order.service.ts created
121
+ ✔ tests/routes/order.test.ts created
122
+ ✔ Route registered in routes.ts
123
+ ✔ Swagger docs registered in swaggerOptions.ts
124
+ ```
125
+
126
+ The route and Swagger docs are wired up automatically. For Prisma projects a
127
+ `prisma/schema/<module>.prisma` model is created as well.
63
128
 
64
- 🔗 Route registered in routes.ts
65
- 📘 Swagger docs registered
129
+ A ready-to-run [supertest](https://github.com/ladjs/supertest) suite is created in
130
+ `tests/routes/<module>.test.ts`. It covers create, list, get, update and delete,
131
+ including the 404s, and passes as generated:
66
132
 
133
+ ```bash
134
+ npm test -- tests/routes/order.test.ts
135
+ ```
136
+
137
+ Generated modules start with an example `name` field, from the validation to the
138
+ database schema and the tests, so data flows end to end right away. Rename it or add
139
+ your fields in the same places, and update `createPayload` / `updatePayload` at the
140
+ top of the test. These are starter tests: extend them with your business rules.
141
+
142
+ ### Protected routes
143
+
144
+ Generated routes are **public** by default. To require a signed-in user on every
145
+ route:
146
+
147
+ ```bash
148
+ npx exp-node-server -g order --auth
149
+ ```
150
+
151
+ The generated test then also checks that anonymous requests get a 401. To limit a
152
+ route to admins, change `auth()` to `auth('ADMIN')` in `order.route.ts`.
153
+ Mongoose tests run against an in-memory MongoDB; Prisma tests use the Prisma mock in
154
+ `tests/helpers/prisma.ts`. Module names whose path is already taken (e.g. `auth`) are
155
+ refused.
156
+
157
+ ## 🛠️ CLI Reference
158
+
159
+ ```text
160
+ npx exp-node-server create <project-name> [--template <mongoose|prisma>]
161
+ npx exp-node-server generate <module-name> [--auth] [--force]
162
+
163
+ Commands
164
+ create, -c <name> Create a new project from a template
165
+ generate, -g <name> Generate a module in the current project
166
+
167
+ Options
168
+ -t, --template <name> Template to use with create (mongoose | prisma)
169
+ -a, --auth Generated routes require a signed-in user (default: public)
170
+ -f, --force Overwrite an existing module when generating
171
+ -h, --help Show help
172
+ -v, --version Show version
67
173
  ```
68
174
 
175
+ - Project names are lowercased and spaces become dashes (`My API` → `my-api`).
176
+ - Module names are lowercased and spaces/dashes become underscores (`order-item` → `order_item`).
177
+
178
+ ### Safety
179
+
180
+ - **All or nothing:** a module is generated in one step. If anything fails (e.g. a
181
+ file can't be written), the project is left exactly as it was.
182
+ - **`--force` keeps backups:** files it overwrites are copied first to
183
+ `.exp-node-server/backups/<timestamp>/` (ignored by git, ESLint and Prettier).
184
+ Files that wouldn't change are left alone.
185
+ - **No clashes:** a module name whose URL is already used by another router (e.g.
186
+ `auth`) is refused.
187
+ - **Validated input only:** generated services receive `req.body` after
188
+ `RequestValidator` parsed it with the module's zod schema, so unknown fields are
189
+ dropped. Payload types come from that schema (`z.infer`).
190
+
69
191
  ## 📁 Generated Module Structure
70
192
 
71
193
  Each module follows a clean structure:
72
194
 
73
195
  ```Bash
74
196
  src/app/modules/<module-name>/
75
- ├── <module>.interface.ts
76
- ├── <module>.schema.ts
197
+ ├── <module>.interface.ts # mongoose
198
+ ├── <module>.schema.ts # mongoose (prisma: prisma/schema/<module>.prisma)
77
199
  ├── <module>.validation.ts
78
200
  ├── <module>.route.ts
79
201
  ├── <module>.controller.ts
80
202
  ├── <module>.service.ts
81
203
  └── <module>.swagger.ts
204
+
205
+ tests/routes/
206
+ └── <module>.test.ts
82
207
  ```
83
208
 
84
209
  ## ⚙️ Run the Project
@@ -103,6 +228,22 @@ http://localhost:5000/docs
103
228
  - JWT — Authentication
104
229
  - Swagger — API documentation
105
230
 
231
+ ## 🧑‍🔧 Development
232
+
233
+ ```bash
234
+ npm install
235
+ npm run dev # rebuild on change (tsup --watch)
236
+ npm run typecheck # tsc
237
+ npm test # vitest (unit + end-to-end against dist/)
238
+ npm run build # bundle to dist/cli.js
239
+ ```
240
+
241
+ Try the local build with `node dist/cli.js --help`, or run `npm link` to use
242
+ `exp-node-server` globally.
243
+
244
+ > Templates live in `templates/`. npm strips `.gitignore` files when
245
+ > publishing, so name them `_gitignore`. They're renamed when a project is created.
246
+
106
247
  ## 👨‍💻 Author
107
248
 
108
249
  ### Abumahid
@@ -124,3 +265,4 @@ https://linkedin.com/in/md-abu-mahid-islam
124
265
  If you find this project helpful, consider giving it a ⭐ on `GitHub`.
125
266
 
126
267
  It helps the project grow and reach more developers.
268
+