Skip to content

Commit 9ccd81f

Browse files
authored
Fb/fts scope callback (#55)
* wip 1 * wip 2 * use resolve * pathlib * there is no try * trust that cds context exists * keep it const
1 parent d6963f4 commit 9ccd81f

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

cds-plugin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const _overwriteServiceAccessRoles = (envFeatureToggles) => {
4040
});
4141
};
4242

43-
const _registerFeatureProvider = () => {
43+
const _registerFeatureProvider = (envFeatureToggles) => {
4444
if (!cds.env.requires?.toggles) {
4545
return;
4646
}
@@ -56,17 +56,20 @@ const _registerFeatureProvider = () => {
5656
return;
5757
}
5858

59+
const defaultFtsScopeCallback = (context) => ({ user: context.user?.id, tenant: context.tenant });
60+
const ftsScopeCallback = envFeatureToggles?.ftsScopeCallback
61+
? require(pathlib.resolve(envFeatureToggles.ftsScopeCallback))
62+
: defaultFtsScopeCallback;
63+
5964
const _getReqFeatures = (req) => {
6065
if (doEnableHeaderFeatures && req.headers.features) {
6166
return req.headers.features;
6267
}
63-
if (cds.context?.user?.features) {
68+
if (cds.context.user?.features) {
6469
return cds.context.user.features;
6570
}
66-
const user = cds.context?.user?.id;
67-
const tenant = cds.context?.tenant;
6871
return cdsFeatures.reduce((result, [key, feature]) => {
69-
if (toggles.getFeatureValue(key, { user, tenant })) {
72+
if (toggles.getFeatureValue(key, ftsScopeCallback(cds.context, key))) {
7073
result.push(feature);
7174
}
7275
return result;
@@ -122,7 +125,7 @@ const activate = async () => {
122125
configAuto: ftsAutoConfig,
123126
});
124127

125-
_registerFeatureProvider();
128+
_registerFeatureProvider(envFeatureToggles);
126129
};
127130

128131
// NOTE: for sap/cds < 7.3.0 it was expected to export activate as function property, otherwise export the promise of

src/featureToggles.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// TODO locale for validation messages
1515

1616
const { promisify } = require("util");
17-
const path = require("path");
17+
const pathlib = require("path");
1818
const { readFile } = require("fs");
1919
const VError = require("verror");
2020
const yaml = require("yaml");
@@ -31,7 +31,7 @@ const { Semaphore } = require("./shared/semaphore");
3131
const ENV_UNIQUE_NAME = process.env[ENV.UNIQUE_NAME];
3232
const DEFAULT_REDIS_CHANNEL = process.env[ENV.REDIS_CHANNEL] || "features";
3333
const DEFAULT_REDIS_KEY = process.env[ENV.REDIS_KEY] || "features";
34-
const DEFAULT_CONFIG_FILEPATH = path.join(process.cwd(), ".features.yaml");
34+
const DEFAULT_CONFIG_FILEPATH = pathlib.join(process.cwd(), ".features.yaml");
3535
const FEATURE_VALID_TYPES = ["string", "number", "boolean"];
3636

3737
const SUPER_SCOPE_CACHE_SIZE_LIMIT = 15;
@@ -110,8 +110,7 @@ class FeatureToggles {
110110
// ========================================
111111

112112
_processValidations(featureKey, validations, configFilepath) {
113-
const workingDir = process.cwd();
114-
const configDir = configFilepath ? path.dirname(configFilepath) : workingDir;
113+
const configDir = configFilepath ? pathlib.dirname(configFilepath) : process.cwd();
115114

116115
const validationsScopesMap = {};
117116
const validationsRegex = [];
@@ -130,15 +129,9 @@ class FeatureToggles {
130129
}
131130

132131
if (validation.module) {
133-
let modulePath = validation.module.replace("$CONFIG_DIR", configDir);
134-
if (!path.isAbsolute(modulePath)) {
135-
modulePath = path.join(workingDir, modulePath);
136-
}
137-
let validator = tryRequire(modulePath);
138-
139-
if (validation.call) {
140-
validator = validator?.[validation.call];
141-
}
132+
const modulePath = validation.module.replace("$CONFIG_DIR", configDir);
133+
const validatorModule = tryRequire(pathlib.resolve(modulePath));
134+
const validator = validation.call ? validatorModule?.[validation.call] : validatorModule;
142135

143136
const validatorType = typeof validator;
144137
if (validatorType === "function") {

0 commit comments

Comments
 (0)