jimpex 8.0.0 → 10.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 (215) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +2 -2
  3. package/dist/app/index.d.mts +14 -0
  4. package/dist/app/index.d.ts +5 -4
  5. package/dist/app/index.js +4 -0
  6. package/dist/app/jimpex.d.mts +14 -0
  7. package/dist/app/jimpex.d.ts +3 -2
  8. package/dist/app/jimpex.js +238 -2
  9. package/dist/app/jimpex.js.map +1 -1
  10. package/dist/controllers/common/config.d.mts +79 -0
  11. package/dist/controllers/common/config.d.ts +4 -3
  12. package/dist/controllers/common/config.js +18 -0
  13. package/dist/controllers/common/config.js.map +1 -1
  14. package/dist/controllers/common/health.d.mts +83 -0
  15. package/dist/controllers/common/health.d.ts +5 -4
  16. package/dist/controllers/common/health.js +18 -0
  17. package/dist/controllers/common/health.js.map +1 -1
  18. package/dist/controllers/common/index.d.mts +22 -0
  19. package/dist/controllers/common/index.d.ts +5 -4
  20. package/dist/controllers/common/index.js +6 -0
  21. package/dist/controllers/common/statics.d.mts +223 -0
  22. package/dist/controllers/common/statics.d.ts +13 -5
  23. package/dist/controllers/common/statics.js +60 -3
  24. package/dist/controllers/common/statics.js.map +1 -1
  25. package/dist/controllers/index.d.mts +25 -0
  26. package/dist/controllers/index.d.ts +5 -4
  27. package/dist/controllers/index.js +5 -0
  28. package/dist/controllers/utils/gateway.d.mts +725 -0
  29. package/dist/controllers/utils/gateway.d.ts +5 -4
  30. package/dist/controllers/utils/gateway.js +155 -0
  31. package/dist/controllers/utils/gateway.js.map +1 -1
  32. package/dist/controllers/utils/index.d.mts +17 -0
  33. package/dist/controllers/utils/index.d.ts +4 -3
  34. package/dist/controllers/utils/index.js +4 -0
  35. package/dist/esm/app/jimpex.js +235 -3
  36. package/dist/esm/app/jimpex.js.map +1 -1
  37. package/dist/esm/{chunk-T2T6Q22Z.js → chunk-F3FYYIAV.js} +1 -1
  38. package/dist/esm/controllers/common/config.js +19 -1
  39. package/dist/esm/controllers/common/config.js.map +1 -1
  40. package/dist/esm/controllers/common/health.js +19 -1
  41. package/dist/esm/controllers/common/health.js.map +1 -1
  42. package/dist/esm/controllers/common/statics.js +57 -4
  43. package/dist/esm/controllers/common/statics.js.map +1 -1
  44. package/dist/esm/controllers/utils/gateway.js +156 -1
  45. package/dist/esm/controllers/utils/gateway.js.map +1 -1
  46. package/dist/esm/middlewares/common/errorHandler.js +25 -1
  47. package/dist/esm/middlewares/common/errorHandler.js.map +1 -1
  48. package/dist/esm/middlewares/common/forceHTTPS.js +13 -1
  49. package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -1
  50. package/dist/esm/middlewares/common/hsts.js +22 -1
  51. package/dist/esm/middlewares/common/hsts.js.map +1 -1
  52. package/dist/esm/middlewares/html/fastHTML.js +55 -1
  53. package/dist/esm/middlewares/html/fastHTML.js.map +1 -1
  54. package/dist/esm/middlewares/html/showHTML.js +33 -1
  55. package/dist/esm/middlewares/html/showHTML.js.map +1 -1
  56. package/dist/esm/middlewares/utils/versionValidator.js +35 -1
  57. package/dist/esm/middlewares/utils/versionValidator.js.map +1 -1
  58. package/dist/esm/services/common/appError.js +48 -2
  59. package/dist/esm/services/common/appError.js.map +1 -1
  60. package/dist/esm/services/common/httpError.js +10 -1
  61. package/dist/esm/services/common/httpError.js.map +1 -1
  62. package/dist/esm/services/common/index.js +1 -1
  63. package/dist/esm/services/common/sendFile.js +1 -1
  64. package/dist/esm/services/frontend/frontendFs.js +29 -1
  65. package/dist/esm/services/frontend/frontendFs.js.map +1 -1
  66. package/dist/esm/services/frontend/index.js +1 -1
  67. package/dist/esm/services/html/htmlGenerator.js +51 -1
  68. package/dist/esm/services/html/htmlGenerator.js.map +1 -1
  69. package/dist/esm/services/html/index.js +1 -1
  70. package/dist/esm/services/http/apiClient.js +27 -1
  71. package/dist/esm/services/http/apiClient.js.map +1 -1
  72. package/dist/esm/services/http/http.js +60 -5
  73. package/dist/esm/services/http/http.js.map +1 -1
  74. package/dist/esm/services/http/index.js +1 -1
  75. package/dist/esm/services/http/responsesBuilder.js +49 -2
  76. package/dist/esm/services/http/responsesBuilder.js.map +1 -1
  77. package/dist/esm/services/utils/ensureBearerToken.js +17 -1
  78. package/dist/esm/services/utils/ensureBearerToken.js.map +1 -1
  79. package/dist/esm/services/utils/index.js +1 -1
  80. package/dist/esm/utils/fns/others.js +1 -1
  81. package/dist/esm/utils/fns/routes.js +1 -1
  82. package/dist/esm/utils/fns/routes.js.map +1 -1
  83. package/dist/esm/utils/fns/statuses.js +1 -1
  84. package/dist/esm/utils/fns/text.js +1 -1
  85. package/dist/esm/utils/wrappers.js +1 -1
  86. package/dist/index-Bwf7JHu9.d.mts +1287 -0
  87. package/dist/{jimpex-7eaee271.d.ts → index-C6I3NCC-.d.ts} +15 -6
  88. package/dist/index.d.mts +47 -0
  89. package/dist/index.d.ts +3 -2
  90. package/dist/index.js +9 -0
  91. package/dist/middlewares/common/errorHandler.d.mts +132 -0
  92. package/dist/middlewares/common/errorHandler.d.ts +5 -4
  93. package/dist/middlewares/common/errorHandler.js +24 -0
  94. package/dist/middlewares/common/errorHandler.js.map +1 -1
  95. package/dist/middlewares/common/forceHTTPS.d.mts +69 -0
  96. package/dist/middlewares/common/forceHTTPS.d.ts +6 -5
  97. package/dist/middlewares/common/forceHTTPS.js +12 -0
  98. package/dist/middlewares/common/forceHTTPS.js.map +1 -1
  99. package/dist/middlewares/common/hsts.d.mts +110 -0
  100. package/dist/middlewares/common/hsts.d.ts +6 -5
  101. package/dist/middlewares/common/hsts.js +21 -0
  102. package/dist/middlewares/common/hsts.js.map +1 -1
  103. package/dist/middlewares/common/index.d.mts +22 -0
  104. package/dist/middlewares/common/index.d.ts +4 -3
  105. package/dist/middlewares/common/index.js +6 -0
  106. package/dist/middlewares/html/fastHTML.d.mts +180 -0
  107. package/dist/middlewares/html/fastHTML.d.ts +5 -5
  108. package/dist/middlewares/html/fastHTML.js +54 -0
  109. package/dist/middlewares/html/fastHTML.js.map +1 -1
  110. package/dist/middlewares/html/index.d.mts +21 -0
  111. package/dist/middlewares/html/index.d.ts +4 -4
  112. package/dist/middlewares/html/index.js +5 -0
  113. package/dist/middlewares/html/showHTML.d.mts +127 -0
  114. package/dist/middlewares/html/showHTML.d.ts +5 -5
  115. package/dist/middlewares/html/showHTML.js +32 -0
  116. package/dist/middlewares/html/showHTML.js.map +1 -1
  117. package/dist/middlewares/index.d.mts +30 -0
  118. package/dist/middlewares/index.d.ts +4 -4
  119. package/dist/middlewares/index.js +6 -0
  120. package/dist/middlewares/utils/index.d.mts +20 -0
  121. package/dist/middlewares/utils/index.d.ts +4 -3
  122. package/dist/middlewares/utils/index.js +4 -0
  123. package/dist/middlewares/utils/versionValidator.d.mts +215 -0
  124. package/dist/middlewares/utils/versionValidator.d.ts +5 -4
  125. package/dist/middlewares/utils/versionValidator.js +34 -0
  126. package/dist/middlewares/utils/versionValidator.js.map +1 -1
  127. package/dist/services/common/appError.d.mts +139 -0
  128. package/dist/services/common/appError.d.ts +5 -4
  129. package/dist/services/common/appError.js +47 -1
  130. package/dist/services/common/appError.js.map +1 -1
  131. package/dist/services/common/httpError.d.mts +80 -0
  132. package/dist/services/common/httpError.d.ts +5 -4
  133. package/dist/services/common/httpError.js +9 -0
  134. package/dist/services/common/httpError.js.map +1 -1
  135. package/dist/services/common/index.d.mts +47 -0
  136. package/dist/services/common/index.d.ts +4 -4
  137. package/dist/services/common/index.js +4 -1
  138. package/dist/services/common/sendFile.d.mts +102 -0
  139. package/dist/services/common/sendFile.d.ts +4 -4
  140. package/dist/services/frontend/frontendFs.d.mts +96 -0
  141. package/dist/services/frontend/frontendFs.d.ts +4 -4
  142. package/dist/services/frontend/frontendFs.js +32 -0
  143. package/dist/services/frontend/frontendFs.js.map +1 -1
  144. package/dist/services/frontend/index.d.mts +40 -0
  145. package/dist/services/frontend/index.d.ts +4 -4
  146. package/dist/services/frontend/index.js +2 -1
  147. package/dist/services/html/htmlGenerator.d.mts +237 -0
  148. package/dist/services/html/htmlGenerator.d.ts +3 -3
  149. package/dist/services/html/htmlGenerator.js +50 -0
  150. package/dist/services/html/htmlGenerator.js.map +1 -1
  151. package/dist/services/html/index.d.mts +43 -0
  152. package/dist/services/html/index.d.ts +4 -4
  153. package/dist/services/html/index.js +2 -1
  154. package/dist/services/http/apiClient.d.mts +170 -0
  155. package/dist/services/http/apiClient.d.ts +5 -4
  156. package/dist/services/http/apiClient.js +26 -0
  157. package/dist/services/http/apiClient.js.map +1 -1
  158. package/dist/services/http/http.d.mts +182 -0
  159. package/dist/services/http/http.d.ts +15 -8
  160. package/dist/services/http/http.js +63 -4
  161. package/dist/services/http/http.js.map +1 -1
  162. package/dist/services/http/index.d.mts +51 -0
  163. package/dist/services/http/index.d.ts +4 -3
  164. package/dist/services/http/index.js +4 -1
  165. package/dist/services/http/responsesBuilder.d.mts +179 -0
  166. package/dist/services/http/responsesBuilder.d.ts +4 -3
  167. package/dist/services/http/responsesBuilder.js +48 -1
  168. package/dist/services/http/responsesBuilder.js.map +1 -1
  169. package/dist/services/index.d.mts +33 -0
  170. package/dist/services/index.d.ts +4 -4
  171. package/dist/services/index.js +8 -0
  172. package/dist/services/utils/ensureBearerToken.d.mts +158 -0
  173. package/dist/services/utils/ensureBearerToken.d.ts +6 -5
  174. package/dist/services/utils/ensureBearerToken.js +16 -0
  175. package/dist/services/utils/ensureBearerToken.js.map +1 -1
  176. package/dist/services/utils/index.d.mts +44 -0
  177. package/dist/services/utils/index.d.ts +4 -3
  178. package/dist/services/utils/index.js +2 -1
  179. package/dist/types/events.d.mts +14 -0
  180. package/dist/types/events.d.ts +5 -4
  181. package/dist/types/express.d.mts +10 -0
  182. package/dist/types/express.d.ts +1 -1
  183. package/dist/types/http.d.mts +82 -0
  184. package/dist/types/http.d.ts +5 -2
  185. package/dist/types/http.js.map +1 -1
  186. package/dist/types/index.d.mts +14 -0
  187. package/dist/types/index.d.ts +2 -2
  188. package/dist/types/index.js +9 -0
  189. package/dist/types/options.d.mts +14 -0
  190. package/dist/types/options.d.ts +4 -3
  191. package/dist/types/utils.d.mts +48 -0
  192. package/dist/types/utils.d.ts +1 -1
  193. package/dist/types/wootils.d.mts +4 -0
  194. package/dist/utils/fns/index.d.mts +5 -0
  195. package/dist/utils/fns/index.js +7 -0
  196. package/dist/utils/fns/others.d.mts +17 -0
  197. package/dist/utils/fns/routes.d.mts +39 -0
  198. package/dist/utils/fns/routes.js.map +1 -1
  199. package/dist/utils/fns/statuses.d.mts +45 -0
  200. package/dist/utils/fns/statuses.d.ts +1 -1
  201. package/dist/utils/fns/statuses.js +4 -0
  202. package/dist/utils/fns/statuses.js.map +1 -1
  203. package/dist/utils/fns/text.d.mts +9 -0
  204. package/dist/utils/index.d.mts +19 -0
  205. package/dist/utils/index.d.ts +5 -4
  206. package/dist/utils/index.js +5 -0
  207. package/dist/utils/wrappers.d.mts +14 -0
  208. package/dist/utils/wrappers.d.ts +4 -3
  209. package/package.json +50 -49
  210. package/src/app/jimpex.ts +15 -2
  211. package/src/controllers/common/statics.ts +13 -2
  212. package/src/controllers/utils/gateway.ts +4 -0
  213. package/src/services/http/http.ts +13 -4
  214. package/src/types/http.ts +3 -0
  215. /package/dist/esm/{chunk-T2T6Q22Z.js.map → chunk-F3FYYIAV.js.map} +0 -0
