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,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import { deepAssignWithOverwrite } from "@homer0/deep-assign";
|
|
5
|
+
import { middlewareCreator } from "../../utils";
|
|
6
|
+
class VersionValidator {
|
|
7
|
+
constructor({ inject, version, ...options }) {
|
|
8
|
+
__publicField(this, "_HTTPError");
|
|
9
|
+
__publicField(this, "_responsesBuilder");
|
|
10
|
+
__publicField(this, "_statuses");
|
|
11
|
+
__publicField(this, "_options");
|
|
12
|
+
this._HTTPError = inject.HTTPError;
|
|
13
|
+
this._responsesBuilder = inject.responsesBuilder;
|
|
14
|
+
this._statuses = inject.statuses;
|
|
15
|
+
this._options = deepAssignWithOverwrite(
|
|
16
|
+
{
|
|
17
|
+
error: "The application version doesn't match",
|
|
18
|
+
latest: {
|
|
19
|
+
allow: true,
|
|
20
|
+
name: "latest"
|
|
21
|
+
},
|
|
22
|
+
popup: {
|
|
23
|
+
variable: "popup",
|
|
24
|
+
title: "Conflict",
|
|
25
|
+
message: "vesion:conflict"
|
|
26
|
+
},
|
|
27
|
+
version
|
|
28
|
+
},
|
|
29
|
+
options
|
|
30
|
+
);
|
|
31
|
+
if (!this._options.version) {
|
|
32
|
+
throw new Error("You need to supply a version");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
getMiddleware() {
|
|
36
|
+
return (req, res, next) => {
|
|
37
|
+
const { version } = req.params;
|
|
38
|
+
if (!version) {
|
|
39
|
+
next();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (version === this._options.version || this._isTheAllowedLatest(version)) {
|
|
43
|
+
next();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const status = this._statuses("conflict");
|
|
47
|
+
if (this._isPopup(req)) {
|
|
48
|
+
const { title, message } = this._options.popup;
|
|
49
|
+
this._responsesBuilder.htmlPostMessage({
|
|
50
|
+
res,
|
|
51
|
+
title,
|
|
52
|
+
message,
|
|
53
|
+
status
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
next(
|
|
58
|
+
new this._HTTPError(this._options.error, status, {
|
|
59
|
+
response: {
|
|
60
|
+
validation: true
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
get options() {
|
|
67
|
+
return deepAssignWithOverwrite({}, this._options);
|
|
68
|
+
}
|
|
69
|
+
_isPopup(req) {
|
|
70
|
+
const popup = req.query[this._options.popup.variable];
|
|
71
|
+
return !!(popup && String(popup).toLowerCase() === "true");
|
|
72
|
+
}
|
|
73
|
+
_isTheAllowedLatest(version) {
|
|
74
|
+
const { allow, name } = this._options.latest;
|
|
75
|
+
return allow && version === name;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const versionValidatorMiddleware = middlewareCreator(
|
|
79
|
+
(options = {}) => (app, route) => {
|
|
80
|
+
const version = app.getConfig("version");
|
|
81
|
+
const middleware = new VersionValidator({
|
|
82
|
+
inject: {
|
|
83
|
+
HTTPError: app.get("HTTPError"),
|
|
84
|
+
responsesBuilder: app.get("responsesBuilder"),
|
|
85
|
+
statuses: app.get("statuses")
|
|
86
|
+
},
|
|
87
|
+
version,
|
|
88
|
+
...options
|
|
89
|
+
}).getMiddleware();
|
|
90
|
+
if (route) {
|
|
91
|
+
const router = app.getRouter();
|
|
92
|
+
return router.all("/:version/*", middleware);
|
|
93
|
+
}
|
|
94
|
+
return middleware;
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
export {
|
|
98
|
+
VersionValidator,
|
|
99
|
+
versionValidatorMiddleware
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=versionValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/middlewares/utils/versionValidator.ts"],"sourcesContent":["import { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { middlewareCreator, type Statuses } from '../../utils';\nimport { DeepPartial, Request, ExpressMiddleware } from '../../types';\nimport type { HTTPErrorClass, ResponsesBuilder } from '../../services';\n/**\n * The options for how the middleware should behave if the requested version is `latest`.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorLatestOptions = {\n /**\n * Whether or not the middleware should validate the _\"latest version\"_.\n *\n * @default true\n */\n allow: boolean;\n /**\n * The name of the _\"latest version\"_. Basically, `req.params.version` must match with\n * this property in order to be consider \"latest\".\n *\n * @default 'latest'\n */\n name: string;\n};\n/**\n * The options for how to detect if the request comes from a popup and how to compose the\n * post message the middleware will use to respond.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorPopupOptions = {\n /**\n * The name of the query string variable the middleware will check in order to indentify\n * whether the request comes from a popup or not. The variable must have `'true'` as its\n * value.\n *\n * @default 'popup'\n */\n variable: string;\n /**\n * The title of the page that will be generated to respond in case the versions don't\n * match.\n *\n * @default 'Conflict'\n */\n title: string;\n /**\n * The contents of the post message the generated page will send if the versions don't\n * match.\n *\n * @default 'version:conflict'\n */\n message: string;\n};\n/**\n * The options used to customize a {@link VersionValidator} instance.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorOptions = {\n /**\n * The version used to validate the requests.\n */\n version: string;\n /**\n * The options for how the middleware should behave if the requested version is\n * `latest`.\n */\n latest: VersionValidatorLatestOptions;\n /**\n * The options for how to detect if the request comes from a popup and how to compose\n * the post message the middleware will use to respond.\n */\n popup: VersionValidatorPopupOptions;\n /**\n * The error message to show when the version is invalid.\n *\n * @default \"The application version doesn't match\"\n * @prettierignore\n */\n error: string;\n};\n/**\n * A partial version of the {@link VersionValidatorOptions}, to be used in the constructor\n * and the middleware creator. The reason it omits `version` it's because for the\n * constructor it's required, but for the middleware creator it's not.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorPartialOptions = DeepPartial<\n Omit<VersionValidatorOptions, 'version'>\n>;\n/**\n * The options to construct a {@link VersionValidator}.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorConstructorOptions = VersionValidatorPartialOptions & {\n /**\n * The version used to validate the requests.\n */\n version: string;\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n HTTPError: HTTPErrorClass;\n responsesBuilder: ResponsesBuilder;\n statuses: Statuses;\n };\n};\n/**\n * The options for the middleware creator that will mount an instance of\n * {@link VersionValidator}.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorMiddlewareOptions = VersionValidatorPartialOptions & {\n /**\n * The version used to validate the requests. This is optional in here because if it's\n * not specified, it will be obtained from the configuration service.\n */\n version?: string;\n};\n/**\n * This is the handler for the middleware/controller that validates the application\n * version.\n * This is useful in cases where you want to restrict the access to specific versions; for\n * example: you have a frontend which needs to be aligned with the \"current\" version of\n * the application, since the frontend won't realize a new version was released, the\n * validator can be used to let the frontend know.\n * Also, it can be configured to handle requests from popups, in which case, instead of\n * generating an error message, it will send a post message.\n *\n * @group Middleware Classes\n * @group Middlewares/VersionValidator\n * @prettierignore\n */\nexport class VersionValidator {\n /**\n * To generate the errors in case the validation fails.\n */\n protected readonly _HTTPError: HTTPErrorClass;\n /**\n * To generate responses for popups.\n */\n protected readonly _responsesBuilder: ResponsesBuilder;\n /**\n * The uility service to get HTTP status codes.\n */\n protected readonly _statuses: Statuses;\n /**\n * The customization options.\n */\n protected readonly _options: VersionValidatorOptions;\n /**\n * @param options The options to construct the class.\n * @throws If no `version` is specified in the options.\n */\n constructor({ inject, version, ...options }: VersionValidatorConstructorOptions) {\n this._HTTPError = inject.HTTPError;\n this._responsesBuilder = inject.responsesBuilder;\n this._statuses = inject.statuses;\n this._options = deepAssignWithOverwrite(\n {\n error: \"The application version doesn't match\",\n latest: {\n allow: true,\n name: 'latest',\n },\n popup: {\n variable: 'popup',\n title: 'Conflict',\n message: 'vesion:conflict',\n },\n version,\n },\n options,\n );\n\n if (!this._options.version) {\n throw new Error('You need to supply a version');\n }\n }\n /**\n * Generates the middleware that validates the version.\n */\n getMiddleware(): ExpressMiddleware {\n return (req, res, next) => {\n // Get the `version` parameter from the request.\n const { version } = req.params;\n // If no version is present, move on to the next middleware.\n if (!version) {\n next();\n return;\n }\n // If the version matches, or it's a \"latest\" version, move on to the next middleware.\n if (version === this._options.version || this._isTheAllowedLatest(version)) {\n next();\n return;\n }\n\n const status = this._statuses('conflict');\n // If the request comes from a popup, send the post message.\n if (this._isPopup(req)) {\n const { title, message } = this._options.popup;\n this._responsesBuilder.htmlPostMessage({\n res,\n title,\n message,\n status,\n });\n return;\n }\n\n // Every other validation failed, and it's not a popup, so generate an error.\n next(\n new this._HTTPError(this._options.error, status, {\n response: {\n validation: true,\n },\n }),\n );\n };\n }\n /**\n * The customization options.\n */\n get options(): Readonly<VersionValidatorOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Helper method that checks if the incoming request is from a popup. It will look for\n * the query string variable defined in the constructor options.\n *\n * @param req The request object sent by the application.\n */\n protected _isPopup(req: Request): boolean {\n const popup = req.query[this._options.popup.variable];\n return !!(popup && String(popup).toLowerCase() === 'true');\n }\n /**\n * Helper method that checks if the \"latest version\" is enabled and if the given version\n * is \"the latest\" (comparing it with the option name).\n *\n * @param version The version received in the request.\n */\n protected _isTheAllowedLatest(version: string): boolean {\n const { allow, name } = this._options.latest;\n return allow && version === name;\n }\n}\n/**\n * A middleware that will validate a `version` request parameter against the application\n * version, and generate an error if they don't match.\n * This is a \"middleware/controller\" because the wrappers for both are the same, the\n * difference is that, for controllers, Jimpex sends a second parameter with the route\n * where they are mounted.\n * By validating the route parameter, the function can know whether the implementation is\n * going to use the middleware by itself or as a route middleware.\n * If used as middleware, it will just return the result of\n * {@link VersionValidator.getMiddleware}; but if used as controller, it will mount it on\n * `[route]/:version/*`.\n *\n * @group Middlewares\n * @group Middlewares/VersionValidator\n */\nexport const versionValidatorMiddleware = middlewareCreator(\n (options: VersionValidatorMiddlewareOptions = {}) =>\n (app, route) => {\n const version = app.getConfig<string>('version');\n const middleware = new VersionValidator({\n inject: {\n HTTPError: app.get('HTTPError'),\n responsesBuilder: app.get('responsesBuilder'),\n statuses: app.get('statuses'),\n },\n version,\n ...options,\n }).getMiddleware();\n\n if (route) {\n const router = app.getRouter();\n return router.all('/:version/*', middleware);\n }\n\n return middleware;\n },\n);\n"],"mappings":";;;AAAA,SAAS,+BAA+B;AACxC,SAAS,yBAAwC;AAyI1C,MAAM,iBAAiB;AAAA,EAqB5B,YAAY,EAAE,QAAQ,YAAY,QAAQ,GAAuC;AAjBjF,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAMjB,SAAK,aAAa,OAAO;AACzB,SAAK,oBAAoB,OAAO;AAChC,SAAK,YAAY,OAAO;AACxB,SAAK,WAAW;AAAA,MACd;AAAA,QACE,OAAO;AAAA,QACP,QAAQ;AAAA,UACN,OAAO;AAAA,UACP,MAAM;AAAA,QACR;AAAA,QACA,OAAO;AAAA,UACL,UAAU;AAAA,UACV,OAAO;AAAA,UACP,SAAS;AAAA,QACX;AAAA,QACA;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAEA,QAAI,CAAC,KAAK,SAAS,SAAS;AAC1B,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AAAA,EACF;AAAA,EAIA,gBAAmC;AACjC,WAAO,CAAC,KAAK,KAAK,SAAS;AAEzB,YAAM,EAAE,QAAQ,IAAI,IAAI;AAExB,UAAI,CAAC,SAAS;AACZ,aAAK;AACL;AAAA,MACF;AAEA,UAAI,YAAY,KAAK,SAAS,WAAW,KAAK,oBAAoB,OAAO,GAAG;AAC1E,aAAK;AACL;AAAA,MACF;AAEA,YAAM,SAAS,KAAK,UAAU,UAAU;AAExC,UAAI,KAAK,SAAS,GAAG,GAAG;AACtB,cAAM,EAAE,OAAO,QAAQ,IAAI,KAAK,SAAS;AACzC,aAAK,kBAAkB,gBAAgB;AAAA,UACrC;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF,CAAC;AACD;AAAA,MACF;AAGA;AAAA,QACE,IAAI,KAAK,WAAW,KAAK,SAAS,OAAO,QAAQ;AAAA,UAC/C,UAAU;AAAA,YACR,YAAY;AAAA,UACd;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA,EAIA,IAAI,UAA6C;AAC/C,WAAO,wBAAwB,CAAC,GAAG,KAAK,QAAQ;AAAA,EAClD;AAAA,EAOU,SAAS,KAAuB;AACxC,UAAM,QAAQ,IAAI,MAAM,KAAK,SAAS,MAAM;AAC5C,WAAO,CAAC,EAAE,SAAS,OAAO,KAAK,EAAE,YAAY,MAAM;AAAA,EACrD;AAAA,EAOU,oBAAoB,SAA0B;AACtD,UAAM,EAAE,OAAO,KAAK,IAAI,KAAK,SAAS;AACtC,WAAO,SAAS,YAAY;AAAA,EAC9B;AACF;AAgBO,MAAM,6BAA6B;AAAA,EACxC,CAAC,UAA6C,CAAC,MAC7C,CAAC,KAAK,UAAU;AACd,UAAM,UAAU,IAAI,UAAkB,SAAS;AAC/C,UAAM,aAAa,IAAI,iBAAiB;AAAA,MACtC,QAAQ;AAAA,QACN,WAAW,IAAI,IAAI,WAAW;AAAA,QAC9B,kBAAkB,IAAI,IAAI,kBAAkB;AAAA,QAC5C,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC,EAAE,cAAc;AAEjB,QAAI,OAAO;AACT,YAAM,SAAS,IAAI,UAAU;AAC7B,aAAO,OAAO,IAAI,eAAe,UAAU;AAAA,IAC7C;AAEA,WAAO;AAAA,EACT;AACJ;","names":[]}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import { provider, statuses as statusesFn } from "../../utils";
|
|
5
|
+
class AppError extends Error {
|
|
6
|
+
constructor(message, context = {}, statuses = statusesFn) {
|
|
7
|
+
super(message);
|
|
8
|
+
__publicField(this, "_date");
|
|
9
|
+
__publicField(this, "_context");
|
|
10
|
+
__publicField(this, "_statuses");
|
|
11
|
+
this.name = this.constructor.name;
|
|
12
|
+
this._date = new Date();
|
|
13
|
+
this._statuses = statuses;
|
|
14
|
+
this._context = this._parseContext(context);
|
|
15
|
+
if (Error.captureStackTrace) {
|
|
16
|
+
Error.captureStackTrace(this, this.constructor);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
getResponse() {
|
|
20
|
+
return this.response;
|
|
21
|
+
}
|
|
22
|
+
get response() {
|
|
23
|
+
return this._context.response || {};
|
|
24
|
+
}
|
|
25
|
+
get status() {
|
|
26
|
+
return this._context.status;
|
|
27
|
+
}
|
|
28
|
+
get context() {
|
|
29
|
+
return this._context;
|
|
30
|
+
}
|
|
31
|
+
get date() {
|
|
32
|
+
return this._date;
|
|
33
|
+
}
|
|
34
|
+
_parseContext(context) {
|
|
35
|
+
const result = { ...context };
|
|
36
|
+
if (result.status && typeof result.status === "string") {
|
|
37
|
+
result.status = this._statuses.code[result.status.toLowerCase()] || result.status;
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const createAppError = (...args) => new AppError(...args);
|
|
43
|
+
const appErrorProvider = provider((app) => {
|
|
44
|
+
app.set("AppError", () => AppError);
|
|
45
|
+
app.set("appError", () => createAppError);
|
|
46
|
+
});
|
|
47
|
+
export {
|
|
48
|
+
AppError,
|
|
49
|
+
appErrorProvider,
|
|
50
|
+
createAppError
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=appError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/common/appError.ts"],"sourcesContent":["import { provider, statuses as statusesFn, type Statuses } from '../../utils';\n/**\n * A dictionary with some context information that can be provided to {@link AppError}.\n *\n * @group Services/AppError\n */\nexport type AppErrorContext = {\n /**\n * Extra information that the error handler can pick and include the response.\n */\n response?: unknown;\n /**\n * A status code or name for the error handler to set in the response.\n */\n status?: string | number;\n} & Record<string, unknown>;\n/**\n * A simple subclass of `Error` but with support for context information.\n *\n * @group Services\n * @group Services/AppError\n */\nexport class AppError extends Error {\n /**\n * The date of when the error was generated.\n */\n readonly _date: Date;\n /**\n * The context information that can be provided to the error, and picked by the error\n * handler.\n */\n readonly _context: AppErrorContext;\n /**\n * The service that generates HTTP status codes.\n */\n protected _statuses: Statuses;\n /**\n * @param message The message of the error.\n * @param context The context information, for the error handler.\n * @param statuses A reference to the service that generates HTTP status codes. This\n * is in case the implementation wants to use a special version from\n * the container; otherwise, it will use the `statuses` library\n * directly.\n */\n constructor(\n message: string,\n context: AppErrorContext = {},\n statuses: Statuses = statusesFn,\n ) {\n super(message);\n this.name = this.constructor.name;\n this._date = new Date();\n this._statuses = statuses;\n this._context = this._parseContext(context);\n\n // Limit the stack trace if possible.\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n /**\n * Gets an object that can be included in a response from the application. This method\n * is a helper for the `response` getter, as it allows for the assertion of the response\n * type.\n *\n * @template T The type of the response.\n */\n getResponse<T>(): T {\n return this.response as T;\n }\n /**\n * Information about the error that can be included in a response. This is set using the\n * `response` key in the `context` option.\n */\n get response(): unknown {\n return this._context.response || {};\n }\n /**\n * An HTTP status code related to the error. This is set using the `status` key on the\n * `context`.\n */\n get status(): number | undefined {\n return this._context.status as number | undefined;\n }\n /**\n * Context information related to the error.\n */\n get context(): unknown {\n return this._context;\n }\n /**\n * The date of when the error was generated.\n */\n get date(): Date {\n return this._date;\n }\n /**\n * Utility method that formats the context before saving it in the instance:\n * - If the context includes a `status` as a `string`, it will try to replace it with\n * its actual status code from the `statuses `service.\n *\n * @param context The original context sent to the constructor.\n */\n protected _parseContext(context: AppErrorContext): AppErrorContext {\n const result = { ...context };\n if (result.status && typeof result.status === 'string') {\n result.status = this._statuses.code[result.status.toLowerCase()] || result.status;\n }\n\n return result;\n }\n}\n/**\n * Shorthand for `new AppError()`.\n *\n * @param args The same parameters as the {@link AppError} constructor.\n * @returns A new instance of {@link AppError}.\n * @group Services\n * @group Services/AppError\n */\nexport const createAppError = (\n ...args: ConstructorParameters<typeof AppError>\n): AppError => new AppError(...args);\n/**\n * The type of the function that generates a new instance of {@link AppError}.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/AppError\n */\nexport type CreateAppErrorFn = typeof createAppError;\n/**\n * THe type of the {@link AppError} class.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/AppError\n */\nexport type AppErrorClass = typeof AppError;\n/**\n * A service provider that will register both, {@link AppError} and\n * {@link createAppError}, on the container. `AppError` will be the key for class, and\n * `appError` will be for the generator function.\n *\n * @example\n *\n * // Register it on the container\n * container.register(appErrorProvider);\n * // Getting access to the class.\n * const AppError = container.get<AppErrorClass>('AppError');\n * // Getting access to the function.\n * const appError = container.get<CreateAppErrorFn>('appError');\n *\n * @group Providers\n * @group Services/AppError\n */\nexport const appErrorProvider = provider((app) => {\n app.set('AppError', () => AppError);\n app.set('appError', () => createAppError);\n});\n"],"mappings":";;;AAAA,SAAS,UAAU,YAAY,kBAAiC;AAsBzD,MAAM,iBAAiB,MAAM;AAAA,EAsBlC,YACE,SACA,UAA2B,CAAC,GAC5B,WAAqB,YACrB;AACA,UAAM,OAAO;AAvBf,wBAAS;AAKT,wBAAS;AAIT,wBAAU;AAeR,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,IAAI,KAAK;AACtB,SAAK,YAAY;AACjB,SAAK,WAAW,KAAK,cAAc,OAAO;AAG1C,QAAI,MAAM,mBAAmB;AAC3B,YAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,IAChD;AAAA,EACF;AAAA,EAQA,cAAoB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO,KAAK,SAAS,YAAY,CAAC;AAAA,EACpC;AAAA,EAKA,IAAI,SAA6B;AAC/B,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA,EAIA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAIA,IAAI,OAAa;AACf,WAAO,KAAK;AAAA,EACd;AAAA,EAQU,cAAc,SAA2C;AACjE,UAAM,SAAS,EAAE,GAAG,QAAQ;AAC5B,QAAI,OAAO,UAAU,OAAO,OAAO,WAAW,UAAU;AACtD,aAAO,SAAS,KAAK,UAAU,KAAK,OAAO,OAAO,YAAY,MAAM,OAAO;AAAA,IAC7E;AAEA,WAAO;AAAA,EACT;AACF;AASO,MAAM,iBAAiB,IACzB,SACU,IAAI,SAAS,GAAG,IAAI;AAgC5B,MAAM,mBAAmB,SAAS,CAAC,QAAQ;AAChD,MAAI,IAAI,YAAY,MAAM,QAAQ;AAClC,MAAI,IAAI,YAAY,MAAM,cAAc;AAC1C,CAAC;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { provider, statuses as statusesFn } from "../../utils";
|
|
3
|
+
import { AppError } from "./appError";
|
|
4
|
+
class HTTPError extends AppError {
|
|
5
|
+
constructor(message, status = statusesFn("ok"), context = {}, statuses = statusesFn) {
|
|
6
|
+
super(message, { ...context, status }, statuses);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
const createHTTPError = (...args) => new HTTPError(...args);
|
|
10
|
+
const httpErrorProvider = provider((app) => {
|
|
11
|
+
app.set("HTTPError", () => HTTPError);
|
|
12
|
+
app.set("httpError", () => createHTTPError);
|
|
13
|
+
});
|
|
14
|
+
export {
|
|
15
|
+
HTTPError,
|
|
16
|
+
createHTTPError,
|
|
17
|
+
httpErrorProvider
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=httpError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/common/httpError.ts"],"sourcesContent":["import { provider, statuses as statusesFn, type Statuses } from '../../utils';\nimport { AppError, type AppErrorContext } from './appError';\n/**\n * A type of error to be used on HTTP requests. This is the most common type of error used\n * by Jimpex.\n *\n * @group Services\n * @group Services/HTTPError\n */\nexport class HTTPError extends AppError {\n /**\n * @param message The error message.\n * @param status The HTTP status code.\n * @param context Context information related to the error.\n * @param statuses A reference to the service that generates HTTP status codes. This\n * is in case the implementation wants to use a special version from\n * the container; otherwise, it will use the `statuses` library\n * directly.\n */\n constructor(\n message: string,\n status: number | string = statusesFn('ok'),\n context: AppErrorContext = {},\n statuses: Statuses = statusesFn,\n ) {\n super(message, { ...context, status }, statuses);\n }\n}\n/**\n * Shorthand for `new HTTPError()`.\n *\n * @param args The same parameters as the {@link HTTPError} constructor.\n * @returns A new instance of {@link HTTPError}.\n * @group Services\n * @group Services/HTTPError\n */\nexport const createHTTPError = (\n ...args: ConstructorParameters<typeof HTTPError>\n): HTTPError => new HTTPError(...args);\n/**\n * The type of the function that generates a new instance of {@link HTTPError}.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/HTTPError\n */\nexport type CreateHTTPErrorFn = typeof createHTTPError;\n/**\n * THe type of the {@link HTTPError} class.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/HTTPError\n */\nexport type HTTPErrorClass = typeof HTTPError;\n/**\n * A service provider that will register both the {@link HTTPError} and a generator\n * function on the container. `HTTPError` will be the key for class, and `httpError` will\n * be for the generator function.\n *\n * @example\n *\n * // Register it on the container\n * container.register(httpErrorProvider);\n * // Getting access to the class.\n * const HTTPError = container.get<HTTPErrorClass>('HTTPError');\n * // Getting access to the function.\n * const httpError = container.get<CreateHTTPErrorFn>('httpError');\n *\n * @group Providers\n * @group Services/HTTPError\n */\nexport const httpErrorProvider = provider((app) => {\n app.set('HTTPError', () => HTTPError);\n app.set('httpError', () => createHTTPError);\n});\n"],"mappings":";AAAA,SAAS,UAAU,YAAY,kBAAiC;AAChE,SAAS,gBAAsC;AAQxC,MAAM,kBAAkB,SAAS;AAAA,EAUtC,YACE,SACA,SAA0B,WAAW,IAAI,GACzC,UAA2B,CAAC,GAC5B,WAAqB,YACrB;AACA,UAAM,SAAS,EAAE,GAAG,SAAS,OAAO,GAAG,QAAQ;AAAA,EACjD;AACF;AASO,MAAM,kBAAkB,IAC1B,SACW,IAAI,UAAU,GAAG,IAAI;AAgC9B,MAAM,oBAAoB,SAAS,CAAC,QAAQ;AACjD,MAAI,IAAI,aAAa,MAAM,SAAS;AACpC,MAAI,IAAI,aAAa,MAAM,eAAe;AAC5C,CAAC;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { providers } from "../../utils";
|
|
3
|
+
import { appErrorProvider } from "./appError";
|
|
4
|
+
import { httpErrorProvider } from "./httpError";
|
|
5
|
+
import { sendFileProvider } from "./sendFile";
|
|
6
|
+
const commonServicesProvider = providers({
|
|
7
|
+
appErrorProvider,
|
|
8
|
+
httpErrorProvider,
|
|
9
|
+
sendFileProvider
|
|
10
|
+
});
|
|
11
|
+
export * from "./appError";
|
|
12
|
+
export * from "./httpError";
|
|
13
|
+
export * from "./sendFile";
|
|
14
|
+
export {
|
|
15
|
+
commonServicesProvider
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/common/index.ts"],"sourcesContent":["import { providers } from '../../utils';\nimport { appErrorProvider } from './appError';\nimport { httpErrorProvider } from './httpError';\nimport { sendFileProvider } from './sendFile';\n/**\n * Registers all the common services on the container.\n *\n * - {@link AppError | appError}\n * - {@link HTTPError | httpError}\n * - {@link SendFile | sendFile}\n *\n * @example\n *\n * // Register the collection on the container\n * container.register(commonServicesProvider);\n * // Getting access to one the services instance\n * const sendFile = container.get<SendFile>('sendFile');\n *\n * @group Providers\n */\nexport const commonServicesProvider = providers({\n appErrorProvider,\n httpErrorProvider,\n sendFileProvider,\n});\n\nexport * from './appError';\nexport * from './httpError';\nexport * from './sendFile';\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,wBAAwB;AACjC,SAAS,yBAAyB;AAClC,SAAS,wBAAwB;AAiB1B,MAAM,yBAAyB,UAAU;AAAA,EAC9C;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { provider } from "../../utils";
|
|
3
|
+
const sendFile = ({ inject: { pathUtils } }) => ({ res, filepath, from = "app", next = () => {
|
|
4
|
+
} }) => {
|
|
5
|
+
res.sendFile(pathUtils.joinFrom(from, filepath), (error) => {
|
|
6
|
+
if (error) {
|
|
7
|
+
next(error);
|
|
8
|
+
} else {
|
|
9
|
+
res.end();
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
const sendFileProvider = provider((app) => {
|
|
14
|
+
app.set(
|
|
15
|
+
"sendFile",
|
|
16
|
+
() => sendFile({
|
|
17
|
+
inject: {
|
|
18
|
+
pathUtils: app.get("pathUtils")
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
export {
|
|
24
|
+
sendFile,
|
|
25
|
+
sendFileProvider
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=sendFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/common/sendFile.ts"],"sourcesContent":["import { provider } from '../../utils';\nimport type { Response, NextFunction, PathUtils } from '../../types';\n/**\n * The options to create a {@link SendFile} function.\n *\n * @group Services/SendFile\n */\nexport type SendFileGeneratorOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n pathUtils: PathUtils;\n };\n};\n/**\n * The options for the function that actually serves a file.\n *\n * @group Services/SendFile\n */\nexport type SendFileOptions = {\n /**\n * The response object sent by the application. Necessary to write the file.\n */\n res: Response;\n /**\n * The path of the file to serve. Depending on the `from` option, it will be either\n * relative to the project root, or the application executable.\n */\n filepath: string;\n /**\n * The name of a location on the `pathUtils` service from where the `filepath` is\n * relative to. It can be `app` for the directory containing the application executable,\n * or `project` for the project root. It could also be any other location that the\n * implementation may have registered.\n *\n * @default 'app'\n */\n from?: string;\n /**\n * The function to move to the next middleware. It can be used to report an error in\n * case the file can't be served.\n *\n * @default {() => {}}\n */\n next?: NextFunction;\n};\n/**\n * The type of the function that serves a file.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/SendFile\n */\nexport type SendFile = (options: SendFileOptions) => void;\n/**\n * Generates a function to send files on the application response.\n *\n * @param options To inject the required dependencies.\n * @example\n *\n * <caption>Basic usage</caption>\n *\n * // Let's say this is inside an Express middleware.\n * // Get the function\n * const send = sendFile(pathUtils);\n * send({ res, filepath: 'some-file.html', next });\n * // If your app is on \"/app/index.js\", this will send \"/app/some-file.html\".\n *\n * @group Services\n * @group Services/SendFile\n */\nexport const sendFile =\n ({ inject: { pathUtils } }: SendFileGeneratorOptions): SendFile =>\n ({ res, filepath, from = 'app', next = () => {} }) => {\n res.sendFile(pathUtils.joinFrom(from, filepath), (error) => {\n if (error) {\n next(error);\n } else {\n res.end();\n }\n });\n };\n\n/**\n * The service provider that once registered on the container will set the result of\n * {@link sendFile} as the `sendFile` service.\n *\n * @example\n *\n * // Register it on the container\n * container.register(sendFileProvider);\n * // Getting access to the service instance\n * const sendFile = container.get<SendFile>('sendFile');\n *\n * @group Providers\n * @group Services/SendFile\n */\nexport const sendFileProvider = provider((app) => {\n app.set('sendFile', () =>\n sendFile({\n inject: {\n pathUtils: app.get('pathUtils'),\n },\n }),\n );\n});\n"],"mappings":";AAAA,SAAS,gBAAgB;AAuElB,MAAM,WACX,CAAC,EAAE,QAAQ,EAAE,UAAU,EAAE,MACzB,CAAC,EAAE,KAAK,UAAU,OAAO,OAAO,OAAO,MAAM;AAAC,EAAE,MAAM;AACpD,MAAI,SAAS,UAAU,SAAS,MAAM,QAAQ,GAAG,CAAC,UAAU;AAC1D,QAAI,OAAO;AACT,WAAK,KAAK;AAAA,IACZ,OAAO;AACL,UAAI,IAAI;AAAA,IACV;AAAA,EACF,CAAC;AACH;AAgBK,MAAM,mBAAmB,SAAS,CAAC,QAAQ;AAChD,MAAI;AAAA,IAAI;AAAA,IAAY,MAClB,SAAS;AAAA,MACP,QAAQ;AAAA,QACN,WAAW,IAAI,IAAI,WAAW;AAAA,MAChC;AAAA,IACF,CAAC;AAAA,EACH;AACF,CAAC;","names":[]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import fs from "fs/promises";
|
|
5
|
+
import { provider } from "../../utils";
|
|
6
|
+
class FrontendFs {
|
|
7
|
+
constructor({ inject: { pathUtils } }) {
|
|
8
|
+
__publicField(this, "_pathUtils");
|
|
9
|
+
this._pathUtils = pathUtils;
|
|
10
|
+
}
|
|
11
|
+
delete(filepath) {
|
|
12
|
+
return fs.unlink(this.getAppPath(filepath));
|
|
13
|
+
}
|
|
14
|
+
read(filepath, encoding = "utf-8") {
|
|
15
|
+
return fs.readFile(this.getAppPath(filepath), encoding);
|
|
16
|
+
}
|
|
17
|
+
write(filepath, content) {
|
|
18
|
+
return fs.writeFile(this.getAppPath(filepath), content);
|
|
19
|
+
}
|
|
20
|
+
getAppPath(filepath) {
|
|
21
|
+
return this._pathUtils.joinFrom("app", filepath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const frontendFsProvider = provider((app) => {
|
|
25
|
+
app.set(
|
|
26
|
+
"frontendFs",
|
|
27
|
+
() => new FrontendFs({
|
|
28
|
+
inject: {
|
|
29
|
+
pathUtils: app.get("pathUtils")
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
export {
|
|
35
|
+
FrontendFs,
|
|
36
|
+
frontendFsProvider
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=frontendFs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/frontend/frontendFs.ts"],"sourcesContent":["import fs from 'fs/promises';\nimport { provider } from '../../utils';\nimport type { PathUtils } from '../../types';\n/**\n * The options to construct a {@link FrontendFs}.\n *\n * @group Services/FrontendFs\n */\nexport type FrontendFsOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n pathUtils: PathUtils;\n };\n};\n/**\n * This service allows the application to easily work with the filesystem. The idea behind\n * centralizing this functionalities into a service is that is pretty common to have\n * bundling tools to generate the frontend, and on that process files can have different\n * paths or not even be generated all. The service can be extended/overwritten to\n * accommodate any requirements and avoid having to update or add `if`s to every `fs` call\n * the application does.\n * Another _'feature'_ of this service is that all the paths are relative to the directory\n * where the app executable is located, so you don't have to remember the relative path\n * from the place you are accessing a file to the place where it's located.\n *\n * @group Services\n * @group Services/FrontendFs\n */\nexport class FrontendFs {\n /**\n * The service that generates the relative paths.\n */\n protected readonly _pathUtils: PathUtils;\n /**\n * @param options The options to construct the service.\n */\n constructor({ inject: { pathUtils } }: FrontendFsOptions) {\n this._pathUtils = pathUtils;\n }\n /**\n * Deletes a file from the file system.\n *\n * @param filepath The path to the file.\n */\n delete(filepath: string): Promise<void> {\n return fs.unlink(this.getAppPath(filepath));\n }\n /**\n * Reads a file from the file system.\n *\n * @param filepath The path to the file.\n * @param encoding The text encoding in which the file should be read.\n */\n read(filepath: string, encoding: BufferEncoding = 'utf-8'): Promise<string> {\n return fs.readFile(this.getAppPath(filepath), encoding);\n }\n /**\n * Writes a file on the file system.\n *\n * @param filepath The path to the file.\n * @param content The contents of the file.\n */\n write(filepath: string, content: Parameters<typeof fs.writeFile>[1]): Promise<void> {\n return fs.writeFile(this.getAppPath(filepath), content);\n }\n /**\n * Utility method to get the path of a file relative to the application executable.\n *\n * @param filepath The path to the file.\n */\n protected getAppPath(filepath: string): string {\n return this._pathUtils.joinFrom('app', filepath);\n }\n}\n/**\n * The service provider that once registered on the container will set an instance of\n * {@link FrontendFs} as the `frontendFs` service.\n *\n * @example\n *\n * // Register it on the container\n * container.register(frontendFsProvider);\n * // Getting access to the service instance\n * const frontendFs = container.get<FrontendFs>('frontendFs');\n *\n * @group Providers\n * @group Services/FrontendFs\n */\nexport const frontendFsProvider = provider((app) => {\n app.set(\n 'frontendFs',\n () =>\n new FrontendFs({\n inject: {\n pathUtils: app.get<PathUtils>('pathUtils'),\n },\n }),\n );\n});\n"],"mappings":";;;AAAA,OAAO,QAAQ;AACf,SAAS,gBAAgB;AA6BlB,MAAM,WAAW;AAAA,EAQtB,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAsB;AAJ1D,wBAAmB;AAKjB,SAAK,aAAa;AAAA,EACpB;AAAA,EAMA,OAAO,UAAiC;AACtC,WAAO,GAAG,OAAO,KAAK,WAAW,QAAQ,CAAC;AAAA,EAC5C;AAAA,EAOA,KAAK,UAAkB,WAA2B,SAA0B;AAC1E,WAAO,GAAG,SAAS,KAAK,WAAW,QAAQ,GAAG,QAAQ;AAAA,EACxD;AAAA,EAOA,MAAM,UAAkB,SAA4D;AAClF,WAAO,GAAG,UAAU,KAAK,WAAW,QAAQ,GAAG,OAAO;AAAA,EACxD;AAAA,EAMU,WAAW,UAA0B;AAC7C,WAAO,KAAK,WAAW,SAAS,OAAO,QAAQ;AAAA,EACjD;AACF;AAeO,MAAM,qBAAqB,SAAS,CAAC,QAAQ;AAClD,MAAI;AAAA,IACF;AAAA,IACA,MACE,IAAI,WAAW;AAAA,MACb,QAAQ;AAAA,QACN,WAAW,IAAI,IAAe,WAAW;AAAA,MAC3C;AAAA,IACF,CAAC;AAAA,EACL;AACF,CAAC;","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { providers } from "../../utils";
|
|
3
|
+
import { frontendFsProvider } from "./frontendFs";
|
|
4
|
+
const frontendServicesProvider = providers({
|
|
5
|
+
frontendFsProvider
|
|
6
|
+
});
|
|
7
|
+
export * from "./frontendFs";
|
|
8
|
+
export {
|
|
9
|
+
frontendServicesProvider
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/frontend/index.ts"],"sourcesContent":["import { providers } from '../../utils';\nimport { frontendFsProvider } from './frontendFs';\n/**\n * Registers all the frontend services on the container.\n *\n * - {@link FrontendFs | frontendFs}\n *\n * @example\n *\n * // Register the collection on the container\n * container.register(frontendServicesProvider);\n * // Getting access to one the services instance\n * const frontendFs = container.get<FrontendFs>('frontendFs');\n *\n * @group Providers\n */\nexport const frontendServicesProvider = providers({\n frontendFsProvider,\n});\n\nexport * from './frontendFs';\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,0BAA0B;AAe5B,MAAM,2BAA2B,UAAU;AAAA,EAChD;AACF,CAAC;AAED,cAAc;","names":[]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import { get } from "@homer0/object-utils";
|
|
5
|
+
import { deepAssignWithOverwrite } from "@homer0/deep-assign";
|
|
6
|
+
import { deferred } from "@homer0/deferred";
|
|
7
|
+
import { providerCreator } from "../../utils";
|
|
8
|
+
class HTMLGenerator {
|
|
9
|
+
constructor({
|
|
10
|
+
inject: { config, logger, frontendFs, valuesService },
|
|
11
|
+
...options
|
|
12
|
+
}) {
|
|
13
|
+
__publicField(this, "_options");
|
|
14
|
+
__publicField(this, "_config");
|
|
15
|
+
__publicField(this, "_logger");
|
|
16
|
+
__publicField(this, "_frontendFs");
|
|
17
|
+
__publicField(this, "_valuesService");
|
|
18
|
+
__publicField(this, "_fileReady", false);
|
|
19
|
+
__publicField(this, "_fileDeferred");
|
|
20
|
+
this._config = config;
|
|
21
|
+
this._logger = logger;
|
|
22
|
+
this._frontendFs = frontendFs;
|
|
23
|
+
this._valuesService = valuesService;
|
|
24
|
+
this._options = deepAssignWithOverwrite(
|
|
25
|
+
{
|
|
26
|
+
template: "index.tpl.html",
|
|
27
|
+
file: "index.html",
|
|
28
|
+
silent: false,
|
|
29
|
+
deleteTemplateAfter: true,
|
|
30
|
+
replacePlaceholder: /\{\{appConfig(?:uration)?\}\}/,
|
|
31
|
+
placeholderExpression: /\{\{(.*?)\}\}/gi,
|
|
32
|
+
variableName: "appConfig",
|
|
33
|
+
configKeys: ["features", "version", "postMessagesPrefix"]
|
|
34
|
+
},
|
|
35
|
+
options
|
|
36
|
+
);
|
|
37
|
+
if (this._valuesService && typeof this._valuesService.getValues !== "function") {
|
|
38
|
+
throw new Error("The HTMLGenerator values service must have a `getValues` method");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
whenReady() {
|
|
42
|
+
return this._fileDeferred ? this._fileDeferred.promise : Promise.resolve();
|
|
43
|
+
}
|
|
44
|
+
async generateHTML() {
|
|
45
|
+
if (this._fileReady)
|
|
46
|
+
return void 0;
|
|
47
|
+
if (this._fileDeferred)
|
|
48
|
+
return this._fileDeferred.promise;
|
|
49
|
+
this._fileDeferred = deferred();
|
|
50
|
+
const { template, deleteTemplateAfter, file, silent } = this._options;
|
|
51
|
+
try {
|
|
52
|
+
const templateContents = await this._frontendFs.read(template);
|
|
53
|
+
const values = await this._getValues();
|
|
54
|
+
const html = this._processHTML(templateContents, values);
|
|
55
|
+
await this._frontendFs.write(file, html);
|
|
56
|
+
if (!silent) {
|
|
57
|
+
this._logger.success(`The HTML file was successfully generated (${file})`);
|
|
58
|
+
}
|
|
59
|
+
if (deleteTemplateAfter) {
|
|
60
|
+
await this._frontendFs.delete(template);
|
|
61
|
+
if (!silent) {
|
|
62
|
+
this._logger.info(`The HTML template was successfully removed (${template})`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this._fileReady = true;
|
|
66
|
+
this._fileDeferred.resolve();
|
|
67
|
+
this._fileDeferred = void 0;
|
|
68
|
+
return void 0;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
this._fileDeferred.reject(error);
|
|
71
|
+
this._fileDeferred = void 0;
|
|
72
|
+
if (!silent) {
|
|
73
|
+
this._logger.error("There was an error while generating the HTML");
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
get options() {
|
|
79
|
+
return deepAssignWithOverwrite({}, this._options);
|
|
80
|
+
}
|
|
81
|
+
_getValues() {
|
|
82
|
+
if (this._valuesService) {
|
|
83
|
+
return this._valuesService.getValues(this._options);
|
|
84
|
+
}
|
|
85
|
+
const { configKeys } = this._options;
|
|
86
|
+
if (configKeys && configKeys.length) {
|
|
87
|
+
return Promise.resolve(this._config.get(configKeys));
|
|
88
|
+
}
|
|
89
|
+
return Promise.resolve({});
|
|
90
|
+
}
|
|
91
|
+
_processHTML(template, values) {
|
|
92
|
+
const { replacePlaceholder, placeholderExpression, variableName } = this._options;
|
|
93
|
+
const htmlObject = JSON.stringify(values);
|
|
94
|
+
let code = template.replace(
|
|
95
|
+
replacePlaceholder,
|
|
96
|
+
`window.${variableName} = ${htmlObject}`
|
|
97
|
+
);
|
|
98
|
+
const matches = [];
|
|
99
|
+
let match = placeholderExpression.exec(code);
|
|
100
|
+
while (match) {
|
|
101
|
+
const [string, value] = match;
|
|
102
|
+
if (string && value) {
|
|
103
|
+
matches.push({
|
|
104
|
+
string,
|
|
105
|
+
value
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
match = placeholderExpression.exec(code);
|
|
109
|
+
}
|
|
110
|
+
matches.forEach((info) => {
|
|
111
|
+
code = code.replace(info.string, String(get(values, info.value)));
|
|
112
|
+
});
|
|
113
|
+
return code;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const htmlGeneratorProvider = providerCreator(
|
|
117
|
+
(options = {}) => (app) => {
|
|
118
|
+
const {
|
|
119
|
+
serviceName = "htmlGenerator",
|
|
120
|
+
valuesServiceName = "htmlGeneratorValues",
|
|
121
|
+
...rest
|
|
122
|
+
} = options;
|
|
123
|
+
app.set(
|
|
124
|
+
serviceName,
|
|
125
|
+
() => new HTMLGenerator({
|
|
126
|
+
inject: {
|
|
127
|
+
config: app.get("config"),
|
|
128
|
+
logger: app.get("logger"),
|
|
129
|
+
frontendFs: app.get("frontendFs"),
|
|
130
|
+
valuesService: app.try(valuesServiceName)
|
|
131
|
+
},
|
|
132
|
+
...rest
|
|
133
|
+
})
|
|
134
|
+
);
|
|
135
|
+
app.once("afterStart", () => {
|
|
136
|
+
app.get(serviceName).generateHTML();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
export {
|
|
141
|
+
HTMLGenerator,
|
|
142
|
+
htmlGeneratorProvider
|
|
143
|
+
};
|
|
144
|
+
//# sourceMappingURL=htmlGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/html/htmlGenerator.ts"],"sourcesContent":["import { get } from '@homer0/object-utils';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { deferred, type DeferredPromise } from '@homer0/deferred';\nimport { providerCreator } from '../../utils';\nimport type { Config, Logger } from '../../types';\nimport type { FrontendFs } from '../frontend';\n/**\n * The options to customize a {@link HTMLGenerator} instance.\n *\n * @group Services/HTMLGenerator\n */\nexport type HTMLGeneratorOptions = {\n /**\n * The name of the file it should use as template.\n *\n * @default 'index.tpl.html'\n */\n template: string;\n /**\n * The name of the generated file.\n *\n * @default 'index.html'\n */\n file: string;\n /**\n * If `true`, it won't log messages on the terminal when generating the file.\n *\n * @default false\n */\n silent: boolean;\n /**\n * Whether or not to delete the tempalte after generating the file.\n *\n * @default true\n */\n deleteTemplateAfter: boolean;\n /**\n * The placeholder string where the information will be written.\n *\n * @default /\\{\\{appConfi(?:guration)?\\}\\}/\n */\n replacePlaceholder: string | RegExp;\n /**\n * A regular expression for dynamic placeholders that will be replaced by values when\n * the file is generated.\n *\n * @default /\\{\\{(.*?)\\}\\}/gi\n */\n placeholderExpression: RegExp;\n /**\n * The name of the variable that will have the information on the file.\n *\n * @default 'appConfig'\n */\n variableName: string;\n /**\n * A list of settings from the app configuration that will be used as the information to\n * inject on the file.\n *\n * @default ['features', 'version', 'postMessagesPrefix']\n */\n configKeys: string[];\n};\n/**\n * An external service that can be used to provide the values the generator will replace\n * in the template.\n *\n * @group Services/HTMLGenerator\n */\nexport type HTMLGeneratorValuesService = {\n /**\n * A function that will be called to get the values to replace in the template.\n *\n * @param options The service customization options.\n */\n getValues: (options: HTMLGeneratorOptions) => Promise<Record<string, unknown>>;\n};\n/**\n * The options to construct a {@link HTMLGenerator}.\n *\n * @group Services/HTMLGenerator\n */\nexport type HTMLGeneratorConstructorOptions = Partial<HTMLGeneratorOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n config: Config;\n logger: Logger;\n frontendFs: FrontendFs;\n /**\n * A service that can provide the values to replace in the template. If specified, the\n * values from `configKeys` will be ignored.\n */\n valuesService?: HTMLGeneratorValuesService;\n };\n};\n/**\n * Custom options for the provider that will register an instance of {@link HTMLGenerator}\n * as a service.\n *\n * @group Services/HTMLGenerator\n */\nexport type HTMLGeneratorProviderOptions = Partial<HTMLGeneratorOptions> & {\n /**\n * The name that will be used to register the service on the container. This is to allow\n * multiple \"instances\" of the service to be created.\n *\n * @default 'htmlGenerator'\n */\n serviceName?: string;\n /**\n * The name of a service that the generator will use in order to read the values that\n * will be replaced on the template. If the service is available, the values from\n * `configKeys` will be ignored.\n *\n * @default 'htmlGeneratorValues'\n */\n valuesServiceName?: string;\n};\n/**\n * This is a utility service that generates an HTML file with custom information when the\n * application is started.\n *\n * @group Services\n * @group Services/HTMLGenerator\n */\nexport class HTMLGenerator {\n /**\n * The service customization options.\n */\n protected readonly _options: HTMLGeneratorOptions;\n /**\n * The application configuration service, to get the settings specified by the\n * `configKeys` option.\n */\n protected readonly _config: Config;\n /**\n * The service that logs messages on the terminal, in case the `silent` option is `false`.\n */\n protected readonly _logger: Logger;\n /**\n * The service that interacts with the filesystem.\n */\n protected readonly _frontendFs: FrontendFs;\n /**\n * A service that can provide values to be replaced in the template.\n */\n protected readonly _valuesService?: HTMLGeneratorValuesService;\n /**\n * Whether or not the file was already generated.\n */\n protected _fileReady: boolean = false;\n /**\n * A deferred promise to return when another service asks if the file has been\n * generated. Once this sevice finishes generating the file, the promise will be\n * resolved for all implementations that hold a reference to this deferred.\n */\n protected _fileDeferred?: DeferredPromise<void>;\n /**\n * @param options The options to construct the class.\n * @throws If `valuesService` is specified but it doesn't have a `getValues`\n * method.\n */\n constructor({\n inject: { config, logger, frontendFs, valuesService },\n ...options\n }: HTMLGeneratorConstructorOptions) {\n this._config = config;\n this._logger = logger;\n this._frontendFs = frontendFs;\n this._valuesService = valuesService;\n this._options = deepAssignWithOverwrite(\n {\n template: 'index.tpl.html',\n file: 'index.html',\n silent: false,\n deleteTemplateAfter: true,\n replacePlaceholder: /\\{\\{appConfig(?:uration)?\\}\\}/,\n placeholderExpression: /\\{\\{(.*?)\\}\\}/gi,\n variableName: 'appConfig',\n configKeys: ['features', 'version', 'postMessagesPrefix'],\n },\n options,\n );\n\n if (this._valuesService && typeof this._valuesService.getValues !== 'function') {\n throw new Error('The HTMLGenerator values service must have a `getValues` method');\n }\n }\n /**\n * Gets a promise that will be resolved when the file has been generated.\n */\n whenReady(): Promise<void> {\n return this._fileDeferred ? this._fileDeferred.promise : Promise.resolve();\n }\n /**\n * Generates the HTML file.\n */\n async generateHTML(): Promise<void> {\n // The file is already generated, and since this is async, return the promise.\n if (this._fileReady) return undefined;\n // If the file is not ready, but the deferred exists, return the reference to the promise.\n if (this._fileDeferred) return this._fileDeferred.promise;\n // Create the deferred promise.\n this._fileDeferred = deferred<void>();\n const { template, deleteTemplateAfter, file, silent } = this._options;\n try {\n // Get the template.\n const templateContents = await this._frontendFs.read(template);\n // Get the values to replace.\n const values = await this._getValues();\n // Replace/process the template.\n const html = this._processHTML(templateContents, values);\n // Write it in the filesystem.\n await this._frontendFs.write(file, html);\n if (!silent) {\n this._logger.success(`The HTML file was successfully generated (${file})`);\n }\n // Delete the template, if specified by the options.\n if (deleteTemplateAfter) {\n await this._frontendFs.delete(template);\n if (!silent) {\n this._logger.info(`The HTML template was successfully removed (${template})`);\n }\n }\n\n // Switch the flag, resolve the deferred promise, and delete it.\n this._fileReady = true;\n this._fileDeferred!.resolve();\n this._fileDeferred = undefined;\n return undefined;\n } catch (error) {\n this._fileDeferred!.reject(error);\n this._fileDeferred = undefined;\n if (!silent) {\n this._logger.error('There was an error while generating the HTML');\n }\n throw error;\n }\n }\n /**\n * Gets the customization options.\n */\n get options(): Readonly<HTMLGeneratorOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Helper method to get the values that will be replaced in the template. If a \"values\n * service\" was specified in the constructor, it will get the values from there,\n * otherwise, it will use the `configKeys` option to get the values from the\n * application configuration.\n */\n protected _getValues(): Promise<Record<string, unknown>> {\n if (this._valuesService) {\n return this._valuesService.getValues(this._options);\n }\n\n const { configKeys } = this._options;\n if (configKeys && configKeys.length) {\n return Promise.resolve(this._config.get(configKeys));\n }\n\n return Promise.resolve({});\n }\n /**\n * Processes the HTML template by replacing the placeholders with the actual values.\n *\n * @param template The template for the HTML file.\n * @param values The values dictionary that should be replaced in the template.\n */\n protected _processHTML(template: string, values: Record<string, unknown>) {\n const { replacePlaceholder, placeholderExpression, variableName } = this._options;\n const htmlObject = JSON.stringify(values);\n let code = template.replace(\n replacePlaceholder,\n `window.${variableName} = ${htmlObject}`,\n );\n const matches: Array<{ string: string; value: string }> = [];\n let match = placeholderExpression.exec(code);\n while (match) {\n const [string, value] = match;\n if (string && value) {\n matches.push({\n string,\n value,\n });\n }\n\n match = placeholderExpression.exec(code);\n }\n\n matches.forEach((info) => {\n code = code.replace(info.string, String(get(values, info.value)));\n });\n\n return code;\n }\n}\n/**\n * The service provider that once registered on the container will set an instance of\n * {@link HTMLGenerator} as the `htmlGenerator` service. it will also hook itself to the\n * `after-start` event of the application in order to trigger the generator of the HTML\n * file.\n *\n * @example\n *\n * <caption>Basic usage</caption>\n *\n * // Register it on the container\n * container.register(htmlGeneratorProvider);\n * // Getting access to the service instance\n * const htmlGenerator = container.get<HTMLGenerator>('htmlGenerator');\n *\n * @example\n *\n * <caption>Using with custom options</caption>\n *\n * container.register(\n * htmlGeneratorProvider({\n * serviceName: 'myHtmlGenerator',\n * valuesServiceName: 'myValuesService',\n * template: 'my-template.tpl.html',\n * }),\n * );\n *\n * @group Providers\n * @group Services/HTMLGenerator\n */\nexport const htmlGeneratorProvider = providerCreator(\n (options: HTMLGeneratorProviderOptions = {}) =>\n (app) => {\n const {\n serviceName = 'htmlGenerator',\n valuesServiceName = 'htmlGeneratorValues',\n ...rest\n } = options;\n app.set(\n serviceName,\n () =>\n new HTMLGenerator({\n inject: {\n config: app.get('config'),\n logger: app.get('logger'),\n frontendFs: app.get('frontendFs'),\n valuesService: app.try(valuesServiceName),\n },\n ...rest,\n }),\n );\n\n app.once('afterStart', () => {\n app.get<HTMLGenerator>(serviceName).generateHTML();\n });\n },\n);\n"],"mappings":";;;AAAA,SAAS,WAAW;AACpB,SAAS,+BAA+B;AACxC,SAAS,gBAAsC;AAC/C,SAAS,uBAAuB;AA4HzB,MAAM,cAAc;AAAA,EAqCzB,YAAY;AAAA,IACV,QAAQ,EAAE,QAAQ,QAAQ,YAAY,cAAc;AAAA,OACjD;AAAA,EACL,GAAoC;AApCpC,wBAAmB;AAKnB,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAInB,wBAAU,cAAsB;AAMhC,wBAAU;AAUR,SAAK,UAAU;AACf,SAAK,UAAU;AACf,SAAK,cAAc;AACnB,SAAK,iBAAiB;AACtB,SAAK,WAAW;AAAA,MACd;AAAA,QACE,UAAU;AAAA,QACV,MAAM;AAAA,QACN,QAAQ;AAAA,QACR,qBAAqB;AAAA,QACrB,oBAAoB;AAAA,QACpB,uBAAuB;AAAA,QACvB,cAAc;AAAA,QACd,YAAY,CAAC,YAAY,WAAW,oBAAoB;AAAA,MAC1D;AAAA,MACA;AAAA,IACF;AAEA,QAAI,KAAK,kBAAkB,OAAO,KAAK,eAAe,cAAc,YAAY;AAC9E,YAAM,IAAI,MAAM,iEAAiE;AAAA,IACnF;AAAA,EACF;AAAA,EAIA,YAA2B;AACzB,WAAO,KAAK,gBAAgB,KAAK,cAAc,UAAU,QAAQ,QAAQ;AAAA,EAC3E;AAAA,EAIA,MAAM,eAA8B;AAElC,QAAI,KAAK;AAAY,aAAO;AAE5B,QAAI,KAAK;AAAe,aAAO,KAAK,cAAc;AAElD,SAAK,gBAAgB,SAAe;AACpC,UAAM,EAAE,UAAU,qBAAqB,MAAM,OAAO,IAAI,KAAK;AAC7D,QAAI;AAEF,YAAM,mBAAmB,MAAM,KAAK,YAAY,KAAK,QAAQ;AAE7D,YAAM,SAAS,MAAM,KAAK,WAAW;AAErC,YAAM,OAAO,KAAK,aAAa,kBAAkB,MAAM;AAEvD,YAAM,KAAK,YAAY,MAAM,MAAM,IAAI;AACvC,UAAI,CAAC,QAAQ;AACX,aAAK,QAAQ,QAAQ,6CAA6C,OAAO;AAAA,MAC3E;AAEA,UAAI,qBAAqB;AACvB,cAAM,KAAK,YAAY,OAAO,QAAQ;AACtC,YAAI,CAAC,QAAQ;AACX,eAAK,QAAQ,KAAK,+CAA+C,WAAW;AAAA,QAC9E;AAAA,MACF;AAGA,WAAK,aAAa;AAClB,WAAK,cAAe,QAAQ;AAC5B,WAAK,gBAAgB;AACrB,aAAO;AAAA,IACT,SAAS,OAAP;AACA,WAAK,cAAe,OAAO,KAAK;AAChC,WAAK,gBAAgB;AACrB,UAAI,CAAC,QAAQ;AACX,aAAK,QAAQ,MAAM,8CAA8C;AAAA,MACnE;AACA,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAIA,IAAI,UAA0C;AAC5C,WAAO,wBAAwB,CAAC,GAAG,KAAK,QAAQ;AAAA,EAClD;AAAA,EAOU,aAA+C;AACvD,QAAI,KAAK,gBAAgB;AACvB,aAAO,KAAK,eAAe,UAAU,KAAK,QAAQ;AAAA,IACpD;AAEA,UAAM,EAAE,WAAW,IAAI,KAAK;AAC5B,QAAI,cAAc,WAAW,QAAQ;AACnC,aAAO,QAAQ,QAAQ,KAAK,QAAQ,IAAI,UAAU,CAAC;AAAA,IACrD;AAEA,WAAO,QAAQ,QAAQ,CAAC,CAAC;AAAA,EAC3B;AAAA,EAOU,aAAa,UAAkB,QAAiC;AACxE,UAAM,EAAE,oBAAoB,uBAAuB,aAAa,IAAI,KAAK;AACzE,UAAM,aAAa,KAAK,UAAU,MAAM;AACxC,QAAI,OAAO,SAAS;AAAA,MAClB;AAAA,MACA,UAAU,kBAAkB;AAAA,IAC9B;AACA,UAAM,UAAoD,CAAC;AAC3D,QAAI,QAAQ,sBAAsB,KAAK,IAAI;AAC3C,WAAO,OAAO;AACZ,YAAM,CAAC,QAAQ,KAAK,IAAI;AACxB,UAAI,UAAU,OAAO;AACnB,gBAAQ,KAAK;AAAA,UACX;AAAA,UACA;AAAA,QACF,CAAC;AAAA,MACH;AAEA,cAAQ,sBAAsB,KAAK,IAAI;AAAA,IACzC;AAEA,YAAQ,QAAQ,CAAC,SAAS;AACxB,aAAO,KAAK,QAAQ,KAAK,QAAQ,OAAO,IAAI,QAAQ,KAAK,KAAK,CAAC,CAAC;AAAA,IAClE,CAAC;AAED,WAAO;AAAA,EACT;AACF;AA+BO,MAAM,wBAAwB;AAAA,EACnC,CAAC,UAAwC,CAAC,MACxC,CAAC,QAAQ;AACP,UAAM;AAAA,MACJ,cAAc;AAAA,MACd,oBAAoB;AAAA,SACjB;AAAA,IACL,IAAI;AACJ,QAAI;AAAA,MACF;AAAA,MACA,MACE,IAAI,cAAc;AAAA,QAChB,QAAQ;AAAA,UACN,QAAQ,IAAI,IAAI,QAAQ;AAAA,UACxB,QAAQ,IAAI,IAAI,QAAQ;AAAA,UACxB,YAAY,IAAI,IAAI,YAAY;AAAA,UAChC,eAAe,IAAI,IAAI,iBAAiB;AAAA,QAC1C;AAAA,QACA,GAAG;AAAA,MACL,CAAC;AAAA,IACL;AAEA,QAAI,KAAK,cAAc,MAAM;AAC3B,UAAI,IAAmB,WAAW,EAAE,aAAa;AAAA,IACnD,CAAC;AAAA,EACH;AACJ;","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { providers } from "../../utils";
|
|
3
|
+
import { htmlGeneratorProvider } from "./htmlGenerator";
|
|
4
|
+
const htmlServicesProvider = providers({
|
|
5
|
+
htmlGeneratorProvider
|
|
6
|
+
});
|
|
7
|
+
export * from "./htmlGenerator";
|
|
8
|
+
export {
|
|
9
|
+
htmlServicesProvider
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/html/index.ts"],"sourcesContent":["import { providers } from '../../utils';\nimport { htmlGeneratorProvider } from './htmlGenerator';\n/**\n * Registers all the HTML services on the container.\n *\n * - {@link HTMLGenerator | htmlGenerator}\n *\n * @example\n *\n * // Register the collection on the container\n * container.register(htmlServicesProvider);\n * // Getting access to one the services instance\n * const htmlGenerator = container.get<HTMLGenerator>('htmlGenerator');\n *\n * @group Providers\n */\nexport const htmlServicesProvider = providers({\n htmlGeneratorProvider,\n});\n\nexport * from './htmlGenerator';\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,6BAA6B;AAe/B,MAAM,uBAAuB,UAAU;AAAA,EAC5C;AACF,CAAC;AAED,cAAc;","names":[]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import {
|
|
5
|
+
APIClient as APIClientBase
|
|
6
|
+
} from "@homer0/api-utils";
|
|
7
|
+
import { deepAssignWithOverwrite } from "@homer0/deep-assign";
|
|
8
|
+
import { providerCreator } from "../../utils";
|
|
9
|
+
class APIClient extends APIClientBase {
|
|
10
|
+
constructor({ inject: { http, HTTPError }, ...rest }) {
|
|
11
|
+
const { endpoints, gateway, url } = rest;
|
|
12
|
+
const useEndpoints = endpoints || gateway;
|
|
13
|
+
const apiConfig = {
|
|
14
|
+
url,
|
|
15
|
+
endpoints: useEndpoints
|
|
16
|
+
};
|
|
17
|
+
super({
|
|
18
|
+
...apiConfig,
|
|
19
|
+
fetchClient: http.fetch
|
|
20
|
+
});
|
|
21
|
+
__publicField(this, "_http");
|
|
22
|
+
__publicField(this, "_HTTPError");
|
|
23
|
+
__publicField(this, "_apiConfig");
|
|
24
|
+
this._http = http;
|
|
25
|
+
this._HTTPError = HTTPError;
|
|
26
|
+
this._apiConfig = deepAssignWithOverwrite({}, apiConfig);
|
|
27
|
+
}
|
|
28
|
+
get apiConfig() {
|
|
29
|
+
return deepAssignWithOverwrite({}, this._apiConfig);
|
|
30
|
+
}
|
|
31
|
+
_getErrorMessageFromResponse(response) {
|
|
32
|
+
const res = response;
|
|
33
|
+
if (res.error)
|
|
34
|
+
return res.error;
|
|
35
|
+
if (res.data) {
|
|
36
|
+
if (res.data.message)
|
|
37
|
+
return res.data.message;
|
|
38
|
+
if (res.data.error)
|
|
39
|
+
return res.data.error;
|
|
40
|
+
}
|
|
41
|
+
return "Unexpected error";
|
|
42
|
+
}
|
|
43
|
+
formatError(response, status) {
|
|
44
|
+
return new this._HTTPError(this._getErrorMessageFromResponse(response), status);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const apiClientProvider = providerCreator(
|
|
48
|
+
(options = {}) => (app) => {
|
|
49
|
+
const defaultName = "apiClient";
|
|
50
|
+
const { serviceName = defaultName, clientClass: ClientClass = APIClient } = options;
|
|
51
|
+
let { configSetting } = options;
|
|
52
|
+
if (!configSetting) {
|
|
53
|
+
configSetting = serviceName === defaultName ? "api" : serviceName;
|
|
54
|
+
}
|
|
55
|
+
app.set(
|
|
56
|
+
serviceName,
|
|
57
|
+
() => new ClientClass({
|
|
58
|
+
inject: {
|
|
59
|
+
http: app.get("http"),
|
|
60
|
+
HTTPError: app.get("HTTPError")
|
|
61
|
+
},
|
|
62
|
+
...app.getConfig(configSetting)
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
export {
|
|
68
|
+
APIClient,
|
|
69
|
+
apiClientProvider
|
|
70
|
+
};
|
|
71
|
+
//# sourceMappingURL=apiClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/http/apiClient.ts"],"sourcesContent":["import {\n APIClient as APIClientBase,\n type ErrorResponse,\n type APIClientOptions,\n type FetchClient,\n} from '@homer0/api-utils';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { providerCreator } from '../../utils';\nimport type { HTTP } from './http';\nimport type { HTTPErrorClass } from '../common';\n\nexport type { ErrorResponse };\n/**\n * The required settings needed to create the an API client.\n *\n * @group Services/APIClient\n */\nexport type APIClientConfig = Pick<APIClientOptions, 'url' | 'endpoints'>;\n/**\n * A dictionary of endpoints for the API client.\n * This is declared as standalone because it needs to reference them multiple times.\n *\n * @group Services/APIClient\n */\nexport type EndpointsType = APIClientOptions['endpoints'];\n/**\n * The format the settings needs to have in the application configuration in order to\n * create a valid API client.\n * The settings may include the endpoints dictionary in the `endpoints` property, or the\n * gateway `property`, with `endpoints` always having priority if both exists.\n * The reason for the `gateway` property to be valid, is in case the application also\n * implements a {@link GatewayController}, using the same property for both things will\n * help reduce the amount of duplicated definitions.\n *\n * @group Services/APIClient\n */\nexport type APIClientSettings = Omit<APIClientConfig, 'endpoints'> &\n (\n | {\n endpoints: EndpointsType;\n gateway?: EndpointsType;\n }\n | {\n endpoints?: EndpointsType;\n gateway: EndpointsType;\n }\n );\n/**\n * The options to construct a {@link APIClient}.\n *\n * @group Services/APIClient\n */\nexport type APIClientConstructorOptions = APIClientSettings & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n http: HTTP;\n HTTPError: HTTPErrorClass;\n };\n};\n/**\n * An API client for the application to use. What makes this service special is that it\n * formats received errors using the {@link _HTTPError} class, and as fetch client, it\n * uses the {@link HTTP} service, allowing the application to to internally handle all the\n * requests and responses.\n *\n * The only reason to use the class directly is if you want to subclass it, as you would\n * normally just register the {@link apiClientProvider}.\n *\n * @group Services\n * @group Services/APIClient\n */\nexport class APIClient extends APIClientBase {\n /**\n * The service that makes the requests to the API.\n */\n protected readonly _http: HTTP;\n /**\n * The class to generate possible errors in the requests.\n */\n protected readonly _HTTPError: HTTPErrorClass;\n /**\n * The configuration of the API it uses.\n */\n protected readonly _apiConfig: APIClientConfig;\n /**\n * @param options The options to construct the class.\n */\n constructor({ inject: { http, HTTPError }, ...rest }: APIClientConstructorOptions) {\n const { endpoints, gateway, url } = rest;\n const useEndpoints = (endpoints || gateway)!;\n const apiConfig = {\n url,\n endpoints: useEndpoints,\n };\n super({\n ...apiConfig,\n fetchClient: http.fetch as unknown as FetchClient,\n });\n\n this._http = http;\n this._HTTPError = HTTPError;\n this._apiConfig = deepAssignWithOverwrite({}, apiConfig);\n }\n /**\n * The configuration for the API.\n */\n get apiConfig(): Readonly<APIClientConfig> {\n return deepAssignWithOverwrite({}, this._apiConfig);\n }\n /**\n * Tries to obtain a message from an error caused on a failed request.\n *\n * @param response The response from the failed request.\n */\n protected _getErrorMessageFromResponse(response: unknown) {\n const res = response as {\n error?: string;\n data?: {\n message?: string;\n error?: string;\n };\n };\n if (res.error) return res.error;\n if (res.data) {\n if (res.data.message) return res.data.message;\n if (res.data.error) return res.data.error;\n }\n\n return 'Unexpected error';\n }\n /**\n * Generates an {@link _HTTPError} from the response of a failed request.\n *\n * @param response The response from the failed request.\n * @param status The status code of the response.\n */\n protected override formatError<ResponseType extends ErrorResponse>(\n response: ResponseType,\n status: number,\n ): Error {\n return new this._HTTPError(this._getErrorMessageFromResponse(response), status);\n }\n}\n/**\n * The options for the provider creator that registers an {@link APIClient} in the\n * container.\n * These options allow the application to register multiple instances for different APIs.\n *\n * @group Services/APIClient\n */\nexport type APIClientProviderOptions = {\n /**\n * The name of the service that will be registered into the app.\n *\n * @default 'apiClient'\n */\n serviceName?: string;\n /**\n * The name of the configuration setting that has the API information.\n *\n * @default 'api'\n */\n configSetting?: string;\n /**\n * The class the service will instantiate. It has to extend {@link APIClient}.\n *\n * @default APIClient\n */\n clientClass?: typeof APIClient;\n};\n/**\n * The provider creator to register an instance of {@link APIClient} on the container.\n *\n * @example\n *\n * <caption>Basic usage</caption>\n *\n * // Register it on the container\n * container.register(apiClientProvider);\n * // Getting access to the service instance\n * const apiClient = container.get<APIClient>('apiClient');\n *\n * @example\n *\n * <caption>With custom options</caption>\n *\n * container.register(\n * apiClientProvider({\n * serviceName: 'myApiClient',\n * configSetting: 'myApi',\n * }),\n * );\n *\n * @group Providers\n * @group Services/APIClient\n */\nexport const apiClientProvider = providerCreator(\n (options: APIClientProviderOptions = {}) =>\n (app) => {\n const defaultName = 'apiClient';\n const { serviceName = defaultName, clientClass: ClientClass = APIClient } = options;\n let { configSetting } = options;\n if (!configSetting) {\n configSetting = serviceName === defaultName ? 'api' : serviceName;\n }\n\n app.set(\n serviceName,\n () =>\n new ClientClass({\n inject: {\n http: app.get('http'),\n HTTPError: app.get('HTTPError'),\n },\n ...app.getConfig<APIClientSettings>(configSetting!),\n }),\n );\n },\n);\n"],"mappings":";;;AAAA;AAAA,EACE,aAAa;AAAA,OAIR;AACP,SAAS,+BAA+B;AACxC,SAAS,uBAAuB;AAkEzB,MAAM,kBAAkB,cAAc;AAAA,EAgB3C,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,MAAM,KAAK,GAAgC;AACjF,UAAM,EAAE,WAAW,SAAS,IAAI,IAAI;AACpC,UAAM,eAAgB,aAAa;AACnC,UAAM,YAAY;AAAA,MAChB;AAAA,MACA,WAAW;AAAA,IACb;AACA,UAAM;AAAA,MACJ,GAAG;AAAA,MACH,aAAa,KAAK;AAAA,IACpB,CAAC;AAtBH,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAgBjB,SAAK,QAAQ;AACb,SAAK,aAAa;AAClB,SAAK,aAAa,wBAAwB,CAAC,GAAG,SAAS;AAAA,EACzD;AAAA,EAIA,IAAI,YAAuC;AACzC,WAAO,wBAAwB,CAAC,GAAG,KAAK,UAAU;AAAA,EACpD;AAAA,EAMU,6BAA6B,UAAmB;AACxD,UAAM,MAAM;AAOZ,QAAI,IAAI;AAAO,aAAO,IAAI;AAC1B,QAAI,IAAI,MAAM;AACZ,UAAI,IAAI,KAAK;AAAS,eAAO,IAAI,KAAK;AACtC,UAAI,IAAI,KAAK;AAAO,eAAO,IAAI,KAAK;AAAA,IACtC;AAEA,WAAO;AAAA,EACT;AAAA,EAOmB,YACjB,UACA,QACO;AACP,WAAO,IAAI,KAAK,WAAW,KAAK,6BAA6B,QAAQ,GAAG,MAAM;AAAA,EAChF;AACF;AAsDO,MAAM,oBAAoB;AAAA,EAC/B,CAAC,UAAoC,CAAC,MACpC,CAAC,QAAQ;AACP,UAAM,cAAc;AACpB,UAAM,EAAE,cAAc,aAAa,aAAa,cAAc,UAAU,IAAI;AAC5E,QAAI,EAAE,cAAc,IAAI;AACxB,QAAI,CAAC,eAAe;AAClB,sBAAgB,gBAAgB,cAAc,QAAQ;AAAA,IACxD;AAEA,QAAI;AAAA,MACF;AAAA,MACA,MACE,IAAI,YAAY;AAAA,QACd,QAAQ;AAAA,UACN,MAAM,IAAI,IAAI,MAAM;AAAA,UACpB,WAAW,IAAI,IAAI,WAAW;AAAA,QAChC;AAAA,QACA,GAAG,IAAI,UAA6B,aAAc;AAAA,MACpD,CAAC;AAAA,IACL;AAAA,EACF;AACJ;","names":[]}
|