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
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
const fetch = require('node-fetch');
|
|
2
|
-
const urijs = require('urijs');
|
|
3
|
-
const { provider } = require('../../utils/wrappers');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {import('../../types').Logger} Logger
|
|
7
|
-
* @typedef {import('../../types').Provider} Provider
|
|
8
|
-
* @typedef {import('node-fetch').Response} Response
|
|
9
|
-
* @typedef {import('../../types').ExpressRequest} ExpressRequest
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {Object} HTTPFetchOptions
|
|
14
|
-
* @property {?string} method The request method.
|
|
15
|
-
* @property {?Object.<string, string>} headers The request headers.
|
|
16
|
-
* @property {?string} body The request body.
|
|
17
|
-
* @property {?Object.<string, string>} qs The request query string parameters.
|
|
18
|
-
* @property {?ExpressRequest} req An Express request object used to get
|
|
19
|
-
* extra infromation (like headers and the
|
|
20
|
-
* IP).
|
|
21
|
-
* @parent module:services
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A set of utilities to work with HTTP requests and responses.
|
|
26
|
-
*
|
|
27
|
-
* @parent module:services
|
|
28
|
-
*/
|
|
29
|
-
class HTTP {
|
|
30
|
-
/**
|
|
31
|
-
* @param {boolean} logRequests Whether or not to log the requests and their responses.
|
|
32
|
-
* @param {Logger} appLogger If `logRequests` is `true`, this will be used to log
|
|
33
|
-
* the requests and responses information.
|
|
34
|
-
*/
|
|
35
|
-
constructor(logRequests, appLogger) {
|
|
36
|
-
/**
|
|
37
|
-
* Whether or not to log the requests and their responses.
|
|
38
|
-
*
|
|
39
|
-
* @type {boolean}
|
|
40
|
-
* @access protected
|
|
41
|
-
* @ignore
|
|
42
|
-
*/
|
|
43
|
-
this._logRequests = logRequests;
|
|
44
|
-
/**
|
|
45
|
-
* A local reference for the `appLogger` service.
|
|
46
|
-
*
|
|
47
|
-
* @type {Logger}
|
|
48
|
-
* @access protected
|
|
49
|
-
* @ignore
|
|
50
|
-
*/
|
|
51
|
-
this._appLogger = appLogger;
|
|
52
|
-
/**
|
|
53
|
-
* So it can be sent to other services as a reference.
|
|
54
|
-
*
|
|
55
|
-
* @ignore
|
|
56
|
-
*/
|
|
57
|
-
this.fetch = this.fetch.bind(this);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Make a request.
|
|
61
|
-
*
|
|
62
|
-
* @param {string} url The request URL.
|
|
63
|
-
* @param {Partial<HTTPFetchOptions>} [options={}] The request options.
|
|
64
|
-
* @returns {Promise<Response>}
|
|
65
|
-
*/
|
|
66
|
-
fetch(url, options = {}) {
|
|
67
|
-
// Get a mutable reference for the URL.
|
|
68
|
-
let fetchURL = url;
|
|
69
|
-
// If there are query string parameters...
|
|
70
|
-
if (options.qs) {
|
|
71
|
-
// ...use `urijs` to inject them on the URL reference.
|
|
72
|
-
fetchURL = urijs(url).addSearch(options.qs).toString();
|
|
73
|
-
}
|
|
74
|
-
// Define an object that will hold the new set of options.
|
|
75
|
-
const fetchOptions = {
|
|
76
|
-
// Set the request method and make it fallback to `GET` if it wasn't set.
|
|
77
|
-
method: (options.method || 'get').toUpperCase(),
|
|
78
|
-
};
|
|
79
|
-
// If there's a body, add it to the new options.
|
|
80
|
-
if (options.body) {
|
|
81
|
-
fetchOptions.body = options.body;
|
|
82
|
-
}
|
|
83
|
-
// Define the base headers for the request.
|
|
84
|
-
let defaultHeaders = {};
|
|
85
|
-
// If there's an Express request object on the `options`...
|
|
86
|
-
if (options.req) {
|
|
87
|
-
/**
|
|
88
|
-
* Overwrite the base headers with the request original IP as `x-forwarded-for` and all the
|
|
89
|
-
* received custom headers that request may have.
|
|
90
|
-
*/
|
|
91
|
-
defaultHeaders = {
|
|
92
|
-
'x-forwarded-for': this.getIPFromRequest(options.req),
|
|
93
|
-
...this.getCustomHeadersFromRequest(options.req),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
// Merge the base headers with the ones received on the `options`.
|
|
97
|
-
const headers = {
|
|
98
|
-
...defaultHeaders,
|
|
99
|
-
...(options.headers || {}),
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* If there's at least one header on the dictionary, add it to the new options. This check is
|
|
103
|
-
* to avoid sending an empty object.
|
|
104
|
-
*/
|
|
105
|
-
if (Object.keys(headers).length) {
|
|
106
|
-
fetchOptions.headers = this.normalizeHeaders(headers);
|
|
107
|
-
}
|
|
108
|
-
// If the `logRequests` flag is `true`, call the method to log the request.
|
|
109
|
-
if (this._logRequests) {
|
|
110
|
-
this._logRequest(fetchURL, fetchOptions);
|
|
111
|
-
}
|
|
112
|
-
// Make the request.
|
|
113
|
-
let result = fetch(fetchURL, fetchOptions);
|
|
114
|
-
// If the `logRequests` flag is `true`...
|
|
115
|
-
if (this._logRequests) {
|
|
116
|
-
// Add an extra step on the promise chain to log the response.
|
|
117
|
-
result = result.then((response) => {
|
|
118
|
-
this._logResponse(response);
|
|
119
|
-
return response;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
// Return the request promise.
|
|
123
|
-
return result;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Creates a dictionary with all the custom headers a request has. By custom header it
|
|
127
|
-
* means all the headers which name start with `x-`.
|
|
128
|
-
* This method doesn't copy `x-forwarded-for` as the `fetch` method generates it by
|
|
129
|
-
* calling `getIPFromRequest`.
|
|
130
|
-
*
|
|
131
|
-
* @param {ExpressRequest} req The request from which it will try to get the headers.
|
|
132
|
-
* @returns {Object.<string, string>}
|
|
133
|
-
*/
|
|
134
|
-
getCustomHeadersFromRequest(req) {
|
|
135
|
-
const headers = {};
|
|
136
|
-
Object.keys(req.headers).forEach((headerName) => {
|
|
137
|
-
if (headerName.startsWith('x-') && !headerName.startsWith('x-forwarded-for')) {
|
|
138
|
-
headers[headerName] = req.headers[headerName];
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
return headers;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Try to get the IP from a given request.
|
|
146
|
-
*
|
|
147
|
-
* @param {ExpressRequest} req The request from which it will try to obtain the IP
|
|
148
|
-
* address.
|
|
149
|
-
* @returns {?string}
|
|
150
|
-
*/
|
|
151
|
-
getIPFromRequest(req) {
|
|
152
|
-
return (
|
|
153
|
-
req.headers['x-forwarded-for'] ||
|
|
154
|
-
req.connection.remoteAddress ||
|
|
155
|
-
req.socket.remoteAddress ||
|
|
156
|
-
req.connection.socket.remoteAddress
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* It takes a dictionary of headers and normalize the names so each word will start with
|
|
161
|
-
* an upper case character. This is helpful in case you added custom headers and didn't
|
|
162
|
-
* care about the casing, or when copying headers from a server request, in which case
|
|
163
|
-
* they are all tranformed to lower case.
|
|
164
|
-
*
|
|
165
|
-
* @param {Object.<string, string>} headers The dictionary of headers to normalize.
|
|
166
|
-
* @returns {Object.<string, string>}
|
|
167
|
-
*/
|
|
168
|
-
normalizeHeaders(headers) {
|
|
169
|
-
return Object.keys(headers).reduce((newHeaders, name) => {
|
|
170
|
-
const newName = name
|
|
171
|
-
.split('-')
|
|
172
|
-
.map((part) => part.replace(/^(\w)/, (ignore, letter) => letter.toUpperCase()))
|
|
173
|
-
.join('-');
|
|
174
|
-
return {
|
|
175
|
-
...newHeaders,
|
|
176
|
-
[newName]: headers[name],
|
|
177
|
-
};
|
|
178
|
-
}, {});
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Whether or not to log the requests and their responses.
|
|
182
|
-
*
|
|
183
|
-
* @type {boolean}
|
|
184
|
-
*/
|
|
185
|
-
get logRequests() {
|
|
186
|
-
return this._logRequests;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Log a a request information using the `appLogger` service.
|
|
190
|
-
*
|
|
191
|
-
* @param {string} url The request URL.
|
|
192
|
-
* @param {HTTPFetchOptions} options The options generated by the `fetch` method.
|
|
193
|
-
* @access protected
|
|
194
|
-
* @ignore
|
|
195
|
-
*/
|
|
196
|
-
_logRequest(url, options) {
|
|
197
|
-
const prefix = 'REQUEST> ';
|
|
198
|
-
const lines = ['--->>', `${prefix}${options.method} ${url}`];
|
|
199
|
-
if (options.headers) {
|
|
200
|
-
Object.keys(options.headers).forEach((header) => {
|
|
201
|
-
lines.push(`${prefix}${header}: ${options.headers[header]}`);
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (options.body) {
|
|
206
|
-
lines.push(`${prefix}body: "${options.body}"`);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
this._appLogger.info(lines);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Log a a response information using the `appLogger` service.
|
|
213
|
-
*
|
|
214
|
-
* @param {Response} response The response object returned by `node-fetch`.
|
|
215
|
-
* @access protected
|
|
216
|
-
* @ignore
|
|
217
|
-
*/
|
|
218
|
-
_logResponse(response) {
|
|
219
|
-
const prefix = 'RESPONSE> ';
|
|
220
|
-
const lines = [
|
|
221
|
-
'<<---',
|
|
222
|
-
`${prefix}${response.url}`,
|
|
223
|
-
`${prefix}status: ${response.status}`,
|
|
224
|
-
];
|
|
225
|
-
|
|
226
|
-
response.headers.forEach((value, header) => {
|
|
227
|
-
lines.push(`${prefix}${header}: ${value}`);
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
this._appLogger.info(lines);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* The service provider that once registered on the app container will set an instance of
|
|
235
|
-
* `HTTP` as the `http` service. The provider also checks the `debug.logRequests` setting
|
|
236
|
-
* on the app configuration in order to enable or not the logging of requests.
|
|
237
|
-
*
|
|
238
|
-
* @type {Provider}
|
|
239
|
-
* @example
|
|
240
|
-
*
|
|
241
|
-
* // Register it on the container
|
|
242
|
-
* container.register(http);
|
|
243
|
-
* // Getting access to the service instance
|
|
244
|
-
* const http = container.get('http');
|
|
245
|
-
*
|
|
246
|
-
* @parent module:services
|
|
247
|
-
*/
|
|
248
|
-
const http = provider((app) => {
|
|
249
|
-
app.set('http', () => {
|
|
250
|
-
const logRequests = app.get('appConfiguration').get('debug.logRequests') === true;
|
|
251
|
-
return new HTTP(logRequests, app.get('appLogger'));
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
module.exports.HTTP = HTTP;
|
|
256
|
-
module.exports.http = http;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const { apiClient } = require('./apiClient');
|
|
2
|
-
const { http } = require('./http');
|
|
3
|
-
const { responsesBuilder } = require('./responsesBuilder');
|
|
4
|
-
const { providers } = require('../../utils/wrappers');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {import('../../types').Provider} Provider
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* The providers collection for the HTTP services.
|
|
12
|
-
*
|
|
13
|
-
* @type {Provider}
|
|
14
|
-
* @property {Provider} apiClient The provider for {@link APIClient}.
|
|
15
|
-
* @property {Provider} http The provider for {@link HTTP}.
|
|
16
|
-
* @property {Provider} responsesBuilder The provider for {@link ResponsesBuilder}.
|
|
17
|
-
* @parent module:services
|
|
18
|
-
*/
|
|
19
|
-
const httpServices = providers({
|
|
20
|
-
apiClient,
|
|
21
|
-
http,
|
|
22
|
-
responsesBuilder,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
module.exports = httpServices;
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
const { code: statuses } = require('statuses');
|
|
2
|
-
const { provider } = require('../../utils/wrappers');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @typedef {import('../../types').Provider} Provider
|
|
6
|
-
* @typedef {import('../../types').AppConfiguration} AppConfiguration
|
|
7
|
-
* @typedef {import('../../types').ExpressResponse} ExpressResponse
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* It allows customization of a post message HTML template.
|
|
12
|
-
*
|
|
13
|
-
* @typedef {Object} ResponsesBuilderPostMessageOptions
|
|
14
|
-
* @property {?string} target
|
|
15
|
-
* The target that will emit the `postMessage`.
|
|
16
|
-
* Default `'window.opener'`.
|
|
17
|
-
* @property {?boolean} close
|
|
18
|
-
* Whether or not to do a `window.close` after sending the message.
|
|
19
|
-
* Default `true`.
|
|
20
|
-
* @property {?number} closeDelay
|
|
21
|
-
* How many ms should it wait before closing the window, if `options.close` is `true`.
|
|
22
|
-
* Default `700`.
|
|
23
|
-
* @parent module:services
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* A utility service to build server responses.
|
|
28
|
-
*
|
|
29
|
-
* @parent module:services
|
|
30
|
-
*/
|
|
31
|
-
class ResponsesBuilder {
|
|
32
|
-
/**
|
|
33
|
-
* @param {AppConfiguration} appConfiguration To get the app version.
|
|
34
|
-
*/
|
|
35
|
-
constructor(appConfiguration) {
|
|
36
|
-
/**
|
|
37
|
-
* A local reference for the `appConfiguration` service.
|
|
38
|
-
*
|
|
39
|
-
* @type {AppConfiguration}
|
|
40
|
-
* @access protected
|
|
41
|
-
* @ignore
|
|
42
|
-
*/
|
|
43
|
-
this._appConfiguration = appConfiguration;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Generates and send an HTML response that emits a post message.
|
|
47
|
-
* The post message will be prefixed with the value of the configuration setting
|
|
48
|
-
* `postMessagesPrefix`.
|
|
49
|
-
*
|
|
50
|
-
* @param {ExpressResponse} res
|
|
51
|
-
* The Express response object necessary to write the HTML.
|
|
52
|
-
* @param {string} title
|
|
53
|
-
* The title for the HTML.
|
|
54
|
-
* @param {string} message
|
|
55
|
-
* The contents of the post message.
|
|
56
|
-
* @param {number} [status=200]
|
|
57
|
-
* The HTTP status.
|
|
58
|
-
* @param {Partial<ResponsesBuilderPostMessageOptions>} [options={}]
|
|
59
|
-
* Options to customize the HTML.
|
|
60
|
-
*/
|
|
61
|
-
htmlPostMessage(res, title, message, status = statuses.ok, options = {}) {
|
|
62
|
-
const prefix = this._appConfiguration.get('postMessagesPrefix') || '';
|
|
63
|
-
const target = options.target || 'window.opener';
|
|
64
|
-
const close = typeof options.close !== 'undefined' ? options.close : true;
|
|
65
|
-
const defaultCloseDelay = 700;
|
|
66
|
-
const closeDelay = options.closeDelay || defaultCloseDelay;
|
|
67
|
-
const closeCode = close
|
|
68
|
-
? `setTimeout(function() { window.close(); }, ${closeDelay});`
|
|
69
|
-
: '';
|
|
70
|
-
const html = this._htmlTemplate(
|
|
71
|
-
title,
|
|
72
|
-
`
|
|
73
|
-
(function() {
|
|
74
|
-
if (${target}) {
|
|
75
|
-
${target}.postMessage('${prefix}${message}', '*');
|
|
76
|
-
${closeCode}
|
|
77
|
-
}
|
|
78
|
-
})();
|
|
79
|
-
`,
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
res.setHeader('Content-Type', 'text/html');
|
|
83
|
-
res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store');
|
|
84
|
-
res.status(this._normalizeStatus(status));
|
|
85
|
-
res.write(html);
|
|
86
|
-
res.end();
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Generates and sends a JSON response.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
*
|
|
93
|
-
* <caption>The generated looks like this.</caption>
|
|
94
|
-
*
|
|
95
|
-
* {
|
|
96
|
-
* metadata: {
|
|
97
|
-
* version: '[app-version]',
|
|
98
|
-
* status: [http-status],
|
|
99
|
-
* },
|
|
100
|
-
* data: [...],
|
|
101
|
-
* }
|
|
102
|
-
*
|
|
103
|
-
* @param {ExpressResponse} res The Express response object necessary to
|
|
104
|
-
* write the JSON.
|
|
105
|
-
* @param {Object} data The information for the `data` key.
|
|
106
|
-
* @param {number} [status=200] The HTTP status.
|
|
107
|
-
* @param {Object} [metadata={}] Extra information to include on the
|
|
108
|
-
* `metadata` key.
|
|
109
|
-
*/
|
|
110
|
-
json(res, data, status = statuses.ok, metadata = {}) {
|
|
111
|
-
const useStatus = this._normalizeStatus(status);
|
|
112
|
-
res
|
|
113
|
-
.status(useStatus)
|
|
114
|
-
.json({
|
|
115
|
-
metadata: {
|
|
116
|
-
version: this._appConfiguration.get('version'),
|
|
117
|
-
status: useStatus,
|
|
118
|
-
...metadata,
|
|
119
|
-
},
|
|
120
|
-
data,
|
|
121
|
-
})
|
|
122
|
-
.end();
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Generates a basic HTML template for other services to use.
|
|
126
|
-
*
|
|
127
|
-
* @param {string} title The HTML `<title />` attribute.
|
|
128
|
-
* @param {string} code Javascript code to be wrapped on a `<script />` tag.
|
|
129
|
-
* @returns {string}
|
|
130
|
-
* @access protected
|
|
131
|
-
* @ignore
|
|
132
|
-
*/
|
|
133
|
-
_htmlTemplate(title, code) {
|
|
134
|
-
return `
|
|
135
|
-
<!DOCTYPE html>
|
|
136
|
-
<html>
|
|
137
|
-
<head>
|
|
138
|
-
<meta charset="utf-8" />
|
|
139
|
-
<meta http-equiv="x-ua-compatible" content="ie=edge" />
|
|
140
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
141
|
-
<title>${title}</title>
|
|
142
|
-
</head>
|
|
143
|
-
<body>
|
|
144
|
-
<script type="text/javascript">
|
|
145
|
-
${code}
|
|
146
|
-
</script>
|
|
147
|
-
</body>
|
|
148
|
-
</html>
|
|
149
|
-
`;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Utility method used to make sure a recevied status is a valid status code. If the
|
|
153
|
-
* status is a string, the method will try to find the code from the `statuses` package.
|
|
154
|
-
*
|
|
155
|
-
* @param {string | number} status The status to normalize.
|
|
156
|
-
* @returns {string | number} If `status` is a string, but there's no valid code, it
|
|
157
|
-
* will return it as it was received.
|
|
158
|
-
* @access protected
|
|
159
|
-
* @ignore
|
|
160
|
-
* @todo On the next breaking version, if there's no valid code, it will be
|
|
161
|
-
* transformed to `200`.
|
|
162
|
-
*/
|
|
163
|
-
_normalizeStatus(status) {
|
|
164
|
-
let result;
|
|
165
|
-
if (typeof status === 'string') {
|
|
166
|
-
result = statuses[status] || status;
|
|
167
|
-
} else {
|
|
168
|
-
result = status;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return result;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* The service provider that once registered on the app container will set an instance of
|
|
176
|
-
* `ResponsesBuilder` as the `responsesBuilder` service.
|
|
177
|
-
*
|
|
178
|
-
* @type {Provider}
|
|
179
|
-
* @example
|
|
180
|
-
*
|
|
181
|
-
* // Register it on the container
|
|
182
|
-
* container.register(responsesBuilder);
|
|
183
|
-
* // Getting access to the service instance
|
|
184
|
-
* const responsesBuilder = container.get('responsesBuilder');
|
|
185
|
-
*
|
|
186
|
-
* @parent module:services
|
|
187
|
-
*/
|
|
188
|
-
const responsesBuilder = provider((app) => {
|
|
189
|
-
app.set('responsesBuilder', () => new ResponsesBuilder(app.get('appConfiguration')));
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
module.exports.ResponsesBuilder = ResponsesBuilder;
|
|
193
|
-
module.exports.responsesBuilder = responsesBuilder;
|
package/src/services/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const common = require('./common');
|
|
2
|
-
const frontend = require('./frontend');
|
|
3
|
-
const html = require('./html');
|
|
4
|
-
const http = require('./http');
|
|
5
|
-
const utils = require('./utils');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @module module:services
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
module.exports.common = common;
|
|
12
|
-
module.exports.frontend = frontend;
|
|
13
|
-
module.exports.html = html;
|
|
14
|
-
module.exports.http = http;
|
|
15
|
-
module.exports.utils = utils;
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
const { code: statuses } = require('statuses');
|
|
2
|
-
const ObjectUtils = require('wootils/shared/objectUtils');
|
|
3
|
-
const { providerCreator } = require('../../utils/wrappers');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {import('../../types').ExpressMiddleware} ExpressMiddleware
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {import('../../types').ProviderCreator<O>} ProviderCreator<O>
|
|
11
|
-
* @template O
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* These options allow you to modify the error generated by the middleware when the
|
|
16
|
-
* request doesn't have a valid token.
|
|
17
|
-
*
|
|
18
|
-
* @typedef {Object} EnsureBearerTokenErrorOptions
|
|
19
|
-
* @property {string} message The error message for the response. Default
|
|
20
|
-
* `'Unauthorized'`.
|
|
21
|
-
* @property {number} status The HTTP status that will be added to error context
|
|
22
|
-
* information.
|
|
23
|
-
* Default `401`.
|
|
24
|
-
* @property {Object} response Context information that the error handler can read and
|
|
25
|
-
* add to the default response. Default `{}`.
|
|
26
|
-
* @parent module:services
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The options for how to validate the token and, possibly, create the errors.
|
|
31
|
-
*
|
|
32
|
-
* @typedef {Object} EnsureBearerTokenOptions
|
|
33
|
-
* @property {EnsureBearerTokenErrorOptions} error
|
|
34
|
-
* The options to modify the error generated by the middleware when the request doesn't
|
|
35
|
-
* have a valid token.
|
|
36
|
-
* @property {RegExp} expression
|
|
37
|
-
* The regular expression used to extract the token from the request authorization header.
|
|
38
|
-
* @property {string} local
|
|
39
|
-
* The property inside the `res.locals` where the token, if found, will be saved.
|
|
40
|
-
* Default `'token'`.
|
|
41
|
-
* @parent module:services
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* This service gives you a middleware that verifies if a request has an `Authorization`
|
|
46
|
-
* header with a bearer token; if it does, the token will be saved on the `res.locals`,
|
|
47
|
-
* otherwise, it will generate an error.
|
|
48
|
-
*
|
|
49
|
-
* @parent module:services
|
|
50
|
-
*/
|
|
51
|
-
class EnsureBearerToken {
|
|
52
|
-
/**
|
|
53
|
-
* @param {ClassAppError} AppError
|
|
54
|
-
* To format the error caused when the request doesn't have a valid token.
|
|
55
|
-
* @param {Partial<EnsureBearerTokenOptions>} [options={}]
|
|
56
|
-
* The options to customize the middleware behavior: how to validate the token, how to
|
|
57
|
-
* save it and what kind of error should generate.
|
|
58
|
-
*/
|
|
59
|
-
constructor(AppError, options = {}) {
|
|
60
|
-
/**
|
|
61
|
-
* A local reference for the class the app uses to generate errors.
|
|
62
|
-
*
|
|
63
|
-
* @type {ClassAppError}
|
|
64
|
-
* @access protected
|
|
65
|
-
* @ignore
|
|
66
|
-
*/
|
|
67
|
-
this._AppError = AppError;
|
|
68
|
-
/**
|
|
69
|
-
* The options that define how the middleware validates the token, saves it and
|
|
70
|
-
* generates the possible error.
|
|
71
|
-
*
|
|
72
|
-
* @type {EnsureBearerTokenOptions}
|
|
73
|
-
* @access protected
|
|
74
|
-
* @ignore
|
|
75
|
-
*/
|
|
76
|
-
this._options = ObjectUtils.merge(
|
|
77
|
-
{
|
|
78
|
-
error: {
|
|
79
|
-
message: 'Unauthorized',
|
|
80
|
-
status: statuses.unauthorized,
|
|
81
|
-
response: {},
|
|
82
|
-
},
|
|
83
|
-
expression: /bearer (.*?)(?:$|\s)/i,
|
|
84
|
-
local: 'token',
|
|
85
|
-
},
|
|
86
|
-
options,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Creates the middleware that will validate the presence of a bearer token on the
|
|
91
|
-
* request authorization header.
|
|
92
|
-
*
|
|
93
|
-
* @returns {ExpressMiddleware}
|
|
94
|
-
*/
|
|
95
|
-
middleware() {
|
|
96
|
-
return (req, res, next) => {
|
|
97
|
-
let unauthorized = true;
|
|
98
|
-
const {
|
|
99
|
-
headers: { authorization },
|
|
100
|
-
} = req;
|
|
101
|
-
if (authorization) {
|
|
102
|
-
const matches = this._options.expression.exec(authorization);
|
|
103
|
-
if (matches) {
|
|
104
|
-
const [, token] = matches;
|
|
105
|
-
res.locals[this._options.local] = token;
|
|
106
|
-
unauthorized = false;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (unauthorized) {
|
|
111
|
-
const { error } = this._options;
|
|
112
|
-
next(
|
|
113
|
-
new this._AppError(error.message, {
|
|
114
|
-
status: error.status,
|
|
115
|
-
response: error.response,
|
|
116
|
-
}),
|
|
117
|
-
);
|
|
118
|
-
} else {
|
|
119
|
-
next();
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* The options that define how the middleware validates the token, saves it and
|
|
125
|
-
* generates the possible error.
|
|
126
|
-
*
|
|
127
|
-
* @type {EnsureBearerTokenOptions}
|
|
128
|
-
*/
|
|
129
|
-
get options() {
|
|
130
|
-
return this._options;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Generates a "service middleware" that can be used on route controllers in order to
|
|
135
|
-
* validate the presence of a bearer token on the requests authorization header.
|
|
136
|
-
*
|
|
137
|
-
* @type {ProviderCreator<EnsureBearerTokenOptions>}
|
|
138
|
-
* @parent module:services
|
|
139
|
-
*/
|
|
140
|
-
const ensureBearerToken = providerCreator((options) => (app) => {
|
|
141
|
-
app.set('ensureBearerToken', () =>
|
|
142
|
-
new EnsureBearerToken(app.get('AppError'), options).middleware(),
|
|
143
|
-
);
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
module.exports.EnsureBearerToken = EnsureBearerToken;
|
|
147
|
-
module.exports.ensureBearerToken = ensureBearerToken;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const { ensureBearerToken } = require('./ensureBearerToken');
|
|
2
|
-
const { providers } = require('../../utils/wrappers');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @typedef {import('../../types').Provider} Provider
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The providers collection for the utility services.
|
|
10
|
-
*
|
|
11
|
-
* @type {Provider}
|
|
12
|
-
* @property {Provider} ensureBearerToken The provider for {@link EnsureBearerToken}.
|
|
13
|
-
* @parent module:services
|
|
14
|
-
*/
|
|
15
|
-
const utilsServices = providers({
|
|
16
|
-
ensureBearerToken,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
module.exports = utilsServices;
|