jimpex 10.0.2 → 11.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 +29 -0
- package/README.md +7 -9
- package/dist/app/index.d.ts +1 -1
- package/dist/app/index.js +1 -22
- package/dist/app/index.js.map +1 -1
- package/dist/app/jimpex.d.ts +1 -1
- package/dist/app/jimpex.js +100 -131
- package/dist/app/jimpex.js.map +1 -1
- package/dist/controllers/common/config.d.ts +2 -2
- package/dist/controllers/common/config.js +12 -39
- package/dist/controllers/common/config.js.map +1 -1
- package/dist/controllers/common/health.d.ts +4 -4
- package/dist/controllers/common/health.js +20 -47
- package/dist/controllers/common/health.js.map +1 -1
- package/dist/controllers/common/index.d.ts +1 -1
- package/dist/controllers/common/index.js +3 -26
- package/dist/controllers/common/index.js.map +1 -1
- package/dist/controllers/common/statics.d.ts +3 -3
- package/dist/controllers/common/statics.js +32 -65
- package/dist/controllers/common/statics.js.map +1 -1
- package/dist/controllers/index.d.ts +1 -1
- package/dist/controllers/index.js +2 -24
- package/dist/controllers/index.js.map +1 -1
- package/dist/controllers/utils/gateway.d.ts +5 -5
- package/dist/controllers/utils/gateway.js +70 -91
- package/dist/controllers/utils/gateway.js.map +1 -1
- package/dist/controllers/utils/index.d.ts +1 -1
- package/dist/controllers/utils/index.js +1 -22
- package/dist/controllers/utils/index.js.map +1 -1
- package/dist/{index-C6I3NCC-.d.ts → index-BlSpNHox.d.ts} +13 -13
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -32
- package/dist/index.js.map +1 -1
- package/dist/middlewares/common/errorHandler.d.ts +5 -5
- package/dist/middlewares/common/errorHandler.js +31 -56
- package/dist/middlewares/common/errorHandler.js.map +1 -1
- package/dist/middlewares/common/forceHTTPS.d.ts +1 -1
- package/dist/middlewares/common/forceHTTPS.js +8 -35
- package/dist/middlewares/common/forceHTTPS.js.map +1 -1
- package/dist/middlewares/common/hsts.d.ts +1 -1
- package/dist/middlewares/common/hsts.js +12 -39
- package/dist/middlewares/common/hsts.js.map +1 -1
- package/dist/middlewares/common/index.d.ts +3 -3
- package/dist/middlewares/common/index.js +3 -26
- package/dist/middlewares/common/index.js.map +1 -1
- package/dist/middlewares/html/fastHTML.d.ts +4 -4
- package/dist/middlewares/html/fastHTML.js +43 -66
- package/dist/middlewares/html/fastHTML.js.map +1 -1
- package/dist/middlewares/html/index.d.ts +2 -2
- package/dist/middlewares/html/index.js +2 -24
- package/dist/middlewares/html/index.js.map +1 -1
- package/dist/middlewares/html/showHTML.d.ts +3 -3
- package/dist/middlewares/html/showHTML.js +25 -52
- package/dist/middlewares/html/showHTML.js.map +1 -1
- package/dist/middlewares/index.d.ts +4 -4
- package/dist/middlewares/index.js +3 -26
- package/dist/middlewares/index.js.map +1 -1
- package/dist/middlewares/utils/index.d.ts +3 -3
- package/dist/middlewares/utils/index.js +1 -22
- package/dist/middlewares/utils/index.js.map +1 -1
- package/dist/middlewares/utils/versionValidator.d.ts +5 -5
- package/dist/middlewares/utils/versionValidator.js +24 -51
- package/dist/middlewares/utils/versionValidator.js.map +1 -1
- package/dist/services/common/appError.d.ts +2 -2
- package/dist/services/common/appError.js +18 -46
- package/dist/services/common/appError.js.map +1 -1
- package/dist/services/common/httpError.d.ts +2 -2
- package/dist/services/common/httpError.js +7 -33
- package/dist/services/common/httpError.js.map +1 -1
- package/dist/services/common/index.d.ts +1 -1
- package/dist/services/common/index.js +14 -42
- package/dist/services/common/index.js.map +1 -1
- package/dist/services/common/sendFile.d.ts +1 -1
- package/dist/services/common/sendFile.js +4 -29
- package/dist/services/common/sendFile.js.map +1 -1
- package/dist/services/frontend/frontendFs.d.ts +2 -2
- package/dist/services/frontend/frontendFs.js +12 -49
- package/dist/services/frontend/frontendFs.js.map +1 -1
- package/dist/services/frontend/index.d.ts +2 -2
- package/dist/services/frontend/index.js +8 -34
- package/dist/services/frontend/index.js.map +1 -1
- package/dist/services/html/htmlGenerator.d.ts +5 -5
- package/dist/services/html/htmlGenerator.js +46 -75
- package/dist/services/html/htmlGenerator.js.map +1 -1
- package/dist/services/html/index.d.ts +2 -2
- package/dist/services/html/index.js +8 -34
- package/dist/services/html/index.js.map +1 -1
- package/dist/services/http/apiClient.d.ts +1 -1
- package/dist/services/http/apiClient.js +23 -48
- package/dist/services/http/apiClient.js.map +1 -1
- package/dist/services/http/http.d.ts +5 -5
- package/dist/services/http/http.js +23 -60
- package/dist/services/http/http.js.map +1 -1
- package/dist/services/http/index.d.ts +2 -2
- package/dist/services/http/index.js +14 -42
- package/dist/services/http/index.js.map +1 -1
- package/dist/services/http/responsesBuilder.d.ts +4 -4
- package/dist/services/http/responsesBuilder.js +13 -40
- package/dist/services/http/responsesBuilder.js.map +1 -1
- package/dist/services/index.d.ts +3 -3
- package/dist/services/index.js +5 -30
- package/dist/services/index.js.map +1 -1
- package/dist/services/utils/ensureBearerToken.d.ts +2 -2
- package/dist/services/utils/ensureBearerToken.js +15 -42
- package/dist/services/utils/ensureBearerToken.js.map +1 -1
- package/dist/services/utils/index.d.ts +1 -1
- package/dist/services/utils/index.js +8 -34
- package/dist/services/utils/index.js.map +1 -1
- package/dist/types/events.d.ts +1 -1
- package/dist/types/events.js +0 -16
- package/dist/types/events.js.map +1 -1
- package/dist/types/express.js +0 -16
- package/dist/types/express.js.map +1 -1
- package/dist/types/http.js +0 -16
- package/dist/types/http.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.js +6 -32
- package/dist/types/index.js.map +1 -1
- package/dist/types/options.d.ts +1 -1
- package/dist/types/options.js +0 -16
- package/dist/types/options.js.map +1 -1
- package/dist/types/utils.js +0 -16
- package/dist/types/utils.js.map +1 -1
- package/dist/types/wootils.js +0 -16
- package/dist/types/wootils.js.map +1 -1
- package/dist/utils/fns/index.js +4 -28
- package/dist/utils/fns/index.js.map +1 -1
- package/dist/utils/fns/others.js +2 -26
- package/dist/utils/fns/others.js.map +1 -1
- package/dist/utils/fns/routes.js +4 -31
- package/dist/utils/fns/routes.js.map +1 -1
- package/dist/utils/fns/statuses.js +3 -37
- package/dist/utils/fns/statuses.js.map +1 -1
- package/dist/utils/fns/text.js +2 -26
- package/dist/utils/fns/text.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +2 -24
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/wrappers.d.ts +1 -1
- package/dist/utils/wrappers.js +20 -48
- package/dist/utils/wrappers.js.map +1 -1
- package/eslint.config.js +19 -0
- package/examples/basic/app.ts +4 -4
- package/examples/basic/controller.ts +2 -2
- package/examples/basic/index.ts +4 -4
- package/examples/basic/middleware.ts +2 -2
- package/examples/basic/service.ts +1 -1
- package/package.json +61 -49
- package/pnpm-workspace.yaml +4 -0
- package/src/app/index.ts +1 -1
- package/src/app/jimpex.ts +23 -20
- package/src/controllers/common/config.ts +4 -4
- package/src/controllers/common/health.ts +9 -5
- package/src/controllers/common/index.ts +3 -3
- package/src/controllers/common/statics.ts +12 -7
- package/src/controllers/index.ts +2 -2
- package/src/controllers/utils/gateway.ts +8 -8
- package/src/controllers/utils/index.ts +1 -1
- package/src/index.ts +6 -6
- package/src/middlewares/common/errorHandler.ts +9 -5
- package/src/middlewares/common/forceHTTPS.ts +2 -2
- package/src/middlewares/common/hsts.ts +2 -2
- package/src/middlewares/common/index.ts +3 -3
- package/src/middlewares/html/fastHTML.ts +14 -5
- package/src/middlewares/html/index.ts +2 -2
- package/src/middlewares/html/showHTML.ts +8 -4
- package/src/middlewares/index.ts +3 -3
- package/src/middlewares/utils/index.ts +1 -1
- package/src/middlewares/utils/versionValidator.ts +6 -6
- package/src/services/common/appError.ts +1 -1
- package/src/services/common/httpError.ts +2 -2
- package/src/services/common/index.ts +7 -7
- package/src/services/common/sendFile.ts +2 -2
- package/src/services/frontend/frontendFs.ts +3 -3
- package/src/services/frontend/index.ts +3 -3
- package/src/services/html/htmlGenerator.ts +11 -13
- package/src/services/html/index.ts +3 -3
- package/src/services/http/apiClient.ts +3 -3
- package/src/services/http/http.ts +5 -5
- package/src/services/http/index.ts +7 -7
- package/src/services/http/responsesBuilder.ts +5 -5
- package/src/services/index.ts +5 -5
- package/src/services/utils/ensureBearerToken.ts +4 -4
- package/src/services/utils/index.ts +3 -3
- package/src/types/events.ts +8 -8
- package/src/types/http.ts +1 -1
- package/src/types/index.ts +6 -6
- package/src/types/options.ts +2 -2
- package/src/types/utils.ts +4 -4
- package/src/utils/fns/index.ts +4 -4
- package/src/utils/fns/routes.ts +1 -1
- package/src/utils/index.ts +2 -2
- package/src/utils/wrappers.ts +6 -6
- package/tsconfig.json +9 -4
- package/tsup.config.ts +1 -2
- package/vitest.config.ts +24 -0
- package/dist/app/index.d.mts +0 -14
- package/dist/app/jimpex.d.mts +0 -14
- package/dist/controllers/common/config.d.mts +0 -79
- package/dist/controllers/common/health.d.mts +0 -83
- package/dist/controllers/common/index.d.mts +0 -22
- package/dist/controllers/common/statics.d.mts +0 -223
- package/dist/controllers/index.d.mts +0 -25
- package/dist/controllers/utils/gateway.d.mts +0 -725
- package/dist/controllers/utils/index.d.mts +0 -17
- package/dist/esm/app/index.js +0 -2
- package/dist/esm/app/index.js.map +0 -1
- package/dist/esm/app/jimpex.js +0 -640
- package/dist/esm/app/jimpex.js.map +0 -1
- package/dist/esm/chunk-V6TY7KAL.js +0 -8
- package/dist/esm/chunk-V6TY7KAL.js.map +0 -1
- package/dist/esm/controllers/common/config.js +0 -80
- package/dist/esm/controllers/common/config.js.map +0 -1
- package/dist/esm/controllers/common/health.js +0 -88
- package/dist/esm/controllers/common/health.js.map +0 -1
- package/dist/esm/controllers/common/index.js +0 -4
- package/dist/esm/controllers/common/index.js.map +0 -1
- package/dist/esm/controllers/common/statics.js +0 -226
- package/dist/esm/controllers/common/statics.js.map +0 -1
- package/dist/esm/controllers/index.js +0 -3
- package/dist/esm/controllers/index.js.map +0 -1
- package/dist/esm/controllers/utils/gateway.js +0 -559
- package/dist/esm/controllers/utils/gateway.js.map +0 -1
- package/dist/esm/controllers/utils/index.js +0 -2
- package/dist/esm/controllers/utils/index.js.map +0 -1
- package/dist/esm/index.js +0 -7
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/middlewares/common/errorHandler.js +0 -116
- package/dist/esm/middlewares/common/errorHandler.js.map +0 -1
- package/dist/esm/middlewares/common/forceHTTPS.js +0 -52
- package/dist/esm/middlewares/common/forceHTTPS.js.map +0 -1
- package/dist/esm/middlewares/common/hsts.js +0 -74
- package/dist/esm/middlewares/common/hsts.js.map +0 -1
- package/dist/esm/middlewares/common/index.js +0 -4
- package/dist/esm/middlewares/common/index.js.map +0 -1
- package/dist/esm/middlewares/html/fastHTML.js +0 -157
- package/dist/esm/middlewares/html/fastHTML.js.map +0 -1
- package/dist/esm/middlewares/html/index.js +0 -3
- package/dist/esm/middlewares/html/index.js.map +0 -1
- package/dist/esm/middlewares/html/showHTML.js +0 -100
- package/dist/esm/middlewares/html/showHTML.js.map +0 -1
- package/dist/esm/middlewares/index.js +0 -4
- package/dist/esm/middlewares/index.js.map +0 -1
- package/dist/esm/middlewares/utils/index.js +0 -2
- package/dist/esm/middlewares/utils/index.js.map +0 -1
- package/dist/esm/middlewares/utils/versionValidator.js +0 -135
- package/dist/esm/middlewares/utils/versionValidator.js.map +0 -1
- package/dist/esm/services/common/appError.js +0 -98
- package/dist/esm/services/common/appError.js.map +0 -1
- package/dist/esm/services/common/httpError.js +0 -28
- package/dist/esm/services/common/httpError.js.map +0 -1
- package/dist/esm/services/common/index.js +0 -17
- package/dist/esm/services/common/index.js.map +0 -1
- package/dist/esm/services/common/sendFile.js +0 -27
- package/dist/esm/services/common/sendFile.js.map +0 -1
- package/dist/esm/services/frontend/frontendFs.js +0 -66
- package/dist/esm/services/frontend/frontendFs.js.map +0 -1
- package/dist/esm/services/frontend/index.js +0 -11
- package/dist/esm/services/frontend/index.js.map +0 -1
- package/dist/esm/services/html/htmlGenerator.js +0 -192
- package/dist/esm/services/html/htmlGenerator.js.map +0 -1
- package/dist/esm/services/html/index.js +0 -11
- package/dist/esm/services/html/index.js.map +0 -1
- package/dist/esm/services/http/apiClient.js +0 -94
- package/dist/esm/services/http/apiClient.js.map +0 -1
- package/dist/esm/services/http/http.js +0 -180
- package/dist/esm/services/http/http.js.map +0 -1
- package/dist/esm/services/http/index.js +0 -17
- package/dist/esm/services/http/index.js.map +0 -1
- package/dist/esm/services/http/responsesBuilder.js +0 -152
- package/dist/esm/services/http/responsesBuilder.js.map +0 -1
- package/dist/esm/services/index.js +0 -6
- package/dist/esm/services/index.js.map +0 -1
- package/dist/esm/services/utils/ensureBearerToken.js +0 -94
- package/dist/esm/services/utils/ensureBearerToken.js.map +0 -1
- package/dist/esm/services/utils/index.js +0 -11
- package/dist/esm/services/utils/index.js.map +0 -1
- package/dist/esm/types/events.js +0 -1
- package/dist/esm/types/events.js.map +0 -1
- package/dist/esm/types/express.js +0 -1
- package/dist/esm/types/express.js.map +0 -1
- package/dist/esm/types/http.js +0 -1
- package/dist/esm/types/http.js.map +0 -1
- package/dist/esm/types/index.js +0 -7
- package/dist/esm/types/index.js.map +0 -1
- package/dist/esm/types/options.js +0 -1
- package/dist/esm/types/options.js.map +0 -1
- package/dist/esm/types/utils.js +0 -1
- package/dist/esm/types/utils.js.map +0 -1
- package/dist/esm/types/wootils.js +0 -1
- package/dist/esm/types/wootils.js.map +0 -1
- package/dist/esm/utils/fns/index.js +0 -5
- package/dist/esm/utils/fns/index.js.map +0 -1
- package/dist/esm/utils/fns/others.js +0 -6
- package/dist/esm/utils/fns/others.js.map +0 -1
- package/dist/esm/utils/fns/routes.js +0 -25
- package/dist/esm/utils/fns/routes.js.map +0 -1
- package/dist/esm/utils/fns/statuses.js +0 -6
- package/dist/esm/utils/fns/statuses.js.map +0 -1
- package/dist/esm/utils/fns/text.js +0 -6
- package/dist/esm/utils/fns/text.js.map +0 -1
- package/dist/esm/utils/index.js +0 -3
- package/dist/esm/utils/index.js.map +0 -1
- package/dist/esm/utils/wrappers.js +0 -41
- package/dist/esm/utils/wrappers.js.map +0 -1
- package/dist/index-Bwf7JHu9.d.mts +0 -1287
- package/dist/index.d.mts +0 -47
- package/dist/middlewares/common/errorHandler.d.mts +0 -132
- package/dist/middlewares/common/forceHTTPS.d.mts +0 -69
- package/dist/middlewares/common/hsts.d.mts +0 -110
- package/dist/middlewares/common/index.d.mts +0 -22
- package/dist/middlewares/html/fastHTML.d.mts +0 -180
- package/dist/middlewares/html/index.d.mts +0 -21
- package/dist/middlewares/html/showHTML.d.mts +0 -127
- package/dist/middlewares/index.d.mts +0 -30
- package/dist/middlewares/utils/index.d.mts +0 -20
- package/dist/middlewares/utils/versionValidator.d.mts +0 -215
- package/dist/services/common/appError.d.mts +0 -139
- package/dist/services/common/httpError.d.mts +0 -80
- package/dist/services/common/index.d.mts +0 -47
- package/dist/services/common/sendFile.d.mts +0 -102
- package/dist/services/frontend/frontendFs.d.mts +0 -96
- package/dist/services/frontend/index.d.mts +0 -40
- package/dist/services/html/htmlGenerator.d.mts +0 -237
- package/dist/services/html/index.d.mts +0 -43
- package/dist/services/http/apiClient.d.mts +0 -170
- package/dist/services/http/http.d.mts +0 -182
- package/dist/services/http/index.d.mts +0 -51
- package/dist/services/http/responsesBuilder.d.mts +0 -179
- package/dist/services/index.d.mts +0 -33
- package/dist/services/utils/ensureBearerToken.d.mts +0 -158
- package/dist/services/utils/index.d.mts +0 -44
- package/dist/types/events.d.mts +0 -14
- package/dist/types/express.d.mts +0 -10
- package/dist/types/http.d.mts +0 -82
- package/dist/types/index.d.mts +0 -14
- package/dist/types/options.d.mts +0 -14
- package/dist/types/utils.d.mts +0 -48
- package/dist/types/wootils.d.mts +0 -4
- package/dist/utils/fns/index.d.mts +0 -5
- package/dist/utils/fns/others.d.mts +0 -17
- package/dist/utils/fns/routes.d.mts +0 -39
- package/dist/utils/fns/statuses.d.mts +0 -45
- package/dist/utils/fns/text.d.mts +0 -9
- package/dist/utils/index.d.mts +0 -19
- package/dist/utils/wrappers.d.mts +0 -14
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "../../chunk-V6TY7KAL.js";
|
|
4
|
-
import { deepAssignWithOverwrite } from "@homer0/deep-assign";
|
|
5
|
-
import { middlewareCreator } from "../../utils";
|
|
6
|
-
class VersionValidator {
|
|
7
|
-
/**
|
|
8
|
-
* @param options The options to construct the class.
|
|
9
|
-
* @throws If no `version` is specified in the options.
|
|
10
|
-
*/
|
|
11
|
-
constructor({ inject, version, ...options }) {
|
|
12
|
-
/**
|
|
13
|
-
* To generate the errors in case the validation fails.
|
|
14
|
-
*/
|
|
15
|
-
__publicField(this, "_HTTPError");
|
|
16
|
-
/**
|
|
17
|
-
* To generate responses for popups.
|
|
18
|
-
*/
|
|
19
|
-
__publicField(this, "_responsesBuilder");
|
|
20
|
-
/**
|
|
21
|
-
* The uility service to get HTTP status codes.
|
|
22
|
-
*/
|
|
23
|
-
__publicField(this, "_statuses");
|
|
24
|
-
/**
|
|
25
|
-
* The customization options.
|
|
26
|
-
*/
|
|
27
|
-
__publicField(this, "_options");
|
|
28
|
-
this._HTTPError = inject.HTTPError;
|
|
29
|
-
this._responsesBuilder = inject.responsesBuilder;
|
|
30
|
-
this._statuses = inject.statuses;
|
|
31
|
-
this._options = deepAssignWithOverwrite(
|
|
32
|
-
{
|
|
33
|
-
error: "The application version doesn't match",
|
|
34
|
-
latest: {
|
|
35
|
-
allow: true,
|
|
36
|
-
name: "latest"
|
|
37
|
-
},
|
|
38
|
-
popup: {
|
|
39
|
-
variable: "popup",
|
|
40
|
-
title: "Conflict",
|
|
41
|
-
message: "vesion:conflict"
|
|
42
|
-
},
|
|
43
|
-
version
|
|
44
|
-
},
|
|
45
|
-
options
|
|
46
|
-
);
|
|
47
|
-
if (!this._options.version) {
|
|
48
|
-
throw new Error("You need to supply a version");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Generates the middleware that validates the version.
|
|
53
|
-
*/
|
|
54
|
-
getMiddleware() {
|
|
55
|
-
return (req, res, next) => {
|
|
56
|
-
const { version } = req.params;
|
|
57
|
-
if (!version) {
|
|
58
|
-
next();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (version === this._options.version || this._isTheAllowedLatest(version)) {
|
|
62
|
-
next();
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const status = this._statuses("conflict");
|
|
66
|
-
if (this._isPopup(req)) {
|
|
67
|
-
const { title, message } = this._options.popup;
|
|
68
|
-
this._responsesBuilder.htmlPostMessage({
|
|
69
|
-
res,
|
|
70
|
-
title,
|
|
71
|
-
message,
|
|
72
|
-
status
|
|
73
|
-
});
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
next(
|
|
77
|
-
new this._HTTPError(this._options.error, status, {
|
|
78
|
-
response: {
|
|
79
|
-
validation: true
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
);
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* The customization options.
|
|
87
|
-
*/
|
|
88
|
-
get options() {
|
|
89
|
-
return deepAssignWithOverwrite({}, this._options);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Helper method that checks if the incoming request is from a popup. It will look for
|
|
93
|
-
* the query string variable defined in the constructor options.
|
|
94
|
-
*
|
|
95
|
-
* @param req The request object sent by the application.
|
|
96
|
-
*/
|
|
97
|
-
_isPopup(req) {
|
|
98
|
-
const popup = req.query[this._options.popup.variable];
|
|
99
|
-
return !!(popup && String(popup).toLowerCase() === "true");
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Helper method that checks if the "latest version" is enabled and if the given version
|
|
103
|
-
* is "the latest" (comparing it with the option name).
|
|
104
|
-
*
|
|
105
|
-
* @param version The version received in the request.
|
|
106
|
-
*/
|
|
107
|
-
_isTheAllowedLatest(version) {
|
|
108
|
-
const { allow, name } = this._options.latest;
|
|
109
|
-
return allow && version === name;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
const versionValidatorMiddleware = middlewareCreator(
|
|
113
|
-
(options = {}) => (app, route) => {
|
|
114
|
-
const version = app.getConfig("version");
|
|
115
|
-
const middleware = new VersionValidator({
|
|
116
|
-
inject: {
|
|
117
|
-
HTTPError: app.get("HTTPError"),
|
|
118
|
-
responsesBuilder: app.get("responsesBuilder"),
|
|
119
|
-
statuses: app.get("statuses")
|
|
120
|
-
},
|
|
121
|
-
version,
|
|
122
|
-
...options
|
|
123
|
-
}).getMiddleware();
|
|
124
|
-
if (route) {
|
|
125
|
-
const router = app.getRouter();
|
|
126
|
-
return router.all("/:version/*", middleware);
|
|
127
|
-
}
|
|
128
|
-
return middleware;
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
export {
|
|
132
|
-
VersionValidator,
|
|
133
|
-
versionValidatorMiddleware
|
|
134
|
-
};
|
|
135
|
-
//# sourceMappingURL=versionValidator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAAA;AAAA;AAAA;AAAA,EAqB5B,YAAY,EAAE,QAAQ,SAAS,GAAG,QAAQ,GAAuC;AAjBjF;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,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;AAAA;AAAA;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;AAAA;AAAA;AAAA,EAIA,IAAI,UAA6C;AAC/C,WAAO,wBAAwB,CAAC,GAAG,KAAK,QAAQ;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU,SAAS,KAAuB;AACxC,UAAM,QAAQ,IAAI,MAAM,KAAK,SAAS,MAAM,QAAQ;AACpD,WAAO,CAAC,EAAE,SAAS,OAAO,KAAK,EAAE,YAAY,MAAM;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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":[]}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "../../chunk-V6TY7KAL.js";
|
|
4
|
-
import { provider, statuses as statusesFn } from "../../utils";
|
|
5
|
-
class AppError extends Error {
|
|
6
|
-
/**
|
|
7
|
-
* @param message The message of the error.
|
|
8
|
-
* @param context The context information, for the error handler.
|
|
9
|
-
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
10
|
-
* is in case the implementation wants to use a special version from
|
|
11
|
-
* the container; otherwise, it will use the `statuses` library
|
|
12
|
-
* directly.
|
|
13
|
-
*/
|
|
14
|
-
constructor(message, context = {}, statuses = statusesFn) {
|
|
15
|
-
super(message);
|
|
16
|
-
/**
|
|
17
|
-
* The date of when the error was generated.
|
|
18
|
-
*/
|
|
19
|
-
__publicField(this, "_date");
|
|
20
|
-
/**
|
|
21
|
-
* The context information that can be provided to the error, and picked by the error
|
|
22
|
-
* handler.
|
|
23
|
-
*/
|
|
24
|
-
__publicField(this, "_context");
|
|
25
|
-
/**
|
|
26
|
-
* The service that generates HTTP status codes.
|
|
27
|
-
*/
|
|
28
|
-
__publicField(this, "_statuses");
|
|
29
|
-
this.name = this.constructor.name;
|
|
30
|
-
this._date = /* @__PURE__ */ new Date();
|
|
31
|
-
this._statuses = statuses;
|
|
32
|
-
this._context = this._parseContext(context);
|
|
33
|
-
if (Error.captureStackTrace) {
|
|
34
|
-
Error.captureStackTrace(this, this.constructor);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Gets an object that can be included in a response from the application. This method
|
|
39
|
-
* is a helper for the `response` getter, as it allows for the assertion of the response
|
|
40
|
-
* type.
|
|
41
|
-
*
|
|
42
|
-
* @template T The type of the response.
|
|
43
|
-
*/
|
|
44
|
-
getResponse() {
|
|
45
|
-
return this.response;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Information about the error that can be included in a response. This is set using the
|
|
49
|
-
* `response` key in the `context` option.
|
|
50
|
-
*/
|
|
51
|
-
get response() {
|
|
52
|
-
return this._context.response || {};
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* An HTTP status code related to the error. This is set using the `status` key on the
|
|
56
|
-
* `context`.
|
|
57
|
-
*/
|
|
58
|
-
get status() {
|
|
59
|
-
return this._context.status;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Context information related to the error.
|
|
63
|
-
*/
|
|
64
|
-
get context() {
|
|
65
|
-
return this._context;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* The date of when the error was generated.
|
|
69
|
-
*/
|
|
70
|
-
get date() {
|
|
71
|
-
return this._date;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Utility method that formats the context before saving it in the instance:
|
|
75
|
-
* - If the context includes a `status` as a `string`, it will try to replace it with
|
|
76
|
-
* its actual status code from the `statuses `service.
|
|
77
|
-
*
|
|
78
|
-
* @param context The original context sent to the constructor.
|
|
79
|
-
*/
|
|
80
|
-
_parseContext(context) {
|
|
81
|
-
const result = { ...context };
|
|
82
|
-
if (result.status && typeof result.status === "string") {
|
|
83
|
-
result.status = this._statuses.code[result.status.toLowerCase()] || result.status;
|
|
84
|
-
}
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
const createAppError = (...args) => new AppError(...args);
|
|
89
|
-
const appErrorProvider = provider((app) => {
|
|
90
|
-
app.set("AppError", () => AppError);
|
|
91
|
-
app.set("appError", () => createAppError);
|
|
92
|
-
});
|
|
93
|
-
export {
|
|
94
|
-
AppError,
|
|
95
|
-
appErrorProvider,
|
|
96
|
-
createAppError
|
|
97
|
-
};
|
|
98
|
-
//# sourceMappingURL=appError.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBlC,YACE,SACA,UAA2B,CAAC,GAC5B,WAAqB,YACrB;AACA,UAAM,OAAO;AAvBf;AAAA;AAAA;AAAA,wBAAS;AAKT;AAAA;AAAA;AAAA;AAAA,wBAAS;AAIT;AAAA;AAAA;AAAA,wBAAU;AAeR,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,oBAAI,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,cAAoB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO,KAAK,SAAS,YAAY,CAAC;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAA6B;AAC/B,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAIA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAIA,IAAI,OAAa;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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,CAAC,KAAK,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":[]}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import "../../chunk-V6TY7KAL.js";
|
|
2
|
-
import { provider, statuses as statusesFn } from "../../utils";
|
|
3
|
-
import { AppError } from "./appError";
|
|
4
|
-
class HTTPError extends AppError {
|
|
5
|
-
/**
|
|
6
|
-
* @param message The error message.
|
|
7
|
-
* @param status The HTTP status code.
|
|
8
|
-
* @param context Context information related to the error.
|
|
9
|
-
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
10
|
-
* is in case the implementation wants to use a special version from
|
|
11
|
-
* the container; otherwise, it will use the `statuses` library
|
|
12
|
-
* directly.
|
|
13
|
-
*/
|
|
14
|
-
constructor(message, status = statusesFn("ok"), context = {}, statuses = statusesFn) {
|
|
15
|
-
super(message, { ...context, status }, statuses);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
const createHTTPError = (...args) => new HTTPError(...args);
|
|
19
|
-
const httpErrorProvider = provider((app) => {
|
|
20
|
-
app.set("HTTPError", () => HTTPError);
|
|
21
|
-
app.set("httpError", () => createHTTPError);
|
|
22
|
-
});
|
|
23
|
-
export {
|
|
24
|
-
HTTPError,
|
|
25
|
-
createHTTPError,
|
|
26
|
-
httpErrorProvider
|
|
27
|
-
};
|
|
28
|
-
//# sourceMappingURL=httpError.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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":[]}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import "../../chunk-V6TY7KAL.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
|
|
@@ -1 +0,0 @@
|
|
|
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":[]}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import "../../chunk-V6TY7KAL.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
|
|
@@ -1 +0,0 @@
|
|
|
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":[]}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "../../chunk-V6TY7KAL.js";
|
|
4
|
-
import fs from "fs/promises";
|
|
5
|
-
import { provider } from "../../utils";
|
|
6
|
-
class FrontendFs {
|
|
7
|
-
/**
|
|
8
|
-
* @param options The options to construct the service.
|
|
9
|
-
*/
|
|
10
|
-
constructor({ inject: { pathUtils } }) {
|
|
11
|
-
/**
|
|
12
|
-
* The service that generates the relative paths.
|
|
13
|
-
*/
|
|
14
|
-
__publicField(this, "_pathUtils");
|
|
15
|
-
this._pathUtils = pathUtils;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Deletes a file from the file system.
|
|
19
|
-
*
|
|
20
|
-
* @param filepath The path to the file.
|
|
21
|
-
*/
|
|
22
|
-
delete(filepath) {
|
|
23
|
-
return fs.unlink(this.getAppPath(filepath));
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Reads a file from the file system.
|
|
27
|
-
*
|
|
28
|
-
* @param filepath The path to the file.
|
|
29
|
-
* @param encoding The text encoding in which the file should be read.
|
|
30
|
-
*/
|
|
31
|
-
read(filepath, encoding = "utf-8") {
|
|
32
|
-
return fs.readFile(this.getAppPath(filepath), encoding);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Writes a file on the file system.
|
|
36
|
-
*
|
|
37
|
-
* @param filepath The path to the file.
|
|
38
|
-
* @param content The contents of the file.
|
|
39
|
-
*/
|
|
40
|
-
write(filepath, content) {
|
|
41
|
-
return fs.writeFile(this.getAppPath(filepath), content);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Utility method to get the path of a file relative to the application executable.
|
|
45
|
-
*
|
|
46
|
-
* @param filepath The path to the file.
|
|
47
|
-
*/
|
|
48
|
-
getAppPath(filepath) {
|
|
49
|
-
return this._pathUtils.joinFrom("app", filepath);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
const frontendFsProvider = provider((app) => {
|
|
53
|
-
app.set(
|
|
54
|
-
"frontendFs",
|
|
55
|
-
() => new FrontendFs({
|
|
56
|
-
inject: {
|
|
57
|
-
pathUtils: app.get("pathUtils")
|
|
58
|
-
}
|
|
59
|
-
})
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
export {
|
|
63
|
-
FrontendFs,
|
|
64
|
-
frontendFsProvider
|
|
65
|
-
};
|
|
66
|
-
//# sourceMappingURL=frontendFs.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAAA;AAAA;AAAA,EAQtB,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAsB;AAJ1D;AAAA;AAAA;AAAA,wBAAmB;AAKjB,SAAK,aAAa;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,UAAiC;AACtC,WAAO,GAAG,OAAO,KAAK,WAAW,QAAQ,CAAC;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,KAAK,UAAkB,WAA2B,SAA0B;AAC1E,WAAO,GAAG,SAAS,KAAK,WAAW,QAAQ,GAAG,QAAQ;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,UAAkB,SAA4D;AAClF,WAAO,GAAG,UAAU,KAAK,WAAW,QAAQ,GAAG,OAAO;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA;AAAA;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":[]}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import "../../chunk-V6TY7KAL.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
|
|
@@ -1 +0,0 @@
|
|
|
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":[]}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "../../chunk-V6TY7KAL.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
|
-
/**
|
|
10
|
-
* @param options The options to construct the class.
|
|
11
|
-
* @throws If `valuesService` is specified but it doesn't have a `getValues`
|
|
12
|
-
* method.
|
|
13
|
-
*/
|
|
14
|
-
constructor({
|
|
15
|
-
inject: { config, logger, frontendFs, valuesService },
|
|
16
|
-
...options
|
|
17
|
-
}) {
|
|
18
|
-
/**
|
|
19
|
-
* The service customization options.
|
|
20
|
-
*/
|
|
21
|
-
__publicField(this, "_options");
|
|
22
|
-
/**
|
|
23
|
-
* The application configuration service, to get the settings specified by the
|
|
24
|
-
* `configKeys` option.
|
|
25
|
-
*/
|
|
26
|
-
__publicField(this, "_config");
|
|
27
|
-
/**
|
|
28
|
-
* The service that logs messages on the terminal, in case the `silent` option is `false`.
|
|
29
|
-
*/
|
|
30
|
-
__publicField(this, "_logger");
|
|
31
|
-
/**
|
|
32
|
-
* The service that interacts with the filesystem.
|
|
33
|
-
*/
|
|
34
|
-
__publicField(this, "_frontendFs");
|
|
35
|
-
/**
|
|
36
|
-
* A service that can provide values to be replaced in the template.
|
|
37
|
-
*/
|
|
38
|
-
__publicField(this, "_valuesService");
|
|
39
|
-
/**
|
|
40
|
-
* Whether or not the file was already generated.
|
|
41
|
-
*/
|
|
42
|
-
__publicField(this, "_fileReady", false);
|
|
43
|
-
/**
|
|
44
|
-
* A deferred promise to return when another service asks if the file has been
|
|
45
|
-
* generated. Once this sevice finishes generating the file, the promise will be
|
|
46
|
-
* resolved for all implementations that hold a reference to this deferred.
|
|
47
|
-
*/
|
|
48
|
-
__publicField(this, "_fileDeferred");
|
|
49
|
-
this._config = config;
|
|
50
|
-
this._logger = logger;
|
|
51
|
-
this._frontendFs = frontendFs;
|
|
52
|
-
this._valuesService = valuesService;
|
|
53
|
-
this._options = deepAssignWithOverwrite(
|
|
54
|
-
{
|
|
55
|
-
template: "index.tpl.html",
|
|
56
|
-
file: "index.html",
|
|
57
|
-
silent: false,
|
|
58
|
-
deleteTemplateAfter: true,
|
|
59
|
-
replacePlaceholder: /\{\{appConfig(?:uration)?\}\}/,
|
|
60
|
-
placeholderExpression: /\{\{(.*?)\}\}/gi,
|
|
61
|
-
variableName: "appConfig",
|
|
62
|
-
configKeys: ["features", "version", "postMessagesPrefix"]
|
|
63
|
-
},
|
|
64
|
-
options
|
|
65
|
-
);
|
|
66
|
-
if (this._valuesService && typeof this._valuesService.getValues !== "function") {
|
|
67
|
-
throw new Error("The HTMLGenerator values service must have a `getValues` method");
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Gets a promise that will be resolved when the file has been generated.
|
|
72
|
-
*/
|
|
73
|
-
whenReady() {
|
|
74
|
-
return this._fileDeferred ? this._fileDeferred.promise : Promise.resolve();
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Generates the HTML file.
|
|
78
|
-
*/
|
|
79
|
-
async generateHTML() {
|
|
80
|
-
if (this._fileReady) return void 0;
|
|
81
|
-
if (this._fileDeferred) return this._fileDeferred.promise;
|
|
82
|
-
this._fileDeferred = deferred();
|
|
83
|
-
const { template, deleteTemplateAfter, file, silent } = this._options;
|
|
84
|
-
try {
|
|
85
|
-
const templateContents = await this._frontendFs.read(template);
|
|
86
|
-
const values = await this._getValues();
|
|
87
|
-
const html = this._processHTML(templateContents, values);
|
|
88
|
-
await this._frontendFs.write(file, html);
|
|
89
|
-
if (!silent) {
|
|
90
|
-
this._logger.success(`The HTML file was successfully generated (${file})`);
|
|
91
|
-
}
|
|
92
|
-
if (deleteTemplateAfter) {
|
|
93
|
-
await this._frontendFs.delete(template);
|
|
94
|
-
if (!silent) {
|
|
95
|
-
this._logger.info(`The HTML template was successfully removed (${template})`);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
this._fileReady = true;
|
|
99
|
-
this._fileDeferred.resolve();
|
|
100
|
-
this._fileDeferred = void 0;
|
|
101
|
-
return void 0;
|
|
102
|
-
} catch (error) {
|
|
103
|
-
this._fileDeferred.reject(error);
|
|
104
|
-
this._fileDeferred = void 0;
|
|
105
|
-
if (!silent) {
|
|
106
|
-
this._logger.error("There was an error while generating the HTML");
|
|
107
|
-
}
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Gets the customization options.
|
|
113
|
-
*/
|
|
114
|
-
get options() {
|
|
115
|
-
return deepAssignWithOverwrite({}, this._options);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Helper method to get the values that will be replaced in the template. If a "values
|
|
119
|
-
* service" was specified in the constructor, it will get the values from there,
|
|
120
|
-
* otherwise, it will use the `configKeys` option to get the values from the
|
|
121
|
-
* application configuration.
|
|
122
|
-
*/
|
|
123
|
-
_getValues() {
|
|
124
|
-
if (this._valuesService) {
|
|
125
|
-
return this._valuesService.getValues(this._options);
|
|
126
|
-
}
|
|
127
|
-
const { configKeys } = this._options;
|
|
128
|
-
if (configKeys && configKeys.length) {
|
|
129
|
-
return Promise.resolve(this._config.get(configKeys));
|
|
130
|
-
}
|
|
131
|
-
return Promise.resolve({});
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Processes the HTML template by replacing the placeholders with the actual values.
|
|
135
|
-
*
|
|
136
|
-
* @param template The template for the HTML file.
|
|
137
|
-
* @param values The values dictionary that should be replaced in the template.
|
|
138
|
-
*/
|
|
139
|
-
_processHTML(template, values) {
|
|
140
|
-
const { replacePlaceholder, placeholderExpression, variableName } = this._options;
|
|
141
|
-
const htmlObject = JSON.stringify(values);
|
|
142
|
-
let code = template.replace(
|
|
143
|
-
replacePlaceholder,
|
|
144
|
-
`window.${variableName} = ${htmlObject}`
|
|
145
|
-
);
|
|
146
|
-
const matches = [];
|
|
147
|
-
let match = placeholderExpression.exec(code);
|
|
148
|
-
while (match) {
|
|
149
|
-
const [string, value] = match;
|
|
150
|
-
if (string && value) {
|
|
151
|
-
matches.push({
|
|
152
|
-
string,
|
|
153
|
-
value
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
match = placeholderExpression.exec(code);
|
|
157
|
-
}
|
|
158
|
-
matches.forEach((info) => {
|
|
159
|
-
code = code.replace(info.string, String(get(values, info.value)));
|
|
160
|
-
});
|
|
161
|
-
return code;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const htmlGeneratorProvider = providerCreator(
|
|
165
|
-
(options = {}) => (app) => {
|
|
166
|
-
const {
|
|
167
|
-
serviceName = "htmlGenerator",
|
|
168
|
-
valuesServiceName = "htmlGeneratorValues",
|
|
169
|
-
...rest
|
|
170
|
-
} = options;
|
|
171
|
-
app.set(
|
|
172
|
-
serviceName,
|
|
173
|
-
() => new HTMLGenerator({
|
|
174
|
-
inject: {
|
|
175
|
-
config: app.get("config"),
|
|
176
|
-
logger: app.get("logger"),
|
|
177
|
-
frontendFs: app.get("frontendFs"),
|
|
178
|
-
valuesService: app.try(valuesServiceName)
|
|
179
|
-
},
|
|
180
|
-
...rest
|
|
181
|
-
})
|
|
182
|
-
);
|
|
183
|
-
app.once("afterStart", () => {
|
|
184
|
-
app.get(serviceName).generateHTML();
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
);
|
|
188
|
-
export {
|
|
189
|
-
HTMLGenerator,
|
|
190
|
-
htmlGeneratorProvider
|
|
191
|
-
};
|
|
192
|
-
//# sourceMappingURL=htmlGenerator.js.map
|