From 00165eff13f30b29812d9895f21e102f286ece66 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 24 Jan 2021 17:02:07 -0800 Subject: [PATCH 1/2] Refactored project structure --- package.json | 2 +- src/index.d.ts | 63 ++++++++++++---------- src/index.js | 2 +- src/lib/bypasser.d.ts | 25 --------- src/lib/{bypasser.js => bypasser/index.js} | 15 ++++++ src/lib/{ => bypasser}/key.js | 16 ++++++ src/lib/datemoment.d.ts | 40 -------------- src/lib/executionLogger.d.ts | 19 ------- src/lib/key.d.ts | 32 ----------- src/lib/services.js | 2 +- src/lib/snapshot.js | 2 +- src/lib/{ => utils}/datemoment.js | 22 ++++++++ src/lib/{ => utils}/executionLogger.js | 11 ++++ test/datemoment.test.js | 2 +- 14 files changed, 104 insertions(+), 149 deletions(-) delete mode 100644 src/lib/bypasser.d.ts rename src/lib/{bypasser.js => bypasser/index.js} (71%) rename src/lib/{ => bypasser}/key.js (55%) delete mode 100644 src/lib/datemoment.d.ts delete mode 100644 src/lib/executionLogger.d.ts delete mode 100644 src/lib/key.d.ts rename src/lib/{ => utils}/datemoment.js (73%) rename src/lib/{ => utils}/executionLogger.js (72%) diff --git a/package.json b/package.json index 741c952..dcd2f60 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "switcher-client", "version": "2.1.0", - "description": "Module for working with Switcher-API", + "description": "SDK for working with Switcher-API", "main": "./src/index.js", "types": "./src/index.d.ts", "author": { diff --git a/src/index.d.ts b/src/index.d.ts index 9a45ee2..af8a52a 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,3 +1,5 @@ +import Key from "./lib/key"; + /** * Quick start with the following 3 steps. * @@ -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 @@ -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() @@ -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; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 4510caa..3bced71 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); diff --git a/src/lib/bypasser.d.ts b/src/lib/bypasser.d.ts deleted file mode 100644 index d4ad57b..0000000 --- a/src/lib/bypasser.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -declare class Bypasser { - - /** - * 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; - - /** - * Search for key registered via 'assume' - * - * @param key - */ - static searchBypassed(key: string): Key; -} - -export = Bypasser; \ No newline at end of file diff --git a/src/lib/bypasser.js b/src/lib/bypasser/index.js similarity index 71% rename from src/lib/bypasser.js rename to src/lib/bypasser/index.js index f96b28b..fbcb500 100644 --- a/src/lib/bypasser.js +++ b/src/lib/bypasser/index.js @@ -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) { @@ -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 => { diff --git a/src/lib/key.js b/src/lib/bypasser/key.js similarity index 55% rename from src/lib/key.js rename to src/lib/bypasser/key.js index f3b335d..5c7d875 100644 --- a/src/lib/key.js +++ b/src/lib/bypasser/key.js @@ -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; } diff --git a/src/lib/datemoment.d.ts b/src/lib/datemoment.d.ts deleted file mode 100644 index 5dc6b20..0000000 --- a/src/lib/datemoment.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -declare class DateMoment { - - constructor(date: Date, time: string); - - date: Date; - time: string; - - /** - * Updates current date with the given time. - * Use hh:mm format - */ - setTime(time: string): void - - /** - * Current date configured - */ - getDate(): Date - - /** - * It verifies if the configured date is before the given date/time - */ - isSameOrBefore(date: Date, time: string): boolean - - /** - * It verifies if the configured date is after the given date/time - */ - isSameOrAfter(date: Date, time: string): boolean - - /** - * It verifies if the configured date is in between the given date/time (A/B) - */ - isBetween(dateA: Date, dateB: Date, timeA: string, timeB: string): boolean - - /** - * Add time to the configured date based on the unit - */ - add(amount: string | number, unit: string): DateMoment -} - -export = DateMoment; \ No newline at end of file diff --git a/src/lib/executionLogger.d.ts b/src/lib/executionLogger.d.ts deleted file mode 100644 index f41c5fe..0000000 --- a/src/lib/executionLogger.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -declare class ExecutionLogger { - - /** - * Add new execution result - * - * @param key - * @param response - */ - static add(key: string, reasponse: any): void; - - /** - * Retrieve results given a switcher key - * - * @param key - */ - static getByKey(key: string): any[]; -} - -export = ExecutionLogger; \ No newline at end of file diff --git a/src/lib/key.d.ts b/src/lib/key.d.ts deleted file mode 100644 index f17f47f..0000000 --- a/src/lib/key.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -declare class Key { - - /** - * @param key Switcher name - */ - constructor(key: string); - - key: string; - valaue: boolean; - - /** - * Force result to true - */ - true(): void; - - /** - * Force result to false - */ - false(): void; - - /** - * Return selected switcher name - */ - getKey(): string; - - /** - * Return current value - */ - getValue(): boolean; -} - -export = Key; \ No newline at end of file diff --git a/src/lib/services.js b/src/lib/services.js index b8b8710..6799918 100644 --- a/src/lib/services.js +++ b/src/lib/services.js @@ -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}`; diff --git a/src/lib/snapshot.js b/src/lib/snapshot.js index 5a9d7ce..04617e5 100644 --- a/src/lib/snapshot.js +++ b/src/lib/snapshot.js @@ -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) => { diff --git a/src/lib/datemoment.js b/src/lib/utils/datemoment.js similarity index 73% rename from src/lib/datemoment.js rename to src/lib/utils/datemoment.js index 3f90236..72d9be3 100644 --- a/src/lib/datemoment.js +++ b/src/lib/utils/datemoment.js @@ -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(':'); @@ -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': diff --git a/src/lib/executionLogger.js b/src/lib/utils/executionLogger.js similarity index 72% rename from src/lib/executionLogger.js rename to src/lib/utils/executionLogger.js index 786ec3e..d19b243 100644 --- a/src/lib/executionLogger.js +++ b/src/lib/utils/executionLogger.js @@ -4,6 +4,12 @@ var logger = new Array(); class ExecutionLogger { + /** + * Add new execution result + * + * @param key + * @param response + */ static add(key, reasponse) { let keyIndex = undefined; logger.forEach((value, index) => @@ -14,6 +20,11 @@ class ExecutionLogger { logger.push({ key, reasponse }); } + /** + * Retrieve results given a switcher key + * + * @param key + */ static getByKey(key) { return logger.filter(value => value.key === key); } diff --git a/test/datemoment.test.js b/test/datemoment.test.js index 154d212..26f11f4 100644 --- a/test/datemoment.test.js +++ b/test/datemoment.test.js @@ -1,5 +1,5 @@ const assert = require('chai').assert; -const DateMoment = require('../src/lib/datemoment'); +const DateMoment = require('../src/lib/utils/datemoment'); describe('Manipulate date and time', () => { From 0f14a81656b20bdfaea414ec2706c5e29c257a16 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 24 Jan 2021 17:03:53 -0800 Subject: [PATCH 2/2] Fixed conflict --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dcd2f60..5533523 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "switcher-client", - "version": "2.1.0", + "version": "2.1.1", "description": "SDK for working with Switcher-API", "main": "./src/index.js", "types": "./src/index.d.ts",