Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/code-snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: code-snippets-check

on:
pull_request:
push:
branches:
- main

jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Node.js test suite dependencies
run: cd fern/pages/sdks/examples/node && npm ci

- name: Run test suite for Node.js SDK
run: cd fern/pages/sdks/examples/node && npm test
env:
UNLEASH_URL: ${{ secrets.UNLEASH_URL }}
UNLEASH_TOKEN: ${{ secrets.UNLEASH_TOKEN }}
119 changes: 41 additions & 78 deletions fern/pages/sdks/backend/node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ We recommend that you initialize the Unleash client SDK **as early as possible**
application. The SDK will set up an in-memory repository and poll for updates from the Unleash
server at regular intervals.

```js
import { initialize } from 'unleash-client';

const unleash = initialize({
url: 'https://YOUR-API-URL',
appName: 'my-node-name',
customHeaders: { Authorization: '<YOUR_API_TOKEN>' },
});
```
<Code
src="../examples/node/node-sdk-initialize.mjs"
title="Initialize Node.js SDK"
language="js"
lines="[1-8]"
/>

The `initialize` function will configure a **global** Unleash instance. If you call this method
multiple times, the global instance will be changed. You will **not** create multiple instances.
Expand All @@ -73,12 +70,12 @@ Because the SDK takes care of talking to the server in the background, it can be
exactly when it has connected and is ready to evaluate toggles. If you want to run some code when
the SDK becomes ready, you can listen for the `'synchronized'` event:

```js
unleash.on('synchronized', () => {
// the SDK has synchronized with the server
// and is ready to serve
});
```
<Code
src="../examples/node/node-sdk-initialize.mjs"
title="synchronized event"
language="js"
lines="[9-12]"
/>

