@featurevisor/sdk 1.28.0 → 1.29.2

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.
@@ -1,4 +1,4 @@
1
- import { DatafileContent } from "@featurevisor/types";
1
+ import { Attribute, DatafileContent } from "@featurevisor/types";
2
2
  import { DatafileReader } from "./datafileReader";
3
3
 
4
4
  describe("sdk: DatafileReader", function () {
@@ -6,7 +6,7 @@ describe("sdk: DatafileReader", function () {
6
6
  expect(typeof DatafileReader).toEqual("function");
7
7
  });
8
8
 
9
- it("should return requested entities", function () {
9
+ it("v1 datafile schema: should return requested entities", function () {
10
10
  const datafileJson: DatafileContent = {
11
11
  schemaVersion: "1",
12
12
  revision: "1",
@@ -86,4 +86,90 @@ describe("sdk: DatafileReader", function () {
86
86
  expect(reader.getFeature("test")).toEqual(datafileJson.features[0]);
87
87
  expect(reader.getFeature("test2")).toEqual(undefined);
88
88
  });
89
+
90
+ it("v2 datafile schema: should return requested entities", function () {
91
+ const datafileJson: DatafileContent = {
92
+ schemaVersion: "2",
93
+ revision: "1",
94
+ attributes: {
95
+ userId: {
96
+ key: "userId",
97
+ type: "string",
98
+ capture: true,
99
+ },
100
+ country: {
101
+ key: "country",
102
+ type: "string",
103
+ },
104
+ },
105
+ segments: {
106
+ netherlands: {
107
+ key: "netherlands",
108
+ conditions: [
109
+ {
110
+ attribute: "country",
111
+ operator: "equals",
112
+ value: "nl",
113
+ },
114
+ ],
115
+ },
116
+ germany: {
117
+ key: "germany",
118
+ conditions: JSON.stringify([
119
+ {
120
+ attribute: "country",
121
+ operator: "equals",
122
+ value: "de",
123
+ },
124
+ ]),
125
+ },
126
+ },
127
+ features: {
128
+ test: {
129
+ key: "test",
130
+ bucketBy: "userId",
131
+ variations: [
132
+ { value: "control" },
133
+ {
134
+ value: "treatment",
135
+ variables: [
136
+ {
137
+ key: "showSidebar",
138
+ value: true,
139
+ },
140
+ ],
141
+ },
142
+ ],
143
+ traffic: [
144
+ {
145
+ key: "1",
146
+ segments: "*",
147
+ percentage: 100000,
148
+ allocation: [
149
+ { variation: "control", range: [0, 0] },
150
+ { variation: "treatment", range: [0, 100000] },
151
+ ],
152
+ },
153
+ ],
154
+ },
155
+ },
156
+ };
157
+
158
+ const reader = new DatafileReader(datafileJson);
159
+
160
+ expect(reader.getRevision()).toEqual("1");
161
+ expect(reader.getSchemaVersion()).toEqual("2");
162
+ expect(reader.getAllAttributes()).toEqual(
163
+ Object.keys(datafileJson.attributes).reduce((acc, key) => {
164
+ acc.push(datafileJson.attributes[key]);
165
+ return acc;
166
+ }, [] as Attribute[]),
167
+ );
168
+ expect(reader.getAttribute("userId")).toEqual(datafileJson.attributes.userId);
169
+ expect(reader.getSegment("netherlands")).toEqual(datafileJson.segments.netherlands);
170
+ expect((reader.getSegment("germany") as any).conditions[0].value).toEqual("de");
171
+ expect(reader.getSegment("belgium")).toEqual(undefined);
172
+ expect(reader.getFeature("test")).toEqual(datafileJson.features.test);
173
+ expect(reader.getFeature("test2")).toEqual(undefined);
174
+ });
89
175
  });
@@ -25,32 +25,39 @@ export class DatafileReader {
25
25
  private schemaVersion: string;
26
26
  private revision: string;
27
27
 
28
- // v1
29
- private attributes: Attribute[];
30
- private segments: Segment[];
31
- private features: Feature[];
32
-
33
- // v2
34
- private attributesV2: Record<AttributeKey, Attribute>;
35
- private segmentsV2: Record<SegmentKey, Segment>;
36
- private featuresV2: Record<FeatureKey, Feature>;
28
+ private attributes: Record<AttributeKey, Attribute>;
29
+ private segments: Record<SegmentKey, Segment>;
30
+ private features: Record<FeatureKey, Feature>;
37
31
 
38
32
  constructor(datafileJson: DatafileContentV1 | DatafileContentV2) {
39
33
  this.schemaVersion = datafileJson.schemaVersion;
40
34
  this.revision = datafileJson.revision;
41
35
 
42
36
  if (this.schemaVersion === "2") {
37
+ // v2
43
38
  const datafileJsonV2 = datafileJson as DatafileContentV2;
44
39
 
45
- this.attributesV2 = datafileJsonV2.attributes;
46
- this.segmentsV2 = datafileJsonV2.segments;
47
- this.featuresV2 = datafileJsonV2.features;
40
+ this.attributes = datafileJsonV2.attributes;
41
+ this.segments = datafileJsonV2.segments;
42
+ this.features = datafileJsonV2.features;
48
43
  } else {
44
+ // v1
49
45
  const datafileJsonV1 = datafileJson as DatafileContentV1;
50
46
 
51
- this.segments = datafileJsonV1.segments;
52
- this.attributes = datafileJsonV1.attributes;
53
- this.features = datafileJsonV1.features;
47
+ this.attributes = {};
48
+ datafileJsonV1.attributes.forEach((a) => {
49
+ this.attributes[a.key] = a;
50
+ });
51
+
52
+ this.segments = {};
53
+ datafileJsonV1.segments.forEach((s) => {
54
+ this.segments[s.key] = s;
55
+ });
56
+
57
+ this.features = {};
58
+ datafileJsonV1.features.forEach((f) => {
59
+ this.features[f.key] = f;
60
+ });
54
61
  }
55
62
  }
56
63
 
@@ -63,33 +70,21 @@ export class DatafileReader {
63
70
  }
64
71
 
65
72
  getAllAttributes(): Attribute[] {
66
- if (this.schemaVersion !== "2") {
67
- return this.attributes;
68
- }
69
-
70
- // v2
71
73
  const result: Attribute[] = [];
72
74
 
73
- for (const key in this.attributesV2) {
74
- result.push(this.attributesV2[key]);
75
- }
75
+ Object.keys(this.attributes).forEach((key) => {
76
+ result.push(this.attributes[key]);
77
+ });
76
78
 
77
79
  return result;
78
80
  }
79
81
 
80
82
  getAttribute(attributeKey: AttributeKey): Attribute | undefined {
81
- if (this.schemaVersion === "2") {
82
- return this.attributesV2[attributeKey];
83
- }
84
-
85
- return this.attributes.find((a) => a.key === attributeKey);
83
+ return this.attributes[attributeKey];
86
84
  }
87
85
 
88
86
  getSegment(segmentKey: SegmentKey): Segment | undefined {
89
- const segment =
90
- this.schemaVersion === "2"
91
- ? this.segmentsV2[segmentKey]
92
- : this.segments.find((s) => s.key === segmentKey);
87
+ const segment = this.segments[segmentKey];
93
88
 
94
89
  if (!segment) {
95
90
  return undefined;
@@ -99,15 +94,6 @@ export class DatafileReader {
99
94
  }
100
95
 
101
96
  getFeature(featureKey: FeatureKey): Feature | undefined {
102
- const feature =
103
- this.schemaVersion === "2"
104
- ? this.featuresV2[featureKey]
105
- : this.features.find((f) => f.key === featureKey);
106
-
107
- if (!feature) {
108
- return undefined;
109
- }
110
-
111
- return feature;
97
+ return this.features[featureKey];
112
98
  }
113
99
  }