Skip to content

Commit

Permalink
Fixes import deps, improved default context/options assignment (#63)
Browse files Browse the repository at this point in the history
* Fixes import deps, improved default context/options assignment

* Refactored snapshot/validateSnapshot
  • Loading branch information
petruki committed May 3, 2024
1 parent ee3823d commit 00e6cd5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
25 changes: 10 additions & 15 deletions src/lib/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 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';
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;
Expand Down Expand Up @@ -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;
};

Expand Down
37 changes: 24 additions & 13 deletions src/switcher-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -141,8 +145,8 @@ export class Switcher {
fetchRemote = false,
): Promise<number> {
Switcher._snapshot = loadDomain(
Switcher._options.snapshotLocation || '',
Switcher._context.environment,
Switcher._get(Switcher._options.snapshotLocation, ''),
Switcher._get(Switcher._context.environment, DEFAULT_ENVIRONMENT),
);

if (
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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();
}
Expand All @@ -343,6 +347,10 @@ export class Switcher {
return !Switcher._context.exp || Date.now() > (Switcher._context.exp * 1000);
}

private static _get<T>(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()
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type SwitcherContext = {
apiKey?: string;
domain: string;
component?: string;
environment: string;
environment?: string;
token?: string;
exp?: number;
};
Expand Down

0 comments on commit 00e6cd5

Please sign in to comment.