@dvsa/cvs-feature-flags 1.1.0-canary.0 → 1.1.0-canary.1
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/feature-flags.d.ts +0 -9
- package/feature-flags.js +22 -15
- package/package.json +1 -1
package/feature-flags.d.ts
CHANGED
|
@@ -1,11 +1,2 @@
|
|
|
1
1
|
import { FeatureFlagsClientName } from './';
|
|
2
|
-
export interface Variant {
|
|
3
|
-
enabled: boolean;
|
|
4
|
-
emails: string[];
|
|
5
|
-
overrides: Record<string, unknown>;
|
|
6
|
-
}
|
|
7
|
-
export interface VariantResult {
|
|
8
|
-
variant: string | null;
|
|
9
|
-
flags: Record<string, unknown>;
|
|
10
|
-
}
|
|
11
2
|
export declare const getFeatureFlags: <T>(clientName: FeatureFlagsClientName, email?: string) => Promise<T>;
|
package/feature-flags.js
CHANGED
|
@@ -1,30 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.getFeatureFlags = void 0;
|
|
7
4
|
const appconfig_1 = require("@aws-lambda-powertools/parameters/appconfig");
|
|
8
|
-
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
9
5
|
const config_1 = require("./config");
|
|
6
|
+
function hasEmails(flag) {
|
|
7
|
+
return typeof flag === 'object' && flag !== null && Array.isArray(flag.emails);
|
|
8
|
+
}
|
|
9
|
+
function resolveVariants(config, email) {
|
|
10
|
+
const resolved = {};
|
|
11
|
+
for (const [key, value] of Object.entries(config)) {
|
|
12
|
+
if (!hasEmails(value)) {
|
|
13
|
+
resolved[key] = value;
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const { emails, ...flagValues } = value;
|
|
17
|
+
const normalisedEmail = email?.toLowerCase();
|
|
18
|
+
const isTargeted = normalisedEmail && emails.map((e) => e.toLowerCase()).includes(normalisedEmail);
|
|
19
|
+
if (isTargeted) {
|
|
20
|
+
resolved[key] = flagValues;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return resolved;
|
|
24
|
+
}
|
|
10
25
|
class FeatureFlagsClient {
|
|
11
26
|
async get(clientName, email) {
|
|
12
27
|
const config = await (0, appconfig_1.getAppConfig)(`${clientName}-profile`, {
|
|
13
28
|
...config_1.defaultFeatureFlagConfig,
|
|
14
29
|
transform: 'json',
|
|
15
30
|
});
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (!email || !variants) {
|
|
19
|
-
return flags;
|
|
20
|
-
}
|
|
21
|
-
const matchedVariant = Object.values(variants).find((v) => v.enabled && v.emails.map((e) => e.toLowerCase()).includes(email.toLowerCase()));
|
|
22
|
-
console.log('matchedVariant', { matchedVariant });
|
|
23
|
-
if (!matchedVariant) {
|
|
24
|
-
return flags;
|
|
31
|
+
if (!email) {
|
|
32
|
+
return config;
|
|
25
33
|
}
|
|
26
|
-
|
|
27
|
-
return (0, lodash_merge_1.default)({}, flags, matchedVariant.overrides);
|
|
34
|
+
return resolveVariants(config, email);
|
|
28
35
|
}
|
|
29
36
|
}
|
|
30
37
|
const getFeatureFlags = async (clientName, email) => {
|