LaunchDarkly JavaScript SDK Common Components

5.8.0 · active · verified Sun Apr 19

This package (`launchdarkly-js-sdk-common`) serves as the foundational core for all client-side JavaScript-based LaunchDarkly SDKs, including the browser, React, client-side Node, and Electron SDKs. It provides the essential, platform-agnostic implementation components, such as the `initialize` function used to create the basic client object and shared TypeScript definitions. Crucially, application developers should not directly reference or import from this package; it is an internal dependency for other LaunchDarkly SDKs. The current stable version is 5.8.0, with minor releases occurring frequently, often monthly or bi-monthly, introducing new features and bug fixes to support broader LaunchDarkly platform capabilities. Its key differentiator is providing a consistent, shared codebase to reduce duplication and ensure behavioral parity across various JavaScript environments for the LaunchDarkly ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This TypeScript example illustrates how a higher-level LaunchDarkly SDK (like `js-client-sdk`) internally utilizes `launchdarkly-js-sdk-common`'s `initialize` function with a platform object and basic options to create a core `LDClient` instance, and demonstrates its internal event handling and variation fetching.

import { initialize, LDClient, LDOptions } from 'launchdarkly-js-sdk-common';

// This is an example of how a higher-level LaunchDarkly SDK (e.g., js-client-sdk)
// might internally use launchdarkly-js-sdk-common to create a client instance.
// Application code should *not* directly use this package.

// Define a minimal "platform" object that provides environment-specific capabilities
const browserPlatform = {
  // Example placeholder for browser-specific functions
  getCurrentUrl: () => typeof window !== 'undefined' ? window.location.href : 'about:blank',
  sendEvent: (event: any) => { console.log('Sending event (browser mock):', event); },
  // ... other browser-specific implementations like localStorage, network requests, etc.
};

// Define some basic SDK options
const sdkOptions: LDOptions = {
  // A placeholder SDK key, in a real scenario this would come from env or config
  sdkKey: process.env.LAUNCHDARKLY_SDK_KEY ?? 'your-sdk-key',
  bootstrap: {}, // No initial flags for this example
  streaming: true,
  // More options as needed by the specific SDK
};

let client: LDClient | undefined;

try {
  // The 'initialize' function from common is called by specific SDKs
  // with their platform implementation to create the core client.
  client = initialize(sdkOptions, browserPlatform);

  client.on('ready', () => {
    console.log('LaunchDarkly common client is ready internally.');
    const userContext = { kind: 'user', key: 'test-user-123' };
    client?.identify(userContext); // Internal identify call

    // Simulate getting a flag value (would be done by the higher-level SDK)
    const flagValue = client?.variation('my-test-feature', false);
    console.log(`Internal variation for 'my-test-feature': ${flagValue}`);

    // Clean up
    client?.close();
  });

  client.on('failed', (err: any) => {
    console.error('LaunchDarkly common client failed to initialize:', err);
  });

  console.log('Attempting to initialize LaunchDarkly common client...');
} catch (error) {
  console.error('Error during common client initialization:', error);
}

view raw JSON →