jimpex 7.0.1 → 8.0.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 (388) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/README.md +446 -434
  4. package/dist/app/index.d.ts +13 -0
  5. package/dist/app/index.js +19 -0
  6. package/dist/app/index.js.map +1 -0
  7. package/dist/app/jimpex.d.ts +13 -0
  8. package/dist/app/jimpex.js +440 -0
  9. package/dist/app/jimpex.js.map +1 -0
  10. package/dist/controllers/common/config.d.ts +78 -0
  11. package/dist/controllers/common/config.js +89 -0
  12. package/dist/controllers/common/config.js.map +1 -0
  13. package/dist/controllers/common/health.d.ts +82 -0
  14. package/dist/controllers/common/health.js +97 -0
  15. package/dist/controllers/common/health.js.map +1 -0
  16. package/dist/controllers/common/index.d.ts +21 -0
  17. package/dist/controllers/common/index.js +21 -0
  18. package/dist/controllers/common/index.js.map +1 -0
  19. package/dist/controllers/common/statics.d.ts +215 -0
  20. package/dist/controllers/common/statics.js +202 -0
  21. package/dist/controllers/common/statics.js.map +1 -0
  22. package/dist/controllers/index.d.ts +24 -0
  23. package/dist/controllers/index.js +20 -0
  24. package/dist/controllers/index.js.map +1 -0
  25. package/dist/controllers/utils/gateway.d.ts +724 -0
  26. package/dist/controllers/utils/gateway.js +425 -0
  27. package/dist/controllers/utils/gateway.js.map +1 -0
  28. package/dist/controllers/utils/index.d.ts +16 -0
  29. package/dist/controllers/utils/index.js +19 -0
  30. package/dist/controllers/utils/index.js.map +1 -0
  31. package/dist/esm/app/index.js +2 -0
  32. package/dist/esm/app/index.js.map +1 -0
  33. package/dist/esm/app/jimpex.js +415 -0
  34. package/dist/esm/app/jimpex.js.map +1 -0
  35. package/dist/esm/chunk-T2T6Q22Z.js +11 -0
  36. package/dist/esm/chunk-T2T6Q22Z.js.map +1 -0
  37. package/dist/esm/controllers/common/config.js +62 -0
  38. package/dist/esm/controllers/common/config.js.map +1 -0
  39. package/dist/esm/controllers/common/health.js +70 -0
  40. package/dist/esm/controllers/common/health.js.map +1 -0
  41. package/dist/esm/controllers/common/index.js +4 -0
  42. package/dist/esm/controllers/common/index.js.map +1 -0
  43. package/dist/esm/controllers/common/statics.js +173 -0
  44. package/dist/esm/controllers/common/statics.js.map +1 -0
  45. package/dist/esm/controllers/index.js +3 -0
  46. package/dist/esm/controllers/index.js.map +1 -0
  47. package/dist/esm/controllers/utils/gateway.js +404 -0
  48. package/dist/esm/controllers/utils/gateway.js.map +1 -0
  49. package/dist/esm/controllers/utils/index.js +2 -0
  50. package/dist/esm/controllers/utils/index.js.map +1 -0
  51. package/dist/esm/index.js +7 -0
  52. package/dist/esm/index.js.map +1 -0
  53. package/dist/esm/middlewares/common/errorHandler.js +92 -0
  54. package/dist/esm/middlewares/common/errorHandler.js.map +1 -0
  55. package/dist/esm/middlewares/common/forceHTTPS.js +41 -0
  56. package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -0
  57. package/dist/esm/middlewares/common/hsts.js +53 -0
  58. package/dist/esm/middlewares/common/hsts.js.map +1 -0
  59. package/dist/esm/middlewares/common/index.js +4 -0
  60. package/dist/esm/middlewares/common/index.js.map +1 -0
  61. package/dist/esm/middlewares/html/fastHTML.js +104 -0
  62. package/dist/esm/middlewares/html/fastHTML.js.map +1 -0
  63. package/dist/esm/middlewares/html/index.js +3 -0
  64. package/dist/esm/middlewares/html/index.js.map +1 -0
  65. package/dist/esm/middlewares/html/showHTML.js +68 -0
  66. package/dist/esm/middlewares/html/showHTML.js.map +1 -0
  67. package/dist/esm/middlewares/index.js +4 -0
  68. package/dist/esm/middlewares/index.js.map +1 -0
  69. package/dist/esm/middlewares/utils/index.js +2 -0
  70. package/dist/esm/middlewares/utils/index.js.map +1 -0
  71. package/dist/esm/middlewares/utils/versionValidator.js +101 -0
  72. package/dist/esm/middlewares/utils/versionValidator.js.map +1 -0
  73. package/dist/esm/services/common/appError.js +52 -0
  74. package/dist/esm/services/common/appError.js.map +1 -0
  75. package/dist/esm/services/common/httpError.js +19 -0
  76. package/dist/esm/services/common/httpError.js.map +1 -0
  77. package/dist/esm/services/common/index.js +17 -0
  78. package/dist/esm/services/common/index.js.map +1 -0
  79. package/dist/esm/services/common/sendFile.js +27 -0
  80. package/dist/esm/services/common/sendFile.js.map +1 -0
  81. package/dist/esm/services/frontend/frontendFs.js +38 -0
  82. package/dist/esm/services/frontend/frontendFs.js.map +1 -0
  83. package/dist/esm/services/frontend/index.js +11 -0
  84. package/dist/esm/services/frontend/index.js.map +1 -0
  85. package/dist/esm/services/html/htmlGenerator.js +144 -0
  86. package/dist/esm/services/html/htmlGenerator.js.map +1 -0
  87. package/dist/esm/services/html/index.js +11 -0
  88. package/dist/esm/services/html/index.js.map +1 -0
  89. package/dist/esm/services/http/apiClient.js +71 -0
  90. package/dist/esm/services/http/apiClient.js.map +1 -0
  91. package/dist/esm/services/http/http.js +125 -0
  92. package/dist/esm/services/http/http.js.map +1 -0
  93. package/dist/esm/services/http/index.js +17 -0
  94. package/dist/esm/services/http/index.js.map +1 -0
  95. package/dist/esm/services/http/responsesBuilder.js +105 -0
  96. package/dist/esm/services/http/responsesBuilder.js.map +1 -0
  97. package/dist/esm/services/index.js +6 -0
  98. package/dist/esm/services/index.js.map +1 -0
  99. package/dist/esm/services/utils/ensureBearerToken.js +78 -0
  100. package/dist/esm/services/utils/ensureBearerToken.js.map +1 -0
  101. package/dist/esm/services/utils/index.js +11 -0
  102. package/dist/esm/services/utils/index.js.map +1 -0
  103. package/dist/esm/types/events.js +1 -0
  104. package/dist/esm/types/events.js.map +1 -0
  105. package/dist/esm/types/express.js +1 -0
  106. package/dist/esm/types/express.js.map +1 -0
  107. package/dist/esm/types/http.js +1 -0
  108. package/dist/esm/types/http.js.map +1 -0
  109. package/dist/esm/types/index.js +7 -0
  110. package/dist/esm/types/index.js.map +1 -0
  111. package/dist/esm/types/options.js +1 -0
  112. package/dist/esm/types/options.js.map +1 -0
  113. package/dist/esm/types/utils.js +1 -0
  114. package/dist/esm/types/utils.js.map +1 -0
  115. package/dist/esm/types/wootils.js +1 -0
  116. package/dist/esm/types/wootils.js.map +1 -0
  117. package/dist/esm/utils/fns/index.js +5 -0
  118. package/dist/esm/utils/fns/index.js.map +1 -0
  119. package/dist/esm/utils/fns/others.js +6 -0
  120. package/dist/esm/utils/fns/others.js.map +1 -0
  121. package/dist/esm/utils/fns/routes.js +25 -0
  122. package/dist/esm/utils/fns/routes.js.map +1 -0
  123. package/dist/esm/utils/fns/statuses.js +6 -0
  124. package/dist/esm/utils/fns/statuses.js.map +1 -0
  125. package/dist/esm/utils/fns/text.js +6 -0
  126. package/dist/esm/utils/fns/text.js.map +1 -0
  127. package/dist/esm/utils/index.js +3 -0
  128. package/dist/esm/utils/index.js.map +1 -0
  129. package/dist/esm/utils/wrappers.js +41 -0
  130. package/dist/esm/utils/wrappers.js.map +1 -0
  131. package/dist/index.d.ts +46 -0
  132. package/dist/index.js +24 -0
  133. package/dist/index.js.map +1 -0
  134. package/dist/jimpex-7eaee271.d.ts +1278 -0
  135. package/dist/middlewares/common/errorHandler.d.ts +131 -0
  136. package/dist/middlewares/common/errorHandler.js +119 -0
  137. package/dist/middlewares/common/errorHandler.js.map +1 -0
  138. package/dist/middlewares/common/forceHTTPS.d.ts +68 -0
  139. package/dist/middlewares/common/forceHTTPS.js +68 -0
  140. package/dist/middlewares/common/forceHTTPS.js.map +1 -0
  141. package/dist/middlewares/common/hsts.d.ts +109 -0
  142. package/dist/middlewares/common/hsts.js +80 -0
  143. package/dist/middlewares/common/hsts.js.map +1 -0
  144. package/dist/middlewares/common/index.d.ts +21 -0
  145. package/dist/middlewares/common/index.js +21 -0
  146. package/dist/middlewares/common/index.js.map +1 -0
  147. package/dist/middlewares/html/fastHTML.d.ts +180 -0
  148. package/dist/middlewares/html/fastHTML.js +131 -0
  149. package/dist/middlewares/html/fastHTML.js.map +1 -0
  150. package/dist/middlewares/html/index.d.ts +21 -0
  151. package/dist/middlewares/html/index.js +20 -0
  152. package/dist/middlewares/html/index.js.map +1 -0
  153. package/dist/middlewares/html/showHTML.d.ts +127 -0
  154. package/dist/middlewares/html/showHTML.js +95 -0
  155. package/dist/middlewares/html/showHTML.js.map +1 -0
  156. package/dist/middlewares/index.d.ts +30 -0
  157. package/dist/middlewares/index.js +21 -0
  158. package/dist/middlewares/index.js.map +1 -0
  159. package/dist/middlewares/utils/index.d.ts +19 -0
  160. package/dist/middlewares/utils/index.js +19 -0
  161. package/dist/middlewares/utils/index.js.map +1 -0
  162. package/dist/middlewares/utils/versionValidator.d.ts +214 -0
  163. package/dist/middlewares/utils/versionValidator.js +128 -0
  164. package/dist/middlewares/utils/versionValidator.js.map +1 -0
  165. package/dist/services/common/appError.d.ts +138 -0
  166. package/dist/services/common/appError.js +80 -0
  167. package/dist/services/common/appError.js.map +1 -0
  168. package/dist/services/common/httpError.d.ts +79 -0
  169. package/dist/services/common/httpError.js +44 -0
  170. package/dist/services/common/httpError.js.map +1 -0
  171. package/dist/services/common/index.d.ts +47 -0
  172. package/dist/services/common/index.js +41 -0
  173. package/dist/services/common/index.js.map +1 -0
  174. package/dist/services/common/sendFile.d.ts +102 -0
  175. package/dist/services/common/sendFile.js +51 -0
  176. package/dist/services/common/sendFile.js.map +1 -0
  177. package/dist/services/frontend/frontendFs.d.ts +96 -0
  178. package/dist/services/frontend/frontendFs.js +71 -0
  179. package/dist/services/frontend/frontendFs.js.map +1 -0
  180. package/dist/services/frontend/index.d.ts +40 -0
  181. package/dist/services/frontend/index.js +35 -0
  182. package/dist/services/frontend/index.js.map +1 -0
  183. package/dist/services/html/htmlGenerator.d.ts +237 -0
  184. package/dist/services/html/htmlGenerator.js +171 -0
  185. package/dist/services/html/htmlGenerator.js.map +1 -0
  186. package/dist/services/html/index.d.ts +43 -0
  187. package/dist/services/html/index.js +35 -0
  188. package/dist/services/html/index.js.map +1 -0
  189. package/dist/services/http/apiClient.d.ts +169 -0
  190. package/dist/services/http/apiClient.js +96 -0
  191. package/dist/services/http/apiClient.js.map +1 -0
  192. package/dist/services/http/http.d.ts +175 -0
  193. package/dist/services/http/http.js +158 -0
  194. package/dist/services/http/http.js.map +1 -0
  195. package/dist/services/http/index.d.ts +50 -0
  196. package/dist/services/http/index.js +41 -0
  197. package/dist/services/http/index.js.map +1 -0
  198. package/dist/services/http/responsesBuilder.d.ts +178 -0
  199. package/dist/services/http/responsesBuilder.js +132 -0
  200. package/dist/services/http/responsesBuilder.js.map +1 -0
  201. package/dist/services/index.d.ts +33 -0
  202. package/dist/services/index.js +23 -0
  203. package/dist/services/index.js.map +1 -0
  204. package/dist/services/utils/ensureBearerToken.d.ts +157 -0
  205. package/dist/services/utils/ensureBearerToken.js +105 -0
  206. package/dist/services/utils/ensureBearerToken.js.map +1 -0
  207. package/dist/services/utils/index.d.ts +43 -0
  208. package/dist/services/utils/index.js +35 -0
  209. package/dist/services/utils/index.js.map +1 -0
  210. package/dist/types/events.d.ts +13 -0
  211. package/dist/types/events.js +17 -0
  212. package/dist/types/events.js.map +1 -0
  213. package/dist/types/express.d.ts +10 -0
  214. package/dist/types/express.js +17 -0
  215. package/dist/types/express.js.map +1 -0
  216. package/dist/types/http.d.ts +79 -0
  217. package/dist/types/http.js +17 -0
  218. package/dist/types/http.js.map +1 -0
  219. package/dist/types/index.d.ts +14 -0
  220. package/dist/types/index.js +24 -0
  221. package/dist/types/index.js.map +1 -0
  222. package/dist/types/options.d.ts +13 -0
  223. package/dist/types/options.js +17 -0
  224. package/dist/types/options.js.map +1 -0
  225. package/dist/types/utils.d.ts +48 -0
  226. package/dist/types/utils.js +17 -0
  227. package/dist/types/utils.js.map +1 -0
  228. package/dist/types/wootils.d.ts +4 -0
  229. package/dist/types/wootils.js +17 -0
  230. package/dist/types/wootils.js.map +1 -0
  231. package/dist/utils/fns/index.d.ts +5 -0
  232. package/dist/utils/fns/index.js +22 -0
  233. package/dist/utils/fns/index.js.map +1 -0
  234. package/dist/utils/fns/others.d.ts +17 -0
  235. package/dist/utils/fns/others.js +29 -0
  236. package/dist/utils/fns/others.js.map +1 -0
  237. package/dist/utils/fns/routes.d.ts +39 -0
  238. package/dist/utils/fns/routes.js +51 -0
  239. package/dist/utils/fns/routes.js.map +1 -0
  240. package/dist/utils/fns/statuses.d.ts +45 -0
  241. package/dist/utils/fns/statuses.js +35 -0
  242. package/dist/utils/fns/statuses.js.map +1 -0
  243. package/dist/utils/fns/text.d.ts +9 -0
  244. package/dist/utils/fns/text.js +29 -0
  245. package/dist/utils/fns/text.js.map +1 -0
  246. package/dist/utils/index.d.ts +18 -0
  247. package/dist/utils/index.js +20 -0
  248. package/dist/utils/index.js.map +1 -0
  249. package/dist/utils/wrappers.d.ts +13 -0
  250. package/dist/utils/wrappers.js +68 -0
  251. package/dist/utils/wrappers.js.map +1 -0
  252. package/examples/basic/404.html +12 -0
  253. package/examples/basic/README.md +30 -0
  254. package/examples/basic/app.ts +18 -0
  255. package/examples/basic/controller.ts +45 -0
  256. package/examples/basic/index.ts +19 -0
  257. package/examples/basic/middleware.ts +13 -0
  258. package/examples/basic/service.ts +11 -0
  259. package/package.json +59 -38
  260. package/src/app/index.ts +1 -0
  261. package/src/app/jimpex.ts +743 -0
  262. package/src/controllers/common/config.ts +115 -0
  263. package/src/controllers/common/health.ts +128 -0
  264. package/src/controllers/common/index.ts +3 -0
  265. package/src/controllers/common/statics.ts +380 -0
  266. package/src/controllers/index.ts +2 -0
  267. package/src/controllers/utils/gateway.ts +1186 -0
  268. package/src/controllers/utils/index.ts +1 -0
  269. package/src/index.ts +6 -0
  270. package/src/middlewares/common/errorHandler.ts +203 -0
  271. package/src/middlewares/common/forceHTTPS.ts +83 -0
  272. package/src/middlewares/common/hsts.ts +135 -0
  273. package/src/middlewares/common/index.ts +3 -0
  274. package/src/middlewares/html/fastHTML.ts +255 -0
  275. package/src/middlewares/html/index.ts +2 -0
  276. package/src/middlewares/html/showHTML.ts +165 -0
  277. package/src/middlewares/index.ts +3 -0
  278. package/src/middlewares/utils/index.ts +1 -0
  279. package/src/middlewares/utils/versionValidator.ts +289 -0
  280. package/src/services/common/appError.ts +158 -0
  281. package/src/services/common/httpError.ts +74 -0
  282. package/src/services/common/index.ts +29 -0
  283. package/src/services/common/sendFile.ts +106 -0
  284. package/src/services/frontend/frontendFs.ts +101 -0
  285. package/src/services/frontend/index.ts +21 -0
  286. package/src/services/html/htmlGenerator.ts +356 -0
  287. package/src/services/html/index.ts +21 -0
  288. package/src/services/http/apiClient.ts +221 -0
  289. package/src/services/http/http.ts +286 -0
  290. package/src/services/http/index.ts +29 -0
  291. package/src/services/http/responsesBuilder.ts +265 -0
  292. package/src/services/index.ts +5 -0
  293. package/src/services/utils/ensureBearerToken.ts +202 -0
  294. package/src/services/utils/index.ts +21 -0
  295. package/src/types/events.ts +303 -0
  296. package/src/types/express.ts +21 -0
  297. package/src/types/http.ts +77 -0
  298. package/src/types/index.ts +6 -0
  299. package/src/types/options.ts +248 -0
  300. package/src/types/utils.ts +52 -0
  301. package/src/types/wootils.ts +4 -0
  302. package/src/utils/fns/index.ts +4 -0
  303. package/src/utils/fns/others.ts +15 -0
  304. package/src/utils/fns/routes.ts +64 -0
  305. package/src/utils/fns/statuses.ts +44 -0
  306. package/src/utils/fns/text.ts +8 -0
  307. package/src/utils/index.ts +2 -0
  308. package/src/utils/wrappers.ts +537 -0
  309. package/tsconfig.json +5 -8
  310. package/tsup.config.ts +10 -0
  311. package/src/app/index.js +0 -692
  312. package/src/constants/eventNames.js +0 -48
  313. package/src/constants/index.js +0 -7
  314. package/src/controllers/common/configuration.js +0 -116
  315. package/src/controllers/common/health.js +0 -79
  316. package/src/controllers/common/index.js +0 -7
  317. package/src/controllers/common/statics.js +0 -336
  318. package/src/controllers/index.js +0 -9
  319. package/src/controllers/utils/gateway.js +0 -1039
  320. package/src/controllers/utils/index.js +0 -3
  321. package/src/index.js +0 -30
  322. package/src/middlewares/common/errorHandler.js +0 -185
  323. package/src/middlewares/common/forceHTTPS.js +0 -80
  324. package/src/middlewares/common/hsts.js +0 -122
  325. package/src/middlewares/common/index.js +0 -7
  326. package/src/middlewares/html/fastHTML.js +0 -298
  327. package/src/middlewares/html/index.js +0 -5
  328. package/src/middlewares/html/showHTML.js +0 -167
  329. package/src/middlewares/index.js +0 -11
  330. package/src/middlewares/utils/index.js +0 -3
  331. package/src/middlewares/utils/versionValidator.js +0 -261
  332. package/src/services/common/appError.js +0 -136
  333. package/src/services/common/httpError.js +0 -60
  334. package/src/services/common/index.js +0 -25
  335. package/src/services/common/sendFile.js +0 -68
  336. package/src/services/frontend/frontendFs.js +0 -85
  337. package/src/services/frontend/index.js +0 -17
  338. package/src/services/html/htmlGenerator.js +0 -391
  339. package/src/services/html/index.js +0 -17
  340. package/src/services/http/apiClient.js +0 -148
  341. package/src/services/http/http.js +0 -256
  342. package/src/services/http/index.js +0 -25
  343. package/src/services/http/responsesBuilder.js +0 -193
  344. package/src/services/index.js +0 -15
  345. package/src/services/utils/ensureBearerToken.js +0 -147
  346. package/src/services/utils/index.js +0 -19
  347. package/src/types.js +0 -377
  348. package/src/utils/functions.js +0 -78
  349. package/src/utils/wrappers.js +0 -131
  350. package/types/app/index.d.ts +0 -417
  351. package/types/constants/eventNames.d.ts +0 -93
  352. package/types/constants/index.d.ts +0 -2
  353. package/types/controllers/common/configuration.d.ts +0 -71
  354. package/types/controllers/common/health.d.ts +0 -55
  355. package/types/controllers/common/index.d.ts +0 -4
  356. package/types/controllers/common/statics.d.ts +0 -271
  357. package/types/controllers/index.d.ts +0 -3
  358. package/types/controllers/utils/gateway.d.ts +0 -947
  359. package/types/controllers/utils/index.d.ts +0 -2
  360. package/types/index.d.ts +0 -15
  361. package/types/middlewares/common/errorHandler.d.ts +0 -143
  362. package/types/middlewares/common/forceHTTPS.d.ts +0 -64
  363. package/types/middlewares/common/hsts.d.ts +0 -111
  364. package/types/middlewares/common/index.d.ts +0 -4
  365. package/types/middlewares/html/fastHTML.d.ts +0 -238
  366. package/types/middlewares/html/index.d.ts +0 -3
  367. package/types/middlewares/html/showHTML.d.ts +0 -128
  368. package/types/middlewares/index.d.ts +0 -4
  369. package/types/middlewares/utils/index.d.ts +0 -2
  370. package/types/middlewares/utils/versionValidator.d.ts +0 -247
  371. package/types/services/common/appError.d.ts +0 -89
  372. package/types/services/common/httpError.d.ts +0 -37
  373. package/types/services/common/index.d.ts +0 -18
  374. package/types/services/common/sendFile.d.ts +0 -56
  375. package/types/services/frontend/frontendFs.d.ts +0 -72
  376. package/types/services/frontend/index.d.ts +0 -3
  377. package/types/services/html/htmlGenerator.d.ts +0 -298
  378. package/types/services/html/index.d.ts +0 -3
  379. package/types/services/http/apiClient.d.ts +0 -141
  380. package/types/services/http/http.d.ts +0 -159
  381. package/types/services/http/index.d.ts +0 -18
  382. package/types/services/http/responsesBuilder.d.ts +0 -140
  383. package/types/services/index.d.ts +0 -6
  384. package/types/services/utils/ensureBearerToken.d.ts +0 -137
  385. package/types/services/utils/index.d.ts +0 -16
  386. package/types/types.d.ts +0 -280
  387. package/types/utils/functions.d.ts +0 -55
  388. package/types/utils/wrappers.d.ts +0 -127
