-
Notifications
You must be signed in to change notification settings - Fork 468
docs: Node.js v5 docs #4821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
docs: Node.js v5 docs #4821
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eddbc10
Add dynamic Node.js SDK version
rolodato 79ac73c
Node.js docs improvements
rolodato f67897b
Node.js v4 docs
rolodato 3ae6a6f
Fix link/formatting
rolodato 1c2313c
import type instead of import
rolodato e8c42be
more import types
rolodato 27851e1
Merge branch 'main' into docs/nodejs-v4-client
rolodato 1978f95
prettier
rolodato d18b4f4
prettier
rolodato 60318bc
fix + bump nodejs sdk version
rolodato de69025
space in URL, still works tho
rolodato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,13 @@ import CodeBlock from '@theme/CodeBlock'; | |
| import Tabs from '@theme/Tabs'; | ||
| import TabItem from '@theme/TabItem'; | ||
|
|
||
| import { JavaVersion, RustVersion, DotnetVersion, ElixirVersion } from '@site/src/components/SdkVersions.js'; | ||
| import { | ||
| JavaVersion, | ||
| RustVersion, | ||
| DotnetVersion, | ||
| ElixirVersion, | ||
| NodejsVersion, | ||
| } from '@site/src/components/SdkVersions.js'; | ||
|
|
||
| # Server Side SDKs | ||
|
|
||
|
|
@@ -40,11 +46,10 @@ Server Side SDKs can run in 2 different modes: Local Evaluation and Remote Evalu | |
| - Source Code: https://github.com/Flagsmith/flagsmith-dotnet-client | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| - Version Compatibility: **Node 14+** | ||
| - Source Code: | ||
| - https://github.com/Flagsmith/flagsmith-nodejs-client | ||
| - Version Compatibility: **Node 18+** | ||
| - Source Code: https://github.com/Flagsmith/flagsmith-nodejs-client | ||
|
|
||
| </TabItem> | ||
| <TabItem value="ruby" label="Ruby"> | ||
|
|
@@ -140,11 +145,10 @@ pip install flagsmith | |
| </CodeBlock> | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```bash | ||
| # Via NPM | ||
| npm i flagsmith-nodejs --save | ||
| npm install flagsmith-nodejs | ||
| ``` | ||
|
|
||
| </TabItem> | ||
|
|
@@ -245,10 +249,10 @@ _flagsmithClient = new("FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY"); | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```javascript | ||
| const Flagsmith = require('flagsmith-nodejs'); | ||
| import { Flagsmith } from 'flagsmith-nodejs'; | ||
|
|
||
| const flagsmith = new Flagsmith({ | ||
| environmentKey: 'FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY', | ||
|
|
@@ -367,12 +371,12 @@ var buttonData = await flags.GetFeatureValue("secret_button"); | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```javascript | ||
| const flags = await flagsmith.getEnvironmentFlags(); | ||
| var showButton = flags.isFeatureEnabled('secret_button'); | ||
| var buttonData = flags.getFeatureValue('secret_button'); | ||
| const showButton = flags.isFeatureEnabled('secret_button'); | ||
| const buttonData = flags.getFeatureValue('secret_button'); | ||
| ``` | ||
|
|
||
| </TabItem> | ||
|
|
@@ -479,7 +483,7 @@ var showButton = await flags.IsFeatureEnabled("secret_button"); | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```javascript | ||
| const identifier = '[email protected]'; | ||
|
|
@@ -662,7 +666,7 @@ static Flag defaultFlagHandler(string featureName) | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```javascript | ||
| const flagsmith = new Flagsmith({ | ||
|
|
@@ -853,18 +857,32 @@ public class MyCustomOfflineHandler: BaseOfflineHandler | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```javascript | ||
| // Using the built-in local file handler | ||
| const localFileHandler = new LocalFileHandler('path_to_environment_file/environment_file.json'); | ||
| const flagsmith = new Flagsmith({ offlineMode: true, offlineHandler: localFileHandler }); | ||
| Use <a | ||
| href={`https://www.tsdocs.dev/docs/flagsmith-nodejs/${NodejsVersion()}/classes/LocalFileHandler.html`}>LocalFileHandler</a> | ||
| to read an environment file generated by the [Flagsmith CLI](/clients/CLI): | ||
|
|
||
| // Defining a custom offline handler | ||
| class CustomOfflineHandler extends BaseOfflineHandler { | ||
| getEnvironment(): EnvironmentModel { | ||
| return someMethodToGetTheEnvironment(); | ||
| } | ||
| ```typescript | ||
| import { Flagsmith, LocalFileHandler } from 'flagsmith-nodejs'; | ||
|
|
||
| const flagsmith = new Flagsmith({ | ||
| offlineMode: true, | ||
| offlineHandler: new LocalFileHandler('./flagsmith.json'), | ||
| }); | ||
| ``` | ||
|
|
||
| To create your own offline handler, implement the `BaseOfflineHandler` interface. It must return an | ||
| {/* prettier-ignore */}<a target="_blank" href={`https://www.tsdocs.dev/docs/flagsmith-nodejs/${NodejsVersion()}/classes/EnvironmentModel.html`}>EnvironmentModel</a> | ||
| object: | ||
|
|
||
| ```typescript | ||
| import type { BaseOfflineHandler, EnvironmentModel } from 'flagsmith-nodejs'; | ||
|
|
||
| class CustomOfflineHandler implements BaseOfflineHandler { | ||
| getEnvironment(): EnvironmentModel { | ||
| // ... | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
@@ -1015,10 +1033,9 @@ flagsmith.close(); | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label = "NodeJS"> | ||
| <TabItem value="nodejs" label = "Node.js"> | ||
|
|
||
| ```javascript | ||
| // available from v2.2.1 | ||
| flagsmith.close(); | ||
| ``` | ||
|
|
||
|
|
@@ -1413,10 +1430,11 @@ $flagsmith = Flagsmith::Client.new( | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| ```typescript | ||
| import { bool, number } from 'prop-types'; | ||
| import { Flagsmith } from 'flagsmith-nodejs'; | ||
| import type { EnvironmentModel } from 'flagsmith-nodejs'; | ||
|
|
||
| const flagsmith = new Flagsmith({ | ||
| /* | ||
|
|
@@ -1440,9 +1458,8 @@ const flagsmith = new Flagsmith({ | |
| See https://docs.flagsmith.com/clients/server-side#caching | ||
| */ | ||
| cache: { | ||
| has: (key: string) => bool, | ||
| get: (key: string) => string | number | null, | ||
| set: (k: string, v: Flags) => (cache[k] = v), | ||
| get: (key: string) => Promise.resolve(), | ||
| set: (k: string, v: Flags) => Promise.resolve(), | ||
| }, | ||
|
|
||
| /* | ||
|
|
@@ -1780,7 +1797,8 @@ client_configuration = Flagsmith.Client.new( | |
|
|
||
| ## Caching | ||
|
|
||
| The following SDKs have code and functionality related to caching flags. | ||
| Some SDKs support caching flags retrieved from the Flagsmith API, or calculated from your environment definition if | ||
| using Local Evaluation. | ||
|
|
||
| <Tabs groupId="language" queryString> | ||
| <TabItem value="java" label="Java"> | ||
|
|
@@ -1874,68 +1892,37 @@ final FlagsAndTraits flags = cache.getIfPresent(projectLevelCacheKey); | |
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| You can initialise the SDK with something like this: | ||
| The `cache` option in the `Flagsmith` constructor accepts a cache implementation. This cache must implement the | ||
| {/* prettier-ignore */}<a href={`https://www.tsdocs.dev/docs/flagsmith-nodejs/${NodejsVersion()}/interfaces/FlagsmithCache.html`}>FlagsmithCache</a> | ||
| interface. | ||
|
|
||
| ```javascript | ||
| flagsmith.init({ | ||
| cache: { | ||
| has:(key)=> return Promise.resolve(!!cache[key]) , // true | false | ||
| get: (k)=> cache[k] // return flags or flags for user | ||
| set: (k,v)=> cache[k] = v // gets called if has returns false with response from API for Identify or getFlags | ||
| } | ||
| }) | ||
| ``` | ||
| For example, this cache implementation uses Redis as a backing store: | ||
|
|
||
| The core concept is that if `has` returns false, the SDK will make the required API calls under the hood. The keys are | ||
| either `flags` or `flags_traits-${identity}`. | ||
|
|
||
| An example of a concrete implemention is below. | ||
|
|
||
| ```javascript | ||
| const flagsmith = require('flagsmith-nodejs'); | ||
| const redis = require('redis'); | ||
| ```typescript | ||
| import { Flagsmith } from 'flagsmith-nodejs'; | ||
| import type { BaseOfflineHandler, EnvironmentModel, Flags, FlagsmithCache } from 'flagsmith-nodejs'; | ||
| import * as redis from 'redis'; | ||
|
|
||
| const redisClient = redis.createClient({ | ||
| host: 'localhost', | ||
| port: 6379, | ||
| url: 'localhost:6379', | ||
| }); | ||
|
|
||
| flagsmith.init({ | ||
| environmentID: 'FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY', | ||
| cache: { | ||
| has: (key) => | ||
| new Promise((resolve, reject) => { | ||
| redisClient.exists(key, (err, reply) => { | ||
| console.log('check ' + key + ' from cache', err, reply); | ||
| resolve(reply === 1); | ||
| }); | ||
| }), | ||
| get: (key) => | ||
| new Promise((resolve) => { | ||
| redisClient.get(key, (err, cacheValue) => { | ||
| console.log('get ' + key + ' from cache'); | ||
| resolve(cacheValue && JSON.parse(cacheValue)); | ||
| }); | ||
| }), | ||
| set: (key, value) => | ||
| new Promise((resolve) => { | ||
| // Expire the key after 60 seconds | ||
| redisClient.set(key, JSON.stringify(value), 'EX', 60, (err, reply) => { | ||
| console.log('set ' + key + ' to cache', err); | ||
| resolve(); | ||
| }); | ||
| }), | ||
| const redisFlagsmithCache = { | ||
| async get(key: string): Promise<Flags | undefined> { | ||
| const cachedValue = await redisClient.get(key); | ||
| return Promise.resolve(cachedValue && JSON.parse(cachedValue)); | ||
| }, | ||
| }); | ||
| async set(key: string, value: Flags): Promise<void> { | ||
| await redisClient.set(key, JSON.stringify(value), { EX: 60 }); | ||
| return Promise.resolve(); | ||
| }, | ||
| } satisfies FlagsmithCache; | ||
|
|
||
| router.get('/', function (req, res, next) { | ||
| flagsmith.getValue('background_colour').then((value) => { | ||
| res.render('index', { | ||
| title: value, | ||
| }); | ||
| }); | ||
| const flagsmith = new Flagsmith({ | ||
| environmentKey: 'ser...', | ||
| cache: redisFlagsmithCache, | ||
| }); | ||
| ``` | ||
|
|
||
|
|
@@ -2037,7 +2024,7 @@ https://github.com/Flagsmith/flagsmith-java-client | |
| https://github.com/Flagsmith/flagsmith-dotnet-client | ||
|
|
||
| </TabItem> | ||
| <TabItem value="nodejs" label="NodeJS"> | ||
| <TabItem value="nodejs" label="Node.js"> | ||
|
|
||
| https://github.com/Flagsmith/flagsmith-nodejs-client | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're changing this here, we should also do it here for consistency.