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,1039 +0,0 @@
1
- const ObjectUtils = require('wootils/shared/objectUtils');
2
- const { removeSlashes, createRouteExpression } = require('../../utils/functions');
3
- const { providerCreator, controller } = require('../../utils/wrappers');
4
-
5
- /**
6
- * @typedef {import('../../types').ExpressRouter} ExpressRouter
7
- * @typedef {import('../../app').Jimpex} Jimpex
8
- * @typedef {import('../../types').ExpressRequest} ExpressRequest
9
- * @typedef {import('../../types').ExpressResponse} ExpressResponse
10
- * @typedef {import('../../types').ExpressNext} ExpressNext
11
- * @typedef {import('../../types').ExpressMiddleware} ExpressMiddleware
12
- * @typedef {import('../../types').MiddlewareLike} MiddlewareLike
13
- * @typedef {import('../../services/http/http').HTTP} HTTP
14
- * @typedef {import('../../services/http/http').HTTPFetchOptions} HTTPFetchOptions
15
- * @typedef {import('../../services/http/http').Response} Response
16
- * @typedef {import('../../services/http/apiClient').APIClientConfiguration} APIClientConfiguration
17
- * @prettierignore
18
- */
19
-
20
- /**
21
- * @typedef {import('../../types').ProviderCreator<O>} ProviderCreator<O>
22
- * @template O
23
- */
24
-
25
- /**
26
- * This object represets an HTTP method for a route the controller will mount.
27
- *
28
- * @typedef {Object} GatewayControllerRouteMethod
29
- * @property {string} method
30
- * The name of the HTTP method.
31
- * @property {GatewayControllerEndpointInformation} endpoint
32
- * The information for the endpoint responsible from creating the route.
33
- * @parent module:controllers
34
- * @ignore
35
- */
36
-
37
- /**
38
- * This object contains the information for an specific route the controller will mount.
39
- *
40
- * @typedef {Object} GatewayControllerRoute
41
- * @property {string} path The path to the endpoint relative
42
- * to the entry point.
43
- * @property {string} route The path the route will have. This
44
- * is different from `path` as it's
45
- * possible for the gateway to be
46
- * implemented using the `root`
47
- * option.
48
- * @property {GatewayControllerRouteMethod[]} methods A list with all the methods the
49
- * controller will use to mount the
50
- * route.
51
- * @parent module:controllers
52
- * @ignore
53
- */
54
-
55
- /**
56
- * Normally, you would define an endpoint with just a string path, but you can use this
57
- * type of object to add extra settings.
58
- *
59
- * @typedef {Object} GatewayConfigurationEndpoint
60
- * @property {string} path The path to the endpoint relative to the entry point. It can
61
- * include placeholders for parameters like `/:parameter/`.
62
- * @property {string} method The HTTP method for the endpoint. This will tell the gateway
63
- * the type of route it should mount. If is not specified, it
64
- * will use `all`.
65
- * @parent module:controllers
66
- */
67
-
68
- /**
69
- * A dictionary of endpoints or sub endpoints the gateway will use in order to mount
70
- * routes.
71
- *
72
- * @typedef {Object} GatewayConfigurationEndpoints
73
- * @property {string | GatewayConfigurationEndpoints | GatewayConfigurationEndpoint} [endpointName]
74
- * It can be the path to an actual endpoint, a dictionary of sub endpoints, or a
75
- * definition of an endpoint with settings ({@link GatewayConfigurationEndpoint}).
76
- * @parent module:controllers
77
- * @prettierignore
78
- */
79
-
80
- /**
81
- * This is a configuration object very similar to the one {@link APIClient} uses in order
82
- * to configure the endpoints; the controller uses it to create the routes and to validate
83
- * the HTTP methods.
84
- *
85
- * @typedef {Object} GatewayConfiguration
86
- * @property {string} url The entry point to the API the
87
- * controller will make the requests
88
- * to.
89
- * @property {GatewayConfigurationEndpoints} gateway A dictionary with the endpoints the
90
- * gateway will make available.
91
- * @parent module:controllers
92
- */
93
-
94
- /**
95
- * The options for how the gateway will handle the headers from the requests and the
96
- * responses.
97
- *
98
- * @typedef {Object} GatewayControllerHeadersOptions
99
- * @property {boolean} useXForwardedFor Whether or not to include the header with the
100
- * request's IP address. Default `true`.
101
- * @property {boolean} copyCustomHeaders Whether or not to copy all custom headers from
102
- * the request. By custom header, it means all the
103
- * headers which names start with `x-`. Default
104
- * `true`.
105
- * @property {string[]} copy A list of "known" headers the gateway will try
106
- * to copy from the incoming request. Default:
107
- * `['authorization','content-type', 'referer',
108
- * 'user-agent']`.
109
- * @property {string[]} remove A list of "known" headers the gateway will try
110
- * to remove the response. Default:
111
- * `['server', 'x-powered-by']`.
112
- * @parent module:controllers
113
- */
114
-
115
- /**
116
- * The options to configure how the gateway will manage the requests and the responses.
117
- *
118
- * @typedef {Object} GatewayControllerOptions
119
- * @property {string} root
120
- * This is really a helper for when the gateway is used with an API client. The idea is
121
- * that,
122
- * by default, the routes are mounted on the controller route, but with this option, you
123
- * can specify another sub path. For example: The controller is mounted on `/routes`, if
124
- * you set `root` to `gateway`, all the routes will be on `/routes/gateway`.
125
- * This become important (and useful) when you get the API client configuration (with
126
- * `endpointsForAPIClient`): The `url` will be the controller route, but all the endpoints
127
- * will be modified and prefixed with the `root`. Default `''`.
128
- * @property {string} configurationSetting
129
- * This is another option for when the gateway is used with an API client. When calling
130
- * `endpointsForAPIClient`, all the endpoints will be wrapped inside an object named after
131
- * this option. For example: `{ url: '...', endpoints: { api: { ... } } }`. Default `api`.
132
- * @property {GatewayControllerHeadersOptions} headers
133
- * The options for how the gateway will handle the headers from the requests and the
134
- * responses.
135
- * @parent module:controllers
136
- */
137
-
138
- /**
139
- * This are the options sent to the controller creator that instantiates
140
- * {@link GatewayController}.
141
- * They're basically the same as {@link GatewayControllerOptions} but with a couple of
142
- * extra ones.
143
- *
144
- * @typedef {Object} GatewayControllerCreatorOptions
145
- * @property {string} serviceName
146
- * The name of the creator will use to register the controller in the container. No, this
147
- * is not a typo. The creator will register the controller so other services can access
148
- * the `endpointsForAPIClient` getter. The service will be available after the app routes
149
- * are mounted.
150
- * If this is overwritten, the creator will ensure that the name ends with `Gateway`; and
151
- * if overwritten, but it doesn't include `Gateway` at the end, and no
152
- * `configurationSetting` was defined, the creator will use the custom name (without
153
- * `Gatway`) for `configurationSetting`.
154
- * Default `'apiGeteway'`.
155
- * @property {string} helperServiceName
156
- * The name of the helper service the creator will try to obtain from the container. If
157
- * `serviceName` is overwritten, the default for this will be `${serviceName}Helper`.
158
- * Default:
159
- * `'apiGatewayHelper'`.
160
- * @property {string} configurationSetting
161
- * The name of the configuration setting where the gateway configuration is stored. If not
162
- * overwritten, check the description of `serviceName` to understand which will be its
163
- * default value. Default `'api'`.
164
- * @property {typeof GatewayController} gatewayClass
165
- * The class the creator will instantiate. Similar to {@link APIClient}, this allows for
166
- * extra customization in cases you may need multiple gateways. Default
167
- * {@link GatewayController}.
168
- * @property {?GatewayControllerMiddlewaresFn} middlewares
169
- * This function can be used to add custom middlewares on the gateway routes. If
170
- * implemented, it must return a list of middlewares when executed. Default `null`.
171
- * @parent module:controllers
172
- */
173
-
174
- /**
175
- * This is the information for a request the controller will make.
176
- *
177
- * @typedef {Object} GatewayControllerRequest
178
- * @property {string} url The URL for the request.
179
- * @property {HTTPFetchOptions} options The request options.
180
- * @parent module:controllers
181
- */
182
-
183
- /**
184
- * This is the information for an specific endpoint that the gateway may use to send to a
185
- * helper method in order to give it context.
186
- *
187
- * @typedef {Object} GatewayControllerEndpointInformation
188
- * @property {string} name
189
- * The name of the endpoint, which is actually the path inside the gateway configuration's
190
- * `gateway` property.
191
- * @property {string | GatewayConfigurationEndpoint} settings
192
- * The path for the endpoint, or the dictionary of settings.
193
- * @parent module:controllers
194
- */
195
-
196
- /**
197
- * This is called in order to allow the helper to modify the information of a request that
198
- * is about the fired.
199
- *
200
- * @callback GatewayHelperServiceRequestReducer
201
- * @param {GatewayControllerRequest} request
202
- * The information for a request the controller will make.
203
- * @param {GatewayControllerEndpointInformation} endpoint
204
- * The information for the endpoint responsible of creating the route.
205
- * @param {ExpressRequest} req
206
- * The server's incoming request information.
207
- * @param {ExpressResponse} res
208
- * The server's response information.
209
- * @param {ExpressNext} next
210
- * The function to call the next middleware.
211
- * @returns {GatewayControllerRequest}
212
- * @parent module:controllers
213
- */
214
-
215
- /**
216
- * This is called in order to allow the helper to modify the information of a response the
217
- * gateway made.
218
- *
219
- * @callback GatewayHelperServiceResponseReducer
220
- * @param {Response} response
221
- * The response generated by the fetch request.
222
- * @param {GatewayControllerEndpointInformation} endpoint
223
- * The information for the endpoint responsible of creating the route.
224
- * @param {ExpressRequest} req
225
- * The server's incoming request information.
226
- * @param {ExpressResponse} res
227
- * The server's response information.
228
- * @param {ExpressNext} next
229
- * The function to call the next middleware.
230
- * @returns {Response}
231
- * @parent module:controllers
232
- */
233
-
234
- /**
235
- * This is called in order to allow the helper to decide whether a fetch request response
236
- * should be added to the server's response stream. This will only be called if the helper
237
- * also implements `handleEndpointResponse`.
238
- *
239
- * @callback GatewayHelperServiceStreamVerification
240
- * @param {Response} response
241
- * The response generated by the fetch request.
242
- * @param {GatewayControllerEndpointInformation} endpoint
243
- * The information for the endpoint responsible of creating the route.
244
- * @param {ExpressRequest} req
245
- * The server's incoming request information.
246
- * @param {ExpressResponse} res
247
- * The server's response information.
248
- * @param {ExpressNext} next
249
- * The function to call the next middleware.
250
- * @returns {boolean}
251
- * @parent module:controllers
252
- */
253
-
254
- /**
255
- * This is called in order for the helper to handle a response. This is only called if
256
- * `shouldStreamEndpointResponse` returned `false`.
257
- *
258
- * @callback GatewayHelperServiceResponseHandler
259
- * @param {Response} response
260
- * The response generated by the fetch request.
261
- * @param {GatewayControllerEndpointInformation} endpoint
262
- * The information for the endpoint responsible of creating the route.
263
- * @param {ExpressRequest} req
264
- * The server's incoming request information.
265
- * @param {ExpressResponse} res
266
- * The server's response information.
267
- * @param {ExpressNext} next
268
- * The function to call the next middleware.
269
- * @parent module:controllers
270
- */
271
-
272
- /**
273
- * This is called in order for the helper to handle a fetch request error.
274
- *
275
- * @callback GatewayHelperServiceErrorHandler
276
- * @param {Error} error
277
- * The fetch request error.
278
- * @param {GatewayControllerEndpointInformation} endpoint
279
- * The information for the endpoint responsible of creating the route.
280
- * @param {ExpressRequest} req
281
- * The server's incoming request information.
282
- * @param {ExpressResponse} res
283
- * The server's response information.
284
- * @param {ExpressNext} next
285
- * The function to call the next middleware.
286
- * @parent module:controllers
287
- */
288
-
289
- /**
290
- * A service that can have specific methods the gateway will call in order to modify
291
- * requests,
292
- * responses, handle errors, etc.
293
- *
294
- * @typedef {Object} GatewayHelperService
295
- * @property {?GatewayHelperServiceRequestReducer} reduceEndpointRequest
296
- * This is called in order to allow the helper to modify the information of a request that
297
- * is about the fired.
298
- * @property {?GatewayHelperServiceResponseReducer} reduceEndpointResponse
299
- * This is called in order to allow the helper to modify the information of a response the
300
- * gateway made.
301
- * @property {?GatewayHelperServiceStreamVerification} shouldStreamEndpointResponse
302
- * This is called in order to allow the helper to decide whether a fetch request response
303
- * should be added to the server's response stream. This will only be called if the helper
304
- * also implements `handleEndpointResponse`.
305
- * @property {?GatewayHelperServiceResponseHandler} handleEndpointResponse
306
- * This is called in order for the helper to handle a response. This is only called if
307
- * `shouldStreamEndpointResponse` returned `false`.
308
- * @property {?GatewayHelperServiceErrorHandler} handleEndpointError
309
- * This is called in order for the helper to handle a fetch request error.
310
- * @parent module:controllers
311
- */
312
-
313
- /**
314
- * @callback GatewayControllerMiddlewaresFn
315
- * @param {Jimpex} app A reference for the container.
316
- * @returns {MiddlewareLike[]}
317
- * @parent module:controllers
318
- */
319
-
320
- /**
321
- * Geneters routes that will act as a gateway to an specific set of endpoints.
322
- *
323
- * @parent module:controllers
324
- */
325
- class GatewayController {
326
- /**
327
- * @param {GatewayConfiguration} gatewayConfig
328
- * This is a configuration object very similar to the one {@link APIClient} uses in
329
- * order to configure the endpoints; the controller uses it to create the routes and to
330
- * validate the HTTP methods.
331
- * @param {string} route
332
- * The route where the controller will be mounted.
333
- * @param {HTTP} http
334
- * To make the fetch requests on the routes.
335
- * @param {GatewayControllerOptions} [options={}]
336
- * The options to configure how the gateway will manage the requests and the responses.
337
- * @param {?GatewayHelperService} [helperService=null]
338
- * A service that can have specific methods the gateway will call in order to modify
339
- * requests,
340
- * responses, handle errors, etc.
341
- */
342
- constructor(gatewayConfig, route, http, options = {}, helperService = null) {
343
- /**
344
- * The options to configure how the gateway will manage the requests and the
345
- * responses.
346
- *
347
- * @type {GatewayControllerOptions}
348
- * @access protected
349
- * @ignore
350
- * @todo Use deepAssign for this merge.
351
- */
352
- this._options = this._normalizeOptions(
353
- ObjectUtils.merge(
354
- {
355
- root: '',
356
- configurationSetting: 'api',
357
- headers: {
358
- useXForwardedFor: true,
359
- copyCustomHeaders: true,
360
- copy:
361
- options.headers && options.headers.copy
362
- ? options.headers.copy
363
- : ['authorization', 'content-type', 'referer', 'user-agent'],
364
- remove:
365
- options.headers && options.headers.remove
366
- ? options.headers.remove
367
- : ['server', 'x-powered-by', 'content-encoding'],
368
- },
369
- },
370
- options,
371
- ),
372
- );
373
- /**
374
- * The configuration for the API the controller will make requests to.
375
- *
376
- * @type {GatewayConfiguration}
377
- * @access protected
378
- * @ignore
379
- */
380
- this._gatewayConfig = {
381
- ...gatewayConfig,
382
- url: removeSlashes(gatewayConfig.url, false, true),
383
- };
384
- /**
385
- * A local reference for the `http` service.
386
- *
387
- * @type {HTTP}
388
- * @access protected
389
- * @ignore
390
- */
391
- this._http = http;
392
- /**
393
- * A list of the allowed HTTP methods an endpoint can have.
394
- *
395
- * @type {string[]}
396
- * @access protected
397
- * @ignore
398
- */
399
- this._allowedHTTPMethods = [
400
- 'get',
401
- 'head',
402
- 'post',
403
- 'put',
404
- 'delete',
405
- 'connect',
406
- 'options',
407
- 'trace',
408
- ];
409
- /**
410
- * A flat dictionary of the gateway endpoints. The key is the path on the original
411
- * dictionary (`this._gatewayConfig.gateway`) and the value is either the path
412
- * (`string`)
413
- * or the endpoint settings ({@link GatewayConfigurationEndpoint}).
414
- *
415
- * @type {Object.<string, string | GatewayConfigurationEndpoint>}
416
- * @access protected
417
- * @ignore
418
- */
419
- this._endpoints = this._getNormalizedEndpoints();
420
- /**
421
- * The route where the controller is mounted.
422
- *
423
- * @type {string}
424
- * @access protected
425
- * @ignore
426
- */
427
- this._route = removeSlashes(route);
428
- /**
429
- * A regular expression that will be used to remove the controller route from a
430
- * request path. This will allow the main middleware to extract the path to where the
431
- * request should be made.
432
- *
433
- * @type {RegExp}
434
- * @access protected
435
- * @ignore
436
- */
437
- this._routeExpression = this._createRouteExpression();
438
- /**
439
- * This is the list of routes the controller will define.
440
- *
441
- * @type {GatewayControllerRoute[]}
442
- * @access protected
443
- * @ignore
444
- */
445
- this._routes = this._createEndpointRoutes();
446
- /**
447
- * An {@link APIClient} configuration based on the controller routes.
448
- *
449
- * @type {APIClientConfiguration}
450
- * @access protected
451
- * @ignore
452
- */
453
- this._apiClientConfiguration = this._createAPIClientConfiguration();
454
- /**
455
- * A service that can have specific methods the gateway will call in order to modify
456
- * requests, responses, handle errors, etc.
457
- *
458
- * @type {?GatewayHelperService}
459
- * @access protected
460
- */
461
- this._helperService = helperService;
462
- /**
463
- * A dictionary of boolean flags that specify if a helper service has method. This is
464
- * to avoid checking if the helper is defined and if "x method" is a function. If no
465
- * helper was specified, the object will have all the flags set to `false`.
466
- *
467
- * @type {Object.<string, boolean>}
468
- * @access protected
469
- * @ignore
470
- */
471
- this._helperServiceInfo = this._createHelperServiceInfo();
472
- }
473
- /**
474
- * Defines all the routes on a given router.
475
- *
476
- * @param {ExpressRouter} router The router where all the routes will
477
- * be added.
478
- * @param {ExpressMiddleware[]} [middlewares=[]] A list of custom middlewares that will
479
- * be added before the one that makes the
480
- * request.
481
- * @returns {ExpressRouter}
482
- */
483
- addRoutes(router, middlewares = []) {
484
- this._routes.forEach((route) =>
485
- route.methods.forEach((info) =>
486
- this._addRoute(
487
- router,
488
- info.method,
489
- route.route,
490
- this._getMiddleware(info.endpoint),
491
- middlewares,
492
- ),
493
- ),
494
- );
495
-
496
- return router;
497
- }
498
- /**
499
- * An {@link APIClient} configuration based on the controller routes.
500
- *
501
- * @type {APIClientConfiguration}
502
- */
503
- get endpointsForAPIClient() {
504
- return this._apiClientConfiguration;
505
- }
506
- /**
507
- * The configuration for the API the controller will make requests to.
508
- *
509
- * @type {GatewayConfiguration}
510
- */
511
- get gatewayConfig() {
512
- return this._gatewayConfig;
513
- }
514
- /**
515
- * The options to configure how the gateway will manage the requests and the responses.
516
- *
517
- * @type {GatewayControllerOptions}
518
- */
519
- get options() {
520
- return this._options;
521
- }
522
- /**
523
- * Adds a route on a given router.
524
- *
525
- * @param {ExpressRouter} router The router where the route will be
526
- * added.
527
- * @param {string} method The HTTP method for the route.
528
- * @param {string} route The path for the route.
529
- * @param {ExpressMiddleware} endpointMiddleware The middleware that makes the request.
530
- * @param {Array} middlewares Extra middlewares to add before the
531
- * main one.
532
- * @returns {ExpressRouter}
533
- * @access protected
534
- * @ignore
535
- */
536
- _addRoute(router, method, route, endpointMiddleware, middlewares) {
537
- return router[method](route, [...middlewares, endpointMiddleware]);
538
- }
539
- /**
540
- * Based on the controller options and the gateway endpoints, this method will create an
541
- * API client configuration that can be used to make requests to this controller.
542
- *
543
- * @returns {APIClientConfiguration}
544
- * @access protected
545
- * @ignore
546
- */
547
- _createAPIClientConfiguration() {
548
- let endpoints;
549
- const { root } = this._options;
550
- if (root) {
551
- endpoints = Object.keys(this._endpoints).reduce((acc, name) => {
552
- const endpoint = this._endpoints[name];
553
- let newEndpoint;
554
- if (typeof endpoint === 'string') {
555
- newEndpoint = removeSlashes(endpoint);
556
- newEndpoint = `${root}/${newEndpoint}`;
557
- } else {
558
- const endpointPath = removeSlashes(endpoint.path);
559
- newEndpoint = {
560
- ...endpoint,
561
- path: `${root}/${endpointPath}`,
562
- };
563
- }
564
-
565
- return {
566
- ...acc,
567
- [name]: newEndpoint,
568
- };
569
- }, {});
570
- } else {
571
- endpoints = this._endpoints;
572
- }
573
- return {
574
- url: `/${this._route}`,
575
- endpoints: {
576
- [this._options.configurationSetting]: ObjectUtils.unflat(endpoints),
577
- },
578
- };
579
- }
580
- /**
581
- * Based on the information from the endpoints, this method will create the routes the
582
- * controller will later add on a router.
583
- *
584
- * @returns {GatewayControllerRoute[]}
585
- * @throws {Error} If there's more than one endpoint using the same path with the same
586
- * HTTP method.
587
- * @access protected
588
- * @ignore
589
- */
590
- _createEndpointRoutes() {
591
- const routePrefixes = this._options.root ? `/${this._options.root}/` : '/';
592
- const routes = {};
593
- Object.keys(this._endpoints).forEach((name) => {
594
- const endpoint = this._endpoints[name];
595
- let endpointPath;
596
- let endpointMethod;
597
- if (typeof endpoint === 'string') {
598
- endpointPath = endpoint;
599
- endpointMethod = 'all';
600
- } else {
601
- endpointPath = endpoint.path;
602
- endpointMethod = endpoint.method
603
- ? this._normalizeHTTPMethod(endpoint.method)
604
- : 'all';
605
- }
606
-
607
- endpointPath = removeSlashes(endpointPath);
608
- if (!routes[endpointPath]) {
609
- routes[endpointPath] = {
610
- path: endpointPath,
611
- methods: {},
612
- };
613
- }
614
-
615
- if (routes[endpointPath].methods[endpointMethod]) {
616
- const repeatedEndpoint = routes[endpointPath].methods[endpointMethod];
617
- throw new Error(
618
- "You can't have two gateway endpoints to the same path and with the same " +
619
- `HTTP method: '${repeatedEndpoint}' and '${name}'`,
620
- );
621
- }
622
-
623
- routes[endpointPath].methods[endpointMethod] = name;
624
- });
625
-
626
- return Object.keys(routes).map((endpointPath) => {
627
- const info = routes[endpointPath];
628
- return {
629
- path: info.path,
630
- route: `${routePrefixes}${info.path}`,
631
- methods: Object.keys(info.methods).map((methodName) => ({
632
- method: methodName,
633
- endpoint: {
634
- name: info.methods[methodName],
635
- settings: this._endpoints[info.methods[methodName]],
636
- },
637
- })),
638
- };
639
- });
640
- }
641
- /**
642
- * Validates if a server helper exists and creates a dictionary with flags for all the
643
- * methods a helper can have; this will allow other methods to check if the "helper
644
- * method X" is available without having to check if the helper is defined and if
645
- * "method X" is a function.
646
- *
647
- * @returns {Object.<string, boolean>}
648
- * @access protected
649
- * @ignore
650
- */
651
- _createHelperServiceInfo() {
652
- const methods = [
653
- 'reduceEndpointRequest',
654
- 'reduceEndpointResponse',
655
- 'shouldStreamEndpointResponse',
656
- 'handleEndpointResponse',
657
- 'handleEndpointError',
658
- ];
659
- let result;
660
- if (this._helperService) {
661
- result = methods.reduce(
662
- (methodsDict, name) => ({
663
- ...methodsDict,
664
- [name]: typeof this._helperService[name] === 'function',
665
- }),
666
- {},
667
- );
668
- } else {
669
- result = methods.reduce(
670
- (methodsDict, name) => ({ ...methodsDict, [name]: false }),
671
- {},
672
- );
673
- }
674
-
675
- return result;
676
- }
677
- /**
678
- * Creates a regular expression the main middleware will later use in order to remove
679
- * the controller route from the request url. That's needed in order to build the URL
680
- * where the request will be made.
681
- *
682
- * @returns {RegExp}
683
- * @access protected
684
- * @ignore
685
- */
686
- _createRouteExpression() {
687
- return createRouteExpression(
688
- this._options.root ? `${this._route}/${this._options.root}` : this._route,
689
- true,
690
- true,
691
- );
692
- }
693
- /**
694
- * Generates a middleware that will make a request and stream back the response.
695
- *
696
- * @param {GatewayControllerEndpointInformation} endpoint
697
- * The information for the enpdoint for which the middleware is being created.
698
- * @returns {ExpressMiddleware}
699
- * @access protected
700
- * @ignore
701
- */
702
- _getMiddleware(endpoint) {
703
- return (req, res, next) => {
704
- // Remove the controller route from the requested URL.
705
- const reqPath = req.originalUrl.replace(this._routeExpression, '');
706
- // Define the request options.
707
- const options = {
708
- method: req.method.toUpperCase(),
709
- headers: {},
710
- };
711
- // Copy the specified headers from the incoming request.
712
- this._options.headers.copy.forEach((name) => {
713
- if (req.headers[name]) {
714
- options.headers[name] = req.headers[name];
715
- }
716
- });
717
- // If enabled, copy the custom headers.
718
- if (this._options.headers.copyCustomHeaders) {
719
- options.headers = ObjectUtils.merge(
720
- options.headers,
721
- this._http.getCustomHeadersFromRequest(req),
722
- );
723
- }
724
- // If enabled, add the header with the request's IP.
725
- if (this._options.headers.useXForwardedFor) {
726
- options.headers['x-forwarded-for'] = this._http.getIPFromRequest(req);
727
- }
728
- /**
729
- * If the request has a body and the method is not `GET`, stringify it and addit to
730
- * the options.
731
- */
732
- if (options.method !== 'GET' && typeof req.body === 'object') {
733
- options.body = JSON.stringify(req.body);
734
- // If there's no `content-type`, let's assume it's JSON.
735
- if (!options.headers['content-type']) {
736
- options.headers['content-type'] = 'application/json';
737
- }
738
- }
739
- // Reduce the request information.
740
- const request = this._reduceEndpointRequest(
741
- {
742
- url: `${this._gatewayConfig.url}/${reqPath}`,
743
- options,
744
- },
745
- endpoint,
746
- req,
747
- res,
748
- next,
749
- );
750
- // Make the fetch request.
751
- return this._http
752
- .fetch(request.url, request.options)
753
- .then((response) => {
754
- // Reduce the response.
755
- const newResponse = this._reduceEndpointResponse(
756
- response,
757
- endpoint,
758
- req,
759
- res,
760
- next,
761
- );
762
- // If the response should be sent down on the stream...
763
- if (this._shouldStreamEndpointResponse(newResponse, endpoint, req, res, next)) {
764
- // Update the server's response status.
765
- res.status(newResponse.status);
766
- // Copy the headers.
767
- newResponse.headers.forEach((value, name) => {
768
- if (!this._options.headers.remove.includes(name)) {
769
- res.setHeader(name, value);
770
- }
771
- });
772
- // Pipe the server's response into the fetch response stream.
773
- newResponse.body.pipe(res).on('error', (error) => {
774
- next(error);
775
- });
776
- } else {
777
- // Otherwise, let the helper handle the response.
778
- this._handleEndpointResponse(newResponse, endpoint, req, res, next);
779
- }
780
- })
781
- .catch((error) => this._handleEndpointError(error, endpoint, req, res, next));
782
- };
783
- }
784
- /**
785
- * Flattens all the endpoints from gateway configuration into a one level dictionary,
786
- * where the key are the paths they used to have on the original configuration, and the
787
- * values are the endpoints definitions.
788
- *
789
- * @returns {Object.<string, string | GatewayConfigurationEndpoint>}
790
- * @access protected
791
- * @ignore
792
- */
793
- _getNormalizedEndpoints() {
794
- return ObjectUtils.flat(
795
- this._gatewayConfig.gateway,
796
- '.',
797
- '',
798
- (ignore, value) => typeof value.path === 'undefined',
799
- );
800
- }
801
- /**
802
- * This method is called in order to handle a fetch request error. It will check if a
803
- * helper is defined and allow it to do it, or fallback and call the next middleware.
804
- *
805
- * @param {Error} error
806
- * The fetch request error.
807
- * @param {GatewayControllerEndpointInformation} endpoint
808
- * The information for the endpoint responsible of creating the route.
809
- * @param {ExpressRequest} req
810
- * The server's incoming request information.
811
- * @param {ExpressResponse} res
812
- * The server's response information.
813
- * @param {ExpressNext} next
814
- * The function to call the next middleware.
815
- * @returns {*}
816
- * @access protected
817
- * @ignore
818
- */
819
- _handleEndpointError(error, endpoint, req, res, next) {
820
- return this._helperServiceInfo.handleEndpointError
821
- ? this._helperService.handleEndpointError(error, endpoint, req, res, next)
822
- : next(error);
823
- }
824
- /**
825
- * This is called when the helper say that a fetch response shouldn't be sent, so the
826
- * controller will allow it to handle the response by itself.
827
- *
828
- * @param {Object} response
829
- * The response generated by the fetch request.
830
- * @param {GatewayControllerEndpointInformation} endpoint
831
- * The information for the endpoint responsible of creating the route.
832
- * @param {ExpressRequest} req
833
- * The server's incoming request information.
834
- * @param {ExpressResponse} res
835
- * The server's response information.
836
- * @param {ExpressNext} next
837
- * The function to call the next middleware.
838
- * @returns {*}
839
- * @access protected
840
- * @ignore
841
- */
842
- _handleEndpointResponse(response, endpoint, req, res, next) {
843
- return this._helperService.handleEndpointResponse(response, endpoint, req, res, next);
844
- }
845
- /**
846
- * This is a helper method used in order to validate if an HTTP method can be used in
847
- * order to define a route in the router. If the given method is not on the list of
848
- * allowed methods,
849
- * it will be "normalized" to `all`. It also transforms the method into lower case.
850
- *
851
- * @param {string} method The method to validate.
852
- * @returns {string}
853
- * @access protected
854
- * @ignore
855
- */
856
- _normalizeHTTPMethod(method) {
857
- const newMethod = method.toLowerCase();
858
- return this._allowedHTTPMethods.includes(newMethod) ? newMethod : 'all';
859
- }
860
- /**
861
- * Normalizes the options recevied by the controller:
862
- * - Removes any trailing and leading slashes from the `root` path, if defined.
863
- *
864
- * @param {GatewayControllerOptions} options The options to normalize.
865
- * @returns {GatewayControllerOptions}
866
- * @access protected
867
- * @ignore
868
- */
869
- _normalizeOptions(options) {
870
- let newOptions;
871
- if (options.root) {
872
- const root = removeSlashes(options.root).trim();
873
- newOptions = { ...options, root };
874
- } else {
875
- newOptions = options;
876
- }
877
-
878
- return newOptions;
879
- }
880
- /**
881
- * This method is called in order to reduce a fetch request information. It will check
882
- * if a helper is defined and allow it to do it, or fallback and return the given
883
- * information.
884
- *
885
- * @param {GatewayControllerRequest} request
886
- * The information for a request the controller will make.
887
- * @param {GatewayControllerEndpointInformation} endpoint
888
- * The information for the endpoint responsible of creating the route.
889
- * @param {ExpressRequest} req
890
- * The server's incoming request information.
891
- * @param {ExpressResponse} res
892
- * The server's response information.
893
- * @param {ExpressNext} next
894
- * The function to call the next middleware.
895
- * @returns {GatewayControllerRequest}
896
- * @access protected
897
- * @ignore
898
- */
899
- _reduceEndpointRequest(request, endpoint, req, res, next) {
900
- return this._helperServiceInfo.reduceEndpointRequest
901
- ? this._helperService.reduceEndpointRequest(request, endpoint, req, res, next)
902
- : request;
903
- }
904
- /**
905
- * This method is called in order to reduce a fetch response information. It will check
906
- * if a helper is defined and allow it to do it, or fallback and return the given
907
- * information.
908
- *
909
- * @param {Response} response
910
- * The response generated by the fetch request.
911
- * @param {GatewayControllerEndpointInformation} endpoint
912
- * The information for the endpoint responsible of creating the route.
913
- * @param {ExpressRequest} req
914
- * The server's incoming request information.
915
- * @param {ExpressResponse} res
916
- * The server's response information.
917
- * @param {ExpressNext} next
918
- * The function to call the next middleware.
919
- * @returns {Response}
920
- * @access protected
921
- * @ignore
922
- */
923
- _reduceEndpointResponse(response, endpoint, req, res, next) {
924
- return this._helperServiceInfo.reduceEndpointResponse
925
- ? this._helperService.reduceEndpointResponse(response, endpoint, req, res, next)
926
- : response;
927
- }
928
- /**
929
- * This method is called in order to validate if the main middleware should pipe the
930
- * fetch response stream into the server's response or if the helper will handle the
931
- * response.
932
- * This method will only call the helper if it implements both
933
- * `shouldStreamEndpointResponse`
934
- * and `handleEndpointResponse`.
935
- *
936
- * @param {Response} response
937
- * The response generated by the fetch request.
938
- * @param {GatewayControllerEndpointInformation} endpoint
939
- * The information for the endpoint responsible of creating the route.
940
- * @param {ExpressRequest} req
941
- * The server's incoming request information.
942
- * @param {ExpressResponse} res
943
- * The server's response information.
944
- * @param {ExpressNext} next
945
- * The function to call the next middleware.
946
- * @returns {boolean}
947
- * @access protected
948
- * @ignore
949
- */
950
- _shouldStreamEndpointResponse(response, endpoint, req, res, next) {
951
- return this._helperServiceInfo.shouldStreamEndpointResponse &&
952
- this._helperServiceInfo.handleEndpointResponse
953
- ? this._helperService.shouldStreamEndpointResponse(
954
- response,
955
- endpoint,
956
- req,
957
- res,
958
- next,
959
- )
960
- : true;
961
- }
962
- }
963
- /**
964
- * This controller allows you to have gateway routes that actually make requests and
965
- * respond with the contents from an specified API.
966
- *
967
- * @type {ProviderCreator<GatewayControllerCreatorOptions>}
968
- * @parent module:controllers
969
- */
970
- const gatewayController = providerCreator((options = {}) => (app, route) => {
971
- /**
972
- * Formats the name in order to keep consistency with the helper service and the configuration
973
- * setting: If the `serviceName` is different from the default, make sure it ends with
974
- * `Gateway`, set the default helper service name to `${serviceName}Helper` and the default
975
- * configuration setting to the same as the service name (without the `Gateway`).
976
- * This way, if you just use `myApi`, the service name will be `myApiGateway`, the helper name
977
- * will be `myApiGatewayHelper` and the configuration setting `myApi`.
978
- */
979
- const defaultServiceName = 'apiGateway';
980
- let defaultHelperServiceName = 'apiGatewayHelper';
981
- let defaultConfigurationSetting = 'api';
982
- let { serviceName = defaultServiceName } = options;
983
- if (serviceName !== defaultServiceName) {
984
- defaultConfigurationSetting = serviceName;
985
- if (!serviceName.match(/gateway$/i)) {
986
- serviceName = `${serviceName}Gateway`;
987
- }
988
- defaultHelperServiceName = `${serviceName}Helper`;
989
- }
990
- /**
991
- * Get the settings the controller needs in order to use with the container before creating
992
- * the instance.
993
- */
994
- const {
995
- helperServiceName = defaultHelperServiceName,
996
- configurationSetting = defaultConfigurationSetting,
997
- GatewayClass = GatewayController,
998
- } = options;
999
- /**
1000
- * Update the options with the resolved configuration setting name, because the class will
1001
- * needed when generating API Client endpoints.
1002
- */
1003
- const newOptions = {
1004
- ...options,
1005
- configurationSetting,
1006
- };
1007
- // Register the controller on the container.
1008
- app.set(
1009
- serviceName,
1010
- () =>
1011
- new GatewayClass(
1012
- app.get('appConfiguration').get(configurationSetting),
1013
- route,
1014
- app.get('http'),
1015
- newOptions,
1016
- helperServiceName ? app.try(helperServiceName) : null,
1017
- ),
1018
- );
1019
-
1020
- return controller(() => {
1021
- // Get the controller
1022
- const ctrl = app.get(serviceName);
1023
- /**
1024
- * Check if there are actual middlewares to be included, and in case there are Jimpex
1025
- * middlewares, connect them.
1026
- */
1027
- let useMiddlewares;
1028
- if (options.middlewares) {
1029
- useMiddlewares = options
1030
- .middlewares(app)
1031
- .map((middleware) => (middleware.connect ? middleware.connect(app) : middleware));
1032
- }
1033
- // Add the routes to the router and return it.
1034
- return ctrl.addRoutes(app.get('router'), useMiddlewares);
1035
- });
1036
- });
1037
-
1038
- module.exports.GatewayController = GatewayController;
1039
- module.exports.gatewayController = gatewayController;