@@ -0,0 +1 @@
1
+ export * from './gateway';
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './app';
2
+ export * from './types';
3
+ export * from './controllers';
4
+ export * from './middlewares';
5
+ export * from './services';
6
+ export * from './utils';
@@ -0,0 +1,203 @@
1
+ import { deepAssignWithOverwrite } from '@homer0/deep-assign';
2
+ import { middlewareCreator, type Statuses } from '../../utils';
3
+ import type { DeepPartial, Logger, ExpressErrorHandler } from '../../types';
4
+ import { AppError, type HTTPErrorClass, type ResponsesBuilder } from '../../services';
5
+ /**
6
+ * The options for the responses the middleware will create.
7
+ *
8
+ * @group Middlewares/ErrorHandler
9
+ */
10
+ export type ErrorHandlerResponseOptions = {
11
+ /**
12
+ * The message to show in case `showErrors` is set to `false`.
13
+ *
14
+ * @default 'Oops! Something went wrong, please try again'
15
+ * @prettierignore
16
+ */
17
+ message: string;
18
+ /**
19
+ * The default status code for the responses.
20
+ *
21
+ * @default 500
22
+ */
23
+ status: number;
24
+ };
25
+ /**
26
+ * The customization options for the middleware.
27
+ *
28
+ * @group Middlewares/ErrorHandler
29
+ */
30
+ export type ErrorHandlerOptions = {
31
+ /**
32
+ * If `false`, unknown errors will show a generic message instead of real message. And
33
+ * if `true`, it will not only show all kind of errors but it will also show the error
34
+ * stack.
35
+ *
36
+ * By "uknown errors", it means that are not `AppError` nor `HTTPError`.
37
+ */
38
+ showErrors: boolean;
39
+ /**
40
+ * The options for the default response the middleware will create.
41
+ */
42
+ response: ErrorHandlerResponseOptions;
43
+ };
44
+ /**
45
+ * A partial version of the {@link ErrorHandlerOptions}, to be used in the constructor and
46
+ * the middleware creator.
47
+ *
48
+ * @group Middlewares/ErrorHandler
49
+ */
50
+ export type ErrorHandlerPartialOptions = DeepPartial<ErrorHandlerOptions>;
51
+ /**
52
+ * The options to construct a {@link ErrorHandler}.
53
+ *
54
+ * @group Middlewares/ErrorHandler
55
+ */
56
+ export type ErrorHandlerConstructorOptions = ErrorHandlerPartialOptions & {
57
+ /**
58
+ * A dictionary with the dependencies to inject.
59
+ */
60
+ inject: {
61
+ logger: Logger;
62
+ responsesBuilder: ResponsesBuilder;
63
+ statuses: Statuses;
64
+ HTTPError: HTTPErrorClass;
65
+ };
66
+ };
67
+ /**
68
+ * Creates a middleware that handles errors and generates the responses.
69
+ *
70
+ * @group Middleware Classes
71
+ * @group Middlewares/ErrorHandler
72
+ * @prettierignore
73
+ */
74
+ export class ErrorHandler {
75
+ /**
76
+ * The service that will log the messages in the console.
77
+ */
78
+ protected readonly _logger: Logger;
79
+ /**
80
+ * The service to generate the responses.
81
+ */
82
+ protected readonly _responsesBuilder: ResponsesBuilder;
83
+ /**
84
+ * The uility service to get HTTP status codes.
85
+ */
86
+ protected readonly _statuses: Statuses;
87
+ /**
88
+ * The Error class used by the "known errors".
89
+ */
90
+ protected readonly _HTTPError: HTTPErrorClass;
91
+ /**
92
+ * The customization options.
93
+ */
94
+ protected readonly _options: ErrorHandlerOptions;
95
+ /**
96
+ * @param options The options to construct the class.
97
+ */
98
+ constructor({
99
+ inject: { logger, responsesBuilder, statuses, HTTPError },
100
+ ...options
101
+ }: ErrorHandlerConstructorOptions) {
102
+ this._logger = logger;
103
+ this._responsesBuilder = responsesBuilder;
104
+ this._statuses = statuses;
105
+ this._HTTPError = HTTPError;
106
+ this._options = deepAssignWithOverwrite(
107
+ {
108
+ showErrors: false,
109
+ response: {
110
+ message: 'Unexpected error',
111
+ status: this._statuses('internal server error'),
112
+ },
113
+ },
114
+ options,
115
+ );
116
+ }
117
+ /**
118
+ * Generates the middleware that handles the errors.
119
+ */
120
+ getMiddleware(): ExpressErrorHandler {
121
+ return (err, _, res, next) => {
122
+ // If there wasn't any error, continue the execution.
123
+ if (!err) {
124
+ next();
125
+ return;
126
+ }
127
+
128
+ const { response, showErrors } = this._options;
129
+
130
+ // Define the base status and response.
131
+ let { status } = response;
132
+ let data: {
133
+ error: boolean;
134
+ message: string;
135
+ stack?: string[];
136
+ } & Record<string, unknown> = {
137
+ error: true,
138
+ message: response.message,
139
+ };
140
+
141
+ const knownError = err instanceof AppError;
142
+ // If it's a "known error" or the `showErrors` flag is set to `true`...
143
+ if (showErrors || knownError) {
144
+ // Get the real message.
145
+ data.message = err.message;
146
+ // If it's a "known error"...
147
+ if (knownError) {
148
+ // Try to extract information for the response.
149
+ data = {
150
+ ...data,
151
+ ...err.getResponse(),
152
+ };
153
+ status = err.status || (this._statuses('bad request') as number);
154
+ }
155
+ /**
156
+ * If the flag is set to `true`, and it's a "valid error", try to extract the
157
+ * stack, format it, and include it in the response.
158
+ */
159
+ if (showErrors && err instanceof Error && err.stack) {
160
+ const stack = err.stack.split('\n').map((line) => line.trim());
161
+ data.stack = stack;
162
+ stack.splice(0, 1);
163
+ this._logger.error(`ERROR: ${err.message}`);
164
+ this._logger.info(stack);
165
+ }
166
+ }
167
+
168
+ this._responsesBuilder.json({
169
+ res,
170
+ data,
171
+ status,
172
+ });
173
+ };
174
+ }
175
+ /**
176
+ * The handler customization options.
177
+ */
178
+ get options(): Readonly<ErrorHandlerOptions> {
179
+ return deepAssignWithOverwrite({}, this._options);
180
+ }
181
+ }
182
+ /**
183
+ * Creates the middleware that handles errors.
184
+ *
185
+ * @group Middlewares
186
+ * @group Middlewares/ErrorHandler
187
+ */
188
+ export const errorHandlerMiddleware = middlewareCreator(
189
+ (options: ErrorHandlerPartialOptions = {}) =>
190
+ (app) => {
191
+ const showErrors = app.getConfig<boolean | undefined>('debug.showErrors') === true;
192
+ return new ErrorHandler({
193
+ inject: {
194
+ logger: app.get('logger'),
195
+ responsesBuilder: app.get('responsesBuilder'),
196
+ statuses: app.get('statuses'),
197
+ HTTPError: app.get('HTTPError'),
198
+ },
199
+ showErrors,
200
+ ...options,
201
+ }).getMiddleware();
202
+ },
203
+ );
@@ -0,0 +1,83 @@
1
+ import { middlewareCreator } from '../../utils';
2
+ import { ExpressMiddleware } from '../../types';
3
+ /**
4
+ * The customization options for the middleware.
5
+ *
6
+ * @group Middlewares/ForceHTTPS
7
+ */
8
+ export type ForceHTTPSOptions = {
9
+ /**
10
+ * A list of regular expressions to match routes that should be ignored.
11
+ *
12
+ * @default [/^\/service\//]
13
+ */
14
+ ignoredRoutes: RegExp[];
15
+ };
16
+ /**
17
+ * A partial version of the {@link ForceHTTPSOptions}, to be used in the constructor and
18
+ * the middleware creator.
19
+ *
20
+ * @group Middlewares/ForceHTTPS
21
+ */
22
+ export type ForceHTTPSPartialOptions = Partial<ForceHTTPSOptions>;
23
+ /**
24
+ * Creates a middleware that forces all the traffic to be through HTTPS.
25
+ *
26
+ * @group Middleware Classes
27
+ * @group Middlewares/ForceHTTPS
28
+ * @prettierignore
29
+ */
30
+ export class ForceHTTPS {
31
+ /**
32
+ * The customization options.
33
+ */
34
+ protected readonly _options: ForceHTTPSOptions;
35
+ /**
36
+ * @param options The options to construct the class.
37
+ */
38
+ constructor(options: ForceHTTPSPartialOptions = {}) {
39
+ this._options = {
40
+ ignoredRoutes: [/^\/service\//],
41
+ ...options,
42
+ };
43
+ }
44
+ /**
45
+ * Generates the middleware that redirects the traffic.
46
+ */
47
+ getMiddleware(): ExpressMiddleware {
48
+ return (req, res, next) => {
49
+ if (
50
+ !req.secure &&
51
+ req.get('X-Forwarded-Proto') !== 'https' &&
52
+ !this._options.ignoredRoutes.some((expression) =>
53
+ expression.test(req.originalUrl),
54
+ )
55
+ ) {
56
+ const host = req.get('Host');
57
+ res.redirect(`https://${host}${req.url}`);
58
+ } else {
59
+ next();
60
+ }
61
+ };
62
+ }
63
+ /**
64
+ * The customization options.
65
+ */
66
+ get options(): Readonly<ForceHTTPSOptions> {
67
+ return { ...this._options };
68
+ }
69
+ }
70
+ /**
71
+ * Creates the middleware that redirects the traffic to HTTPS.
72
+ *
73
+ * @group Middlewares
74
+ * @group Middlewares/ForceHTTPS
75
+ */
76
+ export const forceHTTPSMiddleware = middlewareCreator(
77
+ (options: ForceHTTPSPartialOptions = {}) =>
78
+ (app) => {
79
+ const enabled = app.getConfig<boolean | undefined>('forceHTTPS');
80
+ if (!enabled) return undefined;
81
+ return new ForceHTTPS(options).getMiddleware();
82
+ },
83
+ );
@@ -0,0 +1,135 @@
1
+ import { middlewareCreator } from '../../utils';
2
+ import { ExpressMiddleware } from '../../types';
3
+ /**
4
+ * The options to customize the HSTS header value.
5
+ *
6
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security}
7
+ * @group Middlewares/HSTS
8
+ */
9
+ export type HSTSMiddlewareOptions = {
10
+ /**
11
+ * The time, in seconds, that the browser should remember that a site is only to be
12
+ * accessed using HTTPS.
13
+ *
14
+ * @default 31536000
15
+ */
16
+ maxAge: number;
17
+ /**
18
+ * Whether or not the rule should apply to all sub domains.
19
+ *
20
+ * @default true
21
+ */
22
+ includeSubDomains: boolean;
23
+ /**
24
+ * Whether or not to include on the major browsers'
25
+ * preload list. This directive is not part of the specification, for more information
26
+ * about it, you should check the MDN documentation for the header.
27
+ *
28
+ * @default false
29
+ */
30
+ preload: boolean;
31
+ };
32
+ /**
33
+ * A partial version of the {@link HSTSMiddlewareOptions}, to be used in the constructor
34
+ * and the middleware creator.
35
+ *
36
+ * @group Middlewares/HSTS
37
+ */
38
+ export type HSTSMiddlewarePartialOptions = Partial<HSTSMiddlewareOptions>;
39
+ /**
40
+ * The options for the middleware creator that will create the middleware, or not,
41
+ * depending on the `enabled` option.
42
+ *
43
+ * @group Middlewares/HSTS
44
+ */
45
+ export type HSTSMiddlewareSettings = HSTSMiddlewarePartialOptions & {
46
+ /**
47
+ * If it's not `true`, it won't return the middleware.
48
+ */
49
+ enabled?: boolean;
50
+ };
51
+ /**
52
+ * Generates a middleware that includes the HSTS header on the responses.
53
+ *
54
+ * @see {@link https://tools.ietf.org/html/rfc6797}
55
+ * @group Middleware Classes
56
+ * @group Middlewares/HSTS
57
+ * @prettierignore
58
+ */
59
+ export class HSTS {
60
+ /**
61
+ * The customization options for the header.
62
+ */
63
+ protected readonly _options: HSTSMiddlewareOptions;
64
+ /**
65
+ * The value of the header that will be included in the responses.
66
+ */
67
+ protected readonly _header: string;
68
+ /**
69
+ * @param options The options to construct the class.
70
+ */
71
+ constructor(options: HSTSMiddlewarePartialOptions = {}) {
72
+ this._options = {
73
+ maxAge: 31536000,
74
+ includeSubDomains: true,
75
+ preload: false,
76
+ ...options,
77
+ };
78
+ this._header = this._buildHeader();
79
+ }
80
+ /**
81
+ * Generates the middleware that includes the HSTS header on the responses.
82
+ */
83
+ getMiddleware(): ExpressMiddleware {
84
+ return (_, res, next) => {
85
+ res.setHeader('Strict-Transport-Security', this._header);
86
+ next();
87
+ };
88
+ }
89
+ /**
90
+ * The customization options.
91
+ */
92
+ get options(): Readonly<HSTSMiddlewareOptions> {
93
+ return { ...this._options };
94
+ }
95
+ /**
96
+ * The value of the header that will be included in the responses.
97
+ */
98
+ get header(): string {
99
+ return this._header;
100
+ }
101
+ /**
102
+ * Creates the header value based on the customization options.
103
+ */
104
+ protected _buildHeader(): string {
105
+ const { maxAge, includeSubDomains, preload } = this._options;
106
+ const directives = [`max-age=${maxAge}`];
107
+ if (includeSubDomains) {
108
+ directives.push('includeSubDomains');
109
+ }
110
+
111
+ if (preload) {
112
+ directives.push('preload');
113
+ }
114
+
115
+ return directives.join('; ');
116
+ }
117
+ }
118
+ /**
119
+ * Creates the middleware that includes the HSTS header on the responses.
120
+ *
121
+ * @group Middlewares
122
+ * @group Middlewares/HSTS
123
+ */
124
+ export const hstsMiddleware = middlewareCreator(
125
+ (options: HSTSMiddlewareSettings = {}) =>
126
+ (app) => {
127
+ const setting = app.getConfig<HSTSMiddlewareSettings | undefined>('hsts');
128
+ if (
129
+ !setting?.enabled ||
130
+ (typeof options.enabled !== 'undefined' && !options.enabled)
131
+ )
132
+ return undefined;
133
+ return new HSTS(options).getMiddleware();
134
+ },
135
+ );
@@ -0,0 +1,3 @@
1
+ export * from './errorHandler';
2
+ export * from './forceHTTPS';
3
+ export * from './hsts';
@@ -0,0 +1,255 @@
1
+ import { createRouteExpression, middlewareCreator, removeSlashes } from '../../utils';
2
+ import type { HTMLGenerator, SendFile } from '../../services';
3
+ import type { AsyncExpressMiddleware, Response, NextFunction, Events } from '../../types';
4
+ /**
5
+ * The options to customize the behavior of the middleware.
6
+ *
7
+ * @group Middlewares/FastHTML
8
+ */
9
+ export type FastHTMLOptions = {
10
+ /**
11
+ * The name of the file the middleware will serve. If the {@link HTMLGenerator} service
12
+ * is available, it will be overriden by the service.
13
+ *
14
+ * @default 'index.html'
15
+ */
16
+ file: string;
17
+ /**
18
+ * A list of regular expressions to match request paths that should be ignored by the
19
+ * middleware.
20
+ *
21
+ * @default [/\.ico$/i]
22
+ */
23
+ ignoredRoutes: RegExp[];
24
+ /**
25
+ * If `true`, {@link FastHTML} will get the list of all the routes controlled by the
26
+ * application, and will use them to validate the incoming requests (in addition to the
27
+ * `ignore` list): If a request URL doesn't match with any of the "controlled routes",
28
+ * it will serve the HTML file.
29
+ *
30
+ * @default true
31
+ */
32
+ useAppRoutes: boolean;
33
+ };
34
+ /**
35
+ * The options to construct a {@link FastHTML}.
36
+ *
37
+ * @group Middlewares/FastHTML
38
+ */
39
+ export type FastHTMLConstructorOptions = Partial<FastHTMLOptions> & {
40
+ /**
41
+ * A dictionary with the dependencies to inject.
42
+ */
43
+ inject: {
44
+ sendFile: SendFile;
45
+ events: Events;
46
+ /**
47
+ * A function to get a possible {@link HTMLGenerator}. This is injected as a "getter"
48
+ * to not interrupt the DIC "lifecycle": middlewares are initialized right when the
49
+ * app starts, and injecting a reference would force the service to be initialized
50
+ * too, even if a request is not being made.
51
+ */
52
+ getHTMLGenerator?: () => HTMLGenerator | undefined;
53
+ };
54
+ };
55
+ /**
56
+ * The options for the middleware creator that will mount an instance of {@link FastHTML}.
57
+ *
58
+ * @group Middlewares/FastHTML
59
+ */
60
+ export type FastHTMLMiddlewareOptions = Partial<FastHTMLOptions> & {
61
+ /**
62
+ * The name of an {@link HTMLGenerator} service already available in the application.
63
+ *
64
+ * @default 'htmlGenerator'
65
+ */
66
+ htmlGeneratorServiceName?: string;
67
+ };
68
+ /**
69
+ * It's common for an app to show an HTML view when no route was able to handle a request,
70
+ * so the idea behind this middleware is to avoid going to every middleware and controller
71
+ * and just specify that if the request is not for a route handled by a controller, just
72
+ * serve the HTML and avoid processing unnecessary data.
73
+ *
74
+ * A simple example: The app has a route `/backend` that a frontend uses to get
75
+ * information.
76
+ * This middleware can be used to only allow the execution of middlewares and controllers
77
+ * when the request route is for `/backend`.
78
+ *
79
+ * **Disclaimer**: Managing statics files with Express is not a best practice, but there
80
+ * are scenarios where there is not other choice.
81
+ *
82
+ * @group Middleware Classes
83
+ * @group Middlewares/FastHTML
84
+ * @prettierignore
85
+ */
86
+ export class FastHTML {
87
+ /**
88
+ * The service that serves a file.
89
+ */
90
+ protected readonly _sendFile: SendFile;
91
+ /**
92
+ * The application envent bus, to listen and get the list of "controlled routes" after
93
+ * the application is started.
94
+ */
95
+ protected readonly _events: Events;
96
+ /**
97
+ * A function to get a possible {@link HTMLGenerator}. This is injected as a "getter"
98
+ * to not interrupt the DIC "lifecycle": middlewares are initialized right when the
99
+ * app starts, and injecting a reference would force the service to be initialized
100
+ * too, even if a request is not being made.
101
+ */
102
+ protected readonly _getHTMLGenerator: () => HTMLGenerator | undefined;
103
+ /**
104
+ * The customization options for the middleware.
105
+ */
106
+ protected _options: FastHTMLOptions;
107
+ /**
108
+ * Whether or not the file is ready to be served. In case the middleware uses an
109
+ * {@link HTMLGenerator} service, the file needs to be generated before being available,
110
+ * and that's why this flag exists.
111
+ */
112
+ protected _fileReady: boolean = false;
113
+ /**
114
+ * A list of regular expression that match the routes controlled by the application.
115
+ * This is in case the `useAppRoutes` option is set to `true`; when the application gets
116
+ * started, an event listener will obtain all the top controlled routes, create regular
117
+ * expressions, and save them on this property.
118
+ */
119
+ protected _routeExpressions: RegExp[] = [];
120
+ /**
121
+ * @param options The options to construct the class.
122
+ * @throws If `ignoredRoutes` is empty and `useAppRoutes` is set to `false`.
123
+ */
124
+ constructor({ inject, ...options }: FastHTMLConstructorOptions) {
125
+ this._sendFile = inject.sendFile;
126
+ this._events = inject.events;
127
+ this._getHTMLGenerator = inject.getHTMLGenerator || (() => undefined);
128
+ this._options = {
129
+ file: 'index.html',
130
+ ignoredRoutes: [/\.ico$/i],
131
+ useAppRoutes: true,
132
+ ...options,
133
+ };
134
+
135
+ if (!this._options.ignoredRoutes.length && !this._options.useAppRoutes) {
136
+ throw new Error('You must provide either `ignoredRoutes` or `useAppRoutes`');
137
+ }
138
+
139
+ if (this._options.useAppRoutes) {
140
+ this._setupEvents();
141
+ }
142
+ }
143
+ /**
144
+ * Generates the middleware that serves the HTML file.
145
+ */
146
+ getMiddleware(): AsyncExpressMiddleware {
147
+ return async (req, res, next) => {
148
+ // If the route should be ignored, move to the next middleware.
149
+ if (this._shouldIgnoreRoute(req.originalUrl)) {
150
+ next();
151
+ return;
152
+ }
153
+
154
+ // If the file is ready to be served, serve it.
155
+ if (this._fileReady) {
156
+ this._sendResponse(res, next);
157
+ return;
158
+ }
159
+
160
+ const htmlGenerator = this._getHTMLGenerator();
161
+ // If there's no generator, switch the flag and just serve the file.
162
+ if (!htmlGenerator) {
163
+ this._fileReady = true;
164
+ this._sendResponse(res, next);
165
+ return;
166
+ }
167
+
168
+ try {
169
+ // Wait for the HTML to be generated.
170
+ await htmlGenerator.whenReady();
171
+ // Update the local option.
172
+ this._options.file = htmlGenerator.options.file;
173
+ // Switch the flag and serve the file.
174
+ this._fileReady = true;
175
+ this._sendResponse(res, next);
176
+ } catch (error) {
177
+ next(error);
178
+ }
179
+ };
180
+ }
181
+ /**
182
+ * The customization options.
183
+ */
184
+ get options(): Readonly<FastHTMLOptions> {
185
+ return { ...this._options };
186
+ }
187
+ /**
188
+ * Serves the HTML file to the response.
189
+ *
190
+ * @param res The response object generated by the application.
191
+ * @param next The function to call the next middleware.
192
+ */
193
+ protected _sendResponse(res: Response, next: NextFunction): void {
194
+ res.setHeader('Content-Type', 'text/html');
195
+ this._sendFile({
196
+ res,
197
+ next,
198
+ filepath: this._options.file,
199
+ });
200
+ }
201
+ /**
202
+ * Adds the event listener that obtains the list of "controlled routes" when
203
+ * `useAppRoutes` is set to `true`.
204
+ */
205
+ protected _setupEvents() {
206
+ this._events.once('afterStart', ({ app }) => {
207
+ const [routeExpressions] = app.routes.reduce<[RegExp[], string[]]>(
208
+ (acc, route) => {
209
+ const [expressions, processed] = acc;
210
+ const clean = removeSlashes(route).trim();
211
+ if (!clean || processed.includes(clean)) return acc;
212
+ expressions.push(createRouteExpression(clean));
213
+ processed.push(clean);
214
+ return acc;
215
+ },
216
+ [[], []],
217
+ );
218
+ this._routeExpressions.push(...routeExpressions);
219
+ });
220
+ }
221
+ /**
222
+ * Validates whether a route should be ignored or not. The method checks first against
223
+ * the `ignore` option, and then against the list of "controlled routes" if
224
+ * `useAppRoutes` is set to `true`.
225
+ *
226
+ * @param route The route to validate.
227
+ */
228
+ protected _shouldIgnoreRoute(route: string): boolean {
229
+ return (
230
+ this._options.ignoredRoutes.some((expression) => route.match(expression)) ||
231
+ this._routeExpressions.some((expression) => route.match(expression))
232
+ );
233
+ }
234
+ }
235
+ /**
236
+ * Creates the middleware that filters the routes and serves an HTML before the
237
+ * application gets to evaluate whether there's a controller for the request or not.
238
+ *
239
+ * @group Middlewares
240
+ * @group Middlewares/FastHTML
241
+ */
242
+ export const fastHTMLMiddleware = middlewareCreator(
243
+ (options: FastHTMLMiddlewareOptions = {}) =>
244
+ (app) => {
245
+ const { htmlGeneratorServiceName = 'htmlGenerator', ...rest } = options;
246
+ return new FastHTML({
247
+ inject: {
248
+ events: app.get('events'),
249
+ sendFile: app.get('sendFile'),
250
+ getHTMLGenerator: () => app.try(htmlGeneratorServiceName),
251
+ },
252
+ ...rest,
253
+ }).getMiddleware();
254
+ },
255
+ );
@@ -0,0 +1,2 @@
1
+ export * from './fastHTML';
2
+ export * from './showHTML';