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
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
const ObjectUtils = require('wootils/shared/objectUtils');
|
|
2
|
-
const { code: statuses } = require('statuses');
|
|
3
|
-
const { middlewareCreator } = require('../../utils/wrappers');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {import('../../types').ExpressMiddleware} ExpressMiddleware
|
|
7
|
-
* @typedef {import('../../types').ExpressRequest} ExpressRequest
|
|
8
|
-
* @typedef {import('../../services/http/responsesBuilder').ResponsesBuilder} ResponsesBuilder
|
|
9
|
-
* @prettierignore
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {import('../../types').MiddlewareCreator<O>} MiddlewareCreator<O>
|
|
14
|
-
* @template O
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The options for how the middleware should behave if the requested version is `latest`.
|
|
19
|
-
*
|
|
20
|
-
* @typedef {Object} VersionValidatorLatestOptions
|
|
21
|
-
* @property {boolean} allow Whether or not the middleware should validate the _"latest
|
|
22
|
-
* version"_.
|
|
23
|
-
* Default `true`.
|
|
24
|
-
* @property {string} name The name of the _"latest version"_. Basically,
|
|
25
|
-
* `req.params.version`
|
|
26
|
-
* must match with this property in order to be consider
|
|
27
|
-
* "latest".
|
|
28
|
-
* Default `'latest'`.
|
|
29
|
-
* @parent module:middlewares
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The options for how to detect if the request comes from a popup and how to compose the
|
|
34
|
-
* post message the middleware will use to respond.
|
|
35
|
-
*
|
|
36
|
-
* @typedef {Object} VersionValidatorPopupOptions
|
|
37
|
-
* @property {string} variable The name of the query string variable the middleware will
|
|
38
|
-
* check in order to indentify whether the request comes from
|
|
39
|
-
* a popup or not.
|
|
40
|
-
* The variable must have `'true'` as its value. Default
|
|
41
|
-
* `'popup'`.
|
|
42
|
-
* @property {string} title The title of the page that will be generated to respond in
|
|
43
|
-
* case the versions don't match. Default `'Conflict'`.
|
|
44
|
-
* @property {string} message The contents of the post message the generated page will
|
|
45
|
-
* send if the versions don't match. Default
|
|
46
|
-
* `'vesion:conflict'`.
|
|
47
|
-
* @parent module:middlewares
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The options used to customize a {@link VersionValidator} instance.
|
|
52
|
-
*
|
|
53
|
-
* @typedef {Object} VersionValidatorOptions
|
|
54
|
-
* @property {string} error The error message to show when the
|
|
55
|
-
* version is invalid.
|
|
56
|
-
* @property {VersionValidatorLatestOptions} latest The options for how the middleware
|
|
57
|
-
* should behave if the requested
|
|
58
|
-
* version is `latest`.
|
|
59
|
-
* @property {VersionValidatorPopupOptions} popup The options for how to detect if the
|
|
60
|
-
* request comes from a popup and how
|
|
61
|
-
* to compose the post message the
|
|
62
|
-
* middleware will use to respond.
|
|
63
|
-
* @property {string | number} version The version used to validate the
|
|
64
|
-
* requests.
|
|
65
|
-
* On the {@link VersionValidator}
|
|
66
|
-
* constructor, if specified via
|
|
67
|
-
* parameter,
|
|
68
|
-
* the class will take care of
|
|
69
|
-
* automatically add it to the options.
|
|
70
|
-
* @parent module:middlewares
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* This is the handler for the middleware/controller that validates the app version.
|
|
75
|
-
* This is useful in cases where you want to restrict the access to the app to specific
|
|
76
|
-
* versions,
|
|
77
|
-
* for example: you have a frontend app which needs to be aligned with the "current"
|
|
78
|
-
* version of the app, since the frontend won't realize a new version was released, the
|
|
79
|
-
* validator can be used to let the frontend know.
|
|
80
|
-
* Also, it can be configured to handle requests from popups, in which case, instead of
|
|
81
|
-
* generating an error message, it will send a post message.
|
|
82
|
-
*
|
|
83
|
-
* @parent module:middlewares
|
|
84
|
-
*/
|
|
85
|
-
class VersionValidator {
|
|
86
|
-
/**
|
|
87
|
-
* @param {?(string | ?number)} version
|
|
88
|
-
* The current version of the app. The reason this is nullable is because this comes
|
|
89
|
-
* directly from the app configuration, but you may want to re use this to validate
|
|
90
|
-
* "another version", so you can use the custom shorthand and send the version using the
|
|
91
|
-
* `options` parameter.
|
|
92
|
-
* @param {ResponsesBuilder} responsesBuilder
|
|
93
|
-
* To generate post message responses for popups.
|
|
94
|
-
* @param {ClassAppError} AppError
|
|
95
|
-
* To generate the error in case the version is invalid.
|
|
96
|
-
* @param {Partial<VersionValidatorOptions>} [options={}]
|
|
97
|
-
* Custom options to modify the middleware behavior.
|
|
98
|
-
* @throws {Error}
|
|
99
|
-
* If the version is `null` and the `options` don't include one either.
|
|
100
|
-
*/
|
|
101
|
-
constructor(version, responsesBuilder, AppError, options = {}) {
|
|
102
|
-
/**
|
|
103
|
-
* A local reference for the `responsesBuilder` service.
|
|
104
|
-
*
|
|
105
|
-
* @type {ResponsesBuilder}
|
|
106
|
-
* @access protected
|
|
107
|
-
* @ignore
|
|
108
|
-
*/
|
|
109
|
-
this._responsesBuilder = responsesBuilder;
|
|
110
|
-
/**
|
|
111
|
-
* A local reference for the class the app uses to generate errors.
|
|
112
|
-
*
|
|
113
|
-
* @type {ClassAppError}
|
|
114
|
-
* @access protected
|
|
115
|
-
* @ignore
|
|
116
|
-
*/
|
|
117
|
-
this._AppError = AppError;
|
|
118
|
-
/**
|
|
119
|
-
* These are the "settings" the middleware will use in order to validate the requests.
|
|
120
|
-
*
|
|
121
|
-
* @type {VersionValidatorOptions}
|
|
122
|
-
* @access protected
|
|
123
|
-
* @ignore
|
|
124
|
-
*/
|
|
125
|
-
this._options = ObjectUtils.merge(
|
|
126
|
-
{
|
|
127
|
-
error: "The application version doesn't match",
|
|
128
|
-
latest: {
|
|
129
|
-
allow: true,
|
|
130
|
-
name: 'latest',
|
|
131
|
-
},
|
|
132
|
-
popup: {
|
|
133
|
-
variable: 'popup',
|
|
134
|
-
title: 'Conflict',
|
|
135
|
-
message: 'vesion:conflict',
|
|
136
|
-
},
|
|
137
|
-
version,
|
|
138
|
-
},
|
|
139
|
-
options,
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
if (!this._options.version) {
|
|
143
|
-
throw new Error('You need to supply a version');
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Returns the Express middleware that will validate the `version` parameter.
|
|
148
|
-
*
|
|
149
|
-
* @returns {ExpressMiddleware}
|
|
150
|
-
*/
|
|
151
|
-
middleware() {
|
|
152
|
-
return (req, res, next) => {
|
|
153
|
-
// Get the `version` parameter from the request.
|
|
154
|
-
const { version } = req.params;
|
|
155
|
-
if (!version) {
|
|
156
|
-
// If no version is present, move on to the next middleware.
|
|
157
|
-
next();
|
|
158
|
-
} else if (version === this._options.version || this._validateLatest(version)) {
|
|
159
|
-
/**
|
|
160
|
-
* If the version matches the one on the options, or the requested version is "latest"
|
|
161
|
-
* (and the option is enabled), move on to the next middleware.
|
|
162
|
-
*/
|
|
163
|
-
next();
|
|
164
|
-
} else if (this._isPopup(req)) {
|
|
165
|
-
/**
|
|
166
|
-
* If it doesn't match and the request is comming from a popup, send a response with a
|
|
167
|
-
* post message.
|
|
168
|
-
*/
|
|
169
|
-
this._responsesBuilder.htmlPostMessage(
|
|
170
|
-
res,
|
|
171
|
-
this._options.popup.title,
|
|
172
|
-
this._options.popup.message,
|
|
173
|
-
statuses.conflict,
|
|
174
|
-
);
|
|
175
|
-
} else {
|
|
176
|
-
// Finally, if it doesn't match and is not from a popup, move to the error handler.
|
|
177
|
-
next(
|
|
178
|
-
new this._AppError(this._options.error, {
|
|
179
|
-
status: statuses.conflict,
|
|
180
|
-
response: {
|
|
181
|
-
validation: true,
|
|
182
|
-
},
|
|
183
|
-
}),
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* The options used to customize the middleware behavior.
|
|
190
|
-
*
|
|
191
|
-
* @returns {VersionValidatorOptions}
|
|
192
|
-
*/
|
|
193
|
-
get options() {
|
|
194
|
-
return this._options;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Helper method that checks if the incoming request is from a popup.
|
|
198
|
-
*
|
|
199
|
-
* @param {ExpressRequest} req The request information.
|
|
200
|
-
* @returns {boolean}
|
|
201
|
-
* @access protected
|
|
202
|
-
* @ignore
|
|
203
|
-
*/
|
|
204
|
-
_isPopup(req) {
|
|
205
|
-
const popup = req.query[this._options.popup.variable];
|
|
206
|
-
return !!(popup && popup.toLowerCase() === 'true');
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Helper method that checks if the "latest version" is enabled and if the given version
|
|
210
|
-
* is "the latest" (comparing it with the option name).
|
|
211
|
-
*
|
|
212
|
-
* @param {string | number} version The version to validate.
|
|
213
|
-
* @returns {boolean}
|
|
214
|
-
* @access protected
|
|
215
|
-
* @ignore
|
|
216
|
-
*/
|
|
217
|
-
_validateLatest(version) {
|
|
218
|
-
return this._options.latest.allow && version === this._options.latest.name;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* A middleware that will validate a `version` request parameter against the app version
|
|
223
|
-
* and generate an error if they don't match.
|
|
224
|
-
* This is a "middleware/controller" is because the wrappers for both are the same, the
|
|
225
|
-
* difference is that, for controllers, Jimpex sends a second parameter with the route
|
|
226
|
-
* where they are mounted.
|
|
227
|
-
* By validating the route parameter, the function can know whether the implementation is
|
|
228
|
-
* going to use the middleware by itself or as a route middleware.
|
|
229
|
-
* If used as middleware, it will just return the result of
|
|
230
|
-
* {@link VersionValidator#middleware};
|
|
231
|
-
* but if used as controller, it will mount it on `[route]/:version/*`.
|
|
232
|
-
*
|
|
233
|
-
* @type {MiddlewareCreator<VersionValidatorOptions>}
|
|
234
|
-
* @parent module:middlewares
|
|
235
|
-
*/
|
|
236
|
-
const versionValidator = middlewareCreator((options = {}) => (app, route) => {
|
|
237
|
-
// Get the middleware function.
|
|
238
|
-
const middlewareValidator = new VersionValidator(
|
|
239
|
-
app.get('appConfiguration').get('version'),
|
|
240
|
-
app.get('responsesBuilder'),
|
|
241
|
-
app.get('AppError'),
|
|
242
|
-
options,
|
|
243
|
-
).middleware();
|
|
244
|
-
// Set the variable to be returned.
|
|
245
|
-
let result;
|
|
246
|
-
if (route) {
|
|
247
|
-
// If the implementation will use it as a router, get the `router` service and mount it.
|
|
248
|
-
const router = app.get('router');
|
|
249
|
-
// Set the array of "routes" as the return value.
|
|
250
|
-
result = [router.all('/:version/*', middlewareValidator)];
|
|
251
|
-
} else {
|
|
252
|
-
// If the implementation will use it stand alone, just set the function to be returned.
|
|
253
|
-
result = middlewareValidator;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Return the route or the middleware.
|
|
257
|
-
return result;
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
module.exports.VersionValidator = VersionValidator;
|
|
261
|
-
module.exports.versionValidator = versionValidator;
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
const { code: statuses } = require('statuses');
|
|
2
|
-
const { provider } = require('../../utils/wrappers');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @typedef {import('../../types').Provider} Provider
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A simple subclass of `Error` but with support for context information.
|
|
10
|
-
*
|
|
11
|
-
* @augments Error
|
|
12
|
-
* @parent module:services
|
|
13
|
-
*/
|
|
14
|
-
class AppError extends Error {
|
|
15
|
-
/**
|
|
16
|
-
* @param {string} message The error message.
|
|
17
|
-
* @param {Object} [context={}] Context information related to the error.
|
|
18
|
-
*/
|
|
19
|
-
constructor(message, context = {}) {
|
|
20
|
-
super(message);
|
|
21
|
-
|
|
22
|
-
// Limit the stack trace if possible.
|
|
23
|
-
if (Error.captureStackTrace) {
|
|
24
|
-
Error.captureStackTrace(this, this.constructor);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Context information related to the error.
|
|
28
|
-
*
|
|
29
|
-
* @type {Object}
|
|
30
|
-
* @access protected
|
|
31
|
-
* @todo Remove Object.freeze.
|
|
32
|
-
*/
|
|
33
|
-
this._context = Object.freeze(this._parseContext(context));
|
|
34
|
-
/**
|
|
35
|
-
* The date of when the error was generated.
|
|
36
|
-
*
|
|
37
|
-
* @type {Date}
|
|
38
|
-
* @access protected
|
|
39
|
-
*/
|
|
40
|
-
this._date = new Date();
|
|
41
|
-
/**
|
|
42
|
-
* Overwrite the name of the `Error` with the one from the class.
|
|
43
|
-
*
|
|
44
|
-
* @type {string}
|
|
45
|
-
* @ignore
|
|
46
|
-
*/
|
|
47
|
-
this.name = this.constructor.name;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Context information related to the error.
|
|
51
|
-
*
|
|
52
|
-
* @type {Object}
|
|
53
|
-
*/
|
|
54
|
-
get context() {
|
|
55
|
-
return this._context;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* The date of when the error was generated.
|
|
59
|
-
*
|
|
60
|
-
* @type {Date}
|
|
61
|
-
*/
|
|
62
|
-
get date() {
|
|
63
|
-
return this._date;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Information about the error that can be shown on an app response. This is set using
|
|
67
|
-
* the `response` key on the `context`. The idea is that the error handler will read it
|
|
68
|
-
* and use it on the response.
|
|
69
|
-
*
|
|
70
|
-
* @returns {Object}
|
|
71
|
-
*/
|
|
72
|
-
get response() {
|
|
73
|
-
return this._context.response || {};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* An HTTP status code related to the error. This is set using the `status` key on the
|
|
77
|
-
* `context`. If the error handler finds it, it will use it as the response status,
|
|
78
|
-
* and use it if necessary.
|
|
79
|
-
*
|
|
80
|
-
* @type {?number}
|
|
81
|
-
*/
|
|
82
|
-
get status() {
|
|
83
|
-
return this._context.status || null;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Utility method that formats the context before saving it in the instance:
|
|
87
|
-
* - If the context includes a `status` as a `string`, it will try to replace it with
|
|
88
|
-
* its status code from the `statuses` package.
|
|
89
|
-
*
|
|
90
|
-
* @param {Object} original The original context to format.
|
|
91
|
-
* @returns {Object}
|
|
92
|
-
* @access protected
|
|
93
|
-
* @ignore
|
|
94
|
-
*/
|
|
95
|
-
_parseContext(original) {
|
|
96
|
-
const result = { ...original };
|
|
97
|
-
if (typeof result.status === 'string') {
|
|
98
|
-
result.status = statuses[result.status.toLowerCase()] || result.status;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* A generator function to create {@link AppError} instances.
|
|
106
|
-
*
|
|
107
|
-
* @param {string} message The error message.
|
|
108
|
-
* @param {Object} [context] Context information related to the error.
|
|
109
|
-
* @returns {AppError}
|
|
110
|
-
* @parent module:services
|
|
111
|
-
*/
|
|
112
|
-
const appErrorGenerator = (message, context) => new AppError(message, context);
|
|
113
|
-
/**
|
|
114
|
-
* A service provider that will register both the {@link AppError} and a generator
|
|
115
|
-
* function on the container. `AppError` will be the key for class, and `appError` will be
|
|
116
|
-
* for the generator function.
|
|
117
|
-
*
|
|
118
|
-
* @type {Provider}
|
|
119
|
-
* @example
|
|
120
|
-
*
|
|
121
|
-
* // Register it on the container
|
|
122
|
-
* container.register(appError);
|
|
123
|
-
* // Getting access to the class.
|
|
124
|
-
* const AppError = container.get('AppError');
|
|
125
|
-
* // Getting access to the function.
|
|
126
|
-
* const appError = container.get('appError');
|
|
127
|
-
*
|
|
128
|
-
* @parent module:services
|
|
129
|
-
*/
|
|
130
|
-
const appError = provider((app) => {
|
|
131
|
-
app.set('AppError', () => AppError);
|
|
132
|
-
app.set('appError', () => appErrorGenerator);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
module.exports.AppError = AppError;
|
|
136
|
-
module.exports.appError = appError;
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const { code: statuses } = require('statuses');
|
|
2
|
-
const ObjectUtils = require('wootils/shared/objectUtils');
|
|
3
|
-
const { provider } = require('../../utils/wrappers');
|
|
4
|
-
const { AppError } = require('./appError');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {import('../../types').Provider} Provider
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A type of error to be used on HTTP requests.
|
|
12
|
-
*
|
|
13
|
-
* @augments AppError
|
|
14
|
-
* @parent module:services
|
|
15
|
-
*/
|
|
16
|
-
class HTTPError extends AppError {
|
|
17
|
-
/**
|
|
18
|
-
* @param {string} message The error message.
|
|
19
|
-
* @param {number} [status=200] The HTTP status code of the request response.
|
|
20
|
-
* @param {Object} [context={}] Context information related to the error.
|
|
21
|
-
*/
|
|
22
|
-
constructor(message, status = statuses.ok, context = {}) {
|
|
23
|
-
super(message, ObjectUtils.merge({ status }, context));
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* A generator function to create {@link HTTPError} instances.
|
|
28
|
-
*
|
|
29
|
-
* @param {string} message The error message.
|
|
30
|
-
* @param {number} [status] The HTTP status code of the request response.
|
|
31
|
-
* @param {Object} [context] Context information related to the error.
|
|
32
|
-
* @returns {HTTPError}
|
|
33
|
-
* @parent module:services
|
|
34
|
-
*/
|
|
35
|
-
const httpErrorGenerator = (message, status, context) =>
|
|
36
|
-
new HTTPError(message, status, context);
|
|
37
|
-
/**
|
|
38
|
-
* A service provider that will register both the {@link HTTPError} and a generator
|
|
39
|
-
* function on the container. `HTTPError` will be the key for class, and `httpError` will
|
|
40
|
-
* be for the generator function.
|
|
41
|
-
*
|
|
42
|
-
* @type {Provider}
|
|
43
|
-
* @example
|
|
44
|
-
*
|
|
45
|
-
* // Register it on the container
|
|
46
|
-
* container.register(httpError);
|
|
47
|
-
* // Getting access to the class.
|
|
48
|
-
* const HTTPError = container.get('HTTPError');
|
|
49
|
-
* // Getting access to the function.
|
|
50
|
-
* const httpError = container.get('httpError');
|
|
51
|
-
*
|
|
52
|
-
* @parent module:services
|
|
53
|
-
*/
|
|
54
|
-
const httpError = provider((app) => {
|
|
55
|
-
app.set('HTTPError', () => HTTPError);
|
|
56
|
-
app.set('httpError', () => httpErrorGenerator);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
module.exports.HTTPError = HTTPError;
|
|
60
|
-
module.exports.httpError = httpError;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const { appError } = require('./appError');
|
|
2
|
-
const { httpError } = require('./httpError');
|
|
3
|
-
const { sendFileProvider } = require('./sendFile');
|
|
4
|
-
const { providers } = require('../../utils/wrappers');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {import('../../types').Provider} Provider
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* The providers collection for the common services.
|
|
12
|
-
*
|
|
13
|
-
* @type {Provider}
|
|
14
|
-
* @property {Provider} appError The provider for {@link AppError}.
|
|
15
|
-
* @property {Provider} httpError The provider for {@link HTTPError}.
|
|
16
|
-
* @property {Provider} sendFile The provider for {@link SendFile}.
|
|
17
|
-
* @parent module:services
|
|
18
|
-
*/
|
|
19
|
-
const commonServices = providers({
|
|
20
|
-
appError,
|
|
21
|
-
httpError,
|
|
22
|
-
sendFile: sendFileProvider,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
module.exports = commonServices;
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
const { provider } = require('../../utils/wrappers');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {import('../../types').PathUtils} PathUtils
|
|
5
|
-
* @typedef {import('../../types').Provider} Provider
|
|
6
|
-
* @typedef {import('../../types').ExpressResponse} ExpressResponse
|
|
7
|
-
* @typedef {import('../../types').ExpressNext} ExpressNext
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @callback SendFile
|
|
12
|
-
* @param {ExpressResponse} res Necessary to write the file.
|
|
13
|
-
* @param {string} filepath The path to the file relative to where the app
|
|
14
|
-
* executable is located.
|
|
15
|
-
* @param {ExpressNext} next To call the send out the error if anything
|
|
16
|
-
* happens.
|
|
17
|
-
* @param {string} [from='app'] The location it uses to build the relative path.
|
|
18
|
-
* It can be `app` for the directory where the app
|
|
19
|
-
* executable is located, or `home` for the project
|
|
20
|
-
* root directory.
|
|
21
|
-
* @parent module:services
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Generates a function to send files with path relatives to the app executable file is
|
|
26
|
-
* located.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
*
|
|
30
|
-
* // Let's say this is inside an Express middleware.
|
|
31
|
-
* // Get the function
|
|
32
|
-
* const send = sendFile(pathUtils);
|
|
33
|
-
* send(res, 'some-file.html', next);
|
|
34
|
-
* // If your app is on `/app/index.js`, this will send `/app/some-file.html`.
|
|
35
|
-
*
|
|
36
|
-
* @param {PathUtils} pathUtils To generate the relative paths.
|
|
37
|
-
* @returns {SendFile}
|
|
38
|
-
* @parent module:services
|
|
39
|
-
*/
|
|
40
|
-
const sendFile = (pathUtils) => (res, filepath, next = () => {}, from = 'app') => {
|
|
41
|
-
res.sendFile(pathUtils.joinFrom(from, filepath), (error) => {
|
|
42
|
-
if (error) {
|
|
43
|
-
next(error);
|
|
44
|
-
} else {
|
|
45
|
-
res.end();
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* The service provider that once registered on the app container will set the result of
|
|
51
|
-
* `sendFile(pathUtils)` as the `sendFile` service.
|
|
52
|
-
*
|
|
53
|
-
* @type {Provider}
|
|
54
|
-
* @example
|
|
55
|
-
*
|
|
56
|
-
* // Register it on the container
|
|
57
|
-
* container.register(sendFileProvider);
|
|
58
|
-
* // Getting access to the service instance
|
|
59
|
-
* const sendFile = container.get('sendFile');
|
|
60
|
-
*
|
|
61
|
-
* @parent module:services
|
|
62
|
-
*/
|
|
63
|
-
const sendFileProvider = provider((app) => {
|
|
64
|
-
app.set('sendFile', () => sendFile(app.get('pathUtils')));
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
module.exports.sendFile = sendFile;
|
|
68
|
-
module.exports.sendFileProvider = sendFileProvider;
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
const fs = require('fs-extra');
|
|
2
|
-
const { provider } = require('../../utils/wrappers');
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {import('../../types').PathUtils} PathUtils
|
|
5
|
-
* @typedef {import('../../types').Provider} Provider
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* This service allows the app to easily read static files. The idea behind centralizing
|
|
10
|
-
* this functionalities into a service is that is pretty common to have bundling tools to
|
|
11
|
-
* generate the frontend, and on that process files can have different paths or not even
|
|
12
|
-
* be generated all, that's why this service exists. The service can be
|
|
13
|
-
* extended/overwritten to accommodate any requirements and avoid having to update or add
|
|
14
|
-
* `if`s to every `fs` call the app does.
|
|
15
|
-
* Another _'feature'_ of this service is that all the paths are relative to the directory
|
|
16
|
-
* where the app executable is located, so you don't have to remember the relative path
|
|
17
|
-
* from the place you are accessing a file to the place where it's located.
|
|
18
|
-
*
|
|
19
|
-
* @parent module:services
|
|
20
|
-
*/
|
|
21
|
-
class FrontendFs {
|
|
22
|
-
/**
|
|
23
|
-
* @param {PathUtils} pathUtils To generate the relative paths.
|
|
24
|
-
*/
|
|
25
|
-
constructor(pathUtils) {
|
|
26
|
-
/**
|
|
27
|
-
* A local reference for the `pathUtils` service.
|
|
28
|
-
*
|
|
29
|
-
* @type {PathUtils}
|
|
30
|
-
* @access protected
|
|
31
|
-
* @ignore
|
|
32
|
-
*/
|
|
33
|
-
this._pathUtils = pathUtils;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Delete a file from the file system.
|
|
37
|
-
*
|
|
38
|
-
* @param {string} filepath The path to the file.
|
|
39
|
-
* @returns {Promise}
|
|
40
|
-
*/
|
|
41
|
-
delete(filepath) {
|
|
42
|
-
return fs.unlink(this._pathUtils.joinFrom('app', filepath));
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Read a file from the file system.
|
|
46
|
-
*
|
|
47
|
-
* @param {string} filepath The path to the file.
|
|
48
|
-
* @param {string} [encoding='utf-8'] The text encoding in which the file should be
|
|
49
|
-
* read.
|
|
50
|
-
* @returns {Promise<string>}
|
|
51
|
-
*/
|
|
52
|
-
read(filepath, encoding = 'utf-8') {
|
|
53
|
-
return fs.readFile(this._pathUtils.joinFrom('app', filepath), encoding);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Write a file on the file system.
|
|
57
|
-
*
|
|
58
|
-
* @param {string} filepath The path to the file.
|
|
59
|
-
* @param {string} data The contents of the file.
|
|
60
|
-
* @returns {Promise}
|
|
61
|
-
*/
|
|
62
|
-
write(filepath, data) {
|
|
63
|
-
return fs.writeFile(this._pathUtils.joinFrom('app', filepath), data);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* The service provider that once registered on the app container will set an instance of
|
|
68
|
-
* `FrontendFs` as the `frontendFs` service.
|
|
69
|
-
*
|
|
70
|
-
* @type {Provider}
|
|
71
|
-
* @example
|
|
72
|
-
*
|
|
73
|
-
* // Register it on the container
|
|
74
|
-
* container.register(frontendFs);
|
|
75
|
-
* // Getting access to the service instance
|
|
76
|
-
* const frontendFs = container.get('frontendFs');
|
|
77
|
-
*
|
|
78
|
-
* @parent module:services
|
|
79
|
-
*/
|
|
80
|
-
const frontendFs = provider((app) => {
|
|
81
|
-
app.set('frontendFs', () => new FrontendFs(app.get('pathUtils')));
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
module.exports.FrontendFs = FrontendFs;
|
|
85
|
-
module.exports.frontendFs = frontendFs;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const { providers } = require('../../utils/wrappers');
|
|
2
|
-
const { frontendFs } = require('./frontendFs');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @typedef {import('../../types').Provider} Provider
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The providers collection for the frontend services.
|
|
10
|
-
*
|
|
11
|
-
* @type {Provider}
|
|
12
|
-
* @property {Provider} frontendFs The default provider for {@link FrontendFs}.
|
|
13
|
-
* @parent module:services
|
|
14
|
-
*/
|
|
15
|
-
module.exports = providers({
|
|
16
|
-
frontendFs,
|
|
17
|
-
});
|