Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "switcher-client",
"version": "4.4.2",
"version": "4.5.0",
"description": "Client JS SDK for working with Switcher-API",
"main": "./switcher-client.js",
"type": "module",
Expand Down Expand Up @@ -32,8 +32,8 @@
],
"devDependencies": {
"@babel/eslint-parser": "^7.28.4",
"@typescript-eslint/eslint-plugin": "^8.45.0",
"@typescript-eslint/parser": "^8.45.0",
"@typescript-eslint/eslint-plugin": "^8.46.0",
"@typescript-eslint/parser": "^8.46.0",
"c8": "^10.1.3",
"chai": "^6.2.0",
"env-cmd": "^11.0.0",
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.projectKey=switcherapi_switcher-client-master
sonar.projectName=switcher-client-js
sonar.organization=switcherapi
sonar.projectVersion=4.4.2
sonar.projectVersion=4.5.0
sonar.links.homepage=https://github.com/switcherapi/switcher-client-js

sonar.javascript.lcov.reportPaths=coverage/lcov.info
Expand Down
8 changes: 4 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Client {

const snapshot = await validateSnapshot(
Client.#context,
GlobalSnapshot.snapshot.data.domain.version
GlobalSnapshot.snapshot.domain.version
);

if (snapshot) {
Expand Down Expand Up @@ -149,7 +149,7 @@ export class Client {
Client.watchSnapshot();
}

return GlobalSnapshot.snapshot?.data.domain.version || 0;
return GlobalSnapshot.snapshot?.domain.version || 0;
}

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

static watchSnapshot(callback = {}) {
Expand Down Expand Up @@ -256,7 +256,7 @@ export class Client {
}

static get snapshotVersion() {
return GlobalSnapshot.snapshot?.data.domain.version || 0;
return GlobalSnapshot.snapshot?.domain.version || 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export async function resolveSnapshot(domain, environment, component) {
});

if (response.status == 200) {
return JSON.stringify(response.json(), null, 4);
return JSON.stringify(response.json().data, null, 4);
}

throw new RemoteError(`[resolveSnapshot] failed with status ${response.status}`);
Expand Down
16 changes: 8 additions & 8 deletions src/lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import * as util from '../lib/utils/index.js';
import { SwitcherResult } from './result.js';

/**
* Resolves the criteria for a given switcher request against the snapshot data.
* Resolves the criteria for a given switcher request against the snapshot domain.
*
* @param {SnapshotData} data - The snapshot data containing domain and group information.
* @param {Domain} domain - The domain containing groups and configurations.
* @param {SwitcherRequest} switcher - The switcher request to be evaluated.
* @returns {SwitcherResult} - The result of the switcher evaluation.
*/
function resolveCriteria(data, switcher) {
if (!data.domain.activated) {
function resolveCriteria(domain, switcher) {
if (!domain.activated) {
return SwitcherResult.disabled('Domain disabled');
}

const { group } = data.domain;
const { group } = domain;
return checkGroup(group, switcher);
}

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

const { data } = snapshot;
return resolveCriteria(data, switcher);
const { domain } = snapshot;
return resolveCriteria(domain, switcher);
}
4 changes: 2 additions & 2 deletions src/lib/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const loadDomain = (snapshotLocation, environment) => {
if (existsSync(snapshotFile)) {
dataBuffer = readFileSync(snapshotFile);
} else {
dataBuffer = JSON.stringify({ data: { domain: { version: 0 } } }, null, 4);
dataBuffer = JSON.stringify({ domain: { version: 0 } }, null, 4);

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

const checkSwitchersLocal = (snapshot, switcherKeys) => {
const { group } = snapshot.data.domain;
const { group } = snapshot.domain;
let notFound = [], found = false;

for (const switcher of switcherKeys) {
Expand Down
2 changes: 1 addition & 1 deletion tests/playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const _testSnapshotAutoUpdate = async () => {
const time = Date.now();
await switcher.checkValue('user_1').isItOn(SWITCHER_KEY);
console.clear();
console.log(Client.getLogger(SWITCHER_KEY), `executed in ${Date.now() - time}ms`);
console.log(JSON.stringify(Client.getLogger(SWITCHER_KEY)), `executed in ${Date.now() - time}ms`);
}, 2000);
};

Expand Down
60 changes: 29 additions & 31 deletions tests/playground/snapshot/default.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
{
"data": {
"domain": {
"name": "Switcher API",
"version": 1,
"activated": true,
"group": [
{
"name": "Test Project",
"activated": true,
"config": [
{
"key": "CLIENT_JS_FEATURE",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"switcher-client-js"
]
}
]
}
]
}
"domain": {
"name": "Switcher API",
"version": 1,
"activated": true,
"group": [
{
"name": "Test Project",
"activated": true,
"config": [
{
"key": "CLIENT_JS_FEATURE",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"switcher-client-js"
]
}
]
}
]
}
}
42 changes: 20 additions & 22 deletions tests/playground/snapshot/local.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
{
"data": {
"domain": {
"name": "Switcher API",
"version": 1,
"activated": true,
"group": [
{
"name": "Test Project",
"activated": true,
"config": [
{
"key": "CLIENT_JS_FEATURE",
"activated": true,
"strategies": [],
"components": [
"switcher-client-js"
]
}
]
}
]
}
"domain": {
"name": "Switcher API",
"version": 1,
"activated": true,
"group": [
{
"name": "Test Project",
"activated": true,
"config": [
{
"key": "CLIENT_JS_FEATURE",
"activated": true,
"strategies": [],
"components": [
"switcher-client-js"
]
}
]
}
]
}
}
Loading