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
Expand Up @@ -54,8 +54,8 @@
"pino": "^9.0.0",
"pino-pretty": "^11.0.0",
"swagger-ui-express": "^5.0.0",
"switcher-client": "^4.0.3",
"validator": "^13.11.0"
"switcher-client": "^4.1.0",
"validator": "^13.12.0"
},
"devDependencies": {
"env-cmd": "^10.1.0",
Expand All @@ -64,7 +64,7 @@
"jest-sonar-reporter": "^2.0.0",
"node-notifier": "^10.0.1",
"nodemon": "^3.1.0",
"sinon": "^17.0.1",
"sinon": "^17.0.2",
"supertest": "^7.0.0"
},
"overrides": {
Expand Down
4 changes: 2 additions & 2 deletions src/exceptions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Switcher } from 'switcher-client';
import { Client } from 'switcher-client';
import Logger from '../helpers/logger.js';

export class NotFoundError extends Error {
Expand Down Expand Up @@ -38,7 +38,7 @@ export class FeatureUnavailableError extends Error {

export function responseException(res, err, code, feature = undefined) {
if (feature) {
Logger.info(`Feature [${feature}]`, { log: Switcher.getLogger(feature) });
Logger.info(`Feature [${feature}]`, { log: Client.getLogger(feature) });
}

responseExceptionSilent(res, err, code, err.message);
Expand Down
122 changes: 65 additions & 57 deletions src/external/switcher-api-facade.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Switcher, checkValue, checkPayload, checkRegex } from 'switcher-client';
import { Client } from 'switcher-client';
import { EnvType } from '../models/environment.js';
import { FeatureUnavailableError } from '../exceptions/index.js';
import { getDomainById, getTotalDomainsByOwner } from '../services/domain.js';
Expand All @@ -18,7 +18,7 @@ const throttle = process.env.SWITCHER_API_THROTTLE;
const certPath = process.env.SSL_CERT;
const component = 'switcherapi';

Switcher.buildContext({ url, apiKey, domain: domainName, component, environment }, { logger, certPath });
Client.buildContext({ url, apiKey, domain: domainName, component, environment }, { logger, certPath });

export const SwitcherKeys = Object.freeze({
ELEMENT_CREATION: 'ELEMENT_CREATION',
Expand All @@ -36,42 +36,44 @@ function switcherFlagResult(response, message) {
}
}

async function checkFeature(feature, params) {
const switcher = Switcher.factory();
function getFeatureFlag(feature) {
const switcher = Client.getSwitcher(feature);

if (throttle) {
switcher.throttle(throttle);
}

return switcher.detail().isItOn(feature, params);
return switcher.detail();
}

export async function checkDomain(req) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

const total = await getTotalDomainsByOwner(req.admin._id);
switcherFlagResult(await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const featureFlag = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'domain',
owner: req.admin._id,
total
}))
]), 'Domain limit has been reached.');
})).isItOn();

switcherFlagResult(featureFlag, 'Domain limit has been reached.');
}

export async function checkGroup(domain) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

const total = await getTotalGroupsByDomainId(domain._id);
switcherFlagResult(await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const featureFlag = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'group',
owner: domain.owner,
total
}))
]), 'Group limit has been reached.');
})).isItOn();

switcherFlagResult(featureFlag, 'Group limit has been reached.');
}

export async function checkSwitcher(group) {
Expand All @@ -80,13 +82,14 @@ export async function checkSwitcher(group) {

const total = await getTotalConfigsByDomainId(group.domain);
const { owner } = await getDomainById(group.domain);
switcherFlagResult(await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const featureFlag = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'switcher',
owner,
total
}))
]), 'Switcher limit has been reached.');
})).isItOn();

switcherFlagResult(featureFlag, 'Switcher limit has been reached.');
}

export async function checkComponent(domain) {
Expand All @@ -95,13 +98,14 @@ export async function checkComponent(domain) {

const total = await getTotalComponentsByDomainId(domain);
const { owner } = await getDomainById(domain);
switcherFlagResult(await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const featureFlag = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'component',
owner,
total
}))
]), 'Component limit has been reached.');
})).isItOn();

switcherFlagResult(featureFlag, 'Component limit has been reached.');
}

export async function checkEnvironment(domain) {
Expand All @@ -110,13 +114,14 @@ export async function checkEnvironment(domain) {

const total = await getTotalEnvByDomainId(domain);
const { owner } = await getDomainById(domain);
switcherFlagResult(await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const featureFlag = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'environment',
owner,
total
}))
]), 'Environment limit has been reached.');
})).isItOn();

switcherFlagResult(featureFlag, 'Environment limit has been reached.');
}

