jimpex 7.0.2 → 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 +80 -0
- package/LICENSE +1 -1
- package/README.md +446 -434
- package/dist/app/index.d.ts +13 -0
- package/dist/app/index.js +19 -0
- package/dist/app/index.js.map +1 -0
- package/dist/app/jimpex.d.ts +13 -0
- package/dist/app/jimpex.js +440 -0
- package/dist/app/jimpex.js.map +1 -0
- package/dist/controllers/common/config.d.ts +78 -0
- package/dist/controllers/common/config.js +89 -0
- package/dist/controllers/common/config.js.map +1 -0
- package/dist/controllers/common/health.d.ts +82 -0
- package/dist/controllers/common/health.js +97 -0
- package/dist/controllers/common/health.js.map +1 -0
- package/dist/controllers/common/index.d.ts +21 -0
- package/dist/controllers/common/index.js +21 -0
- package/dist/controllers/common/index.js.map +1 -0
- package/dist/controllers/common/statics.d.ts +215 -0
- package/dist/controllers/common/statics.js +202 -0
- package/dist/controllers/common/statics.js.map +1 -0
- package/dist/controllers/index.d.ts +24 -0
- package/dist/controllers/index.js +20 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/controllers/utils/gateway.d.ts +724 -0
- package/dist/controllers/utils/gateway.js +425 -0
- package/dist/controllers/utils/gateway.js.map +1 -0
- package/dist/controllers/utils/index.d.ts +16 -0
- package/dist/controllers/utils/index.js +19 -0
- package/dist/controllers/utils/index.js.map +1 -0
- package/dist/esm/app/index.js +2 -0
- package/dist/esm/app/index.js.map +1 -0
- package/dist/esm/app/jimpex.js +415 -0
- package/dist/esm/app/jimpex.js.map +1 -0
- package/dist/esm/chunk-T2T6Q22Z.js +11 -0
- package/dist/esm/chunk-T2T6Q22Z.js.map +1 -0
- package/dist/esm/controllers/common/config.js +62 -0
- package/dist/esm/controllers/common/config.js.map +1 -0
- package/dist/esm/controllers/common/health.js +70 -0
- package/dist/esm/controllers/common/health.js.map +1 -0
- package/dist/esm/controllers/common/index.js +4 -0
- package/dist/esm/controllers/common/index.js.map +1 -0
- package/dist/esm/controllers/common/statics.js +173 -0
- package/dist/esm/controllers/common/statics.js.map +1 -0
- package/dist/esm/controllers/index.js +3 -0
- package/dist/esm/controllers/index.js.map +1 -0
- package/dist/esm/controllers/utils/gateway.js +404 -0
- package/dist/esm/controllers/utils/gateway.js.map +1 -0
- package/dist/esm/controllers/utils/index.js +2 -0
- package/dist/esm/controllers/utils/index.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/middlewares/common/errorHandler.js +92 -0
- package/dist/esm/middlewares/common/errorHandler.js.map +1 -0
- package/dist/esm/middlewares/common/forceHTTPS.js +41 -0
- package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -0
- package/dist/esm/middlewares/common/hsts.js +53 -0
- package/dist/esm/middlewares/common/hsts.js.map +1 -0
- package/dist/esm/middlewares/common/index.js +4 -0
- package/dist/esm/middlewares/common/index.js.map +1 -0
- package/dist/esm/middlewares/html/fastHTML.js +104 -0
- package/dist/esm/middlewares/html/fastHTML.js.map +1 -0
- package/dist/esm/middlewares/html/index.js +3 -0
- package/dist/esm/middlewares/html/index.js.map +1 -0
- package/dist/esm/middlewares/html/showHTML.js +68 -0
- package/dist/esm/middlewares/html/showHTML.js.map +1 -0
- package/dist/esm/middlewares/index.js +4 -0
- package/dist/esm/middlewares/index.js.map +1 -0
- package/dist/esm/middlewares/utils/index.js +2 -0
- package/dist/esm/middlewares/utils/index.js.map +1 -0
- package/dist/esm/middlewares/utils/versionValidator.js +101 -0
- package/dist/esm/middlewares/utils/versionValidator.js.map +1 -0
- package/dist/esm/services/common/appError.js +52 -0
- package/dist/esm/services/common/appError.js.map +1 -0
- package/dist/esm/services/common/httpError.js +19 -0
- package/dist/esm/services/common/httpError.js.map +1 -0
- package/dist/esm/services/common/index.js +17 -0
- package/dist/esm/services/common/index.js.map +1 -0
- package/dist/esm/services/common/sendFile.js +27 -0
- package/dist/esm/services/common/sendFile.js.map +1 -0
- package/dist/esm/services/frontend/frontendFs.js +38 -0
- package/dist/esm/services/frontend/frontendFs.js.map +1 -0
- package/dist/esm/services/frontend/index.js +11 -0
- package/dist/esm/services/frontend/index.js.map +1 -0
- package/dist/esm/services/html/htmlGenerator.js +144 -0
- package/dist/esm/services/html/htmlGenerator.js.map +1 -0
- package/dist/esm/services/html/index.js +11 -0
- package/dist/esm/services/html/index.js.map +1 -0
- package/dist/esm/services/http/apiClient.js +71 -0
- package/dist/esm/services/http/apiClient.js.map +1 -0
- package/dist/esm/services/http/http.js +125 -0
- package/dist/esm/services/http/http.js.map +1 -0
- package/dist/esm/services/http/index.js +17 -0
- package/dist/esm/services/http/index.js.map +1 -0
- package/dist/esm/services/http/responsesBuilder.js +105 -0
- package/dist/esm/services/http/responsesBuilder.js.map +1 -0
- package/dist/esm/services/index.js +6 -0
- package/dist/esm/services/index.js.map +1 -0
- package/dist/esm/services/utils/ensureBearerToken.js +78 -0
- package/dist/esm/services/utils/ensureBearerToken.js.map +1 -0
- package/dist/esm/services/utils/index.js +11 -0
- package/dist/esm/services/utils/index.js.map +1 -0
- package/dist/esm/types/events.js +1 -0
- package/dist/esm/types/events.js.map +1 -0
- package/dist/esm/types/express.js +1 -0
- package/dist/esm/types/express.js.map +1 -0
- package/dist/esm/types/http.js +1 -0
- package/dist/esm/types/http.js.map +1 -0
- package/dist/esm/types/index.js +7 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/options.js.map +1 -0
- package/dist/esm/types/utils.js +1 -0
- package/dist/esm/types/utils.js.map +1 -0
- package/dist/esm/types/wootils.js +1 -0
- package/dist/esm/types/wootils.js.map +1 -0
- package/dist/esm/utils/fns/index.js +5 -0
- package/dist/esm/utils/fns/index.js.map +1 -0
- package/dist/esm/utils/fns/others.js +6 -0
- package/dist/esm/utils/fns/others.js.map +1 -0
- package/dist/esm/utils/fns/routes.js +25 -0
- package/dist/esm/utils/fns/routes.js.map +1 -0
- package/dist/esm/utils/fns/statuses.js +6 -0
- package/dist/esm/utils/fns/statuses.js.map +1 -0
- package/dist/esm/utils/fns/text.js +6 -0
- package/dist/esm/utils/fns/text.js.map +1 -0
- package/dist/esm/utils/index.js +3 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/wrappers.js +41 -0
- package/dist/esm/utils/wrappers.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/jimpex-7eaee271.d.ts +1278 -0
- package/dist/middlewares/common/errorHandler.d.ts +131 -0
- package/dist/middlewares/common/errorHandler.js +119 -0
- package/dist/middlewares/common/errorHandler.js.map +1 -0
- package/dist/middlewares/common/forceHTTPS.d.ts +68 -0
- package/dist/middlewares/common/forceHTTPS.js +68 -0
- package/dist/middlewares/common/forceHTTPS.js.map +1 -0
- package/dist/middlewares/common/hsts.d.ts +109 -0
- package/dist/middlewares/common/hsts.js +80 -0
- package/dist/middlewares/common/hsts.js.map +1 -0
- package/dist/middlewares/common/index.d.ts +21 -0
- package/dist/middlewares/common/index.js +21 -0
- package/dist/middlewares/common/index.js.map +1 -0
- package/dist/middlewares/html/fastHTML.d.ts +180 -0
- package/dist/middlewares/html/fastHTML.js +131 -0
- package/dist/middlewares/html/fastHTML.js.map +1 -0
- package/dist/middlewares/html/index.d.ts +21 -0
- package/dist/middlewares/html/index.js +20 -0
- package/dist/middlewares/html/index.js.map +1 -0
- package/dist/middlewares/html/showHTML.d.ts +127 -0
- package/dist/middlewares/html/showHTML.js +95 -0
- package/dist/middlewares/html/showHTML.js.map +1 -0
- package/dist/middlewares/index.d.ts +30 -0
- package/dist/middlewares/index.js +21 -0
- package/dist/middlewares/index.js.map +1 -0
- package/dist/middlewares/utils/index.d.ts +19 -0
- package/dist/middlewares/utils/index.js +19 -0
- package/dist/middlewares/utils/index.js.map +1 -0
- package/dist/middlewares/utils/versionValidator.d.ts +214 -0
- package/dist/middlewares/utils/versionValidator.js +128 -0
- package/dist/middlewares/utils/versionValidator.js.map +1 -0
- package/dist/services/common/appError.d.ts +138 -0
- package/dist/services/common/appError.js +80 -0
- package/dist/services/common/appError.js.map +1 -0
- package/dist/services/common/httpError.d.ts +79 -0
- package/dist/services/common/httpError.js +44 -0
- package/dist/services/common/httpError.js.map +1 -0
- package/dist/services/common/index.d.ts +47 -0
- package/dist/services/common/index.js +41 -0
- package/dist/services/common/index.js.map +1 -0
- package/dist/services/common/sendFile.d.ts +102 -0
- package/dist/services/common/sendFile.js +51 -0
- package/dist/services/common/sendFile.js.map +1 -0
- package/dist/services/frontend/frontendFs.d.ts +96 -0
- package/dist/services/frontend/frontendFs.js +71 -0
- package/dist/services/frontend/frontendFs.js.map +1 -0
- package/dist/services/frontend/index.d.ts +40 -0
- package/dist/services/frontend/index.js +35 -0
- package/dist/services/frontend/index.js.map +1 -0
- package/dist/services/html/htmlGenerator.d.ts +237 -0
- package/dist/services/html/htmlGenerator.js +171 -0
- package/dist/services/html/htmlGenerator.js.map +1 -0
- package/dist/services/html/index.d.ts +43 -0
- package/dist/services/html/index.js +35 -0
- package/dist/services/html/index.js.map +1 -0
- package/dist/services/http/apiClient.d.ts +169 -0
- package/dist/services/http/apiClient.js +96 -0
- package/dist/services/http/apiClient.js.map +1 -0
- package/dist/services/http/http.d.ts +175 -0
- package/dist/services/http/http.js +158 -0
- package/dist/services/http/http.js.map +1 -0
- package/dist/services/http/index.d.ts +50 -0
- package/dist/services/http/index.js +41 -0
- package/dist/services/http/index.js.map +1 -0
- package/dist/services/http/responsesBuilder.d.ts +178 -0
- package/dist/services/http/responsesBuilder.js +132 -0
- package/dist/services/http/responsesBuilder.js.map +1 -0
- package/dist/services/index.d.ts +33 -0
- package/dist/services/index.js +23 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/utils/ensureBearerToken.d.ts +157 -0
- package/dist/services/utils/ensureBearerToken.js +105 -0
- package/dist/services/utils/ensureBearerToken.js.map +1 -0
- package/dist/services/utils/index.d.ts +43 -0
- package/dist/services/utils/index.js +35 -0
- package/dist/services/utils/index.js.map +1 -0
- package/dist/types/events.d.ts +13 -0
- package/dist/types/events.js +17 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/express.d.ts +10 -0
- package/dist/types/express.js +17 -0
- package/dist/types/express.js.map +1 -0
- package/dist/types/http.d.ts +79 -0
- package/dist/types/http.js +17 -0
- package/dist/types/http.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.js +24 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/options.d.ts +13 -0
- package/dist/types/options.js +17 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/utils.d.ts +48 -0
- package/dist/types/utils.js +17 -0
- package/dist/types/utils.js.map +1 -0
- package/dist/types/wootils.d.ts +4 -0
- package/dist/types/wootils.js +17 -0
- package/dist/types/wootils.js.map +1 -0
- package/dist/utils/fns/index.d.ts +5 -0
- package/dist/utils/fns/index.js +22 -0
- package/dist/utils/fns/index.js.map +1 -0
- package/dist/utils/fns/others.d.ts +17 -0
- package/dist/utils/fns/others.js +29 -0
- package/dist/utils/fns/others.js.map +1 -0
- package/dist/utils/fns/routes.d.ts +39 -0
- package/dist/utils/fns/routes.js +51 -0
- package/dist/utils/fns/routes.js.map +1 -0
- package/dist/utils/fns/statuses.d.ts +45 -0
- package/dist/utils/fns/statuses.js +35 -0
- package/dist/utils/fns/statuses.js.map +1 -0
- package/dist/utils/fns/text.d.ts +9 -0
- package/dist/utils/fns/text.js +29 -0
- package/dist/utils/fns/text.js.map +1 -0
- package/dist/utils/index.d.ts +18 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/wrappers.d.ts +13 -0
- package/dist/utils/wrappers.js +68 -0
- package/dist/utils/wrappers.js.map +1 -0
- package/examples/basic/404.html +12 -0
- package/examples/basic/README.md +30 -0
- package/examples/basic/app.ts +18 -0
- package/examples/basic/controller.ts +45 -0
- package/examples/basic/index.ts +19 -0
- package/examples/basic/middleware.ts +13 -0
- package/examples/basic/service.ts +11 -0
- package/package.json +59 -38
- package/src/app/index.ts +1 -0
- package/src/app/jimpex.ts +743 -0
- package/src/controllers/common/config.ts +115 -0
- package/src/controllers/common/health.ts +128 -0
- package/src/controllers/common/index.ts +3 -0
- package/src/controllers/common/statics.ts +380 -0
- package/src/controllers/index.ts +2 -0
- package/src/controllers/utils/gateway.ts +1186 -0
- package/src/controllers/utils/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/middlewares/common/errorHandler.ts +203 -0
- package/src/middlewares/common/forceHTTPS.ts +83 -0
- package/src/middlewares/common/hsts.ts +135 -0
- package/src/middlewares/common/index.ts +3 -0
- package/src/middlewares/html/fastHTML.ts +255 -0
- package/src/middlewares/html/index.ts +2 -0
- package/src/middlewares/html/showHTML.ts +165 -0
- package/src/middlewares/index.ts +3 -0
- package/src/middlewares/utils/index.ts +1 -0
- package/src/middlewares/utils/versionValidator.ts +289 -0
- package/src/services/common/appError.ts +158 -0
- package/src/services/common/httpError.ts +74 -0
- package/src/services/common/index.ts +29 -0
- package/src/services/common/sendFile.ts +106 -0
- package/src/services/frontend/frontendFs.ts +101 -0
- package/src/services/frontend/index.ts +21 -0
- package/src/services/html/htmlGenerator.ts +356 -0
- package/src/services/html/index.ts +21 -0
- package/src/services/http/apiClient.ts +221 -0
- package/src/services/http/http.ts +286 -0
- package/src/services/http/index.ts +29 -0
- package/src/services/http/responsesBuilder.ts +265 -0
- package/src/services/index.ts +5 -0
- package/src/services/utils/ensureBearerToken.ts +202 -0
- package/src/services/utils/index.ts +21 -0
- package/src/types/events.ts +303 -0
- package/src/types/express.ts +21 -0
- package/src/types/http.ts +77 -0
- package/src/types/index.ts +6 -0
- package/src/types/options.ts +248 -0
- package/src/types/utils.ts +52 -0
- package/src/types/wootils.ts +4 -0
- package/src/utils/fns/index.ts +4 -0
- package/src/utils/fns/others.ts +15 -0
- package/src/utils/fns/routes.ts +64 -0
- package/src/utils/fns/statuses.ts +44 -0
- package/src/utils/fns/text.ts +8 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/wrappers.ts +537 -0
- package/tsconfig.json +5 -8
- package/tsup.config.ts +10 -0
- package/src/app/index.js +0 -692
- package/src/constants/eventNames.js +0 -48
- package/src/constants/index.js +0 -7
- package/src/controllers/common/configuration.js +0 -116
- package/src/controllers/common/health.js +0 -79
- package/src/controllers/common/index.js +0 -7
- package/src/controllers/common/statics.js +0 -336
- package/src/controllers/index.js +0 -9
- package/src/controllers/utils/gateway.js +0 -1039
- package/src/controllers/utils/index.js +0 -3
- package/src/index.js +0 -30
- package/src/middlewares/common/errorHandler.js +0 -185
- package/src/middlewares/common/forceHTTPS.js +0 -80
- package/src/middlewares/common/hsts.js +0 -122
- package/src/middlewares/common/index.js +0 -7
- package/src/middlewares/html/fastHTML.js +0 -298
- package/src/middlewares/html/index.js +0 -5
- package/src/middlewares/html/showHTML.js +0 -167
- package/src/middlewares/index.js +0 -11
- package/src/middlewares/utils/index.js +0 -3
- package/src/middlewares/utils/versionValidator.js +0 -261
- package/src/services/common/appError.js +0 -136
- package/src/services/common/httpError.js +0 -60
- package/src/services/common/index.js +0 -25
- package/src/services/common/sendFile.js +0 -68
- package/src/services/frontend/frontendFs.js +0 -85
- package/src/services/frontend/index.js +0 -17
- package/src/services/html/htmlGenerator.js +0 -391
- package/src/services/html/index.js +0 -17
- package/src/services/http/apiClient.js +0 -148
- package/src/services/http/http.js +0 -256
- package/src/services/http/index.js +0 -25
- package/src/services/http/responsesBuilder.js +0 -193
- package/src/services/index.js +0 -15
- package/src/services/utils/ensureBearerToken.js +0 -147
- package/src/services/utils/index.js +0 -19
- package/src/types.js +0 -377
- package/src/utils/functions.js +0 -78
- package/src/utils/wrappers.js +0 -131
- package/types/app/index.d.ts +0 -417
- package/types/constants/eventNames.d.ts +0 -93
- package/types/constants/index.d.ts +0 -2
- package/types/controllers/common/configuration.d.ts +0 -71
- package/types/controllers/common/health.d.ts +0 -55
- package/types/controllers/common/index.d.ts +0 -4
- package/types/controllers/common/statics.d.ts +0 -271
- package/types/controllers/index.d.ts +0 -3
- package/types/controllers/utils/gateway.d.ts +0 -947
- package/types/controllers/utils/index.d.ts +0 -2
- package/types/index.d.ts +0 -15
- package/types/middlewares/common/errorHandler.d.ts +0 -143
- package/types/middlewares/common/forceHTTPS.d.ts +0 -64
- package/types/middlewares/common/hsts.d.ts +0 -111
- package/types/middlewares/common/index.d.ts +0 -4
- package/types/middlewares/html/fastHTML.d.ts +0 -238
- package/types/middlewares/html/index.d.ts +0 -3
- package/types/middlewares/html/showHTML.d.ts +0 -128
- package/types/middlewares/index.d.ts +0 -4
- package/types/middlewares/utils/index.d.ts +0 -2
- package/types/middlewares/utils/versionValidator.d.ts +0 -247
- package/types/services/common/appError.d.ts +0 -89
- package/types/services/common/httpError.d.ts +0 -37
- package/types/services/common/index.d.ts +0 -18
- package/types/services/common/sendFile.d.ts +0 -56
- package/types/services/frontend/frontendFs.d.ts +0 -72
- package/types/services/frontend/index.d.ts +0 -3
- package/types/services/html/htmlGenerator.d.ts +0 -298
- package/types/services/html/index.d.ts +0 -3
- package/types/services/http/apiClient.d.ts +0 -141
- package/types/services/http/http.d.ts +0 -159
- package/types/services/http/index.d.ts +0 -18
- package/types/services/http/responsesBuilder.d.ts +0 -140
- package/types/services/index.d.ts +0 -6
- package/types/services/utils/ensureBearerToken.d.ts +0 -137
- package/types/services/utils/index.d.ts +0 -16
- package/types/types.d.ts +0 -280
- package/types/utils/functions.d.ts +0 -55
- package/types/utils/wrappers.d.ts +0 -127
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import fetch from "node-fetch";
|
|
5
|
+
import urijs from "urijs";
|
|
6
|
+
import { deepAssignWithOverwrite } from "@homer0/deep-assign";
|
|
7
|
+
import { provider } from "../../utils";
|
|
8
|
+
class HTTP {
|
|
9
|
+
constructor({ inject: { logger }, ...options }) {
|
|
10
|
+
__publicField(this, "_logger");
|
|
11
|
+
__publicField(this, "_options");
|
|
12
|
+
this._logger = logger;
|
|
13
|
+
this._options = deepAssignWithOverwrite(
|
|
14
|
+
{
|
|
15
|
+
logRequests: false
|
|
16
|
+
},
|
|
17
|
+
options
|
|
18
|
+
);
|
|
19
|
+
this.fetch = this.fetch.bind(this);
|
|
20
|
+
}
|
|
21
|
+
async fetch(url, options = {}) {
|
|
22
|
+
let useURL = url;
|
|
23
|
+
if (options.qs) {
|
|
24
|
+
useURL = urijs(url).query(options.qs).toString();
|
|
25
|
+
}
|
|
26
|
+
const fetchOptions = {
|
|
27
|
+
method: (options.method || "get").toUpperCase(),
|
|
28
|
+
body: options.body
|
|
29
|
+
};
|
|
30
|
+
let defaultHeaders;
|
|
31
|
+
if (options.req) {
|
|
32
|
+
defaultHeaders = this.getCustomHeadersFromRequest(options.req);
|
|
33
|
+
const ip = this.getIPFromRequest(options.req);
|
|
34
|
+
if (ip) {
|
|
35
|
+
defaultHeaders["x-forwarded-for"] = ip;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const headers = {
|
|
39
|
+
...defaultHeaders,
|
|
40
|
+
...options.headers
|
|
41
|
+
};
|
|
42
|
+
if (Object.keys(headers).length) {
|
|
43
|
+
fetchOptions.headers = this.normalizeHeaders(headers);
|
|
44
|
+
}
|
|
45
|
+
const { logRequests } = this._options;
|
|
46
|
+
if (logRequests) {
|
|
47
|
+
this._logRequest(useURL, fetchOptions);
|
|
48
|
+
}
|
|
49
|
+
const response = await fetch(useURL, fetchOptions);
|
|
50
|
+
if (logRequests) {
|
|
51
|
+
this._logResponse(response);
|
|
52
|
+
}
|
|
53
|
+
return response;
|
|
54
|
+
}
|
|
55
|
+
getIPFromRequest(req) {
|
|
56
|
+
const headerValue = req.headers["x-forwarded-for"];
|
|
57
|
+
if (headerValue) {
|
|
58
|
+
return String(headerValue);
|
|
59
|
+
}
|
|
60
|
+
return req?.connection?.remoteAddress || req?.socket?.remoteAddress;
|
|
61
|
+
}
|
|
62
|
+
getCustomHeadersFromRequest(req, options = {}) {
|
|
63
|
+
const { includeXForwardedHeaders = false } = options;
|
|
64
|
+
return Object.keys(req.headers).reduce((acc, headerName) => {
|
|
65
|
+
if (headerName.startsWith("x-") && (includeXForwardedHeaders || !headerName.startsWith("x-forwarded-"))) {
|
|
66
|
+
acc[headerName] = req.headers[headerName];
|
|
67
|
+
}
|
|
68
|
+
return acc;
|
|
69
|
+
}, {});
|
|
70
|
+
}
|
|
71
|
+
normalizeHeaders(headers) {
|
|
72
|
+
return Object.keys(headers).reduce((acc, name) => {
|
|
73
|
+
const newName = name.split("-").map((part) => part.replace(/^(\w)/, (_, letter) => letter.toUpperCase())).join("-");
|
|
74
|
+
acc[newName] = headers[name];
|
|
75
|
+
return acc;
|
|
76
|
+
}, {});
|
|
77
|
+
}
|
|
78
|
+
get options() {
|
|
79
|
+
return deepAssignWithOverwrite({}, this._options);
|
|
80
|
+
}
|
|
81
|
+
_logRequest(url, options) {
|
|
82
|
+
const { method, headers } = options;
|
|
83
|
+
const prefix = "REQUEST> ";
|
|
84
|
+
const lines = ["--->>", `${prefix}${method} ${url}`];
|
|
85
|
+
if (headers) {
|
|
86
|
+
Object.keys(headers).forEach((header) => {
|
|
87
|
+
const value = headers[header];
|
|
88
|
+
lines.push(`${prefix}${header}: ${value}`);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (options.body) {
|
|
92
|
+
lines.push(`${prefix}body: "${options.body}"`);
|
|
93
|
+
}
|
|
94
|
+
this._logger.info(lines);
|
|
95
|
+
}
|
|
96
|
+
_logResponse(response) {
|
|
97
|
+
const prefix = "RESPONSE> ";
|
|
98
|
+
const lines = [
|
|
99
|
+
"<<---",
|
|
100
|
+
`${prefix}${response.url}`,
|
|
101
|
+
`${prefix}status: ${response.status}`
|
|
102
|
+
];
|
|
103
|
+
response.headers.forEach((value, header) => {
|
|
104
|
+
lines.push(`${prefix}${header}: ${value}`);
|
|
105
|
+
});
|
|
106
|
+
this._logger.info(lines);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const httpProvider = provider((app) => {
|
|
110
|
+
app.set("http", () => {
|
|
111
|
+
const config = app.getConfig();
|
|
112
|
+
const logRequests = config.get("debug.logRequests") === true;
|
|
113
|
+
return new HTTP({
|
|
114
|
+
inject: {
|
|
115
|
+
logger: app.get("logger")
|
|
116
|
+
},
|
|
117
|
+
logRequests
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
export {
|
|
122
|
+
HTTP,
|
|
123
|
+
httpProvider
|
|
124
|
+
};
|
|
125
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/http/http.ts"],"sourcesContent":["import fetch, { type RequestInit, type BodyInit } from 'node-fetch';\nimport urijs from 'urijs';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { provider } from '../../utils';\nimport { Logger, Request, HTTPResponse } from '../../types';\n/**\n * The options to customize the service.\n *\n * @group Services/HTTP\n */\nexport type HTTPOptions = {\n /**\n * Whether or not the service should log the requests and their responses.\n */\n logRequests?: boolean;\n};\n/**\n * The options to construct a {@link HTTP}.\n *\n * @group Services/HTTP\n */\nexport type HTTPContructorOptions = Partial<HTTPOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n logger: Logger;\n };\n};\n/**\n * The allowed formats for the body of a request.\n *\n * @group Services/HTTP\n */\nexport type HTTPFetchBody = string | Record<string | number, unknown> | BodyInit;\n/**\n * The options for a request.\n *\n * @group Services/HTTP\n */\nexport type HTTPFetchOptions = {\n /**\n * The HTTP method.\n *\n * @default 'GET'\n */\n method?: string;\n /**\n * The headers dictionary.\n */\n headers?: Record<string, string>;\n /**\n * The body of the request.\n */\n body?: HTTPFetchBody;\n /**\n * A dictionary of query string parameters.\n */\n qs?: Record<string, unknown>;\n /**\n * A request object generated by the application. This can be used to copy information\n * like custom headers or the IP (for the X-Forwarded-For header).\n */\n req?: Request;\n};\n/**\n * The options for the method that extracts custom headers from a request.\n *\n * @group Services/HTTP\n */\nexport type GetCustomHeadersFromRequestOptions = {\n /**\n * Since the method considers all headers that start with `x-` as custom headers,\n * setting this to `false` is the only way to exclude `x-forwarded-` headers from the\n * list.\n *\n * @default false\n */\n includeXForwardedHeaders?: boolean;\n};\n/**\n * A set of utilities to work with HTTP requests and responses.\n *\n * @group Services\n * @group Services/HTTP\n */\nexport class HTTP {\n /**\n * The service used to log information in the terminal.\n */\n protected readonly _logger: Logger;\n /**\n * The service customization options.\n */\n protected readonly _options: HTTPOptions;\n /**\n * @param options The options to construct the class.\n */\n constructor({ inject: { logger }, ...options }: HTTPContructorOptions) {\n this._logger = logger;\n this._options = deepAssignWithOverwrite(\n {\n logRequests: false,\n },\n options,\n );\n\n this.fetch = this.fetch.bind(this);\n }\n /**\n * Makes a fetch request.\n *\n * @param url The URL to fetch.\n * @param options The custom options for the request.\n */\n async fetch(url: string, options: HTTPFetchOptions = {}): Promise<HTTPResponse> {\n let useURL = url;\n if (options.qs) {\n useURL = urijs(url).query(options.qs).toString();\n }\n\n const fetchOptions: RequestInit = {\n method: (options.method || 'get').toUpperCase(),\n body: options.body as BodyInit,\n };\n\n let defaultHeaders: Record<string, string> | undefined;\n if (options.req) {\n defaultHeaders = this.getCustomHeadersFromRequest(options.req);\n const ip = this.getIPFromRequest(options.req);\n if (ip) {\n defaultHeaders['x-forwarded-for'] = ip;\n }\n }\n const headers = {\n ...defaultHeaders,\n ...options.headers,\n };\n\n if (Object.keys(headers).length) {\n fetchOptions.headers = this.normalizeHeaders(headers);\n }\n\n const { logRequests } = this._options;\n if (logRequests) {\n this._logRequest(useURL, fetchOptions);\n }\n\n const response = await fetch(useURL, fetchOptions);\n if (logRequests) {\n this._logResponse(response);\n }\n\n return response;\n }\n /**\n * Tries to get the IP address from a given request.\n *\n * @param req The request from which it will try to obtain the IP address.\n */\n getIPFromRequest(req: Request): string | undefined {\n const headerValue = req.headers['x-forwarded-for'];\n if (headerValue) {\n return String(headerValue);\n }\n\n return req?.connection?.remoteAddress || req?.socket?.remoteAddress;\n }\n /**\n * Creates a dictionary with all the custom headers a request has. By custom header it\n * means all the headers which name start with `x-`.\n *\n * @param req The request from which it will try to get the headers.\n * @param options The options to customize the behavior with certain headers.\n */\n getCustomHeadersFromRequest(\n req: Request,\n options: GetCustomHeadersFromRequestOptions = {},\n ): Record<string, string> {\n const { includeXForwardedHeaders = false } = options;\n return Object.keys(req.headers).reduce<Record<string, string>>((acc, headerName) => {\n if (\n headerName.startsWith('x-') &&\n (includeXForwardedHeaders || !headerName.startsWith('x-forwarded-'))\n ) {\n acc[headerName] = req.headers[headerName] as string;\n }\n return acc;\n }, {});\n }\n /**\n * It takes a dictionary of headers and normalize the names so each word will start with\n * an upper case character. This is helpful in case you added custom headers and didn't\n * care about the casing, or when copying headers from a server request, as they all\n * come tranformed into lower case.\n *\n * @param headers The dictionary of headers to normalize.\n */\n normalizeHeaders(headers: Record<string, string>): Record<string, string> {\n return Object.keys(headers).reduce<Record<string, string>>((acc, name) => {\n const newName = name\n .split('-')\n .map((part) => part.replace(/^(\\w)/, (_, letter) => letter.toUpperCase()))\n .join('-');\n\n acc[newName] = headers[name]!;\n return acc;\n }, {});\n }\n /**\n * The customization options.\n */\n get options(): Readonly<HTTPOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Logs a request information into the terminal.\n *\n * @param url The request URL.\n * @param options The options for the request.\n */\n protected _logRequest(url: string, options: RequestInit): void {\n const { method, headers } = options;\n const prefix = 'REQUEST> ';\n const lines = ['--->>', `${prefix}${method} ${url}`];\n if (headers) {\n Object.keys(headers).forEach((header) => {\n const value = headers[header as keyof typeof headers];\n lines.push(`${prefix}${header}: ${value}`);\n });\n }\n\n if (options.body) {\n lines.push(`${prefix}body: \"${options.body}\"`);\n }\n\n this._logger.info(lines);\n }\n /**\n * Logs a response information into the terminal.\n *\n * @param response The response to log.\n */\n protected _logResponse(response: HTTPResponse) {\n const prefix = 'RESPONSE> ';\n const lines = [\n '<<---',\n `${prefix}${response.url}`,\n `${prefix}status: ${response.status}`,\n ];\n\n response.headers.forEach((value, header) => {\n lines.push(`${prefix}${header}: ${value}`);\n });\n\n this._logger.info(lines);\n }\n}\n/**\n * The service provider that once registered on the container will set an instance of\n * {@link HTTP} as the `http` service. The provider also checks the `debug.logRequests`\n * setting on the application configuration in order to enable or not the logging of\n * requests/responses.\n *\n * @example\n *\n * // Register it on the container\n * container.register(httpProvider);\n * // Getting access to the service instance\n * const http = container.get<HTTP>('http');\n *\n * @group Providers\n * @group Services/HTTP\n */\nexport const httpProvider = provider((app) => {\n app.set('http', () => {\n const config = app.getConfig();\n const logRequests = config.get<boolean | undefined>('debug.logRequests') === true;\n return new HTTP({\n inject: {\n logger: app.get('logger'),\n },\n logRequests,\n });\n });\n});\n"],"mappings":";;;AAAA,OAAO,WAAgD;AACvD,OAAO,WAAW;AAClB,SAAS,+BAA+B;AACxC,SAAS,gBAAgB;AAmFlB,MAAM,KAAK;AAAA,EAYhB,YAAY,EAAE,QAAQ,EAAE,OAAO,MAAM,QAAQ,GAA0B;AARvE,wBAAmB;AAInB,wBAAmB;AAKjB,SAAK,UAAU;AACf,SAAK,WAAW;AAAA,MACd;AAAA,QACE,aAAa;AAAA,MACf;AAAA,MACA;AAAA,IACF;AAEA,SAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACnC;AAAA,EAOA,MAAM,MAAM,KAAa,UAA4B,CAAC,GAA0B;AAC9E,QAAI,SAAS;AACb,QAAI,QAAQ,IAAI;AACd,eAAS,MAAM,GAAG,EAAE,MAAM,QAAQ,EAAE,EAAE,SAAS;AAAA,IACjD;AAEA,UAAM,eAA4B;AAAA,MAChC,SAAS,QAAQ,UAAU,OAAO,YAAY;AAAA,MAC9C,MAAM,QAAQ;AAAA,IAChB;AAEA,QAAI;AACJ,QAAI,QAAQ,KAAK;AACf,uBAAiB,KAAK,4BAA4B,QAAQ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k-T2T6Q22Z.js";
|
|
2
|
+
import { providers } from "../../utils";
|
|
3
|
+
import { apiClientProvider } from "./apiClient";
|
|
4
|
+
import { httpProvider } from "./http";
|
|
5
|
+
import { responsesBuilderProvider } from "./responsesBuilder";
|
|
6
|
+
const httpServicesProvider = providers({
|
|
7
|
+
apiClientProvider,
|
|
8
|
+
httpProvider,
|
|
9
|
+
responsesBuilderProvider
|
|
10
|
+
});
|
|
11
|
+
export * from "./apiClient";
|
|
12
|
+
export * from "./http";
|
|
13
|
+
export * from "./responsesBuilder";
|
|
14
|
+
export {
|
|
15
|
+
httpServicesProvider
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/http/index.ts"],"sourcesContent":["import { providers } from '../../utils';\nimport { apiClientProvider } from './apiClient';\nimport { httpProvider } from './http';\nimport { responsesBuilderProvider } from './responsesBuilder';\n/**\n * Registers all the HTTP services on the container.\n *\n * - {@link APIClient | apiClient}\n * - {@link HTTP | http}\n * - {@link ResponsesBuilder | responsesBuilder}\n *\n * @example\n *\n * // Register the collection on the container\n * container.register(httpServicesProvider);\n * // Getting access to one the services instance\n * const apiClient = container.get<APIClient>('apiClient');\n *\n * @group Providers\n */\nexport const httpServicesProvider = providers({\n apiClientProvider,\n httpProvider,\n responsesBuilderProvider,\n});\n\nexport * from './apiClient';\nexport * from './http';\nexport * from './responsesBuilder';\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAC7B,SAAS,gCAAgC;AAiBlC,MAAM,uBAAuB,UAAU;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import { provider } from "../../utils";
|
|
5
|
+
const DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE = 700;
|
|
6
|
+
class ResponsesBuilder {
|
|
7
|
+
constructor({ inject: { config, statuses } }) {
|
|
8
|
+
__publicField(this, "_config");
|
|
9
|
+
__publicField(this, "_statuses");
|
|
10
|
+
this._config = config;
|
|
11
|
+
this._statuses = statuses;
|
|
12
|
+
}
|
|
13
|
+
htmlPostMessage(options) {
|
|
14
|
+
const {
|
|
15
|
+
res,
|
|
16
|
+
title,
|
|
17
|
+
message,
|
|
18
|
+
status,
|
|
19
|
+
target = "window.opener",
|
|
20
|
+
close = true,
|
|
21
|
+
closeDelay = DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE
|
|
22
|
+
} = options;
|
|
23
|
+
const prefix = this._config.get("postMessagesPrefix") ?? "";
|
|
24
|
+
const closeCode = close ? `setTimeout(function() { window.close(); }, ${closeDelay});` : "";
|
|
25
|
+
const html = this._htmlTemplate(
|
|
26
|
+
title,
|
|
27
|
+
`
|
|
28
|
+
(function() {
|
|
29
|
+
if (${target}) {
|
|
30
|
+
${target}.postMessage('${prefix}${message}', '*');
|
|
31
|
+
${closeCode}
|
|
32
|
+
}
|
|
33
|
+
})();
|
|
34
|
+
`
|
|
35
|
+
);
|
|
36
|
+
const useStatus = typeof status === "undefined" ? this._statuses("ok") : this._normalizeStatus(status);
|
|
37
|
+
res.setHeader("Content-Type", "text/html");
|
|
38
|
+
res.setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store");
|
|
39
|
+
res.status(useStatus);
|
|
40
|
+
res.write(html);
|
|
41
|
+
res.end();
|
|
42
|
+
}
|
|
43
|
+
json(options) {
|
|
44
|
+
const { res, data, status, metadata = {} } = options;
|
|
45
|
+
const useStatus = typeof status === "undefined" ? this._statuses("ok") : this._normalizeStatus(status);
|
|
46
|
+
res.status(useStatus);
|
|
47
|
+
res.json({
|
|
48
|
+
metadata: {
|
|
49
|
+
version: this._config.get("version"),
|
|
50
|
+
status: useStatus,
|
|
51
|
+
...metadata
|
|
52
|
+
},
|
|
53
|
+
data
|
|
54
|
+
});
|
|
55
|
+
res.end();
|
|
56
|
+
}
|
|
57
|
+
_htmlTemplate(title, code) {
|
|
58
|
+
return `
|
|
59
|
+
<!DOCTYPE html>
|
|
60
|
+
<html>
|
|
61
|
+
<head>
|
|
62
|
+
<meta charset="utf-8" />
|
|
63
|
+
<meta http-equiv="x-ua-compatible" content="ie=edge" />
|
|
64
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
65
|
+
<title>${title}</title>
|
|
66
|
+
</head>
|
|
67
|
+
<body>
|
|
68
|
+
<script type="text/javascript">
|
|
69
|
+
${code}
|
|
70
|
+
<\/script>
|
|
71
|
+
</body>
|
|
72
|
+
</html>
|
|
73
|
+
`;
|
|
74
|
+
}
|
|
75
|
+
_normalizeStatus(status) {
|
|
76
|
+
let useStatus;
|
|
77
|
+
try {
|
|
78
|
+
if (typeof status === "string") {
|
|
79
|
+
useStatus = this._statuses(status);
|
|
80
|
+
} else {
|
|
81
|
+
this._statuses(status);
|
|
82
|
+
useStatus = status;
|
|
83
|
+
}
|
|
84
|
+
} catch (_) {
|
|
85
|
+
useStatus = this._statuses("ok");
|
|
86
|
+
}
|
|
87
|
+
return useStatus;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const responsesBuilderProvider = provider((app) => {
|
|
91
|
+
app.set(
|
|
92
|
+
"responsesBuilder",
|
|
93
|
+
() => new ResponsesBuilder({
|
|
94
|
+
inject: {
|
|
95
|
+
config: app.getConfig(),
|
|
96
|
+
statuses: app.get("statuses")
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
export {
|
|
102
|
+
ResponsesBuilder,
|
|
103
|
+
responsesBuilderProvider
|
|
104
|
+
};
|
|
105
|
+
//# sourceMappingURL=responsesBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/http/responsesBuilder.ts"],"sourcesContent":["import type { Config, Response } from '../../types';\nimport { provider, type Statuses } from '../../utils';\n/**\n * The options to construct a {@link ResponsesBuilder}.\n *\n * @group Services/ResponsesBuilder\n */\nexport type ResponsesBuilderConstructorOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n config: Config;\n statuses: Statuses;\n };\n};\n/**\n * The options to build a response for a post message.\n *\n * @group Services/ResponsesBuilder\n */\nexport type HTMLPostMessageResponseOptions = {\n /**\n * The response object generated by the application.\n */\n res: Response;\n /**\n * The title for the HTML.\n */\n title: string;\n /**\n * The contents of the post message.\n */\n message: string;\n /**\n * The status code for the response.\n *\n * @default 200\n */\n status?: number | string;\n /**\n * From which object will the `postMessage` method called from.\n *\n * @default 'window.opener'\n */\n target?: string;\n /**\n * Whether or not to call `window.close` after sending the message.\n *\n * @default true\n */\n close?: boolean;\n /**\n * In case `close` is set to `true`, this option is to specify how many milliseconds\n * should be waited before closing the window.\n *\n * @default 700\n */\n closeDelay?: number;\n};\n/**\n * The options to build a reponse for JSON.\n *\n * @group Services/ResponsesBuilder\n */\nexport type JSONResponseOptions = {\n /**\n * The response object generated by the application.\n */\n res: Response;\n /**\n * The actual data for the response. It will be added in a `data` property.\n */\n data: unknown;\n /**\n * The status code for the response.\n *\n * @default 200\n */\n status?: number | string;\n /**\n * Extra information to include inside the `metadata` object.\n */\n metadata?: object;\n};\n/**\n * The default time to wait before closing a window, in case it's enabled.\n */\nconst DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE = 700;\n/**\n * A utility service to build responses.\n *\n * @group Services\n * @group Services/ResponsesBuilder\n */\nexport class ResponsesBuilder {\n /**\n * The application configuration, to get the `version` and the `postMessagesPrefix`.\n */\n protected readonly _config: Config;\n /**\n * The uility service to get HTTP status codes.\n */\n protected readonly _statuses: Statuses;\n /**\n * @param options The options to construct the service.\n */\n constructor({ inject: { config, statuses } }: ResponsesBuilderConstructorOptions) {\n this._config = config;\n this._statuses = statuses;\n }\n /**\n * Generates and send an HTML response that emits a post message.\n * The post message will be prefixed with the value of the configuration setting\n * `postMessagesPrefix`.\n *\n * @param options The options to customize how the HTML is generated.\n */\n htmlPostMessage(options: HTMLPostMessageResponseOptions): void {\n const {\n res,\n title,\n message,\n status,\n target = 'window.opener',\n close = true,\n closeDelay = DEFAULT_CLOSE_DELAY_FOR_POST_MESSAGE,\n } = options;\n const prefix = this._config.get<string | undefined>('postMessagesPrefix') ?? '';\n const closeCode = close\n ? `setTimeout(function() { window.close(); }, ${closeDelay});`\n : '';\n\n const html = this._htmlTemplate(\n title,\n `\n (function() {\n if (${target}) {\n ${target}.postMessage('${prefix}${message}', '*');\n ${closeCode}\n }\n })();\n `,\n );\n\n const useStatus =\n typeof status === 'undefined'\n ? (this._statuses('ok') as number)\n : this._normalizeStatus(status);\n\n res.setHeader('Content-Type', 'text/html');\n res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store');\n res.status(useStatus);\n res.write(html);\n res.end();\n }\n /**\n * Generates and sends a JSON response.\n *\n * @param options The options to customize how the JSON is generated.\n * @example\n *\n * <caption>The generated looks like this.</caption>\n *\n * {\n * metadata: {\n * version: 'dev',\n * status: 200,\n * },\n * data: { foo: 'bar' },\n * }\n *\n */\n json(options: JSONResponseOptions): void {\n const { res, data, status, metadata = {} } = options;\n const useStatus =\n typeof status === 'undefined'\n ? (this._statuses('ok') as number)\n : this._normalizeStatus(status);\n\n res.status(useStatus);\n res.json({\n metadata: {\n version: this._config.get<string>('version'),\n status: useStatus,\n ...metadata,\n },\n data,\n });\n res.end();\n }\n /**\n * Generates a basic HTML template for the service to use when generating a post message\n * response.\n *\n * @param title The HTML `<title />` attribute.\n * @param code Javascript code to be wrapped on a `<script />` tag.\n */\n protected _htmlTemplate(title: string, code: string): string {\n return `\n <!DOCTYPE html>\n <html>\n <head>\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"x-ua-compatible\" content=\"ie=edge\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <title>${title}</title>\n </head>\n <body>\n <script type=\"text/javascript\">\n ${code}\n </script>\n </body>\n </html>\n `;\n }\n /**\n * Utility method used to make sure a recevied status is a valid status code. If the\n * status is a string, the method will try to find the code from the `statuses` package.\n *\n * @param status The status to normalize.\n * @returns If `status` is a string, but there's no valid code, it will return 200.\n */\n protected _normalizeStatus(status: number | string): number {\n let useStatus: number;\n try {\n if (typeof status === 'string') {\n useStatus = this._statuses(status) as number;\n } else {\n this._statuses(status);\n useStatus = status;\n }\n } catch (_) {\n useStatus = this._statuses('ok') as number;\n }\n\n return useStatus;\n }\n}\n/**\n * The service provider that once registered on the container will set an instance of\n * {@link ResponsesBuilder} as the `responsesBuilder` service.\n *\n * @example\n *\n * // Register it on the container\n * container.register(responsesBuilderProvider);\n * // Getting access to the service instance\n * const responsesBuilder = container.get<ResponsesBuilder>('responsesBuilder');\n *\n * @group Providers\n * @group Services/ResponsesBuilder\n */\nexport const responsesBuilderProvider = provider((app) => {\n app.set(\n 'responsesBuilder',\n () =>\n new ResponsesBuilder({\n inject: {\n config: app.getConfig(),\n statuses: app.get('statuses'),\n },\n }),\n );\n});\n"],"mappings":";;;AACA,SAAS,gBAA+B;AAuFxC,MAAM,uCAAuC;AAOtC,MAAM,iBAAiB;AAAA,EAY5B,YAAY,EAAE,QAAQ,EAAE,QAAQ,SAAS,EAAE,GAAuC;AARlF,wBAAmB;AAInB,wBAAmB;AAKjB,SAAK,UAAU;AACf,SAAK,YAAY;AAAA,EACnB;AAAA,EAQA,gBAAgB,SAA+C;AAC7D,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,SAAS;AAAA,MACT,QAAQ;AAAA,MACR,aAAa;AAAA,IACf,IAAI;AACJ,UAAM,SAAS,KAAK,QAAQ,IAAwB,oBAAoB,KAAK;AAC7E,UAAM,YAAY,QACd,8CAA8C,iBAC9C;AAEJ,UAAM,OAAO,KAAK;AAAA,MAChB;AAAA,MACA;A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,QACN,QAAQ,IAAI,UAAU;AAAA,QACtB,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,IACF,CAAC;AAAA,EACL;AACF,CAAC;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/services/index.ts"],"sourcesContent":["export * from './common';\nexport * from './frontend';\nexport * from './html';\nexport * from './http';\nexport * from './utils';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "../../chunk-T2T6Q22Z.js";
|
|
4
|
+
import { deepAssignWithOverwrite } from "@homer0/deep-assign";
|
|
5
|
+
import { providerCreator } from "../../utils";
|
|
6
|
+
class EnsureBearerToken {
|
|
7
|
+
constructor({
|
|
8
|
+
inject: { HTTPError, statuses },
|
|
9
|
+
...options
|
|
10
|
+
}) {
|
|
11
|
+
__publicField(this, "_HTTPError");
|
|
12
|
+
__publicField(this, "_options");
|
|
13
|
+
this._HTTPError = HTTPError;
|
|
14
|
+
this._options = deepAssignWithOverwrite(
|
|
15
|
+
{
|
|
16
|
+
error: {
|
|
17
|
+
message: "Unauthorized",
|
|
18
|
+
status: statuses("unauthorized"),
|
|
19
|
+
response: {}
|
|
20
|
+
},
|
|
21
|
+
expression: /bearer (.*?)(?:$|\s)/i,
|
|
22
|
+
local: "token"
|
|
23
|
+
},
|
|
24
|
+
options
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
getMiddleware() {
|
|
28
|
+
return (req, res, next) => {
|
|
29
|
+
let unauthorized = true;
|
|
30
|
+
const {
|
|
31
|
+
headers: { authorization }
|
|
32
|
+
} = req;
|
|
33
|
+
if (authorization) {
|
|
34
|
+
const matches = this._options.expression.exec(authorization);
|
|
35
|
+
if (matches) {
|
|
36
|
+
const [, token] = matches;
|
|
37
|
+
res.locals[this._options.local] = token;
|
|
38
|
+
unauthorized = false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (unauthorized) {
|
|
42
|
+
const {
|
|
43
|
+
error: { message, status, response }
|
|
44
|
+
} = this._options;
|
|
45
|
+
next(
|
|
46
|
+
new this._HTTPError(message, status, {
|
|
47
|
+
response
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
} else {
|
|
51
|
+
next();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
get options() {
|
|
56
|
+
return deepAssignWithOverwrite({}, this._options);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const ensureBearerTokenProvider = providerCreator(
|
|
60
|
+
(options = {}) => (app) => {
|
|
61
|
+
const { serviceName = "ensureBearerToken", ...rest } = options;
|
|
62
|
+
app.set(
|
|
63
|
+
serviceName,
|
|
64
|
+
() => new EnsureBearerToken({
|
|
65
|
+
inject: {
|
|
66
|
+
HTTPError: app.get("HTTPError"),
|
|
67
|
+
statuses: app.get("statuses")
|
|
68
|
+
},
|
|
69
|
+
...rest
|
|
70
|
+
}).getMiddleware()
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
export {
|
|
75
|
+
EnsureBearerToken,
|
|
76
|
+
ensureBearerTokenProvider
|
|
77
|
+
};
|
|
78
|
+
//# sourceMappingURL=ensureBearerToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/utils/ensureBearerToken.ts"],"sourcesContent":["import { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { providerCreator, type Statuses } from '../../utils';\nimport type { HTTPErrorClass } from '../common';\nimport type { DeepPartial, ExpressMiddleware } from '../../types';\n/**\n * The options for the error the middleare can generate.\n *\n * @group Services/EnsureBearerToken\n */\nexport type EnsureBearerTokenErrorOptions = {\n /**\n * The error message for the response.\n *\n * @default 'Unauthorized'\n */\n message: string;\n /**\n * The HTTP status that will be added to the error context.\n *\n * @default 401\n */\n status: number;\n /**\n * Context information for the error handler and that can be added to the actual\n * response.\n */\n response: unknown;\n};\n/**\n * The options to customize the service/middleware.\n *\n * @group Services/EnsureBearerToken\n */\nexport type EnsureBearerTokenOptions = {\n error: EnsureBearerTokenErrorOptions;\n expression: RegExp;\n local: string;\n};\n/**\n * The options to construct a {@link EnsureBearerToken}.\n *\n * @group Services/EnsureBearerToken\n */\nexport type EnsureBearerConstructorOptions = DeepPartial<EnsureBearerTokenOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n HTTPError: HTTPErrorClass;\n statuses: Statuses;\n };\n};\n/**\n * Custom options for the provider that will register an instance of\n * {@link EnsureBearerToken}\n * as a service.\n *\n * @group Services/EnsureBearerToken\n */\nexport type EnsureBearerTokenProviderOptions = DeepPartial<EnsureBearerTokenOptions> & {\n /**\n * The name that will be used to register the service on the container. This is to allow\n * multiple \"instances\" of the service to be created.\n *\n * @default 'ensureBearerToken'\n */\n serviceName?: string;\n};\n/**\n * This service gives you a middleware that verifies if a request has an `Authorization`\n * header with a bearer token; if it does, the token will be saved on the `res.locals`,\n * otherwise, it will generate an error.\n *\n * This is a \"service middleware\" to allow certain flexibility: you can have controllers\n * where some routes are protected and others are not. For those cases, you get the\n * service from the container, and include it only in the routes that need it.\n *\n * @group Services\n * @group Services/EnsureBearerToken\n */\nexport class EnsureBearerToken {\n /**\n * To generate the errors when the validation fails.\n */\n protected readonly _HTTPError: HTTPErrorClass;\n /**\n * The customization options for the service.\n */\n protected readonly _options: EnsureBearerTokenOptions;\n /**\n * @param options The options to construct the class.\n */\n constructor({\n inject: { HTTPError, statuses },\n ...options\n }: EnsureBearerConstructorOptions) {\n this._HTTPError = HTTPError;\n this._options = deepAssignWithOverwrite(\n {\n error: {\n message: 'Unauthorized',\n status: statuses('unauthorized'),\n response: {},\n },\n expression: /bearer (.*?)(?:$|\\s)/i,\n local: 'token',\n },\n options,\n );\n }\n /**\n * Generates the middleware that verifies if a request has an `Authorization` header\n * with a bearer token.\n */\n getMiddleware(): ExpressMiddleware {\n return (req, res, next) => {\n let unauthorized = true;\n const {\n headers: { authorization },\n } = req;\n if (authorization) {\n const matches = this._options.expression.exec(authorization);\n if (matches) {\n const [, token] = matches;\n res.locals[this._options.local] = token;\n unauthorized = false;\n }\n }\n\n if (unauthorized) {\n const {\n error: { message, status, response },\n } = this._options;\n next(\n new this._HTTPError(message, status, {\n response,\n }),\n );\n } else {\n next();\n }\n };\n }\n /**\n * The customization options.\n */\n get options(): Readonly<EnsureBearerTokenOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n}\n/**\n * Generates a \"service middleware\" that can be used on route controllers in order to\n * validate the presence of a bearer token on the requests authorization header.\n *\n * The registered service is an instance of {@link EnsureBearerToken}, and it uses the key\n * `ensureBearerToken`.\n *\n * Since it's a \"provider creator\", when registering it, you can pass custom options.\n *\n * @example\n *\n * <caption>Basic usage</caption>\n *\n * // Register it on the container\n * container.register(ensureBearerTokenProvider);\n *\n * // Let's assume we are in a controller now...\n * // Getting access to the middleware.\n * const ensureBearerToken = container.get<ExpressMiddleware>('ensureBearerToken');\n *\n * @example\n *\n * <caption>Customizing the service</caption>\n *\n * // Register it on the container\n * container.register(\n * ensureBearerTokenProvider({\n * serviceName: 'ensureBearerTokenCustom',\n * error: {\n * message: 'Missing token!',\n * },\n * }),\n * );\n *\n * @group Providers\n * @group Services/EnsureBearerToken\n */\nexport const ensureBearerTokenProvider = providerCreator(\n (options: EnsureBearerTokenProviderOptions = {}) =>\n (app) => {\n const { serviceName = 'ensureBearerToken', ...rest } = options;\n app.set(serviceName, () =>\n new EnsureBearerToken({\n inject: {\n HTTPError: app.get('HTTPError'),\n statuses: app.get('statuses'),\n },\n ...rest,\n }).getMiddleware(),\n );\n },\n);\n"],"mappings":";;;AAAA,SAAS,+BAA+B;AACxC,SAAS,uBAAsC;AA+ExC,MAAM,kBAAkB;AAAA,EAY7B,YAAY;AAAA,IACV,QAAQ,EAAE,WAAW,SAAS;AAAA,OA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AG;AAAA,MACL,CAAC,EAAE,cAAc;AAAA,IACnB;AAAA,EACF;AACJ;","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { providers } from "../../utils";
|
|
3
|
+
import { ensureBearerTokenProvider } from "./ensureBearerToken";
|
|
4
|
+
const utilsServicesProvider = providers({
|
|
5
|
+
ensureBearerTokenProvider
|
|
6
|
+
});
|
|
7
|
+
export * from "./ensureBearerToken";
|
|
8
|
+
export {
|
|
9
|
+
utilsServicesProvider
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/utils/index.ts"],"sourcesContent":["import { providers } from '../../utils';\nimport { ensureBearerTokenProvider } from './ensureBearerToken';\n/**\n * Registers all the utility services on the container.\n *\n * - {@link EnsureBearerToken | ensureBearerToken}\n *\n * @example\n *\n * // Register the collection on the container\n * container.register(utilsServicesProvider);\n * // Getting access to one the services instance\n * const ensureBearerToken = container.get<EnsureBearerToken>('ensureBearerToken');\n *\n * @group Providers\n */\nexport const utilsServicesProvider = providers({\n ensureBearerTokenProvider,\n});\n\nexport * from './ensureBearerToken';\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,iCAAiC;AAenC,MAAM,wBAAwB,UAAU;AAAA,EAC7C;AACF,CAAC;AAED,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=express.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/types/index.ts"],"sourcesContent":["export * from './events';\nexport * from './express';\nexport * from './http';\nexport * from './options';\nexport * from './utils';\nexport * from './wootils';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=wootils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/fns/index.ts"],"sourcesContent":["export * from './others';\nexport * from './routes';\nexport * from './statuses';\nexport * from './text';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/fns/others.ts"],"sourcesContent":["/**\n * A utility function that can be used in `.filter` calls to remove `undefined` values and\n * assert that the type is not longer `undefined`.\n *\n * @param value The value to check.\n * @template T The type that is not `undefined`.\n * @example\n *\n * const arr: (number | undefined)[] = [1, 2, 3, undefined];\n * const filtered = arr.filter(notUndefined);\n * // filtered = [1, 2, 3] and its type is number[]\n *\n * @group Utilities\n */\nexport const notUndefined = <T>(value: T | undefined): value is T => value !== undefined;\n"],"mappings":";AAcO,MAAM,eAAe,CAAI,UAAqC,UAAU;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "../../chunk-T2T6Q22Z.js";
|
|
2
|
+
import { escapeForRegExp } from "./text";
|
|
3
|
+
const removeLeadingSlash = (url) => url.replace(/^\/+/, "");
|
|
4
|
+
const removeTrailingSlash = (url) => url.replace(/\/+$/, "");
|
|
5
|
+
const removeSlashes = (url, leading = true, trailing = true) => {
|
|
6
|
+
const newUrl = leading ? removeLeadingSlash(url) : url;
|
|
7
|
+
return trailing ? removeTrailingSlash(newUrl) : newUrl;
|
|
8
|
+
};
|
|
9
|
+
const createRouteExpression = (route, leadingSlash = true, trailingSlash = false) => {
|
|
10
|
+
let expression = removeSlashes(route).split("/").map((part) => part.startsWith(":") ? "(?:([^\\/]+?))" : escapeForRegExp(part)).join("\\/");
|
|
11
|
+
if (leadingSlash) {
|
|
12
|
+
expression = `\\/${expression}`;
|
|
13
|
+
}
|
|
14
|
+
if (trailingSlash) {
|
|
15
|
+
expression = `${expression}\\/`;
|
|
16
|
+
}
|
|
17
|
+
return new RegExp(expression);
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
createRouteExpression,
|
|
21
|
+
removeLeadingSlash,
|
|
22
|
+
removeSlashes,
|
|
23
|
+
removeTrailingSlash
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=routes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/fns/routes.ts"],"sourcesContent":["import { escapeForRegExp } from './text';\n\n/**\n * Removes any leading slash from a URL.\n *\n * @param url The URL to format.\n * @group Utilities\n */\nexport const removeLeadingSlash = (url: string): string => url.replace(/^\\/+/, '');\n/**\n * Removes any trailing slash from a URL.\n *\n * @param url The URL to format.\n * @group Utilities\n */\nexport const removeTrailingSlash = (url: string): string => url.replace(/\\/+$/, '');\n/**\n * Remove any leading and trailing slash from a URL.\n *\n * @param url The URL to format.\n * @param leading Whether or not to remove any leading slash.\n * @param trailing Whether or not to remove the trailing slash.\n * @group Utilities\n */\nexport const removeSlashes = (\n url: string,\n leading: boolean = true,\n trailing: boolean = true,\n) => {\n const newUrl = leading ? removeLeadingSlash(url) : url;\n return trailing ? removeTrailingSlash(newUrl) : newUrl;\n};\n\n/**\n * Given a server route definition, this function creates a regular expression to match\n * it: The expression replaces the routes parameters with placeholders so it can be\n * compared with real routes.\n *\n * @param route The route from which the expression will be created.\n * @param leadingSlash Whether or not the expression should match a leading slash.\n * @param trailingSlash Whether or not the expression should match a trailing slash.\n * The reason this is `false` by default is because these\n * expressions are often used to match against incoming requests,\n * and they don't have a trailing slash.\n * @group Utilities\n */\nexport const createRouteExpression = (\n route: string,\n leadingSlash: boolean = true,\n trailingSlash: boolean = false,\n): RegExp => {\n let expression = removeSlashes(route)\n .split('/')\n .map((part) => (part.startsWith(':') ? '(?:([^\\\\/]+?))' : escapeForRegExp(part)))\n .join('\\\\/');\n if (leadingSlash) {\n expression = `\\\\/${expression}`;\n }\n if (trailingSlash) {\n expression = `${expression}\\\\/`;\n }\n\n return new RegExp(expression);\n};\n"],"mappings":";AAAA,SAAS,uBAAuB;AAQzB,MAAM,qBAAqB,CAAC,QAAwB,IAAI,QAAQ,QAAQ,EAAE;AAO1E,MAAM,sBAAsB,CAAC,QAAwB,IAAI,QAAQ,QAAQ,EAAE;AAS3E,MAAM,gBAAgB,CAC3B,KACA,UAAmB,MACnB,WAAoB,SACjB;AACH,QAAM,SAAS,UAAU,mBAAmB,GAAG,IAAI;AACnD,SAAO,WAAW,oBAAoB,MAAM,IAAI;AAClD;AAeO,MAAM,wBAAwB,CACnC,OACA,eAAwB,MACxB,gBAAyB,UACd;AACX,MAAI,aAAa,cAAc,KAAK,EACjC,MAAM,GAAG,EACT,IAAI,CAAC,SAAU,KAAK,WAAW,GAAG,IAAI,mBAAmB,gBAAgB,IAAI,CAAE,EAC/E,KAAK,KAAK;AACb,MAAI,cAAc;AAChB,iBAAa,MAAM;AAAA,EACrB;AACA,MAAI,eAAe;AACjB,iBAAa,GAAG;AAAA,EAClB;AAEA,SAAO,IAAI,OAAO,UAAU;AAC9B;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/fns/statuses.ts"],"sourcesContent":["import statuses from 'statuses';\n/**\n * The type definitions for the `statuses` library.\n *\n * While the library has type definitions, TypeScript throws an error that they can't be\n * exported, so that's why they are copied in there.\n */\nexport type Statuses = {\n /**\n * Given a status code, returns the status text, and given a status text, returns the\n * status code.\n *\n * @param code The status code or status text to look up.\n * @throws If the status is invalid.\n */\n (code: number | string): number | string;\n /**\n * A list of all the supported status codes.\n */\n codes: number[];\n /**\n * A dictionary with the status texts and their corresponding status codes.\n */\n code: Record<string, number | undefined>;\n /**\n * A dictionary with the status codes and their corresponding status texts, for\n * responses that expect an empty body.\n */\n empty: Record<number, boolean | undefined>;\n /**\n * A dictionary with the status codes and their corresponding status texts.\n */\n message: Record<number, string | undefined>;\n /**\n * A dictionary with status codes for responses that are valid for redirections.\n */\n redirect: Record<number, boolean | undefined>;\n /**\n * A dictionary with status codes that should be retried.\n */\n retry: Record<number, boolean | undefined>;\n};\n\nexport { statuses };\n"],"mappings":";AAAA,OAAO,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/fns/text.ts"],"sourcesContent":["/**\n * Escapes a string to be used on `new RegExp(...)`.\n *\n * @param text The text to escape.\n * @group Utilities\n */\nexport const escapeForRegExp = (text: string): string =>\n text.replace(/[-[\\]{}()*+?.,\\\\^$|#\\s]/g, '\\\\$&');\n"],"mappings":";AAMO,MAAM,kBAAkB,CAAC,SAC9B,KAAK,QAAQ,4BAA4B,MAAM;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/utils/index.ts"],"sourcesContent":["export * from './fns';\nexport * from './wrappers';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import "../chunk-T2T6Q22Z.js";
|
|
2
|
+
import {
|
|
3
|
+
createProvider,
|
|
4
|
+
createProviderCreator,
|
|
5
|
+
createProviders,
|
|
6
|
+
resourceFactory,
|
|
7
|
+
resourceCreatorFactory
|
|
8
|
+
} from "@homer0/jimple";
|
|
9
|
+
const provider = createProvider();
|
|
10
|
+
const providerCreator = createProviderCreator();
|
|
11
|
+
const providers = createProviders();
|
|
12
|
+
const controllerFactory = resourceFactory();
|
|
13
|
+
const controller = (connect) => controllerFactory("controller", "connect", connect);
|
|
14
|
+
const controllerCreatorFactory = resourceCreatorFactory();
|
|
15
|
+
const controllerCreator = (creator) => controllerCreatorFactory("controller", "connect", creator);
|
|
16
|
+
const controllerProviderFactory = resourceFactory();
|
|
17
|
+
const controllerProvider = (register) => controllerProviderFactory("provider", "register", register);
|
|
18
|
+
const controllerProviderCreatorFactory = resourceCreatorFactory();
|
|
19
|
+
const controllerProviderCreator = (creator) => controllerProviderCreatorFactory("provider", "register", creator);
|
|
20
|
+
const middlewareFactory = resourceFactory();
|
|
21
|
+
const middleware = (connect) => middlewareFactory("middleware", "connect", connect);
|
|
22
|
+
const middlewareCreatorFactory = resourceCreatorFactory();
|
|
23
|
+
const middlewareCreator = (creator) => middlewareCreatorFactory("middleware", "connect", creator);
|
|
24
|
+
const middlewareProviderFactory = resourceFactory();
|
|
25
|
+
const middlewareProvider = (register) => middlewareProviderFactory("provider", "register", register);
|
|
26
|
+
const middlewareProviderCreatorFactory = resourceCreatorFactory();
|
|
27
|
+
const middlewareProviderCreator = (creator) => middlewareProviderCreatorFactory("provider", "register", creator);
|
|
28
|
+
export {
|
|
29
|
+
controller,
|
|
30
|
+
controllerCreator,
|
|
31
|
+
controllerProvider,
|
|
32
|
+
controllerProviderCreator,
|
|
33
|
+
middleware,
|
|
34
|
+
middlewareCreator,
|
|
35
|
+
middlewareProvider,
|
|
36
|
+
middlewareProviderCreator,
|
|
37
|
+
provider,
|
|
38
|
+
providerCreator,
|
|
39
|
+
providers
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=wrappers.js.map
|