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
@@ -1,261 +0,0 @@
1
- const ObjectUtils = require('wootils/shared/objectUtils');
2
- const { code: statuses } = require('statuses');
3
- const { middlewareCreator } = require('../../utils/wrappers');
4
-
5
- /**
6
- * @typedef {import('../../types').ExpressMiddleware} ExpressMiddleware
7
- * @typedef {import('../../types').ExpressRequest} ExpressRequest
8
- * @typedef {import('../../services/http/responsesBuilder').ResponsesBuilder} ResponsesBuilder
9
- * @prettierignore
10
- */
11
-
12
- /**
13
- * @typedef {import('../../types').MiddlewareCreator<O>} MiddlewareCreator<O>
14
- * @template O
15
- */
16
-
17
- /**
18
- * The options for how the middleware should behave if the requested version is `latest`.
19
- *
20
- * @typedef {Object} VersionValidatorLatestOptions
21
- * @property {boolean} allow Whether or not the middleware should validate the _"latest
22
- * version"_.
23
- * Default `true`.
24
- * @property {string} name The name of the _"latest version"_. Basically,
25
- * `req.params.version`
26
- * must match with this property in order to be consider
27
- * "latest".
28
- * Default `'latest'`.
29
- * @parent module:middlewares
30
- */
31
-
32
- /**
33
- * The options for how to detect if the request comes from a popup and how to compose the
34
- * post message the middleware will use to respond.
35
- *
36
- * @typedef {Object} VersionValidatorPopupOptions
37
- * @property {string} variable The name of the query string variable the middleware will
38
- * check in order to indentify whether the request comes from
39
- * a popup or not.
40
- * The variable must have `'true'` as its value. Default
41
- * `'popup'`.
42
- * @property {string} title The title of the page that will be generated to respond in
43
- * case the versions don't match. Default `'Conflict'`.
44
- * @property {string} message The contents of the post message the generated page will
45
- * send if the versions don't match. Default
46
- * `'vesion:conflict'`.
47
- * @parent module:middlewares
48
- */
49
-
50
- /**
51
- * The options used to customize a {@link VersionValidator} instance.
52
- *
53
- * @typedef {Object} VersionValidatorOptions
54
- * @property {string} error The error message to show when the
55
- * version is invalid.
56
- * @property {VersionValidatorLatestOptions} latest The options for how the middleware
57
- * should behave if the requested
58
- * version is `latest`.
59
- * @property {VersionValidatorPopupOptions} popup The options for how to detect if the
60
- * request comes from a popup and how
61
- * to compose the post message the
62
- * middleware will use to respond.
63
- * @property {string | number} version The version used to validate the
64
- * requests.
65
- * On the {@link VersionValidator}
66
- * constructor, if specified via
67
- * parameter,
68
- * the class will take care of
69
- * automatically add it to the options.
70
- * @parent module:middlewares
71
- */
72
-
73
- /**
74
- * This is the handler for the middleware/controller that validates the app version.
75
- * This is useful in cases where you want to restrict the access to the app to specific
76
- * versions,
77
- * for example: you have a frontend app which needs to be aligned with the "current"
78
- * version of the app, since the frontend won't realize a new version was released, the
79
- * validator can be used to let the frontend know.
80
- * Also, it can be configured to handle requests from popups, in which case, instead of
81
- * generating an error message, it will send a post message.
82
- *
83
- * @parent module:middlewares
84
- */
85
- class VersionValidator {
86
- /**
87
- * @param {?(string | ?number)} version
88
- * The current version of the app. The reason this is nullable is because this comes
89
- * directly from the app configuration, but you may want to re use this to validate
90
- * "another version", so you can use the custom shorthand and send the version using the
91
- * `options` parameter.
92
- * @param {ResponsesBuilder} responsesBuilder
93
- * To generate post message responses for popups.
94
- * @param {ClassAppError} AppError
95
- * To generate the error in case the version is invalid.
96
- * @param {Partial<VersionValidatorOptions>} [options={}]
97
- * Custom options to modify the middleware behavior.
98
- * @throws {Error}
99
- * If the version is `null` and the `options` don't include one either.
100
- */
101
- constructor(version, responsesBuilder, AppError, options = {}) {
102
- /**
103
- * A local reference for the `responsesBuilder` service.
104
- *
105
- * @type {ResponsesBuilder}
106
- * @access protected
107
- * @ignore
108
- */
109
- this._responsesBuilder = responsesBuilder;
110
- /**
111
- * A local reference for the class the app uses to generate errors.
112
- *
113
- * @type {ClassAppError}
114
- * @access protected
115
- * @ignore
116
- */
117
- this._AppError = AppError;
118
- /**
119
- * These are the "settings" the middleware will use in order to validate the requests.
120
- *
121
- * @type {VersionValidatorOptions}
122
- * @access protected
123
- * @ignore
124
- */
125
- this._options = ObjectUtils.merge(
126
- {
127
- error: "The application version doesn't match",
128
- latest: {
129
- allow: true,
130
- name: 'latest',
131
- },
132
- popup: {
133
- variable: 'popup',
134
- title: 'Conflict',
135
- message: 'vesion:conflict',
136
- },
137
- version,
138
- },
139
- options,
140
- );
141
-
142
- if (!this._options.version) {
143
- throw new Error('You need to supply a version');
144
- }
145
- }
146
- /**
147
- * Returns the Express middleware that will validate the `version` parameter.
148
- *
149
- * @returns {ExpressMiddleware}
150
- */
151
- middleware() {
152
- return (req, res, next) => {
153
- // Get the `version` parameter from the request.
154
- const { version } = req.params;
155
- if (!version) {
156
- // If no version is present, move on to the next middleware.
157
- next();
158
- } else if (version === this._options.version || this._validateLatest(version)) {
159
- /**
160
- * If the version matches the one on the options, or the requested version is "latest"
161
- * (and the option is enabled), move on to the next middleware.
162
- */
163
- next();
164
- } else if (this._isPopup(req)) {
165
- /**
166
- * If it doesn't match and the request is comming from a popup, send a response with a
167
- * post message.
168
- */
169
- this._responsesBuilder.htmlPostMessage(
170
- res,
171
- this._options.popup.title,
172
- this._options.popup.message,
173
- statuses.conflict,
174
- );
175
- } else {
176
- // Finally, if it doesn't match and is not from a popup, move to the error handler.
177
- next(
178
- new this._AppError(this._options.error, {
179
- status: statuses.conflict,
180
- response: {
181
- validation: true,
182
- },
183
- }),
184
- );
185
- }
186
- };
187
- }
188
- /**
189
- * The options used to customize the middleware behavior.
190
- *
191
- * @returns {VersionValidatorOptions}
192
- */
193
- get options() {
194
- return this._options;
195
- }
196
- /**
197
- * Helper method that checks if the incoming request is from a popup.
198
- *
199
- * @param {ExpressRequest} req The request information.
200
- * @returns {boolean}
201
- * @access protected
202
- * @ignore
203
- */
204
- _isPopup(req) {
205
- const popup = req.query[this._options.popup.variable];
206
- return !!(popup && popup.toLowerCase() === 'true');
207
- }
208
- /**
209
- * Helper method that checks if the "latest version" is enabled and if the given version
210
- * is "the latest" (comparing it with the option name).
211
- *
212
- * @param {string | number} version The version to validate.
213
- * @returns {boolean}
214
- * @access protected
215
- * @ignore
216
- */
217
- _validateLatest(version) {
218
- return this._options.latest.allow && version === this._options.latest.name;
219
- }
220
- }
221
- /**
222
- * A middleware that will validate a `version` request parameter against the app version
223
- * and generate an error if they don't match.
224
- * This is a "middleware/controller" is because the wrappers for both are the same, the
225
- * difference is that, for controllers, Jimpex sends a second parameter with the route
226
- * where they are mounted.
227
- * By validating the route parameter, the function can know whether the implementation is
228
- * going to use the middleware by itself or as a route middleware.
229
- * If used as middleware, it will just return the result of
230
- * {@link VersionValidator#middleware};
231
- * but if used as controller, it will mount it on `[route]/:version/*`.
232
- *
233
- * @type {MiddlewareCreator<VersionValidatorOptions>}
234
- * @parent module:middlewares
235
- */
236
- const versionValidator = middlewareCreator((options = {}) => (app, route) => {
237
- // Get the middleware function.
238
- const middlewareValidator = new VersionValidator(
239
- app.get('appConfiguration').get('version'),
240
- app.get('responsesBuilder'),
241
- app.get('AppError'),
242
- options,
243
- ).middleware();
244
- // Set the variable to be returned.
245
- let result;
246
- if (route) {
247
- // If the implementation will use it as a router, get the `router` service and mount it.
248
- const router = app.get('router');
249
- // Set the array of "routes" as the return value.
250
- result = [router.all('/:version/*', middlewareValidator)];
251
- } else {
252
- // If the implementation will use it stand alone, just set the function to be returned.
253
- result = middlewareValidator;
254
- }
255
-
256
- // Return the route or the middleware.
257
- return result;
258
- });
259
-
260
- module.exports.VersionValidator = VersionValidator;
261
- module.exports.versionValidator = versionValidator;
@@ -1,136 +0,0 @@
1
- const { code: statuses } = require('statuses');
2
- const { provider } = require('../../utils/wrappers');
3
-
4
- /**
5
- * @typedef {import('../../types').Provider} Provider
6
- */
7
-
8
- /**
9
- * A simple subclass of `Error` but with support for context information.
10
- *
11
- * @augments Error
12
- * @parent module:services
13
- */
14
- class AppError extends Error {
15
- /**
16
- * @param {string} message The error message.
17
- * @param {Object} [context={}] Context information related to the error.
18
- */
19
- constructor(message, context = {}) {
20
- super(message);
21
-
22
- // Limit the stack trace if possible.
23
- if (Error.captureStackTrace) {
24
- Error.captureStackTrace(this, this.constructor);
25
- }
26
- /**
27
- * Context information related to the error.
28
- *
29
- * @type {Object}
30
- * @access protected
31
- * @todo Remove Object.freeze.
32
- */
33
- this._context = Object.freeze(this._parseContext(context));
34
- /**
35
- * The date of when the error was generated.
36
- *
37
- * @type {Date}
38
- * @access protected
39
- */
40
- this._date = new Date();
41
- /**
42
- * Overwrite the name of the `Error` with the one from the class.
43
- *
44
- * @type {string}
45
- * @ignore
46
- */
47
- this.name = this.constructor.name;
48
- }
49
- /**
50
- * Context information related to the error.
51
- *
52
- * @type {Object}
53
- */
54
- get context() {
55
- return this._context;
56
- }
57
- /**
58
- * The date of when the error was generated.
59
- *
60
- * @type {Date}
61
- */
62
- get date() {
63
- return this._date;
64
- }
65
- /**
66
- * Information about the error that can be shown on an app response. This is set using
67
- * the `response` key on the `context`. The idea is that the error handler will read it
68
- * and use it on the response.
69
- *
70
- * @returns {Object}
71
- */
72
- get response() {
73
- return this._context.response || {};
74
- }
75
- /**
76
- * An HTTP status code related to the error. This is set using the `status` key on the
77
- * `context`. If the error handler finds it, it will use it as the response status,
78
- * and use it if necessary.
79
- *
80
- * @type {?number}
81
- */
82
- get status() {
83
- return this._context.status || null;
84
- }
85
- /**
86
- * Utility method that formats the context before saving it in the instance:
87
- * - If the context includes a `status` as a `string`, it will try to replace it with
88
- * its status code from the `statuses` package.
89
- *
90
- * @param {Object} original The original context to format.
91
- * @returns {Object}
92
- * @access protected
93
- * @ignore
94
- */
95
- _parseContext(original) {
96
- const result = { ...original };
97
- if (typeof result.status === 'string') {
98
- result.status = statuses[result.status.toLowerCase()] || result.status;
99
- }
100
-
101
- return result;
102
- }
103
- }
104
- /**
105
- * A generator function to create {@link AppError} instances.
106
- *
107
- * @param {string} message The error message.
108
- * @param {Object} [context] Context information related to the error.
109
- * @returns {AppError}
110
- * @parent module:services
111
- */
112
- const appErrorGenerator = (message, context) => new AppError(message, context);
113
- /**
114
- * A service provider that will register both the {@link AppError} and a generator
115
- * function on the container. `AppError` will be the key for class, and `appError` will be
116
- * for the generator function.
117
- *
118
- * @type {Provider}
119
- * @example
120
- *
121
- * // Register it on the container
122
- * container.register(appError);
123
- * // Getting access to the class.
124
- * const AppError = container.get('AppError');
125
- * // Getting access to the function.
126
- * const appError = container.get('appError');
127
- *
128
- * @parent module:services
129
- */
130
- const appError = provider((app) => {
131
- app.set('AppError', () => AppError);
132
- app.set('appError', () => appErrorGenerator);
133
- });
134
-
135
- module.exports.AppError = AppError;
136
- module.exports.appError = appError;
@@ -1,60 +0,0 @@
1
- const { code: statuses } = require('statuses');
2
- const ObjectUtils = require('wootils/shared/objectUtils');
3
- const { provider } = require('../../utils/wrappers');
4
- const { AppError } = require('./appError');
5
-
6
- /**
7
- * @typedef {import('../../types').Provider} Provider
8
- */
9
-
10
- /**
11
- * A type of error to be used on HTTP requests.
12
- *
13
- * @augments AppError
14
- * @parent module:services
15
- */
16
- class HTTPError extends AppError {
17
- /**
18
- * @param {string} message The error message.
19
- * @param {number} [status=200] The HTTP status code of the request response.
20
- * @param {Object} [context={}] Context information related to the error.
21
- */
22
- constructor(message, status = statuses.ok, context = {}) {
23
- super(message, ObjectUtils.merge({ status }, context));
24
- }
25
- }
26
- /**
27
- * A generator function to create {@link HTTPError} instances.
28
- *
29
- * @param {string} message The error message.
30
- * @param {number} [status] The HTTP status code of the request response.
31
- * @param {Object} [context] Context information related to the error.
32
- * @returns {HTTPError}
33
- * @parent module:services
34
- */
35
- const httpErrorGenerator = (message, status, context) =>
36
- new HTTPError(message, status, context);
37
- /**
38
- * A service provider that will register both the {@link HTTPError} and a generator
39
- * function on the container. `HTTPError` will be the key for class, and `httpError` will
40
- * be for the generator function.
41
- *
42
- * @type {Provider}
43
- * @example
44
- *
45
- * // Register it on the container
46
- * container.register(httpError);
47
- * // Getting access to the class.
48
- * const HTTPError = container.get('HTTPError');
49
- * // Getting access to the function.
50
- * const httpError = container.get('httpError');
51
- *
52
- * @parent module:services
53
- */
54
- const httpError = provider((app) => {
55
- app.set('HTTPError', () => HTTPError);
56
- app.set('httpError', () => httpErrorGenerator);
57
- });
58
-
59
- module.exports.HTTPError = HTTPError;
60
- module.exports.httpError = httpError;
@@ -1,25 +0,0 @@
1
- const { appError } = require('./appError');
2
- const { httpError } = require('./httpError');
3
- const { sendFileProvider } = require('./sendFile');
4
- const { providers } = require('../../utils/wrappers');
5
-
6
- /**
7
- * @typedef {import('../../types').Provider} Provider
8
- */
9
-
10
- /**
11
- * The providers collection for the common services.
12
- *
13
- * @type {Provider}
14
- * @property {Provider} appError The provider for {@link AppError}.
15
- * @property {Provider} httpError The provider for {@link HTTPError}.
16
- * @property {Provider} sendFile The provider for {@link SendFile}.
17
- * @parent module:services
18
- */
19
- const commonServices = providers({
20
- appError,
21
- httpError,
22
- sendFile: sendFileProvider,
23
- });
24
-
25
- module.exports = commonServices;
@@ -1,68 +0,0 @@
1
- const { provider } = require('../../utils/wrappers');
2
-
3
- /**
4
- * @typedef {import('../../types').PathUtils} PathUtils
5
- * @typedef {import('../../types').Provider} Provider
6
- * @typedef {import('../../types').ExpressResponse} ExpressResponse
7
- * @typedef {import('../../types').ExpressNext} ExpressNext
8
- */
9
-
10
- /**
11
- * @callback SendFile
12
- * @param {ExpressResponse} res Necessary to write the file.
13
- * @param {string} filepath The path to the file relative to where the app
14
- * executable is located.
15
- * @param {ExpressNext} next To call the send out the error if anything
16
- * happens.
17
- * @param {string} [from='app'] The location it uses to build the relative path.
18
- * It can be `app` for the directory where the app
19
- * executable is located, or `home` for the project
20
- * root directory.
21
- * @parent module:services
22
- */
23
-
24
- /**
25
- * Generates a function to send files with path relatives to the app executable file is
26
- * located.
27
- *
28
- * @example
29
- *
30
- * // Let's say this is inside an Express middleware.
31
- * // Get the function
32
- * const send = sendFile(pathUtils);
33
- * send(res, 'some-file.html', next);
34
- * // If your app is on `/app/index.js`, this will send `/app/some-file.html`.
35
- *
36
- * @param {PathUtils} pathUtils To generate the relative paths.
37
- * @returns {SendFile}
38
- * @parent module:services
39
- */
40
- const sendFile = (pathUtils) => (res, filepath, next = () => {}, from = 'app') => {
41
- res.sendFile(pathUtils.joinFrom(from, filepath), (error) => {
42
- if (error) {
43
- next(error);
44
- } else {
45
- res.end();
46
- }
47
- });
48
- };
49
- /**
50
- * The service provider that once registered on the app container will set the result of
51
- * `sendFile(pathUtils)` as the `sendFile` service.
52
- *
53
- * @type {Provider}
54
- * @example
55
- *
56
- * // Register it on the container
57
- * container.register(sendFileProvider);
58
- * // Getting access to the service instance
59
- * const sendFile = container.get('sendFile');
60
- *
61
- * @parent module:services
62
- */
63
- const sendFileProvider = provider((app) => {
64
- app.set('sendFile', () => sendFile(app.get('pathUtils')));
65
- });
66
-
67
- module.exports.sendFile = sendFile;
68
- module.exports.sendFileProvider = sendFileProvider;
@@ -1,85 +0,0 @@
1
- const fs = require('fs-extra');
2
- const { provider } = require('../../utils/wrappers');
3
- /**
4
- * @typedef {import('../../types').PathUtils} PathUtils
5
- * @typedef {import('../../types').Provider} Provider
6
- */
7
-
8
- /**
9
- * This service allows the app to easily read static files. The idea behind centralizing
10
- * this functionalities into a service is that is pretty common to have bundling tools to
11
- * generate the frontend, and on that process files can have different paths or not even
12
- * be generated all, that's why this service exists. The service can be
13
- * extended/overwritten to accommodate any requirements and avoid having to update or add
14
- * `if`s to every `fs` call the app does.
15
- * Another _'feature'_ of this service is that all the paths are relative to the directory
16
- * where the app executable is located, so you don't have to remember the relative path
17
- * from the place you are accessing a file to the place where it's located.
18
- *
19
- * @parent module:services
20
- */
21
- class FrontendFs {
22
- /**
23
- * @param {PathUtils} pathUtils To generate the relative paths.
24
- */
25
- constructor(pathUtils) {
26
- /**
27
- * A local reference for the `pathUtils` service.
28
- *
29
- * @type {PathUtils}
30
- * @access protected
31
- * @ignore
32
- */
33
- this._pathUtils = pathUtils;
34
- }
35
- /**
36
- * Delete a file from the file system.
37
- *
38
- * @param {string} filepath The path to the file.
39
- * @returns {Promise}
40
- */
41
- delete(filepath) {
42
- return fs.unlink(this._pathUtils.joinFrom('app', filepath));
43
- }
44
- /**
45
- * Read a file from the file system.
46
- *
47
- * @param {string} filepath The path to the file.
48
- * @param {string} [encoding='utf-8'] The text encoding in which the file should be
49
- * read.
50
- * @returns {Promise<string>}
51
- */
52
- read(filepath, encoding = 'utf-8') {
53
- return fs.readFile(this._pathUtils.joinFrom('app', filepath), encoding);
54
- }
55
- /**
56
- * Write a file on the file system.
57
- *
58
- * @param {string} filepath The path to the file.
59
- * @param {string} data The contents of the file.
60
- * @returns {Promise}
61
- */
62
- write(filepath, data) {
63
- return fs.writeFile(this._pathUtils.joinFrom('app', filepath), data);
64
- }
65
- }
66
- /**
67
- * The service provider that once registered on the app container will set an instance of
68
- * `FrontendFs` as the `frontendFs` service.
69
- *
70
- * @type {Provider}
71
- * @example
72
- *
73
- * // Register it on the container
74
- * container.register(frontendFs);
75
- * // Getting access to the service instance
76
- * const frontendFs = container.get('frontendFs');
77
- *
78
- * @parent module:services
79
- */
80
- const frontendFs = provider((app) => {
81
- app.set('frontendFs', () => new FrontendFs(app.get('pathUtils')));
82
- });
83
-
84
- module.exports.FrontendFs = FrontendFs;
85
- module.exports.frontendFs = frontendFs;
@@ -1,17 +0,0 @@
1
- const { providers } = require('../../utils/wrappers');
2
- const { frontendFs } = require('./frontendFs');
3
-
4
- /**
5
- * @typedef {import('../../types').Provider} Provider
6
- */
7
-
8
- /**
9
- * The providers collection for the frontend services.
10
- *
11
- * @type {Provider}
12
- * @property {Provider} frontendFs The default provider for {@link FrontendFs}.
13
- * @parent module:services
14
- */
15
- module.exports = providers({
16
- frontendFs,
17
- });