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,221 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APIClient as APIClientBase,
|
|
3
|
+
type ErrorResponse,
|
|
4
|
+
type APIClientOptions,
|
|
5
|
+
type FetchClient,
|
|
6
|
+
} from '@homer0/api-utils';
|
|
7
|
+
import { deepAssignWithOverwrite } from '@homer0/deep-assign';
|
|
8
|
+
import { providerCreator } from '../../utils';
|
|
9
|
+
import type { HTTP } from './http';
|
|
10
|
+
import type { HTTPErrorClass } from '../common';
|
|
11
|
+
|
|
12
|
+
export type { ErrorResponse };
|
|
13
|
+
/**
|
|
14
|
+
* The required settings needed to create the an API client.
|
|
15
|
+
*
|
|
16
|
+
* @group Services/APIClient
|
|
17
|
+
*/
|
|
18
|
+
export type APIClientConfig = Pick<APIClientOptions, 'url' | 'endpoints'>;
|
|
19
|
+
/**
|
|
20
|
+
* A dictionary of endpoints for the API client.
|
|
21
|
+
* This is declared as standalone because it needs to reference them multiple times.
|
|
22
|
+
*
|
|
23
|
+
* @group Services/APIClient
|
|
24
|
+
*/
|
|
25
|
+
export type EndpointsType = APIClientOptions['endpoints'];
|
|
26
|
+
/**
|
|
27
|
+
* The format the settings needs to have in the application configuration in order to
|
|
28
|
+
* create a valid API client.
|
|
29
|
+
* The settings may include the endpoints dictionary in the `endpoints` property, or the
|
|
30
|
+
* gateway `property`, with `endpoints` always having priority if both exists.
|
|
31
|
+
* The reason for the `gateway` property to be valid, is in case the application also
|
|
32
|
+
* implements a {@link GatewayController}, using the same property for both things will
|
|
33
|
+
* help reduce the amount of duplicated definitions.
|
|
34
|
+
*
|
|
35
|
+
* @group Services/APIClient
|
|
36
|
+
*/
|
|
37
|
+
export type APIClientSettings = Omit<APIClientConfig, 'endpoints'> &
|
|
38
|
+
(
|
|
39
|
+
| {
|
|
40
|
+
endpoints: EndpointsType;
|
|
41
|
+
gateway?: EndpointsType;
|
|
42
|
+
}
|
|
43
|
+
| {
|
|
44
|
+
endpoints?: EndpointsType;
|
|
45
|
+
gateway: EndpointsType;
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
/**
|
|
49
|
+
* The options to construct a {@link APIClient}.
|
|
50
|
+
*
|
|
51
|
+
* @group Services/APIClient
|
|
52
|
+
*/
|
|
53
|
+
export type APIClientConstructorOptions = APIClientSettings & {
|
|
54
|
+
/**
|
|
55
|
+
* A dictionary with the dependencies to inject.
|
|
56
|
+
*/
|
|
57
|
+
inject: {
|
|
58
|
+
http: HTTP;
|
|
59
|
+
HTTPError: HTTPErrorClass;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* An API client for the application to use. What makes this service special is that it
|
|
64
|
+
* formats received errors using the {@link _HTTPError} class, and as fetch client, it
|
|
65
|
+
* uses the {@link HTTP} service, allowing the application to to internally handle all the
|
|
66
|
+
* requests and responses.
|
|
67
|
+
*
|
|
68
|
+
* The only reason to use the class directly is if you want to subclass it, as you would
|
|
69
|
+
* normally just register the {@link apiClientProvider}.
|
|
70
|
+
*
|
|
71
|
+
* @group Services
|
|
72
|
+
* @group Services/APIClient
|
|
73
|
+
*/
|
|
74
|
+
export class APIClient extends APIClientBase {
|
|
75
|
+
/**
|
|
76
|
+
* The service that makes the requests to the API.
|
|
77
|
+
*/
|
|
78
|
+
protected readonly _http: HTTP;
|
|
79
|
+
/**
|
|
80
|
+
* The class to generate possible errors in the requests.
|
|
81
|
+
*/
|
|
82
|
+
protected readonly _HTTPError: HTTPErrorClass;
|
|
83
|
+
/**
|
|
84
|
+
* The configuration of the API it uses.
|
|
85
|
+
*/
|
|
86
|
+
protected readonly _apiConfig: APIClientConfig;
|
|
87
|
+
/**
|
|
88
|
+
* @param options The options to construct the class.
|
|
89
|
+
*/
|
|
90
|
+
constructor({ inject: { http, HTTPError }, ...rest }: APIClientConstructorOptions) {
|
|
91
|
+
const { endpoints, gateway, url } = rest;
|
|
92
|
+
const useEndpoints = (endpoints || gateway)!;
|
|
93
|
+
const apiConfig = {
|
|
94
|
+
url,
|
|
95
|
+
endpoints: useEndpoints,
|
|
96
|
+
};
|
|
97
|
+
super({
|
|
98
|
+
...apiConfig,
|
|
99
|
+
fetchClient: http.fetch as unknown as FetchClient,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
this._http = http;
|
|
103
|
+
this._HTTPError = HTTPError;
|
|
104
|
+
this._apiConfig = deepAssignWithOverwrite({}, apiConfig);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The configuration for the API.
|
|
108
|
+
*/
|
|
109
|
+
get apiConfig(): Readonly<APIClientConfig> {
|
|
110
|
+
return deepAssignWithOverwrite({}, this._apiConfig);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Tries to obtain a message from an error caused on a failed request.
|
|
114
|
+
*
|
|
115
|
+
* @param response The response from the failed request.
|
|
116
|
+
*/
|
|
117
|
+
protected _getErrorMessageFromResponse(response: unknown) {
|
|
118
|
+
const res = response as {
|
|
119
|
+
error?: string;
|
|
120
|
+
data?: {
|
|
121
|
+
message?: string;
|
|
122
|
+
error?: string;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
if (res.error) return res.error;
|
|
126
|
+
if (res.data) {
|
|
127
|
+
if (res.data.message) return res.data.message;
|
|
128
|
+
if (res.data.error) return res.data.error;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return 'Unexpected error';
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Generates an {@link _HTTPError} from the response of a failed request.
|
|
135
|
+
*
|
|
136
|
+
* @param response The response from the failed request.
|
|
137
|
+
* @param status The status code of the response.
|
|
138
|
+
*/
|
|
139
|
+
protected override formatError<ResponseType extends ErrorResponse>(
|
|
140
|
+
response: ResponseType,
|
|
141
|
+
status: number,
|
|
142
|
+
): Error {
|
|
143
|
+
return new this._HTTPError(this._getErrorMessageFromResponse(response), status);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The options for the provider creator that registers an {@link APIClient} in the
|
|
148
|
+
* container.
|
|
149
|
+
* These options allow the application to register multiple instances for different APIs.
|
|
150
|
+
*
|
|
151
|
+
* @group Services/APIClient
|
|
152
|
+
*/
|
|
153
|
+
export type APIClientProviderOptions = {
|
|
154
|
+
/**
|
|
155
|
+
* The name of the service that will be registered into the app.
|
|
156
|
+
*
|
|
157
|
+
* @default 'apiClient'
|
|
158
|
+
*/
|
|
159
|
+
serviceName?: string;
|
|
160
|
+
/**
|
|
161
|
+
* The name of the configuration setting that has the API information.
|
|
162
|
+
*
|
|
163
|
+
* @default 'api'
|
|
164
|
+
*/
|
|
165
|
+
configSetting?: string;
|
|
166
|
+
/**
|
|
167
|
+
* The class the service will instantiate. It has to extend {@link APIClient}.
|
|
168
|
+
*
|
|
169
|
+
* @default APIClient
|
|
170
|
+
*/
|
|
171
|
+
clientClass?: typeof APIClient;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* The provider creator to register an instance of {@link APIClient} on the container.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
*
|
|
178
|
+
* <caption>Basic usage</caption>
|
|
179
|
+
*
|
|
180
|
+
* // Register it on the container
|
|
181
|
+
* container.register(apiClientProvider);
|
|
182
|
+
* // Getting access to the service instance
|
|
183
|
+
* const apiClient = container.get<APIClient>('apiClient');
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
*
|
|
187
|
+
* <caption>With custom options</caption>
|
|
188
|
+
*
|
|
189
|
+
* container.register(
|
|
190
|
+
* apiClientProvider({
|
|
191
|
+
* serviceName: 'myApiClient',
|
|
192
|
+
* configSetting: 'myApi',
|
|
193
|
+
* }),
|
|
194
|
+
* );
|
|
195
|
+
*
|
|
196
|
+
* @group Providers
|
|
197
|
+
* @group Services/APIClient
|
|
198
|
+
*/
|
|
199
|
+
export const apiClientProvider = providerCreator(
|
|
200
|
+
(options: APIClientProviderOptions = {}) =>
|
|
201
|
+
(app) => {
|
|
202
|
+
const defaultName = 'apiClient';
|
|
203
|
+
const { serviceName = defaultName, clientClass: ClientClass = APIClient } = options;
|
|
204
|
+
let { configSetting } = options;
|
|
205
|
+
if (!configSetting) {
|
|
206
|
+
configSetting = serviceName === defaultName ? 'api' : serviceName;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
app.set(
|
|
210
|
+
serviceName,
|
|
211
|
+
() =>
|
|
212
|
+
new ClientClass({
|
|
213
|
+
inject: {
|
|
214
|
+
http: app.get('http'),
|
|
215
|
+
HTTPError: app.get('HTTPError'),
|
|
216
|
+
},
|
|
217
|
+
...app.getConfig<APIClientSettings>(configSetting!),
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
},
|
|
221
|
+
);
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import fetch, { type RequestInit, type BodyInit } from 'node-fetch';
|
|
2
|
+
import urijs from 'urijs';
|
|
3
|
+
import { deepAssignWithOverwrite } from '@homer0/deep-assign';
|
|
4
|
+
import { provider } from '../../utils';
|
|
5
|
+
import { Logger, Request, HTTPResponse } from '../../types';
|
|
6
|
+
/**
|
|
7
|
+
* The options to customize the service.
|
|
8
|
+
*
|
|
9
|
+
* @group Services/HTTP
|
|
10
|
+
*/
|
|
11
|
+
export type HTTPOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Whether or not the service should log the requests and their responses.
|
|
14
|
+
*/
|
|
15
|
+
logRequests?: boolean;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The options to construct a {@link HTTP}.
|
|
19
|
+
*
|
|
20
|
+
* @group Services/HTTP
|
|
21
|
+
*/
|
|
22
|
+
export type HTTPContructorOptions = Partial<HTTPOptions> & {
|
|
23
|
+
/**
|
|
24
|
+
* A dictionary with the dependencies to inject.
|
|
25
|
+
*/
|
|
26
|
+
inject: {
|
|
27
|
+
logger: Logger;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The allowed formats for the body of a request.
|
|
32
|
+
*
|
|
33
|
+
* @group Services/HTTP
|
|
34
|
+
*/
|
|
35
|
+
export type HTTPFetchBody = string | Record<string | number, unknown> | BodyInit;
|
|
36
|
+
/**
|
|
37
|
+
* The options for a request.
|
|
38
|
+
*
|
|
39
|
+
* @group Services/HTTP
|
|
40
|
+
*/
|
|
41
|
+
export type HTTPFetchOptions = {
|
|
42
|
+
/**
|
|
43
|
+
* The HTTP method.
|
|
44
|
+
*
|
|
45
|
+
* @default 'GET'
|
|
46
|
+
*/
|
|
47
|
+
method?: string;
|
|
48
|
+
/**
|
|
49
|
+
* The headers dictionary.
|
|
50
|
+
*/
|
|
51
|
+
headers?: Record<string, string>;
|
|
52
|
+
/**
|
|
53
|
+
* The body of the request.
|
|
54
|
+
*/
|
|
55
|
+
body?: HTTPFetchBody;
|
|
56
|
+
/**
|
|
57
|
+
* A dictionary of query string parameters.
|
|
58
|
+
*/
|
|
59
|
+
qs?: Record<string, unknown>;
|
|
60
|
+
/**
|
|
61
|
+
* A request object generated by the application. This can be used to copy information
|
|
62
|
+
* like custom headers or the IP (for the X-Forwarded-For header).
|
|
63
|
+
*/
|
|
64
|
+
req?: Request;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* The options for the method that extracts custom headers from a request.
|
|
68
|
+
*
|
|
69
|
+
* @group Services/HTTP
|
|
70
|
+
*/
|
|
71
|
+
export type GetCustomHeadersFromRequestOptions = {
|
|
72
|
+
/**
|
|
73
|
+
* Since the method considers all headers that start with `x-` as custom headers,
|
|
74
|
+
* setting this to `false` is the only way to exclude `x-forwarded-` headers from the
|
|
75
|
+
* list.
|
|
76
|
+
*
|
|
77
|
+
* @default false
|
|
78
|
+
*/
|
|
79
|
+
includeXForwardedHeaders?: boolean;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* A set of utilities to work with HTTP requests and responses.
|
|
83
|
+
*
|
|
84
|
+
* @group Services
|
|
85
|
+
* @group Services/HTTP
|
|
86
|
+
*/
|
|
87
|
+
export class HTTP {
|
|
88
|
+
/**
|
|
89
|
+
* The service used to log information in the terminal.
|
|
90
|
+
*/
|
|
91
|
+
protected readonly _logger: Logger;
|
|
92
|
+
/**
|
|
93
|
+
* The service customization options.
|
|
94
|
+
*/
|
|
95
|
+
protected readonly _options: HTTPOptions;
|
|
96
|
+
/**
|
|
97
|
+
* @param options The options to construct the class.
|
|
98
|
+
*/
|
|
99
|
+
constructor({ inject: { logger }, ...options }: HTTPContructorOptions) {
|
|
100
|
+
this._logger = logger;
|
|
101
|
+
this._options = deepAssignWithOverwrite(
|
|
102
|
+
{
|
|
103
|
+
logRequests: false,
|
|
104
|
+
},
|
|
105
|
+
options,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
this.fetch = this.fetch.bind(this);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Makes a fetch request.
|
|
112
|
+
*
|
|
113
|
+
* @param url The URL to fetch.
|
|
114
|
+
* @param options The custom options for the request.
|
|
115
|
+
*/
|
|
116
|
+
async fetch(url: string, options: HTTPFetchOptions = {}): Promise<HTTPResponse> {
|
|
117
|
+
let useURL = url;
|
|
118
|
+
if (options.qs) {
|
|
119
|
+
useURL = urijs(url).query(options.qs).toString();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const fetchOptions: RequestInit = {
|
|
123
|
+
method: (options.method || 'get').toUpperCase(),
|
|
124
|
+
body: options.body as BodyInit,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
let defaultHeaders: Record<string, string> | undefined;
|
|
128
|
+
if (options.req) {
|
|
129
|
+
defaultHeaders = this.getCustomHeadersFromRequest(options.req);
|
|
130
|
+
const ip = this.getIPFromRequest(options.req);
|
|
131
|
+
if (ip) {
|
|
132
|
+
defaultHeaders['x-forwarded-for'] = ip;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const headers = {
|
|
136
|
+
...defaultHeaders,
|
|
137
|
+
...options.headers,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (Object.keys(headers).length) {
|
|
141
|
+
fetchOptions.headers = this.normalizeHeaders(headers);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const { logRequests } = this._options;
|
|
145
|
+
if (logRequests) {
|
|
146
|
+
this._logRequest(useURL, fetchOptions);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const response = await fetch(useURL, fetchOptions);
|
|
150
|
+
if (logRequests) {
|
|
151
|
+
this._logResponse(response);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return response;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Tries to get the IP address from a given request.
|
|
158
|
+
*
|
|
159
|
+
* @param req The request from which it will try to obtain the IP address.
|
|
160
|
+
*/
|
|
161
|
+
getIPFromRequest(req: Request): string | undefined {
|
|
162
|
+
const headerValue = req.headers['x-forwarded-for'];
|
|
163
|
+
if (headerValue) {
|
|
164
|
+
return String(headerValue);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return req?.connection?.remoteAddress || req?.socket?.remoteAddress;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates a dictionary with all the custom headers a request has. By custom header it
|
|
171
|
+
* means all the headers which name start with `x-`.
|
|
172
|
+
*
|
|
173
|
+
* @param req The request from which it will try to get the headers.
|
|
174
|
+
* @param options The options to customize the behavior with certain headers.
|
|
175
|
+
*/
|
|
176
|
+
getCustomHeadersFromRequest(
|
|
177
|
+
req: Request,
|
|
178
|
+
options: GetCustomHeadersFromRequestOptions = {},
|
|
179
|
+
): Record<string, string> {
|
|
180
|
+
const { includeXForwardedHeaders = false } = options;
|
|
181
|
+
return Object.keys(req.headers).reduce<Record<string, string>>((acc, headerName) => {
|
|
182
|
+
if (
|
|
183
|
+
headerName.startsWith('x-') &&
|
|
184
|
+
(includeXForwardedHeaders || !headerName.startsWith('x-forwarded-'))
|
|
185
|
+
) {
|
|
186
|
+
acc[headerName] = req.headers[headerName] as string;
|
|
187
|
+
}
|
|
188
|
+
return acc;
|
|
189
|
+
}, {});
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* It takes a dictionary of headers and normalize the names so each word will start with
|
|
193
|
+
* an upper case character. This is helpful in case you added custom headers and didn't
|
|
194
|
+
* care about the casing, or when copying headers from a server request, as they all
|
|
195
|
+
* come tranformed into lower case.
|
|
196
|
+
*
|
|
197
|
+
* @param headers The dictionary of headers to normalize.
|
|
198
|
+
*/
|
|
199
|
+
normalizeHeaders(headers: Record<string, string>): Record<string, string> {
|
|
200
|
+
return Object.keys(headers).reduce<Record<string, string>>((acc, name) => {
|
|
201
|
+
const newName = name
|
|
202
|
+
.split('-')
|
|
203
|
+
.map((part) => part.replace(/^(\w)/, (_, letter) => letter.toUpperCase()))
|
|
204
|
+
.join('-');
|
|
205
|
+
|
|
206
|
+
acc[newName] = headers[name]!;
|
|
207
|
+
return acc;
|
|
208
|
+
}, {});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The customization options.
|
|
212
|
+
*/
|
|
213
|
+
get options(): Readonly<HTTPOptions> {
|
|
214
|
+
return deepAssignWithOverwrite({}, this._options);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Logs a request information into the terminal.
|
|
218
|
+
*
|
|
219
|
+
* @param url The request URL.
|
|
220
|
+
* @param options The options for the request.
|
|
221
|
+
*/
|
|
222
|
+
protected _logRequest(url: string, options: RequestInit): void {
|
|
223
|
+
const { method, headers } = options;
|
|
224
|
+
const prefix = 'REQUEST> ';
|
|
225
|
+
const lines = ['--->>', `${prefix}${method} ${url}`];
|
|
226
|
+
if (headers) {
|
|
227
|
+
Object.keys(headers).forEach((header) => {
|
|
228
|
+
const value = headers[header as keyof typeof headers];
|
|
229
|
+
lines.push(`${prefix}${header}: ${value}`);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (options.body) {
|
|
234
|
+
lines.push(`${prefix}body: "${options.body}"`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this._logger.info(lines);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Logs a response information into the terminal.
|
|
241
|
+
*
|
|
242
|
+
* @param response The response to log.
|
|
243
|
+
*/
|
|
244
|
+
protected _logResponse(response: HTTPResponse) {
|
|
245
|
+
const prefix = 'RESPONSE> ';
|
|
246
|
+
const lines = [
|
|
247
|
+
'<<---',
|
|
248
|
+
`${prefix}${response.url}`,
|
|
249
|
+
`${prefix}status: ${response.status}`,
|
|
250
|
+
];
|
|
251
|
+
|
|
252
|
+
response.headers.forEach((value, header) => {
|
|
253
|
+
lines.push(`${prefix}${header}: ${value}`);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
this._logger.info(lines);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The service provider that once registered on the container will set an instance of
|
|
261
|
+
* {@link HTTP} as the `http` service. The provider also checks the `debug.logRequests`
|
|
262
|
+
* setting on the application configuration in order to enable or not the logging of
|
|
263
|
+
* requests/responses.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
*
|
|
267
|
+
* // Register it on the container
|
|
268
|
+
* container.register(httpProvider);
|
|
269
|
+
* // Getting access to the service instance
|
|
270
|
+
* const http = container.get<HTTP>('http');
|
|
271
|
+
*
|
|
272
|
+
* @group Providers
|
|
273
|
+
* @group Services/HTTP
|
|
274
|
+
*/
|
|
275
|
+
export const httpProvider = provider((app) => {
|
|
276
|
+
app.set('http', () => {
|
|
277
|
+
const config = app.getConfig();
|
|
278
|
+
const logRequests = config.get<boolean | undefined>('debug.logRequests') === true;
|
|
279
|
+
return new HTTP({
|
|
280
|
+
inject: {
|
|
281
|
+
logger: app.get('logger'),
|
|
282
|
+
},
|
|
283
|
+
logRequests,
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { providers } from '../../utils';
|
|
2
|
+
import { apiClientProvider } from './apiClient';
|
|
3
|
+
import { httpProvider } from './http';
|
|
4
|
+
import { responsesBuilderProvider } from './responsesBuilder';
|
|
5
|
+
/**
|
|
6
|
+
* Registers all the HTTP services on the container.
|
|
7
|
+
*
|
|
8
|
+
* - {@link APIClient | apiClient}
|
|
9
|
+
* - {@link HTTP | http}
|
|
10
|
+
* - {@link ResponsesBuilder | responsesBuilder}
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* // Register the collection on the container
|
|
15
|
+
* container.register(httpServicesProvider);
|
|
16
|
+
* // Getting access to one the services instance
|
|
17
|
+
* const apiClient = container.get<APIClient>('apiClient');
|
|
18
|
+
*
|
|
19
|
+
* @group Providers
|
|
20
|
+
*/
|
|
21
|
+
export const httpServicesProvider = providers({
|
|
22
|
+
apiClientProvider,
|
|
23
|
+
httpProvider,
|
|
24
|
+
responsesBuilderProvider,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export * from './apiClient';
|
|
28
|
+
export * from './http';
|
|
29
|
+
export * from './responsesBuilder';
|