
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
switcher-client
Advanced tools

JavaScript SDK for working with Switcher-API. https://github.com/switcherapi/switcher-api
npm install switcher-client
The context properties stores all information regarding connectivity.
import { Client } from 'switcher-client';
const apiKey = '[API_KEY]';
const environment = 'default';
const domain = 'My Domain';
const component = 'MyApp';
const url = 'https://api.switcherapi.com';
You can also activate features such as local and silent mode:
const local = true;
const freeze = true;
const logger = true;
const snapshotLocation = './snapshot/';
const snapshotAutoUpdateInterval = 3;
const snapshotWatcher = true;
const silentMode = '5m';
const restrictRelay = true;
const certPath = './certs/ca.pem';
Client.buildContext({ url, apiKey, domain, component, environment }, {
local, freeze, logger, snapshotLocation, snapshotAutoUpdateInterval,
snapshotWatcher, silentMode, restrictRelay, certPath
});
const switcher = Client.getSwitcher();
(*) regexSafe is a feature that prevents your application from being exposed to a reDOS attack. It is recommended to keep this feature enabled.
There are a few different ways to call the API. Here are some examples:
const switcher = Client.getSwitcher();
// Local (synchronous) execution
const isOnBool = switcher.isItOn('FEATURE01'); // true or false
const isOnBool = switcher.isItOnBool('FEATURE01'); // true or false
const isOnDetail = switcher.detail().isItOn('FEATURE01'); // { result: true, reason: 'Success', metadata: {} }
const isOnDetail = switcher.isItOnDetail('FEATURE01'); // { result: true, reason: 'Success', metadata: {} }
// Remote (asynchronous) execution or hybrid (local/remote) execution
const isOnBoolAsync = await switcher.isItOn('FEATURE01'); // Promise<boolean>
const isOnBoolAsync = await switcher.isItOnBool('FEATURE01', true); // Promise<boolean>
const isOnDetailAsync = await switcher.detail().isItOn('FEATURE01'); // Promise<SwitcherResult>
const isOnDetailAsync = await switcher.isItOnDetail('FEATURE01', true); // Promise<SwitcherResult>
await switcher.checkValue('USER_1').prepare('FEATURE01');
await switcher.isItOn();
await switcher
.defaultResult(true) // Default result to be returned in case of no API response
.throttle(1000) // Throttle the API call to improve performance
.checkValue('User 1')
.checkNetwork('192.168.0.1')
.isItOn('FEATURE01');
const switcher = Client.getSwitcher();
await switcher
.throttle(1000)
.isItOn('FEATURE01');
In order to capture issues that may occur during the process, it is possible to log the error by subscribing to the error events.
Client.subscribeNotifyError((error) => {
console.log(error);
});
A particular use case is when a swithcer has a Relay Strategy that requires a remote call to resolve the value.
const switcher = Client.getSwitcher();
await switcher.remote().isItOn('FEATURE01');
You can also bypass your switcher configuration with 'Client.assume' API. This is perfect for your test code where you want to validate both scenarios when the switcher is true and false.
Client.assume('FEATURE01').true();
switcher.isItOn('FEATURE01'); // true
Client.forget('FEATURE01');
switcher.isItOn('FEATURE01'); // Now, it's going to return the result retrieved from the API or the Snaopshot file
Client.assume('FEATURE01').false().withMetadata({ message: 'Feature is disabled' }); // Include metadata to emulate Relay response
const response = await switcher.detail().isItOn('FEATURE01'); // false
console.log(response.metadata.message); // Feature is disabled
Client.assume('FEATURE01').true().when(StrategiesType.VALUE, 'USER_1');
switcher.checkValue('USER_1').isItOn('FEATURE01'); // true when the value is 'USER_1'
Client.assume('FEATURE01').true().when(StrategiesType.NETWORK, ['USER_2', 'USER_3']);
switcher.checkValue('USER_1').isItOn('FEATURE01'); // false as the value is not in the list
Enabling Test Mode You may want to enable this feature while using Switcher Client with automated testing. It prevents the Switcher Client from locking snapshot files even after the test execution.
To enable this feature, it is recommended to place the following on your test setup files:
Client.testMode();
Smoke Test Validate Switcher Keys on your testing pipelines before deploying a change. Switcher Keys may not be configured correctly and can cause your code to have undesired results.
This feature will validate using the context provided to check if everything is up and running. In case something is missing, this operation will throw an exception pointing out which Switcher Keys are not configured.
Client.checkSwitchers(['FEATURE01', 'FEATURE02'])
This step is optional if you want to load a copy of the configuration that can be used to eliminate latency when local mode is activated.
Activate watchSnapshot optionally passing true in the arguments.
Auto load Snapshot from API passing true as second argument.
Client.loadSnapshot();
Activate and monitor snapshot changes using this feature. Optionally, you can implement any action based on the callback response.
Client.watchSnapshot({
success: () => console.log('In-memory snapshot updated'),
reject: (err) => console.log(err)
});
Alternatively, you can also use the client context configuration to monitor changes in the snapshot file.
Client.buildContext({ domain, component, environment }, {
local: true,
snapshotLocation: './snapshot/',
snapshotWatcher: true
});
For convenience, an implementation of a domain version checker is available if you have external processes that manage snapshot files.
Client.checkSnapshot();
You can also schedule a snapshot update using the method below.
It allows you to run the Client SDK in local mode (zero latency) and still have the snapshot updated automatically.
Client.scheduleSnapshotAutoUpdate(3000, {
success: (updated) => console.log('Snapshot updated', updated),
reject: (err) => console.log(err)
});
FAQs
Client JS SDK for working with Switcher-API
The npm package switcher-client receives a total of 98 weekly downloads. As such, switcher-client popularity was classified as not popular.
We found that switcher-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.