Refer to the [events reference](#events) later in this document for more information on events and
an exhaustive list of all the events the SDK can emit.
Expand All @@ -96,39 +93,25 @@ request**. Most applications are expected to only have a single Unleash instance
Unleash instance will maintain a connection to the Unleash API, which may result in flooding the
Unleash API.

```js
import { Unleash } from 'unleash-client';

const unleash = new Unleash({
url: 'https://YOUR-API-URL',
appName: 'my-node-name',
customHeaders: { Authorization: '<YOUR_API_TOKEN>' },
});

unleash.on('ready', console.log.bind(console, 'ready'));

// optional error handling when using unleash directly
unleash.on('error', console.error);
```
<Code
src="../examples/node/node-sdk-new-instance.mjs"
language="javascript"
title="Create new instance"
lines="[1-14]"
/>

#### Synchronous Initialization

You can also use the `startUnleash` function and `await` to wait for the SDK to have fully
synchronized with the Unleash API. This guarantees that the SDK is not operating on local and
potentially stale feature toggle configuration.

```js
import { startUnleash } from 'unleash-client';

const unleash = await startUnleash({
url: 'https://YOUR-API-URL',
appName: 'my-node-name',
customHeaders: { Authorization: '<YOUR_API_TOKEN>' },
});

// The Unleash SDK now has a fresh state from the Unleash API
const isEnabled = unleash.isEnabled('Demo');
```
<Code
src="../examples/node/node-sdk-start.mjs"
language="javascript"
title="Synchronous Initialization"
lines="[1-11]"
/>

### 3. Check Features

Expand Down Expand Up @@ -162,13 +145,12 @@ You can use the `getVariant` method to retrieve the variant of a feature flag. I
disabled or doesn't have any variants, the method returns the
[disabled variant](https://docs.getunleash.io/reference/feature-toggle-variants#the-disabled-variant).

```js
const variant = unleash.getVariant('demo-variant');

if (variant.name === 'blue') {
// do something with the blue variant...
}
```
<Code
src="../examples/node/node-sdk-start.mjs"
language="javascript"
title="Check Variants"
lines="[13-17]"
/>

#### Providing an Unleash Context

Expand All @@ -182,28 +164,23 @@ evaluate some of the built-in strategies.

The `isEnabled` accepts an Unleash context object as a second argument:

```js
const unleashContext = {
userId: '123',
sessionId: 'some-session-id',
remoteAddress: '127.0.0.1',
properties: {
region: 'EMEA',
},
};

const enabled = unleash.isEnabled('someToggle', unleashContext);
```
<Code
src="../examples/node/node-sdk-start.mjs"
language="javascript"
title="Unleash Context"
lines="[20-29]"
/>

### 4. Stop unleash

To shut down the client (turn off the polling) you can simply call the `destroy` method. This is
typically not required.

```js
import { destroy } from 'unleash-client';
destroy();
```
<Code
src="../examples/node/node-sdk-destroy.mjs"
language="javascript"
title="Stop unleash"
/>

### Built-in Activation Strategies

Expand All @@ -212,20 +189,6 @@ The client supports all built-in activation strategies provided by Unleash.
Read more about
[activation strategies in the official docs](https://docs.getunleash.io/reference/activation-strategies).

### Unleash Context

In order to use some of the common activation strategies you must provide an
[Unleash context](https://docs.getunleash.io/reference/unleash-context). This client SDK allows you
to send in the unleash context as part of the `isEnabled` call:

```javascript
const unleashContext = {
userId: '123',
sessionId: 'some-session-id',
remoteAddress: '127.0.0.1',
};
unleash.isEnabled('someToggle', unleashContext);
```

## TypeScript: Custom Feature Toggle Names

Expand Down
1 change: 1 addition & 0 deletions fern/pages/sdks/examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
3 changes: 3 additions & 0 deletions fern/pages/sdks/examples/node/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# copy this file to .env and fill in your Unleash instance details
export UNLEASH_URL="YOUR_URL"
export UNLEASH_TOKEN="YOUR_TOKEN"
2 changes: 2 additions & 0 deletions fern/pages/sdks/examples/node/node-sdk-destroy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { destroy } from 'unleash-client';
destroy();
15 changes: 15 additions & 0 deletions fern/pages/sdks/examples/node/node-sdk-initialize.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { initialize } from 'unleash-client';

const unleash = initialize({
url: process.env.UNLEASH_URL,
appName: 'my-node-name',
customHeaders: { Authorization: process.env.UNLEASH_TOKEN },
});

unleash.on('synchronized', () => {
// the SDK has synchronized with the server
// and is ready to serve
});

// exit test process
setTimeout(() => process.exit(0), 200);
16 changes: 16 additions & 0 deletions fern/pages/sdks/examples/node/node-sdk-new-instance.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Unleash } from 'unleash-client';

console.error("Creating new Unleash instance");
const unleash = new Unleash({
url: process.env.UNLEASH_URL,
appName: 'my-node-name',
customHeaders: { Authorization: process.env.UNLEASH_TOKEN },
});

unleash.on('ready', console.log.bind(console, 'ready'));

// optional error handling when using unleash directly
unleash.on('error', console.error);

// exit test process
setTimeout(() => process.exit(0), 200);
32 changes: 32 additions & 0 deletions fern/pages/sdks/examples/node/node-sdk-start.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { startUnleash } from 'unleash-client';

const unleash = await startUnleash({
url: process.env.UNLEASH_URL,
appName: 'my-node-name',
customHeaders: { Authorization: process.env.UNLEASH_TOKEN },
});

// The Unleash SDK now has a fresh state from the Unleash API
const isEnabled = unleash.isEnabled('Demo');

// check variant
const variant = unleash.getVariant('demo-variant');

if (variant.name === 'blue') {
// do something with the blue variant...
}

// use context
const unleashContext = {
userId: '123',
sessionId: 'some-session-id',
remoteAddress: '127.0.0.1',
properties: {
region: 'EMEA',
},
};

const enabled = unleash.isEnabled('someToggle', unleashContext);

// exit test process
setTimeout(() => process.exit(0), 200);
Loading
Loading