@alanszp/express 11.0.0 → 12.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/dist/helpers/renderErrorJson.d.ts +4 -0
- package/dist/helpers/renderErrorJson.js +32 -0
- package/dist/helpers/renderErrorJson.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/middlewares/authedForOrg.js +3 -4
- package/dist/middlewares/authedForOrg.js.map +1 -1
- package/dist/middlewares/authenticateUser.js +4 -5
- package/dist/middlewares/authenticateUser.js.map +1 -1
- package/dist/middlewares/createContext.js +2 -2
- package/dist/middlewares/createContext.js.map +1 -1
- package/dist/middlewares/createContext.test.js +3 -3
- package/dist/middlewares/createContext.test.js.map +1 -1
- package/dist/middlewares/hasRoles.js +2 -3
- package/dist/middlewares/hasRoles.js.map +1 -1
- package/dist/middlewares/jsonBodyParser.js +3 -4
- package/dist/middlewares/jsonBodyParser.js.map +1 -1
- package/dist/middlewares/returnInternalServerError.js +1 -2
- package/dist/middlewares/returnInternalServerError.js.map +1 -1
- package/dist/middlewares/returnNotFound.js +2 -3
- package/dist/middlewares/returnNotFound.js.map +1 -1
- package/package.json +7 -7
- package/src/helpers/renderErrorJson.ts +29 -0
- package/src/index.ts +0 -1
- package/src/middlewares/authedForOrg.ts +3 -4
- package/src/middlewares/authenticateUser.ts +7 -10
- package/src/middlewares/createContext.test.ts +1 -1
- package/src/middlewares/createContext.ts +1 -1
- package/src/middlewares/hasRoles.ts +2 -3
- package/src/middlewares/jsonBodyParser.ts +3 -4
- package/src/middlewares/returnInternalServerError.ts +1 -2
- package/src/middlewares/returnNotFound.ts +2 -3
- package/dist/helpers/appIdentifier.d.ts +0 -1
- package/dist/helpers/appIdentifier.js +0 -10
- package/dist/helpers/appIdentifier.js.map +0 -1
- package/dist/views/errorView.d.ts +0 -2
- package/dist/views/errorView.js +0 -14
- package/dist/views/errorView.js.map +0 -1
- package/src/helpers/appIdentifier.ts +0 -5
- package/src/views/errorView.ts +0 -11
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.render400Error = exports.render404Error = exports.render401Error = void 0;
|
|
4
|
+
const core_1 = require("@alanszp/core");
|
|
5
|
+
function render401Error(requiredChecks) {
|
|
6
|
+
return {
|
|
7
|
+
code: "unauthorized_error",
|
|
8
|
+
message: "Unauthorized Error",
|
|
9
|
+
context: { requiredChecks },
|
|
10
|
+
origin: (0, core_1.appIdentifier)(),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
exports.render401Error = render401Error;
|
|
14
|
+
function render404Error() {
|
|
15
|
+
return {
|
|
16
|
+
code: "not_found",
|
|
17
|
+
message: "Not Found",
|
|
18
|
+
context: {},
|
|
19
|
+
origin: (0, core_1.appIdentifier)(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.render404Error = render404Error;
|
|
23
|
+
function render400Error(message) {
|
|
24
|
+
return {
|
|
25
|
+
code: "bad_request",
|
|
26
|
+
message,
|
|
27
|
+
context: {},
|
|
28
|
+
origin: (0, core_1.appIdentifier)(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.render400Error = render400Error;
|
|
32
|
+
//# sourceMappingURL=renderErrorJson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderErrorJson.js","sourceRoot":"","sources":["../../src/helpers/renderErrorJson.ts"],"names":[],"mappings":";;;AAAA,wCAA8C;AAG9C,SAAgB,cAAc,CAAC,cAAwB;IACrD,OAAO;QACL,IAAI,EAAE,oBAAoB;QAC1B,OAAO,EAAE,oBAAoB;QAC7B,OAAO,EAAE,EAAE,cAAc,EAAE;QAC3B,MAAM,EAAE,IAAA,oBAAa,GAAE;KACxB,CAAC;AACJ,CAAC;AAPD,wCAOC;AAED,SAAgB,cAAc;IAC5B,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,WAAW;QACpB,OAAO,EAAE,EAAE;QACX,MAAM,EAAE,IAAA,oBAAa,GAAE;KACxB,CAAC;AACJ,CAAC;AAPD,wCAOC;AAED,SAAgB,cAAc,CAAC,OAAe;IAC5C,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,OAAO;QACP,OAAO,EAAE,EAAE;QACX,MAAM,EAAE,IAAA,oBAAa,GAAE;KACxB,CAAC;AACJ,CAAC;AAPD,wCAOC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -10,7 +10,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./views/errorView"), exports);
|
|
14
13
|
__exportStar(require("./types/AuthMethod"), exports);
|
|
15
14
|
__exportStar(require("./middlewares/auditLog"), exports);
|
|
16
15
|
__exportStar(require("./middlewares/accessLogger"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAmC;AACnC,yDAAuC;AACvC,6DAA2C;AAC3C,6DAA2C;AAC3C,8DAA4C;AAC5C,iEAA+C;AAC/C,0EAAwD;AACxD,+DAA6C;AAC7C,+DAA6C;AAC7C,yDAAuC"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.authForOrg = void 0;
|
|
4
|
-
const errors_1 = require("@alanszp/errors");
|
|
5
|
-
const errorView_1 = require("../views/errorView");
|
|
6
4
|
const getRequestLogger_1 = require("../helpers/getRequestLogger");
|
|
5
|
+
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
7
6
|
function response401(res) {
|
|
8
|
-
res.status(401).json((0,
|
|
7
|
+
res.status(401).json((0, renderErrorJson_1.render401Error)(["jwt"]));
|
|
9
8
|
}
|
|
10
9
|
function authForOrg(req, res, next) {
|
|
11
10
|
var _a, _b, _c;
|
|
@@ -39,7 +38,7 @@ function authForOrg(req, res, next) {
|
|
|
39
38
|
logger.info("middleware.authForOrg.error.noOrganization", {
|
|
40
39
|
error,
|
|
41
40
|
});
|
|
42
|
-
res.status(404).json((0,
|
|
41
|
+
res.status(404).json((0, renderErrorJson_1.render404Error)());
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
44
|
exports.authForOrg = authForOrg;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authedForOrg.js","sourceRoot":"","sources":["../../src/middlewares/authedForOrg.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"authedForOrg.js","sourceRoot":"","sources":["../../src/middlewares/authedForOrg.ts"],"names":[],"mappings":";;;AACA,kEAA+D;AAE/D,gEAA4E;AAE5E,SAAS,WAAW,CAAC,GAAa;IAChC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,EAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAChD,CAAC;AAED,SAAgB,UAAU,CACxB,GAAmB,EACnB,GAAa,EACb,IAAkB;;IAElB,MAAM,EAAE,YAAY,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;IACpC,MAAM,MAAM,GAAG,IAAA,mCAAgB,EAAC,GAAG,CAAC,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC,CAAC;IAClE,IAAI;QACF,IAAI,CAAC,YAAY,EAAE;YACjB,MAAM,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAC;YACjE,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;SACzB;QAED,IAAI,CAAC,CAAA,MAAA,MAAA,GAAG,CAAC,OAAO,0CAAE,OAAO,0CAAE,qBAAqB,CAAA,EAAE;YAChD,MAAM,CAAC,IAAI,CAAC,wCAAwC,EAAE;gBACpD,OAAO,EAAE,CAAA,MAAA,GAAG,CAAC,OAAO,0CAAE,OAAO,KAAI,IAAI;aACtC,CAAC,CAAC;YACH,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;SACzB;QAED,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,qBAAqB,CAAC;QACzD,IAAI,MAAM,KAAK,YAAY,EAAE;YAC3B,IAAI,MAAM,KAAK,MAAM,EAAE;gBACrB,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,qBAAqB,GAAG,YAAY,CAAC;gBACzD,OAAO,IAAI,EAAE,CAAC;aACf;YAED,MAAM,CAAC,IAAI,CAAC,sCAAsC,EAAE;gBAClD,MAAM;aACP,CAAC,CAAC;YACH,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;SACzB;QAED,OAAO,IAAI,EAAE,CAAC;KACf;IAAC,OAAO,KAAc,EAAE;QACvB,MAAM,CAAC,IAAI,CAAC,4CAA4C,EAAE;YACxD,KAAK;SACN,CAAC,CAAC;QACH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,GAAE,CAAC,CAAC;KACxC;AACH,CAAC;AAxCD,gCAwCC"}
|
|
@@ -11,10 +11,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createAuthContext = exports.AuthMethods = void 0;
|
|
13
13
|
const jwt_1 = require("@alanszp/jwt");
|
|
14
|
-
const errors_1 = require("@alanszp/errors");
|
|
15
|
-
const errorView_1 = require("../views/errorView");
|
|
16
14
|
const getRequestLogger_1 = require("../helpers/getRequestLogger");
|
|
17
15
|
const lodash_1 = require("lodash");
|
|
16
|
+
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
18
17
|
function parseAuthorizationHeader(authorization) {
|
|
19
18
|
if (!authorization)
|
|
20
19
|
return undefined;
|
|
@@ -87,11 +86,11 @@ function createAuthContext(options) {
|
|
|
87
86
|
if ((0, lodash_1.isEmpty)(successfulAuthAttempts)) {
|
|
88
87
|
res
|
|
89
88
|
.status(401)
|
|
90
|
-
.json((0,
|
|
89
|
+
.json((0, renderErrorJson_1.render401Error)([
|
|
91
90
|
authAttempts.includes(null)
|
|
92
91
|
? `Token invalid for methods ${authMethods}`
|
|
93
92
|
: `Token not set for methods ${authMethods}`,
|
|
94
|
-
]))
|
|
93
|
+
]));
|
|
95
94
|
return;
|
|
96
95
|
}
|
|
97
96
|
const jwtUser = successfulAuthAttempts[0];
|
|
@@ -106,7 +105,7 @@ function createAuthContext(options) {
|
|
|
106
105
|
methods: AuthMethods,
|
|
107
106
|
error,
|
|
108
107
|
});
|
|
109
|
-
res.status(401).json((0,
|
|
108
|
+
res.status(401).json((0, renderErrorJson_1.render401Error)(authMethods));
|
|
110
109
|
}
|
|
111
110
|
});
|
|
112
111
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticateUser.js","sourceRoot":"","sources":["../../src/middlewares/authenticateUser.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sCAAiE;
|
|
1
|
+
{"version":3,"file":"authenticateUser.js","sourceRoot":"","sources":["../../src/middlewares/authenticateUser.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sCAAiE;AAEjE,kEAA+D;AAG/D,mCAAgD;AAChD,gEAA4D;AAE5D,SAAS,wBAAwB,CAC/B,aAAiC;IAEjC,IAAI,CAAC,aAAa;QAAE,OAAO,SAAS,CAAC;IACrC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,KA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ACF,CACF,CAAC;oBAEF,MAAM,sBAAsB,GAAG,IAAA,gBAAO,EAAC,YAAY,CAAC,CAAC;oBAErD,IAAI,IAAA,gBAAO,EAAC,sBAAsB,CAAC,EAAE;wBACnC,GAAG;6BACA,MAAM,CAAC,GAAG,CAAC;6BACX,IAAI,CACH,IAAA,gCAAc,EAAC;4BACb,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC;gCACzB,CAAC,CAAC,6BAA6B,WAAW,EAAE;gCAC5C,CAAC,CAAC,6BAA6B,WAAW,EAAE;yBAC/C,CAAC,CACH,CAAC;wBACJ,OAAO;qBACR;oBAED,MAAM,OAAO,GAAY,sBAAsB,CAAC,CAAC,CAAC,CAAC;oBACnD,GAAG,CAAC,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC;oBAC9B,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,CAC5B,OAAO,CAAC,iBAAiB,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CACtD,CAAC;oBACF,IAAI,EAAE,CAAC;iBACR;gBAAC,OAAO,KAAc,EAAE;oBACvB,MAAM,CAAC,IAAI,CAAC,6BAA6B,EAAE;wBACzC,GAAG;wBACH,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,aAAa;wBAChC,OAAO,EAAE,WAAW;wBACpB,KAAK;qBACN,CAAC,CAAC;oBACH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,EAAC,WAAW,CAAC,CAAC,CAAC;iBACnD;YACH,CAAC;SAAA,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AA3DD,8CA2DC"}
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createContext = void 0;
|
|
4
4
|
const cuid2_1 = require("@paralleldrive/cuid2");
|
|
5
|
-
const
|
|
5
|
+
const core_1 = require("@alanszp/core");
|
|
6
6
|
const lodash_1 = require("lodash");
|
|
7
7
|
function createContext(sharedContext, baseLogger, audit) {
|
|
8
8
|
return (req, res, next) => {
|
|
9
9
|
var _a, _b, _c, _d;
|
|
10
10
|
req.context = req.context || {};
|
|
11
11
|
const receivedChain = req.header("x-lifecycle-chain") || ((_b = (_a = req.body) === null || _a === void 0 ? void 0 : _a.detail) === null || _b === void 0 ? void 0 : _b.lch);
|
|
12
|
-
const lifecycleChain = (0, lodash_1.compact)([receivedChain, (0,
|
|
12
|
+
const lifecycleChain = (0, lodash_1.compact)([receivedChain, (0, core_1.appIdentifier)()]).join(",");
|
|
13
13
|
const lifecycleId = req.header("x-lifecycle-id") || ((_d = (_c = req.body) === null || _c === void 0 ? void 0 : _c.detail) === null || _d === void 0 ? void 0 : _d.lid) || (0, cuid2_1.createId)();
|
|
14
14
|
const contextId = (0, cuid2_1.createId)();
|
|
15
15
|
res.setHeader("x-lifecycle-id", lifecycleId);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createContext.js","sourceRoot":"","sources":["../../src/middlewares/createContext.ts"],"names":[],"mappings":";;;AAAA,gDAAgD;AAIhD,
|
|
1
|
+
{"version":3,"file":"createContext.js","sourceRoot":"","sources":["../../src/middlewares/createContext.ts"],"names":[],"mappings":";;;AAAA,gDAAgD;AAIhD,wCAA8C;AAG9C,mCAAiC;AAEjC,SAAgB,aAAa,CAC3B,aAA4B,EAC5B,UAAmB,EACnB,KAAY;IAEZ,OAAO,CAAC,GAAmB,EAAE,GAAa,EAAE,IAAkB,EAAQ,EAAE;;QACtE,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;QAEhC,MAAM,aAAa,GACjB,GAAG,CAAC,MAAM,CAAC,mBAAmB,CAAC,KAAI,MAAA,MAAA,GAAG,CAAC,IAAI,0CAAE,MAAM,0CAAE,GAAG,CAAA,CAAC;QAC3D,MAAM,cAAc,GAAG,IAAA,gBAAO,EAAC,CAAC,aAAa,EAAE,IAAA,oBAAa,GAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAE3E,MAAM,WAAW,GACf,GAAG,CAAC,MAAM,CAAC,gBAAgB,CAAC,KAAI,MAAA,MAAA,GAAG,CAAC,IAAI,0CAAE,MAAM,0CAAE,GAAG,CAAA,IAAI,IAAA,gBAAQ,GAAE,CAAC;QAEtE,MAAM,SAAS,GAAG,IAAA,gBAAQ,GAAE,CAAC;QAE7B,GAAG,CAAC,SAAS,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;QAC7C,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;QAEzC,aAAa,CAAC,GAAG,CACf,CAAC,OAAO,EAAE,EAAE;YACV,GAAG,CAAC,OAAO,CAAC,aAAa,GAAG,EAAE,CAAC;YAC/B,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;YAC9C,GAAG,CAAC,OAAO,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;YACpD,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;YAC1C,GAAG,CAAC,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;YACjC,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;YAClC,IAAI,EAAE,CAAC;QACT,CAAC,EACD;YACE,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,KAAK,CAAC,SAAS,EAAE;YACxB,WAAW;YACX,cAAc;YACd,SAAS;SACV,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAvCD,sCAuCC"}
|
|
@@ -5,7 +5,7 @@ const shared_context_1 = require("@alanszp/shared-context");
|
|
|
5
5
|
const logger_1 = require("@alanszp/logger");
|
|
6
6
|
const audit_1 = require("@alanszp/audit");
|
|
7
7
|
const expressMocks_1 = require("../test/mocks/expressMocks");
|
|
8
|
-
const
|
|
8
|
+
const core_1 = require("@alanszp/core");
|
|
9
9
|
jest.mock("@alanszp/shared-context");
|
|
10
10
|
const logger = (0, logger_1.createMockLogger)({});
|
|
11
11
|
const sharedContext = new shared_context_1.SharedContext();
|
|
@@ -24,7 +24,7 @@ describe("CreateContext", () => {
|
|
|
24
24
|
}), (0, expressMocks_1.mockResponse)(), (0, expressMocks_1.mockNext)());
|
|
25
25
|
expect(sharedContext.run).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
|
|
26
26
|
lifecycleId,
|
|
27
|
-
lifecycleChain: `${lifecycleChain},${(0,
|
|
27
|
+
lifecycleChain: `${lifecycleChain},${(0, core_1.appIdentifier)()}`,
|
|
28
28
|
}));
|
|
29
29
|
});
|
|
30
30
|
});
|
|
@@ -42,7 +42,7 @@ describe("CreateContext", () => {
|
|
|
42
42
|
}), (0, expressMocks_1.mockResponse)(), (0, expressMocks_1.mockNext)());
|
|
43
43
|
expect(sharedContext.run).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
|
|
44
44
|
lifecycleId,
|
|
45
|
-
lifecycleChain: `${lifecycleChain},${(0,
|
|
45
|
+
lifecycleChain: `${lifecycleChain},${(0, core_1.appIdentifier)()}`,
|
|
46
46
|
}));
|
|
47
47
|
});
|
|
48
48
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createContext.test.js","sourceRoot":"","sources":["../../src/middlewares/createContext.test.ts"],"names":[],"mappings":";;AAAA,mDAAgD;AAChD,4DAAwD;AACxD,4CAAmD;AACnD,0CAAmD;AACnD,6DAKoC;AACpC,
|
|
1
|
+
{"version":3,"file":"createContext.test.js","sourceRoot":"","sources":["../../src/middlewares/createContext.test.ts"],"names":[],"mappings":";;AAAA,mDAAgD;AAChD,4DAAwD;AACxD,4CAAmD;AACnD,0CAAmD;AACnD,6DAKoC;AACpC,wCAA8C;AAC9C,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;AAErC,MAAM,MAAM,G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C,CAC1B,CAAC;YAEF,UAAU,CACR,IAAA,kCAAmB,EACjB,WAAW,EACX,EAAE,EACF;gBACE,MAAM,EAAE;oBACN,GAAG,EAAE,cAAc;oBACnB,GAAG,EAAE,WAAW;iBACjB;aACF,CACF,EACD,IAAA,2BAAY,GAAE,EACd,IAAA,uBAAQ,GAAE,CACX,CAAC;YAEF,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,oBAAoB,CAC5C,MAAM,CAAC,QAAQ,EAAE,EACjB,MAAM,CAAC,gBAAgB,CAAC;gBACtB,WAAW;gBACX,cAAc,EAAE,GAAG,cAAc,IAAI,IAAA,oBAAa,GAAE,EAAE;aACvD,CAAC,CACH,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.hasRoles = void 0;
|
|
4
|
-
const
|
|
5
|
-
const errorView_1 = require("../views/errorView");
|
|
4
|
+
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
6
5
|
function response401(res) {
|
|
7
|
-
res.status(401).json((0,
|
|
6
|
+
res.status(401).json((0, renderErrorJson_1.render401Error)(["permissions"]));
|
|
8
7
|
}
|
|
9
8
|
function hasRoles(roles) {
|
|
10
9
|
return (req, res, next) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hasRoles.js","sourceRoot":"","sources":["../../src/middlewares/hasRoles.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"hasRoles.js","sourceRoot":"","sources":["../../src/middlewares/hasRoles.ts"],"names":[],"mappings":";;;AAEA,gEAA4D;AAE5D,SAAS,WAAW,CAAC,GAAa;IAChC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,EAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;AACxD,CAAC;AAED,SAAgB,QAAQ,CACtB,KAAwB;IAExB,OAAO,CAAC,GAAmB,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;QAChE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC;QAChC,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;SACzB;QAED,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;YAC3B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,WAAW,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC;AACJ,CAAC;AAfD,4BAeC"}
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jsonBodyParser = void 0;
|
|
4
4
|
const body_parser_1 = require("body-parser");
|
|
5
|
-
const
|
|
6
|
-
const errorView_1 = require("../views/errorView");
|
|
5
|
+
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
7
6
|
function jsonBodyParser(options) {
|
|
8
7
|
const bodyParser = (0, body_parser_1.json)(Object.assign({ limit: "1mb" }, options));
|
|
9
8
|
return function jsonBodyParserMiddleware(req, res, next) {
|
|
@@ -11,11 +10,11 @@ function jsonBodyParser(options) {
|
|
|
11
10
|
bodyParser(req, res, (error) => {
|
|
12
11
|
if (!error)
|
|
13
12
|
return next();
|
|
14
|
-
res.status(400).json((0,
|
|
13
|
+
res.status(400).json((0, renderErrorJson_1.render400Error)("Malformed JSON"));
|
|
15
14
|
});
|
|
16
15
|
}
|
|
17
16
|
catch (error) {
|
|
18
|
-
res.status(400).json((0,
|
|
17
|
+
res.status(400).json((0, renderErrorJson_1.render400Error)("Malformed JSON"));
|
|
19
18
|
}
|
|
20
19
|
};
|
|
21
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonBodyParser.js","sourceRoot":"","sources":["../../src/middlewares/jsonBodyParser.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;
|
|
1
|
+
{"version":3,"file":"jsonBodyParser.js","sourceRoot":"","sources":["../../src/middlewares/jsonBodyParser.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAGhD,gEAA4D;AAE5D,SAAgB,cAAc,CAAC,OAAqB;IAClD,MAAM,UAAU,GAAG,IAAA,kBAAI,kBAAG,KAAK,EAAE,KAAK,IAAK,OAAO,EAAG,CAAC;IACtD,OAAO,SAAS,wBAAwB,CACtC,GAAmB,EACnB,GAAa,EACb,IAAkB;QAElB,IAAI;YACF,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,KAAe,EAAE,EAAE;gBACvC,IAAI,CAAC,KAAK;oBAAE,OAAO,IAAI,EAAE,CAAC;gBAC1B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,EAAC,gBAAgB,CAAC,CAAC,CAAC;YACzD,CAAC,CAAC,CAAC;SACJ;QAAC,OAAO,KAAc,EAAE;YACvB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,EAAC,gBAAgB,CAAC,CAAC,CAAC;SACxD;IACH,CAAC,CAAC;AACJ,CAAC;AAhBD,wCAgBC"}
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.returnInternalServerError = void 0;
|
|
4
4
|
const errors_1 = require("@alanszp/errors");
|
|
5
|
-
const errorView_1 = require("../views/errorView");
|
|
6
5
|
const returnInternalServerError = (getLogger) => (error, req, res, _next) => {
|
|
7
|
-
res.status(500).json(
|
|
6
|
+
res.status(500).json(new errors_1.InternalServerError(error).toView());
|
|
8
7
|
getLogger().error("error_to_client", { error });
|
|
9
8
|
};
|
|
10
9
|
exports.returnInternalServerError = returnInternalServerError;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"returnInternalServerError.js","sourceRoot":"","sources":["../../src/middlewares/returnInternalServerError.ts"],"names":[],"mappings":";;;AACA,4CAAsD;
|
|
1
|
+
{"version":3,"file":"returnInternalServerError.js","sourceRoot":"","sources":["../../src/middlewares/returnInternalServerError.ts"],"names":[],"mappings":";;;AACA,4CAAsD;AAQ/C,MAAM,yBAAyB,GACpC,CAAC,SAAwB,EAAE,EAAE,CAC7B,CAAC,KAAc,EAAE,GAAmB,EAAE,GAAa,EAAE,KAAmB,EAAE,EAAE;IAC1E,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,4BAAmB,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IAC9D,SAAS,EAAE,CAAC,KAAK,CAAC,iBAAiB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AAClD,CAAC,CAAC;AALS,QAAA,yBAAyB,6BAKlC"}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.returnNotFound = void 0;
|
|
4
|
-
const
|
|
5
|
-
const errorView_1 = require("../views/errorView");
|
|
4
|
+
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
6
5
|
function returnNotFound(_req, res, _next) {
|
|
7
|
-
res.status(404).json((0,
|
|
6
|
+
res.status(404).json((0, renderErrorJson_1.render404Error)());
|
|
8
7
|
}
|
|
9
8
|
exports.returnNotFound = returnNotFound;
|
|
10
9
|
//# sourceMappingURL=returnNotFound.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"returnNotFound.js","sourceRoot":"","sources":["../../src/middlewares/returnNotFound.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"returnNotFound.js","sourceRoot":"","sources":["../../src/middlewares/returnNotFound.ts"],"names":[],"mappings":";;;AAEA,gEAA4D;AAE5D,SAAgB,cAAc,CAC5B,IAAoB,EACpB,GAAa,EACb,KAAmB;IAEnB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,GAAE,CAAC,CAAC;AACzC,CAAC;AAND,wCAMC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "Alan's express utils and middlewares.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -35,15 +35,15 @@
|
|
|
35
35
|
"typescript": "^4.3.4"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@alanszp/audit": "^
|
|
39
|
-
"@alanszp/errors": "^
|
|
40
|
-
"@alanszp/jwt": "^
|
|
41
|
-
"@alanszp/logger": "^
|
|
42
|
-
"@alanszp/shared-context": "^
|
|
38
|
+
"@alanszp/audit": "^12.0.0",
|
|
39
|
+
"@alanszp/errors": "^12.0.0",
|
|
40
|
+
"@alanszp/jwt": "^12.0.0",
|
|
41
|
+
"@alanszp/logger": "^12.0.0",
|
|
42
|
+
"@alanszp/shared-context": "^12.0.0",
|
|
43
43
|
"@babel/core": "^7.23.9",
|
|
44
44
|
"@paralleldrive/cuid2": "^2.2.2",
|
|
45
45
|
"body-parser": "^1.20.2",
|
|
46
46
|
"lodash": "^4.17.21"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "085a76a387bc0e272e09e9cb1b6f48f814512940"
|
|
49
49
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { appIdentifier } from "@alanszp/core";
|
|
2
|
+
import { RenderableView } from "@alanszp/errors";
|
|
3
|
+
|
|
4
|
+
export function render401Error(requiredChecks: string[]): RenderableView {
|
|
5
|
+
return {
|
|
6
|
+
code: "unauthorized_error",
|
|
7
|
+
message: "Unauthorized Error",
|
|
8
|
+
context: { requiredChecks },
|
|
9
|
+
origin: appIdentifier(),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function render404Error(): RenderableView {
|
|
14
|
+
return {
|
|
15
|
+
code: "not_found",
|
|
16
|
+
message: "Not Found",
|
|
17
|
+
context: {},
|
|
18
|
+
origin: appIdentifier(),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function render400Error(message: string): RenderableView {
|
|
23
|
+
return {
|
|
24
|
+
code: "bad_request",
|
|
25
|
+
message,
|
|
26
|
+
context: {},
|
|
27
|
+
origin: appIdentifier(),
|
|
28
|
+
};
|
|
29
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { NotFoundError, UnauthorizedError } from "@alanszp/errors";
|
|
2
|
-
import { errorView } from "../views/errorView";
|
|
3
1
|
import { NextFunction, Response } from "express";
|
|
4
2
|
import { getRequestLogger } from "../helpers/getRequestLogger";
|
|
5
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
4
|
+
import { render401Error, render404Error } from "../helpers/renderErrorJson";
|
|
6
5
|
|
|
7
6
|
function response401(res: Response): void {
|
|
8
|
-
res.status(401).json(
|
|
7
|
+
res.status(401).json(render401Error(["jwt"]));
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
export function authForOrg(
|
|
@@ -46,6 +45,6 @@ export function authForOrg(
|
|
|
46
45
|
logger.info("middleware.authForOrg.error.noOrganization", {
|
|
47
46
|
error,
|
|
48
47
|
});
|
|
49
|
-
res.status(404).json(
|
|
48
|
+
res.status(404).json(render404Error());
|
|
50
49
|
}
|
|
51
50
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { JWTUser, verifyJWT, VerifyOptions } from "@alanszp/jwt";
|
|
2
|
-
import { UnauthorizedError } from "@alanszp/errors";
|
|
3
|
-
import { errorView } from "../views/errorView";
|
|
4
2
|
import { NextFunction, Response } from "express";
|
|
5
3
|
import { getRequestLogger } from "../helpers/getRequestLogger";
|
|
6
4
|
import { GenericRequest } from "../types/GenericRequest";
|
|
7
5
|
import { ILogger } from "@alanszp/logger";
|
|
8
6
|
import { compact, isEmpty, omit } from "lodash";
|
|
7
|
+
import { render401Error } from "../helpers/renderErrorJson";
|
|
9
8
|
|
|
10
9
|
function parseAuthorizationHeader(
|
|
11
10
|
authorization: string | undefined
|
|
@@ -143,13 +142,11 @@ export function createAuthContext<Options extends AuthOptions>(
|
|
|
143
142
|
res
|
|
144
143
|
.status(401)
|
|
145
144
|
.json(
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
])
|
|
152
|
-
)
|
|
145
|
+
render401Error([
|
|
146
|
+
authAttempts.includes(null)
|
|
147
|
+
? `Token invalid for methods ${authMethods}`
|
|
148
|
+
: `Token not set for methods ${authMethods}`,
|
|
149
|
+
])
|
|
153
150
|
);
|
|
154
151
|
return;
|
|
155
152
|
}
|
|
@@ -167,7 +164,7 @@ export function createAuthContext<Options extends AuthOptions>(
|
|
|
167
164
|
methods: AuthMethods,
|
|
168
165
|
error,
|
|
169
166
|
});
|
|
170
|
-
res.status(401).json(
|
|
167
|
+
res.status(401).json(render401Error(authMethods));
|
|
171
168
|
}
|
|
172
169
|
};
|
|
173
170
|
};
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
mockRequestWithBody,
|
|
9
9
|
mockResponse,
|
|
10
10
|
} from "../test/mocks/expressMocks";
|
|
11
|
-
import { appIdentifier } from "
|
|
11
|
+
import { appIdentifier } from "@alanszp/core";
|
|
12
12
|
jest.mock("@alanszp/shared-context");
|
|
13
13
|
|
|
14
14
|
const logger = createMockLogger({});
|
|
@@ -2,7 +2,7 @@ import { createId } from "@paralleldrive/cuid2";
|
|
|
2
2
|
import { NextFunction, Response } from "express";
|
|
3
3
|
import { ILogger } from "@alanszp/logger";
|
|
4
4
|
import { Audit } from "@alanszp/audit";
|
|
5
|
-
import { appIdentifier } from "
|
|
5
|
+
import { appIdentifier } from "@alanszp/core";
|
|
6
6
|
import { GenericRequest } from "../types/GenericRequest";
|
|
7
7
|
import { SharedContext } from "@alanszp/shared-context";
|
|
8
8
|
import { compact } from "lodash";
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { UnauthorizedError } from "@alanszp/errors";
|
|
2
|
-
import { errorView } from "../views/errorView";
|
|
3
1
|
import { NextFunction, Response } from "express";
|
|
4
2
|
import { GenericRequest } from "../types/GenericRequest";
|
|
3
|
+
import { render401Error } from "../helpers/renderErrorJson";
|
|
5
4
|
|
|
6
5
|
function response401(res: Response): void {
|
|
7
|
-
res.status(401).json(
|
|
6
|
+
res.status(401).json(render401Error(["permissions"]));
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
export function hasRoles(
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { json, OptionsJson } from "body-parser";
|
|
2
2
|
import { NextFunction, Response } from "express";
|
|
3
|
-
import { BadRequestError } from "@alanszp/errors";
|
|
4
|
-
import { errorView } from "../views/errorView";
|
|
5
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
4
|
+
import { render400Error } from "../helpers/renderErrorJson";
|
|
6
5
|
|
|
7
6
|
export function jsonBodyParser(options?: OptionsJson) {
|
|
8
7
|
const bodyParser = json({ limit: "1mb", ...options });
|
|
@@ -14,10 +13,10 @@ export function jsonBodyParser(options?: OptionsJson) {
|
|
|
14
13
|
try {
|
|
15
14
|
bodyParser(req, res, (error?: unknown) => {
|
|
16
15
|
if (!error) return next();
|
|
17
|
-
res.status(400).json(
|
|
16
|
+
res.status(400).json(render400Error("Malformed JSON"));
|
|
18
17
|
});
|
|
19
18
|
} catch (error: unknown) {
|
|
20
|
-
res.status(400).json(
|
|
19
|
+
res.status(400).json(render400Error("Malformed JSON"));
|
|
21
20
|
}
|
|
22
21
|
};
|
|
23
22
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ErrorRequestHandler, NextFunction, Response } from "express";
|
|
2
2
|
import { InternalServerError } from "@alanszp/errors";
|
|
3
|
-
import { errorView } from "../views/errorView";
|
|
4
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
5
4
|
import { ILogger } from "@alanszp/logger";
|
|
6
5
|
|
|
@@ -11,6 +10,6 @@ export type GetInternalServerErrorMiddleware = (
|
|
|
11
10
|
export const returnInternalServerError: GetInternalServerErrorMiddleware =
|
|
12
11
|
(getLogger: () => ILogger) =>
|
|
13
12
|
(error: unknown, req: GenericRequest, res: Response, _next: NextFunction) => {
|
|
14
|
-
res.status(500).json(
|
|
13
|
+
res.status(500).json(new InternalServerError(error).toView());
|
|
15
14
|
getLogger().error("error_to_client", { error });
|
|
16
15
|
};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { NextFunction, Response } from "express";
|
|
2
|
-
import { NotFoundError } from "@alanszp/errors";
|
|
3
|
-
import { errorView } from "../views/errorView";
|
|
4
2
|
import { GenericRequest } from "../types/GenericRequest";
|
|
3
|
+
import { render404Error } from "../helpers/renderErrorJson";
|
|
5
4
|
|
|
6
5
|
export function returnNotFound(
|
|
7
6
|
_req: GenericRequest,
|
|
8
7
|
res: Response,
|
|
9
8
|
_next: NextFunction
|
|
10
9
|
): void {
|
|
11
|
-
res.status(404).json(
|
|
10
|
+
res.status(404).json(render404Error());
|
|
12
11
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function appIdentifier(): string;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.appIdentifier = void 0;
|
|
4
|
-
function appIdentifier() {
|
|
5
|
-
const appName = process.env.API_ORIGIN_NAME || "undefined";
|
|
6
|
-
const env = process.env.NODE_ENV || "development";
|
|
7
|
-
return `${appName}:${env}`;
|
|
8
|
-
}
|
|
9
|
-
exports.appIdentifier = appIdentifier;
|
|
10
|
-
//# sourceMappingURL=appIdentifier.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"appIdentifier.js","sourceRoot":"","sources":["../../src/helpers/appIdentifier.ts"],"names":[],"mappings":";;;AAAA,SAAgB,aAAa;IAC3B,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,WAAW,CAAC;IAC3D,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;IAClD,OAAO,GAAG,OAAO,IAAI,GAAG,EAAE,CAAC;AAC7B,CAAC;AAJD,sCAIC"}
|
package/dist/views/errorView.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.errorView = void 0;
|
|
4
|
-
const appIdentifier_1 = require("../helpers/appIdentifier");
|
|
5
|
-
function errorView(err) {
|
|
6
|
-
return {
|
|
7
|
-
code: err.code(),
|
|
8
|
-
message: err.renderMessage(),
|
|
9
|
-
context: err.context(),
|
|
10
|
-
origin: (0, appIdentifier_1.appIdentifier)(),
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
exports.errorView = errorView;
|
|
14
|
-
//# sourceMappingURL=errorView.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errorView.js","sourceRoot":"","sources":["../../src/views/errorView.ts"],"names":[],"mappings":";;;AACA,4DAAyD;AAEzD,SAAgB,SAAS,CAAC,GAAoB;IAC5C,OAAO;QACL,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE;QAChB,OAAO,EAAE,GAAG,CAAC,aAAa,EAAE;QAC5B,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE;QACtB,MAAM,EAAE,IAAA,6BAAa,GAAE;KACxB,CAAC;AACJ,CAAC;AAPD,8BAOC"}
|
package/src/views/errorView.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { RenderableError } from "@alanszp/errors";
|
|
2
|
-
import { appIdentifier } from "../helpers/appIdentifier";
|
|
3
|
-
|
|
4
|
-
export function errorView(err: RenderableError): unknown {
|
|
5
|
-
return {
|
|
6
|
-
code: err.code(),
|
|
7
|
-
message: err.renderMessage(),
|
|
8
|
-
context: err.context(),
|
|
9
|
-
origin: appIdentifier(),
|
|
10
|
-
};
|
|
11
|
-
}
|