jimpex 7.0.2 → 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 +80 -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,115 @@
1
+ import { controller } from '../../utils';
2
+ import type { ResponsesBuilder } from '../../services';
3
+ import type {
4
+ Config,
5
+ Response,
6
+ AsyncExpressMiddleware,
7
+ ExpressMiddleware,
8
+ } from '../../types';
9
+ /**
10
+ * The options to contruct a {@link ConfigController}.
11
+ *
12
+ * @group Controllers/Config
13
+ */
14
+ export type ConfigControllerOptions = {
15
+ /**
16
+ * A dictionary with the dependencies to inject.
17
+ */
18
+ inject: {
19
+ responsesBuilder: ResponsesBuilder;
20
+ config: Config;
21
+ };
22
+ };
23
+ /**
24
+ * The controller class that allows to show and switch the application configuration.
25
+ *
26
+ * @group Controller Classes
27
+ * @group Controllers/Config
28
+ * @prettierignore
29
+ */
30
+ export class ConfigController {
31
+ /**
32
+ * The service in charge or building the responses.
33
+ */
34
+ protected readonly _responsesBuilder: ResponsesBuilder;
35
+ /**
36
+ * The service in charge of the configuration.
37
+ */
38
+ protected readonly _config: Config;
39
+ /**
40
+ * @param options The options to construct the controller.
41
+ */
42
+ constructor({ inject }: ConfigControllerOptions) {
43
+ this._responsesBuilder = inject.responsesBuilder;
44
+ this._config = inject.config;
45
+ }
46
+ /**
47
+ * Creates the middleware the shows the current configuration.
48
+ */
49
+ showConfig(): ExpressMiddleware {
50
+ return (_, res) => {
51
+ this.respondWithConfig(res);
52
+ };
53
+ }
54
+ /**
55
+ * Creates the middleware the allows to switch the configuration.
56
+ */
57
+ switchConfig(): AsyncExpressMiddleware {
58
+ return async (req, res, next) => {
59
+ const { name } = req.params;
60
+ if (!name || !this._config.canSwitchConfigs()) {
61
+ next();
62
+ return;
63
+ }
64
+
65
+ try {
66
+ await this._config.switch(name);
67
+ this.respondWithConfig(res);
68
+ } catch (error) {
69
+ next(error);
70
+ }
71
+ };
72
+ }
73
+ /**
74
+ * Utility to respond with the current configuration.
75
+ */
76
+ protected respondWithConfig(res: Response): void {
77
+ const name = this._config.get<string>('name');
78
+ const data = {
79
+ name,
80
+ ...this._config.getConfig<Record<string, unknown>>(),
81
+ };
82
+
83
+ this._responsesBuilder.json({
84
+ res,
85
+ data,
86
+ });
87
+ }
88
+ }
89
+ /**
90
+ * This controller is kind of special as it will only mount the routes if the
91
+ * `debug.configController` setting of the app configuration is set to `true`.
92
+ *
93
+ * It provides routes for:
94
+ * - `/`: Showing the current configuration.
95
+ * - `/switch/:name`: Switching the configuration, but only if the service allows it.
96
+ *
97
+ * @group Controllers
98
+ * @group Controllers/Config
99
+ */
100
+ export const configController = controller((app) => {
101
+ const config = app.getConfig();
102
+ const router = app.getRouter();
103
+ if (config.get<boolean | undefined>('debug.configController') !== true) {
104
+ return router;
105
+ }
106
+
107
+ const ctrl = new ConfigController({
108
+ inject: {
109
+ config,
110
+ responsesBuilder: app.get('responsesBuilder'),
111
+ },
112
+ });
113
+
114
+ return router.get('/', ctrl.showConfig()).get('/switch/:name', ctrl.switchConfig());
115
+ });
@@ -0,0 +1,128 @@
1
+ import { controller, type Statuses } from '../../utils';
2
+ import type { ResponsesBuilder } from '../../services';
3
+ import type { JimpexHealthStatus, Config, AsyncExpressMiddleware } from '../../types';
4
+ /**
5
+ * A function that will return the health status of the application.
6
+ *
7
+ * @group Controllers/Health
8
+ */
9
+ export type GetHealthStatus = () => Promise<JimpexHealthStatus>;
10
+ /**
11
+ * The options to contruct a {@link HealthController}.
12
+ *
13
+ * @group Controllers/Health
14
+ */
15
+ export type HealthControllerOptions = {
16
+ /**
17
+ * A dictionary with the dependencies to inject.
18
+ */
19
+ inject: {
20
+ getHealthStatus: GetHealthStatus;
21
+ responsesBuilder: ResponsesBuilder;
22
+ config: Config;
23
+ statuses: Statuses;
24
+ };
25
+ };
26
+ /**
27
+ * The controller class that shows the application health status.
28
+ *
29
+ * @group Controller Classes
30
+ * @group Controllers/Health
31
+ * @prettierignore
32
+ */
33
+ export class HealthController {
34
+ /**
35
+ * The function that returns the health status of the application.
36
+ */
37
+ protected readonly _getHealthStatus: GetHealthStatus;
38
+ /**
39
+ * The service in charge or building the responses.
40
+ */
41
+ protected readonly _responsesBuilder: ResponsesBuilder;
42
+ /**
43
+ * The service in charge of the configuration.
44
+ */
45
+ protected readonly _config: Config;
46
+ /**
47
+ * The uility service to get HTTP status codes.
48
+ */
49
+ protected readonly _statuses: Statuses;
50
+ /**
51
+ * @param options The options to construct the controller.
52
+ */
53
+ constructor({ inject }: HealthControllerOptions) {
54
+ this._getHealthStatus = inject.getHealthStatus;
55
+ this._responsesBuilder = inject.responsesBuilder;
56
+ this._config = inject.config;
57
+ this._statuses = inject.statuses;
58
+ }
59
+ /**
60
+ * Creates the middleware the shows the application health status.
61
+ */
62
+ showHealth(): AsyncExpressMiddleware {
63
+ return async (_, res) => {
64
+ const healthStatus = await this._getHealthStatus();
65
+ let isHealthy: boolean;
66
+ let extras: Record<string, unknown>;
67
+ if (typeof healthStatus === 'boolean') {
68
+ isHealthy = healthStatus;
69
+ extras = {};
70
+ } else {
71
+ if (typeof healthStatus.isHealthy === 'boolean') {
72
+ isHealthy = healthStatus.isHealthy;
73
+ } else if (healthStatus.services) {
74
+ isHealthy = Object.values(healthStatus.services).every(
75
+ (value) => value === true,
76
+ );
77
+ } else {
78
+ isHealthy = true;
79
+ }
80
+
81
+ extras = {
82
+ ...healthStatus.services,
83
+ };
84
+ }
85
+
86
+ const { name: config, version } = this._config.get<{
87
+ name: string;
88
+ version: string;
89
+ }>(['name', 'version']);
90
+
91
+ const status = isHealthy
92
+ ? this._statuses('ok')
93
+ : this._statuses('service unavailable');
94
+
95
+ this._responsesBuilder.json({
96
+ res,
97
+ status,
98
+ data: {
99
+ isHealthy,
100
+ status,
101
+ config,
102
+ version,
103
+ ...extras,
104
+ },
105
+ });
106
+ };
107
+ }
108
+ }
109
+ /**
110
+ * The controller that once mounted, it will add an endpoint to show the application
111
+ * health status.
112
+ *
113
+ * @group Controllers
114
+ * @group Controllers/Health
115
+ */
116
+ export const healthController = controller((app) => {
117
+ const router = app.getRouter();
118
+ const ctrl = new HealthController({
119
+ inject: {
120
+ getHealthStatus: app.isHealthy.bind(app),
121
+ responsesBuilder: app.get('responsesBuilder'),
122
+ config: app.get('config'),
123
+ statuses: app.get('statuses'),
124
+ },
125
+ });
126
+
127
+ return router.get('/', ctrl.showHealth());
128
+ });
@@ -0,0 +1,3 @@
1
+ export * from './config';
2
+ export * from './health';
3
+ export * from './statics';
@@ -0,0 +1,380 @@
1
+ import * as path from 'path';
2
+ import * as mime from 'mime';
3
+ import { deepAssignWithOverwrite } from '@homer0/deep-assign';
4
+ import {
5
+ controllerCreator,
6
+ removeSlashes,
7
+ notUndefined,
8
+ type MiddlewareLike,
9
+ } from '../../utils';
10
+ import type { SendFile } from '../../services';
11
+ import type { Jimpex } from '../../app';
12
+ import type { DeepPartial, ExpressMiddleware, Router, RouterMethod } from '../../types';
13
+ /**
14
+ * The definition for each file the controller handles.
15
+ *
16
+ * @group Controllers/Statics
17
+ */
18
+ export type StaticsControllerFile = {
19
+ /**
20
+ * The route, relative to the controller root, to the file.
21
+ */
22
+ route: string;
23
+ /**
24
+ * The path to the file in the filesystem. Since the file is served using the
25
+ * {@link SendFile} service, whether the file is relative to the project root or the
26
+ * application executable depends on how the service is configured (relative to the
27
+ * executable by default).
28
+ */
29
+ path: string;
30
+ /**
31
+ * A dictionary of headers for the response.
32
+ */
33
+ headers?: Record<string, string>;
34
+ };
35
+ /**
36
+ * These are like "master paths" that get prepended to all the file paths and routes the
37
+ * controller use.
38
+ *
39
+ * @group Controllers/Statics
40
+ */
41
+ export type StaticsControllerPathsOptions = {
42
+ /**
43
+ * A custom route to prefix all the file routes with.
44
+ */
45
+ route: string;
46
+ /**
47
+ * A custom path to prefix all the file paths with.
48
+ */
49
+ source: string;
50
+ };
51
+ /**
52
+ * The options to customize the controller.
53
+ *
54
+ * @group Controllers/Statics
55
+ */
56
+ export type StaticsControllerOptions = {
57
+ /**
58
+ * A list of filenames, or definitions for the files to handle.
59
+ */
60
+ files: Array<string | StaticsControllerFile>;
61
+ /**
62
+ * A dictionary with the allowed router (HTTP) methods the controller can use to serve
63
+ * the files. If `all` is set to `true`, the rest of the values will be ignored.
64
+ *
65
+ * @default {get: true, all: false}
66
+ */
67
+ methods: Partial<Record<RouterMethod, boolean>>;
68
+ /**
69
+ * The "master paths" the controller can use to prefix the file paths and routes.
70
+ *
71
+ * @default {route: '', source: './',}
72
+ */
73
+ paths: StaticsControllerPathsOptions;
74
+ };
75
+ /**
76
+ * The options to construct a {@link StaticsController}.
77
+ *
78
+ * @group Controllers/Statics
79
+ */
80
+ export type StaticsControllerConstructorOptions =
81
+ DeepPartial<StaticsControllerOptions> & {
82
+ /**
83
+ * A dictionary with the dependencies to inject.
84
+ */
85
+ inject: {
86
+ sendFile: SendFile;
87
+ };
88
+ };
89
+ /**
90
+ * A function to generate a list of middlewares that can be executed before the tontroller
91
+ * main middleware.
92
+ *
93
+ * @group Controllers/Statics
94
+ */
95
+ export type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];
96
+ /**
97
+ * The options for the controller creator that mounts {@link StaticsController}.
98
+ *
99
+ * @group Controllers/Statics
100
+ */
101
+ export type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {
102
+ /**
103
+ * A function to generate a list of middlewares that can be executed before the
104
+ * tontroller main middleware.
105
+ */
106
+ getMiddlewares?: StaticsControllerGetMiddlewaresFn;
107
+ };
108
+ /**
109
+ * The options for {@link StaticsController._addRoute}.
110
+ *
111
+ * @access protected
112
+ * @group Controllers/Statics
113
+ */
114
+ export type AddStaticRouteOptions = {
115
+ /**
116
+ * The reference for the router in which the middlewares will be added.
117
+ */
118
+ router: Router;
119
+ /**
120
+ * The router method in which the middlewares will be added.
121
+ */
122
+ method: RouterMethod;
123
+ /**
124
+ * The definition of the file to serve.
125
+ */
126
+ file: StaticsControllerFile;
127
+ /**
128
+ * The middleware created by {@link StaticsController}, that will serve the file.
129
+ */
130
+ fileMiddleware: ExpressMiddleware;
131
+ /**
132
+ * A list of extra middlewares to execute before the file middleware.
133
+ */
134
+ middlewares: ExpressMiddleware[];
135
+ };
136
+
137
+ /**
138
+ * The controller class that allows the application to serve specific files from any
139
+ * folder to any route without the need of mounting directories as "static".
140
+ *
141
+ * @group Controller Classes
142
+ * @group Controllers/Statics
143
+ * @prettierignore
144
+ */
145
+ export class StaticsController {
146
+ /**
147
+ * The service that serves static files.
148
+ */
149
+ protected readonly _sendFile: SendFile;
150
+ /**
151
+ * The controller customization options.
152
+ */
153
+ protected _options: StaticsControllerOptions;
154
+ /**
155
+ * A dictionary with the formatted definitions of the files that will be served.
156
+ * It uses the files' routes as keys, for easy access in the middleware.
157
+ */
158
+ protected files: Record<string, StaticsControllerFile>;
159
+ /**
160
+ * @param options The options to construct the controller.
161
+ */
162
+ constructor({ inject, ...options }: StaticsControllerConstructorOptions) {
163
+ this._sendFile = inject.sendFile;
164
+ this._options = this._validateOptions(
165
+ deepAssignWithOverwrite(
166
+ {
167
+ files: ['favicon.ico', 'index.html'],
168
+ methods: options.methods || {
169
+ all: false,
170
+ get: true,
171
+ },
172
+ paths: {
173
+ route: '',
174
+ source: './',
175
+ },
176
+ },
177
+ options,
178
+ ),
179
+ );
180
+ this.files = this._createFiles();
181
+ }
182
+ /**
183
+ * Mounts the middlewares in the router in order to serve the files.
184
+ *
185
+ * @param router A reference to the application router.
186
+ * @param middlewares A list of extra middlewares to execute before the file
187
+ * middleware.
188
+ */
189
+ addRoutes(router: Router, middlewares: ExpressMiddleware[] = []): Router {
190
+ const { methods } = this._options;
191
+ const use: RouterMethod[] = methods.all
192
+ ? ['all']
193
+ : Object.keys(methods).reduce<RouterMethod[]>((acc, name) => {
194
+ const methodName = name as RouterMethod;
195
+ if (methods[methodName]) {
196
+ acc.push(methodName);
197
+ }
198
+
199
+ return acc;
200
+ }, []);
201
+
202
+ Object.keys(this.files).forEach((route) => {
203
+ const file = this.files[route as keyof typeof this.files]!;
204
+ const fileMiddleware = this._getMiddleware(file);
205
+ use.forEach((method) =>
206
+ this._addRoute({ router, method, file, fileMiddleware, middlewares }),
207
+ );
208
+ });
209
+
210
+ return router;
211
+ }
212
+ /**
213
+ * The controller options.
214
+ */
215
+ get options(): Readonly<StaticsControllerOptions> {
216
+ return { ...this._options };
217
+ }
218
+ /**
219
+ * Generates the middleware that will serve the file.
220
+ *
221
+ * @param file The definition of the file to serve.
222
+ */
223
+ protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware {
224
+ return (_, res, next) => {
225
+ const extension = path.parse(file.path).ext.substring(1);
226
+ const headers = {
227
+ 'Content-Type': mime.getType(extension) || 'text/html',
228
+ ...file.headers,
229
+ };
230
+
231
+ Object.entries(headers).forEach(([key, value]) => {
232
+ res.setHeader(key, value);
233
+ });
234
+
235
+ this._sendFile({
236
+ res,
237
+ filepath: file.path,
238
+ next,
239
+ });
240
+ };
241
+ }
242
+ /**
243
+ * Mounts the middleware(s) for a file in the router.
244
+ *
245
+ * @param options The information of the file and how it needs to be added.
246
+ */
247
+ protected _addRoute({
248
+ router,
249
+ method,
250
+ file,
251
+ fileMiddleware,
252
+ middlewares,
253
+ }: AddStaticRouteOptions): void {
254
+ const { route } = file;
255
+ router[method](route, [...middlewares, fileMiddleware]);
256
+ }
257
+ /**
258
+ * Validates and formats the options sent to the constructor in order to get the final
259
+ * set that will be stored in the controller.
260
+ *
261
+ * @param options The options to validate.
262
+ * @throws If no files are specified.
263
+ * @throws If methods is not defined.
264
+ * @throws If no methods are enabled.
265
+ * @throws If there's an invalid HTTP method.
266
+ */
267
+ protected _validateOptions(
268
+ options: StaticsControllerOptions,
269
+ ): StaticsControllerOptions {
270
+ if (!options.files || !options.files.length) {
271
+ throw new Error('You need to specify a list of files');
272
+ }
273
+
274
+ if (!options.methods) {
275
+ throw new Error('You need to specify which HTTP methods are allowed for the files');
276
+ }
277
+
278
+ const methods = Object.keys(options.methods) as RouterMethod[];
279
+
280
+ const atLeastOne = methods.some((method) => options.methods[method]);
281
+ if (!atLeastOne) {
282
+ throw new Error('You need to enable at least one HTTP method to serve the files');
283
+ }
284
+
285
+ const allowedMethods: RouterMethod[] = [
286
+ 'all',
287
+ 'get',
288
+ 'head',
289
+ 'post',
290
+ 'patch',
291
+ 'put',
292
+ 'delete',
293
+ 'connect',
294
+ 'options',
295
+ 'trace',
296
+ ];
297
+
298
+ const invalid = methods.find(
299
+ (method) => !allowedMethods.includes(method.toLowerCase() as RouterMethod),
300
+ );
301
+
302
+ if (invalid) {
303
+ throw new Error(`${invalid} is not a valid HTTP method`);
304
+ }
305
+
306
+ const newMethods = methods.reduce<Record<string, boolean>>((acc, method) => {
307
+ acc[method.toLowerCase()] = !!options.methods[method];
308
+ return acc;
309
+ }, {});
310
+
311
+ return {
312
+ ...options,
313
+ methods: newMethods,
314
+ };
315
+ }
316
+ /**
317
+ * Parses the files received from the constructor's options, and formats them into
318
+ * proper definitions the controller can use.
319
+ */
320
+ protected _createFiles(): Record<string, StaticsControllerFile> {
321
+ const { files, paths } = this._options;
322
+ const routePath = removeSlashes(paths.route, false, true);
323
+ return files.reduce<Record<string, StaticsControllerFile>>((acc, file) => {
324
+ let src;
325
+ let route;
326
+ let headers;
327
+ if (typeof file === 'object') {
328
+ ({ route, path: src, headers } = file);
329
+ } else {
330
+ src = file;
331
+ route = file;
332
+ }
333
+
334
+ src = path.join(paths.source, src);
335
+ route = removeSlashes(route, true, false);
336
+ route = `${routePath}/${route}`;
337
+ acc[route] = {
338
+ path: src,
339
+ route,
340
+ headers: headers || {},
341
+ };
342
+
343
+ return acc;
344
+ }, {});
345
+ }
346
+ }
347
+ /**
348
+ * A controller that allows the application to server specific files from any folder to
349
+ * any route without the need of mounting directories as "static" folders.
350
+ *
351
+ * @group Controllers
352
+ * @group Controllers/Statics
353
+ */
354
+ export const staticsController = controllerCreator(
355
+ ({ getMiddlewares, ...options }: StaticsControllerCreatorOptions = {}) =>
356
+ (app) => {
357
+ const router = app.getRouter();
358
+ const ctrl = new StaticsController({
359
+ inject: {
360
+ sendFile: app.get('sendFile'),
361
+ },
362
+ ...options,
363
+ });
364
+
365
+ let useMiddlewares: ExpressMiddleware[] | undefined;
366
+ if (getMiddlewares) {
367
+ useMiddlewares = getMiddlewares(app)
368
+ .map((middleware) => {
369
+ if ('middleware' in middleware) {
370
+ return middleware.connect(app) as ExpressMiddleware | undefined;
371
+ }
372
+
373
+ return middleware as ExpressMiddleware;
374
+ })
375
+ .filter(notUndefined);
376
+ }
377
+
378
+ return ctrl.addRoutes(router, useMiddlewares);
379
+ },
380
+ );
@@ -0,0 +1,2 @@
1
+ export * from './common';
2
+ export * from './utils';