Skip to content

Commit f811441

Browse files
authored
Fb/restrict header features (#42)
* better jsdocs * restrict header features to dev profile
1 parent 46e57a9 commit f811441

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

cds-plugin.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const { closeMainClient, closeSubscriberClient } = require("./src/redisWrapper")
88

99
const FEATURE_KEY_REGEX = /\/fts\/([^\s/]+)$/;
1010

11+
const doEnableHeaderFeatures = cds.env.profiles?.includes("development");
12+
1113
const _overwriteServiceAccessRoles = (envFeatureToggles) => {
1214
if (!Array.isArray(envFeatureToggles.serviceAccessRoles)) {
1315
return;
@@ -34,19 +36,26 @@ const _registerFeatureProvider = () => {
3436
if (cdsFeatures.length === 0) {
3537
return;
3638
}
39+
40+
const _getReqFeatures = (req) => {
41+
if (doEnableHeaderFeatures && req.headers.features) {
42+
return req.headers.features;
43+
}
44+
if (cds.context?.user?.features) {
45+
return cds.context.user.features;
46+
}
47+
const user = cds.context?.user?.id;
48+
const tenant = cds.context?.tenant;
49+
return cdsFeatures.reduce((result, [key, feature]) => {
50+
if (toggles.getFeatureValue(key, { user, tenant })) {
51+
result.push(feature);
52+
}
53+
return result;
54+
}, []);
55+
};
3756
cds.middlewares.add(
3857
function cds_feature_provider(req, res, next) {
39-
const user = cds.context?.user?.id;
40-
const tenant = cds.context?.tenant;
41-
req.features =
42-
req.headers.features ||
43-
cds.context?.user?.features ||
44-
cdsFeatures.reduce((result, [key, feature]) => {
45-
if (toggles.getFeatureValue(key, { user, tenant })) {
46-
result.push(feature);
47-
}
48-
return result;
49-
}, []);
58+
req.features = _getReqFeatures(req);
5059
next();
5160
},
5261
{ before: "ctx_model" }

src/featureToggles.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class FeatureToggles {
494494
*
495495
* @param {string} featureKey feature key
496496
* @param {string|number|boolean|null} value intended value
497-
* @param {Map<string, string>} [scopeMap] optional scope restrictions
497+
* @param {Object} [scopeMap] optional scope restrictions
498498
* @returns {Promise<Array<ValidationError>>} validation errors if any are found or an empty array otherwise
499499
*/
500500
async validateFeatureValue(featureKey, value, scopeMap = undefined) {
@@ -988,7 +988,7 @@ class FeatureToggles {
988988
* const resultForTenant = getFeatureValue(FEATURE_VALUE_KEY, { tenant: "tenant123" });
989989
*
990990
* @param {string} featureKey valid feature key
991-
* @param {Map<string, string>} [scopeMap] optional scope restrictions
991+
* @param {Object} [scopeMap] optional scope restrictions
992992
* @returns {string|number|boolean|null}
993993
*/
994994
getFeatureValue(featureKey, scopeMap = undefined) {
@@ -1305,7 +1305,7 @@ class FeatureToggles {
13051305
*
13061306
* @param {string} featureKey valid feature key
13071307
* @param {string|number|boolean|null} newValue new value of valid type or null for deletion
1308-
* @param {Map<string, string>} [scopeMap] optional object with scope restrictions
1308+
* @param {Object} [scopeMap] optional scope restrictions
13091309
* @param {ChangeOptions} [options] optional extra change options
13101310
* @returns {Promise<Array<ValidationError> | void>}
13111311
*/
@@ -1333,7 +1333,7 @@ class FeatureToggles {
13331333
*
13341334
* @param {boolean | number | string | null} newValue
13351335
* @param {boolean | number | string} oldValue
1336-
* @param {Map<string, string>} [scopeMap] optional value in case a scopeMap
1336+
* @param {Object} [scopeMap] optional scope restrictions
13371337
* @param {ChangeOptions} [options] optional switch to clear all sub scopes
13381338
*/
13391339
/**
@@ -1382,7 +1382,7 @@ class FeatureToggles {
13821382
* scopeKey for reference.
13831383
*
13841384
* @param {boolean | number | string} newValue
1385-
* @param {Map<string, string>} [scopeMap] optional scopeMap for reference
1385+
* @param {Object} [scopeMap] optional scopeMap for reference
13861386
* @param {string} [scopeKey] optional scopeKey for reference
13871387
* @returns {undefined | ValidationError | Array<ValidationError>} in case of failure a ValidationError, or an array
13881388
* of ValidationErrors, otherwise undefined

0 commit comments

Comments
 (0)