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
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@switcherapi/switcher-client-deno",
"version": "2.3.2",
"version": "2.4.0",
"description": "Switcher4Deno is a Feature Flag Deno Client SDK for Switcher API",
"tasks": {
"cache-reload": "deno cache --reload --lock=deno.lock mod.ts",
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-deno
sonar.projectName=switcher-client-deno
sonar.organization=switcherapi
sonar.projectVersion=2.3.2
sonar.projectVersion=2.4.0

sonar.javascript.lcov.reportPaths=coverage/report.lcov

Expand Down
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class Client {
util.get(Client._context.domain, ''),
util.get(Client._context.environment, DEFAULT_ENVIRONMENT),
util.get(Client._context.component, ''),
GlobalSnapshot.snapshot.data.domain.version,
GlobalSnapshot.snapshot.domain.version,
);

if (snapshot) {
Expand Down Expand Up @@ -204,7 +204,7 @@ export class Client {
* - GlobalOptions.local is false, meaning it will not use the local snapshot.
*/
private static _isCheckSnapshotAvailable(fetchRemote: boolean): boolean {
return GlobalSnapshot.snapshot?.data.domain.version == 0 && (fetchRemote || !GlobalOptions.local);
return GlobalSnapshot.snapshot?.domain.version == 0 && (fetchRemote || !GlobalOptions.local);
}

/**
Expand Down Expand Up @@ -365,6 +365,6 @@ export class Client {
* Returns the current snapshot version.
*/
static get snapshotVersion(): number {
return GlobalSnapshot.snapshot?.data.domain.version || 0;
return GlobalSnapshot.snapshot?.domain.version || 0;
}
}
10 changes: 8 additions & 2 deletions src/lib/remote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CheckSwitcherError, ClientError, RemoteError } from './exceptions/index.ts';
import type { AuthResponse, CheckSnapshotVersionResponse, Entry, SwitcherContext } from '../types/index.d.ts';
import type { AuthResponse, CheckSnapshotVersionResponse, Domain, Entry, SwitcherContext } from '../types/index.d.ts';
import type { SwitcherResult } from './result.ts';
import * as util from './utils/index.ts';
import { GlobalAuth } from './globals/globalAuth.ts';
Expand Down Expand Up @@ -147,6 +147,12 @@ export const resolveSnapshot = async (
environment: string,
component: string,
) => {
type GraphQLResponse = {
data: {
domain: Domain;
};
};

const data = {
query: `
query domain {
Expand All @@ -172,7 +178,7 @@ export const resolveSnapshot = async (
});

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

throw new RemoteError(`[resolveSnapshot] failed with status ${response.status}`);
Expand Down
18 changes: 9 additions & 9 deletions src/lib/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { processOperation } from './snapshot.ts';
import { getEntry } from '../lib/remote.ts';
import * as util from '../lib/utils/index.ts';
import type { Config, Entry, Group, Snapshot, SnapshotData, Strategy } from '../types/index.d.ts';
import type { Config, Domain, Entry, Group, Snapshot, Strategy } from '../types/index.d.ts';
import type { SwitcherRequest } from '../switcherRequest.ts';
import { SwitcherResult } from './result.ts';

/**
* 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: SnapshotData,
domain: Domain,
switcher: SwitcherRequest,
): SwitcherResult {
if (!data.domain.activated) {
if (!domain.activated) {
return SwitcherResult.disabled('Domain disabled');
}

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

Expand Down Expand Up @@ -138,7 +138,7 @@ function isStrategyFulfilled(strategyEntry: Entry[], strategyConfig: Strategy) {
/**
* 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 @@ -153,6 +153,6 @@ export default function checkCriteriaLocal(
);
}

const { data } = snapshot;
return resolveCriteria(data, switcher);
const { domain } = snapshot;
return resolveCriteria(domain, switcher);
}
4 changes: 2 additions & 2 deletions src/lib/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const loadDomain = (snapshotLocation: string, environment: string) => {
dataBuffer = Deno.readTextFileSync(snapshotFile);
} else {
dataBuffer = JSON.stringify(
{ data: { domain: { version: 0 } } },
{ domain: { version: 0 } },
null,
4,
);
Expand Down Expand Up @@ -111,7 +111,7 @@ export const validateSnapshot = async (
};

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

Expand Down
4 changes: 0 additions & 4 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ export type CheckSnapshotVersionResponse = {
// Switcher API domain types

export type Snapshot = {
data: SnapshotData;
};

export type SnapshotData = {
domain: Domain;
};

Expand Down
2 changes: 1 addition & 1 deletion tests/playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const _testSnapshotAutoUpdate = async () => {
const time = Date.now();
await switcher.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_DENO_FEATURE",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"switcher4deno"
]
}
]
}
]
}
"domain": {
"name": "Switcher API",
"version": 1,
"activated": true,
"group": [
{
"name": "Test Project",
"activated": true,
"config": [
{
"key": "CLIENT_DENO_FEATURE",
"activated": true,
"strategies": [
{
"strategy": "VALUE_VALIDATION",
"activated": false,
"operation": "EXIST",
"values": [
"user_1"
]
}
],
"components": [
"switcher4deno"
]
}
]
}
]
}
}
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_DENO_FEATURE",
"activated": true,
"strategies": [],
"components": [
"switcher4deno"
]
}
]
}
]
}
"domain": {
"name": "Switcher API",
"version": 1,
"activated": true,
"group": [
{
"name": "Test Project",
"activated": true,
"config": [
{
"key": "CLIENT_DENO_FEATURE",
"activated": true,
"strategies": [],
"components": [
"switcher4deno"
]
}
]
}
]
}
}
Loading