jimpex 8.0.0 → 10.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 +33 -0
- package/README.md +2 -2
- package/dist/app/index.d.mts +14 -0
- package/dist/app/index.d.ts +5 -4
- package/dist/app/index.js +4 -0
- package/dist/app/jimpex.d.mts +14 -0
- package/dist/app/jimpex.d.ts +3 -2
- package/dist/app/jimpex.js +238 -2
- package/dist/app/jimpex.js.map +1 -1
- package/dist/controllers/common/config.d.mts +79 -0
- package/dist/controllers/common/config.d.ts +4 -3
- package/dist/controllers/common/config.js +18 -0
- package/dist/controllers/common/config.js.map +1 -1
- package/dist/controllers/common/health.d.mts +83 -0
- package/dist/controllers/common/health.d.ts +5 -4
- package/dist/controllers/common/health.js +18 -0
- package/dist/controllers/common/health.js.map +1 -1
- package/dist/controllers/common/index.d.mts +22 -0
- package/dist/controllers/common/index.d.ts +5 -4
- package/dist/controllers/common/index.js +6 -0
- package/dist/controllers/common/statics.d.mts +223 -0
- package/dist/controllers/common/statics.d.ts +13 -5
- package/dist/controllers/common/statics.js +60 -3
- package/dist/controllers/common/statics.js.map +1 -1
- package/dist/controllers/index.d.mts +25 -0
- package/dist/controllers/index.d.ts +5 -4
- package/dist/controllers/index.js +5 -0
- package/dist/controllers/utils/gateway.d.mts +725 -0
- package/dist/controllers/utils/gateway.d.ts +5 -4
- package/dist/controllers/utils/gateway.js +155 -0
- package/dist/controllers/utils/gateway.js.map +1 -1
- package/dist/controllers/utils/index.d.mts +17 -0
- package/dist/controllers/utils/index.d.ts +4 -3
- package/dist/controllers/utils/index.js +4 -0
- package/dist/esm/app/jimpex.js +235 -3
- package/dist/esm/app/jimpex.js.map +1 -1
- package/dist/esm/{chunk-T2T6Q22Z.js → chunk-F3FYYIAV.js} +1 -1
- package/dist/esm/controllers/common/config.js +19 -1
- package/dist/esm/controllers/common/config.js.map +1 -1
- package/dist/esm/controllers/common/health.js +19 -1
- package/dist/esm/controllers/common/health.js.map +1 -1
- package/dist/esm/controllers/common/statics.js +57 -4
- package/dist/esm/controllers/common/statics.js.map +1 -1
- package/dist/esm/controllers/utils/gateway.js +156 -1
- package/dist/esm/controllers/utils/gateway.js.map +1 -1
- package/dist/esm/middlewares/common/errorHandler.js +25 -1
- package/dist/esm/middlewares/common/errorHandler.js.map +1 -1
- package/dist/esm/middlewares/common/forceHTTPS.js +13 -1
- package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -1
- package/dist/esm/middlewares/common/hsts.js +22 -1
- package/dist/esm/middlewares/common/hsts.js.map +1 -1
- package/dist/esm/middlewares/html/fastHTML.js +55 -1
- package/dist/esm/middlewares/html/fastHTML.js.map +1 -1
- package/dist/esm/middlewares/html/showHTML.js +33 -1
- package/dist/esm/middlewares/html/showHTML.js.map +1 -1
- package/dist/esm/middlewares/utils/versionValidator.js +35 -1
- package/dist/esm/middlewares/utils/versionValidator.js.map +1 -1
- package/dist/esm/services/common/appError.js +48 -2
- package/dist/esm/services/common/appError.js.map +1 -1
- package/dist/esm/services/common/httpError.js +10 -1
- package/dist/esm/services/common/httpError.js.map +1 -1
- package/dist/esm/services/common/index.js +1 -1
- package/dist/esm/services/common/sendFile.js +1 -1
- package/dist/esm/services/frontend/frontendFs.js +29 -1
- package/dist/esm/services/frontend/frontendFs.js.map +1 -1
- package/dist/esm/services/frontend/index.js +1 -1
- package/dist/esm/services/html/htmlGenerator.js +51 -1
- package/dist/esm/services/html/htmlGenerator.js.map +1 -1
- package/dist/esm/services/html/index.js +1 -1
- package/dist/esm/services/http/apiClient.js +27 -1
- package/dist/esm/services/http/apiClient.js.map +1 -1
- package/dist/esm/services/http/http.js +60 -5
- package/dist/esm/services/http/http.js.map +1 -1
- package/dist/esm/services/http/index.js +1 -1
- package/dist/esm/services/http/responsesBuilder.js +49 -2
- package/dist/esm/services/http/responsesBuilder.js.map +1 -1
- package/dist/esm/services/utils/ensureBearerToken.js +17 -1
- package/dist/esm/services/utils/ensureBearerToken.js.map +1 -1
- package/dist/esm/services/utils/index.js +1 -1
- package/dist/esm/utils/fns/others.js +1 -1
- package/dist/esm/utils/fns/routes.js +1 -1
- package/dist/esm/utils/fns/routes.js.map +1 -1
- package/dist/esm/utils/fns/statuses.js +1 -1
- package/dist/esm/utils/fns/text.js +1 -1
- package/dist/esm/utils/wrappers.js +1 -1
- package/dist/index-Bwf7JHu9.d.mts +1287 -0
- package/dist/{jimpex-7eaee271.d.ts → index-C6I3NCC-.d.ts} +15 -6
- package/dist/index.d.mts +47 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +9 -0
- package/dist/middlewares/common/errorHandler.d.mts +132 -0
- package/dist/middlewares/common/errorHandler.d.ts +5 -4
- package/dist/middlewares/common/errorHandler.js +24 -0
- package/dist/middlewares/common/errorHandler.js.map +1 -1
- package/dist/middlewares/common/forceHTTPS.d.mts +69 -0
- package/dist/middlewares/common/forceHTTPS.d.ts +6 -5
- package/dist/middlewares/common/forceHTTPS.js +12 -0
- package/dist/middlewares/common/forceHTTPS.js.map +1 -1
- package/dist/middlewares/common/hsts.d.mts +110 -0
- package/dist/middlewares/common/hsts.d.ts +6 -5
- package/dist/middlewares/common/hsts.js +21 -0
- package/dist/middlewares/common/hsts.js.map +1 -1
- package/dist/middlewares/common/index.d.mts +22 -0
- package/dist/middlewares/common/index.d.ts +4 -3
- package/dist/middlewares/common/index.js +6 -0
- package/dist/middlewares/html/fastHTML.d.mts +180 -0
- package/dist/middlewares/html/fastHTML.d.ts +5 -5
- package/dist/middlewares/html/fastHTML.js +54 -0
- package/dist/middlewares/html/fastHTML.js.map +1 -1
- package/dist/middlewares/html/index.d.mts +21 -0
- package/dist/middlewares/html/index.d.ts +4 -4
- package/dist/middlewares/html/index.js +5 -0
- package/dist/middlewares/html/showHTML.d.mts +127 -0
- package/dist/middlewares/html/showHTML.d.ts +5 -5
- package/dist/middlewares/html/showHTML.js +32 -0
- package/dist/middlewares/html/showHTML.js.map +1 -1
- package/dist/middlewares/index.d.mts +30 -0
- package/dist/middlewares/index.d.ts +4 -4
- package/dist/middlewares/index.js +6 -0
- package/dist/middlewares/utils/index.d.mts +20 -0
- package/dist/middlewares/utils/index.d.ts +4 -3
- package/dist/middlewares/utils/index.js +4 -0
- package/dist/middlewares/utils/versionValidator.d.mts +215 -0
- package/dist/middlewares/utils/versionValidator.d.ts +5 -4
- package/dist/middlewares/utils/versionValidator.js +34 -0
- package/dist/middlewares/utils/versionValidator.js.map +1 -1
- package/dist/services/common/appError.d.mts +139 -0
- package/dist/services/common/appError.d.ts +5 -4
- package/dist/services/common/appError.js +47 -1
- package/dist/services/common/appError.js.map +1 -1
- package/dist/services/common/httpError.d.mts +80 -0
- package/dist/services/common/httpError.d.ts +5 -4
- package/dist/services/common/httpError.js +9 -0
- package/dist/services/common/httpError.js.map +1 -1
- package/dist/services/common/index.d.mts +47 -0
- package/dist/services/common/index.d.ts +4 -4
- package/dist/services/common/index.js +4 -1
- package/dist/services/common/sendFile.d.mts +102 -0
- package/dist/services/common/sendFile.d.ts +4 -4
- package/dist/services/frontend/frontendFs.d.mts +96 -0
- package/dist/services/frontend/frontendFs.d.ts +4 -4
- package/dist/services/frontend/frontendFs.js +32 -0
- package/dist/services/frontend/frontendFs.js.map +1 -1
- package/dist/services/frontend/index.d.mts +40 -0
- package/dist/services/frontend/index.d.ts +4 -4
- package/dist/services/frontend/index.js +2 -1
- package/dist/services/html/htmlGenerator.d.mts +237 -0
- package/dist/services/html/htmlGenerator.d.ts +3 -3
- package/dist/services/html/htmlGenerator.js +50 -0
- package/dist/services/html/htmlGenerator.js.map +1 -1
- package/dist/services/html/index.d.mts +43 -0
- package/dist/services/html/index.d.ts +4 -4
- package/dist/services/html/index.js +2 -1
- package/dist/services/http/apiClient.d.mts +170 -0
- package/dist/services/http/apiClient.d.ts +5 -4
- package/dist/services/http/apiClient.js +26 -0
- package/dist/services/http/apiClient.js.map +1 -1
- package/dist/services/http/http.d.mts +182 -0
- package/dist/services/http/http.d.ts +15 -8
- package/dist/services/http/http.js +63 -4
- package/dist/services/http/http.js.map +1 -1
- package/dist/services/http/index.d.mts +51 -0
- package/dist/services/http/index.d.ts +4 -3
- package/dist/services/http/index.js +4 -1
- package/dist/services/http/responsesBuilder.d.mts +179 -0
- package/dist/services/http/responsesBuilder.d.ts +4 -3
- package/dist/services/http/responsesBuilder.js +48 -1
- package/dist/services/http/responsesBuilder.js.map +1 -1
- package/dist/services/index.d.mts +33 -0
- package/dist/services/index.d.ts +4 -4
- package/dist/services/index.js +8 -0
- package/dist/services/utils/ensureBearerToken.d.mts +158 -0
- package/dist/services/utils/ensureBearerToken.d.ts +6 -5
- package/dist/services/utils/ensureBearerToken.js +16 -0
- package/dist/services/utils/ensureBearerToken.js.map +1 -1
- package/dist/services/utils/index.d.mts +44 -0
- package/dist/services/utils/index.d.ts +4 -3
- package/dist/services/utils/index.js +2 -1
- package/dist/types/events.d.mts +14 -0
- package/dist/types/events.d.ts +5 -4
- package/dist/types/express.d.mts +10 -0
- package/dist/types/express.d.ts +1 -1
- package/dist/types/http.d.mts +82 -0
- package/dist/types/http.d.ts +5 -2
- package/dist/types/http.js.map +1 -1
- package/dist/types/index.d.mts +14 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +9 -0
- package/dist/types/options.d.mts +14 -0
- package/dist/types/options.d.ts +4 -3
- package/dist/types/utils.d.mts +48 -0
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/wootils.d.mts +4 -0
- package/dist/utils/fns/index.d.mts +5 -0
- package/dist/utils/fns/index.js +7 -0
- package/dist/utils/fns/others.d.mts +17 -0
- package/dist/utils/fns/routes.d.mts +39 -0
- package/dist/utils/fns/routes.js.map +1 -1
- package/dist/utils/fns/statuses.d.mts +45 -0
- package/dist/utils/fns/statuses.d.ts +1 -1
- package/dist/utils/fns/statuses.js +4 -0
- package/dist/utils/fns/statuses.js.map +1 -1
- package/dist/utils/fns/text.d.mts +9 -0
- package/dist/utils/index.d.mts +19 -0
- package/dist/utils/index.d.ts +5 -4
- package/dist/utils/index.js +5 -0
- package/dist/utils/wrappers.d.mts +14 -0
- package/dist/utils/wrappers.d.ts +4 -3
- package/package.json +50 -49
- package/src/app/jimpex.ts +15 -2
- package/src/controllers/common/statics.ts +13 -2
- package/src/controllers/utils/gateway.ts +4 -0
- package/src/services/http/http.ts +13 -4
- package/src/types/http.ts +3 -0
- /package/dist/esm/{chunk-T2T6Q22Z.js.map → chunk-F3FYYIAV.js.map} +0 -0
|
@@ -30,10 +30,26 @@ module.exports = __toCommonJS(versionValidator_exports);
|
|
|
30
30
|
var import_deep_assign = require("@homer0/deep-assign");
|
|
31
31
|
var import_utils = require("../../utils");
|
|
32
32
|
class VersionValidator {
|
|
33
|
+
/**
|
|
34
|
+
* @param options The options to construct the class.
|
|
35
|
+
* @throws If no `version` is specified in the options.
|
|
36
|
+
*/
|
|
33
37
|
constructor({ inject, version, ...options }) {
|
|
38
|
+
/**
|
|
39
|
+
* To generate the errors in case the validation fails.
|
|
40
|
+
*/
|
|
34
41
|
__publicField(this, "_HTTPError");
|
|
42
|
+
/**
|
|
43
|
+
* To generate responses for popups.
|
|
44
|
+
*/
|
|
35
45
|
__publicField(this, "_responsesBuilder");
|
|
46
|
+
/**
|
|
47
|
+
* The uility service to get HTTP status codes.
|
|
48
|
+
*/
|
|
36
49
|
__publicField(this, "_statuses");
|
|
50
|
+
/**
|
|
51
|
+
* The customization options.
|
|
52
|
+
*/
|
|
37
53
|
__publicField(this, "_options");
|
|
38
54
|
this._HTTPError = inject.HTTPError;
|
|
39
55
|
this._responsesBuilder = inject.responsesBuilder;
|
|
@@ -58,6 +74,9 @@ class VersionValidator {
|
|
|
58
74
|
throw new Error("You need to supply a version");
|
|
59
75
|
}
|
|
60
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Generates the middleware that validates the version.
|
|
79
|
+
*/
|
|
61
80
|
getMiddleware() {
|
|
62
81
|
return (req, res, next) => {
|
|
63
82
|
const { version } = req.params;
|
|
@@ -89,13 +108,28 @@ class VersionValidator {
|
|
|
89
108
|
);
|
|
90
109
|
};
|
|
91
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* The customization options.
|
|
113
|
+
*/
|
|
92
114
|
get options() {
|
|
93
115
|
return (0, import_deep_assign.deepAssignWithOverwrite)({}, this._options);
|
|
94
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Helper method that checks if the incoming request is from a popup. It will look for
|
|
119
|
+
* the query string variable defined in the constructor options.
|
|
120
|
+
*
|
|
121
|
+
* @param req The request object sent by the application.
|
|
122
|
+
*/
|
|
95
123
|
_isPopup(req) {
|
|
96
124
|
const popup = req.query[this._options.popup.variable];
|
|
97
125
|
return !!(popup && String(popup).toLowerCase() === "true");
|
|
98
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Helper method that checks if the "latest version" is enabled and if the given version
|
|
129
|
+
* is "the latest" (comparing it with the option name).
|
|
130
|
+
*
|
|
131
|
+
* @param version The version received in the request.
|
|
132
|
+
*/
|
|
99
133
|
_isTheAllowedLatest(version) {
|
|
100
134
|
const { allow, name } = this._options.latest;
|
|
101
135
|
return allow && version === name;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middlewares/utils/versionValidator.ts"],"sourcesContent":["import { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { middlewareCreator, type Statuses } from '../../utils';\nimport { DeepPartial, Request, ExpressMiddleware } from '../../types';\nimport type { HTTPErrorClass, ResponsesBuilder } from '../../services';\n/**\n * The options for how the middleware should behave if the requested version is `latest`.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorLatestOptions = {\n /**\n * Whether or not the middleware should validate the _\"latest version\"_.\n *\n * @default true\n */\n allow: boolean;\n /**\n * The name of the _\"latest version\"_. Basically, `req.params.version` must match with\n * this property in order to be consider \"latest\".\n *\n * @default 'latest'\n */\n name: string;\n};\n/**\n * The options for how to detect if the request comes from a popup and how to compose the\n * post message the middleware will use to respond.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorPopupOptions = {\n /**\n * The name of the query string variable the middleware will check in order to indentify\n * whether the request comes from a popup or not. The variable must have `'true'` as its\n * value.\n *\n * @default 'popup'\n */\n variable: string;\n /**\n * The title of the page that will be generated to respond in case the versions don't\n * match.\n *\n * @default 'Conflict'\n */\n title: string;\n /**\n * The contents of the post message the generated page will send if the versions don't\n * match.\n *\n * @default 'version:conflict'\n */\n message: string;\n};\n/**\n * The options used to customize a {@link VersionValidator} instance.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorOptions = {\n /**\n * The version used to validate the requests.\n */\n version: string;\n /**\n * The options for how the middleware should behave if the requested version is\n * `latest`.\n */\n latest: VersionValidatorLatestOptions;\n /**\n * The options for how to detect if the request comes from a popup and how to compose\n * the post message the middleware will use to respond.\n */\n popup: VersionValidatorPopupOptions;\n /**\n * The error message to show when the version is invalid.\n *\n * @default \"The application version doesn't match\"\n * @prettierignore\n */\n error: string;\n};\n/**\n * A partial version of the {@link VersionValidatorOptions}, to be used in the constructor\n * and the middleware creator. The reason it omits `version` it's because for the\n * constructor it's required, but for the middleware creator it's not.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorPartialOptions = DeepPartial<\n Omit<VersionValidatorOptions, 'version'>\n>;\n/**\n * The options to construct a {@link VersionValidator}.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorConstructorOptions = VersionValidatorPartialOptions & {\n /**\n * The version used to validate the requests.\n */\n version: string;\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n HTTPError: HTTPErrorClass;\n responsesBuilder: ResponsesBuilder;\n statuses: Statuses;\n };\n};\n/**\n * The options for the middleware creator that will mount an instance of\n * {@link VersionValidator}.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorMiddlewareOptions = VersionValidatorPartialOptions & {\n /**\n * The version used to validate the requests. This is optional in here because if it's\n * not specified, it will be obtained from the configuration service.\n */\n version?: string;\n};\n/**\n * This is the handler for the middleware/controller that validates the application\n * version.\n * This is useful in cases where you want to restrict the access to specific versions; for\n * example: you have a frontend which needs to be aligned with the \"current\" version of\n * the application, since the frontend won't realize a new version was released, the\n * validator can be used to let the frontend know.\n * Also, it can be configured to handle requests from popups, in which case, instead of\n * generating an error message, it will send a post message.\n *\n * @group Middleware Classes\n * @group Middlewares/VersionValidator\n * @prettierignore\n */\nexport class VersionValidator {\n /**\n * To generate the errors in case the validation fails.\n */\n protected readonly _HTTPError: HTTPErrorClass;\n /**\n * To generate responses for popups.\n */\n protected readonly _responsesBuilder: ResponsesBuilder;\n /**\n * The uility service to get HTTP status codes.\n */\n protected readonly _statuses: Statuses;\n /**\n * The customization options.\n */\n protected readonly _options: VersionValidatorOptions;\n /**\n * @param options The options to construct the class.\n * @throws If no `version` is specified in the options.\n */\n constructor({ inject, version, ...options }: VersionValidatorConstructorOptions) {\n this._HTTPError = inject.HTTPError;\n this._responsesBuilder = inject.responsesBuilder;\n this._statuses = inject.statuses;\n this._options = deepAssignWithOverwrite(\n {\n error: \"The application version doesn't match\",\n latest: {\n allow: true,\n name: 'latest',\n },\n popup: {\n variable: 'popup',\n title: 'Conflict',\n message: 'vesion:conflict',\n },\n version,\n },\n options,\n );\n\n if (!this._options.version) {\n throw new Error('You need to supply a version');\n }\n }\n /**\n * Generates the middleware that validates the version.\n */\n getMiddleware(): ExpressMiddleware {\n return (req, res, next) => {\n // Get the `version` parameter from the request.\n const { version } = req.params;\n // If no version is present, move on to the next middleware.\n if (!version) {\n next();\n return;\n }\n // If the version matches, or it's a \"latest\" version, move on to the next middleware.\n if (version === this._options.version || this._isTheAllowedLatest(version)) {\n next();\n return;\n }\n\n const status = this._statuses('conflict');\n // If the request comes from a popup, send the post message.\n if (this._isPopup(req)) {\n const { title, message } = this._options.popup;\n this._responsesBuilder.htmlPostMessage({\n res,\n title,\n message,\n status,\n });\n return;\n }\n\n // Every other validation failed, and it's not a popup, so generate an error.\n next(\n new this._HTTPError(this._options.error, status, {\n response: {\n validation: true,\n },\n }),\n );\n };\n }\n /**\n * The customization options.\n */\n get options(): Readonly<VersionValidatorOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Helper method that checks if the incoming request is from a popup. It will look for\n * the query string variable defined in the constructor options.\n *\n * @param req The request object sent by the application.\n */\n protected _isPopup(req: Request): boolean {\n const popup = req.query[this._options.popup.variable];\n return !!(popup && String(popup).toLowerCase() === 'true');\n }\n /**\n * Helper method that checks if the \"latest version\" is enabled and if the given version\n * is \"the latest\" (comparing it with the option name).\n *\n * @param version The version received in the request.\n */\n protected _isTheAllowedLatest(version: string): boolean {\n const { allow, name } = this._options.latest;\n return allow && version === name;\n }\n}\n/**\n * A middleware that will validate a `version` request parameter against the application\n * version, and generate an error if they don't match.\n * This is a \"middleware/controller\" because the wrappers for both are the same, the\n * difference is that, for controllers, Jimpex sends a second parameter with the route\n * where they are mounted.\n * By validating the route parameter, the function can know whether the implementation is\n * going to use the middleware by itself or as a route middleware.\n * If used as middleware, it will just return the result of\n * {@link VersionValidator.getMiddleware}; but if used as controller, it will mount it on\n * `[route]/:version/*`.\n *\n * @group Middlewares\n * @group Middlewares/VersionValidator\n */\nexport const versionValidatorMiddleware = middlewareCreator(\n (options: VersionValidatorMiddlewareOptions = {}) =>\n (app, route) => {\n const version = app.getConfig<string>('version');\n const middleware = new VersionValidator({\n inject: {\n HTTPError: app.get('HTTPError'),\n responsesBuilder: app.get('responsesBuilder'),\n statuses: app.get('statuses'),\n },\n version,\n ...options,\n }).getMiddleware();\n\n if (route) {\n const router = app.getRouter();\n return router.all('/:version/*', middleware);\n }\n\n return middleware;\n },\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAwC;AACxC,mBAAiD;AAyI1C,MAAM,iBAAiB;AAAA,EAqB5B,YAAY,EAAE,QAAQ,YAAY,QAAQ,GAAuC;AAjBjF,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAInB,wBAAmB;AAMjB,SAAK,aAAa,OAAO;AACzB,SAAK,oBAAoB,OAAO;AAChC,SAAK,YAAY,OAAO;AACxB,SAAK,eAAW;AAAA,MACd;AAAA,QACE,OAAO;AAAA,QACP,QAAQ;AAAA,UACN,OAAO;AAAA,UACP,MAAM;AAAA,QACR;AAAA,QACA,OAAO;AAAA,UACL,UAAU;AAAA,UACV,OAAO;AAAA,UACP,SAAS;AAAA,QACX;AAAA,QACA;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAEA,QAAI,CAAC,KAAK,SAAS,SAAS;AAC1B,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AAAA,EACF;AAAA,EAIA,gBAAmC;AACjC,WAAO,CAAC,KAAK,KAAK,SAAS;AAEzB,YAAM,EAAE,QAAQ,IAAI,IAAI;AAExB,UAAI,CAAC,SAAS;AACZ,aAAK;AACL;AAAA,MACF;AAEA,UAAI,YAAY,KAAK,SAAS,WAAW,KAAK,oBAAoB,OAAO,GAAG;AAC1E,aAAK;AACL;AAAA,MACF;AAEA,YAAM,SAAS,KAAK,UAAU,UAAU;AAExC,UAAI,KAAK,SAAS,GAAG,GAAG;AACtB,cAAM,EAAE,OAAO,QAAQ,IAAI,KAAK,SAAS;AACzC,aAAK,kBAAkB,gBAAgB;AAAA,UACrC;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF,CAAC;AACD;AAAA,MACF;AAGA;AAAA,QACE,IAAI,KAAK,WAAW,KAAK,SAAS,OAAO,QAAQ;AAAA,UAC/C,UAAU;AAAA,YACR,YAAY;AAAA,UACd;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA,EAIA,IAAI,UAA6C;AAC/C,eAAO,4CAAwB,CAAC,GAAG,KAAK,QAAQ;AAAA,EAClD;AAAA,EAOU,SAAS,KAAuB;AACxC,UAAM,QAAQ,IAAI,MAAM,KAAK,SAAS,MAAM;AAC5C,WAAO,CAAC,EAAE,SAAS,OAAO,KAAK,EAAE,YAAY,MAAM;AAAA,EACrD;AAAA,EAOU,oBAAoB,SAA0B;AACtD,UAAM,EAAE,OAAO,KAAK,IAAI,KAAK,SAAS;AACtC,WAAO,SAAS,YAAY;AAAA,EAC9B;AACF;AAgBO,MAAM,iCAA6B;AAAA,EACxC,CAAC,UAA6C,CAAC,MAC7C,CAAC,KAAK,UAAU;AACd,UAAM,UAAU,IAAI,UAAkB,SAAS;AAC/C,UAAM,aAAa,IAAI,iBAAiB;AAAA,MACtC,QAAQ;AAAA,QACN,WAAW,IAAI,IAAI,WAAW;AAAA,QAC9B,kBAAkB,IAAI,IAAI,kBAAkB;AAAA,QAC5C,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC,EAAE,cAAc;AAEjB,QAAI,OAAO;AACT,YAAM,SAAS,IAAI,UAAU;AAC7B,aAAO,OAAO,IAAI,eAAe,UAAU;AAAA,IAC7C;AAEA,WAAO;AAAA,EACT;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/middlewares/utils/versionValidator.ts"],"sourcesContent":["import { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { middlewareCreator, type Statuses } from '../../utils';\nimport { DeepPartial, Request, ExpressMiddleware } from '../../types';\nimport type { HTTPErrorClass, ResponsesBuilder } from '../../services';\n/**\n * The options for how the middleware should behave if the requested version is `latest`.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorLatestOptions = {\n /**\n * Whether or not the middleware should validate the _\"latest version\"_.\n *\n * @default true\n */\n allow: boolean;\n /**\n * The name of the _\"latest version\"_. Basically, `req.params.version` must match with\n * this property in order to be consider \"latest\".\n *\n * @default 'latest'\n */\n name: string;\n};\n/**\n * The options for how to detect if the request comes from a popup and how to compose the\n * post message the middleware will use to respond.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorPopupOptions = {\n /**\n * The name of the query string variable the middleware will check in order to indentify\n * whether the request comes from a popup or not. The variable must have `'true'` as its\n * value.\n *\n * @default 'popup'\n */\n variable: string;\n /**\n * The title of the page that will be generated to respond in case the versions don't\n * match.\n *\n * @default 'Conflict'\n */\n title: string;\n /**\n * The contents of the post message the generated page will send if the versions don't\n * match.\n *\n * @default 'version:conflict'\n */\n message: string;\n};\n/**\n * The options used to customize a {@link VersionValidator} instance.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorOptions = {\n /**\n * The version used to validate the requests.\n */\n version: string;\n /**\n * The options for how the middleware should behave if the requested version is\n * `latest`.\n */\n latest: VersionValidatorLatestOptions;\n /**\n * The options for how to detect if the request comes from a popup and how to compose\n * the post message the middleware will use to respond.\n */\n popup: VersionValidatorPopupOptions;\n /**\n * The error message to show when the version is invalid.\n *\n * @default \"The application version doesn't match\"\n * @prettierignore\n */\n error: string;\n};\n/**\n * A partial version of the {@link VersionValidatorOptions}, to be used in the constructor\n * and the middleware creator. The reason it omits `version` it's because for the\n * constructor it's required, but for the middleware creator it's not.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorPartialOptions = DeepPartial<\n Omit<VersionValidatorOptions, 'version'>\n>;\n/**\n * The options to construct a {@link VersionValidator}.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorConstructorOptions = VersionValidatorPartialOptions & {\n /**\n * The version used to validate the requests.\n */\n version: string;\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n HTTPError: HTTPErrorClass;\n responsesBuilder: ResponsesBuilder;\n statuses: Statuses;\n };\n};\n/**\n * The options for the middleware creator that will mount an instance of\n * {@link VersionValidator}.\n *\n * @group Middlewares/VersionValidator\n */\nexport type VersionValidatorMiddlewareOptions = VersionValidatorPartialOptions & {\n /**\n * The version used to validate the requests. This is optional in here because if it's\n * not specified, it will be obtained from the configuration service.\n */\n version?: string;\n};\n/**\n * This is the handler for the middleware/controller that validates the application\n * version.\n * This is useful in cases where you want to restrict the access to specific versions; for\n * example: you have a frontend which needs to be aligned with the \"current\" version of\n * the application, since the frontend won't realize a new version was released, the\n * validator can be used to let the frontend know.\n * Also, it can be configured to handle requests from popups, in which case, instead of\n * generating an error message, it will send a post message.\n *\n * @group Middleware Classes\n * @group Middlewares/VersionValidator\n * @prettierignore\n */\nexport class VersionValidator {\n /**\n * To generate the errors in case the validation fails.\n */\n protected readonly _HTTPError: HTTPErrorClass;\n /**\n * To generate responses for popups.\n */\n protected readonly _responsesBuilder: ResponsesBuilder;\n /**\n * The uility service to get HTTP status codes.\n */\n protected readonly _statuses: Statuses;\n /**\n * The customization options.\n */\n protected readonly _options: VersionValidatorOptions;\n /**\n * @param options The options to construct the class.\n * @throws If no `version` is specified in the options.\n */\n constructor({ inject, version, ...options }: VersionValidatorConstructorOptions) {\n this._HTTPError = inject.HTTPError;\n this._responsesBuilder = inject.responsesBuilder;\n this._statuses = inject.statuses;\n this._options = deepAssignWithOverwrite(\n {\n error: \"The application version doesn't match\",\n latest: {\n allow: true,\n name: 'latest',\n },\n popup: {\n variable: 'popup',\n title: 'Conflict',\n message: 'vesion:conflict',\n },\n version,\n },\n options,\n );\n\n if (!this._options.version) {\n throw new Error('You need to supply a version');\n }\n }\n /**\n * Generates the middleware that validates the version.\n */\n getMiddleware(): ExpressMiddleware {\n return (req, res, next) => {\n // Get the `version` parameter from the request.\n const { version } = req.params;\n // If no version is present, move on to the next middleware.\n if (!version) {\n next();\n return;\n }\n // If the version matches, or it's a \"latest\" version, move on to the next middleware.\n if (version === this._options.version || this._isTheAllowedLatest(version)) {\n next();\n return;\n }\n\n const status = this._statuses('conflict');\n // If the request comes from a popup, send the post message.\n if (this._isPopup(req)) {\n const { title, message } = this._options.popup;\n this._responsesBuilder.htmlPostMessage({\n res,\n title,\n message,\n status,\n });\n return;\n }\n\n // Every other validation failed, and it's not a popup, so generate an error.\n next(\n new this._HTTPError(this._options.error, status, {\n response: {\n validation: true,\n },\n }),\n );\n };\n }\n /**\n * The customization options.\n */\n get options(): Readonly<VersionValidatorOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Helper method that checks if the incoming request is from a popup. It will look for\n * the query string variable defined in the constructor options.\n *\n * @param req The request object sent by the application.\n */\n protected _isPopup(req: Request): boolean {\n const popup = req.query[this._options.popup.variable];\n return !!(popup && String(popup).toLowerCase() === 'true');\n }\n /**\n * Helper method that checks if the \"latest version\" is enabled and if the given version\n * is \"the latest\" (comparing it with the option name).\n *\n * @param version The version received in the request.\n */\n protected _isTheAllowedLatest(version: string): boolean {\n const { allow, name } = this._options.latest;\n return allow && version === name;\n }\n}\n/**\n * A middleware that will validate a `version` request parameter against the application\n * version, and generate an error if they don't match.\n * This is a \"middleware/controller\" because the wrappers for both are the same, the\n * difference is that, for controllers, Jimpex sends a second parameter with the route\n * where they are mounted.\n * By validating the route parameter, the function can know whether the implementation is\n * going to use the middleware by itself or as a route middleware.\n * If used as middleware, it will just return the result of\n * {@link VersionValidator.getMiddleware}; but if used as controller, it will mount it on\n * `[route]/:version/*`.\n *\n * @group Middlewares\n * @group Middlewares/VersionValidator\n */\nexport const versionValidatorMiddleware = middlewareCreator(\n (options: VersionValidatorMiddlewareOptions = {}) =>\n (app, route) => {\n const version = app.getConfig<string>('version');\n const middleware = new VersionValidator({\n inject: {\n HTTPError: app.get('HTTPError'),\n responsesBuilder: app.get('responsesBuilder'),\n statuses: app.get('statuses'),\n },\n version,\n ...options,\n }).getMiddleware();\n\n if (route) {\n const router = app.getRouter();\n return router.all('/:version/*', middleware);\n }\n\n return middleware;\n },\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAwC;AACxC,mBAAiD;AAyI1C,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA,EAqB5B,YAAY,EAAE,QAAQ,SAAS,GAAG,QAAQ,GAAuC;AAjBjF;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAmB;AAMjB,SAAK,aAAa,OAAO;AACzB,SAAK,oBAAoB,OAAO;AAChC,SAAK,YAAY,OAAO;AACxB,SAAK,eAAW;AAAA,MACd;AAAA,QACE,OAAO;AAAA,QACP,QAAQ;AAAA,UACN,OAAO;AAAA,UACP,MAAM;AAAA,QACR;AAAA,QACA,OAAO;AAAA,UACL,UAAU;AAAA,UACV,OAAO;AAAA,UACP,SAAS;AAAA,QACX;AAAA,QACA;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAEA,QAAI,CAAC,KAAK,SAAS,SAAS;AAC1B,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAIA,gBAAmC;AACjC,WAAO,CAAC,KAAK,KAAK,SAAS;AAEzB,YAAM,EAAE,QAAQ,IAAI,IAAI;AAExB,UAAI,CAAC,SAAS;AACZ,aAAK;AACL;AAAA,MACF;AAEA,UAAI,YAAY,KAAK,SAAS,WAAW,KAAK,oBAAoB,OAAO,GAAG;AAC1E,aAAK;AACL;AAAA,MACF;AAEA,YAAM,SAAS,KAAK,UAAU,UAAU;AAExC,UAAI,KAAK,SAAS,GAAG,GAAG;AACtB,cAAM,EAAE,OAAO,QAAQ,IAAI,KAAK,SAAS;AACzC,aAAK,kBAAkB,gBAAgB;AAAA,UACrC;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF,CAAC;AACD;AAAA,MACF;AAGA;AAAA,QACE,IAAI,KAAK,WAAW,KAAK,SAAS,OAAO,QAAQ;AAAA,UAC/C,UAAU;AAAA,YACR,YAAY;AAAA,UACd;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAIA,IAAI,UAA6C;AAC/C,eAAO,4CAAwB,CAAC,GAAG,KAAK,QAAQ;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU,SAAS,KAAuB;AACxC,UAAM,QAAQ,IAAI,MAAM,KAAK,SAAS,MAAM,QAAQ;AACpD,WAAO,CAAC,EAAE,SAAS,OAAO,KAAK,EAAE,YAAY,MAAM;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU,oBAAoB,SAA0B;AACtD,UAAM,EAAE,OAAO,KAAK,IAAI,KAAK,SAAS;AACtC,WAAO,SAAS,YAAY;AAAA,EAC9B;AACF;AAgBO,MAAM,iCAA6B;AAAA,EACxC,CAAC,UAA6C,CAAC,MAC7C,CAAC,KAAK,UAAU;AACd,UAAM,UAAU,IAAI,UAAkB,SAAS;AAC/C,UAAM,aAAa,IAAI,iBAAiB;AAAA,MACtC,QAAQ;AAAA,QACN,WAAW,IAAI,IAAI,WAAW;AAAA,QAC9B,kBAAkB,IAAI,IAAI,kBAAkB;AAAA,QAC5C,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC,EAAE,cAAc;AAEjB,QAAI,OAAO;AACT,YAAM,SAAS,IAAI,UAAU;AAC7B,aAAO,OAAO,IAAI,eAAe,UAAU;AAAA,IAC7C;AAEA,WAAO;AAAA,EACT;AACJ;","names":[]}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
+
import { J as Jimpex } from '../../index-Bwf7JHu9.mjs';
|
|
3
|
+
import { Statuses } from '../../utils/fns/statuses.mjs';
|
|
4
|
+
import '../../types/express.mjs';
|
|
5
|
+
import 'express';
|
|
6
|
+
import '../../types/http.mjs';
|
|
7
|
+
import 'https';
|
|
8
|
+
import 'http';
|
|
9
|
+
import 'spdy';
|
|
10
|
+
import 'node-fetch';
|
|
11
|
+
import '../../types/utils.mjs';
|
|
12
|
+
import '@homer0/path-utils';
|
|
13
|
+
import '@homer0/simple-logger';
|
|
14
|
+
import '@homer0/simple-config';
|
|
15
|
+
import '@homer0/events-hub';
|
|
16
|
+
import 'statuses';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A dictionary with some context information that can be provided to {@link AppError}.
|
|
20
|
+
*
|
|
21
|
+
* @group Services/AppError
|
|
22
|
+
*/
|
|
23
|
+
type AppErrorContext = {
|
|
24
|
+
/**
|
|
25
|
+
* Extra information that the error handler can pick and include the response.
|
|
26
|
+
*/
|
|
27
|
+
response?: unknown;
|
|
28
|
+
/**
|
|
29
|
+
* A status code or name for the error handler to set in the response.
|
|
30
|
+
*/
|
|
31
|
+
status?: string | number;
|
|
32
|
+
} & Record<string, unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* A simple subclass of `Error` but with support for context information.
|
|
35
|
+
*
|
|
36
|
+
* @group Services
|
|
37
|
+
* @group Services/AppError
|
|
38
|
+
*/
|
|
39
|
+
declare class AppError extends Error {
|
|
40
|
+
/**
|
|
41
|
+
* The date of when the error was generated.
|
|
42
|
+
*/
|
|
43
|
+
readonly _date: Date;
|
|
44
|
+
/**
|
|
45
|
+
* The context information that can be provided to the error, and picked by the error
|
|
46
|
+
* handler.
|
|
47
|
+
*/
|
|
48
|
+
readonly _context: AppErrorContext;
|
|
49
|
+
/**
|
|
50
|
+
* The service that generates HTTP status codes.
|
|
51
|
+
*/
|
|
52
|
+
protected _statuses: Statuses;
|
|
53
|
+
/**
|
|
54
|
+
* @param message The message of the error.
|
|
55
|
+
* @param context The context information, for the error handler.
|
|
56
|
+
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
57
|
+
* is in case the implementation wants to use a special version from
|
|
58
|
+
* the container; otherwise, it will use the `statuses` library
|
|
59
|
+
* directly.
|
|
60
|
+
*/
|
|
61
|
+
constructor(message: string, context?: AppErrorContext, statuses?: Statuses);
|
|
62
|
+
/**
|
|
63
|
+
* Gets an object that can be included in a response from the application. This method
|
|
64
|
+
* is a helper for the `response` getter, as it allows for the assertion of the response
|
|
65
|
+
* type.
|
|
66
|
+
*
|
|
67
|
+
* @template T The type of the response.
|
|
68
|
+
*/
|
|
69
|
+
getResponse<T>(): T;
|
|
70
|
+
/**
|
|
71
|
+
* Information about the error that can be included in a response. This is set using the
|
|
72
|
+
* `response` key in the `context` option.
|
|
73
|
+
*/
|
|
74
|
+
get response(): unknown;
|
|
75
|
+
/**
|
|
76
|
+
* An HTTP status code related to the error. This is set using the `status` key on the
|
|
77
|
+
* `context`.
|
|
78
|
+
*/
|
|
79
|
+
get status(): number | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Context information related to the error.
|
|
82
|
+
*/
|
|
83
|
+
get context(): unknown;
|
|
84
|
+
/**
|
|
85
|
+
* The date of when the error was generated.
|
|
86
|
+
*/
|
|
87
|
+
get date(): Date;
|
|
88
|
+
/**
|
|
89
|
+
* Utility method that formats the context before saving it in the instance:
|
|
90
|
+
* - If the context includes a `status` as a `string`, it will try to replace it with
|
|
91
|
+
* its actual status code from the `statuses `service.
|
|
92
|
+
*
|
|
93
|
+
* @param context The original context sent to the constructor.
|
|
94
|
+
*/
|
|
95
|
+
protected _parseContext(context: AppErrorContext): AppErrorContext;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Shorthand for `new AppError()`.
|
|
99
|
+
*
|
|
100
|
+
* @param args The same parameters as the {@link AppError} constructor.
|
|
101
|
+
* @returns A new instance of {@link AppError}.
|
|
102
|
+
* @group Services
|
|
103
|
+
* @group Services/AppError
|
|
104
|
+
*/
|
|
105
|
+
declare const createAppError: (message: string, context?: AppErrorContext | undefined, statuses?: Statuses | undefined) => AppError;
|
|
106
|
+
/**
|
|
107
|
+
* The type of the function that generates a new instance of {@link AppError}.
|
|
108
|
+
* This is exported to make it easy to type the dependency injection.
|
|
109
|
+
*
|
|
110
|
+
* @group Services/AppError
|
|
111
|
+
*/
|
|
112
|
+
type CreateAppErrorFn = typeof createAppError;
|
|
113
|
+
/**
|
|
114
|
+
* THe type of the {@link AppError} class.
|
|
115
|
+
* This is exported to make it easy to type the dependency injection.
|
|
116
|
+
*
|
|
117
|
+
* @group Services/AppError
|
|
118
|
+
*/
|
|
119
|
+
type AppErrorClass = typeof AppError;
|
|
120
|
+
/**
|
|
121
|
+
* A service provider that will register both, {@link AppError} and
|
|
122
|
+
* {@link createAppError}, on the container. `AppError` will be the key for class, and
|
|
123
|
+
* `appError` will be for the generator function.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
*
|
|
127
|
+
* // Register it on the container
|
|
128
|
+
* container.register(appErrorProvider);
|
|
129
|
+
* // Getting access to the class.
|
|
130
|
+
* const AppError = container.get<AppErrorClass>('AppError');
|
|
131
|
+
* // Getting access to the function.
|
|
132
|
+
* const appError = container.get<CreateAppErrorFn>('appError');
|
|
133
|
+
*
|
|
134
|
+
* @group Providers
|
|
135
|
+
* @group Services/AppError
|
|
136
|
+
*/
|
|
137
|
+
declare const appErrorProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
138
|
+
|
|
139
|
+
export { AppError, type AppErrorClass, type AppErrorContext, type CreateAppErrorFn, appErrorProvider, createAppError };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
-
import { J as Jimpex } from '../../
|
|
2
|
+
import { J as Jimpex } from '../../index-C6I3NCC-.js';
|
|
3
3
|
import { Statuses } from '../../utils/fns/statuses.js';
|
|
4
|
-
import '@homer0/events-hub';
|
|
5
4
|
import '../../types/express.js';
|
|
6
5
|
import 'express';
|
|
7
6
|
import '../../types/http.js';
|
|
@@ -9,9 +8,11 @@ import 'https';
|
|
|
9
8
|
import 'http';
|
|
10
9
|
import 'spdy';
|
|
11
10
|
import 'node-fetch';
|
|
12
|
-
import '@homer0/simple-config';
|
|
13
11
|
import '../../types/utils.js';
|
|
12
|
+
import '@homer0/path-utils';
|
|
14
13
|
import '@homer0/simple-logger';
|
|
14
|
+
import '@homer0/simple-config';
|
|
15
|
+
import '@homer0/events-hub';
|
|
15
16
|
import 'statuses';
|
|
16
17
|
|
|
17
18
|
/**
|
|
@@ -135,4 +136,4 @@ type AppErrorClass = typeof AppError;
|
|
|
135
136
|
*/
|
|
136
137
|
declare const appErrorProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
137
138
|
|
|
138
|
-
export { AppError, AppErrorClass, AppErrorContext, CreateAppErrorFn, appErrorProvider, createAppError };
|
|
139
|
+
export { AppError, type AppErrorClass, type AppErrorContext, type CreateAppErrorFn, appErrorProvider, createAppError };
|
|
@@ -30,34 +30,80 @@ __export(appError_exports, {
|
|
|
30
30
|
module.exports = __toCommonJS(appError_exports);
|
|
31
31
|
var import_utils = require("../../utils");
|
|
32
32
|
class AppError extends Error {
|
|
33
|
+
/**
|
|
34
|
+
* @param message The message of the error.
|
|
35
|
+
* @param context The context information, for the error handler.
|
|
36
|
+
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
37
|
+
* is in case the implementation wants to use a special version from
|
|
38
|
+
* the container; otherwise, it will use the `statuses` library
|
|
39
|
+
* directly.
|
|
40
|
+
*/
|
|
33
41
|
constructor(message, context = {}, statuses = import_utils.statuses) {
|
|
34
42
|
super(message);
|
|
43
|
+
/**
|
|
44
|
+
* The date of when the error was generated.
|
|
45
|
+
*/
|
|
35
46
|
__publicField(this, "_date");
|
|
47
|
+
/**
|
|
48
|
+
* The context information that can be provided to the error, and picked by the error
|
|
49
|
+
* handler.
|
|
50
|
+
*/
|
|
36
51
|
__publicField(this, "_context");
|
|
52
|
+
/**
|
|
53
|
+
* The service that generates HTTP status codes.
|
|
54
|
+
*/
|
|
37
55
|
__publicField(this, "_statuses");
|
|
38
56
|
this.name = this.constructor.name;
|
|
39
|
-
this._date = new Date();
|
|
57
|
+
this._date = /* @__PURE__ */ new Date();
|
|
40
58
|
this._statuses = statuses;
|
|
41
59
|
this._context = this._parseContext(context);
|
|
42
60
|
if (Error.captureStackTrace) {
|
|
43
61
|
Error.captureStackTrace(this, this.constructor);
|
|
44
62
|
}
|
|
45
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Gets an object that can be included in a response from the application. This method
|
|
66
|
+
* is a helper for the `response` getter, as it allows for the assertion of the response
|
|
67
|
+
* type.
|
|
68
|
+
*
|
|
69
|
+
* @template T The type of the response.
|
|
70
|
+
*/
|
|
46
71
|
getResponse() {
|
|
47
72
|
return this.response;
|
|
48
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Information about the error that can be included in a response. This is set using the
|
|
76
|
+
* `response` key in the `context` option.
|
|
77
|
+
*/
|
|
49
78
|
get response() {
|
|
50
79
|
return this._context.response || {};
|
|
51
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* An HTTP status code related to the error. This is set using the `status` key on the
|
|
83
|
+
* `context`.
|
|
84
|
+
*/
|
|
52
85
|
get status() {
|
|
53
86
|
return this._context.status;
|
|
54
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Context information related to the error.
|
|
90
|
+
*/
|
|
55
91
|
get context() {
|
|
56
92
|
return this._context;
|
|
57
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The date of when the error was generated.
|
|
96
|
+
*/
|
|
58
97
|
get date() {
|
|
59
98
|
return this._date;
|
|
60
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Utility method that formats the context before saving it in the instance:
|
|
102
|
+
* - If the context includes a `status` as a `string`, it will try to replace it with
|
|
103
|
+
* its actual status code from the `statuses `service.
|
|
104
|
+
*
|
|
105
|
+
* @param context The original context sent to the constructor.
|
|
106
|
+
*/
|
|
61
107
|
_parseContext(context) {
|
|
62
108
|
const result = { ...context };
|
|
63
109
|
if (result.status && typeof result.status === "string") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/common/appError.ts"],"sourcesContent":["import { provider, statuses as statusesFn, type Statuses } from '../../utils';\n/**\n * A dictionary with some context information that can be provided to {@link AppError}.\n *\n * @group Services/AppError\n */\nexport type AppErrorContext = {\n /**\n * Extra information that the error handler can pick and include the response.\n */\n response?: unknown;\n /**\n * A status code or name for the error handler to set in the response.\n */\n status?: string | number;\n} & Record<string, unknown>;\n/**\n * A simple subclass of `Error` but with support for context information.\n *\n * @group Services\n * @group Services/AppError\n */\nexport class AppError extends Error {\n /**\n * The date of when the error was generated.\n */\n readonly _date: Date;\n /**\n * The context information that can be provided to the error, and picked by the error\n * handler.\n */\n readonly _context: AppErrorContext;\n /**\n * The service that generates HTTP status codes.\n */\n protected _statuses: Statuses;\n /**\n * @param message The message of the error.\n * @param context The context information, for the error handler.\n * @param statuses A reference to the service that generates HTTP status codes. This\n * is in case the implementation wants to use a special version from\n * the container; otherwise, it will use the `statuses` library\n * directly.\n */\n constructor(\n message: string,\n context: AppErrorContext = {},\n statuses: Statuses = statusesFn,\n ) {\n super(message);\n this.name = this.constructor.name;\n this._date = new Date();\n this._statuses = statuses;\n this._context = this._parseContext(context);\n\n // Limit the stack trace if possible.\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n /**\n * Gets an object that can be included in a response from the application. This method\n * is a helper for the `response` getter, as it allows for the assertion of the response\n * type.\n *\n * @template T The type of the response.\n */\n getResponse<T>(): T {\n return this.response as T;\n }\n /**\n * Information about the error that can be included in a response. This is set using the\n * `response` key in the `context` option.\n */\n get response(): unknown {\n return this._context.response || {};\n }\n /**\n * An HTTP status code related to the error. This is set using the `status` key on the\n * `context`.\n */\n get status(): number | undefined {\n return this._context.status as number | undefined;\n }\n /**\n * Context information related to the error.\n */\n get context(): unknown {\n return this._context;\n }\n /**\n * The date of when the error was generated.\n */\n get date(): Date {\n return this._date;\n }\n /**\n * Utility method that formats the context before saving it in the instance:\n * - If the context includes a `status` as a `string`, it will try to replace it with\n * its actual status code from the `statuses `service.\n *\n * @param context The original context sent to the constructor.\n */\n protected _parseContext(context: AppErrorContext): AppErrorContext {\n const result = { ...context };\n if (result.status && typeof result.status === 'string') {\n result.status = this._statuses.code[result.status.toLowerCase()] || result.status;\n }\n\n return result;\n }\n}\n/**\n * Shorthand for `new AppError()`.\n *\n * @param args The same parameters as the {@link AppError} constructor.\n * @returns A new instance of {@link AppError}.\n * @group Services\n * @group Services/AppError\n */\nexport const createAppError = (\n ...args: ConstructorParameters<typeof AppError>\n): AppError => new AppError(...args);\n/**\n * The type of the function that generates a new instance of {@link AppError}.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/AppError\n */\nexport type CreateAppErrorFn = typeof createAppError;\n/**\n * THe type of the {@link AppError} class.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/AppError\n */\nexport type AppErrorClass = typeof AppError;\n/**\n * A service provider that will register both, {@link AppError} and\n * {@link createAppError}, on the container. `AppError` will be the key for class, and\n * `appError` will be for the generator function.\n *\n * @example\n *\n * // Register it on the container\n * container.register(appErrorProvider);\n * // Getting access to the class.\n * const AppError = container.get<AppErrorClass>('AppError');\n * // Getting access to the function.\n * const appError = container.get<CreateAppErrorFn>('appError');\n *\n * @group Providers\n * @group Services/AppError\n */\nexport const appErrorProvider = provider((app) => {\n app.set('AppError', () => AppError);\n app.set('appError', () => createAppError);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgE;AAsBzD,MAAM,iBAAiB,MAAM;AAAA,EAsBlC,YACE,SACA,UAA2B,CAAC,GAC5B,WAAqB,aAAAA,UACrB;AACA,UAAM,OAAO;AAvBf,wBAAS;AAKT,wBAAS;AAIT,wBAAU;AAeR,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,
|
|
1
|
+
{"version":3,"sources":["../../../src/services/common/appError.ts"],"sourcesContent":["import { provider, statuses as statusesFn, type Statuses } from '../../utils';\n/**\n * A dictionary with some context information that can be provided to {@link AppError}.\n *\n * @group Services/AppError\n */\nexport type AppErrorContext = {\n /**\n * Extra information that the error handler can pick and include the response.\n */\n response?: unknown;\n /**\n * A status code or name for the error handler to set in the response.\n */\n status?: string | number;\n} & Record<string, unknown>;\n/**\n * A simple subclass of `Error` but with support for context information.\n *\n * @group Services\n * @group Services/AppError\n */\nexport class AppError extends Error {\n /**\n * The date of when the error was generated.\n */\n readonly _date: Date;\n /**\n * The context information that can be provided to the error, and picked by the error\n * handler.\n */\n readonly _context: AppErrorContext;\n /**\n * The service that generates HTTP status codes.\n */\n protected _statuses: Statuses;\n /**\n * @param message The message of the error.\n * @param context The context information, for the error handler.\n * @param statuses A reference to the service that generates HTTP status codes. This\n * is in case the implementation wants to use a special version from\n * the container; otherwise, it will use the `statuses` library\n * directly.\n */\n constructor(\n message: string,\n context: AppErrorContext = {},\n statuses: Statuses = statusesFn,\n ) {\n super(message);\n this.name = this.constructor.name;\n this._date = new Date();\n this._statuses = statuses;\n this._context = this._parseContext(context);\n\n // Limit the stack trace if possible.\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n /**\n * Gets an object that can be included in a response from the application. This method\n * is a helper for the `response` getter, as it allows for the assertion of the response\n * type.\n *\n * @template T The type of the response.\n */\n getResponse<T>(): T {\n return this.response as T;\n }\n /**\n * Information about the error that can be included in a response. This is set using the\n * `response` key in the `context` option.\n */\n get response(): unknown {\n return this._context.response || {};\n }\n /**\n * An HTTP status code related to the error. This is set using the `status` key on the\n * `context`.\n */\n get status(): number | undefined {\n return this._context.status as number | undefined;\n }\n /**\n * Context information related to the error.\n */\n get context(): unknown {\n return this._context;\n }\n /**\n * The date of when the error was generated.\n */\n get date(): Date {\n return this._date;\n }\n /**\n * Utility method that formats the context before saving it in the instance:\n * - If the context includes a `status` as a `string`, it will try to replace it with\n * its actual status code from the `statuses `service.\n *\n * @param context The original context sent to the constructor.\n */\n protected _parseContext(context: AppErrorContext): AppErrorContext {\n const result = { ...context };\n if (result.status && typeof result.status === 'string') {\n result.status = this._statuses.code[result.status.toLowerCase()] || result.status;\n }\n\n return result;\n }\n}\n/**\n * Shorthand for `new AppError()`.\n *\n * @param args The same parameters as the {@link AppError} constructor.\n * @returns A new instance of {@link AppError}.\n * @group Services\n * @group Services/AppError\n */\nexport const createAppError = (\n ...args: ConstructorParameters<typeof AppError>\n): AppError => new AppError(...args);\n/**\n * The type of the function that generates a new instance of {@link AppError}.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/AppError\n */\nexport type CreateAppErrorFn = typeof createAppError;\n/**\n * THe type of the {@link AppError} class.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/AppError\n */\nexport type AppErrorClass = typeof AppError;\n/**\n * A service provider that will register both, {@link AppError} and\n * {@link createAppError}, on the container. `AppError` will be the key for class, and\n * `appError` will be for the generator function.\n *\n * @example\n *\n * // Register it on the container\n * container.register(appErrorProvider);\n * // Getting access to the class.\n * const AppError = container.get<AppErrorClass>('AppError');\n * // Getting access to the function.\n * const appError = container.get<CreateAppErrorFn>('appError');\n *\n * @group Providers\n * @group Services/AppError\n */\nexport const appErrorProvider = provider((app) => {\n app.set('AppError', () => AppError);\n app.set('appError', () => createAppError);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgE;AAsBzD,MAAM,iBAAiB,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBlC,YACE,SACA,UAA2B,CAAC,GAC5B,WAAqB,aAAAA,UACrB;AACA,UAAM,OAAO;AAvBf;AAAA;AAAA;AAAA,wBAAS;AAKT;AAAA;AAAA;AAAA;AAAA,wBAAS;AAIT;AAAA;AAAA;AAAA,wBAAU;AAeR,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,QAAQ,oBAAI,KAAK;AACtB,SAAK,YAAY;AACjB,SAAK,WAAW,KAAK,cAAc,OAAO;AAG1C,QAAI,MAAM,mBAAmB;AAC3B,YAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,IAChD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,cAAoB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO,KAAK,SAAS,YAAY,CAAC;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAA6B;AAC/B,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAIA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAIA,IAAI,OAAa;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQU,cAAc,SAA2C;AACjE,UAAM,SAAS,EAAE,GAAG,QAAQ;AAC5B,QAAI,OAAO,UAAU,OAAO,OAAO,WAAW,UAAU;AACtD,aAAO,SAAS,KAAK,UAAU,KAAK,OAAO,OAAO,YAAY,CAAC,KAAK,OAAO;AAAA,IAC7E;AAEA,WAAO;AAAA,EACT;AACF;AASO,MAAM,iBAAiB,IACzB,SACU,IAAI,SAAS,GAAG,IAAI;AAgC5B,MAAM,uBAAmB,uBAAS,CAAC,QAAQ;AAChD,MAAI,IAAI,YAAY,MAAM,QAAQ;AAClC,MAAI,IAAI,YAAY,MAAM,cAAc;AAC1C,CAAC;","names":["statusesFn"]}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
+
import { J as Jimpex } from '../../index-Bwf7JHu9.mjs';
|
|
3
|
+
import { Statuses } from '../../utils/fns/statuses.mjs';
|
|
4
|
+
import { AppError, AppErrorContext } from './appError.mjs';
|
|
5
|
+
import '../../types/express.mjs';
|
|
6
|
+
import 'express';
|
|
7
|
+
import '../../types/http.mjs';
|
|
8
|
+
import 'https';
|
|
9
|
+
import 'http';
|
|
10
|
+
import 'spdy';
|
|
11
|
+
import 'node-fetch';
|
|
12
|
+
import '../../types/utils.mjs';
|
|
13
|
+
import '@homer0/path-utils';
|
|
14
|
+
import '@homer0/simple-logger';
|
|
15
|
+
import '@homer0/simple-config';
|
|
16
|
+
import '@homer0/events-hub';
|
|
17
|
+
import 'statuses';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A type of error to be used on HTTP requests. This is the most common type of error used
|
|
21
|
+
* by Jimpex.
|
|
22
|
+
*
|
|
23
|
+
* @group Services
|
|
24
|
+
* @group Services/HTTPError
|
|
25
|
+
*/
|
|
26
|
+
declare class HTTPError extends AppError {
|
|
27
|
+
/**
|
|
28
|
+
* @param message The error message.
|
|
29
|
+
* @param status The HTTP status code.
|
|
30
|
+
* @param context Context information related to the error.
|
|
31
|
+
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
32
|
+
* is in case the implementation wants to use a special version from
|
|
33
|
+
* the container; otherwise, it will use the `statuses` library
|
|
34
|
+
* directly.
|
|
35
|
+
*/
|
|
36
|
+
constructor(message: string, status?: number | string, context?: AppErrorContext, statuses?: Statuses);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Shorthand for `new HTTPError()`.
|
|
40
|
+
*
|
|
41
|
+
* @param args The same parameters as the {@link HTTPError} constructor.
|
|
42
|
+
* @returns A new instance of {@link HTTPError}.
|
|
43
|
+
* @group Services
|
|
44
|
+
* @group Services/HTTPError
|
|
45
|
+
*/
|
|
46
|
+
declare const createHTTPError: (message: string, status?: string | number | undefined, context?: AppErrorContext | undefined, statuses?: Statuses | undefined) => HTTPError;
|
|
47
|
+
/**
|
|
48
|
+
* The type of the function that generates a new instance of {@link HTTPError}.
|
|
49
|
+
* This is exported to make it easy to type the dependency injection.
|
|
50
|
+
*
|
|
51
|
+
* @group Services/HTTPError
|
|
52
|
+
*/
|
|
53
|
+
type CreateHTTPErrorFn = typeof createHTTPError;
|
|
54
|
+
/**
|
|
55
|
+
* THe type of the {@link HTTPError} class.
|
|
56
|
+
* This is exported to make it easy to type the dependency injection.
|
|
57
|
+
*
|
|
58
|
+
* @group Services/HTTPError
|
|
59
|
+
*/
|
|
60
|
+
type HTTPErrorClass = typeof HTTPError;
|
|
61
|
+
/**
|
|
62
|
+
* A service provider that will register both the {@link HTTPError} and a generator
|
|
63
|
+
* function on the container. `HTTPError` will be the key for class, and `httpError` will
|
|
64
|
+
* be for the generator function.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* // Register it on the container
|
|
69
|
+
* container.register(httpErrorProvider);
|
|
70
|
+
* // Getting access to the class.
|
|
71
|
+
* const HTTPError = container.get<HTTPErrorClass>('HTTPError');
|
|
72
|
+
* // Getting access to the function.
|
|
73
|
+
* const httpError = container.get<CreateHTTPErrorFn>('httpError');
|
|
74
|
+
*
|
|
75
|
+
* @group Providers
|
|
76
|
+
* @group Services/HTTPError
|
|
77
|
+
*/
|
|
78
|
+
declare const httpErrorProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
79
|
+
|
|
80
|
+
export { type CreateHTTPErrorFn, HTTPError, type HTTPErrorClass, createHTTPError, httpErrorProvider };
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
-
import { J as Jimpex } from '../../
|
|
2
|
+
import { J as Jimpex } from '../../index-C6I3NCC-.js';
|
|
3
3
|
import { Statuses } from '../../utils/fns/statuses.js';
|
|
4
4
|
import { AppError, AppErrorContext } from './appError.js';
|
|
5
|
-
import '@homer0/events-hub';
|
|
6
5
|
import '../../types/express.js';
|
|
7
6
|
import 'express';
|
|
8
7
|
import '../../types/http.js';
|
|
@@ -10,9 +9,11 @@ import 'https';
|
|
|
10
9
|
import 'http';
|
|
11
10
|
import 'spdy';
|
|
12
11
|
import 'node-fetch';
|
|
13
|
-
import '@homer0/simple-config';
|
|
14
12
|
import '../../types/utils.js';
|
|
13
|
+
import '@homer0/path-utils';
|
|
15
14
|
import '@homer0/simple-logger';
|
|
15
|
+
import '@homer0/simple-config';
|
|
16
|
+
import '@homer0/events-hub';
|
|
16
17
|
import 'statuses';
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -76,4 +77,4 @@ type HTTPErrorClass = typeof HTTPError;
|
|
|
76
77
|
*/
|
|
77
78
|
declare const httpErrorProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
78
79
|
|
|
79
|
-
export { CreateHTTPErrorFn, HTTPError, HTTPErrorClass, createHTTPError, httpErrorProvider };
|
|
80
|
+
export { type CreateHTTPErrorFn, HTTPError, type HTTPErrorClass, createHTTPError, httpErrorProvider };
|
|
@@ -26,6 +26,15 @@ module.exports = __toCommonJS(httpError_exports);
|
|
|
26
26
|
var import_utils = require("../../utils");
|
|
27
27
|
var import_appError = require("./appError");
|
|
28
28
|
class HTTPError extends import_appError.AppError {
|
|
29
|
+
/**
|
|
30
|
+
* @param message The error message.
|
|
31
|
+
* @param status The HTTP status code.
|
|
32
|
+
* @param context Context information related to the error.
|
|
33
|
+
* @param statuses A reference to the service that generates HTTP status codes. This
|
|
34
|
+
* is in case the implementation wants to use a special version from
|
|
35
|
+
* the container; otherwise, it will use the `statuses` library
|
|
36
|
+
* directly.
|
|
37
|
+
*/
|
|
29
38
|
constructor(message, status = (0, import_utils.statuses)("ok"), context = {}, statuses = import_utils.statuses) {
|
|
30
39
|
super(message, { ...context, status }, statuses);
|
|
31
40
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/common/httpError.ts"],"sourcesContent":["import { provider, statuses as statusesFn, type Statuses } from '../../utils';\nimport { AppError, type AppErrorContext } from './appError';\n/**\n * A type of error to be used on HTTP requests. This is the most common type of error used\n * by Jimpex.\n *\n * @group Services\n * @group Services/HTTPError\n */\nexport class HTTPError extends AppError {\n /**\n * @param message The error message.\n * @param status The HTTP status code.\n * @param context Context information related to the error.\n * @param statuses A reference to the service that generates HTTP status codes. This\n * is in case the implementation wants to use a special version from\n * the container; otherwise, it will use the `statuses` library\n * directly.\n */\n constructor(\n message: string,\n status: number | string = statusesFn('ok'),\n context: AppErrorContext = {},\n statuses: Statuses = statusesFn,\n ) {\n super(message, { ...context, status }, statuses);\n }\n}\n/**\n * Shorthand for `new HTTPError()`.\n *\n * @param args The same parameters as the {@link HTTPError} constructor.\n * @returns A new instance of {@link HTTPError}.\n * @group Services\n * @group Services/HTTPError\n */\nexport const createHTTPError = (\n ...args: ConstructorParameters<typeof HTTPError>\n): HTTPError => new HTTPError(...args);\n/**\n * The type of the function that generates a new instance of {@link HTTPError}.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/HTTPError\n */\nexport type CreateHTTPErrorFn = typeof createHTTPError;\n/**\n * THe type of the {@link HTTPError} class.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/HTTPError\n */\nexport type HTTPErrorClass = typeof HTTPError;\n/**\n * A service provider that will register both the {@link HTTPError} and a generator\n * function on the container. `HTTPError` will be the key for class, and `httpError` will\n * be for the generator function.\n *\n * @example\n *\n * // Register it on the container\n * container.register(httpErrorProvider);\n * // Getting access to the class.\n * const HTTPError = container.get<HTTPErrorClass>('HTTPError');\n * // Getting access to the function.\n * const httpError = container.get<CreateHTTPErrorFn>('httpError');\n *\n * @group Providers\n * @group Services/HTTPError\n */\nexport const httpErrorProvider = provider((app) => {\n app.set('HTTPError', () => HTTPError);\n app.set('httpError', () => createHTTPError);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgE;AAChE,sBAA+C;AAQxC,MAAM,kBAAkB,yBAAS;AAAA,EAUtC,YACE,SACA,aAA0B,aAAAA,UAAW,IAAI,GACzC,UAA2B,CAAC,GAC5B,WAAqB,aAAAA,UACrB;AACA,UAAM,SAAS,EAAE,GAAG,SAAS,OAAO,GAAG,QAAQ;AAAA,EACjD;AACF;AASO,MAAM,kBAAkB,IAC1B,SACW,IAAI,UAAU,GAAG,IAAI;AAgC9B,MAAM,wBAAoB,uBAAS,CAAC,QAAQ;AACjD,MAAI,IAAI,aAAa,MAAM,SAAS;AACpC,MAAI,IAAI,aAAa,MAAM,eAAe;AAC5C,CAAC;","names":["statusesFn"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/services/common/httpError.ts"],"sourcesContent":["import { provider, statuses as statusesFn, type Statuses } from '../../utils';\nimport { AppError, type AppErrorContext } from './appError';\n/**\n * A type of error to be used on HTTP requests. This is the most common type of error used\n * by Jimpex.\n *\n * @group Services\n * @group Services/HTTPError\n */\nexport class HTTPError extends AppError {\n /**\n * @param message The error message.\n * @param status The HTTP status code.\n * @param context Context information related to the error.\n * @param statuses A reference to the service that generates HTTP status codes. This\n * is in case the implementation wants to use a special version from\n * the container; otherwise, it will use the `statuses` library\n * directly.\n */\n constructor(\n message: string,\n status: number | string = statusesFn('ok'),\n context: AppErrorContext = {},\n statuses: Statuses = statusesFn,\n ) {\n super(message, { ...context, status }, statuses);\n }\n}\n/**\n * Shorthand for `new HTTPError()`.\n *\n * @param args The same parameters as the {@link HTTPError} constructor.\n * @returns A new instance of {@link HTTPError}.\n * @group Services\n * @group Services/HTTPError\n */\nexport const createHTTPError = (\n ...args: ConstructorParameters<typeof HTTPError>\n): HTTPError => new HTTPError(...args);\n/**\n * The type of the function that generates a new instance of {@link HTTPError}.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/HTTPError\n */\nexport type CreateHTTPErrorFn = typeof createHTTPError;\n/**\n * THe type of the {@link HTTPError} class.\n * This is exported to make it easy to type the dependency injection.\n *\n * @group Services/HTTPError\n */\nexport type HTTPErrorClass = typeof HTTPError;\n/**\n * A service provider that will register both the {@link HTTPError} and a generator\n * function on the container. `HTTPError` will be the key for class, and `httpError` will\n * be for the generator function.\n *\n * @example\n *\n * // Register it on the container\n * container.register(httpErrorProvider);\n * // Getting access to the class.\n * const HTTPError = container.get<HTTPErrorClass>('HTTPError');\n * // Getting access to the function.\n * const httpError = container.get<CreateHTTPErrorFn>('httpError');\n *\n * @group Providers\n * @group Services/HTTPError\n */\nexport const httpErrorProvider = provider((app) => {\n app.set('HTTPError', () => HTTPError);\n app.set('httpError', () => createHTTPError);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgE;AAChE,sBAA+C;AAQxC,MAAM,kBAAkB,yBAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUtC,YACE,SACA,aAA0B,aAAAA,UAAW,IAAI,GACzC,UAA2B,CAAC,GAC5B,WAAqB,aAAAA,UACrB;AACA,UAAM,SAAS,EAAE,GAAG,SAAS,OAAO,GAAG,QAAQ;AAAA,EACjD;AACF;AASO,MAAM,kBAAkB,IAC1B,SACW,IAAI,UAAU,GAAG,IAAI;AAgC9B,MAAM,wBAAoB,uBAAS,CAAC,QAAQ;AACjD,MAAI,IAAI,aAAa,MAAM,SAAS;AACpC,MAAI,IAAI,aAAa,MAAM,eAAe;AAC5C,CAAC;","names":["statusesFn"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
+
import { J as Jimpex } from '../../index-Bwf7JHu9.mjs';
|
|
3
|
+
export { AppError, AppErrorClass, AppErrorContext, CreateAppErrorFn, appErrorProvider, createAppError } from './appError.mjs';
|
|
4
|
+
export { CreateHTTPErrorFn, HTTPError, HTTPErrorClass, createHTTPError, httpErrorProvider } from './httpError.mjs';
|
|
5
|
+
export { SendFile, SendFileGeneratorOptions, SendFileOptions, sendFile, sendFileProvider } from './sendFile.mjs';
|
|
6
|
+
import '../../types/express.mjs';
|
|
7
|
+
import 'express';
|
|
8
|
+
import '../../types/http.mjs';
|
|
9
|
+
import 'https';
|
|
10
|
+
import 'http';
|
|
11
|
+
import 'spdy';
|
|
12
|
+
import 'node-fetch';
|
|
13
|
+
import '../../types/utils.mjs';
|
|
14
|
+
import '@homer0/path-utils';
|
|
15
|
+
import '@homer0/simple-logger';
|
|
16
|
+
import '@homer0/simple-config';
|
|
17
|
+
import '@homer0/events-hub';
|
|
18
|
+
import '../../utils/fns/statuses.mjs';
|
|
19
|
+
import 'statuses';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Registers all the common services on the container.
|
|
23
|
+
*
|
|
24
|
+
* - {@link AppError | appError}
|
|
25
|
+
* - {@link HTTPError | httpError}
|
|
26
|
+
* - {@link SendFile | sendFile}
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
*
|
|
30
|
+
* // Register the collection on the container
|
|
31
|
+
* container.register(commonServicesProvider);
|
|
32
|
+
* // Getting access to one the services instance
|
|
33
|
+
* const sendFile = container.get<SendFile>('sendFile');
|
|
34
|
+
*
|
|
35
|
+
* @group Providers
|
|
36
|
+
*/
|
|
37
|
+
declare const commonServicesProvider: {
|
|
38
|
+
appErrorProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
39
|
+
httpErrorProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
40
|
+
sendFileProvider: _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>;
|
|
41
|
+
} & Record<string, _homer0_jimple.Resource<"provider", "register", _homer0_jimple.ProviderRegisterFn<Jimpex>>> & {
|
|
42
|
+
provider: true;
|
|
43
|
+
} & {
|
|
44
|
+
register: _homer0_jimple.ProviderRegisterFn<Jimpex>;
|
|
45
|
+
} & Record<string, unknown>;
|
|
46
|
+
|
|
47
|
+
export { commonServicesProvider };
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
-
import { J as Jimpex } from '../../
|
|
2
|
+
import { J as Jimpex } from '../../index-C6I3NCC-.js';
|
|
3
3
|
export { AppError, AppErrorClass, AppErrorContext, CreateAppErrorFn, appErrorProvider, createAppError } from './appError.js';
|
|
4
4
|
export { CreateHTTPErrorFn, HTTPError, HTTPErrorClass, createHTTPError, httpErrorProvider } from './httpError.js';
|
|
5
5
|
export { SendFile, SendFileGeneratorOptions, SendFileOptions, sendFile, sendFileProvider } from './sendFile.js';
|
|
6
|
-
import '@homer0/events-hub';
|
|
7
6
|
import '../../types/express.js';
|
|
8
7
|
import 'express';
|
|
9
8
|
import '../../types/http.js';
|
|
@@ -11,12 +10,13 @@ import 'https';
|
|
|
11
10
|
import 'http';
|
|
12
11
|
import 'spdy';
|
|
13
12
|
import 'node-fetch';
|
|
14
|
-
import '@homer0/simple-config';
|
|
15
13
|
import '../../types/utils.js';
|
|
14
|
+
import '@homer0/path-utils';
|
|
16
15
|
import '@homer0/simple-logger';
|
|
16
|
+
import '@homer0/simple-config';
|
|
17
|
+
import '@homer0/events-hub';
|
|
17
18
|
import '../../utils/fns/statuses.js';
|
|
18
19
|
import 'statuses';
|
|
19
|
-
import '@homer0/path-utils';
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Registers all the common services on the container.
|
|
@@ -36,6 +36,9 @@ const commonServicesProvider = (0, import_utils.providers)({
|
|
|
36
36
|
});
|
|
37
37
|
// Annotate the CommonJS export names for ESM import in node:
|
|
38
38
|
0 && (module.exports = {
|
|
39
|
-
commonServicesProvider
|
|
39
|
+
commonServicesProvider,
|
|
40
|
+
...require("./appError"),
|
|
41
|
+
...require("./httpError"),
|
|
42
|
+
...require("./sendFile")
|
|
40
43
|
});
|
|
41
44
|
//# sourceMappingURL=index.js.map
|