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,179 @@
1
+ import * as _homer0_jimple from '@homer0/jimple';
2
+ import { J as Jimpex } from '../../index-Bwf7JHu9.mjs';
3
+ import { Response } from 'express';
4
+ import { Statuses } from '../../utils/fns/statuses.mjs';
5
+ import { SimpleConfig } from '@homer0/simple-config';
6
+ import '../../types/express.mjs';
7
+ import '../../types/http.mjs';
8
+ import 'https';
9
+ import 'http';
10
+ import 'spdy';
11
+ import 'node-fetch';
12
+ import '../../types/utils.mjs';
13
+ import '@homer0/path-utils';
14
+ import '@homer0/simple-logger';
15
+ import '@homer0/events-hub';
16
+ import 'statuses';
17
+
18
+ /**
19
+ * The options to construct a {@link ResponsesBuilder}.
20
+ *
21
+ * @group Services/ResponsesBuilder
22
+ */
23
+ type ResponsesBuilderConstructorOptions = {
24
+ /**
25
+ * A dictionary with the dependencies to inject.
26
+ */
27
+ inject: {
28
+ config: SimpleConfig;
29
+ statuses: Statuses;
30
+ };
31
+ };
32
+ /**
33
+ * The options to build a response for a post message.
34
+ *
35
+ * @group Services/ResponsesBuilder
36
+ */
37
+ type HTMLPostMessageResponseOptions = {
38
+ /**
39
+ * The response object generated by the application.
40
+ */
41
+ res: Response;
42
+ /**
43
+ * The title for the HTML.
44
+ */
45
+ title: string;
46
+ /**
47
+ * The contents of the post message.
48
+ */
49
+ message: string;
50
+ /**
51
+ * The status code for the response.
52
+ *
53
+ * @default 200
54
+ */
55
+ status?: number | string;
56
+ /**
57
+ * From which object will the `postMessage` method called from.
58
+ *
59
+ * @default 'window.opener'
60
+ */
61
+ target?: string;
62
+ /**
63
+ * Whether or not to call `window.close` after sending the message.
64
+ *
65
+ * @default true
66
+ */
67
+ close?: boolean;
68
+ /**
69
+ * In case `close` is set to `true`, this option is to specify how many milliseconds
70
+ * should be waited before closing the window.
71
+ *
72
+ * @default 700
73
+ */
74
+ closeDelay?: number;
75
+ };
76
+ /**
77
+ * The options to build a reponse for JSON.
78
+ *
79
+ * @group Services/ResponsesBuilder
80
+ */
81
+ type JSONResponseOptions = {
82
+ /**
83
+ * The response object generated by the application.
84
+ */
85
+ res: Response;
86
+ /**
87
+ * The actual data for the response. It will be added in a `data` property.
88
+ */
89
+ data: unknown;
90
+ /**
91
+ * The status code for the response.
92
+ *
93
+ * @default 200
94
+ */
95
+ status?: number | string;
96
+ /**
97
+ * Extra information to include inside the `metadata` object.
98
+ */
99
+ metadata?: object;
100
+ };
101
+ /**
102
+ * A utility service to build responses.
103
+ *
104
+ * @group Services
105
+ * @group Services/ResponsesBuilder
106
+ */
107
+ declare class ResponsesBuilder {
108
+ /**
109
+ * The application configuration, to get the `version` and the `postMessagesPrefix`.
110
+ */
111
+ protected readonly _config: SimpleConfig;
112
+ /**
113
+ * The uility service to get HTTP status codes.
114
+ */
115
+ protected readonly _statuses: Statuses;
116
+ /**
117
+ * @param options The options to construct the service.
118
+ */
119
+ constructor({ inject: { config, statuses } }: ResponsesBuilderConstructorOptions);
120
+ /**
121
+ * Generates and send an HTML response that emits a post message.
122
+ * The post message will be prefixed with the value of the configuration setting
123
+ * `postMessagesPrefix`.
124
+ *
125
+ * @param options The options to customize how the HTML is generated.
126
+ */
127
+ htmlPostMessage(options: HTMLPostMessageResponseOptions): void;
128
+ /**
129
+ * Generates and sends a JSON response.
130
+ *
131
+ * @param options The options to customize how the JSON is generated.
132
+ * @example
133
+ *
134
+ * <caption>The generated looks like this.</caption>
135
+ *
136
+ * {
137
+ * metadata: {
138
+ * version: 'dev',
139
+ * status: 200,
140
+ * },
141
+ * data: { foo: 'bar' },
142
+ * }
143
+ *
144
+ */
145
+ json(options: JSONResponseOptions): void;
146
+ /**
147
+ * Generates a basic HTML template for the service to use when generating a post message
148
+ * response.
149
+ *
150
+ * @param title The HTML `<title />` attribute.
151
+ * @param code Javascript code to be wrapped on a `<script />` tag.
152
+ */
153
+ protected _htmlTemplate(title: string, code: string): string;
154
+ /**
155
+ * Utility method used to make sure a recevied status is a valid status code. If the
156
+ * status is a string, the method will try to find the code from the `statuses` package.
157
+ *
158
+ * @param status The status to normalize.
159
+ * @returns If `status` is a string, but there's no valid code, it will return 200.
160
+ */
161
+ protected _normalizeStatus(status: number | string): number;
162
+ }
163
+ /**
164
+ * The service provider that once registered on the container will set an instance of
165
+ * {@link ResponsesBuilder} as the `responsesBuilder` service.
166
+ *
167
+ * @example
168
+ *
169
+ * // Register it on the container
170
+ * container.register(responsesBuilderProvider);
171
+ * // Getting access to the service instance
172
+ * const responsesBuilder = container.get<ResponsesBuilder>('responsesBuilder');
173
+ *
174
+ * @group Providers
175
+ * @group Services/ResponsesBuilder
176
+ */
177
+ declare const responsesBuilderProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
178
+
179
+ export { type HTMLPostMessageResponseOptions, type JSONResponseOptions, ResponsesBuilder, type ResponsesBuilderConstructorOptions, responsesBuilderProvider };
@@ -1,9 +1,8 @@
1
1
  import * as _homer0_jimple from '@homer0/jimple';
