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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "switcher-client",
"version": "2.1.1",
"description": "Module for working with Switcher-API",
"description": "SDK for working with Switcher-API",
"main": "./src/index.js",
"types": "./src/index.d.ts",
"author": {
Expand Down
63 changes: 35 additions & 28 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Key from "./lib/key";

/**
* Quick start with the following 3 steps.
*
Expand All @@ -7,13 +9,15 @@
*/
declare class Switcher {

constructor();

/**
* Create the necessary configuration to communicate with the API
*
* @param context Necessary arguments
* @param options
*/
static buildContext(context: SwitcherContext, options: SwitcherOptions): void;
static buildContext(context: Switcher.SwitcherContext, options: Switcher.SwitcherOptions): void;

/**
* Creates a new instance of Switcher
Expand All @@ -39,7 +43,7 @@ declare class Switcher {
/**
* Strategies available to use as Switcher input
*/
static get StrategiesType(): StrategiesType;
static get StrategiesType(): Switcher.StrategiesType;

/**
* Force a switcher value to return a given value by calling one of both methods - true() false()
Expand Down Expand Up @@ -97,32 +101,35 @@ declare class Switcher {

}

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;
}

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

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

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

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

export = Switcher;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Bypasser = require('./lib/bypasser');
const ExecutionLogger = require('./lib/executionLogger');
const ExecutionLogger = require('./lib/utils/executionLogger');
const { loadDomain, validateSnapshot, StrategiesType } = require('./lib/snapshot');
const services = require('./lib/services');
const checkCriteriaOffline = require('./lib/resolver');
Expand Down
25 changes: 0 additions & 25 deletions src/lib/bypasser.d.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/lib/bypasser.js → src/lib/bypasser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ var bypassedKeys = new Array();

class Bypasser {

/**
* Force a switcher value to return a given value by calling one of both methods - true() false()
*
* @param key
*/
static assume(key) {
const existentKey = this.searchBypassed(key, bypassedKeys);
if (existentKey) {
Expand All @@ -16,11 +21,21 @@ class Bypasser {
return keyBypassed;
}

/**
* Remove forced value from a switcher
*
* @param key
*/
static forget(key) {
bypassedKeys.splice(
bypassedKeys.indexOf(this.searchBypassed(key, bypassedKeys)), 1);
}

/**
* Search for key registered via 'assume'
*
* @param key
*/
static searchBypassed(key) {
let existentKey;
bypassedKeys.forEach(async bk => {
Expand Down
16 changes: 16 additions & 0 deletions src/lib/key.js → src/lib/bypasser/key.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
'use strict';

/**
* Type definition for Switcher Keys which are used to mock results
*/
class Key {

constructor(key) {
this.key = key;
this.value = undefined;
}

/**
* Force result to true
*/
true() {
this.value = true;
}

/**
* Force result to false
*/
false() {
this.value = false;
}

/**
* Return selected switcher name
*/
getKey() {
return this.key;
}

/**
* Return current value
*/
getValue() {
return this.value;
}
Expand Down
40 changes: 0 additions & 40 deletions src/lib/datemoment.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/lib/executionLogger.d.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/lib/key.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/services.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require('axios');
const DateMoment = require('./datemoment');
const DateMoment = require('./utils/datemoment');

const getConnectivityError = (code) => `Connection has been refused - ${code}`;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/snapshot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
const IPCIDR = require('ip-cidr');
const DateMoment = require('./datemoment');
const DateMoment = require('./utils/datemoment');
const { resolveSnapshot, checkSnapshotVersion } = require('./services');

const loadDomain = (snapshotLocation, environment) => {
Expand Down
22 changes: 22 additions & 0 deletions src/lib/datemoment.js → src/lib/utils/datemoment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class DateMoment {
this.setTime(time);
}

/**
* Updates current date with the given time.
* Use hh:mm format
*/
setTime(time) {
if (time) {
const timeArr = time.split(':');
Expand All @@ -14,25 +18,43 @@ class DateMoment {
}
}

/**
* Current date configured
*/
getDate() {
return this.date;
}

/**
* It verifies if the configured date is before the given date/time
*/
isSameOrBefore(date, time) {
return this.date.getTime() <=
new DateMoment(date, time || undefined).getDate().getTime();
}

/**
* It verifies if the configured date is after the given date/time
*/
isSameOrAfter(date, time) {
return this.date.getTime() >=
new DateMoment(date, time || undefined).getDate().getTime();
}

/**
* It verifies if the configured date is in between the given date/time (A/B)
*/
isBetween(dateA, dateB, timeA, timeB) {
return this.isSameOrAfter(dateA, timeA || undefined) &&
this.isSameOrBefore(dateB, timeB || undefined);
}

/**
* Add time to the configured date based on the unit
*
* @param {*} amount
* @param {*} unit
*/
add(amount, unit) {
switch (unit.toLowerCase()) {
case 's':
Expand Down
Loading