export async function checkTeam(domain) {
Expand All @@ -125,26 +130,26 @@ export async function checkTeam(domain) {

const total = await getTotalTeamsByDomainId(domain);
const { owner } = await getDomainById(domain);
switcherFlagResult(await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const featureFlag = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'team',
owner,
total
}))
]), 'Team limit has been reached.');
})).isItOn();

switcherFlagResult(featureFlag, 'Team limit has been reached.');
}

export async function checkMetrics(config) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return true;

const { owner } = await getDomainById(config.domain);
const response = await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
const response = await getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'metrics',
owner
}))
]);
})).isItOn();

if (!response.result) {
if (!config.disable_metrics) {
Expand All @@ -163,61 +168,62 @@ export async function checkHistory(domain) {
return true;

const { owner } = await getDomainById(domain);
return checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
return getFeatureFlag(SwitcherKeys.ELEMENT_CREATION)
.checkPayload(JSON.stringify({
feature: 'history',
owner
}))
]);
})).isItOn();
}

export async function checkAdmin(login) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

switcherFlagResult(
await checkFeature(SwitcherKeys.ACCOUNT_CREATION, [
checkValue(login)
]), 'Account not released to use the API.');
const featureFlag = await getFeatureFlag(SwitcherKeys.ACCOUNT_CREATION)
.checkValue(login)
.isItOn();

switcherFlagResult(featureFlag, 'Account not released to use the API.');
}

export async function checkSlackIntegration(value) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

switcherFlagResult(
await checkFeature(SwitcherKeys.SLACK_INTEGRATION, [
checkValue(value)
]), 'Slack Integration is not available.');
const featureFlag = await getFeatureFlag(SwitcherKeys.SLACK_INTEGRATION)
.checkValue(value)
.isItOn();

switcherFlagResult(featureFlag, 'Slack Integration is not available.');
}

export function notifyAcCreation(adminid) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

const switcher = Switcher.factory();
switcher.isItOn(SwitcherKeys.ACCOUNT_IN_NOTIFY, [
checkValue(adminid)]);
Client.getSwitcher(SwitcherKeys.ACCOUNT_IN_NOTIFY)
.checkValue(adminid)
.isItOn();
}

export function notifyAcDeletion(adminid) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

const switcher = Switcher.factory();
switcher.isItOn(SwitcherKeys.ACCOUNT_OUT_NOTIFY, [
checkValue(adminid)]);
Client.getSwitcher(SwitcherKeys.ACCOUNT_OUT_NOTIFY)
.checkValue(adminid)
.isItOn();
}

export async function getRateLimit(key, component) {
if (process.env.SWITCHER_API_ENABLE === 'true' && key !== process.env.SWITCHER_API_KEY) {
const domain = await getDomainById(component.domain);
const response = await checkFeature(SwitcherKeys.RATE_LIMIT, [
checkValue(String(domain.owner))
]);
const featureFlag = await getFeatureFlag(SwitcherKeys.RATE_LIMIT)
.checkValue(String(domain.owner))
.isItOn();

if (response.result) {
return response.metadata.rate_limit;
if (featureFlag.result) {
return featureFlag.metadata.rate_limit;
}
}

Expand All @@ -228,5 +234,7 @@ export async function checkHttpsAgent(value) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

return checkFeature(SwitcherKeys.HTTPS_AGENT, [checkRegex(value)]);
return getFeatureFlag(SwitcherKeys.HTTPS_AGENT)
.checkRegex(value)
.isItOn();
}
4 changes: 2 additions & 2 deletions tests/slack.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Switcher } from 'switcher-client';
import { Client } from 'switcher-client';
import mongoose from 'mongoose';
import request from 'supertest';
import jwt from 'jsonwebtoken';
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Slack Installation', () => {
test('SLACK_SUITE - Should NOT save installation - Slack unavailable', async () => {
//given
process.env.SWITCHER_API_ENABLE = true;
Switcher.assume('SLACK_INTEGRATION').false();
Client.assume('SLACK_INTEGRATION').false();

//test
const response = await request(app)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit-test/client/relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from 'axios';
import { RelayMethods } from '../../../src/models/config';
import { StrategiesType } from '../../../src/models/config-strategy';
import { EnvType } from '../../../src/models/environment';
import { Switcher } from 'switcher-client';
import { Client } from 'switcher-client';

describe('Testing Client Relay', () => {

Expand All @@ -21,7 +21,7 @@ describe('Testing Client Relay', () => {
// Given
const mockRelayService = { data: { result: true, reason: 'Success' } };
axiosStub.returns(Promise.resolve(mockRelayService));
Switcher.assume('HTTPS_AGENT').true();
Client.assume('HTTPS_AGENT').true();

const relay = {
endpoint: {
Expand Down
Loading