TAS Client UMD Module
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
-
TypeError: TASClient is not a constructor
cause Attempting to use `const TASClient = require('tas-client-umd'); new TASClient()` in CommonJS, instead of destructuring the import.fixIn CommonJS, use `const { TASClient } = require('tas-client-umd');` or access via the default export if bundling: `const TASClient = require('tas-client-umd').TASClient;` -
ReferenceError: TASClient is not defined
cause In a browser environment using the UMD build, the global variable might not be accessible, or the script tag for `tas-client-umd` was not loaded before its usage.fixEnsure the `tas-client-umd` script is loaded in your HTML before any code attempts to use `TASClient`. Verify the global variable name exposed by the UMD bundle. -
Property 'isFlightEnabled' is deprecated.
cause Using the `isFlightEnabled` method which has been marked for removal.fixUpdate your code to use `getTreatmentVariable(configId, name)` instead of `isFlightEnabled(flightName)`.
Warnings
- deprecated The `isFlightEnabled` method on `IExperimentationService` is deprecated. Consumers should transition to using `getTreatmentVariable` instead for retrieving experiment values.
- gotcha The `TASClient` requires multiple interfaces (`IExperimentationFilterProvider`, `IExperimentationTelemetry`, `IKeyValueStorage`) to be implemented by the consumer and provided during construction. Failing to do so or providing incomplete implementations will lead to runtime errors.
- gotcha Calling `tasClient.getTreatmentVariable()` before the experimentation service has fully initialized (i.e., before `tasClient.initializePromise` resolves) will result in a runtime error. If you cannot await initialization, use `getTreatmentVariableAsync()`.
Install
-
npm install tas-client-umd -
yarn add tas-client-umd -
pnpm add tas-client-umd
Imports
- TASClient
const TASClient = require('tas-client-umd');import { TASClient } from 'tas-client-umd'; - IExperimentationService
import IExperimentationService from 'tas-client-umd';
import { IExperimentationService } from 'tas-client-umd'; - IKeyValueStorage
import type { IKeyValueStorage } from 'tas-client-umd';
Quickstart
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);
});