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
@@ -1,256 +0,0 @@
1
- const fetch = require('node-fetch');
2
- const urijs = require('urijs');
3
- const { provider } = require('../../utils/wrappers');
4
-
5
- /**
6
- * @typedef {import('../../types').Logger} Logger
7
- * @typedef {import('../../types').Provider} Provider
8
- * @typedef {import('node-fetch').Response} Response
9
- * @typedef {import('../../types').ExpressRequest} ExpressRequest
10
- */
11
-
12
- /**
13
- * @typedef {Object} HTTPFetchOptions
14
- * @property {?string} method The request method.
15
- * @property {?Object.<string, string>} headers The request headers.
16
- * @property {?string} body The request body.
17
- * @property {?Object.<string, string>} qs The request query string parameters.
18
- * @property {?ExpressRequest} req An Express request object used to get
19
- * extra infromation (like headers and the
20
- * IP).
21
- * @parent module:services
22
- */
23
-
24
- /**
25
- * A set of utilities to work with HTTP requests and responses.
26
- *
27
- * @parent module:services
28
- */
29
- class HTTP {
30
- /**
31
- * @param {boolean} logRequests Whether or not to log the requests and their responses.
32
- * @param {Logger} appLogger If `logRequests` is `true`, this will be used to log
33
- * the requests and responses information.
34
- */
35
- constructor(logRequests, appLogger) {
36
- /**
37
- * Whether or not to log the requests and their responses.
38
- *
39
- * @type {boolean}
40
- * @access protected
41
- * @ignore
42
- */
43
- this._logRequests = logRequests;
44
- /**
45
- * A local reference for the `appLogger` service.
46
- *
47
- * @type {Logger}
48
- * @access protected
49
- * @ignore
50
- */
51
- this._appLogger = appLogger;
52
- /**
53
- * So it can be sent to other services as a reference.
54
- *
55
- * @ignore
56
- */
57
- this.fetch = this.fetch.bind(this);
58
- }
59
- /**
60
- * Make a request.
61
- *
62
- * @param {string} url The request URL.
63
- * @param {Partial<HTTPFetchOptions>} [options={}] The request options.
64
- * @returns {Promise<Response>}
65
- */
66
- fetch(url, options = {}) {
67
- // Get a mutable reference for the URL.
68
- let fetchURL = url;
69
- // If there are query string parameters...
70
- if (options.qs) {
71
- // ...use `urijs` to inject them on the URL reference.
72
- fetchURL = urijs(url).addSearch(options.qs).toString();
73
- }
74
- // Define an object that will hold the new set of options.
75
- const fetchOptions = {
76
- // Set the request method and make it fallback to `GET` if it wasn't set.
77
- method: (options.method || 'get').toUpperCase(),
78
- };
79
- // If there's a body, add it to the new options.
80
- if (options.body) {
81
- fetchOptions.body = options.body;
82
- }
83
- // Define the base headers for the request.
84
- let defaultHeaders = {};
85
- // If there's an Express request object on the `options`...
86
- if (options.req) {
87
- /**
88
- * Overwrite the base headers with the request original IP as `x-forwarded-for` and all the
89
- * received custom headers that request may have.
90
- */
91
- defaultHeaders = {
92
- 'x-forwarded-for': this.getIPFromRequest(options.req),
93
- ...this.getCustomHeadersFromRequest(options.req),
94
- };
95
- }
96
- // Merge the base headers with the ones received on the `options`.
97
- const headers = {
98
- ...defaultHeaders,
99
- ...(options.headers || {}),
100
- };
101
- /**
102
- * If there's at least one header on the dictionary, add it to the new options. This check is
103
- * to avoid sending an empty object.
104
- */
105
- if (Object.keys(headers).length) {
106
- fetchOptions.headers = this.normalizeHeaders(headers);
107
- }
108
- // If the `logRequests` flag is `true`, call the method to log the request.
109
- if (this._logRequests) {
110
- this._logRequest(fetchURL, fetchOptions);
111
- }
112
- // Make the request.
113
- let result = fetch(fetchURL, fetchOptions);
114
- // If the `logRequests` flag is `true`...
115
- if (this._logRequests) {
116
- // Add an extra step on the promise chain to log the response.
117
- result = result.then((response) => {
118
- this._logResponse(response);
119
- return response;
120
- });
121
- }
122
- // Return the request promise.
123
- return result;
124
- }
125
- /**
126
- * Creates a dictionary with all the custom headers a request has. By custom header it
127
- * means all the headers which name start with `x-`.
128
- * This method doesn't copy `x-forwarded-for` as the `fetch` method generates it by
129
- * calling `getIPFromRequest`.
130
- *
131
- * @param {ExpressRequest} req The request from which it will try to get the headers.
132
- * @returns {Object.<string, string>}
133
- */
134
- getCustomHeadersFromRequest(req) {
135
- const headers = {};
136
- Object.keys(req.headers).forEach((headerName) => {
137
- if (headerName.startsWith('x-') && !headerName.startsWith('x-forwarded-for')) {
138
- headers[headerName] = req.headers[headerName];
139
- }
140
- });
141
-
142
- return headers;
143
- }
144
- /**
145
- * Try to get the IP from a given request.
146
- *
147
- * @param {ExpressRequest} req The request from which it will try to obtain the IP
148
- * address.
149
- * @returns {?string}
150
- */
151
- getIPFromRequest(req) {
152
- return (
153
- req.headers['x-forwarded-for'] ||
154
- req.connection.remoteAddress ||
155
- req.socket.remoteAddress ||
156
- req.connection.socket.remoteAddress
157
- );
158
- }
159
- /**
160
- * It takes a dictionary of headers and normalize the names so each word will start with
161
- * an upper case character. This is helpful in case you added custom headers and didn't
162
- * care about the casing, or when copying headers from a server request, in which case
163
- * they are all tranformed to lower case.
164
- *
165
- * @param {Object.<string, string>} headers The dictionary of headers to normalize.
166
- * @returns {Object.<string, string>}
167
- */
168
- normalizeHeaders(headers) {
169
- return Object.keys(headers).reduce((newHeaders, name) => {
170
- const newName = name
171
- .split('-')
172
- .map((part) => part.replace(/^(\w)/, (ignore, letter) => letter.toUpperCase()))
173
- .join('-');
174
- return {
175
- ...newHeaders,
176
- [newName]: headers[name],
177
- };
178
- }, {});
179
- }
180
- /**
181
- * Whether or not to log the requests and their responses.
182
- *
183
- * @type {boolean}
184
- */
185
- get logRequests() {
186
- return this._logRequests;
187
- }
188
- /**
189
- * Log a a request information using the `appLogger` service.
190
- *
191
- * @param {string} url The request URL.
192
- * @param {HTTPFetchOptions} options The options generated by the `fetch` method.
193
- * @access protected
194
- * @ignore
195
- */
196
- _logRequest(url, options) {
197
- const prefix = 'REQUEST> ';
198
- const lines = ['--->>', `${prefix}${options.method} ${url}`];
199
- if (options.headers) {
200
- Object.keys(options.headers).forEach((header) => {
201
- lines.push(`${prefix}${header}: ${options.headers[header]}`);
202
- });
203
- }
204
-
205
- if (options.body) {
206
- lines.push(`${prefix}body: "${options.body}"`);
207
- }
208
-
209
- this._appLogger.info(lines);
210
- }
211
- /**
212
- * Log a a response information using the `appLogger` service.
213
- *
214
- * @param {Response} response The response object returned by `node-fetch`.
215
- * @access protected
216
- * @ignore
217
- */
218
- _logResponse(response) {
219
- const prefix = 'RESPONSE> ';
220
- const lines = [
221
- '<<---',
222
- `${prefix}${response.url}`,
223
- `${prefix}status: ${response.status}`,
224
- ];
225
-
226
- response.headers.forEach((value, header) => {
227
- lines.push(`${prefix}${header}: ${value}`);
228
- });
229
-
230
- this._appLogger.info(lines);
231
- }
232
- }
233
- /**
234
- * The service provider that once registered on the app container will set an instance of
235
- * `HTTP` as the `http` service. The provider also checks the `debug.logRequests` setting
236
- * on the app configuration in order to enable or not the logging of requests.
237
- *
238
- * @type {Provider}
239
- * @example
240
- *
241
- * // Register it on the container
242
- * container.register(http);
243
- * // Getting access to the service instance
244
- * const http = container.get('http');
245
- *
246
- * @parent module:services
247
- */
248
- const http = provider((app) => {
249
- app.set('http', () => {
250
- const logRequests = app.get('appConfiguration').get('debug.logRequests') === true;
251
- return new HTTP(logRequests, app.get('appLogger'));
252
- });
253
- });
254
-
255
- module.exports.HTTP = HTTP;
256
- module.exports.http = http;
@@ -1,25 +0,0 @@
1
- const { apiClient } = require('./apiClient');
2
- const { http } = require('./http');
3
- const { responsesBuilder } = require('./responsesBuilder');
4
- const { providers } = require('../../utils/wrappers');
5
-
6
- /**
7
- * @typedef {import('../../types').Provider} Provider
8
- */
9
-
10
- /**
11
- * The providers collection for the HTTP services.
12
- *
13
- * @type {Provider}
14
- * @property {Provider} apiClient The provider for {@link APIClient}.
15
- * @property {Provider} http The provider for {@link HTTP}.
16
- * @property {Provider} responsesBuilder The provider for {@link ResponsesBuilder}.
17
- * @parent module:services
18
- */
19
- const httpServices = providers({
20
- apiClient,
21
- http,
22
- responsesBuilder,
23
- });
24
-
25
- module.exports = httpServices;
@@ -1,193 +0,0 @@
1
- const { code: statuses } = require('statuses');
2
- const { provider } = require('../../utils/wrappers');
3
-
4
- /**
5
- * @typedef {import('../../types').Provider} Provider
6
- * @typedef {import('../../types').AppConfiguration} AppConfiguration
7
- * @typedef {import('../../types').ExpressResponse} ExpressResponse
8
- */
9
-
10
- /**
11
- * It allows customization of a post message HTML template.
12
- *
13
- * @typedef {Object} ResponsesBuilderPostMessageOptions
14
- * @property {?string} target
15
- * The target that will emit the `postMessage`.
16
- * Default `'window.opener'`.
17
- * @property {?boolean} close
18
- * Whether or not to do a `window.close` after sending the message.
19
- * Default `true`.
20
- * @property {?number} closeDelay
21
- * How many ms should it wait before closing the window, if `options.close` is `true`.
22
- * Default `700`.
23
- * @parent module:services
24
- */
25
-
26
- /**
27
- * A utility service to build server responses.
28
- *
29
- * @parent module:services
30
- */
31
- class ResponsesBuilder {
32
- /**
33
- * @param {AppConfiguration} appConfiguration To get the app version.
34
- */
35
- constructor(appConfiguration) {
36
- /**
37
- * A local reference for the `appConfiguration` service.
38
- *
39
- * @type {AppConfiguration}
40
- * @access protected
41
- * @ignore
42
- */
43
- this._appConfiguration = appConfiguration;
44
- }
45
- /**
46
- * Generates and send an HTML response that emits a post message.
47
- * The post message will be prefixed with the value of the configuration setting
48
- * `postMessagesPrefix`.
49
- *
50
- * @param {ExpressResponse} res
51
- * The Express response object necessary to write the HTML.
52
- * @param {string} title
53
- * The title for the HTML.
54
- * @param {string} message
55
- * The contents of the post message.
56
- * @param {number} [status=200]
57
- * The HTTP status.
58
- * @param {Partial<ResponsesBuilderPostMessageOptions>} [options={}]
59
- * Options to customize the HTML.
60
- */
61
- htmlPostMessage(res, title, message, status = statuses.ok, options = {}) {
62
- const prefix = this._appConfiguration.get('postMessagesPrefix') || '';
63
- const target = options.target || 'window.opener';
64
- const close = typeof options.close !== 'undefined' ? options.close : true;
65
- const defaultCloseDelay = 700;
66
- const closeDelay = options.closeDelay || defaultCloseDelay;
67
- const closeCode = close
68
- ? `setTimeout(function() { window.close(); }, ${closeDelay});`
69
- : '';
70
- const html = this._htmlTemplate(
71
- title,
72
- `
73
- (function() {
74
- if (${target}) {
75
- ${target}.postMessage('${prefix}${message}', '*');
76
- ${closeCode}
77
- }
78
- })();
79
- `,
80
- );
81
-
82
- res.setHeader('Content-Type', 'text/html');
83
- res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store');
84
- res.status(this._normalizeStatus(status));
85
- res.write(html);
86
- res.end();
87
- }
88
- /**
89
- * Generates and sends a JSON response.
90
- *
91
- * @example
92
- *
93
- * <caption>The generated looks like this.</caption>
94
- *
95
- * {
96
- * metadata: {
97
- * version: '[app-version]',
98
- * status: [http-status],
99
- * },
100
- * data: [...],
101
- * }
102
- *
103
- * @param {ExpressResponse} res The Express response object necessary to
104
- * write the JSON.
105
- * @param {Object} data The information for the `data` key.
106
- * @param {number} [status=200] The HTTP status.
107
- * @param {Object} [metadata={}] Extra information to include on the
108
- * `metadata` key.
109
- */
110
- json(res, data, status = statuses.ok, metadata = {}) {
111
- const useStatus = this._normalizeStatus(status);
112
- res
113
- .status(useStatus)
114
- .json({
115
- metadata: {
116
- version: this._appConfiguration.get('version'),
117
- status: useStatus,
118
- ...metadata,
119
- },
120
- data,
121
- })
122
- .end();
123
- }
124
- /**
125
- * Generates a basic HTML template for other services to use.
126
- *
127
- * @param {string} title The HTML `<title />` attribute.
128
- * @param {string} code Javascript code to be wrapped on a `<script />` tag.
129
- * @returns {string}
130
- * @access protected
131
- * @ignore
132
- */
133
- _htmlTemplate(title, code) {
134
- return `
135
- <!DOCTYPE html>
136
- <html>
137
- <head>
138
- <meta charset="utf-8" />
139
- <meta http-equiv="x-ua-compatible" content="ie=edge" />
140
- <meta name="viewport" content="width=device-width, initial-scale=1" />
141
- <title>${title}</title>
142
- </head>
143
- <body>
144
- <script type="text/javascript">
145
- ${code}
146
- </script>
147
- </body>
148
- </html>
149
- `;
150
- }
151
- /**
152
- * Utility method used to make sure a recevied status is a valid status code. If the
153
- * status is a string, the method will try to find the code from the `statuses` package.
154
- *
155
- * @param {string | number} status The status to normalize.
156
- * @returns {string | number} If `status` is a string, but there's no valid code, it
157
- * will return it as it was received.
158
- * @access protected
159
- * @ignore
160
- * @todo On the next breaking version, if there's no valid code, it will be
161
- * transformed to `200`.
162
- */
163
- _normalizeStatus(status) {
164
- let result;
165
- if (typeof status === 'string') {
166
- result = statuses[status] || status;
167
- } else {
168
- result = status;
169
- }
170
-
171
- return result;
172
- }
173
- }
174
- /**
175
- * The service provider that once registered on the app container will set an instance of
176
- * `ResponsesBuilder` as the `responsesBuilder` service.
177
- *
178
- * @type {Provider}
179
- * @example
180
- *
181
- * // Register it on the container
182
- * container.register(responsesBuilder);
183
- * // Getting access to the service instance
184
- * const responsesBuilder = container.get('responsesBuilder');
185
- *
186
- * @parent module:services
187
- */
188
- const responsesBuilder = provider((app) => {
189
- app.set('responsesBuilder', () => new ResponsesBuilder(app.get('appConfiguration')));
190
- });
191
-
192
- module.exports.ResponsesBuilder = ResponsesBuilder;
193
- module.exports.responsesBuilder = responsesBuilder;
@@ -1,15 +0,0 @@
1
- const common = require('./common');
2
- const frontend = require('./frontend');
3
- const html = require('./html');
4
- const http = require('./http');
5
- const utils = require('./utils');
6
-
7
- /**
8
- * @module module:services
9
- */
10
-
11
- module.exports.common = common;
12
- module.exports.frontend = frontend;
13
- module.exports.html = html;
14
- module.exports.http = http;
15
- module.exports.utils = utils;
@@ -1,147 +0,0 @@
1
- const { code: statuses } = require('statuses');
2
- const ObjectUtils = require('wootils/shared/objectUtils');
3
- const { providerCreator } = require('../../utils/wrappers');
4
-
5
- /**
6
- * @typedef {import('../../types').ExpressMiddleware} ExpressMiddleware
7
- */
8
-
9
- /**
10
- * @typedef {import('../../types').ProviderCreator<O>} ProviderCreator<O>
11
- * @template O
12
- */
13
-
14
- /**
15
- * These options allow you to modify the error generated by the middleware when the
16
- * request doesn't have a valid token.
17
- *
18
- * @typedef {Object} EnsureBearerTokenErrorOptions
19
- * @property {string} message The error message for the response. Default
20
- * `'Unauthorized'`.
21
- * @property {number} status The HTTP status that will be added to error context
22
- * information.
23
- * Default `401`.
24
- * @property {Object} response Context information that the error handler can read and
25
- * add to the default response. Default `{}`.
26
- * @parent module:services
27
- */
28
-
29
- /**
30
- * The options for how to validate the token and, possibly, create the errors.
31
- *
32
- * @typedef {Object} EnsureBearerTokenOptions
33
- * @property {EnsureBearerTokenErrorOptions} error
34
- * The options to modify the error generated by the middleware when the request doesn't
35
- * have a valid token.
36
- * @property {RegExp} expression
37
- * The regular expression used to extract the token from the request authorization header.
38
- * @property {string} local
39
- * The property inside the `res.locals` where the token, if found, will be saved.
40
- * Default `'token'`.
41
- * @parent module:services
42
- */
43
-
44
- /**
45
- * This service gives you a middleware that verifies if a request has an `Authorization`
46
- * header with a bearer token; if it does, the token will be saved on the `res.locals`,
47
- * otherwise, it will generate an error.
48
- *
49
- * @parent module:services
50
- */
51
- class EnsureBearerToken {
52
- /**
53
- * @param {ClassAppError} AppError
54
- * To format the error caused when the request doesn't have a valid token.
55
- * @param {Partial<EnsureBearerTokenOptions>} [options={}]
56
- * The options to customize the middleware behavior: how to validate the token, how to
57
- * save it and what kind of error should generate.
58
- */
59
- constructor(AppError, options = {}) {
60
- /**
61
- * A local reference for the class the app uses to generate errors.
62
- *
63
- * @type {ClassAppError}
64
- * @access protected
65
- * @ignore
66
- */
67
- this._AppError = AppError;
68
- /**
69
- * The options that define how the middleware validates the token, saves it and
70
- * generates the possible error.
71
- *
72
- * @type {EnsureBearerTokenOptions}
73
- * @access protected
74
- * @ignore
75
- */
76
- this._options = ObjectUtils.merge(
77
- {
78
- error: {
79
- message: 'Unauthorized',
80
- status: statuses.unauthorized,
81
- response: {},
82
- },
83
- expression: /bearer (.*?)(?:$|\s)/i,
84
- local: 'token',
85
- },
86
- options,
87
- );
88
- }
89
- /**
90
- * Creates the middleware that will validate the presence of a bearer token on the
91
- * request authorization header.
92
- *
93
- * @returns {ExpressMiddleware}
94
- */
95
- middleware() {
96
- return (req, res, next) => {
97
- let unauthorized = true;
98
- const {
99
- headers: { authorization },
100
- } = req;
101
- if (authorization) {
102
- const matches = this._options.expression.exec(authorization);
103
- if (matches) {
104
- const [, token] = matches;
105
- res.locals[this._options.local] = token;
106
- unauthorized = false;
107
- }
108
- }
109
-
110
- if (unauthorized) {
111
- const { error } = this._options;
112
- next(
113
- new this._AppError(error.message, {
114
- status: error.status,
115
- response: error.response,
116
- }),
117
- );
118
- } else {
119
- next();
120
- }
121
- };
122
- }
123
- /**
124
- * The options that define how the middleware validates the token, saves it and
125
- * generates the possible error.
126
- *
127
- * @type {EnsureBearerTokenOptions}
128
- */
129
- get options() {
130
- return this._options;
131
- }
132
- }
133
- /**
134
- * Generates a "service middleware" that can be used on route controllers in order to
135
- * validate the presence of a bearer token on the requests authorization header.
136
- *
137
- * @type {ProviderCreator<EnsureBearerTokenOptions>}
138
- * @parent module:services
139
- */
140
- const ensureBearerToken = providerCreator((options) => (app) => {
141
- app.set('ensureBearerToken', () =>
142
- new EnsureBearerToken(app.get('AppError'), options).middleware(),
143
- );
144
- });
145
-
146
- module.exports.EnsureBearerToken = EnsureBearerToken;
147
- module.exports.ensureBearerToken = ensureBearerToken;
@@ -1,19 +0,0 @@
1
- const { ensureBearerToken } = require('./ensureBearerToken');
2
- const { providers } = require('../../utils/wrappers');
3
-
4
- /**
5
- * @typedef {import('../../types').Provider} Provider
6
- */
7
-
8
- /**
9
- * The providers collection for the utility services.
10
- *
11
- * @type {Provider}
12
- * @property {Provider} ensureBearerToken The provider for {@link EnsureBearerToken}.
13
- * @parent module:services
14
- */
15
- const utilsServices = providers({
16
- ensureBearerToken,
17
- });
18
-
19
- module.exports = utilsServices;