TAS Client for Experimentation Services

0.3.2 · active · verified Tue Apr 21

The `tas-client` package, currently at version 0.3.2, provides a client-side library for querying, refetching, and caching data from an experimentation service endpoint. It is specifically designed to interact with endpoints whose results conform to a required experimentation data structure. This client library emphasizes integration via consumer-provided implementations of key interfaces, including `IExperimentationFilterProvider`, `IExperimentationTelemetry`, and `IKeyValueStorage`, allowing for flexible integration with various application environments. Its focus on managing experiment data, including features like configurable refetch intervals and treatment variable retrieval, differentiates it from generic HTTP clients by providing specialized logic for experimentation workflows. Given its `0.x` version, releases are likely feature-driven and ad-hoc rather than on a fixed schedule, indicating an active but evolving development phase.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate and use `TASClient` by providing minimal implementations for its required interfaces (`IExperimentationFilterProvider`, `IExperimentationTelemetry`, `IKeyValueStorage`), initializing the client, and retrieving experiment treatment variables both synchronously and asynchronously after awaiting initialization.

import { TASClient, type IExperimentationFilterProvider, type IExperimentationTelemetry, type IKeyValueStorage } from 'tas-client';

// Minimal implementation of IExperimentationFilterProvider
class MyFilterProvider implements IExperimentationFilterProvider {
  getFilters(): Record<string, string> {
    return { 'userSegment': 'premium' };
  }
}

// Minimal implementation of IExperimentationTelemetry
class MyTelemetry implements IExperimentationTelemetry {
  async postEvent(eventName: string, properties: Record<string, any>): Promise<void> {
    console.log(`Telemetry event: ${eventName}`, properties);
  }
}

// Minimal implementation of IKeyValueStorage
class MyKeyValueStorage implements IKeyValueStorage {
  private store: Record<string, string> = {};
  async get(key: string): Promise<string | undefined> {
    console.log(`Getting key: ${key}`);
    return this.store[key];
  }
  async set(key: string, value: string): Promise<void> {
    console.log(`Setting key: ${key} to value: ${value}`);
    this.store[key] = value;
  }
}

async function runTasClient() {
  const filterProvider = new MyFilterProvider();
  const telemetry = new MyTelemetry();
  const keyValueStorage = new MyKeyValueStorage();
  const storageKey = 'myTasClientCache'; // Unique key for storage
  const tasEndpoint = process.env.TAS_ENDPOINT ?? 'https://api.example.com/experimentation'; // Replace with actual endpoint
  const refetchInterval = 60 * 60 * 1000; // Refetch every hour

  const tasClient = new TASClient({
    filterProviders: [filterProvider],
    telemetry: telemetry,
    storageKey: storageKey,
    keyValueStorage: keyValueStorage,
    assignmentContextTelemetryPropertyName: 'assignmentContext',
    telemetryEventName: 'tasClientEvent',
    endpoint: tasEndpoint,
    refetchInterval: refetchInterval,
  });

  // Wait for initialization to complete before getting synchronous treatment variables
  await tasClient.initializePromise;

  const treatmentVariable = tasClient.getTreatmentVariable('myConfigId', 'featureFlag');
  console.log(`Treatment variable for 'featureFlag': ${treatmentVariable}`);

  // Alternatively, if not awaiting initializePromise:
  const treatmentVariableAsync = await tasClient.getTreatmentVariableAsync('anotherConfigId', 'anotherFeature');
  console.log(`Async treatment variable for 'anotherFeature': ${treatmentVariableAsync}`);
}

runTasClient().catch(console.error);

view raw JSON →