TAS Client UMD Module

0.2.0 · active · verified Tue Apr 21

tas-client-umd is a Unified Module Definition (UMD) repackaging of the core `tas-client` module, designed to provide a client for interacting with an experimentation service, typically a Treatment Assignment Service (TAS). Its primary function is to query, refetch, and cache experimentation data, often referred to as "flights" or "treatment variables," from a specified endpoint. This package, currently at version 0.2.0, targets early-stage development with an implied unstable release cadence typical of pre-1.0 software. A key aspect of its design is the requirement for consumers to implement specific interfaces: `IExperimentationFilterProvider` for supplying contextual filters, `IExperimentationTelemetry` for logging events and shared properties, and `IKeyValueStorage` for persistent data storage. This dependency injection approach allows for flexible integration into various environments and telemetry systems. Its UMD format differentiates it by ensuring compatibility across CommonJS, AMD, and browser global environments, making it versatile for diverse JavaScript ecosystems, particularly where `tas-client` is used.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `TASClient` with mock interface implementations and retrieve a treatment variable after initialization.

import { TASClient } from 'tas-client-umd';

// Mock implementations for required interfaces
const mockFilterProvider = {
    getFilters: () => new Map([['region', 'us'], ['platform', 'web']])
};

const mockTelemetry = {
    setSharedProperty: (name, value) => console.log(`Telemetry shared property: ${name}=${value}`),
    postEvent: (eventName, props) => console.log(`Telemetry event: ${eventName}, props: ${JSON.stringify(Object.fromEntries(props))}`)
};

const mockKeyValueStorage = {
    _storage: new Map(),
    getValue: async (key, defaultValue) => {
        const value = mockKeyValueStorage._storage.get(key);
        return value !== undefined ? value : defaultValue;
    },
    setValue: (key, value) => {
        mockKeyValueStorage._storage.set(key, value);
    }
};

const tasClient = new TASClient({
    filterProviders: [mockFilterProvider],
    telemetry: mockTelemetry,
    storageKey: 'my-tas-experiments',
    keyValueStorage: mockKeyValueStorage,
    assignmentContextTelemetryPropertyName: 'myExpContext',
    telemetryEventName: 'MyExperimentationQuery',
    endpoint: process.env.TAS_ENDPOINT ?? 'https://example.com/tas-api', // Replace with your actual endpoint
    refetchInterval: 30000 // Refetch every 30 seconds
});

// Initialize the client and fetch a treatment variable
tasClient.initializePromise.then(async () => {
    console.log('TASClient initialized successfully.');
    // Example: Getting a treatment variable
    const treatmentValue = await tasClient.getTreatmentVariable('my-feature-config', 'my-variable-name');
    console.log(`Treatment variable 'my-variable-name': ${treatmentValue}`);
}).catch(error => {
    console.error('Failed to initialize TASClient:', error);
});

view raw JSON →