Skip to content

Commit 1e3b80a

Browse files
authored
Merge pull request #39 from switcherapi/3.0.0
3.0.0
2 parents ce76ef8 + 18e2235 commit 1e3b80a

File tree

7 files changed

+347
-298
lines changed

7 files changed

+347
-298
lines changed

readme.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ https://github.com/switcherapi/switcher-api
2424
`npm install switcher-client`
2525

2626
## Module initialization
27-
The context properties stores all information regarding connectivity and strategy settings.
27+
The context properties stores all information regarding connectivity.
2828

2929
```js
30-
const Switcher = require("switcher-client");
30+
const Switcher = require('switcher-client');
3131

32-
const apiKey = 'API Key';
33-
const environment = 'default'; // Production = default
32+
const apiKey = '[API_KEY]';
33+
const environment = 'default';
3434
const domain = 'My Domain';
3535
const component = 'MyApp';
3636
const url = 'https://switcher-load-balance.herokuapp.com';
3737
```
3838

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

56-
let switcher = new Switcher(url, apiKey, domain, component, environment, {
57-
offline, logger, snapshotLocation, snapshotAutoload, silentMode, retryAfter
55+
Switcher.buildContext({ url, apiKey, domain, component, environment }, {
56+
offline, logger, snapshotLocation, silentMode, retryAfter
5857
});
58+
59+
let switcher = Switcher.factory();
5960
```
6061

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

68-
## Pre-execution
69-
Before you call the API, there is one single step you need to execute to complete the configuration.
70-
If you are not running the API expecting to use the offline features, you can ignore this step.
71-
72-
After instantiating the Switcher, you need to load the snapshot engine to watch for changes in your Domain structure.
73-
```js
74-
await switcher.loadSnapshot();
75-
```
76-
7768
## Executing
7869
There are a few different ways to call the API using the JavaScript module.
7970
Here are some examples:
@@ -82,7 +73,7 @@ Here are some examples:
8273
Invoking the API can be done by instantiating the switcher and calling *isItOn* passing its key as a parameter.
8374

8475
```js
85-
const switcher = new Switcher(url, apiKey, domain, component, environment);
76+
const switcher = Switcher.factory();
8677
await switcher.isItOn('FEATURE01');
8778
```
8879

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

127+
## Loading Snapshot from the API
128+
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.
129+
130+
```js
131+
Switcher.loadSnapshot();
132+
```
133+
136134
## Snapshot version check
137135
For convenience, an implementation of a domain version checker is available if you have external processes that manage snapshot files.
138136

139137
```js
140-
switcher.checkSnapshot();
138+
Switcher.checkSnapshot();
141139
```

src/index.d.ts

Lines changed: 107 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,128 @@
1+
/**
2+
* Quick start with the following 3 steps.
3+
*
4+
* 1. Use Switcher.buildContext() to define the arguments to connect to the API.
5+
* 2. Use Switcher.factory() to create a new instance of Switcher.
6+
* 3. Use the instance created to call isItOn to query the API.
7+
*/
18
declare class Switcher {
29

310
/**
4-
* @param url Swither-API endpoint
5-
* @param apiKey Switcher-API key generated to your component.
6-
* @param domain Domain name
7-
* @param component Application name
8-
* @param environment Environment name. Production environment is named as 'default'
9-
* @param options offline: boolean - logger: boolean - snapshotLocation: string - snapshotAutoload: string- silentMode: boolean - retryAfter: string;
10-
*/
11-
constructor(
12-
url: string,
13-
apiKey: string,
14-
domain: string,
15-
component: string,
16-
environment: string,
17-
options?: SwitcherOptions);
18-
19-
url: string;
20-
token: string;
21-
apiKey: string;
22-
domain: string;
23-
environment: string;
24-
key: string;
25-
input: string[];
26-
exp: number;
27-
snapshot?: string;
28-
29-
/**
30-
* Validate the input provided to access the API
31-
*/
32-
validate(): Promise<void>;
33-
34-
/**
35-
* Pre-set input values before calling the API
36-
*
37-
* @param key
38-
* @param input
39-
*/
40-
prepare(key: string, input?: string[]): Promise<void>;
41-
42-
/**
43-
* Execute async criteria
11+
* Create the necessary configuration to communicate with the API
4412
*
45-
* @param key
46-
* @param input
47-
* @param showReason
13+
* @param context Necessary arguments
14+
* @param options
4815
*/
49-
isItOn(key?: string, input?: string[], showReason?: boolean): Promise<boolean>;
16+
static buildContext(context: SwitcherContext, options: SwitcherOptions): void;
5017

51-
/**
52-
* Read snapshot file locally and store in a parsed JSON object
53-
*/
54-
loadSnapshot(): Promise<void>;
18+
/**
19+
* Creates a new instance of Switcher
20+
*/
21+
static factory(): Switcher;
5522

56-
/**
57-
* Verifies if the current snapshot file is updated.
58-
* Return true if an update has been made.
59-
*/
60-
checkSnapshot(): Promise<boolean>;
23+
/**
24+
* Read snapshot file locally and store in a parsed JSON object
25+
*/
26+
static loadSnapshot(): Promise<void>;
6127

62-
/**
63-
* Remove snapshot from real-time update
64-
*/
65-
unloadSnapshot(): void;
28+
/**
29+
* Verifies if the current snapshot file is updated.
30+
* Return true if an update has been made.
31+
*/
32+
static checkSnapshot(): Promise<boolean>;
33+
34+
/**
35+
* Remove snapshot from real-time update
36+
*/
37+
static unloadSnapshot(): void;
6638

6739
/**
68-
* Force a switcher value to return a given value by calling one of both methods - true() false()
69-
*
70-
* @param key
71-
*/
72-
static assume(key: string): Key;
40+
* Strategies available to use as Switcher input
41+
*/
42+
static get StrategiesType(): StrategiesType;
43+
44+
/**
45+
* Force a switcher value to return a given value by calling one of both methods - true() false()
46+
*
47+
* @param key
48+
*/
49+
static assume(key: string): Key;
50+
51+
/**
52+
* Remove forced value from a switcher
53+
*
54+
* @param key
55+
*/
56+
static forget(key: string): void;
57+
58+
/**
59+
* Retrieve execution log given a switcher key
60+
*
61+
* @param key
62+
*/
63+
static getLogger(key: string): any[];
7364

7465
/**
75-
* Remove forced value from a switcher
76-
*
77-
* @param key
78-
*/
79-
static forget(key: string): void;
66+
* Enable testing mode
67+
* It prevents from watching Snapshots that may hold process
68+
*/
69+
static setTestEnabled() : void;
70+
71+
/**
72+
* Disable testing mode
73+
*/
74+
static setTestDisabled(): void;
8075

8176
/**
82-
* Retrieve execution log given a switcher key
83-
*
84-
* @param key
85-
*/
86-
static getLogger(key: string): any[];
77+
* Validate the input provided to access the API
78+
*/
79+
validate(): Promise<void>;
8780

88-
/**
89-
* Activate testing mode
90-
* It prevents from watching Snapshots that may hold process
91-
*/
92-
static setTestEnabled() : void;
93-
81+
/**
82+
* Pre-set input values before calling the API
83+
*
84+
* @param key
85+
* @param input
86+
*/
87+
prepare(key: string, input?: string[]): Promise<void>;
88+
89+
/**
90+
* Execute async criteria
91+
*
92+
* @param key
93+
* @param input
94+
* @param showReason
95+
*/
96+
isItOn(key?: string, input?: string[], showReason?: boolean): Promise<boolean>;
97+
98+
}
99+
100+
declare interface SwitcherContext {
101+
url: string;
102+
apiKey: string;
103+
domain: string;
104+
component: string;
105+
environment: string;
106+
token?: string;
107+
exp?: number;
94108
}
95109

96110
declare interface SwitcherOptions {
97-
offline: boolean;
98-
logger: boolean;
99-
snapshotLocation: string;
100-
snapshotAutoload: string;
101-
silentMode: boolean;
102-
retryAfter: string;
111+
offline: boolean;
112+
logger: boolean;
113+
snapshotLocation: string;
114+
snapshotAutoload: string;
115+
silentMode: boolean;
116+
retryAfter: string;
117+
}
118+
119+
declare interface StrategiesType {
120+
NETWORK: string;
121+
VALUE: string;
122+
NUMERIC: string;
123+
TIME: string;
124+
DATE: string;
125+
REGEX: string;
103126
}
104127

105128
export = Switcher;

0 commit comments

Comments
 (0)