anmo
v1.0.0
Published
Official Node.js SDK for Anmo Feature Flags
Maintainers
Readme
Anmo SDK
Official Node.js SDK for Anmo Feature Flags - a modern, real-time feature flag management service.
Installation
npm install anmoQuick Start
import { AnmoClient } from "anmo";
const client = new AnmoClient({
apiKey: "anmo_your_api_key_here",
autoFetch: true, // Automatically fetch flags on init
autoStream: true, // Automatically start streaming updates
});
// Wait for initial flags to load
await client.ready();
// Check if a flag is enabled
if (client.isEnabled("new-dashboard")) {
console.log("New dashboard is enabled!");
}Features
- 🚀 Real-time Updates - Automatically sync flag changes via Server-Sent Events
- 🔒 Type-safe - Full TypeScript support with type definitions
- 📦 Lightweight - Minimal dependencies
- 🎯 Simple API - Easy to use, hard to misuse
- ⚡ Fast - Built for performance
- ⚛️ React Hooks - First-class React support with custom hooks
Installation
npm install anmoFor React applications, React 16.8+ is required (for hooks support).
Usage
Basic Example
import { AnmoClient } from "anmo";
const client = new AnmoClient({
apiKey: "anmo_your_api_key_here",
baseUrl: "https://your-anmo-instance.com",
});
// Wait for initial flags to load
await client.ready();
// Check individual flag
if (client.isEnabled("new-dashboard")) {
console.log("New dashboard is enabled!");
}
// Get a specific flag with a default value
const darkMode = client.getFlag("dark-mode", false);
// Get all flags
const allFlags = client.getAllFlags();
console.log("All flags:", allFlags);Real-time Updates
const client = new AnmoClient({
apiKey: "anmo_your_api_key_here",
baseUrl: "https://your-anmo-instance.com",
autoStream: true, // Start streaming immediately
});
// Listen for flag updates
client.onUpdate((flags) => {
console.log("Flags updated:", flags);
// React to changes
if (flags["maintenance-mode"]) {
showMaintenanceBanner();
}
});
// Clean up when done
process.on("SIGINT", () => {
client.destroy();
process.exit();
});Manual Streaming Control
const client = new AnmoClient({
apiKey: "anmo_your_api_key_here",
baseUrl: "https://your-anmo-instance.com",
autoStream: false, // Don't auto-start
});
// Start streaming manually
client.startStream();
// Stop streaming when needed
client.stopStream();React Usage
The SDK provides React hooks for easy integration:
Basic Hook
import { useFeatureFlag } from "anmo/react";
function App() {
const showNewDashboard = useFeatureFlag("new-dashboard", {
apiKey: process.env.REACT_APP_ANMO_API_KEY!,
});
const useDarkMode = useFeatureFlag("use-dark-mode", {
apiKey: process.env.REACT_APP_ANMO_API_KEY!,
});
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<div>
{showNewDashboard && <NewDashboard />}
{useDarkMode && <DarkModeToggle />}
</div>
);
}React API Reference
useFeatureFlag(flagKey, options, defaultValue?)
Hook for a single feature flag. Returns boolean indicating if the flag is enabled.
Node.js API Reference
Constructor Options
interface AnmoClientOptions {
apiKey: string; // Required: Your Anmo API key
baseUrl?: string; // Optional: Base URL (default: 'http://localhost:3000')
autoFetch?: boolean; // Optional: Auto-fetch on init (default: true)
autoStream?: boolean; // Optional: Auto-start streaming (default: false)
}Methods
ready(): Promise<void>
Wait for the client to be ready (initial flags fetched).
getFlags(): Promise<FlagMap>
Manually fetch all feature flags. Usually not needed if autoFetch is enabled.
isEnabled(key: string, defaultValue?: boolean): boolean
Check if a feature flag is enabled. Returns defaultValue (default: false) if flag doesn't exist.
getFlag(key: string, defaultValue?: boolean): boolean
Alias for isEnabled().
getAllFlags(): FlagMap
Get a copy of all current flags as an object.
onUpdate(listener: (flags: FlagMap) => void): () => void
Subscribe to flag updates. Returns an unsubscribe function.
const unsubscribe = client.onUpdate((flags) => {
console.log("Updated flags:", flags);
});
// Later, to unsubscribe:
unsubscribe();startStream(): void
Start streaming real-time updates via Server-Sent Events.
stopStream(): void
Stop streaming updates and close the connection.
destroy(): void
Clean up all resources, close connections, and remove all listeners.
Publishing to npm
To publish this package to npm:
cd sdks/javascript
npm install
npm run build
npm publish --access publicDevelopment
To work on the SDK locally:
cd sdks/javascript
npm install
npm run build
npm linkThen in your project:
npm link anmoLicense
MIT
