jimpex 7.0.2 → 9.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 +93 -0
- package/LICENSE +1 -1
- package/README.md +447 -435
- package/dist/app/index.d.mts +14 -0
- package/dist/app/index.d.ts +14 -0
- package/dist/app/index.js +23 -0
- package/dist/app/index.js.map +1 -0
- package/dist/app/jimpex.d.mts +14 -0
- package/dist/app/jimpex.d.ts +14 -0
- package/dist/app/jimpex.js +665 -0
- package/dist/app/jimpex.js.map +1 -0
- package/dist/controllers/common/config.d.mts +79 -0
- package/dist/controllers/common/config.d.ts +79 -0
- package/dist/controllers/common/config.js +107 -0
- package/dist/controllers/common/config.js.map +1 -0
- package/dist/controllers/common/health.d.mts +83 -0
- package/dist/controllers/common/health.d.ts +83 -0
- package/dist/controllers/common/health.js +115 -0
- package/dist/controllers/common/health.js.map +1 -0
- package/dist/controllers/common/index.d.mts +21 -0
- package/dist/controllers/common/index.d.ts +21 -0
- package/dist/controllers/common/index.js +27 -0
- package/dist/controllers/common/index.js.map +1 -0
- package/dist/controllers/common/statics.d.mts +215 -0
- package/dist/controllers/common/statics.d.ts +215 -0
- package/dist/controllers/common/statics.js +253 -0
- package/dist/controllers/common/statics.js.map +1 -0
- package/dist/controllers/index.d.mts +24 -0
- package/dist/controllers/index.d.ts +24 -0
- package/dist/controllers/index.js +25 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/controllers/utils/gateway.d.mts +725 -0
- package/dist/controllers/utils/gateway.d.ts +725 -0
- package/dist/controllers/utils/gateway.js +577 -0
- package/dist/controllers/utils/gateway.js.map +1 -0
- package/dist/controllers/utils/index.d.mts +17 -0
- package/dist/controllers/utils/index.d.ts +17 -0
- package/dist/controllers/utils/index.js +23 -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 +636 -0
- package/dist/esm/app/jimpex.js.map +1 -0
- package/dist/esm/chunk-2B2CG5KL.js +11 -0
- package/dist/esm/chunk-2B2CG5KL.js.map +1 -0
- package/dist/esm/controllers/common/config.js +80 -0
- package/dist/esm/controllers/common/config.js.map +1 -0
- package/dist/esm/controllers/common/health.js +88 -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 +220 -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 +556 -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 +116 -0
- package/dist/esm/middlewares/common/errorHandler.js.map +1 -0
- package/dist/esm/middlewares/common/forceHTTPS.js +53 -0
- package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -0
- package/dist/esm/middlewares/common/hsts.js +74 -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 +158 -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 +100 -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 +135 -0
- package/dist/esm/middlewares/utils/versionValidator.js.map +1 -0
- package/dist/esm/services/common/appError.js +98 -0
- package/dist/esm/services/common/appError.js.map +1 -0
- package/dist/esm/services/common/httpError.js +28 -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 +66 -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 +194 -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 +97 -0
- package/dist/esm/services/http/apiClient.js.map +1 -0
- package/dist/esm/services/http/http.js +174 -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 +152 -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 +94 -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-b2a04c78.d.ts +1282 -0
- package/dist/index-efeb437e.d.ts +1282 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/middlewares/common/errorHandler.d.mts +132 -0
- package/dist/middlewares/common/errorHandler.d.ts +132 -0
- package/dist/middlewares/common/errorHandler.js +143 -0
- package/dist/middlewares/common/errorHandler.js.map +1 -0
- package/dist/middlewares/common/forceHTTPS.d.mts +69 -0
- package/dist/middlewares/common/forceHTTPS.d.ts +69 -0
- package/dist/middlewares/common/forceHTTPS.js +80 -0
- package/dist/middlewares/common/forceHTTPS.js.map +1 -0
- package/dist/middlewares/common/hsts.d.mts +110 -0
- package/dist/middlewares/common/hsts.d.ts +110 -0
- package/dist/middlewares/common/hsts.js +101 -0
- package/dist/middlewares/common/hsts.js.map +1 -0
- package/dist/middlewares/common/index.d.mts +22 -0
- package/dist/middlewares/common/index.d.ts +22 -0
- package/dist/middlewares/common/index.js +27 -0
- package/dist/middlewares/common/index.js.map +1 -0
- package/dist/middlewares/html/fastHTML.d.mts +180 -0
- package/dist/middlewares/html/fastHTML.d.ts +180 -0
- package/dist/middlewares/html/fastHTML.js +185 -0
- package/dist/middlewares/html/fastHTML.js.map +1 -0
- package/dist/middlewares/html/index.d.mts +21 -0
- package/dist/middlewares/html/index.d.ts +21 -0
- package/dist/middlewares/html/index.js +25 -0
- package/dist/middlewares/html/index.js.map +1 -0
- package/dist/middlewares/html/showHTML.d.mts +127 -0
- package/dist/middlewares/html/showHTML.d.ts +127 -0
- package/dist/middlewares/html/showHTML.js +127 -0
- package/dist/middlewares/html/showHTML.js.map +1 -0
- package/dist/middlewares/index.d.mts +30 -0
- package/dist/middlewares/index.d.ts +30 -0
- package/dist/middlewares/index.js +27 -0
- package/dist/middlewares/index.js.map +1 -0
- package/dist/middlewares/utils/index.d.mts +20 -0
- package/dist/middlewares/utils/index.d.ts +20 -0
- package/dist/middlewares/utils/index.js +23 -0
- package/dist/middlewares/utils/index.js.map +1 -0
- package/dist/middlewares/utils/versionValidator.d.mts +215 -0
- package/dist/middlewares/utils/versionValidator.d.ts +215 -0
- package/dist/middlewares/utils/versionValidator.js +162 -0
- package/dist/middlewares/utils/versionValidator.js.map +1 -0
- package/dist/services/common/appError.d.mts +139 -0
- package/dist/services/common/appError.d.ts +139 -0
- package/dist/services/common/appError.js +126 -0
- package/dist/services/common/appError.js.map +1 -0
- package/dist/services/common/httpError.d.mts +80 -0
- package/dist/services/common/httpError.d.ts +80 -0
- package/dist/services/common/httpError.js +53 -0
- package/dist/services/common/httpError.js.map +1 -0
- package/dist/services/common/index.d.mts +47 -0
- package/dist/services/common/index.d.ts +47 -0
- package/dist/services/common/index.js +44 -0
- package/dist/services/common/index.js.map +1 -0
- package/dist/services/common/sendFile.d.mts +102 -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.mts +96 -0
- package/dist/services/frontend/frontendFs.d.ts +96 -0
- package/dist/services/frontend/frontendFs.js +103 -0
- package/dist/services/frontend/frontendFs.js.map +1 -0
- package/dist/services/frontend/index.d.mts +40 -0
- package/dist/services/frontend/index.d.ts +40 -0
- package/dist/services/frontend/index.js +36 -0
- package/dist/services/frontend/index.js.map +1 -0
- package/dist/services/html/htmlGenerator.d.mts +237 -0
- package/dist/services/html/htmlGenerator.d.ts +237 -0
- package/dist/services/html/htmlGenerator.js +221 -0
- package/dist/services/html/htmlGenerator.js.map +1 -0
- package/dist/services/html/index.d.mts +43 -0
- package/dist/services/html/index.d.ts +43 -0
- package/dist/services/html/index.js +36 -0
- package/dist/services/html/index.js.map +1 -0
- package/dist/services/http/apiClient.d.mts +170 -0
- package/dist/services/http/apiClient.d.ts +170 -0
- package/dist/services/http/apiClient.js +122 -0
- package/dist/services/http/apiClient.js.map +1 -0
- package/dist/services/http/http.d.mts +176 -0
- package/dist/services/http/http.d.ts +176 -0
- package/dist/services/http/http.js +211 -0
- package/dist/services/http/http.js.map +1 -0
- package/dist/services/http/index.d.mts +51 -0
- package/dist/services/http/index.d.ts +51 -0
- package/dist/services/http/index.js +44 -0
- package/dist/services/http/index.js.map +1 -0
- package/dist/services/http/responsesBuilder.d.mts +179 -0
- package/dist/services/http/responsesBuilder.d.ts +179 -0
- package/dist/services/http/responsesBuilder.js +179 -0
- package/dist/services/http/responsesBuilder.js.map +1 -0
- package/dist/services/index.d.mts +33 -0
- package/dist/services/index.d.ts +33 -0
- package/dist/services/index.js +31 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/utils/ensureBearerToken.d.mts +158 -0
- package/dist/services/utils/ensureBearerToken.d.ts +158 -0
- package/dist/services/utils/ensureBearerToken.js +121 -0
- package/dist/services/utils/ensureBearerToken.js.map +1 -0
- package/dist/services/utils/index.d.mts +44 -0
- package/dist/services/utils/index.d.ts +44 -0
- package/dist/services/utils/index.js +36 -0
- package/dist/services/utils/index.js.map +1 -0
- package/dist/types/events.d.mts +14 -0
- package/dist/types/events.d.ts +14 -0
- package/dist/types/events.js +17 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/express.d.mts +10 -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.mts +79 -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.mts +14 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.js +33 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/options.d.mts +14 -0
- package/dist/types/options.d.ts +14 -0
- package/dist/types/options.js +17 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/utils.d.mts +48 -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.mts +4 -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.mts +5 -0
- package/dist/utils/fns/index.d.ts +5 -0
- package/dist/utils/fns/index.js +29 -0
- package/dist/utils/fns/index.js.map +1 -0
- package/dist/utils/fns/others.d.mts +17 -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.mts +39 -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.mts +45 -0
- package/dist/utils/fns/statuses.d.ts +45 -0
- package/dist/utils/fns/statuses.js +39 -0
- package/dist/utils/fns/statuses.js.map +1 -0
- package/dist/utils/fns/text.d.mts +9 -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.mts +19 -0
- package/dist/utils/index.d.ts +19 -0
- package/dist/utils/index.js +25 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/wrappers.d.mts +14 -0
- package/dist/utils/wrappers.d.ts +14 -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 +60 -39
- package/src/app/index.ts +1 -0
- package/src/app/jimpex.ts +743 -0
- package/src/controllers/common/config.ts +115 -0
- package/src/controllers/common/health.ts +128 -0
- package/src/controllers/common/index.ts +3 -0
- package/src/controllers/common/statics.ts +380 -0
- package/src/controllers/index.ts +2 -0
- package/src/controllers/utils/gateway.ts +1186 -0
- package/src/controllers/utils/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/middlewares/common/errorHandler.ts +203 -0
- package/src/middlewares/common/forceHTTPS.ts +83 -0
- package/src/middlewares/common/hsts.ts +135 -0
- package/src/middlewares/common/index.ts +3 -0
- package/src/middlewares/html/fastHTML.ts +255 -0
- package/src/middlewares/html/index.ts +2 -0
- package/src/middlewares/html/showHTML.ts +165 -0
- package/src/middlewares/index.ts +3 -0
- package/src/middlewares/utils/index.ts +1 -0
- package/src/middlewares/utils/versionValidator.ts +289 -0
- package/src/services/common/appError.ts +158 -0
- package/src/services/common/httpError.ts +74 -0
- package/src/services/common/index.ts +29 -0
- package/src/services/common/sendFile.ts +106 -0
- package/src/services/frontend/frontendFs.ts +101 -0
- package/src/services/frontend/index.ts +21 -0
- package/src/services/html/htmlGenerator.ts +356 -0
- package/src/services/html/index.ts +21 -0
- package/src/services/http/apiClient.ts +221 -0
- package/src/services/http/http.ts +286 -0
- package/src/services/http/index.ts +29 -0
- package/src/services/http/responsesBuilder.ts +265 -0
- package/src/services/index.ts +5 -0
- package/src/services/utils/ensureBearerToken.ts +202 -0
- package/src/services/utils/index.ts +21 -0
- package/src/types/events.ts +303 -0
- package/src/types/express.ts +21 -0
- package/src/types/http.ts +77 -0
- package/src/types/index.ts +6 -0
- package/src/types/options.ts +248 -0
- package/src/types/utils.ts +52 -0
- package/src/types/wootils.ts +4 -0
- package/src/utils/fns/index.ts +4 -0
- package/src/utils/fns/others.ts +15 -0
- package/src/utils/fns/routes.ts +64 -0
- package/src/utils/fns/statuses.ts +44 -0
- package/src/utils/fns/text.ts +8 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/wrappers.ts +537 -0
- package/tsconfig.json +5 -8
- package/tsup.config.ts +10 -0
- package/src/app/index.js +0 -692
- package/src/constants/eventNames.js +0 -48
- package/src/constants/index.js +0 -7
- package/src/controllers/common/configuration.js +0 -116
- package/src/controllers/common/health.js +0 -79
- package/src/controllers/common/index.js +0 -7
- package/src/controllers/common/statics.js +0 -336
- package/src/controllers/index.js +0 -9
- package/src/controllers/utils/gateway.js +0 -1039
- package/src/controllers/utils/index.js +0 -3
- package/src/index.js +0 -30
- package/src/middlewares/common/errorHandler.js +0 -185
- package/src/middlewares/common/forceHTTPS.js +0 -80
- package/src/middlewares/common/hsts.js +0 -122
- package/src/middlewares/common/index.js +0 -7
- package/src/middlewares/html/fastHTML.js +0 -298
- package/src/middlewares/html/index.js +0 -5
- package/src/middlewares/html/showHTML.js +0 -167
- package/src/middlewares/index.js +0 -11
- package/src/middlewares/utils/index.js +0 -3
- package/src/middlewares/utils/versionValidator.js +0 -261
- package/src/services/common/appError.js +0 -136
- package/src/services/common/httpError.js +0 -60
- package/src/services/common/index.js +0 -25
- package/src/services/common/sendFile.js +0 -68
- package/src/services/frontend/frontendFs.js +0 -85
- package/src/services/frontend/index.js +0 -17
- package/src/services/html/htmlGenerator.js +0 -391
- package/src/services/html/index.js +0 -17
- package/src/services/http/apiClient.js +0 -148
- package/src/services/http/http.js +0 -256
- package/src/services/http/index.js +0 -25
- package/src/services/http/responsesBuilder.js +0 -193
- package/src/services/index.js +0 -15
- package/src/services/utils/ensureBearerToken.js +0 -147
- package/src/services/utils/index.js +0 -19
- package/src/types.js +0 -377
- package/src/utils/functions.js +0 -78
- package/src/utils/wrappers.js +0 -131
- package/types/app/index.d.ts +0 -417
- package/types/constants/eventNames.d.ts +0 -93
- package/types/constants/index.d.ts +0 -2
- package/types/controllers/common/configuration.d.ts +0 -71
- package/types/controllers/common/health.d.ts +0 -55
- package/types/controllers/common/index.d.ts +0 -4
- package/types/controllers/common/statics.d.ts +0 -271
- package/types/controllers/index.d.ts +0 -3
- package/types/controllers/utils/gateway.d.ts +0 -947
- package/types/controllers/utils/index.d.ts +0 -2
- package/types/index.d.ts +0 -15
- package/types/middlewares/common/errorHandler.d.ts +0 -143
- package/types/middlewares/common/forceHTTPS.d.ts +0 -64
- package/types/middlewares/common/hsts.d.ts +0 -111
- package/types/middlewares/common/index.d.ts +0 -4
- package/types/middlewares/html/fastHTML.d.ts +0 -238
- package/types/middlewares/html/index.d.ts +0 -3
- package/types/middlewares/html/showHTML.d.ts +0 -128
- package/types/middlewares/index.d.ts +0 -4
- package/types/middlewares/utils/index.d.ts +0 -2
- package/types/middlewares/utils/versionValidator.d.ts +0 -247
- package/types/services/common/appError.d.ts +0 -89
- package/types/services/common/httpError.d.ts +0 -37
- package/types/services/common/index.d.ts +0 -18
- package/types/services/common/sendFile.d.ts +0 -56
- package/types/services/frontend/frontendFs.d.ts +0 -72
- package/types/services/frontend/index.d.ts +0 -3
- package/types/services/html/htmlGenerator.d.ts +0 -298
- package/types/services/html/index.d.ts +0 -3
- package/types/services/http/apiClient.d.ts +0 -141
- package/types/services/http/http.d.ts +0 -159
- package/types/services/http/index.d.ts +0 -18
- package/types/services/http/responsesBuilder.d.ts +0 -140
- package/types/services/index.d.ts +0 -6
- package/types/services/utils/ensureBearerToken.d.ts +0 -137
- package/types/services/utils/index.d.ts +0 -16
- package/types/types.d.ts +0 -280
- package/types/utils/functions.d.ts +0 -55
- package/types/utils/wrappers.d.ts +0 -127
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { controller } from '../../utils';
|
|
2
|
+
import type { ResponsesBuilder } from '../../services';
|
|
3
|
+
import type {
|
|
4
|
+
Config,
|
|
5
|
+
Response,
|
|
6
|
+
AsyncExpressMiddleware,
|
|
7
|
+
ExpressMiddleware,
|
|
8
|
+
} from '../../types';
|
|
9
|
+
/**
|
|
10
|
+
* The options to contruct a {@link ConfigController}.
|
|
11
|
+
*
|
|
12
|
+
* @group Controllers/Config
|
|
13
|
+
*/
|
|
14
|
+
export type ConfigControllerOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* A dictionary with the dependencies to inject.
|
|
17
|
+
*/
|
|
18
|
+
inject: {
|
|
19
|
+
responsesBuilder: ResponsesBuilder;
|
|
20
|
+
config: Config;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The controller class that allows to show and switch the application configuration.
|
|
25
|
+
*
|
|
26
|
+
* @group Controller Classes
|
|
27
|
+
* @group Controllers/Config
|
|
28
|
+
* @prettierignore
|
|
29
|
+
*/
|
|
30
|
+
export class ConfigController {
|
|
31
|
+
/**
|
|
32
|
+
* The service in charge or building the responses.
|
|
33
|
+
*/
|
|
34
|
+
protected readonly _responsesBuilder: ResponsesBuilder;
|
|
35
|
+
/**
|
|
36
|
+
* The service in charge of the configuration.
|
|
37
|
+
*/
|
|
38
|
+
protected readonly _config: Config;
|
|
39
|
+
/**
|
|
40
|
+
* @param options The options to construct the controller.
|
|
41
|
+
*/
|
|
42
|
+
constructor({ inject }: ConfigControllerOptions) {
|
|
43
|
+
this._responsesBuilder = inject.responsesBuilder;
|
|
44
|
+
this._config = inject.config;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates the middleware the shows the current configuration.
|
|
48
|
+
*/
|
|
49
|
+
showConfig(): ExpressMiddleware {
|
|
50
|
+
return (_, res) => {
|
|
51
|
+
this.respondWithConfig(res);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates the middleware the allows to switch the configuration.
|
|
56
|
+
*/
|
|
57
|
+
switchConfig(): AsyncExpressMiddleware {
|
|
58
|
+
return async (req, res, next) => {
|
|
59
|
+
const { name } = req.params;
|
|
60
|
+
if (!name || !this._config.canSwitchConfigs()) {
|
|
61
|
+
next();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
await this._config.switch(name);
|
|
67
|
+
this.respondWithConfig(res);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
next(error);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Utility to respond with the current configuration.
|
|
75
|
+
*/
|
|
76
|
+
protected respondWithConfig(res: Response): void {
|
|
77
|
+
const name = this._config.get<string>('name');
|
|
78
|
+
const data = {
|
|
79
|
+
name,
|
|
80
|
+
...this._config.getConfig<Record<string, unknown>>(),
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
this._responsesBuilder.json({
|
|
84
|
+
res,
|
|
85
|
+
data,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* This controller is kind of special as it will only mount the routes if the
|
|
91
|
+
* `debug.configController` setting of the app configuration is set to `true`.
|
|
92
|
+
*
|
|
93
|
+
* It provides routes for:
|
|
94
|
+
* - `/`: Showing the current configuration.
|
|
95
|
+
* - `/switch/:name`: Switching the configuration, but only if the service allows it.
|
|
96
|
+
*
|
|
97
|
+
* @group Controllers
|
|
98
|
+
* @group Controllers/Config
|
|
99
|
+
*/
|
|
100
|
+
export const configController = controller((app) => {
|
|
101
|
+
const config = app.getConfig();
|
|
102
|
+
const router = app.getRouter();
|
|
103
|
+
if (config.get<boolean | undefined>('debug.configController') !== true) {
|
|
104
|
+
return router;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const ctrl = new ConfigController({
|
|
108
|
+
inject: {
|
|
109
|
+
config,
|
|
110
|
+
responsesBuilder: app.get('responsesBuilder'),
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return router.get('/', ctrl.showConfig()).get('/switch/:name', ctrl.switchConfig());
|
|
115
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { controller, type Statuses } from '../../utils';
|
|
2
|
+
import type { ResponsesBuilder } from '../../services';
|
|
3
|
+
import type { JimpexHealthStatus, Config, AsyncExpressMiddleware } from '../../types';
|
|
4
|
+
/**
|
|
5
|
+
* A function that will return the health status of the application.
|
|
6
|
+
*
|
|
7
|
+
* @group Controllers/Health
|
|
8
|
+
*/
|
|
9
|
+
export type GetHealthStatus = () => Promise<JimpexHealthStatus>;
|
|
10
|
+
/**
|
|
11
|
+
* The options to contruct a {@link HealthController}.
|
|
12
|
+
*
|
|
13
|
+
* @group Controllers/Health
|
|
14
|
+
*/
|
|
15
|
+
export type HealthControllerOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* A dictionary with the dependencies to inject.
|
|
18
|
+
*/
|
|
19
|
+
inject: {
|
|
20
|
+
getHealthStatus: GetHealthStatus;
|
|
21
|
+
responsesBuilder: ResponsesBuilder;
|
|
22
|
+
config: Config;
|
|
23
|
+
statuses: Statuses;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The controller class that shows the application health status.
|
|
28
|
+
*
|
|
29
|
+
* @group Controller Classes
|
|
30
|
+
* @group Controllers/Health
|
|
31
|
+
* @prettierignore
|
|
32
|
+
*/
|
|
33
|
+
export class HealthController {
|
|
34
|
+
/**
|
|
35
|
+
* The function that returns the health status of the application.
|
|
36
|
+
*/
|
|
37
|
+
protected readonly _getHealthStatus: GetHealthStatus;
|
|
38
|
+
/**
|
|
39
|
+
* The service in charge or building the responses.
|
|
40
|
+
*/
|
|
41
|
+
protected readonly _responsesBuilder: ResponsesBuilder;
|
|
42
|
+
/**
|
|
43
|
+
* The service in charge of the configuration.
|
|
44
|
+
*/
|
|
45
|
+
protected readonly _config: Config;
|
|
46
|
+
/**
|
|
47
|
+
* The uility service to get HTTP status codes.
|
|
48
|
+
*/
|
|
49
|
+
protected readonly _statuses: Statuses;
|
|
50
|
+
/**
|
|
51
|
+
* @param options The options to construct the controller.
|
|
52
|
+
*/
|
|
53
|
+
constructor({ inject }: HealthControllerOptions) {
|
|
54
|
+
this._getHealthStatus = inject.getHealthStatus;
|
|
55
|
+
this._responsesBuilder = inject.responsesBuilder;
|
|
56
|
+
this._config = inject.config;
|
|
57
|
+
this._statuses = inject.statuses;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates the middleware the shows the application health status.
|
|
61
|
+
*/
|
|
62
|
+
showHealth(): AsyncExpressMiddleware {
|
|
63
|
+
return async (_, res) => {
|
|
64
|
+
const healthStatus = await this._getHealthStatus();
|
|
65
|
+
let isHealthy: boolean;
|
|
66
|
+
let extras: Record<string, unknown>;
|
|
67
|
+
if (typeof healthStatus === 'boolean') {
|
|
68
|
+
isHealthy = healthStatus;
|
|
69
|
+
extras = {};
|
|
70
|
+
} else {
|
|
71
|
+
if (typeof healthStatus.isHealthy === 'boolean') {
|
|
72
|
+
isHealthy = healthStatus.isHealthy;
|
|
73
|
+
} else if (healthStatus.services) {
|
|
74
|
+
isHealthy = Object.values(healthStatus.services).every(
|
|
75
|
+
(value) => value === true,
|
|
76
|
+
);
|
|
77
|
+
} else {
|
|
78
|
+
isHealthy = true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
extras = {
|
|
82
|
+
...healthStatus.services,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const { name: config, version } = this._config.get<{
|
|
87
|
+
name: string;
|
|
88
|
+
version: string;
|
|
89
|
+
}>(['name', 'version']);
|
|
90
|
+
|
|
91
|
+
const status = isHealthy
|
|
92
|
+
? this._statuses('ok')
|
|
93
|
+
: this._statuses('service unavailable');
|
|
94
|
+
|
|
95
|
+
this._responsesBuilder.json({
|
|
96
|
+
res,
|
|
97
|
+
status,
|
|
98
|
+
data: {
|
|
99
|
+
isHealthy,
|
|
100
|
+
status,
|
|
101
|
+
config,
|
|
102
|
+
version,
|
|
103
|
+
...extras,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The controller that once mounted, it will add an endpoint to show the application
|
|
111
|
+
* health status.
|
|
112
|
+
*
|
|
113
|
+
* @group Controllers
|
|
114
|
+
* @group Controllers/Health
|
|
115
|
+
*/
|
|
116
|
+
export const healthController = controller((app) => {
|
|
117
|
+
const router = app.getRouter();
|
|
118
|
+
const ctrl = new HealthController({
|
|
119
|
+
inject: {
|
|
120
|
+
getHealthStatus: app.isHealthy.bind(app),
|
|
121
|
+
responsesBuilder: app.get('responsesBuilder'),
|
|
122
|
+
config: app.get('config'),
|
|
123
|
+
statuses: app.get('statuses'),
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return router.get('/', ctrl.showHealth());
|
|
128
|
+
});
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as mime from 'mime';
|
|
3
|
+
import { deepAssignWithOverwrite } from '@homer0/deep-assign';
|
|
4
|
+
import {
|
|
5
|
+
controllerCreator,
|
|
6
|
+
removeSlashes,
|
|
7
|
+
notUndefined,
|
|
8
|
+
type MiddlewareLike,
|
|
9
|
+
} from '../../utils';
|
|
10
|
+
import type { SendFile } from '../../services';
|
|
11
|
+
import type { Jimpex } from '../../app';
|
|
12
|
+
import type { DeepPartial, ExpressMiddleware, Router, RouterMethod } from '../../types';
|
|
13
|
+
/**
|
|
14
|
+
* The definition for each file the controller handles.
|
|
15
|
+
*
|
|
16
|
+
* @group Controllers/Statics
|
|
17
|
+
*/
|
|
18
|
+
export type StaticsControllerFile = {
|
|
19
|
+
/**
|
|
20
|
+
* The route, relative to the controller root, to the file.
|
|
21
|
+
*/
|
|
22
|
+
route: string;
|
|
23
|
+
/**
|
|
24
|
+
* The path to the file in the filesystem. Since the file is served using the
|
|
25
|
+
* {@link SendFile} service, whether the file is relative to the project root or the
|
|
26
|
+
* application executable depends on how the service is configured (relative to the
|
|
27
|
+
* executable by default).
|
|
28
|
+
*/
|
|
29
|
+
path: string;
|
|
30
|
+
/**
|
|
31
|
+
* A dictionary of headers for the response.
|
|
32
|
+
*/
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* These are like "master paths" that get prepended to all the file paths and routes the
|
|
37
|
+
* controller use.
|
|
38
|
+
*
|
|
39
|
+
* @group Controllers/Statics
|
|
40
|
+
*/
|
|
41
|
+
export type StaticsControllerPathsOptions = {
|
|
42
|
+
/**
|
|
43
|
+
* A custom route to prefix all the file routes with.
|
|
44
|
+
*/
|
|
45
|
+
route: string;
|
|
46
|
+
/**
|
|
47
|
+
* A custom path to prefix all the file paths with.
|
|
48
|
+
*/
|
|
49
|
+
source: string;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The options to customize the controller.
|
|
53
|
+
*
|
|
54
|
+
* @group Controllers/Statics
|
|
55
|
+
*/
|
|
56
|
+
export type StaticsControllerOptions = {
|
|
57
|
+
/**
|
|
58
|
+
* A list of filenames, or definitions for the files to handle.
|
|
59
|
+
*/
|
|
60
|
+
files: Array<string | StaticsControllerFile>;
|
|
61
|
+
/**
|
|
62
|
+
* A dictionary with the allowed router (HTTP) methods the controller can use to serve
|
|
63
|
+
* the files. If `all` is set to `true`, the rest of the values will be ignored.
|
|
64
|
+
*
|
|
65
|
+
* @default {get: true, all: false}
|
|
66
|
+
*/
|
|
67
|
+
methods: Partial<Record<RouterMethod, boolean>>;
|
|
68
|
+
/**
|
|
69
|
+
* The "master paths" the controller can use to prefix the file paths and routes.
|
|
70
|
+
*
|
|
71
|
+
* @default {route: '', source: './',}
|
|
72
|
+
*/
|
|
73
|
+
paths: StaticsControllerPathsOptions;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* The options to construct a {@link StaticsController}.
|
|
77
|
+
*
|
|
78
|
+
* @group Controllers/Statics
|
|
79
|
+
*/
|
|
80
|
+
export type StaticsControllerConstructorOptions =
|
|
81
|
+
DeepPartial<StaticsControllerOptions> & {
|
|
82
|
+
/**
|
|
83
|
+
* A dictionary with the dependencies to inject.
|
|
84
|
+
*/
|
|
85
|
+
inject: {
|
|
86
|
+
sendFile: SendFile;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* A function to generate a list of middlewares that can be executed before the tontroller
|
|
91
|
+
* main middleware.
|
|
92
|
+
*
|
|
93
|
+
* @group Controllers/Statics
|
|
94
|
+
*/
|
|
95
|
+
export type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];
|
|
96
|
+
/**
|
|
97
|
+
* The options for the controller creator that mounts {@link StaticsController}.
|
|
98
|
+
*
|
|
99
|
+
* @group Controllers/Statics
|
|
100
|
+
*/
|
|
101
|
+
export type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {
|
|
102
|
+
/**
|
|
103
|
+
* A function to generate a list of middlewares that can be executed before the
|
|
104
|
+
* tontroller main middleware.
|
|
105
|
+
*/
|
|
106
|
+
getMiddlewares?: StaticsControllerGetMiddlewaresFn;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* The options for {@link StaticsController._addRoute}.
|
|
110
|
+
*
|
|
111
|
+
* @access protected
|
|
112
|
+
* @group Controllers/Statics
|
|
113
|
+
*/
|
|
114
|
+
export type AddStaticRouteOptions = {
|
|
115
|
+
/**
|
|
116
|
+
* The reference for the router in which the middlewares will be added.
|
|
117
|
+
*/
|
|
118
|
+
router: Router;
|
|
119
|
+
/**
|
|
120
|
+
* The router method in which the middlewares will be added.
|
|
121
|
+
*/
|
|
122
|
+
method: RouterMethod;
|
|
123
|
+
/**
|
|
124
|
+
* The definition of the file to serve.
|
|
125
|
+
*/
|
|
126
|
+
file: StaticsControllerFile;
|
|
127
|
+
/**
|
|
128
|
+
* The middleware created by {@link StaticsController}, that will serve the file.
|
|
129
|
+
*/
|
|
130
|
+
fileMiddleware: ExpressMiddleware;
|
|
131
|
+
/**
|
|
132
|
+
* A list of extra middlewares to execute before the file middleware.
|
|
133
|
+
*/
|
|
134
|
+
middlewares: ExpressMiddleware[];
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The controller class that allows the application to serve specific files from any
|
|
139
|
+
* folder to any route without the need of mounting directories as "static".
|
|
140
|
+
*
|
|
141
|
+
* @group Controller Classes
|
|
142
|
+
* @group Controllers/Statics
|
|
143
|
+
* @prettierignore
|
|
144
|
+
*/
|
|
145
|
+
export class StaticsController {
|
|
146
|
+
/**
|
|
147
|
+
* The service that serves static files.
|
|
148
|
+
*/
|
|
149
|
+
protected readonly _sendFile: SendFile;
|
|
150
|
+
/**
|
|
151
|
+
* The controller customization options.
|
|
152
|
+
*/
|
|
153
|
+
protected _options: StaticsControllerOptions;
|
|
154
|
+
/**
|
|
155
|
+
* A dictionary with the formatted definitions of the files that will be served.
|
|
156
|
+
* It uses the files' routes as keys, for easy access in the middleware.
|
|
157
|
+
*/
|
|
158
|
+
protected files: Record<string, StaticsControllerFile>;
|
|
159
|
+
/**
|
|
160
|
+
* @param options The options to construct the controller.
|
|
161
|
+
*/
|
|
162
|
+
constructor({ inject, ...options }: StaticsControllerConstructorOptions) {
|
|
163
|
+
this._sendFile = inject.sendFile;
|
|
164
|
+
this._options = this._validateOptions(
|
|
165
|
+
deepAssignWithOverwrite(
|
|
166
|
+
{
|
|
167
|
+
files: ['favicon.ico', 'index.html'],
|
|
168
|
+
methods: options.methods || {
|
|
169
|
+
all: false,
|
|
170
|
+
get: true,
|
|
171
|
+
},
|
|
172
|
+
paths: {
|
|
173
|
+
route: '',
|
|
174
|
+
source: './',
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
options,
|
|
178
|
+
),
|
|
179
|
+
);
|
|
180
|
+
this.files = this._createFiles();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Mounts the middlewares in the router in order to serve the files.
|
|
184
|
+
*
|
|
185
|
+
* @param router A reference to the application router.
|
|
186
|
+
* @param middlewares A list of extra middlewares to execute before the file
|
|
187
|
+
* middleware.
|
|
188
|
+
*/
|
|
189
|
+
addRoutes(router: Router, middlewares: ExpressMiddleware[] = []): Router {
|
|
190
|
+
const { methods } = this._options;
|
|
191
|
+
const use: RouterMethod[] = methods.all
|
|
192
|
+
? ['all']
|
|
193
|
+
: Object.keys(methods).reduce<RouterMethod[]>((acc, name) => {
|
|
194
|
+
const methodName = name as RouterMethod;
|
|
195
|
+
if (methods[methodName]) {
|
|
196
|
+
acc.push(methodName);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return acc;
|
|
200
|
+
}, []);
|
|
201
|
+
|
|
202
|
+
Object.keys(this.files).forEach((route) => {
|
|
203
|
+
const file = this.files[route as keyof typeof this.files]!;
|
|
204
|
+
const fileMiddleware = this._getMiddleware(file);
|
|
205
|
+
use.forEach((method) =>
|
|
206
|
+
this._addRoute({ router, method, file, fileMiddleware, middlewares }),
|
|
207
|
+
);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
return router;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The controller options.
|
|
214
|
+
*/
|
|
215
|
+
get options(): Readonly<StaticsControllerOptions> {
|
|
216
|
+
return { ...this._options };
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Generates the middleware that will serve the file.
|
|
220
|
+
*
|
|
221
|
+
* @param file The definition of the file to serve.
|
|
222
|
+
*/
|
|
223
|
+
protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware {
|
|
224
|
+
return (_, res, next) => {
|
|
225
|
+
const extension = path.parse(file.path).ext.substring(1);
|
|
226
|
+
const headers = {
|
|
227
|
+
'Content-Type': mime.getType(extension) || 'text/html',
|
|
228
|
+
...file.headers,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
232
|
+
res.setHeader(key, value);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
this._sendFile({
|
|
236
|
+
res,
|
|
237
|
+
filepath: file.path,
|
|
238
|
+
next,
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Mounts the middleware(s) for a file in the router.
|
|
244
|
+
*
|
|
245
|
+
* @param options The information of the file and how it needs to be added.
|
|
246
|
+
*/
|
|
247
|
+
protected _addRoute({
|
|
248
|
+
router,
|
|
249
|
+
method,
|
|
250
|
+
file,
|
|
251
|
+
fileMiddleware,
|
|
252
|
+
middlewares,
|
|
253
|
+
}: AddStaticRouteOptions): void {
|
|
254
|
+
const { route } = file;
|
|
255
|
+
router[method](route, [...middlewares, fileMiddleware]);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Validates and formats the options sent to the constructor in order to get the final
|
|
259
|
+
* set that will be stored in the controller.
|
|
260
|
+
*
|
|
261
|
+
* @param options The options to validate.
|
|
262
|
+
* @throws If no files are specified.
|
|
263
|
+
* @throws If methods is not defined.
|
|
264
|
+
* @throws If no methods are enabled.
|
|
265
|
+
* @throws If there's an invalid HTTP method.
|
|
266
|
+
*/
|
|
267
|
+
protected _validateOptions(
|
|
268
|
+
options: StaticsControllerOptions,
|
|
269
|
+
): StaticsControllerOptions {
|
|
270
|
+
if (!options.files || !options.files.length) {
|
|
271
|
+
throw new Error('You need to specify a list of files');
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (!options.methods) {
|
|
275
|
+
throw new Error('You need to specify which HTTP methods are allowed for the files');
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const methods = Object.keys(options.methods) as RouterMethod[];
|
|
279
|
+
|
|
280
|
+
const atLeastOne = methods.some((method) => options.methods[method]);
|
|
281
|
+
if (!atLeastOne) {
|
|
282
|
+
throw new Error('You need to enable at least one HTTP method to serve the files');
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const allowedMethods: RouterMethod[] = [
|
|
286
|
+
'all',
|
|
287
|
+
'get',
|
|
288
|
+
'head',
|
|
289
|
+
'post',
|
|
290
|
+
'patch',
|
|
291
|
+
'put',
|
|
292
|
+
'delete',
|
|
293
|
+
'connect',
|
|
294
|
+
'options',
|
|
295
|
+
'trace',
|
|
296
|
+
];
|
|
297
|
+
|
|
298
|
+
const invalid = methods.find(
|
|
299
|
+
(method) => !allowedMethods.includes(method.toLowerCase() as RouterMethod),
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
if (invalid) {
|
|
303
|
+
throw new Error(`${invalid} is not a valid HTTP method`);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const newMethods = methods.reduce<Record<string, boolean>>((acc, method) => {
|
|
307
|
+
acc[method.toLowerCase()] = !!options.methods[method];
|
|
308
|
+
return acc;
|
|
309
|
+
}, {});
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
...options,
|
|
313
|
+
methods: newMethods,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Parses the files received from the constructor's options, and formats them into
|
|
318
|
+
* proper definitions the controller can use.
|
|
319
|
+
*/
|
|
320
|
+
protected _createFiles(): Record<string, StaticsControllerFile> {
|
|
321
|
+
const { files, paths } = this._options;
|
|
322
|
+
const routePath = removeSlashes(paths.route, false, true);
|
|
323
|
+
return files.reduce<Record<string, StaticsControllerFile>>((acc, file) => {
|
|
324
|
+
let src;
|
|
325
|
+
let route;
|
|
326
|
+
let headers;
|
|
327
|
+
if (typeof file === 'object') {
|
|
328
|
+
({ route, path: src, headers } = file);
|
|
329
|
+
} else {
|
|
330
|
+
src = file;
|
|
331
|
+
route = file;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
src = path.join(paths.source, src);
|
|
335
|
+
route = removeSlashes(route, true, false);
|
|
336
|
+
route = `${routePath}/${route}`;
|
|
337
|
+
acc[route] = {
|
|
338
|
+
path: src,
|
|
339
|
+
route,
|
|
340
|
+
headers: headers || {},
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
return acc;
|
|
344
|
+
}, {});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* A controller that allows the application to server specific files from any folder to
|
|
349
|
+
* any route without the need of mounting directories as "static" folders.
|
|
350
|
+
*
|
|
351
|
+
* @group Controllers
|
|
352
|
+
* @group Controllers/Statics
|
|
353
|
+
*/
|
|
354
|
+
export const staticsController = controllerCreator(
|
|
355
|
+
({ getMiddlewares, ...options }: StaticsControllerCreatorOptions = {}) =>
|
|
356
|
+
(app) => {
|
|
357
|
+
const router = app.getRouter();
|
|
358
|
+
const ctrl = new StaticsController({
|
|
359
|
+
inject: {
|
|
360
|
+
sendFile: app.get('sendFile'),
|
|
361
|
+
},
|
|
362
|
+
...options,
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
let useMiddlewares: ExpressMiddleware[] | undefined;
|
|
366
|
+
if (getMiddlewares) {
|
|
367
|
+
useMiddlewares = getMiddlewares(app)
|
|
368
|
+
.map((middleware) => {
|
|
369
|
+
if ('middleware' in middleware) {
|
|
370
|
+
return middleware.connect(app) as ExpressMiddleware | undefined;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return middleware as ExpressMiddleware;
|
|
374
|
+
})
|
|
375
|
+
.filter(notUndefined);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return ctrl.addRoutes(router, useMiddlewares);
|
|
379
|
+
},
|
|
380
|
+
);
|