Skip to content

Commit

Permalink
Fixes loadSnapshot option arguments (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored May 17, 2024
1 parent a47caa0 commit 96d1f9a
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 48 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.43.3
- name: Setup Deno v1.43.4
uses: denoland/setup-deno@v1
with:
deno-version: v1.43.3
deno-version: v1.43.4

- name: Setup LCOV
run: sudo apt install -y lcov
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A Deno SDK for Switcher API
[![Master CI](https://github.com/switcherapi/switcher-client-deno/actions/workflows/master.yml/badge.svg)](https://github.com/switcherapi/switcher-client-deno/actions/workflows/master.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=switcherapi_switcher-client-deno&metric=alert_status)](https://sonarcloud.io/dashboard?id=switcherapi_switcher-client-deno)
[![deno.land/x/switcher4deno](http://shield.deno.dev/x/switcher4deno)](https://deno.land/x/switcher4deno)
[![JSR](https://jsr.io/badges/@switcherapi/switcher-client-deno)](https://jsr.io/@switcherapi/switcher-client-deno)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Slack: Switcher-HQ](https://img.shields.io/badge/slack-@switcher/hq-blue.svg?logo=slack)](https://switcher-hq.slack.com/)

Expand All @@ -34,7 +35,7 @@ https://github.com/switcherapi/switcher-api
## Module initialization
The context properties stores all information regarding connectivity.

(*) Requires Deno 1.4x or higher (use `--unstable` flag for Deno lower than 1.4x)
(*) Requires Deno 1.4x or higher (use `--unstable` flag for Deno versions lower than 1.4x)

> Flags required
```
Expand All @@ -45,7 +46,8 @@ The context properties stores all information regarding connectivity.

> Initialization
```ts
import { Client } from "https://deno.land/x/switcher4deno@v[VERSION]/mod.ts";
import { Client } from "@switcherapi/switcher-client-deno@[VERSION]"; // or
import { Client } from 'https://deno.land/x/switcher4deno@v[VERSION]/mod.ts';

const url = 'https://api.switcherapi.com';
const apiKey = '[API_KEY]';
Expand Down
4 changes: 2 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@switcherapi/switcher-client-deno",
"version": "2.0.0",
"description": "Switcher4Deno is a Feature Flag Deno SDK client for Switcher API",
"version": "2.0.1",
"description": "Switcher4Deno is a Feature Flag Deno Client SDK for Switcher API",
"tasks": {
"cache-reload": "deno cache --reload --lock=deno.lock --lock-write mod.ts",
"fmt": "deno fmt mod.ts src/ --options-single-quote --options-line-width=120 --check",
Expand Down
22 changes: 11 additions & 11 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
// Copyright 2024-present the Switcher API authors. All rights reserved. MIT license.

/**
* Switcher Clinet SDK for working with Switcher API
*
* ```ts
* Client.buildContext({ url, apiKey, domain, component, environment });
*
* const switcher = Client.getSwitcher();
* await switcher.isItOn('SWITCHER_KEY'));
* ```
*
* @module
*/

export { Switcher } from './src/switcher.ts';
export { Client } from './src/client.ts';
export type { ResultDetail, SwitcherContext, SwitcherOptions } from './src/types/index.d.ts';
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.projectKey=switcherapi_switcher-client-deno
sonar.projectName=switcher-client-deno
sonar.organization=switcherapi
sonar.projectVersion=2.0.0
sonar.projectVersion=2.0.1

sonar.javascript.lcov.reportPaths=coverage/report.lcov

Expand Down
9 changes: 4 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DEFAULT_TEST_MODE,
SWITCHER_OPTIONS,
} from './lib/constants.ts';
import type { Snapshot, SwitcherContext, SwitcherOptions } from './types/index.d.ts';
import type { LoadSnapshotOptions, Snapshot, SwitcherContext, SwitcherOptions } from './types/index.d.ts';
import type Key from './lib/bypasser/key.ts';
import TimedMatch from './lib/utils/timed-match/index.ts';
import ExecutionLogger from './lib/utils/executionLogger.ts';
Expand Down Expand Up @@ -151,10 +151,9 @@ export class Client {
* @param watchSnapshot when true, it will watch for snapshot file modifications
* @param fetchRemote when true, it will initialize the snapshot from the API
*/
static async loadSnapshot(
watchSnapshot = false,
fetchRemote = false,
): Promise<number> {
static async loadSnapshot(options: LoadSnapshotOptions = {}): Promise<number> {
const { fetchRemote = false, watchSnapshot = false } = options;

Client._snapshot = loadDomain(
util.get(Client._options.snapshotLocation, ''),
util.get(Client._context.environment, DEFAULT_ENVIRONMENT),
Expand Down
2 changes: 1 addition & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { existsSync } from 'jsr:@std/fs@0.224.0';
export { existsSync } from 'jsr:@std/fs@0.229.1';
5 changes: 5 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export type RetryOptions = {
retryDurationIn: string;
};

export type LoadSnapshotOptions = {
watchSnapshot?: boolean;
fetchRemote?: boolean;
};

// Remote API types

export type AuthResponse = {
Expand Down
6 changes: 3 additions & 3 deletions test/playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let switcher: Switcher;
*/
async function setupSwitcher(local: boolean) {
Client.buildContext({ url, apiKey, domain, component, environment }, { local, logger: true });
await Client.loadSnapshot(false, local)
await Client.loadSnapshot({ fetchRemote: local })
.then(version => console.log('Snapshot loaded - version:', version))
.catch(() => console.log('Failed to load Snapshot'));
}
Expand Down Expand Up @@ -136,7 +136,7 @@ const _testBypasser = async () => {
// Requires remote API
const _testWatchSnapshot = async () => {
Client.buildContext({ url, apiKey, domain, component, environment }, { snapshotLocation, local: true, logger: true });
await Client.loadSnapshot(false, true)
await Client.loadSnapshot({ fetchRemote: true })
.then(() => console.log('Snapshot loaded'))
.catch(() => console.log('Failed to load Snapshot'));

Expand All @@ -153,7 +153,7 @@ const _testSnapshotAutoUpdate = async () => {
Client.buildContext({ url, apiKey, domain, component, environment },
{ local: true, logger: true });

await Client.loadSnapshot(false, true);
await Client.loadSnapshot({ fetchRemote: true });
const switcher = Client.getSwitcher();

Client.scheduleSnapshotAutoUpdate(3, {
Expand Down
4 changes: 2 additions & 2 deletions test/switcher-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('E2E test - Client local:', function () {
snapshotLocation: 'generated-snapshots/'
});

const version = await Client.loadSnapshot(false, false);
const version = await Client.loadSnapshot();
assertEquals(version, 0);
assertExists(Client.snapshot);
});
Expand All @@ -264,7 +264,7 @@ describe('E2E test - Client local:', function () {

//or
let error: Error | undefined;
await Client.loadSnapshot(false, false).catch((e) => error = e);
await Client.loadSnapshot().catch((e) => error = e);
assertEquals(error?.message, 'Something went wrong: It was not possible to load the file at //somewhere/');
});

Expand Down
8 changes: 4 additions & 4 deletions test/switcher-snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('E2E test - Client local - Snapshot:', function () {
regexSafe: false
});

await Client.loadSnapshot(true);
await Client.loadSnapshot({ watchSnapshot: true });
assertTrue(await Client.checkSnapshot());
assertTrue(existsSync(`generated-snapshots/${contextSettings.environment}.json`));

Expand All @@ -129,7 +129,7 @@ describe('E2E test - Client local - Snapshot:', function () {
regexSafe: false
});

await Client.loadSnapshot(true, true);
await Client.loadSnapshot({ watchSnapshot: true, fetchRemote: true });
assertTrue(existsSync(`generated-snapshots/${contextSettings.environment}.json`));

//restore state to avoid process leakage
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('E2E test - Client local - Snapshot:', function () {
success: (updated) => snapshotUpdated = updated
});

await Client.loadSnapshot(false, true);
await Client.loadSnapshot({ fetchRemote: true });

const switcher = Client.getSwitcher();
assertFalse(await switcher.isItOn('FF2FOR2030'));
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('E2E test - Client local - Snapshot:', function () {
reject: (err: Error) => error = err
});

await Client.loadSnapshot(false, true);
await Client.loadSnapshot({ fetchRemote: true });

//next call will fail
givenError('POST@/graphql', 'ECONNREFUSED');
Expand Down

0 comments on commit 96d1f9a

Please sign in to comment.