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
38 changes: 18 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ https://github.com/switcherapi/switcher-api
`npm install switcher-client`

## Module initialization
The context properties stores all information regarding connectivity and strategy settings.
The context properties stores all information regarding connectivity.

```js
const Switcher = require("switcher-client");
const Switcher = require('switcher-client');

const apiKey = 'API Key';
const environment = 'default'; // Production = default
const apiKey = '[API_KEY]';
const environment = 'default';
const domain = 'My Domain';
const component = 'MyApp';
const url = 'https://switcher-load-balance.herokuapp.com';
```

- **apiKey**: Switcher-API key generated to your component.
- **environment**: Environment name. Production environment is named as 'default'.
- **environment**: (optional) Environment name. Production environment is named as 'default'.
- **domain**: Domain name.
- **component**: Application name.
- **url**: Swither-API endpoint.
Expand All @@ -48,32 +48,23 @@ You can also activate features such as offline and silent mode:
```js
const offline = true;
const logger = true;
const snapshotAutoload = true;
const snapshotLocation = './snapshot/';
const silentMode = true;
const retryAfter = '5m';

let switcher = new Switcher(url, apiKey, domain, component, environment, {
offline, logger, snapshotLocation, snapshotAutoload, silentMode, retryAfter
Switcher.buildContext({ url, apiKey, domain, component, environment }, {
offline, logger, snapshotLocation, silentMode, retryAfter
});

let switcher = Switcher.factory();
```

- **offline**: If activated, the client will only fetch the configuration inside your snapshot file. The default value is 'false'.
- **logger**: If activated, it is possible to retrieve the last results from a given Switcher key using Switcher.getLogger('KEY')
- **snapshotLocation**: Location of snapshot files. The default value is './snapshot/'.
- **snapshotAutload**: If activated, snapshot folder and files are going to be created automatically.
- **silentMode**: If activated, all connectivity issues will be ignored and the client will automatically fetch the configuration into your snapshot file.
- **retryAfter** : Time given to the module to re-establish connectivity with the API - e.g. 5s (s: seconds - m: minutes - h: hours).

## Pre-execution
Before you call the API, there is one single step you need to execute to complete the configuration.
If you are not running the API expecting to use the offline features, you can ignore this step.

After instantiating the Switcher, you need to load the snapshot engine to watch for changes in your Domain structure.
```js
await switcher.loadSnapshot();
```

## Executing
There are a few different ways to call the API using the JavaScript module.
Here are some examples:
Expand All @@ -82,7 +73,7 @@ Here are some examples:
Invoking the API can be done by instantiating the switcher and calling *isItOn* passing its key as a parameter.

```js
const switcher = new Switcher(url, apiKey, domain, component, environment);
const switcher = Switcher.factory();
await switcher.isItOn('FEATURE01');
```

Expand Down Expand Up @@ -133,9 +124,16 @@ To enable this feature, it is recommended to place the following on your test se
Switcher.setTestEnabled();
```

## Loading Snapshot from the API
This step is optional if you want to load a copy of the configuration that can be used to eliminate latency when offline mode is activated.

```js
Switcher.loadSnapshot();
```

## Snapshot version check
For convenience, an implementation of a domain version checker is available if you have external processes that manage snapshot files.

```js
switcher.checkSnapshot();
Switcher.checkSnapshot();
```
191 changes: 107 additions & 84 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,128 @@
/**
* Quick start with the following 3 steps.
*
* 1. Use Switcher.buildContext() to define the arguments to connect to the API.
* 2. Use Switcher.factory() to create a new instance of Switcher.
* 3. Use the instance created to call isItOn to query the API.
*/
declare class Switcher {

/**
* @param url Swither-API endpoint
* @param apiKey Switcher-API key generated to your component.
* @param domain Domain name
* @param component Application name
* @param environment Environment name. Production environment is named as 'default'
* @param options offline: boolean - logger: boolean - snapshotLocation: string - snapshotAutoload: string- silentMode: boolean - retryAfter: string;
*/
constructor(
url: string,
apiKey: string,
domain: string,
component: string,
environment: string,
options?: SwitcherOptions);

url: string;
token: string;
apiKey: string;
domain: string;
environment: string;
key: string;
input: string[];
exp: number;
snapshot?: string;

/**
* Validate the input provided to access the API
*/
validate(): Promise<void>;

/**
* Pre-set input values before calling the API
*
* @param key
* @param input
*/
prepare(key: string, input?: string[]): Promise<void>;

/**
* Execute async criteria
* Create the necessary configuration to communicate with the API
*
* @param key
* @param input
* @param showReason
* @param context Necessary arguments
* @param options
*/
isItOn(key?: string, input?: string[], showReason?: boolean): Promise<boolean>;
static buildContext(context: SwitcherContext, options: SwitcherOptions): void;

/**
* Read snapshot file locally and store in a parsed JSON object
*/
loadSnapshot(): Promise<void>;
/**
* Creates a new instance of Switcher
*/
static factory(): Switcher;

/**
* Verifies if the current snapshot file is updated.
* Return true if an update has been made.
*/
checkSnapshot(): Promise<boolean>;
/**
* Read snapshot file locally and store in a parsed JSON object
*/
static loadSnapshot(): Promise<void>;

/**
* Remove snapshot from real-time update
*/
unloadSnapshot(): void;
/**
* Verifies if the current snapshot file is updated.
* Return true if an update has been made.
*/
static checkSnapshot(): Promise<boolean>;

/**
* Remove snapshot from real-time update
*/
static unloadSnapshot(): void;

/**
* Force a switcher value to return a given value by calling one of both methods - true() false()
*
* @param key
*/
static assume(key: string): Key;
* Strategies available to use as Switcher input
*/
static get StrategiesType(): StrategiesType;

/**
* Force a switcher value to return a given value by calling one of both methods - true() false()
*
* @param key
*/
static assume(key: string): Key;

/**
* Remove forced value from a switcher
*
* @param key
*/
static forget(key: string): void;

/**
* Retrieve execution log given a switcher key
*
* @param key
*/
static getLogger(key: string): any[];

/**
* Remove forced value from a switcher
*
* @param key
*/
static forget(key: string): void;
* Enable testing mode
* It prevents from watching Snapshots that may hold process
*/
static setTestEnabled() : void;

/**
* Disable testing mode
*/
static setTestDisabled(): void;

/**
* Retrieve execution log given a switcher key
*
* @param key
*/
static getLogger(key: string): any[];
* Validate the input provided to access the API
*/
validate(): Promise<void>;

/**
* Activate testing mode
* It prevents from watching Snapshots that may hold process
*/
static setTestEnabled() : void;

/**
* Pre-set input values before calling the API
*
* @param key
* @param input
*/
prepare(key: string, input?: string[]): Promise<void>;

/**
* Execute async criteria
*
* @param key
* @param input
* @param showReason
*/
isItOn(key?: string, input?: string[], showReason?: boolean): Promise<boolean>;

}

declare interface SwitcherContext {
url: string;
apiKey: string;
domain: string;
component: string;
environment: string;
token?: string;
exp?: number;
}

declare interface SwitcherOptions {
offline: boolean;
logger: boolean;
snapshotLocation: string;
snapshotAutoload: string;
silentMode: boolean;
retryAfter: string;
offline: boolean;
logger: boolean;
snapshotLocation: string;
snapshotAutoload: string;
silentMode: boolean;
retryAfter: string;
}

declare interface StrategiesType {
NETWORK: string;
VALUE: string;
NUMERIC: string;
TIME: string;
DATE: string;
REGEX: string;
}

export = Switcher;
Loading