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,223 @@
1
+ import * as _homer0_jimple from '@homer0/jimple';
2
+ import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn } from '../../index-Bwf7JHu9.mjs';
3
+ import mime from 'mime';
4
+ import { RouterMethod, ExpressMiddleware } from '../../types/express.mjs';
5
+ import { DeepPartial } from '../../types/utils.mjs';
6
+ import { SendFile } from '../../services/common/sendFile.mjs';
7
+ import { Router } from 'express';
8
+ import '../../types/http.mjs';
9
+ import 'https';
10
+ import 'http';
11
+ import 'spdy';
12
+ import 'node-fetch';
13
+ import '@homer0/path-utils';
14
+ import '@homer0/simple-logger';
15
+ import '@homer0/simple-config';
16
+ import '@homer0/events-hub';
17
+
18
+ type Mime = typeof mime;
19
+ /**
20
+ * The definition for each file the controller handles.
21
+ *
22
+ * @group Controllers/Statics
23
+ */
24
+ type StaticsControllerFile = {
25
+ /**
26
+ * The route, relative to the controller root, to the file.
27
+ */
28
+ route: string;
29
+ /**
30
+ * The path to the file in the filesystem. Since the file is served using the
31
+ * {@link SendFile} service, whether the file is relative to the project root or the
32
+ * application executable depends on how the service is configured (relative to the
33
+ * executable by default).
34
+ */
35
+ path: string;
36
+ /**
37
+ * A dictionary of headers for the response.
38
+ */
39
+ headers?: Record<string, string>;
40
+ };
41
+ /**
42
+ * These are like "master paths" that get prepended to all the file paths and routes the
43
+ * controller use.
44
+ *
45
+ * @group Controllers/Statics
46
+ */
47
+ type StaticsControllerPathsOptions = {
48
+ /**
49
+ * A custom route to prefix all the file routes with.
50
+ */
51
+ route: string;
52
+ /**
53
+ * A custom path to prefix all the file paths with.
54
+ */
55
+ source: string;
56
+ };
57
+ /**
58
+ * The options to customize the controller.
59
+ *
60
+ * @group Controllers/Statics
61
+ */
62
+ type StaticsControllerOptions = {
63
+ /**
64
+ * A list of filenames, or definitions for the files to handle.
65
+ */
66
+ files: Array<string | StaticsControllerFile>;
67
+ /**
68
+ * A dictionary with the allowed router (HTTP) methods the controller can use to serve
69
+ * the files. If `all` is set to `true`, the rest of the values will be ignored.
70
+ *
71
+ * @default {get: true, all: false}
72
+ */
73
+ methods: Partial<Record<RouterMethod, boolean>>;
74
+ /**
75
+ * The "master paths" the controller can use to prefix the file paths and routes.
76
+ *
77
+ * @default {route: '', source: './',}
78
+ */
79
+ paths: StaticsControllerPathsOptions;
80
+ };
81
+ /**
82
+ * The options to construct a {@link StaticsController}.
83
+ *
84
+ * @group Controllers/Statics
85
+ */
86
+ type StaticsControllerConstructorOptions = DeepPartial<StaticsControllerOptions> & {
87
+ /**
88
+ * A dictionary with the dependencies to inject.
89
+ */
90
+ inject: {
91
+ sendFile: SendFile;
92
+ mime: Mime;
93
+ };
94
+ };
95
+ /**
96
+ * A function to generate a list of middlewares that can be executed before the tontroller
97
+ * main middleware.
98
+ *
99
+ * @group Controllers/Statics
100
+ */
101
+ type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];
102
+ /**
103
+ * The options for the controller creator that mounts {@link StaticsController}.
104
+ *
105
+ * @group Controllers/Statics
106
+ */
107
+ type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {
108
+ /**
109
+ * A function to generate a list of middlewares that can be executed before the
110
+ * tontroller main middleware.
111
+ */
112
+ getMiddlewares?: StaticsControllerGetMiddlewaresFn;
113
+ };
114
+ /**
115
+ * The options for {@link StaticsController._addRoute}.
116
+ *
117
+ * @access protected
118
+ * @group Controllers/Statics
119
+ */
120
+ type AddStaticRouteOptions = {
121
+ /**
122
+ * The reference for the router in which the middlewares will be added.
123
+ */
124
+ router: Router;
125
+ /**
126
+ * The router method in which the middlewares will be added.
127
+ */
128
+ method: RouterMethod;
129
+ /**
130
+ * The definition of the file to serve.
131
+ */
132
+ file: StaticsControllerFile;
133
+ /**
134
+ * The middleware created by {@link StaticsController}, that will serve the file.
135
+ */
136
+ fileMiddleware: ExpressMiddleware;
137
+ /**
138
+ * A list of extra middlewares to execute before the file middleware.
139
+ */
140
+ middlewares: ExpressMiddleware[];
141
+ };
142
+ /**
143
+ * The controller class that allows the application to serve specific files from any
144
+ * folder to any route without the need of mounting directories as "static".
145
+ *
146
+ * @group Controller Classes
147
+ * @group Controllers/Statics
148
+ * @prettierignore
149
+ */
150
+ declare class StaticsController {
151
+ /**
152
+ * The service that serves static files.
153
+ */
154
+ protected readonly _sendFile: SendFile;
155
+ /**
156
+ * The MIME type library. Since it's an ESM only module, Jimpex loads it on boot and makes
157
+ * it available on the container.
158
+ */
159
+ protected readonly _mime: Mime;
160
+ /**
161
+ * The controller customization options.
162
+ */
163
+ protected _options: StaticsControllerOptions;
164
+ /**
165
+ * A dictionary with the formatted definitions of the files that will be served.
166
+ * It uses the files' routes as keys, for easy access in the middleware.
167
+ */
168
+ protected files: Record<string, StaticsControllerFile>;
169
+ /**
170
+ * @param options The options to construct the controller.
171
+ */
172
+ constructor({ inject, ...options }: StaticsControllerConstructorOptions);
173
+ /**
174
+ * Mounts the middlewares in the router in order to serve the files.
175
+ *
176
+ * @param router A reference to the application router.
177
+ * @param middlewares A list of extra middlewares to execute before the file
178
+ * middleware.
179
+ */
180
+ addRoutes(router: Router, middlewares?: ExpressMiddleware[]): Router;
181
+ /**
182
+ * The controller options.
183
+ */
184
+ get options(): Readonly<StaticsControllerOptions>;
185
+ /**
186
+ * Generates the middleware that will serve the file.
187
+ *
188
+ * @param file The definition of the file to serve.
189
+ */
190
+ protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware;
191
+ /**
192
+ * Mounts the middleware(s) for a file in the router.
193
+ *
194
+ * @param options The information of the file and how it needs to be added.
195
+ */
196
+ protected _addRoute({ router, method, file, fileMiddleware, middlewares, }: AddStaticRouteOptions): void;
197
+ /**
198
+ * Validates and formats the options sent to the constructor in order to get the final
199
+ * set that will be stored in the controller.
200
+ *
201
+ * @param options The options to validate.
202
+ * @throws If no files are specified.
203
+ * @throws If methods is not defined.
204
+ * @throws If no methods are enabled.
205
+ * @throws If there's an invalid HTTP method.
206
+ */
207
+ protected _validateOptions(options: StaticsControllerOptions): StaticsControllerOptions;
208
+ /**
209
+ * Parses the files received from the constructor's options, and formats them into
210
+ * proper definitions the controller can use.
211
+ */
212
+ protected _createFiles(): Record<string, StaticsControllerFile>;
213
+ }
214
+ /**
215
+ * A controller that allows the application to server specific files from any folder to
216
+ * any route without the need of mounting directories as "static" folders.
217
+ *
218
+ * @group Controllers
219
+ * @group Controllers/Statics
220
+ */
221
+ declare const staticsController: _homer0_jimple.ResourceCreator<"controller", "connect", ({ getMiddlewares, ...options }?: StaticsControllerCreatorOptions) => <ContainerType extends Jimpex = Jimpex>(app: ContainerType) => Router, ControllerConnectFn>;
222
+
223
+ export { type AddStaticRouteOptions, StaticsController, type StaticsControllerConstructorOptions, type StaticsControllerCreatorOptions, type StaticsControllerFile, type StaticsControllerGetMiddlewaresFn, type StaticsControllerOptions, type StaticsControllerPathsOptions, staticsController };
@@ -1,19 +1,21 @@
1
1
  import * as _homer0_jimple from '@homer0/jimple';
