exframe-middleware 1.3.2 → 1.5.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/.eslintrc.json +2 -32
- package/index.js +3 -12
- package/index.mjs +9 -0
- package/lib/authorizeUser.js +1 -2
- package/lib/buildCspFromHeaders.js +3 -4
- package/lib/handleErrors.js +27 -18
- package/lib/standardRouteResponse.js +2 -4
- package/package.json +14 -4
- package/test/.eslintrc.json +2 -4
package/.eslintrc.json
CHANGED
|
@@ -1,33 +1,3 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
|
|
4
|
-
"node": true
|
|
5
|
-
},
|
|
6
|
-
"extends": "airbnb-base",
|
|
7
|
-
"parserOptions": {
|
|
8
|
-
"ecmaVersion": 2020
|
|
9
|
-
},
|
|
10
|
-
"rules": {
|
|
11
|
-
"comma-dangle": [2, "never"],
|
|
12
|
-
"no-underscore-dangle": "off",
|
|
13
|
-
"consistent-return": "off",
|
|
14
|
-
"import/no-unresolved": [2, { "commonjs": true }],
|
|
15
|
-
"max-len": [1, { "code": 250 }],
|
|
16
|
-
"linebreak-style": "off",
|
|
17
|
-
"strict": [0, "global"],
|
|
18
|
-
"no-restricted-syntax": "off",
|
|
19
|
-
"global-require": "off",
|
|
20
|
-
"no-nested-ternary": "off",
|
|
21
|
-
"no-confusing-arrow": "off",
|
|
22
|
-
"arrow-body-style": "off",
|
|
23
|
-
"arrow-parens": "off",
|
|
24
|
-
"brace-style": "off",
|
|
25
|
-
"nonblock-statement-body-position": "off",
|
|
26
|
-
"curly": "off",
|
|
27
|
-
"no-use-before-define": "off",
|
|
28
|
-
"no-plusplus": "off",
|
|
29
|
-
"func-names": "off",
|
|
30
|
-
"no-continue": "off",
|
|
31
|
-
"array-bracket-spacing": [1, "always"]
|
|
32
|
-
}
|
|
33
|
-
}
|
|
2
|
+
"extends": "exzeo/base"
|
|
3
|
+
}
|
package/index.js
CHANGED
|
@@ -7,10 +7,7 @@ const authorizeUser = require('./lib/authorizeUser');
|
|
|
7
7
|
const buildCspFromHeader = require('./lib/buildCspFromHeaders');
|
|
8
8
|
|
|
9
9
|
module.exports = {
|
|
10
|
-
|
|
11
|
-
* @param {Object} config the configuration object
|
|
12
|
-
*/
|
|
13
|
-
handleErrors: (config = {}) => ({ // eslint-disable-line no-unused-vars
|
|
10
|
+
handleErrors: () => ({
|
|
14
11
|
mq: async (error, context, message) => handleErrors(context, error, message),
|
|
15
12
|
express: (error, request, response, next) => {
|
|
16
13
|
if (!error) return next();
|
|
@@ -38,16 +35,10 @@ module.exports = {
|
|
|
38
35
|
next();
|
|
39
36
|
}
|
|
40
37
|
},
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @param {Object} config
|
|
44
|
-
* @param {Boolean} [config.cspIsRequired] should throw error when csp parts do not exist -- default true
|
|
45
|
-
*/
|
|
46
|
-
buildCsp: (config = {}) => ({ // eslint-disable-line no-unused-vars
|
|
47
|
-
// eslint-disable-next-line no-param-reassign
|
|
38
|
+
buildCsp: () => ({
|
|
48
39
|
mq: context => { context.csp = context.csp || {}; },
|
|
49
40
|
express: (req, res, next) => {
|
|
50
|
-
buildCspFromHeader(req,
|
|
41
|
+
buildCspFromHeader(req, {});
|
|
51
42
|
next();
|
|
52
43
|
}
|
|
53
44
|
}),
|
package/index.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import middleware from './index.js';
|
|
4
|
+
|
|
5
|
+
const { handleErrors, standardRouteResponseBuilder, authorizeUser, buildCsp, buildExpressRequestContext } = middleware;
|
|
6
|
+
|
|
7
|
+
export { handleErrors, standardRouteResponseBuilder, authorizeUser, buildCsp, buildExpressRequestContext };
|
|
8
|
+
|
|
9
|
+
export default middleware;
|
package/lib/authorizeUser.js
CHANGED
|
@@ -7,6 +7,5 @@ const errors = require('exframe-errors');
|
|
|
7
7
|
* @param {Object} context.user
|
|
8
8
|
*/
|
|
9
9
|
module.exports = ({ user }) => {
|
|
10
|
-
if (!user)
|
|
11
|
-
throw new errors.Unauthorized('Could not authorize user');
|
|
10
|
+
if (!user) { throw new errors.Unauthorized('Could not authorize user'); }
|
|
12
11
|
};
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
/* eslint-disable no-param-reassign */
|
|
4
|
-
|
|
5
3
|
const errors = require('exframe-errors');
|
|
6
4
|
|
|
7
5
|
module.exports = (req, { cspIsRequired = true }) => {
|
|
8
6
|
const csp = req.headers['harmony-csp'] || '';
|
|
9
|
-
if (cspIsRequired && !/^[A-Z]+:[A-Z]{2}:[A-Z\d]+$/.test(csp))
|
|
7
|
+
if (cspIsRequired && csp && !/^[A-Z]+:[A-Z]{2}:[A-Z\d]+$/.test(csp)) {
|
|
10
8
|
throw new errors.Forbidden('CSP is required');
|
|
9
|
+
}
|
|
11
10
|
|
|
12
|
-
const [
|
|
11
|
+
const [companyCode, state, product] = csp.split(/:/g);
|
|
13
12
|
req.csp = {
|
|
14
13
|
// in instance of csp === '', only companyCode will not default to ''
|
|
15
14
|
companyCode: companyCode || undefined,
|
package/lib/handleErrors.js
CHANGED
|
@@ -13,8 +13,11 @@ module.exports = (context, rawError, optionalIncomingMessage) => {
|
|
|
13
13
|
// not complete errors and represent each schema validation error from a single request. These objects will not
|
|
14
14
|
// have a "statusCode" property and is the reason for the check here to keep them out of the workflow error logic.
|
|
15
15
|
if (Array.isArray(rawError.errors) && rawError.errors.length > 0 && rawError.errors[0].statusCode !== undefined) {
|
|
16
|
-
if (rawError.errors.length > 1)
|
|
17
|
-
|
|
16
|
+
if (rawError.errors.length > 1) {
|
|
17
|
+
error = rawError.errors;
|
|
18
|
+
} else {
|
|
19
|
+
error = rawError.errors[0] || rawError;
|
|
20
|
+
}
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
const { errorCode } = error;
|
|
@@ -25,39 +28,45 @@ module.exports = (context, rawError, optionalIncomingMessage) => {
|
|
|
25
28
|
// uses statusCode hierarchy of buckets a, else b, else c
|
|
26
29
|
if (Array.isArray(error) && error.length !== 0) {
|
|
27
30
|
message = `multiple errors occured: [${error.map(v => v.message).join(', ')}]`;
|
|
31
|
+
const errorsToCheck = error.filter(({ statusCode }) => statusCode >= 400);
|
|
28
32
|
|
|
29
33
|
const a = new Set(); // 401 or 403
|
|
30
34
|
const b = new Set(); // 500
|
|
31
35
|
const c = new Set(); // other 400s
|
|
32
36
|
|
|
33
|
-
for (const { statusCode } of
|
|
34
|
-
if (statusCode
|
|
35
|
-
if (statusCode === 401 || statusCode === 403)
|
|
37
|
+
for (const { statusCode } of errorsToCheck) {
|
|
38
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
36
39
|
a.add(statusCode);
|
|
37
|
-
else if (statusCode > 499)
|
|
40
|
+
} else if (statusCode > 499) {
|
|
38
41
|
b.add(statusCode);
|
|
39
|
-
else
|
|
42
|
+
} else {
|
|
43
|
+
c.add(statusCode);
|
|
44
|
+
}
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
if (a.size)
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
if (a.size) {
|
|
48
|
+
status = a.has(403) ? 403 : 401;
|
|
49
|
+
} else if (b.size) {
|
|
50
|
+
if (b.size === 1) {
|
|
45
51
|
status = b.values().next().value;
|
|
46
|
-
else
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
} else {
|
|
53
|
+
status = 500;
|
|
54
|
+
}
|
|
55
|
+
} else if (c.size === 1) {
|
|
56
|
+
status = c.values().next().value;
|
|
57
|
+
} else {
|
|
58
|
+
status = 400;
|
|
52
59
|
}
|
|
60
|
+
} else {
|
|
61
|
+
status = error.statusCode || error.status || 500;
|
|
53
62
|
}
|
|
54
|
-
else status = error.statusCode || error.status || 500;
|
|
55
63
|
|
|
56
64
|
if (status === 500) {
|
|
57
65
|
context.log.error('An error occurred', { errorDetails: error });
|
|
58
66
|
message = error.message || 'Internal Server Error';
|
|
67
|
+
} else {
|
|
68
|
+
context.log.warn('There was an issue with the client request', { message: optionalIncomingMessage || error.message, errorDetails: error });
|
|
59
69
|
}
|
|
60
|
-
else context.log.warn('There was an issue with the client request', { message: optionalIncomingMessage || error.message, errorDetails: error });
|
|
61
70
|
|
|
62
71
|
return { status, message, errorCode };
|
|
63
72
|
};
|
|
@@ -13,14 +13,12 @@ module.exports = (result, isSingleItemResponse = true) => {
|
|
|
13
13
|
status = 404;
|
|
14
14
|
message = 'not found';
|
|
15
15
|
result = undefined; // eslint-disable-line no-param-reassign
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
16
|
+
} else {
|
|
18
17
|
status = 200;
|
|
19
18
|
message = 'ok';
|
|
20
19
|
result = []; // eslint-disable-line no-param-reassign
|
|
21
20
|
}
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
21
|
+
} else {
|
|
24
22
|
status = 200;
|
|
25
23
|
message = 'ok';
|
|
26
24
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exframe-middleware",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"module": "index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./index.mjs",
|
|
10
|
+
"require": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
6
13
|
"config": {
|
|
7
14
|
"reporter": "mocha-exzeo-reporter"
|
|
8
15
|
},
|
|
@@ -23,8 +30,8 @@
|
|
|
23
30
|
"devDependencies": {
|
|
24
31
|
"chai": "*",
|
|
25
32
|
"eslint": "*",
|
|
26
|
-
"eslint-config-
|
|
27
|
-
"eslint-plugin-import": "
|
|
33
|
+
"eslint-config-exzeo": "*",
|
|
34
|
+
"eslint-plugin-import": "*",
|
|
28
35
|
"mocha": "*",
|
|
29
36
|
"mocha-exzeo-reporter": "*",
|
|
30
37
|
"nyc": "*",
|
|
@@ -33,5 +40,8 @@
|
|
|
33
40
|
"dependencies": {
|
|
34
41
|
"exframe-errors": "^1.2.0"
|
|
35
42
|
},
|
|
36
|
-
"
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=14.0.0"
|
|
45
|
+
},
|
|
46
|
+
"gitHead": "846806709f00dedc764c794b3ee05ca3c7297244"
|
|
37
47
|
}
|
package/test/.eslintrc.json
CHANGED