diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index ea2cb54..a9535fe 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -18,10 +18,10 @@ jobs: with: fetch-depth: 0 - - name: Setup Deno v1.42.2 + - name: Setup Deno v1.43.1 uses: denoland/setup-deno@v1 with: - deno-version: v1.42.2 + deno-version: v1.43.1 - name: Setup LCOV run: sudo apt install -y lcov diff --git a/deno.lock b/deno.lock index e6c0e02..58f1c18 100644 --- a/deno.lock +++ b/deno.lock @@ -3,7 +3,7 @@ "packages": { "specifiers": { "jsr:@std/assert@^0.224.0": "jsr:@std/assert@0.224.0", - "jsr:@std/fs": "jsr:@std/fs@0.224.0", + "jsr:@std/fs@^0.224.0": "jsr:@std/fs@0.224.0", "jsr:@std/path@^0.224.0": "jsr:@std/path@0.224.0" }, "jsr": { diff --git a/src/lib/snapshot.ts b/src/lib/snapshot.ts index f4147b7..3de06b3 100644 --- a/src/lib/snapshot.ts +++ b/src/lib/snapshot.ts @@ -1,5 +1,5 @@ // deno-lint-ignore-file -import { existsSync } from 'jsr:@std/fs'; +import { existsSync } from '@std/fs'; import DateMoment from './utils/datemoment.ts'; import IPCIDR from './utils/ipcidr.ts'; @@ -7,7 +7,7 @@ import TimedMatch from './utils/timed-match/index.ts'; import { parseJSON, payloadReader } from './utils/payloadReader.ts'; import { CheckSwitcherError } from './exceptions/index.ts'; import { checkSnapshotVersion, resolveSnapshot } from './remote.ts'; -import type { Snapshot, SwitcherContext } from '../types/index.d.ts'; +import type { Snapshot } from '../types/index.d.ts'; export const loadDomain = (snapshotLocation: string, environment: string) => { let dataJSON; @@ -40,25 +40,20 @@ export const loadDomain = (snapshotLocation: string, environment: string) => { }; export const validateSnapshot = async ( - context: SwitcherContext, + url: string, + token: string, + domain: string, + environment: string, + component: string, snapshotVersion: number, ) => { - const { status } = await checkSnapshotVersion( - context.url || '', - context.token || '', - snapshotVersion, - ); + const { status } = await checkSnapshotVersion(url, token, snapshotVersion); if (!status) { - const snapshot = await resolveSnapshot( - context.url || '', - context.token || '', - context.domain, - context.environment, - context.component || '', - ); + const snapshot = await resolveSnapshot(url, token, domain, environment, component); return snapshot; } + return undefined; }; diff --git a/src/switcher-client.ts b/src/switcher-client.ts index 82dd439..f681b73 100644 --- a/src/switcher-client.ts +++ b/src/switcher-client.ts @@ -56,14 +56,14 @@ export class Switcher { this._snapshot = undefined; this._context = context; this._context.url = context.url; - this._context.environment = context.environment || DEFAULT_ENVIRONMENT; + this._context.environment = Switcher._get(context.environment, DEFAULT_ENVIRONMENT); // Default values this._options = { snapshotAutoUpdateInterval: 0, snapshotLocation: options?.snapshotLocation, - local: options?.local != undefined ? options.local : DEFAULT_LOCAL, - logger: options?.logger != undefined ? options.logger : DEFAULT_LOGGER, + local: Switcher._get(options?.local, DEFAULT_LOCAL), + logger: Switcher._get(options?.logger, DEFAULT_LOGGER), }; if (options) { @@ -112,7 +112,11 @@ export class Switcher { } const snapshot = await validateSnapshot( - Switcher._context, + Switcher._get(Switcher._context.url, ''), + Switcher._get(Switcher._context.token, ''), + Switcher._get(Switcher._context.domain, ''), + Switcher._get(Switcher._context.environment, DEFAULT_ENVIRONMENT), + Switcher._get(Switcher._context.component, ''), Switcher._snapshot.data.domain.version, ); @@ -141,8 +145,8 @@ export class Switcher { fetchRemote = false, ): Promise { Switcher._snapshot = loadDomain( - Switcher._options.snapshotLocation || '', - Switcher._context.environment, + Switcher._get(Switcher._options.snapshotLocation, ''), + Switcher._get(Switcher._context.environment, DEFAULT_ENVIRONMENT), ); if ( @@ -274,8 +278,8 @@ export class Switcher { if (event.kind === 'modify') { try { Switcher._snapshot = loadDomain( - Switcher._options.snapshotLocation || '', - Switcher._context.environment, + Switcher._get(Switcher._options.snapshotLocation, ''), + Switcher._get(Switcher._context.environment, DEFAULT_ENVIRONMENT), ); success(); @@ -297,11 +301,11 @@ export class Switcher { private static _initTimedMatch(options: SwitcherOptions) { if (SWITCHER_OPTIONS.REGEX_MAX_BLACK_LIST in options) { - TimedMatch.setMaxBlackListed(options.regexMaxBlackList || DEFAULT_REGEX_MAX_BLACKLISTED); + TimedMatch.setMaxBlackListed(Switcher._get(options.regexMaxBlackList, DEFAULT_REGEX_MAX_BLACKLISTED)); } if (SWITCHER_OPTIONS.REGEX_MAX_TIME_LIMIT in options) { - TimedMatch.setMaxTimeLimit(options.regexMaxTimeLimit || DEFAULT_REGEX_MAX_TIME_LIMIT); + TimedMatch.setMaxTimeLimit(Switcher._get(options.regexMaxTimeLimit, DEFAULT_REGEX_MAX_TIME_LIMIT)); } const hasRegexSafeOption = SWITCHER_OPTIONS.REGEX_SAFE in options; @@ -323,7 +327,7 @@ export class Switcher { if (Switcher._isTokenExpired()) { Switcher._updateSilentToken(); - remote.checkAPIHealth(Switcher._context.url || '').then((isAlive) => { + remote.checkAPIHealth(Switcher._get(Switcher._context.url, '')).then((isAlive) => { if (isAlive) { Switcher._auth(); } @@ -343,6 +347,10 @@ export class Switcher { return !Switcher._context.exp || Date.now() > (Switcher._context.exp * 1000); } + private static _get(value: T | undefined, defaultValue: T): T { + return value ?? defaultValue; + } + /** * Force a switcher value to return a given value by calling one of both methods - true() false() * @@ -500,6 +508,9 @@ export class Switcher { return this; } + /** + * When enabled, isItOn will return a ResultDetail object + */ detail(showDetail = true): this { this._showDetail = showDetail; return this; @@ -566,8 +577,8 @@ export class Switcher { > { const response = await checkCriteriaLocal( Switcher._snapshot, - this._key || '', - this._input || [], + Switcher._get(this._key, ''), + Switcher._get(this._input, []), ); if (Switcher._options.logger) { diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 1b3fe6a..414646f 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -7,7 +7,7 @@ export type SwitcherContext = { apiKey?: string; domain: string; component?: string; - environment: string; + environment?: string; token?: string; exp?: number; };