Skip to content

unlaunch/javascript-sdk

Repository files navigation

Unlaunch JavaScript Library

main develop
Build Status Build Status

Overview

The Unlaunch JavaScript Library provides JavaScript API to access Unlaunch feature flags and other features. Using this library, you can easily build JavaScript apps that can evaluate feature flags, dynamic configurations, and more.

Important Links

Compatibility

The Unlaunch JavaScript library doesn't require or depend on any specific JavaScript framework. You can use it with your favorite framework like Angular. If you want to integrate with React, we have a separate React SDK available.

Browser Support

The Unlaunch Javascript Library can be used in all major browsers. However, some browsers may not support some features that the library uses, such as ES6 Promises. You may have to use polyfill if your target users use browsers that do not support ES6 Promise.

Getting Started

Here is a simple example.

First, add the library to your project. To load the JavaScript Library, include the following in the or tag of your webpage.

Embed directly in your HTML

<script crossorigin="anonymous" src="https://unpkg.com/unlaunch-js-client-lib/dist/ulclient.min.js">
</script>

Integrate with a JavaScript framework

Or using, npm install:

npm i unlaunch-js-client-lib

and then,

import * as ULClient from "unlaunch-js-client-lib";

Here's how you'd use JavaScript library in an HTML page.

const flag = '<YOUR_FLAG_KEY>'
const apiKey = '<PROVIDE_BROWSER_PUBLIC_KEY_FOR_YOUR_PROJECT>'
const identity = 'anonymous' // Use special anonymous identity which generates a unique UUID

const options = {
    localStorage: true, // Use local storage
    logLevel: 'debug'   // Available logLevels are ['error','warn','info','debug','none']. Default logLevel is 'error'. To disable logging use 'none'. If empty ''            
                        // loglevel is provided all logs are printed. 
                        // Log levels severity are in the order ['error','warn','info','debug']. Error has high severity and debug has lowest.
                        // All logs which have high severity level then the level provided will be printed. So if logLevel info is selected 'error', 'warn' and 
                        // 'info' logs are printed. If 'debug' is provided only debug logs are printed.   
}

const ulclient = ULClient.initialize(
    apiKey,
    [flag],
    identity,
    null,
    options
);

ulclient.on('ready', function () {

let variation = ulclient.variation(flag);
console.log(`[UL] Variation is ${variation}`)

const details = ulclient.variationDetail(flag);
console.log(`[UL] Evaluation reason is ${details.reason}`)


if (variation === 'on') {
    // Show the feature
} else {
    // Hide the feature
}

For more information, see the official guide.

Build instructions

Requirements

  • npm version 6.14.5 or higher
  • node version 12.18.2 or higher

The library has dependency in javascript-sdk-common project. After cloning both the repos, follow these steps.

  1. Go to javascript-sdk-common directory and run npm install and then run npm run build
  2. Go to javascript-sdk-common directory and run npm link. Then go to the javascript-client-sdk and type npm link unlaunch-js-sdk-common
  3. Install javascript-client-sdk dependencies by running npm install and then build npm run build
  4. Go to your project directory and run npm link <path-to-js-client-sdk-directory>
  5. In your project directory run npm install
  6. Import unlaunch-js-client-lib in your project or use the minified JavaScript library.

Publish to npm

  • npm login
  • npm publish

Customization

You can use options to customize the client. For more information, see the official guide.

var options = {
     localStorage: true,
     offline: false,
     requestTimeoutInMillis: 1000
}

Offline Mode

You can start the SDK in offline mode for testing purposes. In offline mode, flags aren't downloaded from the server and no data is sent. All calls to variation() or its variants will return control. Read more in the official guide.

Contributing

Please see CONTRIBUTING to find how you can contribute.

License

Licensed under the Apache License, Version 2.0. See: Apache License.

Publish Releases on npmjs

TODO

About Unlaunch

Unlaunch is a Feature Release Platform for engineering teams. Our mission is allow engineering teams of all sizes to release features safely and quickly to delight their customers. To learn more about Unlaunch, please visit www.unlaunch.io. You can sign up to get started for free at https://app.unlaunch.io/signup .

More Questions?

At Unlaunch, we are obsessed about making it easier for developers all over the world to release features safely and with confidence. If you have any questions or something isn't working as expected, please email unlaunch@gmail.com.