2
- import { J as Jimpex } from '../../jimpex-7eaee271.js';
2
+ import { J as Jimpex } from '../../index-C6I3NCC-.js';
3
3
  import { Response } from 'express';
4
4
  import { Statuses } from '../../utils/fns/statuses.js';
5
5
  import { SimpleConfig } from '@homer0/simple-config';
6
- import '@homer0/events-hub';
7
6
  import '../../types/express.js';
8
7
  import '../../types/http.js';
9
8
  import 'https';
@@ -11,7 +10,9 @@ import 'http';
11
10
  import 'spdy';
12
11
  import 'node-fetch';
13
12
  import '../../types/utils.js';
13
+ import '@homer0/path-utils';
14
14
  import '@homer0/simple-logger';
15
+ import '@homer0/events-hub';
15
16
  import 'statuses';
16
17
 
17
18
  /**
@@ -175,4 +176,4 @@ declare class ResponsesBuilder {
175
176
  */
176
177
  declare const responsesBuilderProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
177
178
 
178
- export { HTMLPostMessageResponseOptions, JSONResponseOptions, ResponsesBuilder, ResponsesBuilderConstructorOptions, responsesBuilderProvider };
179
+ export { type HTMLPostMessageResponseOptions, type JSONResponseOptions, ResponsesBuilder, type ResponsesBuilderConstructorOptions, responsesBuilderProvider };
@@ -30,12 +30,28 @@ module.exports = __toCommonJS(responsesBuilder_exports);
30
30
  var import_utils = require("../../utils");
31
31
  const DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE = 700;