@@ -0,0 +1,725 @@
1
+ import * as _homer0_jimple from '@homer0/jimple';
2
+ import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn, K as ControllerProviderRegisterFn } from '../../index-Bwf7JHu9.mjs';
3
+ import { APIClientOptions } from '@homer0/api-utils';
4
+ import { RouterMethod, AsyncExpressMiddleware, ExpressMiddleware } from '../../types/express.mjs';
5
+ import { Response as Response$1 } from 'node-fetch';
6
+ import { DeepPartial } from '../../types/utils.mjs';
7
+ import { HTTPFetchOptions, HTTP } from '../../services/http/http.mjs';
8
+ import { Request, Response, NextFunction, Router } from 'express';
9
+ import '../../types/http.mjs';
10
+ import 'https';
11
+ import 'http';
12
+ import 'spdy';
13
+ import '@homer0/path-utils';
14
+ import '@homer0/simple-logger';
15
+ import '@homer0/simple-config';
16
+ import '@homer0/events-hub';
17
+
18
+ /**
19
+ * The extended definition for endpoints.
20
+ *
21
+ * @group Controllers/Gateway
22
+ */
23
+ type GatewayConfigEndpointProps = {
24
+ /**
25
+ * The path to the endpoint relative to the entry point. It can include placeholders for
26
+ * parameters like `/:parameter/`.
27
+ */
28
+ path: string;
29
+ /**
30
+ * The router (HTTP) method for the endpoint.
31
+ *
32
+ * @default 'all'
33
+ */
34
+ method?: RouterMethod;
35
+ };
36
+ /**
37
+ * The definition of an endpoint: it can be just the path, relative to the entry point, or
38
+ * an object in which you can also specify things like the method.
39
+ *
40
+ * @group Controllers/Gateway
41
+ */
42
+ type GatewayConfigEndpointDefinition = string | GatewayConfigEndpointProps;
43
+ /**
44
+ * The dictionary of endpoints the controller uses. The reason for this type is that this
45
+ * could be a flat dictionary, or a nested one.
46
+ *
47
+ * @example
48
+ *
49
+ * <caption>A flat dictionary</caption>
50
+ *
51
+ * {
52
+ * random: '/random',
53
+ * users: '/users',
54
+ * userById: {
55
+ * path: '/users/:id',
56
+ * method: 'get',
57
+ * },
58
+ * }
59
+ *
60
+ * @example
61
+ *
62
+ * <caption>A nested dictionary</caption>
63
+ *
64
+ * {
65
+ * random: '/random',
66
+ * users: {
67
+ * list: '/users',
68
+ * byId: {
69
+ * path: '/users/:id',
70
+ * method: 'get',
71
+ * },
72
+ * },
73
+ * }
74
+ *
75
+ * @group Controllers/Gateway
76
+ */
77
+ type GatewayConfigEndpoints = {
78
+ [key: string]: GatewayConfigEndpointDefinition | GatewayConfigEndpoints;
79
+ };
80
+ /**
81
+ * The configuration for the gateway the controller uses.
82
+ *
83
+ * @group Controllers/Gateway
84
+ */
85
+ type GatewayConfig = {
86
+ /**
87
+ * The entry point to the API the controller will make the requests to.
88
+ */
89
+ url: string;
90
+ /**
91
+ * The dictionary of enpoints the gateway will make available.
92
+ */
93
+ gateway: GatewayConfigEndpoints;
94
+ };
95
+ /**
96
+ * The options for how the gateway will handle the headers from the requests and the
97
+ * responses.
98
+ *
99
+ * @group Controllers/Gateway
100
+ */
101
+ type GatewayControllerHeaderOptions = {
102
+ /**
103
+ * Whether or not to include the header with the request's IP address.
104
+ *
105
+ * @default true
106
+ */
107
+ useXForwardedFor: boolean;
108
+ /**
109
+ * Whether or not to copy all custom headers from the request. By custom header, it
110
+ * means all the headers which names start with `x-`.
111
+ *
112
+ * @default true
113
+ */
114
+ copyCustomHeaders: boolean;
115
+ /**
116
+ * A list of "known" headers the gateway will try to copy from the incoming request.
117
+ *
118
+ * @default ['authorization','content-type', 'referer', 'user-agent']
119
+ */
120
+ copy: string[];
121
+ /**
122
+ * A list of "known" headers the gateway will try to remove the response.
123
+ *
124
+ * @default ['server', 'x-powered-by']
125
+ */
126
+ remove: string[];
127
+ };
128
+ /**
129
+ * The extra options for the gateway controller. They are "extra" because they are mostly
130
+ * helpers for when used with an API client, or for optional features.
131
+ *
132
+ * @group Controllers/Gateway
133
+ */
134
+ type GatewayControllerExtraOptions = {
135
+ /**
136
+ * This is really a helper for when the gateway is used with an API client. The idea is
137
+ * that, by default, the routes are mounted on the controller route, but with this
138
+ * option, you can specify another sub path. For example: The controller is mounted on
139
+ * `/routes`, and if you set `root` to `gateway`, all the routes will be on
140
+ * `/routes/gateway`.
141
+ *
142
+ * This become important (and useful) when you get the API client configuration (with
143
+ * `getAPIConfig`): The `url` will be the controller route, but all the endpoints will
144
+ * be modified and prefixed with the `root`, that way, you can have multiple gateways in
145
+ * the same "base route".
146
+ *
147
+ * It can also includes placeholders for parameters like `/:parameter/`, that will be
148
+ * replaced with the `placeholders` option when `getAPIConfig` gets called.
149
+ *
150
+ * @default ''
151
+ */
152
+ root: string;
153
+ /**
154
+ * This is another option for when the gateway is used with an API client. When calling
155
+ * `getAPIConfig`, all the endpoints will be wrapped inside an object named after this
156
+ * option. For example: `{ url: '...', endpoints: { api: { ... } } }`.
157
+ *
158
+ * @default 'api'
159
+ */
160
+ apiConfigSetting: string;
161
+ /**
162
+ * The options for how the gateway will handle the headers from the requests and the
163
+ * responses.
164
+ */
165
+ headers: GatewayControllerHeaderOptions;
166
+ };
167
+ /**
168
+ * The required options for the gateway controller.
169
+ *
170
+ * @group Controllers/Gateway
171
+ */
172
+ type GatewayControllerOptions = {
173
+ /**
174
+ * The configuration for the API the gateway will make the requests to.
175
+ */
176
+ gatewayConfig: GatewayConfig;
177
+ /**
178
+ * The route where the controller is mounted.
179
+ */
180
+ route: string;
181
+ } & DeepPartial<GatewayControllerExtraOptions>;
182
+ /**
183
+ * The information for a request the controller will make.
184
+ *
185
+ * @group Controllers/Gateway
186
+ */
187
+ type GatewayControllerRequest = {
188
+ /**
189
+ * The URL for the request.
190
+ */
191
+ url: string;
192
+ /**
193
+ * The options for the fetch client that will make the requests.
194
+ */
195
+ options: HTTPFetchOptions;
196
+ };
197
+ /**
198
+ * The information for an endpoint the gateway is calling.
199
+ *
200
+ * @group Controllers/Gateway
201
+ */
202
+ type GatewayControllerEndpointInfo = {
203
+ /**
204
+ * The name of the endpoint in the configuration.
205
+ */
206
+ name: string;
207
+ /**
208
+ * The properties (path and method) of the endpoint.
209
+ */
210
+ definition: GatewayConfigEndpointDefinition;
211
+ };
212
+ /**
213
+ * These are the base options sent to all the helper service functions.
214
+ *
215
+ * @group Controllers/Gateway
216
+ */
217
+ type GatewayHelperServiceBaseFnOptions = {
218
+ /**
219
+ * The information of the endpoint the gateway is calling.
220
+ */
221
+ endpoint: GatewayControllerEndpointInfo;
222
+ /**
223
+ * The request recived by the application.
224
+ */
225
+ req: Request;
226
+ /**
227
+ * The response object created by the application.
228
+ */
229
+ res: Response;
230
+ /**
231
+ * The function to call the next middleware in the chain.
232
+ */
233
+ next: NextFunction;
234
+ };
235
+ /**
236
+ * The information sent to the helper service in order to modify, or not, a request before
237
+ * it is sent.
238
+ *
239
+ * @group Controllers/Gateway
240
+ */
241
+ type GatewayHelperServiceRequestReducerOptions = GatewayHelperServiceBaseFnOptions & {
242
+ /**
243
+ * The options the controller created for the fetch client.
244
+ */
245
+ endpointReq: GatewayControllerRequest;
246
+ };
247
+ /**
248
+ * A function that can be used to modify the information of an endpoint before making a
249
+ * request.
250
+ *
251
+ * @param options The information of the request.
252
+ * @group Controllers/Gateway
253
+ */
254
+ type GatewayHelperServiceRequestReducer = (options: GatewayHelperServiceRequestReducerOptions) => Promise<GatewayControllerRequest>;
255
+ /**
256
+ * The information sent to the helper service in order to modify a response before
257
+ * processing it, decide if it should be streamed or not, and even handle it.
258
+ *
259
+ * @group Controllers/Gateway
260
+ */
261
+ type GatewayHelperServiceResponseReducerOptions = GatewayHelperServiceBaseFnOptions & {
262
+ /**
263
+ * The response from the endpoint request.
264
+ */
265
+ endpointRes: Response$1;
266
+ };
267
+ /**
268
+ * A function that can be used to modify the response of an endpoint before the controller
269
+ * processes it.
270
+ *
271
+ * @param options The information of the response.
272
+ * @group Controllers/Gateway
273
+ */
274
+ type GatewayHelperServiceResponseReducer = (options: GatewayHelperServiceResponseReducerOptions) => Promise<Response$1>;
275
+ /**
276
+ * A function that can be used to tell the controller to stream the response of an
277
+ * endpoint or not.
278
+ * If it returns `false`, the function to handle responses should be defined, otherwise,
279
+ * an error will be generated.
280
+ *
281
+ * @param options The information of the response.
282
+ * @group Controllers/Gateway
283
+ */
284
+ type GatewayHelperServiceStreamVerification = (options: GatewayHelperServiceResponseReducerOptions) => Promise<boolean>;
285
+ /**
286
+ * A function to handle the response of an endpoint. This is called when the helper
287
+ * service tells the controller that the endpoint shouldn't be streamed, so this method
288
+ * should handle the response.
289
+ *
290
+ * @param options The information of the response.
291
+ * @group Controllers/Gateway
292
+ */
293
+ type GatewayHelperServiceResponseHandler = (options: GatewayHelperServiceResponseReducerOptions) => Promise<void>;
294
+ /**
295
+ * The information sent to the helper service in order to handle a failed request for an
296
+ * endpoint.
297
+ *
298
+ * @group Controllers/Gateway
299
+ */
300
+ type GatewayHelperServiceErrorHandlerOptions = GatewayHelperServiceBaseFnOptions & {
301
+ /**
302
+ * The error generated during the request.
303
+ */
304
+ error: Error;
305
+ };
306
+ /**
307
+ * A function to handle the error of an endpoint request.
308
+ *
309
+ * @param options The information of the error.
310
+ * @group Controllers/Gateway
311
+ */
312
+ type GatewayHelperServiceErrorHandler = (options: GatewayHelperServiceErrorHandlerOptions) => void;
313
+ /**
314
+ * The interface of a helper service that can intercept/modify the requests and responses
315
+ * the gateway makes.
316
+ *
317
+ * @group Controllers/Gateway
318
+ */
319
+ type GatewayHelperService = Partial<{
320
+ /**
321
+ * A function that is called before an endpoint request is made.
322
+ */
323
+ reduceEndpointRequest: GatewayHelperServiceRequestReducer;
324
+ /**
325
+ * A function that is called with the response of an endpoint request.
326
+ */
327
+ reduceEndpointResponse: GatewayHelperServiceResponseReducer;
328
+ /**
329
+ * A function called in order to validate if an endpoint response should be streamed or
330
+ * not. If the function returns `false`, `handleEndpointResponse` will be called.
331
+ */
332
+ shouldStreamEndpointResponse: GatewayHelperServiceStreamVerification;
333
+ /**
334
+ * A function called when `shouldStreamEndpointResponse` returns `false`. The function
335
+ * should handle the response for the application.
336
+ */
337
+ handleEndpointResponse: GatewayHelperServiceResponseHandler;
338
+ /**
339
+ * A function called when an error is generated during an endpoint request/processing.
340
+ */
341
+ handleEndpointError: GatewayHelperServiceErrorHandler;
342
+ }>;
343
+ /**
344
+ * Utility type for the options object sent to the "proxy methods" the controller has for
345
+ * the helper service.
346
+ *
347
+ * @template T The type of the options for a specific helper service function.
348
+ * @access protected
349
+ * @group Controllers/Gateway
350
+ */
351
+ type GatewayControllerHelperOptions<T> = T & {
352
+ /**
353
+ * The reference for the helper service.
354
+ */
355
+ helper: GatewayHelperService;
356
+ };
357
+ /**
358
+ * The information for a single HTTP method an endpoint can handle.
359
+ *
360
+ * @group Controllers/Gateway
361
+ */
362
+ type GatewayControllerRouteMethod = {
363
+ /**
364
+ * The method for the route.
365
+ */
366
+ method: RouterMethod;
367
+ /**
368
+ * The information of the endpoint.
369
+ */
370
+ endpoint: GatewayControllerEndpointInfo;
371
+ };
372
+ /**
373
+ * The information for all the HTTP methods that can be handled for an endpoint.
374
+ *
375
+ * @group Controllers/Gateway
376
+ */
377
+ type GatewayControllerRoute = {
378
+ /**
379
+ * The path to the endpoint, relative to the entry point.
380
+ */
381
+ path: string;
382
+ /**
383
+ * The path for the route in the controller. This is different from `path` as it's possible for
384
+ * the gateway to be implemented using the `root` option.
385
+ */
386
+ route: string;
387
+ /**
388
+ * A list with all the methods the controller uses on the route.
389
+ */
390
+ methods: GatewayControllerRouteMethod[];
391
+ };
392
+ /**
393
+ * The API client configuration the gateway can generate for its endpoints.
394
+ *
395
+ * @group Controllers/Gateway
396
+ */
397
+ type GatewayControllerAPIConfig = {
398
+ /**
399
+ * The base URL for the API.
400
+ */
401
+ url: string;
402
+ /**
403
+ * The dictionary of endpoints the controller handles.
404
+ */
405
+ endpoints: APIClientOptions['endpoints'];
406
+ };
407
+ /**
408
+ * The options sent to {@link GatewayController.getAPIConfig}.
409
+ *
410
+ * @group Controllers/Gateway
411
+ */
412
+ type GatewayControllerAPIConfigOptions = {
413
+ /**
414
+ * This can be used to overwrite the gateway's `apiConfigSetting` option, and set a new
415
+ * setting as a wrapper for the endpoints.
416
+ */
417
+ setting?: string;
418
+ /**
419
+ * A dictionary of values for possible placeholders that were sent using the `root`
420
+ * option.
421
+ */
422
+ placeholders?: Record<string, string>;
423
+ };
424
+ /**
425
+ * The options to construct a {@link GatewayController}.
426
+ *
427
+ * @group Controllers/Gateway
428
+ */
429
+ type GatewayControllerConstructorOptions = GatewayControllerOptions & {
430
+ /**
431
+ * A dictionary with the dependencies to inject.
432
+ */
433
+ inject: {
434
+ http: HTTP;
435
+ /**
436
+ * A function to get a possible helper service. This is injected as a "getter" to not
437
+ * interrupt the DIC "lifecycle": controllers are initialized right when the app
438
+ * starts, and injecting a reference would force the service to be initialized too,
439
+ * even if a request is not being made.
440
+ */
441
+ getHelperService?: () => GatewayHelperService | undefined;
442
+ };
443
+ };
444
+ /**
445
+ * The options for {@link GatewayController._addRoute}.
446
+ *
447
+ * @access protected
448
+ * @group Controllers/Gateway
449
+ */
450
+ type AddGatewayRouteOptions = {
451
+ /**
452
+ * The reference for the router in which the middlewares will be added.
453
+ */
454
+ router: Router;
455
+ /**
456
+ * The router method in which the middlewares will be added.
457
+ */
458
+ method: RouterMethod;
459
+ /**
460
+ * The route in which the middlewares will be added.
461
+ */
462
+ route: string;
463
+ /**
464
+ * The middleware created by {@link GatewayController}, that makes the request.
465
+ */
466
+ gatewayMiddleware: AsyncExpressMiddleware;
467
+ /**
468
+ * A list of extra middlewares to prepend to the gateway middleware.
469
+ */
470
+ middlewares: ExpressMiddleware[];
471
+ };
472
+ /**
473
+ * A utility controller that generates routes that act as a gateway for a specific API.
474
+ *
475
+ * @group Controller Classes
476
+ * @group Controllers/Gateway
477
+ * @prettierignore
478
+ */
479
+ declare class GatewayController {
480
+ /**
481
+ * The service that makes HTTP requests.
482
+ */
483
+ protected readonly http: HTTP;
484
+ /**
485
+ * A function to get a possible helper service. This is injected as a "getter" to not
486
+ * interrupt the DIC "lifecycle": controllers are initialized right when the app
487
+ * starts, and injecting a reference would force the service to be initialized too,
488
+ * even if a request is not being made.
489
+ */
490
+ protected readonly _getHelperService: () => GatewayHelperService | undefined;
491
+ /**
492
+ * The information, url and endpoints, for the gateway the controller will make requests to.
493
+ */
494
+ protected readonly _gatewayConfig: GatewayConfig;
495
+ /**
496
+ * The route in which the controller is mounted.
497
+ */
498
+ protected readonly _route: string;
499
+ /**
500
+ * A regular expression that will be used to remove the controller route from a
501
+ * request path. This will allow the main middleware to extract the path to where the
502
+ * request should be made.
503
+ */
504
+ protected readonly _routeExpression: RegExp;
505
+ /**
506
+ * The controller customization options.
507
+ */
508
+ protected readonly _options: GatewayControllerExtraOptions;
509
+ /**
510
+ * A flat dictionary with the endpoints information.
511
+ */
512
+ protected readonly _endpoints: Record<string, GatewayConfigEndpointDefinition>;
513
+ /**
514
+ * The entry URL for the API client configuration the controller can generate.
515
+ */
516
+ protected readonly _apiConfigUrl: string;
517
+ /**
518
+ * The generated endpoints for the API client configuration the controller can generate.
519
+ */
520
+ protected readonly _apiConfigEndpoints: APIClientOptions['endpoints'];
521
+ /**
522
+ * The list of routes the controller can handle.
523
+ */
524
+ protected readonly _routes: GatewayControllerRoute[];
525
+ /**
526
+ * @param options The options to construct the controller.
527
+ */
528
+ constructor({ inject, route, gatewayConfig, ...options }: GatewayControllerConstructorOptions);
529
+ /**
530
+ * Generates an API client configuration based on the controller routes.
531
+ *
532
+ * @param options The options to customize the generated configuration.
533
+ */
534
+ getAPIConfig({ setting, placeholders, }?: GatewayControllerAPIConfigOptions): Readonly<GatewayControllerAPIConfig>;
535
+ /**
536
+ * Mounts the middlewares in the router in order to make the requests.
537
+ *
538
+ * @param router A reference to the application router.
539
+ * @param middlewares A list of extra middlewares to execute before the gateway
540
+ * middleware.
541
+ */
542
+ addRoutes(router: Router, middlewares?: ExpressMiddleware[]): Router;
543
+ /**
544
+ * The customization options.
545
+ */
546
+ get options(): Readonly<GatewayControllerExtraOptions>;
547
+ /**
548
+ * The configuration for the gateway the controller will make requests to.
549
+ */
550
+ get gatewayConfig(): Readonly<GatewayConfig>;
551
+ /**
552
+ * Generates a middleware that will make the request to an endpoint and stream the
553
+ * response.
554
+ *
555
+ * @param endpoint The information of the endpoint the middleware will handle.
556
+ */
557
+ protected _createGatewayMiddleware(endpoint: GatewayControllerEndpointInfo): AsyncExpressMiddleware;
558
+ /**
559
+ * Mounts the middleware(s) for an endpoint in the router.
560
+ *
561
+ * @param options The information of the endpoint and how it needs to be added.
562
+ */
563
+ protected _addRoute({ router, method, route, gatewayMiddleware, middlewares, }: AddGatewayRouteOptions): void;
564
+ /**
565
+ * Formats the endpoints for the gateway into a flat dictionary without nesting.
566
+ */
567
+ protected _formatEndpoints(): Record<string, GatewayConfigEndpointDefinition>;
568
+ /**
569
+ * Based on the information from the endpoints, this method will create the routes the
570
+ * controller will later add on a router.
571
+ *
572
+ * @throws If there's more than one endpoint using the same path with the same HTTP
573
+ * method.
574
+ */
575
+ protected _createRoutes(): GatewayControllerRoute[];
576
+ /**
577
+ * This is a "proxy method" to call the helper service's function that can modify an
578
+ * endpoint request before it gets made.
579
+ *
580
+ * The reason this is a "proxy method" is in case the controller gets subclassed and
581
+ * "used itself as a helper" instead of relying on a difference one.
582
+ *
583
+ * If the helper doesn't implement `reduceEndpointRequest`, it will just return
584
+ * information for the request.
585
+ *
586
+ * @param options The information of the request and the reference to the helper.
587
+ */
588
+ protected _reduceEndpointRequest({ helper, ...options }: GatewayControllerHelperOptions<GatewayHelperServiceRequestReducerOptions>): Promise<GatewayControllerRequest>;
589
+ /**
590
+ * This is a "proxy method" to call the helper service's function that can modify an
591
+ * endpoint response before it gets processed.
592
+ *
593
+ * The reason this is a "proxy method" is in case the controller gets subclassed and
594
+ * "used itself as a helper" instead of relying on a difference one.
595
+ *
596
+ * If the helper doesn't implement `reduceEndpointResponse`, it will just return
597
+ * information for the response.
598
+ *
599
+ * @param options The information of the response and the reference to the helper.
600
+ */
601
+ protected _reduceEndpointResponse({ helper, ...options }: GatewayControllerHelperOptions<GatewayHelperServiceResponseReducerOptions>): Promise<Response$1>;
602
+ /**
603
+ * This is a "proxy method" to call the helper service's function that can decide if an
604
+ * endpoint response should be streamed or not.
605
+ *
606
+ * The reason this is a "proxy method" is in case the controller gets subclassed and
607
+ * "used itself as a helper" instead of relying on a difference one.
608
+ *
609
+ * If the helper doesn't implement `shouldStreamEndpointResponse`, it will just return
610
+ * `true`.
611
+ *
612
+ * @param options The information of the response and the reference to the helper.
613
+ */
614
+ protected _shouldStreamEndpointResponse({ helper, ...options }: GatewayControllerHelperOptions<GatewayHelperServiceResponseReducerOptions>): Promise<boolean>;
615
+ /**
616
+ * This is a "proxy method" to call the helper service's function that handles a
617
+ * response in case it already said that a response shouldn't be streamed.
618
+ *
619
+ * The reason this is a "proxy method" is in case the controller gets subclassed and
620
+ * "used itself as a helper" instead of relying on a difference one.
621
+ *
622
+ * If the helper doesn't implement `shouldStreamEndpointResponse`, it will throw an
623
+ * error.
624
+ *
625
+ * @param options The information of the response and the reference to the helper.
626
+ * @throws If the helper doesn't implement `handleEndpointResponse`.
627
+ */
628
+ protected _handleEndpointResponse({ helper, ...options }: GatewayControllerHelperOptions<GatewayHelperServiceResponseReducerOptions>): Promise<void>;
629
+ /**
630
+ * This is a "proxy method" to call the helper service's function that handles an error
631
+ * on an endpoint request.
632
+ *
633
+ * The reason this is a "proxy method" is in case the controller gets subclassed and
634
+ * "used itself as a helper" instead of relying on a difference one.
635
+ *
636
+ * If the helper doesn't implement `handleEndpointError`, it will just send the error to
637
+ * the next middleware/error handler.
638
+ *
639
+ * @param options The information of the response and the reference to the helper.
640
+ */
641
+ protected _handleEndpointError({ helper, ...options }: GatewayControllerHelperOptions<GatewayHelperServiceErrorHandlerOptions>): void;
642
+ /**
643
+ * Validates and formats the customization options sent to the controller.
644
+ *
645
+ * @param options The options sent to the constructor.
646
+ */
647
+ protected _formatOptions(options: GatewayControllerExtraOptions): GatewayControllerExtraOptions;
648
+ /**
649
+ * Validates a router/HTTP method that the controller intends to use for an endpoint. If
650
+ * it's not valid, it will return `all`.
651
+ *
652
+ * @param method The HTTP method for the endpoint.
653
+ */
654
+ protected _validateHTTPMethod(method: string): RouterMethod;
655
+ /**
656
+ * Creates the API client configuration based on the controller routes.
657
+ */
658
+ protected _createAPIConfig(): GatewayControllerAPIConfig;
659
+ }
660
+ /**
661
+ * A function to generate a list of middlewares that can be executed before the tontroller
662
+ * main middleware.
663
+ *
664
+ * @group Controllers/Gateway
665
+ */
666
+ type GatewayControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];
667
+ /**
668
+ * The options for the controller creator that mounts the {@link GatewayController}.
669
+ *
670
+ * @group Controllers/Gateway
671
+ */
672
+ type GatewayControllerCreatorOptions = DeepPartial<GatewayControllerExtraOptions> & {
673
+ /**
674
+ * The name the creator will use to register the controller in the container. No,
675
+ * this is not a typo. The creator will register the controller so other services can
676
+ * access the `getAPIConfig` method. The service will be available after the app
677
+ * routes are mounted.
678
+ * If this is overwritten, the creator will ensure that the name ends with `Gateway`;
679
+ * and if overwritten, but it doesn't include `Gateway` at the end, and no
680
+ * `gatewaySettingName` was defined, the creator will use the custom name (without
681
+ * `Gatway`) for `gatewaySettingName`.
682
+ *
683
+ * @default 'apiGateway'
684
+ */
685
+ serviceName?: string;
686
+ /**
687
+ * The name of the helper service the creator will try to obtain from the container.
688
+ * If `serviceName` is overwritten, the default for this will be
689
+ * `${serviceName}Helper`.
690
+ *
691
+ * @default 'apiGatewayHelper'
692
+ */
693
+ helperServiceName?: string;
694
+ /**
695
+ * The name of the configuration setting where the gateway configuration is stored. If
696
+ * not overwritten, check the description of `serviceName` to understand which will be
697
+ * its default value.
698
+ *
699
+ * @default 'api'
700
+ */
701
+ gatewaySettingName?: string;
702
+ /**
703
+ * The class the creator will instantiate. Similar to the API Client, this allows for
704
+ * extra customization as you can send a custom subclass of the
705
+ * {@link GatewayController}.
706
+ *
707
+ * @default GatewayController
708
+ */
709
+ gatewayClass?: typeof GatewayController;
710
+ /**
711
+ * A function to generate a list of middlewares that can be executed before the
712
+ * controller main middleware.
713
+ */
714
+ getMiddlewares?: GatewayControllerGetMiddlewaresFn;
715
+ };
716
+ /**
717
+ * Creates a controller that allows the application to mount routes that will work like
718
+ * gateway to a specific API.
719
+ *
720
+ * @group Controllers
721
+ * @group Controllers/Gateway
722
+ */
723
+ declare const gatewayController: _homer0_jimple.ResourceCreator<"provider", "register", (options?: GatewayControllerCreatorOptions) => <ContainerType extends Jimpex = Jimpex>(app: ContainerType, route: string) => _homer0_jimple.Resource<"controller", "connect", ControllerConnectFn>, ControllerProviderRegisterFn>;
724
+
725
+ export { type AddGatewayRouteOptions, type GatewayConfig, type GatewayConfigEndpointDefinition, type GatewayConfigEndpointProps, type GatewayConfigEndpoints, GatewayController, type GatewayControllerAPIConfig, type GatewayControllerAPIConfigOptions, type GatewayControllerConstructorOptions, type GatewayControllerCreatorOptions, type GatewayControllerEndpointInfo, type GatewayControllerExtraOptions, type GatewayControllerGetMiddlewaresFn, type GatewayControllerHeaderOptions, type GatewayControllerHelperOptions, type GatewayControllerOptions, type GatewayControllerRequest, type GatewayControllerRoute, type GatewayControllerRouteMethod, type GatewayHelperService, type GatewayHelperServiceBaseFnOptions, type GatewayHelperServiceErrorHandler, type GatewayHelperServiceErrorHandlerOptions, type GatewayHelperServiceRequestReducer, type GatewayHelperServiceRequestReducerOptions, type GatewayHelperServiceResponseHandler, type GatewayHelperServiceResponseReducer, type GatewayHelperServiceResponseReducerOptions, type GatewayHelperServiceStreamVerification, gatewayController };