jimpex 7.0.1 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/LICENSE +1 -1
- package/README.md +446 -434
- package/dist/app/index.d.ts +13 -0
- package/dist/app/index.js +19 -0
- package/dist/app/index.js.map +1 -0
- package/dist/app/jimpex.d.ts +13 -0
- package/dist/app/jimpex.js +440 -0
- package/dist/app/jimpex.js.map +1 -0
- package/dist/controllers/common/config.d.ts +78 -0
- package/dist/controllers/common/config.js +89 -0
- package/dist/controllers/common/config.js.map +1 -0
- package/dist/controllers/common/health.d.ts +82 -0
- package/dist/controllers/common/health.js +97 -0
- package/dist/controllers/common/health.js.map +1 -0
- package/dist/controllers/common/index.d.ts +21 -0
- package/dist/controllers/common/index.js +21 -0
- package/dist/controllers/common/index.js.map +1 -0
- package/dist/controllers/common/statics.d.ts +215 -0
- package/dist/controllers/common/statics.js +202 -0
- package/dist/controllers/common/statics.js.map +1 -0
- package/dist/controllers/index.d.ts +24 -0
- package/dist/controllers/index.js +20 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/controllers/utils/gateway.d.ts +724 -0
- package/dist/controllers/utils/gateway.js +425 -0
- package/dist/controllers/utils/gateway.js.map +1 -0
- package/dist/controllers/utils/index.d.ts +16 -0
- package/dist/controllers/utils/index.js +19 -0
- package/dist/controllers/utils/index.js.map +1 -0
- package/dist/esm/app/index.js +2 -0
- package/dist/esm/app/index.js.map +1 -0
- package/dist/esm/app/jimpex.js +415 -0
- package/dist/esm/app/jimpex.js.map +1 -0
- package/dist/esm/chunk-T2T6Q22Z.js +11 -0
- package/dist/esm/chunk-T2T6Q22Z.js.map +1 -0
- package/dist/esm/controllers/common/config.js +62 -0
- package/dist/esm/controllers/common/config.js.map +1 -0
- package/dist/esm/controllers/common/health.js +70 -0
- package/dist/esm/controllers/common/health.js.map +1 -0
- package/dist/esm/controllers/common/index.js +4 -0
- package/dist/esm/controllers/common/index.js.map +1 -0
- package/dist/esm/controllers/common/statics.js +173 -0
- package/dist/esm/controllers/common/statics.js.map +1 -0
- package/dist/esm/controllers/index.js +3 -0
- package/dist/esm/controllers/index.js.map +1 -0
- package/dist/esm/controllers/utils/gateway.js +404 -0
- package/dist/esm/controllers/utils/gateway.js.map +1 -0
- package/dist/esm/controllers/utils/index.js +2 -0
- package/dist/esm/controllers/utils/index.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/middlewares/common/errorHandler.js +92 -0
- package/dist/esm/middlewares/common/errorHandler.js.map +1 -0
- package/dist/esm/middlewares/common/forceHTTPS.js +41 -0
- package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -0
- package/dist/esm/middlewares/common/hsts.js +53 -0
- package/dist/esm/middlewares/common/hsts.js.map +1 -0
- package/dist/esm/middlewares/common/index.js +4 -0
- package/dist/esm/middlewares/common/index.js.map +1 -0
- package/dist/esm/middlewares/html/fastHTML.js +104 -0
- package/dist/esm/middlewares/html/fastHTML.js.map +1 -0
- package/dist/esm/middlewares/html/index.js +3 -0
- package/dist/esm/middlewares/html/index.js.map +1 -0
- package/dist/esm/middlewares/html/showHTML.js +68 -0
- package/dist/esm/middlewares/html/showHTML.js.map +1 -0
- package/dist/esm/middlewares/index.js +4 -0
- package/dist/esm/middlewares/index.js.map +1 -0
- package/dist/esm/middlewares/utils/index.js +2 -0
- package/dist/esm/middlewares/utils/index.js.map +1 -0
- package/dist/esm/middlewares/utils/versionValidator.js +101 -0
- package/dist/esm/middlewares/utils/versionValidator.js.map +1 -0
- package/dist/esm/services/common/appError.js +52 -0
- package/dist/esm/services/common/appError.js.map +1 -0
- package/dist/esm/services/common/httpError.js +19 -0
- package/dist/esm/services/common/httpError.js.map +1 -0
- package/dist/esm/services/common/index.js +17 -0
- package/dist/esm/services/common/index.js.map +1 -0
- package/dist/esm/services/common/sendFile.js +27 -0
- package/dist/esm/services/common/sendFile.js.map +1 -0
- package/dist/esm/services/frontend/frontendFs.js +38 -0
- package/dist/esm/services/frontend/frontendFs.js.map +1 -0
- package/dist/esm/services/frontend/index.js +11 -0
- package/dist/esm/services/frontend/index.js.map +1 -0
- package/dist/esm/services/html/htmlGenerator.js +144 -0
- package/dist/esm/services/html/htmlGenerator.js.map +1 -0
- package/dist/esm/services/html/index.js +11 -0
- package/dist/esm/services/html/index.js.map +1 -0
- package/dist/esm/services/http/apiClient.js +71 -0
- package/dist/esm/services/http/apiClient.js.map +1 -0
- package/dist/esm/services/http/http.js +125 -0
- package/dist/esm/services/http/http.js.map +1 -0
- package/dist/esm/services/http/index.js +17 -0
- package/dist/esm/services/http/index.js.map +1 -0
- package/dist/esm/services/http/responsesBuilder.js +105 -0
- package/dist/esm/services/http/responsesBuilder.js.map +1 -0
- package/dist/esm/services/index.js +6 -0
- package/dist/esm/services/index.js.map +1 -0
- package/dist/esm/services/utils/ensureBearerToken.js +78 -0
- package/dist/esm/services/utils/ensureBearerToken.js.map +1 -0
- package/dist/esm/services/utils/index.js +11 -0
- package/dist/esm/services/utils/index.js.map +1 -0
- package/dist/esm/types/events.js +1 -0
- package/dist/esm/types/events.js.map +1 -0
- package/dist/esm/types/express.js +1 -0
- package/dist/esm/types/express.js.map +1 -0
- package/dist/esm/types/http.js +1 -0
- package/dist/esm/types/http.js.map +1 -0
- package/dist/esm/types/index.js +7 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/options.js.map +1 -0
- package/dist/esm/types/utils.js +1 -0
- package/dist/esm/types/utils.js.map +1 -0
- package/dist/esm/types/wootils.js +1 -0
- package/dist/esm/types/wootils.js.map +1 -0
- package/dist/esm/utils/fns/index.js +5 -0
- package/dist/esm/utils/fns/index.js.map +1 -0
- package/dist/esm/utils/fns/others.js +6 -0
- package/dist/esm/utils/fns/others.js.map +1 -0
- package/dist/esm/utils/fns/routes.js +25 -0
- package/dist/esm/utils/fns/routes.js.map +1 -0
- package/dist/esm/utils/fns/statuses.js +6 -0
- package/dist/esm/utils/fns/statuses.js.map +1 -0
- package/dist/esm/utils/fns/text.js +6 -0
- package/dist/esm/utils/fns/text.js.map +1 -0
- package/dist/esm/utils/index.js +3 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/wrappers.js +41 -0
- package/dist/esm/utils/wrappers.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/jimpex-7eaee271.d.ts +1278 -0
- package/dist/middlewares/common/errorHandler.d.ts +131 -0
- package/dist/middlewares/common/errorHandler.js +119 -0
- package/dist/middlewares/common/errorHandler.js.map +1 -0
- package/dist/middlewares/common/forceHTTPS.d.ts +68 -0
- package/dist/middlewares/common/forceHTTPS.js +68 -0
- package/dist/middlewares/common/forceHTTPS.js.map +1 -0
- package/dist/middlewares/common/hsts.d.ts +109 -0
- package/dist/middlewares/common/hsts.js +80 -0
- package/dist/middlewares/common/hsts.js.map +1 -0
- package/dist/middlewares/common/index.d.ts +21 -0
- package/dist/middlewares/common/index.js +21 -0
- package/dist/middlewares/common/index.js.map +1 -0
- package/dist/middlewares/html/fastHTML.d.ts +180 -0
- package/dist/middlewares/html/fastHTML.js +131 -0
- package/dist/middlewares/html/fastHTML.js.map +1 -0
- package/dist/middlewares/html/index.d.ts +21 -0
- package/dist/middlewares/html/index.js +20 -0
- package/dist/middlewares/html/index.js.map +1 -0
- package/dist/middlewares/html/showHTML.d.ts +127 -0
- package/dist/middlewares/html/showHTML.js +95 -0
- package/dist/middlewares/html/showHTML.js.map +1 -0
- package/dist/middlewares/index.d.ts +30 -0
- package/dist/middlewares/index.js +21 -0
- package/dist/middlewares/index.js.map +1 -0
- package/dist/middlewares/utils/index.d.ts +19 -0
- package/dist/middlewares/utils/index.js +19 -0
- package/dist/middlewares/utils/index.js.map +1 -0
- package/dist/middlewares/utils/versionValidator.d.ts +214 -0
- package/dist/middlewares/utils/versionValidator.js +128 -0
- package/dist/middlewares/utils/versionValidator.js.map +1 -0
- package/dist/services/common/appError.d.ts +138 -0
- package/dist/services/common/appError.js +80 -0
- package/dist/services/common/appError.js.map +1 -0
- package/dist/services/common/httpError.d.ts +79 -0
- package/dist/services/common/httpError.js +44 -0
- package/dist/services/common/httpError.js.map +1 -0
- package/dist/services/common/index.d.ts +47 -0
- package/dist/services/common/index.js +41 -0
- package/dist/services/common/index.js.map +1 -0
- package/dist/services/common/sendFile.d.ts +102 -0
- package/dist/services/common/sendFile.js +51 -0
- package/dist/services/common/sendFile.js.map +1 -0
- package/dist/services/frontend/frontendFs.d.ts +96 -0
- package/dist/services/frontend/frontendFs.js +71 -0
- package/dist/services/frontend/frontendFs.js.map +1 -0
- package/dist/services/frontend/index.d.ts +40 -0
- package/dist/services/frontend/index.js +35 -0
- package/dist/services/frontend/index.js.map +1 -0
- package/dist/services/html/htmlGenerator.d.ts +237 -0
- package/dist/services/html/htmlGenerator.js +171 -0
- package/dist/services/html/htmlGenerator.js.map +1 -0
- package/dist/services/html/index.d.ts +43 -0
- package/dist/services/html/index.js +35 -0
- package/dist/services/html/index.js.map +1 -0
- package/dist/services/http/apiClient.d.ts +169 -0
- package/dist/services/http/apiClient.js +96 -0
- package/dist/services/http/apiClient.js.map +1 -0
- package/dist/services/http/http.d.ts +175 -0
- package/dist/services/http/http.js +158 -0
- package/dist/services/http/http.js.map +1 -0
- package/dist/services/http/index.d.ts +50 -0
- package/dist/services/http/index.js +41 -0
- package/dist/services/http/index.js.map +1 -0
- package/dist/services/http/responsesBuilder.d.ts +178 -0
- package/dist/services/http/responsesBuilder.js +132 -0
- package/dist/services/http/responsesBuilder.js.map +1 -0
- package/dist/services/index.d.ts +33 -0
- package/dist/services/index.js +23 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/utils/ensureBearerToken.d.ts +157 -0
- package/dist/services/utils/ensureBearerToken.js +105 -0
- package/dist/services/utils/ensureBearerToken.js.map +1 -0
- package/dist/services/utils/index.d.ts +43 -0
- package/dist/services/utils/index.js +35 -0
- package/dist/services/utils/index.js.map +1 -0
- package/dist/types/events.d.ts +13 -0
- package/dist/types/events.js +17 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/express.d.ts +10 -0
- package/dist/types/express.js +17 -0
- package/dist/types/express.js.map +1 -0
- package/dist/types/http.d.ts +79 -0
- package/dist/types/http.js +17 -0
- package/dist/types/http.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.js +24 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/options.d.ts +13 -0
- package/dist/types/options.js +17 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/utils.d.ts +48 -0
- package/dist/types/utils.js +17 -0
- package/dist/types/utils.js.map +1 -0
- package/dist/types/wootils.d.ts +4 -0
- package/dist/types/wootils.js +17 -0
- package/dist/types/wootils.js.map +1 -0
- package/dist/utils/fns/index.d.ts +5 -0
- package/dist/utils/fns/index.js +22 -0
- package/dist/utils/fns/index.js.map +1 -0
- package/dist/utils/fns/others.d.ts +17 -0
- package/dist/utils/fns/others.js +29 -0
- package/dist/utils/fns/others.js.map +1 -0
- package/dist/utils/fns/routes.d.ts +39 -0
- package/dist/utils/fns/routes.js +51 -0
- package/dist/utils/fns/routes.js.map +1 -0
- package/dist/utils/fns/statuses.d.ts +45 -0
- package/dist/utils/fns/statuses.js +35 -0
- package/dist/utils/fns/statuses.js.map +1 -0
- package/dist/utils/fns/text.d.ts +9 -0
- package/dist/utils/fns/text.js +29 -0
- package/dist/utils/fns/text.js.map +1 -0
- package/dist/utils/index.d.ts +18 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/wrappers.d.ts +13 -0
- package/dist/utils/wrappers.js +68 -0
- package/dist/utils/wrappers.js.map +1 -0
- package/examples/basic/404.html +12 -0
- package/examples/basic/README.md +30 -0
- package/examples/basic/app.ts +18 -0
- package/examples/basic/controller.ts +45 -0
- package/examples/basic/index.ts +19 -0
- package/examples/basic/middleware.ts +13 -0
- package/examples/basic/service.ts +11 -0
- package/package.json +59 -38
- package/src/app/index.ts +1 -0
- package/src/app/jimpex.ts +743 -0
- package/src/controllers/common/config.ts +115 -0
- package/src/controllers/common/health.ts +128 -0
- package/src/controllers/common/index.ts +3 -0
- package/src/controllers/common/statics.ts +380 -0
- package/src/controllers/index.ts +2 -0
- package/src/controllers/utils/gateway.ts +1186 -0
- package/src/controllers/utils/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/middlewares/common/errorHandler.ts +203 -0
- package/src/middlewares/common/forceHTTPS.ts +83 -0
- package/src/middlewares/common/hsts.ts +135 -0
- package/src/middlewares/common/index.ts +3 -0
- package/src/middlewares/html/fastHTML.ts +255 -0
- package/src/middlewares/html/index.ts +2 -0
- package/src/middlewares/html/showHTML.ts +165 -0
- package/src/middlewares/index.ts +3 -0
- package/src/middlewares/utils/index.ts +1 -0
- package/src/middlewares/utils/versionValidator.ts +289 -0
- package/src/services/common/appError.ts +158 -0
- package/src/services/common/httpError.ts +74 -0
- package/src/services/common/index.ts +29 -0
- package/src/services/common/sendFile.ts +106 -0
- package/src/services/frontend/frontendFs.ts +101 -0
- package/src/services/frontend/index.ts +21 -0
- package/src/services/html/htmlGenerator.ts +356 -0
- package/src/services/html/index.ts +21 -0
- package/src/services/http/apiClient.ts +221 -0
- package/src/services/http/http.ts +286 -0
- package/src/services/http/index.ts +29 -0
- package/src/services/http/responsesBuilder.ts +265 -0
- package/src/services/index.ts +5 -0
- package/src/services/utils/ensureBearerToken.ts +202 -0
- package/src/services/utils/index.ts +21 -0
- package/src/types/events.ts +303 -0
- package/src/types/express.ts +21 -0
- package/src/types/http.ts +77 -0
- package/src/types/index.ts +6 -0
- package/src/types/options.ts +248 -0
- package/src/types/utils.ts +52 -0
- package/src/types/wootils.ts +4 -0
- package/src/utils/fns/index.ts +4 -0
- package/src/utils/fns/others.ts +15 -0
- package/src/utils/fns/routes.ts +64 -0
- package/src/utils/fns/statuses.ts +44 -0
- package/src/utils/fns/text.ts +8 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/wrappers.ts +537 -0
- package/tsconfig.json +5 -8
- package/tsup.config.ts +10 -0
- package/src/app/index.js +0 -692
- package/src/constants/eventNames.js +0 -48
- package/src/constants/index.js +0 -7
- package/src/controllers/common/configuration.js +0 -116
- package/src/controllers/common/health.js +0 -79
- package/src/controllers/common/index.js +0 -7
- package/src/controllers/common/statics.js +0 -336
- package/src/controllers/index.js +0 -9
- package/src/controllers/utils/gateway.js +0 -1039
- package/src/controllers/utils/index.js +0 -3
- package/src/index.js +0 -30
- package/src/middlewares/common/errorHandler.js +0 -185
- package/src/middlewares/common/forceHTTPS.js +0 -80
- package/src/middlewares/common/hsts.js +0 -122
- package/src/middlewares/common/index.js +0 -7
- package/src/middlewares/html/fastHTML.js +0 -298
- package/src/middlewares/html/index.js +0 -5
- package/src/middlewares/html/showHTML.js +0 -167
- package/src/middlewares/index.js +0 -11
- package/src/middlewares/utils/index.js +0 -3
- package/src/middlewares/utils/versionValidator.js +0 -261
- package/src/services/common/appError.js +0 -136
- package/src/services/common/httpError.js +0 -60
- package/src/services/common/index.js +0 -25
- package/src/services/common/sendFile.js +0 -68
- package/src/services/frontend/frontendFs.js +0 -85
- package/src/services/frontend/index.js +0 -17
- package/src/services/html/htmlGenerator.js +0 -391
- package/src/services/html/index.js +0 -17
- package/src/services/http/apiClient.js +0 -148
- package/src/services/http/http.js +0 -256
- package/src/services/http/index.js +0 -25
- package/src/services/http/responsesBuilder.js +0 -193
- package/src/services/index.js +0 -15
- package/src/services/utils/ensureBearerToken.js +0 -147
- package/src/services/utils/index.js +0 -19
- package/src/types.js +0 -377
- package/src/utils/functions.js +0 -78
- package/src/utils/wrappers.js +0 -131
- package/types/app/index.d.ts +0 -417
- package/types/constants/eventNames.d.ts +0 -93
- package/types/constants/index.d.ts +0 -2
- package/types/controllers/common/configuration.d.ts +0 -71
- package/types/controllers/common/health.d.ts +0 -55
- package/types/controllers/common/index.d.ts +0 -4
- package/types/controllers/common/statics.d.ts +0 -271
- package/types/controllers/index.d.ts +0 -3
- package/types/controllers/utils/gateway.d.ts +0 -947
- package/types/controllers/utils/index.d.ts +0 -2
- package/types/index.d.ts +0 -15
- package/types/middlewares/common/errorHandler.d.ts +0 -143
- package/types/middlewares/common/forceHTTPS.d.ts +0 -64
- package/types/middlewares/common/hsts.d.ts +0 -111
- package/types/middlewares/common/index.d.ts +0 -4
- package/types/middlewares/html/fastHTML.d.ts +0 -238
- package/types/middlewares/html/index.d.ts +0 -3
- package/types/middlewares/html/showHTML.d.ts +0 -128
- package/types/middlewares/index.d.ts +0 -4
- package/types/middlewares/utils/index.d.ts +0 -2
- package/types/middlewares/utils/versionValidator.d.ts +0 -247
- package/types/services/common/appError.d.ts +0 -89
- package/types/services/common/httpError.d.ts +0 -37
- package/types/services/common/index.d.ts +0 -18
- package/types/services/common/sendFile.d.ts +0 -56
- package/types/services/frontend/frontendFs.d.ts +0 -72
- package/types/services/frontend/index.d.ts +0 -3
- package/types/services/html/htmlGenerator.d.ts +0 -298
- package/types/services/html/index.d.ts +0 -3
- package/types/services/http/apiClient.d.ts +0 -141
- package/types/services/http/http.d.ts +0 -159
- package/types/services/http/index.d.ts +0 -18
- package/types/services/http/responsesBuilder.d.ts +0 -140
- package/types/services/index.d.ts +0 -6
- package/types/services/utils/ensureBearerToken.d.ts +0 -137
- package/types/services/utils/index.d.ts +0 -16
- package/types/types.d.ts +0 -280
- package/types/utils/functions.d.ts +0 -55
- package/types/utils/wrappers.d.ts +0 -127
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { middlewareCreator } from '../../utils';
|
|
2
|
+
import type { HTMLGenerator, SendFile } from '../../services';
|
|
3
|
+
import type { AsyncExpressMiddleware, Response, NextFunction } from '../../types';
|
|
4
|
+
/**
|
|
5
|
+
* The options to customize the behavior of the middleware.
|
|
6
|
+
*
|
|
7
|
+
* @group Middlewares/ShowHTML
|
|
8
|
+
*/
|
|
9
|
+
export type ShowHTMLOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* The name of the file the middleware will serve. If the {@link HTMLGenerator} service
|
|
12
|
+
* is available, it will be overriden by the service.
|
|
13
|
+
*
|
|
14
|
+
* @default 'index.html'
|
|
15
|
+
*/
|
|
16
|
+
file: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The options to construct a {@link ShowHTML}.
|
|
20
|
+
*
|
|
21
|
+
* @group Middlewares/ShowHTML
|
|
22
|
+
*/
|
|
23
|
+
export type ShowHTMLConstructorOptions = Partial<ShowHTMLOptions> & {
|
|
24
|
+
/**
|
|
25
|
+
* A dictionary with the dependencies to inject.
|
|
26
|
+
*/
|
|
27
|
+
inject: {
|
|
28
|
+
sendFile: SendFile;
|
|
29
|
+
/**
|
|
30
|
+
* A function to get a possible {@link HTMLGenerator}. This is injected as a "getter"
|
|
31
|
+
* to not interrupt the DIC "lifecycle": middlewares are initialized right when the
|
|
32
|
+
* app starts, and injecting a reference would force the service to be initialized
|
|
33
|
+
* too, even if a request is not being made.
|
|
34
|
+
*/
|
|
35
|
+
getHTMLGenerator?: () => HTMLGenerator | undefined;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The options for the middleware creator that will mount an instance of {@link ShowHTML}.
|
|
40
|
+
*
|
|
41
|
+
* @group Middlewares/ShowHTML
|
|
42
|
+
*/
|
|
43
|
+
export type ShowHTMLMiddlewareOptions = Partial<ShowHTMLOptions> & {
|
|
44
|
+
/**
|
|
45
|
+
* The name of an {@link HTMLGenerator} service already available in the application.
|
|
46
|
+
*
|
|
47
|
+
* @default 'htmlGenerator'
|
|
48
|
+
*/
|
|
49
|
+
htmlGeneratorServiceName?: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A very simple middleware service to send an HTML on a server response. The special
|
|
54
|
+
* _'feature'_ of this service is that it can be hooked up to an {@link HTMLGenerator}
|
|
55
|
+
* service and it will automatically server the file generated by it.
|
|
56
|
+
*
|
|
57
|
+
* @group Middleware Classes
|
|
58
|
+
* @group Middlewares/ShowHTML
|
|
59
|
+
* @prettierignore
|
|
60
|
+
*/
|
|
61
|
+
export class ShowHTML {
|
|
62
|
+
/**
|
|
63
|
+
* The service that serves a file.
|
|
64
|
+
*/
|
|
65
|
+
protected readonly _sendFile: SendFile;
|
|
66
|
+
/**
|
|
67
|
+
* A function to get a possible {@link HTMLGenerator}. This is injected as a "getter"
|
|
68
|
+
* to not interrupt the DIC "lifecycle": middlewares are initialized right when the
|
|
69
|
+
* app starts, and injecting a reference would force the service to be initialized
|
|
70
|
+
* too, even if a request is not being made.
|
|
71
|
+
*/
|
|
72
|
+
protected readonly _getHTMLGenerator: () => HTMLGenerator | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The customization options for the middleware.
|
|
75
|
+
*/
|
|
76
|
+
protected _options: ShowHTMLOptions;
|
|
77
|
+
/**
|
|
78
|
+
* Whether or not the file is ready to be served. In case the middleware uses an
|
|
79
|
+
* {@link HTMLGenerator} service, the file needs to be generated before being available,
|
|
80
|
+
* and that's why this flag exists.
|
|
81
|
+
*/
|
|
82
|
+
protected _fileReady: boolean = false;
|
|
83
|
+
/**
|
|
84
|
+
* @param options The options to construct the class.
|
|
85
|
+
*/
|
|
86
|
+
constructor({ inject, ...options }: ShowHTMLConstructorOptions) {
|
|
87
|
+
this._sendFile = inject.sendFile;
|
|
88
|
+
this._getHTMLGenerator = inject.getHTMLGenerator || (() => undefined);
|
|
89
|
+
this._options = {
|
|
90
|
+
file: 'index.html',
|
|
91
|
+
...options,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Generates the middleware that serves the HTML file.
|
|
96
|
+
*/
|
|
97
|
+
getMiddleware(): AsyncExpressMiddleware {
|
|
98
|
+
return async (_, res, next) => {
|
|
99
|
+
// If the file is ready to be served, serve it.
|
|
100
|
+
if (this._fileReady) {
|
|
101
|
+
this._sendResponse(res, next);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const htmlGenerator = this._getHTMLGenerator();
|
|
106
|
+
// If there's no generator, switch the flag and just serve the file.
|
|
107
|
+
if (!htmlGenerator) {
|
|
108
|
+
this._fileReady = true;
|
|
109
|
+
this._sendResponse(res, next);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
// Wait for the HTML to be generated.
|
|
115
|
+
await htmlGenerator.whenReady();
|
|
116
|
+
// Update the local option.
|
|
117
|
+
this._options.file = htmlGenerator.options.file;
|
|
118
|
+
// Switch the flag and serve the file.
|
|
119
|
+
this._fileReady = true;
|
|
120
|
+
this._sendResponse(res, next);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
next(error);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The customization options.
|
|
128
|
+
*/
|
|
129
|
+
get options(): Readonly<ShowHTMLOptions> {
|
|
130
|
+
return { ...this._options };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Serves the HTML file to the response.
|
|
134
|
+
*
|
|
135
|
+
* @param res The response object generated by the application.
|
|
136
|
+
* @param next The function to call the next middleware.
|
|
137
|
+
*/
|
|
138
|
+
protected _sendResponse(res: Response, next: NextFunction): void {
|
|
139
|
+
res.setHeader('Content-Type', 'text/html');
|
|
140
|
+
this._sendFile({
|
|
141
|
+
res,
|
|
142
|
+
next,
|
|
143
|
+
filepath: this._options.file,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Creates the middleware that serves an HTML file in the response.
|
|
149
|
+
*
|
|
150
|
+
* @group Middlewares
|
|
151
|
+
* @group Middlewares/ShowHTML
|
|
152
|
+
*/
|
|
153
|
+
export const showHTMLMiddleware = middlewareCreator(
|
|
154
|
+
(options: ShowHTMLMiddlewareOptions = {}) =>
|
|
155
|
+
(app) => {
|
|
156
|
+
const { htmlGeneratorServiceName = 'htmlGenerator', ...rest } = options;
|
|
157
|
+
return new ShowHTML({
|
|
158
|
+
inject: {
|
|
159
|
+
sendFile: app.get('sendFile'),
|
|
160
|
+
getHTMLGenerator: () => app.try(htmlGeneratorServiceName),
|
|
161
|
+
},
|
|
162
|
+
...rest,
|
|
163
|
+
}).getMiddleware();
|
|
164
|
+
},
|
|
165
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './versionValidator';
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { deepAssignWithOverwrite } from '@homer0/deep-assign';
|
|
2
|
+
import { middlewareCreator, type Statuses } from '../../utils';
|
|
3
|
+
import { DeepPartial, Request, ExpressMiddleware } from '../../types';
|
|
4
|
+
import type { HTTPErrorClass, ResponsesBuilder } from '../../services';
|
|
5
|
+
/**
|
|
6
|
+
* The options for how the middleware should behave if the requested version is `latest`.
|
|
7
|
+
*
|
|
8
|
+
* @group Middlewares/VersionValidator
|
|
9
|
+
*/
|
|
10
|
+
export type VersionValidatorLatestOptions = {
|
|
11
|
+
/**
|
|
12
|
+
* Whether or not the middleware should validate the _"latest version"_.
|
|
13
|
+
*
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
allow: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The name of the _"latest version"_. Basically, `req.params.version` must match with
|
|
19
|
+
* this property in order to be consider "latest".
|
|
20
|
+
*
|
|
21
|
+
* @default 'latest'
|
|
22
|
+
*/
|
|
23
|
+
name: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The options for how to detect if the request comes from a popup and how to compose the
|
|
27
|
+
* post message the middleware will use to respond.
|
|
28
|
+
*
|
|
29
|
+
* @group Middlewares/VersionValidator
|
|
30
|
+
*/
|
|
31
|
+
export type VersionValidatorPopupOptions = {
|
|
32
|
+
/**
|
|
33
|
+
* The name of the query string variable the middleware will check in order to indentify
|
|
34
|
+
* whether the request comes from a popup or not. The variable must have `'true'` as its
|
|
35
|
+
* value.
|
|
36
|
+
*
|
|
37
|
+
* @default 'popup'
|
|
38
|
+
*/
|
|
39
|
+
variable: string;
|
|
40
|
+
/**
|
|
41
|
+
* The title of the page that will be generated to respond in case the versions don't
|
|
42
|
+
* match.
|
|
43
|
+
*
|
|
44
|
+
* @default 'Conflict'
|
|
45
|
+
*/
|
|
46
|
+
title: string;
|
|
47
|
+
/**
|
|
48
|
+
* The contents of the post message the generated page will send if the versions don't
|
|
49
|
+
* match.
|
|
50
|
+
*
|
|
51
|
+
* @default 'version:conflict'
|
|
52
|
+
*/
|
|
53
|
+
message: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* The options used to customize a {@link VersionValidator} instance.
|
|
57
|
+
*
|
|
58
|
+
* @group Middlewares/VersionValidator
|
|
59
|
+
*/
|
|
60
|
+
export type VersionValidatorOptions = {
|
|
61
|
+
/**
|
|
62
|
+
* The version used to validate the requests.
|
|
63
|
+
*/
|
|
64
|
+
version: string;
|
|
65
|
+
/**
|
|
66
|
+
* The options for how the middleware should behave if the requested version is
|
|
67
|
+
* `latest`.
|
|
68
|
+
*/
|
|
69
|
+
latest: VersionValidatorLatestOptions;
|
|
70
|
+
/**
|
|
71
|
+
* The options for how to detect if the request comes from a popup and how to compose
|
|
72
|
+
* the post message the middleware will use to respond.
|
|
73
|
+
*/
|
|
74
|
+
popup: VersionValidatorPopupOptions;
|
|
75
|
+
/**
|
|
76
|
+
* The error message to show when the version is invalid.
|
|
77
|
+
*
|
|
78
|
+
* @default "The application version doesn't match"
|
|
79
|
+
* @prettierignore
|
|
80
|
+
*/
|
|
81
|
+
error: string;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* A partial version of the {@link VersionValidatorOptions}, to be used in the constructor
|
|
85
|
+
* and the middleware creator. The reason it omits `version` it's because for the
|
|
86
|
+
* constructor it's required, but for the middleware creator it's not.
|
|
87
|
+
*
|
|
88
|
+
* @group Middlewares/VersionValidator
|
|
89
|
+
*/
|
|
90
|
+
export type VersionValidatorPartialOptions = DeepPartial<
|
|
91
|
+
Omit<VersionValidatorOptions, 'version'>
|
|
92
|
+
>;
|
|
93
|
+
/**
|
|
94
|
+
* The options to construct a {@link VersionValidator}.
|
|
95
|
+
*
|
|
96
|
+
* @group Middlewares/VersionValidator
|
|
97
|
+
*/
|
|
98
|
+
export type VersionValidatorConstructorOptions = VersionValidatorPartialOptions & {
|
|
99
|
+
/**
|
|
100
|
+
* The version used to validate the requests.
|
|
101
|
+
*/
|
|
102
|
+
version: string;
|
|
103
|
+
/**
|
|
104
|
+
* A dictionary with the dependencies to inject.
|
|
105
|
+
*/
|
|
106
|
+
inject: {
|
|
107
|
+
HTTPError: HTTPErrorClass;
|
|
108
|
+
responsesBuilder: ResponsesBuilder;
|
|
109
|
+
statuses: Statuses;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* The options for the middleware creator that will mount an instance of
|
|
114
|
+
* {@link VersionValidator}.
|
|
115
|
+
*
|
|
116
|
+
* @group Middlewares/VersionValidator
|
|
117
|
+
*/
|
|
118
|
+
export type VersionValidatorMiddlewareOptions = VersionValidatorPartialOptions & {
|
|
119
|
+
/**
|
|
120
|
+
* The version used to validate the requests. This is optional in here because if it's
|
|
121
|
+
* not specified, it will be obtained from the configuration service.
|
|
122
|
+
*/
|
|
123
|
+
version?: string;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* This is the handler for the middleware/controller that validates the application
|
|
127
|
+
* version.
|
|
128
|
+
* This is useful in cases where you want to restrict the access to specific versions; for
|
|
129
|
+
* example: you have a frontend which needs to be aligned with the "current" version of
|
|
130
|
+
* the application, since the frontend won't realize a new version was released, the
|
|
131
|
+
* validator can be used to let the frontend know.
|
|
132
|
+
* Also, it can be configured to handle requests from popups, in which case, instead of
|
|
133
|
+
* generating an error message, it will send a post message.
|
|
134
|
+
*
|
|
135
|
+
* @group Middleware Classes
|
|
136
|
+
* @group Middlewares/VersionValidator
|
|
137
|
+
* @prettierignore
|
|
138
|
+
*/
|
|
139
|
+
export class VersionValidator {
|
|
140
|
+
/**
|
|
141
|
+
* To generate the errors in case the validation fails.
|
|
142
|
+
*/
|
|
143
|
+
protected readonly _HTTPError: HTTPErrorClass;
|
|
144
|
+
/**
|
|
145
|
+
* To generate responses for popups.
|
|
146
|
+
*/
|
|
147
|
+
protected readonly _responsesBuilder: ResponsesBuilder;
|
|
148
|
+
/**
|
|
149
|
+
* The uility service to get HTTP status codes.
|
|
150
|
+
*/
|
|
151
|
+
protected readonly _statuses: Statuses;
|
|
152
|
+
/**
|
|
153
|
+
* The customization options.
|
|
154
|
+
*/
|
|
155
|
+
protected readonly _options: VersionValidatorOptions;
|
|
156
|
+
/**
|
|
157
|
+
* @param options The options to construct the class.
|
|
158
|
+
* @throws If no `version` is specified in the options.
|
|
159
|
+
*/
|
|
160
|
+
constructor({ inject, version, ...options }: VersionValidatorConstructorOptions) {
|
|
161
|
+
this._HTTPError = inject.HTTPError;
|
|
162
|
+
this._responsesBuilder = inject.responsesBuilder;
|
|
163
|
+
this._statuses = inject.statuses;
|
|
164
|
+
this._options = deepAssignWithOverwrite(
|
|
165
|
+
{
|
|
166
|
+
error: "The application version doesn't match",
|
|
167
|
+
latest: {
|
|
168
|
+
allow: true,
|
|
169
|
+
name: 'latest',
|
|
170
|
+
},
|
|
171
|
+
popup: {
|
|
172
|
+
variable: 'popup',
|
|
173
|
+
title: 'Conflict',
|
|
174
|
+
message: 'vesion:conflict',
|
|
175
|
+
},
|
|
176
|
+
version,
|
|
177
|
+
},
|
|
178
|
+
options,
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
if (!this._options.version) {
|
|
182
|
+
throw new Error('You need to supply a version');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Generates the middleware that validates the version.
|
|
187
|
+
*/
|
|
188
|
+
getMiddleware(): ExpressMiddleware {
|
|
189
|
+
return (req, res, next) => {
|
|
190
|
+
// Get the `version` parameter from the request.
|
|
191
|
+
const { version } = req.params;
|
|
192
|
+
// If no version is present, move on to the next middleware.
|
|
193
|
+
if (!version) {
|
|
194
|
+
next();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
// If the version matches, or it's a "latest" version, move on to the next middleware.
|
|
198
|
+
if (version === this._options.version || this._isTheAllowedLatest(version)) {
|
|
199
|
+
next();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const status = this._statuses('conflict');
|
|
204
|
+
// If the request comes from a popup, send the post message.
|
|
205
|
+
if (this._isPopup(req)) {
|
|
206
|
+
const { title, message } = this._options.popup;
|
|
207
|
+
this._responsesBuilder.htmlPostMessage({
|
|
208
|
+
res,
|
|
209
|
+
title,
|
|
210
|
+
message,
|
|
211
|
+
status,
|
|
212
|
+
});
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Every other validation failed, and it's not a popup, so generate an error.
|
|
217
|
+
next(
|
|
218
|
+
new this._HTTPError(this._options.error, status, {
|
|
219
|
+
response: {
|
|
220
|
+
validation: true,
|
|
221
|
+
},
|
|
222
|
+
}),
|
|
223
|
+
);
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The customization options.
|
|
228
|
+
*/
|
|
229
|
+
get options(): Readonly<VersionValidatorOptions> {
|
|
230
|
+
return deepAssignWithOverwrite({}, this._options);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Helper method that checks if the incoming request is from a popup. It will look for
|
|
234
|
+
* the query string variable defined in the constructor options.
|
|
235
|
+
*
|
|
236
|
+
* @param req The request object sent by the application.
|
|
237
|
+
*/
|
|
238
|
+
protected _isPopup(req: Request): boolean {
|
|
239
|
+
const popup = req.query[this._options.popup.variable];
|
|
240
|
+
return !!(popup && String(popup).toLowerCase() === 'true');
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Helper method that checks if the "latest version" is enabled and if the given version
|
|
244
|
+
* is "the latest" (comparing it with the option name).
|
|
245
|
+
*
|
|
246
|
+
* @param version The version received in the request.
|
|
247
|
+
*/
|
|
248
|
+
protected _isTheAllowedLatest(version: string): boolean {
|
|
249
|
+
const { allow, name } = this._options.latest;
|
|
250
|
+
return allow && version === name;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* A middleware that will validate a `version` request parameter against the application
|
|
255
|
+
* version, and generate an error if they don't match.
|
|
256
|
+
* This is a "middleware/controller" because the wrappers for both are the same, the
|
|
257
|
+
* difference is that, for controllers, Jimpex sends a second parameter with the route
|
|
258
|
+
* where they are mounted.
|
|
259
|
+
* By validating the route parameter, the function can know whether the implementation is
|
|
260
|
+
* going to use the middleware by itself or as a route middleware.
|
|
261
|
+
* If used as middleware, it will just return the result of
|
|
262
|
+
* {@link VersionValidator.getMiddleware}; but if used as controller, it will mount it on
|
|
263
|
+
* `[route]/:version/*`.
|
|
264
|
+
*
|
|
265
|
+
* @group Middlewares
|
|
266
|
+
* @group Middlewares/VersionValidator
|
|
267
|
+
*/
|
|
268
|
+
export const versionValidatorMiddleware = middlewareCreator(
|
|
269
|
+
(options: VersionValidatorMiddlewareOptions = {}) =>
|
|
270
|
+
(app, route) => {
|
|
271
|
+
const version = app.getConfig<string>('version');
|
|
272
|
+
const middleware = new VersionValidator({
|
|
273
|
+
inject: {
|
|
274
|
+
HTTPError: app.get('HTTPError'),
|
|
275
|
+
responsesBuilder: app.get('responsesBuilder'),
|
|
276
|
+
statuses: app.get('statuses'),
|
|
277
|
+
},
|
|
278
|
+
version,
|
|
279
|
+
...options,
|
|
280
|
+
}).getMiddleware();
|
|
281
|
+
|
|
282
|
+
if (route) {
|
|
283
|
+
const router = app.getRouter();
|
|
284
|
+
return router.all('/:version/*', middleware);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return middleware;
|
|
288
|
+
},
|
|
289
|
+
);
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { provider, statuses as statusesFn, type Statuses } from '../../utils';
|
|
2
|
+
/**
|
|
3
|
+
* A dictionary with some context information that can be provided to {@link AppError}.
|
|
4
|
+
*
|
|
5
|
+
* @group Services/AppError
|
|
6
|
+
*/
|
|
7
|
+
export type AppErrorContext = {
|
|
8
|
+
/**
|
|
9
|
+
* Extra information that the error handler can pick and include the response.
|
|
10
|
+
*/
|
|
11
|
+
response?: unknown;
|
|
12
|
+
/**
|
|
13
|
+
* A status code or name for the error handler to set in the response.
|
|
14
|
+
*/
|
|
15
|
+
status?: string | number;
|
|
16
|
+
} & Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* A simple subclass of `Error` but with support for context information.
|
|
19
|
+
*
|
|
20
|
+
* @group Services
|
|
21
|
+
* @group Services/AppError
|
|
22
|
+
*/
|
|
23
|
+
export class AppError extends Error {
|
|
24
|
+
/**
|
|
25
|
+
* The date of when the error was generated.
|
|
26
|
+
*/
|
|
27
|
+
readonly _date: Date;
|
|
28
|
+
/**
|
|
29
|
+
* The context information that can be provided to the error, and picked by the error
|
|
30
|
+
* handler.
|
|
31
|
+
*/
|
|
32
|
+
readonly _context: AppErrorContext;
|
|
33
|
+
/**
|
|
34
|
+
* The service that generates HTTP status codes.
|
|
35
|
+
*/
|
|
36
|
+
protected _statuses: Statuses;
|
|
37
|
+
/**
|
|
38
|
+
* @param message The message of the error.
|
|
39
|
+
* @param context The context information, for the error handler.
|
|
40
|
+
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
41
|
+
* is in case the implementation wants to use a special version from
|
|
42
|
+
* the container; otherwise, it will use the `statuses` library
|
|
43
|
+
* directly.
|
|
44
|
+
*/
|
|
45
|
+
constructor(
|
|
46
|
+
message: string,
|
|
47
|
+
context: AppErrorContext = {},
|
|
48
|
+
statuses: Statuses = statusesFn,
|
|
49
|
+
) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.name = this.constructor.name;
|
|
52
|
+
this._date = new Date();
|
|
53
|
+
this._statuses = statuses;
|
|
54
|
+
this._context = this._parseContext(context);
|
|
55
|
+
|
|
56
|
+
// Limit the stack trace if possible.
|
|
57
|
+
if (Error.captureStackTrace) {
|
|
58
|
+
Error.captureStackTrace(this, this.constructor);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Gets an object that can be included in a response from the application. This method
|
|
63
|
+
* is a helper for the `response` getter, as it allows for the assertion of the response
|
|
64
|
+
* type.
|
|
65
|
+
*
|
|
66
|
+
* @template T The type of the response.
|
|
67
|
+
*/
|
|
68
|
+
getResponse<T>(): T {
|
|
69
|
+
return this.response as T;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Information about the error that can be included in a response. This is set using the
|
|
73
|
+
* `response` key in the `context` option.
|
|
74
|
+
*/
|
|
75
|
+
get response(): unknown {
|
|
76
|
+
return this._context.response || {};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* An HTTP status code related to the error. This is set using the `status` key on the
|
|
80
|
+
* `context`.
|
|
81
|
+
*/
|
|
82
|
+
get status(): number | undefined {
|
|
83
|
+
return this._context.status as number | undefined;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Context information related to the error.
|
|
87
|
+
*/
|
|
88
|
+
get context(): unknown {
|
|
89
|
+
return this._context;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The date of when the error was generated.
|
|
93
|
+
*/
|
|
94
|
+
get date(): Date {
|
|
95
|
+
return this._date;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Utility method that formats the context before saving it in the instance:
|
|
99
|
+
* - If the context includes a `status` as a `string`, it will try to replace it with
|
|
100
|
+
* its actual status code from the `statuses `service.
|
|
101
|
+
*
|
|
102
|
+
* @param context The original context sent to the constructor.
|
|
103
|
+
*/
|
|
104
|
+
protected _parseContext(context: AppErrorContext): AppErrorContext {
|
|
105
|
+
const result = { ...context };
|
|
106
|
+
if (result.status && typeof result.status === 'string') {
|
|
107
|
+
result.status = this._statuses.code[result.status.toLowerCase()] || result.status;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Shorthand for `new AppError()`.
|
|
115
|
+
*
|
|
116
|
+
* @param args The same parameters as the {@link AppError} constructor.
|
|
117
|
+
* @returns A new instance of {@link AppError}.
|
|
118
|
+
* @group Services
|
|
119
|
+
* @group Services/AppError
|
|
120
|
+
*/
|
|
121
|
+
export const createAppError = (
|
|
122
|
+
...args: ConstructorParameters<typeof AppError>
|
|
123
|
+
): AppError => new AppError(...args);
|
|
124
|
+
/**
|
|
125
|
+
* The type of the function that generates a new instance of {@link AppError}.
|
|
126
|
+
* This is exported to make it easy to type the dependency injection.
|
|
127
|
+
*
|
|
128
|
+
* @group Services/AppError
|
|
129
|
+
*/
|
|
130
|
+
export type CreateAppErrorFn = typeof createAppError;
|
|
131
|
+
/**
|
|
132
|
+
* THe type of the {@link AppError} class.
|
|
133
|
+
* This is exported to make it easy to type the dependency injection.
|
|
134
|
+
*
|
|
135
|
+
* @group Services/AppError
|
|
136
|
+
*/
|
|
137
|
+
export type AppErrorClass = typeof AppError;
|
|
138
|
+
/**
|
|
139
|
+
* A service provider that will register both, {@link AppError} and
|
|
140
|
+
* {@link createAppError}, on the container. `AppError` will be the key for class, and
|
|
141
|
+
* `appError` will be for the generator function.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
*
|
|
145
|
+
* // Register it on the container
|
|
146
|
+
* container.register(appErrorProvider);
|
|
147
|
+
* // Getting access to the class.
|
|
148
|
+
* const AppError = container.get<AppErrorClass>('AppError');
|
|
149
|
+
* // Getting access to the function.
|
|
150
|
+
* const appError = container.get<CreateAppErrorFn>('appError');
|
|
151
|
+
*
|
|
152
|
+
* @group Providers
|
|
153
|
+
* @group Services/AppError
|
|
154
|
+
*/
|
|
155
|
+
export const appErrorProvider = provider((app) => {
|
|
156
|
+
app.set('AppError', () => AppError);
|
|
157
|
+
app.set('appError', () => createAppError);
|
|
158
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { provider, statuses as statusesFn, type Statuses } from '../../utils';
|
|
2
|
+
import { AppError, type AppErrorContext } from './appError';
|
|
3
|
+
/**
|
|
4
|
+
* A type of error to be used on HTTP requests. This is the most common type of error used
|
|
5
|
+
* by Jimpex.
|
|
6
|
+
*
|
|
7
|
+
* @group Services
|
|
8
|
+
* @group Services/HTTPError
|
|
9
|
+
*/
|
|
10
|
+
export class HTTPError extends AppError {
|
|
11
|
+
/**
|
|
12
|
+
* @param message The error message.
|
|
13
|
+
* @param status The HTTP status code.
|
|
14
|
+
* @param context Context information related to the error.
|
|
15
|
+
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
16
|
+
* is in case the implementation wants to use a special version from
|
|
17
|
+
* the container; otherwise, it will use the `statuses` library
|
|
18
|
+
* directly.
|
|
19
|
+
*/
|
|
20
|
+
constructor(
|
|
21
|
+
message: string,
|
|
22
|
+
status: number | string = statusesFn('ok'),
|
|
23
|
+
context: AppErrorContext = {},
|
|
24
|
+
statuses: Statuses = statusesFn,
|
|
25
|
+
) {
|
|
26
|
+
super(message, { ...context, status }, statuses);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Shorthand for `new HTTPError()`.
|
|
31
|
+
*
|
|
32
|
+
* @param args The same parameters as the {@link HTTPError} constructor.
|
|
33
|
+
* @returns A new instance of {@link HTTPError}.
|
|
34
|
+
* @group Services
|
|
35
|
+
* @group Services/HTTPError
|
|
36
|
+
*/
|
|
37
|
+
export const createHTTPError = (
|
|
38
|
+
...args: ConstructorParameters<typeof HTTPError>
|
|
39
|
+
): HTTPError => new HTTPError(...args);
|
|
40
|
+
/**
|
|
41
|
+
* The type of the function that generates a new instance of {@link HTTPError}.
|
|
42
|
+
* This is exported to make it easy to type the dependency injection.
|
|
43
|
+
*
|
|
44
|
+
* @group Services/HTTPError
|
|
45
|
+
*/
|
|
46
|
+
export type CreateHTTPErrorFn = typeof createHTTPError;
|
|
47
|
+
/**
|
|
48
|
+
* THe type of the {@link HTTPError} class.
|
|
49
|
+
* This is exported to make it easy to type the dependency injection.
|
|
50
|
+
*
|
|
51
|
+
* @group Services/HTTPError
|
|
52
|
+
*/
|
|
53
|
+
export type HTTPErrorClass = typeof HTTPError;
|
|
54
|
+
/**
|
|
55
|
+
* A service provider that will register both the {@link HTTPError} and a generator
|
|
56
|
+
* function on the container. `HTTPError` will be the key for class, and `httpError` will
|
|
57
|
+
* be for the generator function.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
*
|
|
61
|
+
* // Register it on the container
|
|
62
|
+
* container.register(httpErrorProvider);
|
|
63
|
+
* // Getting access to the class.
|
|
64
|
+
* const HTTPError = container.get<HTTPErrorClass>('HTTPError');
|
|
65
|
+
* // Getting access to the function.
|
|
66
|
+
* const httpError = container.get<CreateHTTPErrorFn>('httpError');
|
|
67
|
+
*
|
|
68
|
+
* @group Providers
|
|
69
|
+
* @group Services/HTTPError
|
|
70
|
+
*/
|
|
71
|
+
export const httpErrorProvider = provider((app) => {
|
|
72
|
+
app.set('HTTPError', () => HTTPError);
|
|
73
|
+
app.set('httpError', () => createHTTPError);
|
|
74
|
+
});
|