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 .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM denoland/deno:2.5.4
FROM denoland/deno:2.6.4

# Install tools
RUN apt-get update && \
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
with:
fetch-depth: 0

- name: Setup Deno v2.6.3
- name: Setup Deno v2.6.4
uses: denoland/setup-deno@v2
with:
deno-version: v2.6.3
deno-version: v2.6.4

- name: Setup LCOV
run: sudo apt install -y lcov
Expand All @@ -45,7 +45,7 @@ jobs:
strategy:
fail-fast: false
matrix:
deno-version: [v1.46.3, v2.6.3]
deno-version: [v1.46.3, v2.6.4]
os: [ ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
ref: ${{ steps.pr.outputs.head_sha }}
fetch-depth: 0

- name: Setup Deno v2.6.3
- name: Setup Deno v2.6.4
uses: denoland/setup-deno@v2
with:
deno-version: v2.6.3
deno-version: v2.6.4

- name: Setup LCOV
run: sudo apt install -y lcov
Expand Down
78 changes: 37 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Switcher4Deno

<div align="center">
<b>Switcher4Deno</b><br>
<b>Switcher Client Deno SDK</b><br>
A Deno SDK for Switcher API
</div>

Expand All @@ -22,49 +20,49 @@ A Deno SDK for Switcher API

## 📋 Table of Contents

- [🎯 About](#-about)
- [Key Features](#-key-features)
- [🚀 Quick Start](#-quick-start)
- [About](#-about)
- [Key Features](#-key-features)
- [Quick Start](#-quick-start)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Basic Setup](#basic-setup)
- [⚙️ Configuration](#️-configuration)
- [Configuration](#️-configuration)
- [Context Parameters](#context-parameters)
- [Advanced Options](#advanced-options)
- [Options Reference](#options-reference)
- [💡 Usage Examples](#-usage-examples)
- [Usage Examples](#-usage-examples)
- [1. Basic Feature Flag Check](#1-basic-feature-flag-check)
- [2. Strategy Validation with Input Preparation](#2-strategy-validation-with-input-preparation)
- [3. All-in-One Execution](#3-all-in-one-execution)
- [4. Performance Optimization with Throttling](#4-performance-optimization-with-throttling)
- [5. Hybrid Mode - Force Remote Resolution](#5-hybrid-mode---force-remote-resolution)
- [🧪 Testing & Development](#-testing--development)
- [Testing & Development](#-testing--development)
- [Built-in Stub Feature](#built-in-stub-feature)
- [Test Mode](#test-mode)
- [Smoke Testing](#smoke-testing)
- [📸 Snapshot Management](#-snapshot-management)
- [Snapshot Management](#-snapshot-management)
- [Loading Snapshots](#loading-snapshots)
- [Real-time Snapshot Monitoring](#real-time-snapshot-monitoring)
- [Snapshot Version Checking](#snapshot-version-checking)
- [Automatic Snapshot Updates](#automatic-snapshot-updates)

## 🎯 About
## About

**Switcher4Deno** is a feature-rich SDK for integrating [Switcher API](https://github.com/switcherapi/switcher-api) into your Deno applications. It provides robust feature flag management with enterprise-grade capabilities.
**Switcher Client Deno** (former Switcher4Deno) is a feature-rich SDK for integrating [Switcher API](https://github.com/switcherapi/switcher-api) into your Deno applications. It provides robust feature flag management with enterprise-grade capabilities.

### Key Features
### Key Features

- 🚀 **Zero Latency**: Local mode with snapshot files or in-memory for instant feature flag resolution
- 🔄 **Hybrid Configuration**: Silent mode with automatic fallback handling
- 🧪 **Testing Ready**: Built-in stub implementation for comprehensive testing
- ⚡ **Performance Optimized**: Throttling optimizes remote API calls to reduce bottlenecks in critical code paths
- 🛠️ **Developer Tools**: Runtime snapshot updates without app restart and automatic sync with remote API

## 🚀 Quick Start
## Quick Start

### Prerequisites

- **Deno**: Version 1.4x or higher
- **Deno**: Version 1.4x or above
- **Required Permissions**:
```bash
--allow-read --allow-write --allow-net
Expand All @@ -85,7 +83,7 @@ import { Client } from 'https://deno.land/x/switcher4deno@v[VERSION]/mod.ts';
```ts
import { Client } from "@switcherapi/switcher-client-deno";

// Initialize the client context
// 1. Initialize the client
Client.buildContext({
url: 'https://api.switcherapi.com',
apiKey: '[YOUR_API_KEY]',
Expand All @@ -94,15 +92,15 @@ Client.buildContext({
environment: 'default'
});

// Get a switcher instance
const switcher = Client.getSwitcher();
// 2. Get a switcher instance
const switcher = Client.getSwitcher('FEATURE01');

// Check if a feature is enabled
const isEnabled = await switcher.isItOn('FEATURE01');
// 3. Check if a feature is enabled
const isEnabled = await switcher.isItOn();
console.log('Feature enabled:', isEnabled);
```

## ⚙️ Configuration
## Configuration
### Context Parameters

| Parameter | Type | Required | Description |
Expand Down Expand Up @@ -153,31 +151,29 @@ Client.buildContext({

> **⚠️ Note on regexSafe**: This feature protects against reDOS attacks but uses Web Workers, which are incompatible with compiled executables.

## 💡 Usage Examples
## Usage Examples

### 1. Basic Feature Flag Check

Simple on/off checks for feature flags:

```ts
// Non-persisted switcher instance
const switcher = Client.getSwitcher();

// Synchronous (local mode only)
const isEnabled = switcher.isItOn('FEATURE01') as boolean;
const isEnabledBool = switcher.isItOnBool('FEATURE01');
// Returns: true or false

// With detailed response
const response = switcher.detail().isItOn('FEATURE01') as SwitcherResult;
const detailedResponse = switcher.isItOnDetail('FEATURE01');
// Returns: { result: true, reason: 'Success', metadata: {} }

// Asynchronous (remote/hybrid mode)
const isEnabledAsync = await switcher.isItOn('FEATURE01');
const isEnabledBoolAsync = await switcher.isItOnBool('FEATURE01', true);
const responseAsync = await switcher.detail().isItOn('FEATURE01');
const detailedResponseAsync = await switcher.isItOnDetail('FEATURE01', true);
// Returns: Promise<boolean> or Promise<SwitcherResult>
// Persisted switcher instance
const switcher = Client.getSwitcher('FEATURE01');

// 🚀 Synchronous (local mode only)
const isEnabled = switcher.isItOn(); // Returns: boolean
const isEnabledBool = switcher.isItOnBool(); // Returns: boolean
const detailResult = switcher.detail().isItOn(); // Returns: { result, reason, metadata }
const detailDirect = switcher.isItOnDetail(); // Returns: { result, reason, metadata }

// 🌐 Asynchronous (remote/hybrid mode)
const isEnabledAsync = await switcher.isItOn(); // Returns: Promise<boolean>
const isEnabledBoolAsync = await switcher.isItOnBool(true); // Returns: Promise<boolean>
const detailResultAsync = await switcher.detail().isItOn(); // Returns: Promise<SwitcherResult>
const detailDirectAsync = await switcher.isItOnDetail(true); // Returns: Promise<SwitcherResult>
```

### 2. Strategy Validation with Input Preparation
Expand Down Expand Up @@ -243,7 +239,7 @@ This is useful for:
- Critical features that must be resolved remotely
- Real-time configuration updates

## 🧪 Testing & Development
## Testing & Development

### Built-in Stub Feature

Expand Down Expand Up @@ -301,7 +297,7 @@ try {
}
```

## 📸 Snapshot Management
## Snapshot Management

Snapshots enable zero-latency local mode by caching your feature flag configuration.

Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@switcherapi/switcher-client-deno",
"version": "2.4.1",
"description": "Switcher4Deno is a Feature Flag Deno Client SDK for Switcher API",
"description": "Switcher Client Deno is a Feature Flag Deno Client SDK for Switcher API",
"tasks": {
"cache-reload": "deno cache --reload --lock=deno.lock mod.ts",
"fmt": "deno fmt mod.ts src/ --options-single-quote --options-line-width=120 --check",
Expand Down
20 changes: 18 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { SnapshotWatcher } from './lib/snapshotWatcher.ts';
*/
export class Client {
private static readonly _snapshotWatcher = new SnapshotWatcher();
private static readonly _switchers = new Map<string, Switcher>();
private static _testEnabled: boolean;
private static _context: SwitcherContext;

Expand Down Expand Up @@ -128,11 +129,26 @@ export class Client {
}

/**
* Creates a new instance of Switcher
* Creates a new instance of Switcher.
*
* Provide a key if you want to persist the instance.
*/
static getSwitcher(key?: string): Switcher {
return new Switcher(util.get(key, ''))
const keyValue = util.get(key, '');
const persistedSwitcher = Client._switchers.get(keyValue);

if (persistedSwitcher) {
return persistedSwitcher;
}

const switcher = new Switcher(keyValue)
.restrictRelay(GlobalOptions.restrictRelay);

if (keyValue) {
Client._switchers.set(keyValue, switcher);
}

return switcher;
}

/**
Expand Down
Loading