32
32
  class ResponsesBuilder {
33
+ /**
34
+ * @param options The options to construct the service.
35
+ */
33
36
  constructor({ inject: { config, statuses } }) {
37
+ /**
38
+ * The application configuration, to get the `version` and the `postMessagesPrefix`.
39
+ */
34
40
  __publicField(this, "_config");
41
+ /**
42
+ * The uility service to get HTTP status codes.
43
+ */
35
44
  __publicField(this, "_statuses");
36
45
  this._config = config;
37
46
  this._statuses = statuses;
38
47
  }
48
+ /**
49
+ * Generates and send an HTML response that emits a post message.
50
+ * The post message will be prefixed with the value of the configuration setting
51
+ * `postMessagesPrefix`.
52
+ *
53
+ * @param options The options to customize how the HTML is generated.
54
+ */
39
55
  htmlPostMessage(options) {
40
56
  const {
41
57
  res,
@@ -66,6 +82,23 @@ class ResponsesBuilder {
66
82
  res.write(html);
67
83
  res.end();
68
84
  }
85
+ /**
86
+ * Generates and sends a JSON response.
87
+ *
88
+ * @param options The options to customize how the JSON is generated.
89
+ * @example
90
+ *
91
+ * <caption>The generated looks like this.</caption>
92
+ *
93
+ * {
94
+ * metadata: {
95
+ * version: 'dev',
96
+ * status: 200,
97
+ * },
98
+ * data: { foo: 'bar' },
99
+ * }
100
+ *
101
+ */
69
102
  json(options) {
70
103
  const { res, data, status, metadata = {} } = options;
71
104
  const useStatus = typeof status === "undefined" ? this._statuses("ok") : this._normalizeStatus(status);
@@ -80,6 +113,13 @@ class ResponsesBuilder {
80
113
  });
81
114
  res.end();
82
115
  }
116
+ /**
117
+ * Generates a basic HTML template for the service to use when generating a post message
118
+ * response.
119
+ *
120
+ * @param title The HTML `<title />` attribute.
121
+ * @param code Javascript code to be wrapped on a `<script />` tag.
122
+ */
83
123
  _htmlTemplate(title, code) {
84
124
  return `
85
125
  <!DOCTYPE html>
@@ -93,11 +133,18 @@ class ResponsesBuilder {
93
133
  <body>
94
134
  <script type="text/javascript">
95
135
  ${code}
96
- <\/script>
136
+ </script>
97
137
  </body>
98
138
  </html>
99
139
  `;
100
140
  }
141
+ /**
142
+ * Utility method used to make sure a recevied status is a valid status code. If the
143
+ * status is a string, the method will try to find the code from the `statuses` package.
144
+ *
145
+ * @param status The status to normalize.
146
+ * @returns If `status` is a string, but there's no valid code, it will return 200.
147
+ */
101
148
  _normalizeStatus(status) {
102
149
  let useStatus;
103
150
  try {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/services/http/responsesBuilder.ts"],"sourcesContent":["import type { Config, Response } from '../../types';\nimport { provider, type Statuses } from '../../utils';\n/**\n * The options to construct a {@link ResponsesBuilder}.\n *\n * @group Services/ResponsesBuilder\n */\nexport type ResponsesBuilderConstructorOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n config: Config;\n statuses: Statuses;\n };\n};\n/**\n * The options to build a response for a post message.\n *\n * @group Services/ResponsesBuilder\n */\nexport type HTMLPostMessageResponseOptions = {\n /**\n * The response object generated by the application.\n */\n res: Response;\n /**\n * The title for the HTML.\n */\n title: string;\n /**\n * The contents of the post message.\n */\n message: string;\n /**\n * The status code for the response.\n *\n * @default 200\n */\n status?: number | string;\n /**\n * From which object will the `postMessage` method called from.\n *\n * @default 'window.opener'\n */\n target?: string;\n /**\n * Whether or not to call `window.close` after sending the message.\n *\n * @default true\n */\n close?: boolean;\n /**\n * In case `close` is set to `true`, this option is to specify how many milliseconds\n * should be waited before closing the window.\n *\n * @default 700\n */\n closeDelay?: number;\n};\n/**\n * The options to build a reponse for JSON.\n *\n * @group Services/ResponsesBuilder\n */\nexport type JSONResponseOptions = {\n /**\n * The response object generated by the application.\n */\n res: Response;\n /**\n * The actual data for the response. It will be added in a `data` property.\n */\n data: unknown;\n /**\n * The status code for the response.\n *\n * @default 200\n */\n status?: number | string;\n /**\n * Extra information to include inside the `metadata` object.\n */\n metadata?: object;\n};\n/**\n * The default time to wait before closing a window, in case it's enabled.\n */\nconst DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE = 700;\n/**\n * A utility service to build responses.\n *\n * @group Services\n * @group Services/ResponsesBuilder\n */\nexport class ResponsesBuilder {\n /**\n * The application configuration, to get the `version` and the `postMessagesPrefix`.\n */\n protected readonly _config: Config;\n /**\n * The uility service to get HTTP status codes.\n */\n protected readonly _statuses: Statuses;\n /**\n * @param options The options to construct the service.\n */\n constructor({ inject: { config, statuses } }: ResponsesBuilderConstructorOptions) {\n this._config = config;\n this._statuses = statuses;\n }\n /**\n * Generates and send an HTML response that emits a post message.\n * The post message will be prefixed with the value of the configuration setting\n * `postMessagesPrefix`.\n *\n * @param options The options to customize how the HTML is generated.\n */\n htmlPostMessage(options: HTMLPostMessageResponseOptions): void {\n const {\n res,\n title,\n message,\n status,\n target = 'window.opener',\n close = true,\n closeDelay = DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE,\n } = options;\n const prefix = this._config.get<string | undefined>('postMessagesPrefix') ?? '';\n const closeCode = close\n ? `setTimeout(function() { window.close(); }, ${closeDelay});`\n : '';\n\n const html = this._htmlTemplate(\n title,\n `\n (function() {\n if (${target}) {\n ${target}.postMessage('${prefix}${message}', '*');\n ${closeCode}\n }\n })();\n `,\n );\n\n const useStatus =\n typeof status === 'undefined'\n ? (this._statuses('ok') as number)\n : this._normalizeStatus(status);\n\n res.setHeader('Content-Type', 'text/html');\n res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store');\n res.status(useStatus);\n res.write(html);\n res.end();\n }\n /**\n * Generates and sends a JSON response.\n *\n * @param options The options to customize how the JSON is generated.\n * @example\n *\n * <caption>The generated looks like this.</caption>\n *\n * {\n * metadata: {\n * version: 'dev',\n * status: 200,\n * },\n * data: { foo: 'bar' },\n * }\n *\n */\n json(options: JSONResponseOptions): void {\n const { res, data, status, metadata = {} } = options;\n const useStatus =\n typeof status === 'undefined'\n ? (this._statuses('ok') as number)\n : this._normalizeStatus(status);\n\n res.status(useStatus);\n res.json({\n metadata: {\n version: this._config.get<string>('version'),\n status: useStatus,\n ...metadata,\n },\n data,\n });\n res.end();\n }\n /**\n * Generates a basic HTML template for the service to use when generating a post message\n * response.\n *\n * @param title The HTML `<title />` attribute.\n * @param code Javascript code to be wrapped on a `<script />` tag.\n */\n protected _htmlTemplate(title: string, code: string): string {\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"x-ua-compatible\" content=\"ie=edge\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <title>${title}</title>\n </head>\n <body>\n <script type=\"text/javascript\">\n ${code}\n </script>\n </body>\n </html>\n `;\n }\n /**\n * Utility method used to make sure a recevied status is a valid status code. If the\n * status is a string, the method will try to find the code from the `statuses` package.\n *\n * @param status The status to normalize.\n * @returns If `status` is a string, but there's no valid code, it will return 200.\n */\n protected _normalizeStatus(status: number | string): number {\n let useStatus: number;\n try {\n if (typeof status === 'string') {\n useStatus = this._statuses(status) as number;\n } else {\n this._statuses(status);\n useStatus = status;\n }\n } catch (_) {\n useStatus = this._statuses('ok') as number;\n }\n\n return useStatus;\n }\n}\n/**\n * The service provider that once registered on the container will set an instance of\n * {@link ResponsesBuilder} as the `responsesBuilder` service.\n *\n * @example\n *\n * // Register it on the container\n * container.register(responsesBuilderProvider);\n * // Getting access to the service instance\n * const responsesBuilder = container.get<ResponsesBuilder>('responsesBuilder');\n *\n * @group Providers\n * @group Services/ResponsesBuilder\n */\nexport const responsesBuilderProvider = provider((app) => {\n app.set(\n 'responsesBuilder',\n () =>\n new ResponsesBuilder({\n inject: {\n config: app.getConfig(),\n statuses: app.get('statuses'),\n },\n }),\n );\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAwC;AAuFxC,MAAM,uCAAuC;AAOtC,MAAM,iBAAiB;AAAA,EAY5B,YAAY,EAAE,QAAQ,EAAE,QAAQ,SAAS,EAAE,GAAuC;AARlF,wBAAmB;AAInB,wBAAmB;AAKjB,SAAK,UAAU;AACf,SAAK,YAAY;AAAA,EACnB;AAAA,EAQA,gBAAgB,SAA+C;AAC7D,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,SAAS;AAAA,MACT,QAAQ;AAAA,MACR,aAAa;AAAA,IACf,IAAI;AACJ,UAAM,SAAS,KAAK,QAAQ,IAAwB,oBAAoB,KAAK;AAC7E,UAAM,YAAY,QACd,8CAA8C,iBAC9C;AAEJ,UAAM,OAAO,KAAK;AAAA,MAChB;AAAA,MACA;AAAA;AAAA,cAEQ;AAAA,YACF,uBAAuB,SAAS;AAAA,YAChC;AAAA;AAAA;AAAA;AAAA,IAIR;AAEA,UAAM,YACJ,OAAO,WAAW,cACb,KAAK,UAAU,IAAI,IACpB,KAAK,iBAAiB,MAAM;AAElC,QAAI,UAAU,gBAAgB,WAAW;AACzC,QAAI,UAAU,iBAAiB,gDAAgD;AAC/E,QAAI,OAAO,SAAS;AACpB,QAAI,MAAM,IAAI;AACd,QAAI,IAAI;AAAA,EACV;AAAA,EAkBA,KAAK,SAAoC;AACvC,UAAM,EAAE,KAAK,MAAM,QAAQ,WAAW,CAAC,EAAE,IAAI;AAC7C,UAAM,YACJ,OAAO,WAAW,cACb,KAAK,UAAU,IAAI,IACpB,KAAK,iBAAiB,MAAM;AAElC,QAAI,OAAO,SAAS;AACpB,QAAI,KAAK;AAAA,MACP,UAAU;AAAA,QACR,SAAS,KAAK,QAAQ,IAAY,SAAS;AAAA,QAC3C,QAAQ;AAAA,QACR,GAAG;AAAA,MACL;AAAA,MACA;AAAA,IACF,CAAC;AACD,QAAI,IAAI;AAAA,EACV;AAAA,EAQU,cAAc,OAAe,MAAsB;AAC3D,WAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAOQ;AAAA;AAAA;AAAA;AAAA,YAIP;AAAA;AAAA;AAAA;AAAA;AAAA,EAKV;AAAA,EAQU,iBAAiB,QAAiC;AAC1D,QAAI;AACJ,QAAI;AACF,UAAI,OAAO,WAAW,UAAU;AAC9B,oBAAY,KAAK,UAAU,MAAM;AAAA,MACnC,OAAO;AACL,aAAK,UAAU,MAAM;AACrB,oBAAY;AAAA,MACd;AAAA,IACF,SAAS,GAAP;AACA,kBAAY,KAAK,UAAU,IAAI;AAAA,IACjC;AAEA,WAAO;AAAA,EACT;AACF;AAeO,MAAM,+BAA2B,uBAAS,CAAC,QAAQ;AACxD,MAAI;AAAA,IACF;AAAA,IACA,MACE,IAAI,iBAAiB;AAAA,MACnB,QAAQ;AAAA,QACN,QAAQ,IAAI,UAAU;AAAA,QACtB,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,IACF,CAAC;AAAA,EACL;AACF,CAAC;","names":[]}
1
+ {"version":3,"sources":["../../../src/services/http/responsesBuilder.ts"],"sourcesContent":["import type { Config, Response } from '../../types';\nimport { provider, type Statuses } from '../../utils';\n/**\n * The options to construct a {@link ResponsesBuilder}.\n *\n * @group Services/ResponsesBuilder\n */\nexport type ResponsesBuilderConstructorOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n config: Config;\n statuses: Statuses;\n };\n};\n/**\n * The options to build a response for a post message.\n *\n * @group Services/ResponsesBuilder\n */\nexport type HTMLPostMessageResponseOptions = {\n /**\n * The response object generated by the application.\n */\n res: Response;\n /**\n * The title for the HTML.\n */\n title: string;\n /**\n * The contents of the post message.\n */\n message: string;\n /**\n * The status code for the response.\n *\n * @default 200\n */\n status?: number | string;\n /**\n * From which object will the `postMessage` method called from.\n *\n * @default 'window.opener'\n */\n target?: string;\n /**\n * Whether or not to call `window.close` after sending the message.\n *\n * @default true\n */\n close?: boolean;\n /**\n * In case `close` is set to `true`, this option is to specify how many milliseconds\n * should be waited before closing the window.\n *\n * @default 700\n */\n closeDelay?: number;\n};\n/**\n * The options to build a reponse for JSON.\n *\n * @group Services/ResponsesBuilder\n */\nexport type JSONResponseOptions = {\n /**\n * The response object generated by the application.\n */\n res: Response;\n /**\n * The actual data for the response. It will be added in a `data` property.\n */\n data: unknown;\n /**\n * The status code for the response.\n *\n * @default 200\n */\n status?: number | string;\n /**\n * Extra information to include inside the `metadata` object.\n */\n metadata?: object;\n};\n/**\n * The default time to wait before closing a window, in case it's enabled.\n */\nconst DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE = 700;\n/**\n * A utility service to build responses.\n *\n * @group Services\n * @group Services/ResponsesBuilder\n */\nexport class ResponsesBuilder {\n /**\n * The application configuration, to get the `version` and the `postMessagesPrefix`.\n */\n protected readonly _config: Config;\n /**\n * The uility service to get HTTP status codes.\n */\n protected readonly _statuses: Statuses;\n /**\n * @param options The options to construct the service.\n */\n constructor({ inject: { config, statuses } }: ResponsesBuilderConstructorOptions) {\n this._config = config;\n this._statuses = statuses;\n }\n /**\n * Generates and send an HTML response that emits a post message.\n * The post message will be prefixed with the value of the configuration setting\n * `postMessagesPrefix`.\n *\n * @param options The options to customize how the HTML is generated.\n */\n htmlPostMessage(options: HTMLPostMessageResponseOptions): void {\n const {\n res,\n title,\n message,\n status,\n target = 'window.opener',\n close = true,\n closeDelay = DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE,\n } = options;\n const prefix = this._config.get<string | undefined>('postMessagesPrefix') ?? '';\n const closeCode = close\n ? `setTimeout(function() { window.close(); }, ${closeDelay});`\n : '';\n\n const html = this._htmlTemplate(\n title,\n `\n (function() {\n if (${target}) {\n ${target}.postMessage('${prefix}${message}', '*');\n ${closeCode}\n }\n })();\n `,\n );\n\n const useStatus =\n typeof status === 'undefined'\n ? (this._statuses('ok') as number)\n : this._normalizeStatus(status);\n\n res.setHeader('Content-Type', 'text/html');\n res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store');\n res.status(useStatus);\n res.write(html);\n res.end();\n }\n /**\n * Generates and sends a JSON response.\n *\n * @param options The options to customize how the JSON is generated.\n * @example\n *\n * <caption>The generated looks like this.</caption>\n *\n * {\n * metadata: {\n * version: 'dev',\n * status: 200,\n * },\n * data: { foo: 'bar' },\n * }\n *\n */\n json(options: JSONResponseOptions): void {\n const { res, data, status, metadata = {} } = options;\n const useStatus =\n typeof status === 'undefined'\n ? (this._statuses('ok') as number)\n : this._normalizeStatus(status);\n\n res.status(useStatus);\n res.json({\n metadata: {\n version: this._config.get<string>('version'),\n status: useStatus,\n ...metadata,\n },\n data,\n });\n res.end();\n }\n /**\n * Generates a basic HTML template for the service to use when generating a post message\n * response.\n *\n * @param title The HTML `<title />` attribute.\n * @param code Javascript code to be wrapped on a `<script />` tag.\n */\n protected _htmlTemplate(title: string, code: string): string {\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"x-ua-compatible\" content=\"ie=edge\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <title>${title}</title>\n </head>\n <body>\n <script type=\"text/javascript\">\n ${code}\n </script>\n </body>\n </html>\n `;\n }\n /**\n * Utility method used to make sure a recevied status is a valid status code. If the\n * status is a string, the method will try to find the code from the `statuses` package.\n *\n * @param status The status to normalize.\n * @returns If `status` is a string, but there's no valid code, it will return 200.\n */\n protected _normalizeStatus(status: number | string): number {\n let useStatus: number;\n try {\n if (typeof status === 'string') {\n useStatus = this._statuses(status) as number;\n } else {\n this._statuses(status);\n useStatus = status;\n }\n } catch (_) {\n useStatus = this._statuses('ok') as number;\n }\n\n return useStatus;\n }\n}\n/**\n * The service provider that once registered on the container will set an instance of\n * {@link ResponsesBuilder} as the `responsesBuilder` service.\n *\n * @example\n *\n * // Register it on the container\n * container.register(responsesBuilderProvider);\n * // Getting access to the service instance\n * const responsesBuilder = container.get<ResponsesBuilder>('responsesBuilder');\n *\n * @group Providers\n * @group Services/ResponsesBuilder\n */\nexport const responsesBuilderProvider = provider((app) => {\n app.set(\n 'responsesBuilder',\n () =>\n new ResponsesBuilder({\n inject: {\n config: app.getConfig(),\n statuses: app.get('statuses'),\n },\n }),\n );\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAwC;AAuFxC,MAAM,uCAAuC;AAOtC,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA,EAY5B,YAAY,EAAE,QAAQ,EAAE,QAAQ,SAAS,EAAE,GAAuC;AARlF;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAmB;AAKjB,SAAK,UAAU;AACf,SAAK,YAAY;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,gBAAgB,SAA+C;AAC7D,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,SAAS;AAAA,MACT,QAAQ;AAAA,MACR,aAAa;AAAA,IACf,IAAI;AACJ,UAAM,SAAS,KAAK,QAAQ,IAAwB,oBAAoB,KAAK;AAC7E,UAAM,YAAY,QACd,8CAA8C,UAAU,OACxD;AAEJ,UAAM,OAAO,KAAK;AAAA,MAChB;AAAA,MACA;AAAA;AAAA,cAEQ,MAAM;AAAA,YACR,MAAM,iBAAiB,MAAM,GAAG,OAAO;AAAA,YACvC,SAAS;AAAA;AAAA;AAAA;AAAA,IAIjB;AAEA,UAAM,YACJ,OAAO,WAAW,cACb,KAAK,UAAU,IAAI,IACpB,KAAK,iBAAiB,MAAM;AAElC,QAAI,UAAU,gBAAgB,WAAW;AACzC,QAAI,UAAU,iBAAiB,gDAAgD;AAC/E,QAAI,OAAO,SAAS;AACpB,QAAI,MAAM,IAAI;AACd,QAAI,IAAI;AAAA,EACV;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBA,KAAK,SAAoC;AACvC,UAAM,EAAE,KAAK,MAAM,QAAQ,WAAW,CAAC,EAAE,IAAI;AAC7C,UAAM,YACJ,OAAO,WAAW,cACb,KAAK,UAAU,IAAI,IACpB,KAAK,iBAAiB,MAAM;AAElC,QAAI,OAAO,SAAS;AACpB,QAAI,KAAK;AAAA,MACP,UAAU;AAAA,QACR,SAAS,KAAK,QAAQ,IAAY,SAAS;AAAA,QAC3C,QAAQ;AAAA,QACR,GAAG;AAAA,MACL;AAAA,MACA;AAAA,IACF,CAAC;AACD,QAAI,IAAI;AAAA,EACV;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQU,cAAc,OAAe,MAAsB;AAC3D,WAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAOQ,KAAK;AAAA;AAAA;AAAA;AAAA,YAIZ,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA,EAKd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQU,iBAAiB,QAAiC;AAC1D,QAAI;AACJ,QAAI;AACF,UAAI,OAAO,WAAW,UAAU;AAC9B,oBAAY,KAAK,UAAU,MAAM;AAAA,MACnC,OAAO;AACL,aAAK,UAAU,MAAM;AACrB,oBAAY;AAAA,MACd;AAAA,IACF,SAAS,GAAG;AACV,kBAAY,KAAK,UAAU,IAAI;AAAA,IACjC;AAEA,WAAO;AAAA,EACT;AACF;AAeO,MAAM,+BAA2B,uBAAS,CAAC,QAAQ;AACxD,MAAI;AAAA,IACF;AAAA,IACA,MACE,IAAI,iBAAiB;AAAA,MACnB,QAAQ;AAAA,QACN,QAAQ,IAAI,UAAU;AAAA,QACtB,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,IACF,CAAC;AAAA,EACL;AACF,CAAC;","names":[]}
@@ -0,0 +1,33 @@
1
+ export { commonServicesProvider } from './common/index.mjs';
2
+ export { frontendServicesProvider } from './frontend/index.mjs';
3
+ export { htmlServicesProvider } from './html/index.mjs';
4
+ export { httpServicesProvider } from './http/index.mjs';
5
+ export { utilsServicesProvider } from './utils/index.mjs';
6
+ export { AppError, AppErrorClass, AppErrorContext, CreateAppErrorFn, appErrorProvider, createAppError } from './common/appError.mjs';
7
+ export { CreateHTTPErrorFn, HTTPError, HTTPErrorClass, createHTTPError, httpErrorProvider } from './common/httpError.mjs';
8
+ export { SendFile, SendFileGeneratorOptions, SendFileOptions, sendFile, sendFileProvider } from './common/sendFile.mjs';
9
+ export { FrontendFs, FrontendFsOptions, frontendFsProvider } from './frontend/frontendFs.mjs';
10
+ export { HTMLGenerator, HTMLGeneratorConstructorOptions, HTMLGeneratorOptions, HTMLGeneratorProviderOptions, HTMLGeneratorValuesService, htmlGeneratorProvider } from './html/htmlGenerator.mjs';
11
+ export { ErrorResponse } from '@homer0/api-utils';
12
+ export { APIClient, APIClientConfig, APIClientConstructorOptions, APIClientProviderOptions, APIClientSettings, EndpointsType, apiClientProvider } from './http/apiClient.mjs';
13
+ export { GetCustomHeadersFromRequestOptions, HTTP, HTTPContructorOptions, HTTPFetchBody, HTTPFetchOptions, HTTPOptions, httpProvider } from './http/http.mjs';
14
+ export { HTMLPostMessageResponseOptions, JSONResponseOptions, ResponsesBuilder, ResponsesBuilderConstructorOptions, responsesBuilderProvider } from './http/responsesBuilder.mjs';
15
+ export { EnsureBearerConstructorOptions, EnsureBearerToken, EnsureBearerTokenErrorOptions, EnsureBearerTokenOptions, EnsureBearerTokenProviderOptions, ensureBearerTokenProvider } from './utils/ensureBearerToken.mjs';
16
+ import '@homer0/jimple';
17
+ import '../index-Bwf7JHu9.mjs';
18
+ import '../types/express.mjs';
19
+ import 'express';
20
+ import '../types/http.mjs';
21
+ import 'https';
22
+ import 'http';
23
+ import 'spdy';
24
+ import 'node-fetch';
25
+ import '../types/utils.mjs';
26
+ import '@homer0/path-utils';
27
+ import '@homer0/simple-logger';
28
+ import '@homer0/simple-config';
29
+ import '@homer0/events-hub';
30
+ import '../utils/fns/statuses.mjs';
31
+ import 'statuses';
32
+ import 'fs/promises';
33
+ import '@homer0/deferred';
@@ -14,8 +14,7 @@ export { GetCustomHeadersFromRequestOptions, HTTP, HTTPContructorOptions, HTTPFe
14
14
  export { HTMLPostMessageResponseOptions, JSONResponseOptions, ResponsesBuilder, ResponsesBuilderConstructorOptions, responsesBuilderProvider } from './http/responsesBuilder.js';
15
15
  export { EnsureBearerConstructorOptions, EnsureBearerToken, EnsureBearerTokenErrorOptions, EnsureBearerTokenOptions, EnsureBearerTokenProviderOptions, ensureBearerTokenProvider } from './utils/ensureBearerToken.js';
16
16
  import '@homer0/jimple';
17
- import '../jimpex-7eaee271.js';
18
- import '@homer0/events-hub';
17
+ import '../index-C6I3NCC-.js';
19
18
  import '../types/express.js';
20
19
  import 'express';
21
20
  import '../types/http.js';
@@ -23,11 +22,12 @@ import 'https';
23
22
  import 'http';
24
23
  import 'spdy';
25
24
  import 'node-fetch';
26
- import '@homer0/simple-config';
27
25
  import '../types/utils.js';
26
+ import '@homer0/path-utils';
28
27
  import '@homer0/simple-logger';
28
+ import '@homer0/simple-config';
29
+ import '@homer0/events-hub';
29
30
  import '../utils/fns/statuses.js';
30
31
  import 'statuses';
31
- import '@homer0/path-utils';
32
32
  import 'fs/promises';
33
33
  import '@homer0/deferred';
@@ -20,4 +20,12 @@ __reExport(services_exports, require("./frontend"), module.exports);
20
20
  __reExport(services_exports, require("./html"), module.exports);
21
21
  __reExport(services_exports, require("./http"), module.exports);
22
22
  __reExport(services_exports, require("./utils"), module.exports);
23
+ // Annotate the CommonJS export names for ESM import in node:
24
+ 0 && (module.exports = {
25
+ ...require("./common"),
26
+ ...require("./frontend"),
27
+ ...require("./html"),
28
+ ...require("./http"),
29
+ ...require("./utils")
30
+ });
23
31
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,158 @@
1
+ import * as _homer0_jimple from '@homer0/jimple';
2
+ import { J as Jimpex } from '../../index-Bwf7JHu9.mjs';
3
+ import { ExpressMiddleware } from '../../types/express.mjs';
4
+ import { Statuses } from '../../utils/fns/statuses.mjs';
5
+ import { DeepPartial } from '../../types/utils.mjs';
6
+ import { HTTPErrorClass } from '../common/httpError.mjs';
7
+ import '../../types/http.mjs';
8
+ import 'https';
9
+ import 'http';
10
+ import 'spdy';
11
+ import 'node-fetch';
12
+ import 'express';
13
+ import '@homer0/path-utils';
14
+ import '@homer0/simple-logger';
15
+ import '@homer0/simple-config';
16
+ import '@homer0/events-hub';
17
+ import 'statuses';
18
+ import '../common/appError.mjs';
19
+
20
+ /**
21
+ * The options for the error the middleare can generate.
22
+ *
23
+ * @group Services/EnsureBearerToken
24
+ */
25
+ type EnsureBearerTokenErrorOptions = {
26
+ /**
27
+ * The error message for the response.
28
+ *
29
+ * @default 'Unauthorized'
30
+ */
31
+ message: string;
32
+ /**
33
+ * The HTTP status that will be added to the error context.
34
+ *
35
+ * @default 401
36
+ */
37
+ status: number;
38
+ /**
39
+ * Context information for the error handler and that can be added to the actual
40
+ * response.
41
+ */
42
+ response: unknown;
43
+ };
44
+ /**
45
+ * The options to customize the service/middleware.
46
+ *
47
+ * @group Services/EnsureBearerToken
48
+ */
49
+ type EnsureBearerTokenOptions = {
50
+ error: EnsureBearerTokenErrorOptions;
51
+ expression: RegExp;
52
+ local: string;
53
+ };
54
+ /**
55
+ * The options to construct a {@link EnsureBearerToken}.
56
+ *
57
+ * @group Services/EnsureBearerToken
58
+ */
59
+ type EnsureBearerConstructorOptions = DeepPartial<EnsureBearerTokenOptions> & {
60
+ /**
61
+ * A dictionary with the dependencies to inject.
62
+ */
63
+ inject: {
64
+ HTTPError: HTTPErrorClass;
65
+ statuses: Statuses;
66
+ };
67
+ };
68
+ /**
69
+ * Custom options for the provider that will register an instance of
70
+ * {@link EnsureBearerToken}
71
+ * as a service.
72
+ *
73
+ * @group Services/EnsureBearerToken
74
+ */
75
+ type EnsureBearerTokenProviderOptions = DeepPartial<EnsureBearerTokenOptions> & {
76
+ /**
77
+ * The name that will be used to register the service on the container. This is to allow
78
+ * multiple "instances" of the service to be created.
79
+ *
80
+ * @default 'ensureBearerToken'
81
+ */
82
+ serviceName?: string;
83
+ };
84
+ /**
85
+ * This service gives you a middleware that verifies if a request has an `Authorization`
86
+ * header with a bearer token; if it does, the token will be saved on the `res.locals`,
87
+ * otherwise, it will generate an error.
88
+ *
89
+ * This is a "service middleware" to allow certain flexibility: you can have controllers
90
+ * where some routes are protected and others are not. For those cases, you get the
91
+ * service from the container, and include it only in the routes that need it.
92
+ *
93
+ * @group Services
94
+ * @group Services/EnsureBearerToken
95
+ */
96
+ declare class EnsureBearerToken {
97
+ /**
98
+ * To generate the errors when the validation fails.
99
+ */
100
+ protected readonly _HTTPError: HTTPErrorClass;
101
+ /**
102
+ * The customization options for the service.
103
+ */
104
+ protected readonly _options: EnsureBearerTokenOptions;
105
+ /**
106
+ * @param options The options to construct the class.
107
+ */
108
+ constructor({ inject: { HTTPError, statuses }, ...options }: EnsureBearerConstructorOptions);
109
+ /**
110
+ * Generates the middleware that verifies if a request has an `Authorization` header
111
+ * with a bearer token.
112
+ */
113
+ getMiddleware(): ExpressMiddleware;
114
+ /**
115
+ * The customization options.
116
+ */
117
+ get options(): Readonly<EnsureBearerTokenOptions>;
118
+ }
119
+ /**
120
+ * Generates a "service middleware" that can be used on route controllers in order to
121
+ * validate the presence of a bearer token on the requests authorization header.
122
+ *
123
+ * The registered service is an instance of {@link EnsureBearerToken}, and it uses the key
124
+ * `ensureBearerToken`.
125
+ *
126
+ * Since it's a "provider creator", when registering it, you can pass custom options.
127
+ *
128
+ * @example
129
+ *
130
+ * <caption>Basic usage</caption>
131
+ *
132
+ * // Register it on the container
133
+ * container.register(ensureBearerTokenProvider);
134
+ *
135
+ * // Let's assume we are in a controller now...
136
+ * // Getting access to the middleware.
137
+ * const ensureBearerToken = container.get<ExpressMiddleware>('ensureBearerToken');
138
+ *
139
+ * @example
140
+ *
141
+ * <caption>Customizing the service</caption>
142
+ *
143
+ * // Register it on the container
144
+ * container.register(
145
+ * ensureBearerTokenProvider({
146
+ * serviceName: 'ensureBearerTokenCustom',
147
+ * error: {
148
+ * message: 'Missing token!',
149
+ * },
150
+ * }),
151
+ * );
152
+ *
153
+ * @group Providers
154
+ * @group Services/EnsureBearerToken
155
+ */
156
+ declare const ensureBearerTokenProvider: _homer0_jimple.ResourceCreator<"provider", "register", (options?: EnsureBearerTokenProviderOptions) => (app: Jimpex) => void, _homer0_jimple.ProviderRegisterFn<Jimpex>>;
157
+
158
+ export { type EnsureBearerConstructorOptions, EnsureBearerToken, type EnsureBearerTokenErrorOptions, type EnsureBearerTokenOptions, type EnsureBearerTokenProviderOptions, ensureBearerTokenProvider };
@@ -1,18 +1,19 @@
1
1
  import * as _homer0_jimple from '@homer0/jimple';
2
- import { J as Jimpex } from '../../jimpex-7eaee271.js';
2
+ import { J as Jimpex } from '../../index-C6I3NCC-.js';
3
3
  import { ExpressMiddleware } from '../../types/express.js';
4
4
  import { Statuses } from '../../utils/fns/statuses.js';
5
5
  import { DeepPartial } from '../../types/utils.js';
6
6
  import { HTTPErrorClass } from '../common/httpError.js';
7
- import '@homer0/events-hub';
8
- import 'express';
9
7
  import '../../types/http.js';
10
8
  import 'https';
11
9
  import 'http';
12
10
  import 'spdy';
13
11
  import 'node-fetch';
14
- import '@homer0/simple-config';
12
+ import 'express';
13
+ import '@homer0/path-utils';
15
14
  import '@homer0/simple-logger';
15
+ import '@homer0/simple-config';
16
+ import '@homer0/events-hub';
16
17
  import 'statuses';
17
18
  import '../common/appError.js';
18
19
 
@@ -154,4 +155,4 @@ declare class EnsureBearerToken {
154
155
  */
155
156
  declare const ensureBearerTokenProvider: _homer0_jimple.ResourceCreator<"provider", "register", (options?: EnsureBearerTokenProviderOptions) => (app: Jimpex) => void, _homer0_jimple.ProviderRegisterFn<Jimpex>>;
156
157
 
157
- export { EnsureBearerConstructorOptions, EnsureBearerToken, EnsureBearerTokenErrorOptions, EnsureBearerTokenOptions, EnsureBearerTokenProviderOptions, ensureBearerTokenProvider };
158
+ export { type EnsureBearerConstructorOptions, EnsureBearerToken, type EnsureBearerTokenErrorOptions, type EnsureBearerTokenOptions, type EnsureBearerTokenProviderOptions, ensureBearerTokenProvider };
@@ -30,11 +30,20 @@ module.exports = __toCommonJS(ensureBearerToken_exports);
30
30
  var import_deep_assign = require("@homer0/deep-assign");
31
31
  var import_utils = require("../../utils");
32
32
  class EnsureBearerToken {
33
+ /**
34
+ * @param options The options to construct the class.
35
+ */
33
36
  constructor({
34
37
  inject: { HTTPError, statuses },
35
38
  ...options
36
39
  }) {
40
+ /**
41
+ * To generate the errors when the validation fails.
42
+ */
37
43
  __publicField(this, "_HTTPError");
44
+ /**
45
+ * The customization options for the service.
46
+ */
38
47
  __publicField(this, "_options");
39
48
  this._HTTPError = HTTPError;
40
49
  this._options = (0, import_deep_assign.deepAssignWithOverwrite)(
@@ -50,6 +59,10 @@ class EnsureBearerToken {
50
59
  options
51
60
  );
52
61
  }
62
+ /**
63
+ * Generates the middleware that verifies if a request has an `Authorization` header
64
+ * with a bearer token.
65
+ */
53
66
  getMiddleware() {
54
67
  return (req, res, next) => {
55
68
  let unauthorized = true;
@@ -78,6 +91,9 @@ class EnsureBearerToken {
78
91
  }
79
92
  };
80
93
  }
94
+ /**
95
+ * The customization options.
96
+ */
81
97
  get options() {
82
98
  return (0, import_deep_assign.deepAssignWithOverwrite)({}, this._options);
83
99
  }