1.7.0 • Published 14 days ago

@harnessio/ff-nodejs-server-sdk v1.7.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
14 days ago

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:

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',
    },
  };

  try {
    await client.waitForInitialization();
  } catch (e) {
    console.log("Error when authenticating Feature Flags client: " + e)
  }

  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.

Using custom TLS certificates

If using a proxy or SMP instance that does not use a widely publicised pre-bundled CA you can provide your own CA using the tlsTrustedCa option. This option takes a path to a file that contains a bundle of certificates in PEM format. If this option is present, the HTTP client will ignore any pre-bundled certs so you need to include the entire certificate chain of your custom cert.

  const client = new Client(apiKey, {
    baseUrl: 'https://ffserver:8000/api/1.0',
    eventsUrl: 'https://ffserver:8001/api/1.0',
    tlsTrustedCa: 'path_to_cert_chain.crt',
  });

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.

1.7.0

14 days ago

1.6.1

1 month ago

1.6.0

3 months ago

1.6.0-rc.2

3 months ago

1.6.0-rc.1

3 months ago

1.5.0

4 months ago

1.5.0-rc.0

4 months ago

1.4.1

4 months ago

1.4.1-rc.1

4 months ago

1.3.7

6 months ago

1.3.6

6 months ago

1.3.5

6 months ago

1.3.4

7 months ago

1.3.3

7 months ago

1.3.2

8 months ago

1.4.0

5 months ago

1.3.2-rc.1

9 months ago

1.3.1

10 months ago

1.3.1-rc.1

10 months ago

1.3.4-rc.4

7 months ago

1.3.4-rc.3

7 months ago

1.3.4-rc.2

7 months ago

1.3.4-rc.1

7 months ago

1.3.1-rc.0

10 months ago

1.3.7-rc.1

6 months ago

1.3.6-rc.2

6 months ago

1.3.6-rc.1

6 months ago

1.3.0

10 months ago

1.2.17

11 months ago

1.2.13

1 year ago

1.2.16

12 months ago

1.2.14

1 year ago

1.2.15

1 year ago

1.2.12

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.4-0

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1-rc1

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago