Skip to content

Commit def41ff

Browse files
authored
feat: removed data attribute from snapshot (#212)
1 parent d86318b commit def41ff

15 files changed

+274
-286
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "switcher-client",
3-
"version": "4.4.2",
3+
"version": "4.5.0",
44
"description": "Client JS SDK for working with Switcher-API",
55
"main": "./switcher-client.js",
66
"type": "module",
@@ -32,8 +32,8 @@
3232
],
3333
"devDependencies": {
3434
"@babel/eslint-parser": "^7.28.4",
35-
"@typescript-eslint/eslint-plugin": "^8.45.0",
36-
"@typescript-eslint/parser": "^8.45.0",
35+
"@typescript-eslint/eslint-plugin": "^8.46.0",
36+
"@typescript-eslint/parser": "^8.46.0",
3737
"c8": "^10.1.3",
3838
"chai": "^6.2.0",
3939
"env-cmd": "^11.0.0",

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sonar.projectKey=switcherapi_switcher-client-master
22
sonar.projectName=switcher-client-js
33
sonar.organization=switcherapi
4-
sonar.projectVersion=4.4.2
4+
sonar.projectVersion=4.5.0
55
sonar.links.homepage=https://github.com/switcherapi/switcher-client-js
66

77
sonar.javascript.lcov.reportPaths=coverage/lcov.info

src/client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class Client {
118118

119119
const snapshot = await validateSnapshot(
120120
Client.#context,
121-
GlobalSnapshot.snapshot.data.domain.version
121+
GlobalSnapshot.snapshot.domain.version
122122
);
123123

124124
if (snapshot) {
@@ -149,7 +149,7 @@ export class Client {
149149
Client.watchSnapshot();
150150
}
151151

152-
return GlobalSnapshot.snapshot?.data.domain.version || 0;
152+
return GlobalSnapshot.snapshot?.domain.version || 0;
153153
}
154154

155155
/**
@@ -160,7 +160,7 @@ export class Client {
160160
* - GlobalOptions.local is false, meaning it will not use the local snapshot.
161161
*/
162162
static #isCheckSnapshotAvailable(fetchRemote) {
163-
return GlobalSnapshot.snapshot?.data.domain.version == 0 && (fetchRemote || !GlobalOptions.local);
163+
return GlobalSnapshot.snapshot?.domain.version == 0 && (fetchRemote || !GlobalOptions.local);
164164
}
165165

166166
static watchSnapshot(callback = {}) {
@@ -256,7 +256,7 @@ export class Client {
256256
}
257257

258258
static get snapshotVersion() {
259-
return GlobalSnapshot.snapshot?.data.domain.version || 0;
259+
return GlobalSnapshot.snapshot?.domain.version || 0;
260260
}
261261
}
262262

src/lib/remote.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export async function resolveSnapshot(domain, environment, component) {
166166
});
167167

168168
if (response.status == 200) {
169-
return JSON.stringify(response.json(), null, 4);
169+
return JSON.stringify(response.json().data, null, 4);
170170
}
171171

172172
throw new RemoteError(`[resolveSnapshot] failed with status ${response.status}`);

src/lib/resolver.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import * as util from '../lib/utils/index.js';
44
import { SwitcherResult } from './result.js';
55

66
/**
7-
* Resolves the criteria for a given switcher request against the snapshot data.
7+
* Resolves the criteria for a given switcher request against the snapshot domain.
88
*
9-
* @param {SnapshotData} data - The snapshot data containing domain and group information.
9+
* @param {Domain} domain - The domain containing groups and configurations.
1010
* @param {SwitcherRequest} switcher - The switcher request to be evaluated.
1111
* @returns {SwitcherResult} - The result of the switcher evaluation.
1212
*/
13-
function resolveCriteria(data, switcher) {
14-
if (!data.domain.activated) {
13+
function resolveCriteria(domain, switcher) {
14+
if (!domain.activated) {
1515
return SwitcherResult.disabled('Domain disabled');
1616
}
1717

18-
const { group } = data.domain;
18+
const { group } = domain;
1919
return checkGroup(group, switcher);
2020
}
2121

@@ -127,7 +127,7 @@ function isStrategyFulfilled(strategyEntry, strategyConfig) {
127127
/**
128128
* Checks the criteria for a switcher request against the local snapshot.
129129
*
130-
* @param {Snapshot | undefined} snapshot - The snapshot containing the data to check against.
130+
* @param {Snapshot | undefined} snapshot - The snapshot containing the domain to check against.
131131
* @param {SwitcherRequest} switcher - The switcher request to be evaluated.
132132
* @returns {SwitcherResult} - The result of the switcher evaluation.
133133
* @throws {Error} - If the snapshot is not loaded.
@@ -137,6 +137,6 @@ export default function checkCriteriaLocal(snapshot, switcher) {
137137
throw new Error('Snapshot not loaded. Try to use \'Client.loadSnapshot()\'');
138138
}
139139

140-
const { data } = snapshot;
141-
return resolveCriteria(data, switcher);
140+
const { domain } = snapshot;
141+
return resolveCriteria(domain, switcher);
142142
}

src/lib/snapshot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const loadDomain = (snapshotLocation, environment) => {
1414
if (existsSync(snapshotFile)) {
1515
dataBuffer = readFileSync(snapshotFile);
1616
} else {
17-
dataBuffer = JSON.stringify({ data: { domain: { version: 0 } } }, null, 4);
17+
dataBuffer = JSON.stringify({ domain: { version: 0 } }, null, 4);
1818

1919
if (snapshotLocation.length) {
2020
mkdirSync(snapshotLocation, { recursive: true });
@@ -40,7 +40,7 @@ const validateSnapshot = async (context, snapshotVersion) => {
4040
};
4141

4242
const checkSwitchersLocal = (snapshot, switcherKeys) => {
43-
const { group } = snapshot.data.domain;
43+
const { group } = snapshot.domain;
4444
let notFound = [], found = false;
4545

4646
for (const switcher of switcherKeys) {

tests/playground/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const _testSnapshotAutoUpdate = async () => {
187187
const time = Date.now();
188188
await switcher.checkValue('user_1').isItOn(SWITCHER_KEY);
189189
console.clear();
190-
console.log(Client.getLogger(SWITCHER_KEY), `executed in ${Date.now() - time}ms`);
190+
console.log(JSON.stringify(Client.getLogger(SWITCHER_KEY)), `executed in ${Date.now() - time}ms`);
191191
}, 2000);
192192
};
193193

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
{
2-
"data": {
3-
"domain": {
4-
"name": "Switcher API",
5-
"version": 1,
6-
"activated": true,
7-
"group": [
8-
{
9-
"name": "Test Project",
10-
"activated": true,
11-
"config": [
12-
{
13-
"key": "CLIENT_JS_FEATURE",
14-
"activated": true,
15-
"strategies": [
16-
{
17-
"strategy": "VALUE_VALIDATION",
18-
"activated": false,
19-
"operation": "EXIST",
20-
"values": [
21-
"user_1"
22-
]
23-
}
24-
],
25-
"components": [
26-
"switcher-client-js"
27-
]
28-
}
29-
]
30-
}
31-
]
32-
}
2+
"domain": {
3+
"name": "Switcher API",
4+
"version": 1,
5+
"activated": true,
6+
"group": [
7+
{
8+
"name": "Test Project",
9+
"activated": true,
10+
"config": [
11+
{
12+
"key": "CLIENT_JS_FEATURE",
13+
"activated": true,
14+
"strategies": [
15+
{
16+
"strategy": "VALUE_VALIDATION",
17+
"activated": false,
18+
"operation": "EXIST",
19+
"values": [
20+
"user_1"
21+
]
22+
}
23+
],
24+
"components": [
25+
"switcher-client-js"
26+
]
27+
}
28+
]
29+
}
30+
]
3331
}
3432
}
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
{
2-
"data": {
3-
"domain": {
4-
"name": "Switcher API",
5-
"version": 1,
6-
"activated": true,
7-
"group": [
8-
{
9-
"name": "Test Project",
10-
"activated": true,
11-
"config": [
12-
{
13-
"key": "CLIENT_JS_FEATURE",
14-
"activated": true,
15-
"strategies": [],
16-
"components": [
17-
"switcher-client-js"
18-
]
19-
}
20-
]
21-
}
22-
]
23-
}
2+
"domain": {
3+
"name": "Switcher API",
4+
"version": 1,
5+
"activated": true,
6+
"group": [
7+
{
8+
"name": "Test Project",
9+
"activated": true,
10+
"config": [
11+
{
12+
"key": "CLIENT_JS_FEATURE",
13+
"activated": true,
14+
"strategies": [],
15+
"components": [
16+
"switcher-client-js"
17+
]
18+
}
19+
]
20+
}
21+
]
2422
}
2523
}

0 commit comments

Comments
 (0)