2
- import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn } from '../../jimpex-7eaee271.js';
2
+ import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn } from '../../index-C6I3NCC-.js';
3
+ import mime from 'mime';
3
4
  import { RouterMethod, ExpressMiddleware } from '../../types/express.js';
4
5
  import { DeepPartial } from '../../types/utils.js';
5
6
  import { SendFile } from '../../services/common/sendFile.js';
6
7
  import { Router } from 'express';
7
- import '@homer0/events-hub';
8
8
  import '../../types/http.js';
9
9
  import 'https';
10
10
  import 'http';
11
11
  import 'spdy';
12
12
  import 'node-fetch';
13
- import '@homer0/simple-config';
14
- import '@homer0/simple-logger';
15
13
  import '@homer0/path-utils';
14
+ import '@homer0/simple-logger';
15
+ import '@homer0/simple-config';
16
+ import '@homer0/events-hub';
16
17
 
18
+ type Mime = typeof mime;
17
19
  /**
18
20
  * The definition for each file the controller handles.
19
21
  *
@@ -87,6 +89,7 @@ type StaticsControllerConstructorOptions = DeepPartial<StaticsControllerOptions>
87
89
  */
88
90
  inject: {
89
91
  sendFile: SendFile;
92
+ mime: Mime;
90
93
  };
91
94
  };
92
95
  /**
@@ -149,6 +152,11 @@ declare class StaticsController {
149
152
  * The service that serves static files.
150
153
  */
151
154
  protected readonly _sendFile: SendFile;
155
+ /**
156
+ * The MIME type library. Since it's an ESM only module, Jimpex loads it on boot and makes
157
+ * it available on the container.
158
+ */
159
+ protected readonly _mime: Mime;
152
160
  /**
153
161
  * The controller customization options.
154
162
  */
@@ -212,4 +220,4 @@ declare class StaticsController {
212
220
  */
213
221
  declare const staticsController: _homer0_jimple.ResourceCreator<"controller", "connect", ({ getMiddlewares, ...options }?: StaticsControllerCreatorOptions) => <ContainerType extends Jimpex = Jimpex>(app: ContainerType) => Router, ControllerConnectFn>;
214
222
 
215
- export { AddStaticRouteOptions, StaticsController, StaticsControllerConstructorOptions, StaticsControllerCreatorOptions, StaticsControllerFile, StaticsControllerGetMiddlewaresFn, StaticsControllerOptions, StaticsControllerPathsOptions, staticsController };
223
+ export { type AddStaticRouteOptions, StaticsController, type StaticsControllerConstructorOptions, type StaticsControllerCreatorOptions, type StaticsControllerFile, type StaticsControllerGetMiddlewaresFn, type StaticsControllerOptions, type StaticsControllerPathsOptions, staticsController };
@@ -19,6 +19,10 @@ var __copyProps = (to, from, except, desc) => {
19
19
  return to;
20
20
  };
21
21
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
26
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
27
  mod
24
28
  ));
