4.2.0 • Published 4 days ago

@kameleoon/nodejs-sdk v4.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 days ago

Kameleoon NodeJS SDK

Introduction

Kameleoon NodeJS SDK is used to work with Kameleoon Feature Flags and Experiments using native NodeJS api. Integration of this SDK on server is easy, and its footprint is low.

This page describes the most basic KameleoonClient configuration, for more in-depth review of all the methods and configuration options follow Official Kameleoon Documentation

Contents

Installation

  • npm - npm install @kameleoon/nodejs-sdk
  • yarn - yarn add @kameleoon/nodejs-sdk
  • pnpm - pnpm add @kameleoon/nodejs-sdk
  • bun - bun install @kameleoon/nodejs-sdk

Configuration

  1. Obtain your site code and credentials from Kameleoon Platform
  2. Instantiate a client providing external dependencies.
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';

const client = new KameleoonClient({
  siteCode: 'my_site_code',
  credentials: {
    clientId: 'my_id',
    clientSecret: 'my_secret',
  },
  externals: {
    visitorCodeManager: new KameleoonVisitorCodeManager(),
    eventSource: new KameleoonEventSource(),
  },
});

Usage Example

// -- Wait for the client initialization
await client.initialize();

// -- Generate or obtain a visitor code
const visitorCode = KameleoonClient.getVisitorCode({
  request: req, // `NodeJS/NextJS/Deno` request object
  response: res, // `NodeJS/NextJS/Deno` response object
});

// -- Add targeting data
const customDataIndex = 0;
client.addData(visitorCode, new CustomData(customDataIndex, 'my_data'));

// -- Check if the feature flag is active
const isActive = client.isFeatureFlagActive(
  visitorCode,
  'my_feature_key',
);

External Dependencies

NodeJS SDK utilizes certain external dependencies, which are required to be able to use specific API when working in different contexts.

There are several possible external dependencies used by Kameleoon NodeJS SDK, some of them have default Kameleoon implementations in form of external npm packages, while other are SDK built-ins, but can still be re-implemented by the developers using an SDK.

Here is the list of such dependencies:

  • visitorCodeManager(mandatory) is responsible for managing the visitor code and cookies, it has several default Kameleoon implementations:
    • @kameleoon/nodejs-visitor-code-manager
    • @kameleoon/nextjs-visitor-code-manager
    • @kameleoon/deno-visitor-code-manager
  • eventSource(madatory) is responsible for Real Time Updates(Streaming) for SDK, it has one default Kameleoon implementation:
    • @kameleoon/nodejs-event-source (suitable for NodeJS/Deno/NextJS)
  • storage(optional) is responsible for storing all SDK related data, it has a built-in implementation in the SDK.

Following is the example implementation for each dependency.

visitorCodeManager

import { IExternalVisitorCodeManager, GetDataParametersType } from "@kameleoon/nodejs-sdk";

class MyVisitorCodeManager implements IExternalVisitorCodeManager {
  // - Get visitor code from `request` cookie
  public getData({ request, key }: GetDataParametersType): string | null {
    const cookieString = request.headers.cookie;

    if (!cookieString) {
      return null;
    }

    const cookieEntry = cookieString
      .split(' ;')
      .find((keyValue) => {
        const [cookieKey, cookieValue] = keyValue.split('=');

        return cookieKey === key && cookieValue !== '';
      });

    if (cookieEntry) {
      const [_, value] = cookieEntry.split('=');

      return value;
    }

    return null;
  }

  // - Set visitor code to `response` cookie
  public setData({
    visitorCode,
    response,
    domain,
    maxAge,
    key,
    path,
  }: SetDataParametersType): void {
    let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;

    if (domain) {
      resultCookie += `; Domain=${domain}`;
    }

    response.setHeader('Set-Cookie', resultCookie);
  }
}

eventSource

import { IExternalEventSource } from "@kameleoon/nodejs-sdk";

class MyEventSource implements IExternalEventSource {
  private eventSource?: EventSource;

  public open({
    eventType,
    onEvent,
    url,
  }: EventSourceOpenParametersType): void {
    // - Use any suitable EventSource implementation here
    const eventSource = new EventSource(url);

    this.eventSource = eventSource;
    this.eventSource.addEventListener(eventType, onEvent);
  }

  public close(): void {
    if (this.eventSource) {
      this.eventSource.close();
    }
  }
}

storage

import { IExternalStorage } from "@kameleoon/nodejs-sdk";

const storage = new Map();

class MyStorage<T> implements IExternalStorage<T> {
  public read(key: string): T {
    // - Utilize the storage implementation of your choice
    const data = storage.get(key);

    // - Optionally handle errors
    if (!data) {
      throw new Error("Couldn't read data from myStorage");
    }

    return data;
  }

  public write(key: string, data: T): void {
    storage.set(key, data);
  }
}
4.2.0

4 days ago

4.1.0

19 days ago

4.0.1

3 months ago

4.0.0

3 months ago

3.6.1

3 months ago

3.6.0

3 months ago

3.5.0

4 months ago

3.4.1

5 months ago

3.4.0

5 months ago

3.3.0

5 months ago

2.5.0

10 months ago

2.4.1

10 months ago

2.4.0

10 months ago

2.3.1

11 months ago

2.7.0

8 months ago

2.6.1

9 months ago

2.6.0

9 months ago

2.8.1

7 months ago

2.6.3

8 months ago

2.8.0

7 months ago

2.7.1

7 months ago

2.6.2

8 months ago

3.2.0

5 months ago

3.1.0

6 months ago

3.0.0

6 months ago

2.3.0

11 months ago

2.2.1

12 months ago

2.2.0

12 months ago

2.2.3

12 months ago

2.1.4

12 months ago

2.2.2

12 months ago

2.2.5

12 months ago

2.2.4

12 months ago

2.1.3

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago