Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@harnessio/ff-nodejs-server-sdk

Package Overview
Dependencies
Maintainers
8
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@harnessio/ff-nodejs-server-sdk

Feature flags SDK for NodeJS environments

  • 1.3.1-rc.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11K
decreased by-16.84%
Maintainers
8
Weekly downloads
 
Created
Source

Node.js SDK For Harness Feature Flags

TypeScript version Node.js version APLv2

Use this README to get started with our Feature Flags (FF) SDK for Node.js. This guide outlines the basics of getting started with the SDK and provides a full code sample for you to try out.

This sample doesn't include configuration options, for in depth steps and configuring the SDK, for example, disabling streaming or using our Relay Proxy, see the Node.js SDK Reference.

Requirements

To use this SDK, make sure you’ve:

  • Installed Node.js v12 or a newer version

To follow along with our test code sample, make sure you’ve:

  • Created a Feature Flag on the Harness Platform called harnessappdemodarkmode
  • Created a server SDK key and made a copy of it

Installing the SDK

The first step is to install the FF SDK as a dependency in your application. To install using npm, use:

npm install @harnessio/ff-nodejs-server-sdk

Or to install with yarn, use:

yarn add @harnessio/ff-nodejs-server-sdk

Code Sample

The following is a complete code example that you can use to test the harnessappdemodarkmode Flag you created on the Harness Platform. When you run the code it will:

  • Connect to the FF service.
  • Report the value of the Flag every 10 seconds until the connection is closed.
  • Listen for when the harnessappdemodarkmode Flag is toggled on or off on the Harness Platform and report the new value.
const { Client, Event } = require('@harnessio/ff-nodejs-server-sdk');

(async () => {
  // set apiKey to your SDK API Key
  const apiKey = 'YOUR_FF_SDK_KEY';

  // set flagName to your flag identifier from the UI
  const flagName = 'harnessappdemodarkmode';

  console.log('Harness SDK Getting Started');

  // Create Client
  const client = new Client(apiKey);

  // Create a target (different targets can receive different results based on rules.
  // Here we are including "location" as a custom attribute)
  const target = {
    identifier: 'nodeserversdk',
    name: 'NodeServerSDK',
    attributes: {
      location: 'emea',
    },
  };

  await client.waitForInitialization();

  try {
    // Log the state of the flag every 10 seconds
    setInterval(async () => {
      const value = await client.boolVariation(flagName, target, false);
      console.log('Flag variation:', value);
    }, 10000);

    // We can also watch for the event when a flag changes
    client.on(Event.CHANGED, async (flagIdentifier) => {
      const value = await client.boolVariation(flagIdentifier, target, false);
      console.log(`${flagIdentifier} changed: ${value}`);
    });
  } catch (e) {
    console.error('Error:', e);
  }
})();

Running the example

To run the example, execute your script:

node example.js

When you're finished you can exit the example by stopping the process using control-c.

Additional Reading

For further examples and config options, see the Node.js SDK Reference and the test Node.js project. For more information about Feature Flags, see our Feature Flags documentation.

FAQs

Package last updated on 13 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc