Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump [email protected] #22

Merged
merged 1 commit into from
May 13, 2024
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"
},
"repository": {
Expand Down
24 changes: 13 additions & 11 deletions src/external/switcher-api-facade.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Switcher, checkValue, checkRegex } from 'switcher-client';
import { Client } from 'switcher-client';
import { getDomainById } from '../services/domain.js';
import { DEFAULT_RATE_LIMIT } from '../middleware/limiter.js';

Expand All @@ -11,32 +11,32 @@ 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({
RATE_LIMIT: 'RATE_LIMIT',
HTTPS_AGENT: 'HTTPS_AGENT'
});

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 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 @@ -47,5 +47,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/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
6 changes: 3 additions & 3 deletions tests/unit-test/switcher-api-facade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
component1,
component1Key
} from '../fixtures/db_api';
import { Switcher } from 'switcher-client';
import { Client } from 'switcher-client';

import '../../src/db/mongoose';

Expand All @@ -27,7 +27,7 @@ describe('Testing Switcher API Facade', () => {

test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute', async () => {
const call = async () => {
Switcher.assume(SwitcherKeys.RATE_LIMIT).true().withMetadata({ rate_limit: 100 });
Client.assume(SwitcherKeys.RATE_LIMIT).true().withMetadata({ rate_limit: 100 });
return getRateLimit(component1Key, component1);
};

Expand All @@ -36,7 +36,7 @@ describe('Testing Switcher API Facade', () => {

test('UNIT_API_FACADE - Should NOT read rate limit - Default Request Per Minute', async () => {
const call = async () => {
Switcher.assume(SwitcherKeys.RATE_LIMIT).false();
Client.assume(SwitcherKeys.RATE_LIMIT).false();
return getRateLimit(component1Key, component1);
};

Expand Down