@@ -34,15 +38,33 @@ __export(statics_exports, {
34
38
  });
35
39
  module.exports = __toCommonJS(statics_exports);
36
40
  var path = __toESM(require("path"));
37
- var mime = __toESM(require("mime"));
38
41
  var import_deep_assign = require("@homer0/deep-assign");
39
42
  var import_utils = require("../../utils");
40
43
  class StaticsController {
44
+ /**
45
+ * @param options The options to construct the controller.
46
+ */
41
47
  constructor({ inject, ...options }) {
48
+ /**
49
+ * The service that serves static files.
50
+ */
42
51
  __publicField(this, "_sendFile");
52
+ /**
53
+ * The MIME type library. Since it's an ESM only module, Jimpex loads it on boot and makes
54
+ * it available on the container.
55
+ */
56
+ __publicField(this, "_mime");
57
+ /**
58
+ * The controller customization options.
59
+ */
43
60
  __publicField(this, "_options");
61
+ /**
62
+ * A dictionary with the formatted definitions of the files that will be served.
63
+ * It uses the files' routes as keys, for easy access in the middleware.
64
+ */
44
65
  __publicField(this, "files");
45
66
  this._sendFile = inject.sendFile;
67
+ this._mime = inject.mime;
46
68
  this._options = this._validateOptions(
47
69
  (0, import_deep_assign.deepAssignWithOverwrite)(
48
70
  {
@@ -61,6 +83,13 @@ class StaticsController {
61
83
  );
62
84
  this.files = this._createFiles();
63
85
  }
86
+ /**
87
+ * Mounts the middlewares in the router in order to serve the files.
88
+ *
89
+ * @param router A reference to the application router.
90
+ * @param middlewares A list of extra middlewares to execute before the file
91
+ * middleware.
92
+ */
64
93
  addRoutes(router, middlewares = []) {
65
94
  const { methods } = this._options;
66
95
  const use = methods.all ? ["all"] : Object.keys(methods).reduce((acc, name) => {
@@ -79,14 +108,22 @@ class StaticsController {
79
108
  });
80
109
  return router;
81
110
  }
111
+ /**
112
+ * The controller options.
113
+ */
82
114
  get options() {
83
115
  return { ...this._options };
84
116
  }
117
+ /**
118
+ * Generates the middleware that will serve the file.
119
+ *
120
+ * @param file The definition of the file to serve.
121
+ */
85
122
  _getMiddleware(file) {
86
123
  return (_, res, next) => {
87
124
  const extension = path.parse(file.path).ext.substring(1);
88
125
  const headers = {
89
- "Content-Type": mime.getType(extension) || "text/html",
126
+ "Content-Type": this._mime.getType(extension) || "text/html",
90
127
  ...file.headers
91
128
  };
92
129
  Object.entries(headers).forEach(([key, value]) => {
@@ -99,6 +136,11 @@ class StaticsController {
99
136
  });
100
137
  };
101
138
  }
139
+ /**
140
+ * Mounts the middleware(s) for a file in the router.
141
+ *
142
+ * @param options The information of the file and how it needs to be added.
143
+ */
102
144
  _addRoute({
103
145
  router,
104
146
  method,
@@ -109,6 +151,16 @@ class StaticsController {
109
151
  const { route } = file;
110
152
  router[method](route, [...middlewares, fileMiddleware]);
111
153
  }
154
+ /**
155
+ * Validates and formats the options sent to the constructor in order to get the final
156
+ * set that will be stored in the controller.
157
+ *
158
+ * @param options The options to validate.
159
+ * @throws If no files are specified.
160
+ * @throws If methods is not defined.
161
+ * @throws If no methods are enabled.
162
+ * @throws If there's an invalid HTTP method.
163
+ */
112
164
  _validateOptions(options) {
113
165
  if (!options.files || !options.files.length) {
114
166
  throw new Error("You need to specify a list of files");
@@ -148,6 +200,10 @@ class StaticsController {
148
200
  methods: newMethods
149
201
  };
150
202
  }
203
+ /**
204
+ * Parses the files received from the constructor's options, and formats them into
205
+ * proper definitions the controller can use.
206
+ */
151
207
  _createFiles() {
152
208
  const { files, paths } = this._options;
153
209
  const routePath = (0, import_utils.removeSlashes)(paths.route, false, true);
@@ -178,7 +234,8 @@ const staticsController = (0, import_utils.controllerCreator)(
178
234
  const router = app.getRouter();
179
235
  const ctrl = new StaticsController({
180
236
  inject: {
181
- sendFile: app.get("sendFile")
237
+ sendFile: app.get("sendFile"),
238
+ mime: app.get("mime")
182
239
  },
183
240
  ...options
184
241
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/controllers/common/statics.ts"],"sourcesContent":["import * as path from 'path';\nimport * as mime from 'mime';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport {\n controllerCreator,\n removeSlashes,\n notUndefined,\n type MiddlewareLike,\n} from '../../utils';\nimport type { SendFile } from '../../services';\nimport type { Jimpex } from '../../app';\nimport type { DeepPartial, ExpressMiddleware, Router, RouterMethod } from '../../types';\n/**\n * The definition for each file the controller handles.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerFile = {\n /**\n * The route, relative to the controller root, to the file.\n */\n route: string;\n /**\n * The path to the file in the filesystem. Since the file is served using the\n * {@link SendFile} service, whether the file is relative to the project root or the\n * application executable depends on how the service is configured (relative to the\n * executable by default).\n */\n path: string;\n /**\n * A dictionary of headers for the response.\n */\n headers?: Record<string, string>;\n};\n/**\n * These are like \"master paths\" that get prepended to all the file paths and routes the\n * controller use.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerPathsOptions = {\n /**\n * A custom route to prefix all the file routes with.\n */\n route: string;\n /**\n * A custom path to prefix all the file paths with.\n */\n source: string;\n};\n/**\n * The options to customize the controller.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerOptions = {\n /**\n * A list of filenames, or definitions for the files to handle.\n */\n files: Array<string | StaticsControllerFile>;\n /**\n * A dictionary with the allowed router (HTTP) methods the controller can use to serve\n * the files. If `all` is set to `true`, the rest of the values will be ignored.\n *\n * @default {get: true, all: false}\n */\n methods: Partial<Record<RouterMethod, boolean>>;\n /**\n * The \"master paths\" the controller can use to prefix the file paths and routes.\n *\n * @default {route: '', source: './',}\n */\n paths: StaticsControllerPathsOptions;\n};\n/**\n * The options to construct a {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerConstructorOptions =\n DeepPartial<StaticsControllerOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n sendFile: SendFile;\n };\n };\n/**\n * A function to generate a list of middlewares that can be executed before the tontroller\n * main middleware.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];\n/**\n * The options for the controller creator that mounts {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {\n /**\n * A function to generate a list of middlewares that can be executed before the\n * tontroller main middleware.\n */\n getMiddlewares?: StaticsControllerGetMiddlewaresFn;\n};\n/**\n * The options for {@link StaticsController._addRoute}.\n *\n * @access protected\n * @group Controllers/Statics\n */\nexport type AddStaticRouteOptions = {\n /**\n * The reference for the router in which the middlewares will be added.\n */\n router: Router;\n /**\n * The router method in which the middlewares will be added.\n */\n method: RouterMethod;\n /**\n * The definition of the file to serve.\n */\n file: StaticsControllerFile;\n /**\n * The middleware created by {@link StaticsController}, that will serve the file.\n */\n fileMiddleware: ExpressMiddleware;\n /**\n * A list of extra middlewares to execute before the file middleware.\n */\n middlewares: ExpressMiddleware[];\n};\n\n/**\n * The controller class that allows the application to serve specific files from any\n * folder to any route without the need of mounting directories as \"static\".\n *\n * @group Controller Classes\n * @group Controllers/Statics\n * @prettierignore\n */\nexport class StaticsController {\n /**\n * The service that serves static files.\n */\n protected readonly _sendFile: SendFile;\n /**\n * The controller customization options.\n */\n protected _options: StaticsControllerOptions;\n /**\n * A dictionary with the formatted definitions of the files that will be served.\n * It uses the files' routes as keys, for easy access in the middleware.\n */\n protected files: Record<string, StaticsControllerFile>;\n /**\n * @param options The options to construct the controller.\n */\n constructor({ inject, ...options }: StaticsControllerConstructorOptions) {\n this._sendFile = inject.sendFile;\n this._options = this._validateOptions(\n deepAssignWithOverwrite(\n {\n files: ['favicon.ico', 'index.html'],\n methods: options.methods || {\n all: false,\n get: true,\n },\n paths: {\n route: '',\n source: './',\n },\n },\n options,\n ),\n );\n this.files = this._createFiles();\n }\n /**\n * Mounts the middlewares in the router in order to serve the files.\n *\n * @param router A reference to the application router.\n * @param middlewares A list of extra middlewares to execute before the file\n * middleware.\n */\n addRoutes(router: Router, middlewares: ExpressMiddleware[] = []): Router {\n const { methods } = this._options;\n const use: RouterMethod[] = methods.all\n ? ['all']\n : Object.keys(methods).reduce<RouterMethod[]>((acc, name) => {\n const methodName = name as RouterMethod;\n if (methods[methodName]) {\n acc.push(methodName);\n }\n\n return acc;\n }, []);\n\n Object.keys(this.files).forEach((route) => {\n const file = this.files[route as keyof typeof this.files]!;\n const fileMiddleware = this._getMiddleware(file);\n use.forEach((method) =>\n this._addRoute({ router, method, file, fileMiddleware, middlewares }),\n );\n });\n\n return router;\n }\n /**\n * The controller options.\n */\n get options(): Readonly<StaticsControllerOptions> {\n return { ...this._options };\n }\n /**\n * Generates the middleware that will serve the file.\n *\n * @param file The definition of the file to serve.\n */\n protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware {\n return (_, res, next) => {\n const extension = path.parse(file.path).ext.substring(1);\n const headers = {\n 'Content-Type': mime.getType(extension) || 'text/html',\n ...file.headers,\n };\n\n Object.entries(headers).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n\n this._sendFile({\n res,\n filepath: file.path,\n next,\n });\n };\n }\n /**\n * Mounts the middleware(s) for a file in the router.\n *\n * @param options The information of the file and how it needs to be added.\n */\n protected _addRoute({\n router,\n method,\n file,\n fileMiddleware,\n middlewares,\n }: AddStaticRouteOptions): void {\n const { route } = file;\n router[method](route, [...middlewares, fileMiddleware]);\n }\n /**\n * Validates and formats the options sent to the constructor in order to get the final\n * set that will be stored in the controller.\n *\n * @param options The options to validate.\n * @throws If no files are specified.\n * @throws If methods is not defined.\n * @throws If no methods are enabled.\n * @throws If there's an invalid HTTP method.\n */\n protected _validateOptions(\n options: StaticsControllerOptions,\n ): StaticsControllerOptions {\n if (!options.files || !options.files.length) {\n throw new Error('You need to specify a list of files');\n }\n\n if (!options.methods) {\n throw new Error('You need to specify which HTTP methods are allowed for the files');\n }\n\n const methods = Object.keys(options.methods) as RouterMethod[];\n\n const atLeastOne = methods.some((method) => options.methods[method]);\n if (!atLeastOne) {\n throw new Error('You need to enable at least one HTTP method to serve the files');\n }\n\n const allowedMethods: RouterMethod[] = [\n 'all',\n 'get',\n 'head',\n 'post',\n 'patch',\n 'put',\n 'delete',\n 'connect',\n 'options',\n 'trace',\n ];\n\n const invalid = methods.find(\n (method) => !allowedMethods.includes(method.toLowerCase() as RouterMethod),\n );\n\n if (invalid) {\n throw new Error(`${invalid} is not a valid HTTP method`);\n }\n\n const newMethods = methods.reduce<Record<string, boolean>>((acc, method) => {\n acc[method.toLowerCase()] = !!options.methods[method];\n return acc;\n }, {});\n\n return {\n ...options,\n methods: newMethods,\n };\n }\n /**\n * Parses the files received from the constructor's options, and formats them into\n * proper definitions the controller can use.\n */\n protected _createFiles(): Record<string, StaticsControllerFile> {\n const { files, paths } = this._options;\n const routePath = removeSlashes(paths.route, false, true);\n return files.reduce<Record<string, StaticsControllerFile>>((acc, file) => {\n let src;\n let route;\n let headers;\n if (typeof file === 'object') {\n ({ route, path: src, headers } = file);\n } else {\n src = file;\n route = file;\n }\n\n src = path.join(paths.source, src);\n route = removeSlashes(route, true, false);\n route = `${routePath}/${route}`;\n acc[route] = {\n path: src,\n route,\n headers: headers || {},\n };\n\n return acc;\n }, {});\n }\n}\n/**\n * A controller that allows the application to server specific files from any folder to\n * any route without the need of mounting directories as \"static\" folders.\n *\n * @group Controllers\n * @group Controllers/Statics\n */\nexport const staticsController = controllerCreator(\n ({ getMiddlewares, ...options }: StaticsControllerCreatorOptions = {}) =>\n (app) => {\n const router = app.getRouter();\n const ctrl = new StaticsController({\n inject: {\n sendFile: app.get('sendFile'),\n },\n ...options,\n });\n\n let useMiddlewares: ExpressMiddleware[] | undefined;\n if (getMiddlewares) {\n useMiddlewares = getMiddlewares(app)\n .map((middleware) => {\n if ('middleware' in middleware) {\n return middleware.connect(app) as ExpressMiddleware | undefined;\n }\n\n return middleware as ExpressMiddleware;\n })\n .filter(notUndefined);\n }\n\n return ctrl.addRoutes(router, useMiddlewares);\n },\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAAsB;AACtB,WAAsB;AACtB,yBAAwC;AACxC,mBAKO;AAwIA,MAAM,kBAAkB;AAAA,EAiB7B,YAAY,EAAE,WAAW,QAAQ,GAAwC;AAbzE,wBAAmB;AAInB,wBAAU;AAKV,wBAAU;AAKR,SAAK,YAAY,OAAO;AACxB,SAAK,WAAW,KAAK;AAAA,UACnB;AAAA,QACE;AAAA,UACE,OAAO,CAAC,eAAe,YAAY;AAAA,UACnC,SAAS,QAAQ,WAAW;AAAA,YAC1B,KAAK;AAAA,YACL,KAAK;AAAA,UACP;AAAA,UACA,OAAO;AAAA,YACL,OAAO;AAAA,YACP,QAAQ;AAAA,UACV;AAAA,QACF;AAAA,QACA;AAAA,MACF;AAAA,IACF;AACA,SAAK,QAAQ,KAAK,aAAa;AAAA,EACjC;AAAA,EAQA,UAAU,QAAgB,cAAmC,CAAC,GAAW;AACvE,UAAM,EAAE,QAAQ,IAAI,KAAK;AACzB,UAAM,MAAsB,QAAQ,MAChC,CAAC,KAAK,IACN,OAAO,KAAK,OAAO,EAAE,OAA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ACxB,UAAI,SAAS;AAAA,QACX,MAAM;AAAA,QACN;AAAA,QACA,SAAS,WAAW,CAAC;AAAA,MACvB;AAEA,aAAO;AAAA,IACT,GAAG,CAAC,CAAC;AAAA,EACP;AACF;AAQO,MAAM,wBAAoB;AAAA,EAC/B,CAAC,EAAE,mBAAmB,QAAQ,IAAqC,CAAC,MAClE,CAAC,QAAQ;AACP,UAAM,SAAS,IAAI,UAAU;AAC7B,UAAM,OAAO,IAAI,kBAAkB;AAAA,MACjC,QAAQ;AAAA,QACN,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAED,QAAI;AACJ,QAAI,gBAAgB;AAClB,uBAAiB,eAAe,GAAG,EAChC,IAAI,CAAC,eAAe;AACnB,YAAI,gBAAgB,YAAY;AAC9B,iBAAO,WAAW,QAAQ,GAAG;AAAA,QAC/B;AAEA,eAAO;AAAA,MACT,CAAC,EACA,OAAO,yBAAY;AAAA,IACxB;AAEA,WAAO,KAAK,UAAU,QAAQ,cAAc;AAAA,EAC9C;AACJ;","names":[]}
1
+ {"version":3,"sources":["../../../src/controllers/common/statics.ts"],"sourcesContent":["import * as path from 'path';\nimport type mime from 'mime';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport {\n controllerCreator,\n removeSlashes,\n notUndefined,\n type MiddlewareLike,\n} from '../../utils';\nimport type { SendFile } from '../../services';\nimport type { Jimpex } from '../../app';\nimport type { DeepPartial, ExpressMiddleware, Router, RouterMethod } from '../../types';\n\ntype Mime = typeof mime;\n\n/**\n * The definition for each file the controller handles.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerFile = {\n /**\n * The route, relative to the controller root, to the file.\n */\n route: string;\n /**\n * The path to the file in the filesystem. Since the file is served using the\n * {@link SendFile} service, whether the file is relative to the project root or the\n * application executable depends on how the service is configured (relative to the\n * executable by default).\n */\n path: string;\n /**\n * A dictionary of headers for the response.\n */\n headers?: Record<string, string>;\n};\n/**\n * These are like \"master paths\" that get prepended to all the file paths and routes the\n * controller use.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerPathsOptions = {\n /**\n * A custom route to prefix all the file routes with.\n */\n route: string;\n /**\n * A custom path to prefix all the file paths with.\n */\n source: string;\n};\n/**\n * The options to customize the controller.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerOptions = {\n /**\n * A list of filenames, or definitions for the files to handle.\n */\n files: Array<string | StaticsControllerFile>;\n /**\n * A dictionary with the allowed router (HTTP) methods the controller can use to serve\n * the files. If `all` is set to `true`, the rest of the values will be ignored.\n *\n * @default {get: true, all: false}\n */\n methods: Partial<Record<RouterMethod, boolean>>;\n /**\n * The \"master paths\" the controller can use to prefix the file paths and routes.\n *\n * @default {route: '', source: './',}\n */\n paths: StaticsControllerPathsOptions;\n};\n/**\n * The options to construct a {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerConstructorOptions =\n DeepPartial<StaticsControllerOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n sendFile: SendFile;\n mime: Mime;\n };\n };\n/**\n * A function to generate a list of middlewares that can be executed before the tontroller\n * main middleware.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];\n/**\n * The options for the controller creator that mounts {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {\n /**\n * A function to generate a list of middlewares that can be executed before the\n * tontroller main middleware.\n */\n getMiddlewares?: StaticsControllerGetMiddlewaresFn;\n};\n/**\n * The options for {@link StaticsController._addRoute}.\n *\n * @access protected\n * @group Controllers/Statics\n */\nexport type AddStaticRouteOptions = {\n /**\n * The reference for the router in which the middlewares will be added.\n */\n router: Router;\n /**\n * The router method in which the middlewares will be added.\n */\n method: RouterMethod;\n /**\n * The definition of the file to serve.\n */\n file: StaticsControllerFile;\n /**\n * The middleware created by {@link StaticsController}, that will serve the file.\n */\n fileMiddleware: ExpressMiddleware;\n /**\n * A list of extra middlewares to execute before the file middleware.\n */\n middlewares: ExpressMiddleware[];\n};\n\n/**\n * The controller class that allows the application to serve specific files from any\n * folder to any route without the need of mounting directories as \"static\".\n *\n * @group Controller Classes\n * @group Controllers/Statics\n * @prettierignore\n */\nexport class StaticsController {\n /**\n * The service that serves static files.\n */\n protected readonly _sendFile: SendFile;\n /**\n * The MIME type library. Since it's an ESM only module, Jimpex loads it on boot and makes\n * it available on the container.\n */\n protected readonly _mime: Mime;\n /**\n * The controller customization options.\n */\n protected _options: StaticsControllerOptions;\n /**\n * A dictionary with the formatted definitions of the files that will be served.\n * It uses the files' routes as keys, for easy access in the middleware.\n */\n protected files: Record<string, StaticsControllerFile>;\n /**\n * @param options The options to construct the controller.\n */\n constructor({ inject, ...options }: StaticsControllerConstructorOptions) {\n this._sendFile = inject.sendFile;\n this._mime = inject.mime;\n this._options = this._validateOptions(\n deepAssignWithOverwrite(\n {\n files: ['favicon.ico', 'index.html'],\n methods: options.methods || {\n all: false,\n get: true,\n },\n paths: {\n route: '',\n source: './',\n },\n },\n options,\n ),\n );\n this.files = this._createFiles();\n }\n /**\n * Mounts the middlewares in the router in order to serve the files.\n *\n * @param router A reference to the application router.\n * @param middlewares A list of extra middlewares to execute before the file\n * middleware.\n */\n addRoutes(router: Router, middlewares: ExpressMiddleware[] = []): Router {\n const { methods } = this._options;\n const use: RouterMethod[] = methods.all\n ? ['all']\n : Object.keys(methods).reduce<RouterMethod[]>((acc, name) => {\n const methodName = name as RouterMethod;\n if (methods[methodName]) {\n acc.push(methodName);\n }\n\n return acc;\n }, []);\n\n Object.keys(this.files).forEach((route) => {\n const file = this.files[route as keyof typeof this.files]!;\n const fileMiddleware = this._getMiddleware(file);\n use.forEach((method) =>\n this._addRoute({ router, method, file, fileMiddleware, middlewares }),\n );\n });\n\n return router;\n }\n /**\n * The controller options.\n */\n get options(): Readonly<StaticsControllerOptions> {\n return { ...this._options };\n }\n /**\n * Generates the middleware that will serve the file.\n *\n * @param file The definition of the file to serve.\n */\n protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware {\n return (_, res, next) => {\n const extension = path.parse(file.path).ext.substring(1);\n const headers = {\n 'Content-Type': this._mime.getType(extension) || 'text/html',\n ...file.headers,\n };\n\n Object.entries(headers).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n\n this._sendFile({\n res,\n filepath: file.path,\n next,\n });\n };\n }\n /**\n * Mounts the middleware(s) for a file in the router.\n *\n * @param options The information of the file and how it needs to be added.\n */\n protected _addRoute({\n router,\n method,\n file,\n fileMiddleware,\n middlewares,\n }: AddStaticRouteOptions): void {\n const { route } = file;\n router[method](route, [...middlewares, fileMiddleware]);\n }\n /**\n * Validates and formats the options sent to the constructor in order to get the final\n * set that will be stored in the controller.\n *\n * @param options The options to validate.\n * @throws If no files are specified.\n * @throws If methods is not defined.\n * @throws If no methods are enabled.\n * @throws If there's an invalid HTTP method.\n */\n protected _validateOptions(\n options: StaticsControllerOptions,\n ): StaticsControllerOptions {\n if (!options.files || !options.files.length) {\n throw new Error('You need to specify a list of files');\n }\n\n if (!options.methods) {\n throw new Error('You need to specify which HTTP methods are allowed for the files');\n }\n\n const methods = Object.keys(options.methods) as RouterMethod[];\n\n const atLeastOne = methods.some((method) => options.methods[method]);\n if (!atLeastOne) {\n throw new Error('You need to enable at least one HTTP method to serve the files');\n }\n\n const allowedMethods: RouterMethod[] = [\n 'all',\n 'get',\n 'head',\n 'post',\n 'patch',\n 'put',\n 'delete',\n 'connect',\n 'options',\n 'trace',\n ];\n\n const invalid = methods.find(\n (method) => !allowedMethods.includes(method.toLowerCase() as RouterMethod),\n );\n\n if (invalid) {\n throw new Error(`${invalid} is not a valid HTTP method`);\n }\n\n const newMethods = methods.reduce<Record<string, boolean>>((acc, method) => {\n acc[method.toLowerCase()] = !!options.methods[method];\n return acc;\n }, {});\n\n return {\n ...options,\n methods: newMethods,\n };\n }\n /**\n * Parses the files received from the constructor's options, and formats them into\n * proper definitions the controller can use.\n */\n protected _createFiles(): Record<string, StaticsControllerFile> {\n const { files, paths } = this._options;\n const routePath = removeSlashes(paths.route, false, true);\n return files.reduce<Record<string, StaticsControllerFile>>((acc, file) => {\n let src;\n let route;\n let headers;\n if (typeof file === 'object') {\n ({ route, path: src, headers } = file);\n } else {\n src = file;\n route = file;\n }\n\n src = path.join(paths.source, src);\n route = removeSlashes(route, true, false);\n route = `${routePath}/${route}`;\n acc[route] = {\n path: src,\n route,\n headers: headers || {},\n };\n\n return acc;\n }, {});\n }\n}\n/**\n * A controller that allows the application to server specific files from any folder to\n * any route without the need of mounting directories as \"static\" folders.\n *\n * @group Controllers\n * @group Controllers/Statics\n */\nexport const staticsController = controllerCreator(\n ({ getMiddlewares, ...options }: StaticsControllerCreatorOptions = {}) =>\n (app) => {\n const router = app.getRouter();\n const ctrl = new StaticsController({\n inject: {\n sendFile: app.get('sendFile'),\n mime: app.get('mime'),\n },\n ...options,\n });\n\n let useMiddlewares: ExpressMiddleware[] | undefined;\n if (getMiddlewares) {\n useMiddlewares = getMiddlewares(app)\n .map((middleware) => {\n if ('middleware' in middleware) {\n return middleware.connect(app) as ExpressMiddleware | undefined;\n }\n\n return middleware as ExpressMiddleware;\n })\n .filter(notUndefined);\n }\n\n return ctrl.addRoutes(router, useMiddlewares);\n },\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAAsB;AAEtB,yBAAwC;AACxC,mBAKO;AA4IA,MAAM,kBAAkB;AAAA;AAAA;AAAA;AAAA,EAsB7B,YAAY,EAAE,QAAQ,GAAG,QAAQ,GAAwC;AAlBzE;AAAA;AAAA;AAAA,wBAAmB;AAKnB;AAAA;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAU;AAKV;AAAA;AAAA;AAAA;AAAA,wBAAU;AAKR,SAAK,YAAY,OAAO;AACxB,SAAK,QAAQ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eAAe,GAAG,EAChC,IAAI,CAAC,eAAe;AACnB,YAAI,gBAAgB,YAAY;AAC9B,iBAAO,WAAW,QAAQ,GAAG;AAAA,QAC/B;AAEA,eAAO;AAAA,MACT,CAAC,EACA,OAAO,yBAAY;AAAA,IACxB;AAEA,WAAO,KAAK,UAAU,QAAQ,cAAc;AAAA,EAC9C;AACJ;","names":[]}
@@ -0,0 +1,25 @@
1
+ export { ConfigController, ConfigControllerOptions, configController } from './common/config.mjs';
2
+ export { GetHealthStatus, HealthController, HealthControllerOptions, healthController } from './common/health.mjs';
3
+ export { AddStaticRouteOptions, StaticsController, StaticsControllerConstructorOptions, StaticsControllerCreatorOptions, StaticsControllerFile, StaticsControllerGetMiddlewaresFn, StaticsControllerOptions, StaticsControllerPathsOptions, staticsController } from './common/statics.mjs';
4
+ export { AddGatewayRouteOptions, GatewayConfig, GatewayConfigEndpointDefinition, GatewayConfigEndpointProps, GatewayConfigEndpoints, GatewayController, GatewayControllerAPIConfig, GatewayControllerAPIConfigOptions, GatewayControllerConstructorOptions, GatewayControllerCreatorOptions, GatewayControllerEndpointInfo, GatewayControllerExtraOptions, GatewayControllerGetMiddlewaresFn, GatewayControllerHeaderOptions, GatewayControllerHelperOptions, GatewayControllerOptions, GatewayControllerRequest, GatewayControllerRoute, GatewayControllerRouteMethod, GatewayHelperService, GatewayHelperServiceBaseFnOptions, GatewayHelperServiceErrorHandler, GatewayHelperServiceErrorHandlerOptions, GatewayHelperServiceRequestReducer, GatewayHelperServiceRequestReducerOptions, GatewayHelperServiceResponseHandler, GatewayHelperServiceResponseReducer, GatewayHelperServiceResponseReducerOptions, GatewayHelperServiceStreamVerification, gatewayController } from './utils/gateway.mjs';
5
+ import '@homer0/jimple';
6
+ import '../index-Bwf7JHu9.mjs';
7
+ import '../types/express.mjs';
8
+ import 'express';
9
+ import '../types/http.mjs';
10
+ import 'https';
11
+ import 'http';
12
+ import 'spdy';
13
+ import 'node-fetch';
14
+ import '../types/utils.mjs';
15
+ import '@homer0/path-utils';
16
+ import '@homer0/simple-logger';
17
+ import '@homer0/simple-config';
18
+ import '@homer0/events-hub';
19
+ import '../services/http/responsesBuilder.mjs';
20
+ import '../utils/fns/statuses.mjs';
21
+ import 'statuses';
22
+ import 'mime';
23
+ import '../services/common/sendFile.mjs';
24
+ import '@homer0/api-utils';
25
+ import '../services/http/http.mjs';
@@ -3,8 +3,7 @@ export { GetHealthStatus, HealthController, HealthControllerOptions, healthContr
3
3
  export { AddStaticRouteOptions, StaticsController, StaticsControllerConstructorOptions, StaticsControllerCreatorOptions, StaticsControllerFile, StaticsControllerGetMiddlewaresFn, StaticsControllerOptions, StaticsControllerPathsOptions, staticsController } from './common/statics.js';
4
4
  export { AddGatewayRouteOptions, GatewayConfig, GatewayConfigEndpointDefinition, GatewayConfigEndpointProps, GatewayConfigEndpoints, GatewayController, GatewayControllerAPIConfig, GatewayControllerAPIConfigOptions, GatewayControllerConstructorOptions, GatewayControllerCreatorOptions, GatewayControllerEndpointInfo, GatewayControllerExtraOptions, GatewayControllerGetMiddlewaresFn, GatewayControllerHeaderOptions, GatewayControllerHelperOptions, GatewayControllerOptions, GatewayControllerRequest, GatewayControllerRoute, GatewayControllerRouteMethod, GatewayHelperService, GatewayHelperServiceBaseFnOptions, GatewayHelperServiceErrorHandler, GatewayHelperServiceErrorHandlerOptions, GatewayHelperServiceRequestReducer, GatewayHelperServiceRequestReducerOptions, GatewayHelperServiceResponseHandler, GatewayHelperServiceResponseReducer, GatewayHelperServiceResponseReducerOptions, GatewayHelperServiceStreamVerification, gatewayController } from './utils/gateway.js';
5
5
  import '@homer0/jimple';
6
- import '../jimpex-7eaee271.js';
7
- import '@homer0/events-hub';
6
+ import '../index-C6I3NCC-.js';
8
7
  import '../types/express.js';
9
8
  import 'express';
10
9
  import '../types/http.js';
@@ -12,13 +11,15 @@ import 'https';
12
11
  import 'http';
13
12
  import 'spdy';
14
13
  import 'node-fetch';
15
- import '@homer0/simple-config';
16
14
  import '../types/utils.js';
15
+ import '@homer0/path-utils';
17
16
  import '@homer0/simple-logger';
17
+ import '@homer0/simple-config';
18
+ import '@homer0/events-hub';
18
19
  import '../services/http/responsesBuilder.js';
19
20
  import '../utils/fns/statuses.js';
20
21
  import 'statuses';
22
+ import 'mime';
21
23
  import '../services/common/sendFile.js';
22
- import '@homer0/path-utils';
23
24
  import '@homer0/api-utils';
24
25
  import '../services/http/http.js';
@@ -17,4 +17,9 @@ var controllers_exports = {};
17
17
  module.exports = __toCommonJS(controllers_exports);
18
18
  __reExport(controllers_exports, require("./common"), module.exports);
19
19
  __reExport(controllers_exports, require("./utils"), module.exports);
20
+ // Annotate the CommonJS export names for ESM import in node:
21
+ 0 && (module.exports = {
22
+ ...require("./common"),
23
+ ...require("./utils")
24
+ });
20
25
  //# sourceMappingURL=index.js.map