From 8ae2de8a3ea8dc4a80e483e1c065690ca080bea9 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 3 Jan 2022 10:12:13 +0100 Subject: [PATCH 1/5] docs: add steps for running the proxy in node with custom strats. --- .../how-to/how-to-use-custom-strategies.md | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/website/docs/how-to/how-to-use-custom-strategies.md b/website/docs/how-to/how-to-use-custom-strategies.md index 84a6ac51e5..bcc7e94cb2 100644 --- a/website/docs/how-to/how-to-use-custom-strategies.md +++ b/website/docs/how-to/how-to-use-custom-strategies.md @@ -83,7 +83,11 @@ The steps to implement a custom strategy for your client depend on the kind of c ### Option B: Implement the strategy for a front-end client SDK {#step-3-b} -Front-end client SDK don't evaluate strategies directly, so you need to implement the **custom strategy in the [Unleash Proxy](../sdks/unleash-proxy.md)**. The below steps assume you're running the Unleash Proxy as a Docker container. If you're running the Unleash Proxy via Node directly, follow the steps for [server-side SDKs](#step-3-a "Step 3 option A: implement the strategy for a server-side client SDK") instead. +Front-end client SDK don't evaluate strategies directly, so you need to implement the **custom strategy in the [Unleash Proxy](../sdks/unleash-proxy.md)**. Depending on how you run the Unleash Proxy, follow one of the below series of steps: +- If you're running the Unleash Proxy as a Docker container, refer to the [steps for using a containerized Proxy](#step-3-b-docker). +- If you're using the Unleash Proxy via Node.js, refer to the [steps for using custom strategies via Node.js](#step-3-b-node). + +#### With a containerized proxy {#step-3-b-docker} Strategies are stored in separate JavaScript files and loaded into the container at startup. Refer to [the Unleash Proxy documentation](../sdks/unleash-proxy.md) for a full overview of all the options. @@ -126,3 +130,64 @@ Strategies are stored in separate JavaScript files and loaded into the container # highlight-end -p 3000:3000 --network unleash unleashorg/unleash-proxy ``` + +#### When running the proxy with node {#step-3-b-node} + +The Unleash Proxy accepts a `customStrategies` property as part of its initialization options. Use this to pass it initialized strategies. + + + + +1. **Install the `unleash-client` package**. You'll need this to implement the custom strategy: + + ``` shell + npm install unleash-client + ``` + +2. **Implement your strategy**. You can import it from a different file or put it in the same file as the Proxy initialization. For instance, a `TimeStampStrategy` could look like this: + + ``` js + const { Strategy } = require('unleash-client'); + + class TimeStampStrategy extends Strategy { + constructor() { + super('TimeStamp'); + } + + isEnabled(parameters, context) { + return Date.parse(parameters.enableAfter) > Date.now(); + } + } + ``` + +3. **Pass the strategy to the Proxy Client** using the **`customStrategies`** option. A full code example: + +``` javascript +const { createApp } = require('@unleash/proxy'); +const { Strategy } = require('unleash-client'); + +class TimeStampStrategy extends Strategy { + constructor() { + super('TimeStamp'); + } + + isEnabled(parameters, context) { + return Date.parse(parameters.enableAfter) > Date.now(); + } +} + +const port = 3000; + +const app = createApp({ + unleashUrl: 'https://app.unleash-hosted.com/demo/api/', + unleashApiToken: '*:default.56907a2fa53c1d16101d509a10b78e36190b0f918d9f122d', + proxySecrets: ['proxy-secret', 'another-proxy-secret', 's1'], + refreshInterval: 1000, + customStrategies: [new TimeStampStrategy()] +}); + +app.listen(port, () => + // eslint-disable-next-line no-console + console.log(`Unleash Proxy listening on http://localhost:${port}/proxy`), +); +``` From bdbd659a71ac0f54fcb7143b34a69e67ba877bf2 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 3 Jan 2022 10:19:04 +0100 Subject: [PATCH 2/5] docs(chore): indent code block properly. --- .../how-to/how-to-use-custom-strategies.md | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/website/docs/how-to/how-to-use-custom-strategies.md b/website/docs/how-to/how-to-use-custom-strategies.md index bcc7e94cb2..6d965d1ac0 100644 --- a/website/docs/how-to/how-to-use-custom-strategies.md +++ b/website/docs/how-to/how-to-use-custom-strategies.md @@ -162,32 +162,32 @@ The Unleash Proxy accepts a `customStrategies` property as part of its initializ 3. **Pass the strategy to the Proxy Client** using the **`customStrategies`** option. A full code example: -``` javascript -const { createApp } = require('@unleash/proxy'); -const { Strategy } = require('unleash-client'); + ``` javascript + const { createApp } = require('@unleash/proxy'); + const { Strategy } = require('unleash-client'); -class TimeStampStrategy extends Strategy { - constructor() { - super('TimeStamp'); - } + class TimeStampStrategy extends Strategy { + constructor() { + super('TimeStamp'); + } - isEnabled(parameters, context) { - return Date.parse(parameters.enableAfter) > Date.now(); - } -} + isEnabled(parameters, context) { + return Date.parse(parameters.enableAfter) > Date.now(); + } + } -const port = 3000; + const port = 3000; -const app = createApp({ - unleashUrl: 'https://app.unleash-hosted.com/demo/api/', - unleashApiToken: '*:default.56907a2fa53c1d16101d509a10b78e36190b0f918d9f122d', - proxySecrets: ['proxy-secret', 'another-proxy-secret', 's1'], - refreshInterval: 1000, - customStrategies: [new TimeStampStrategy()] -}); + const app = createApp({ + unleashUrl: 'https://app.unleash-hosted.com/demo/api/', + unleashApiToken: '*:default.56907a2fa53c1d16101d509a10b78e36190b0f918d9f122d', + proxySecrets: ['proxy-secret', 'another-proxy-secret', 's1'], + refreshInterval: 1000, + customStrategies: [new TimeStampStrategy()] + }); -app.listen(port, () => - // eslint-disable-next-line no-console - console.log(`Unleash Proxy listening on http://localhost:${port}/proxy`), -); -``` + app.listen(port, () => + // eslint-disable-next-line no-console + console.log(`Unleash Proxy listening on http://localhost:${port}/proxy`), + ); + ``` From dd975ec344842e5e8a9d0dd5911f3be6e52e6746 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 3 Jan 2022 10:26:29 +0100 Subject: [PATCH 3/5] docs: highlight `customStrategies` option. --- website/docs/how-to/how-to-use-custom-strategies.md | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/how-to/how-to-use-custom-strategies.md b/website/docs/how-to/how-to-use-custom-strategies.md index 6d965d1ac0..4a4c62a536 100644 --- a/website/docs/how-to/how-to-use-custom-strategies.md +++ b/website/docs/how-to/how-to-use-custom-strategies.md @@ -183,6 +183,7 @@ The Unleash Proxy accepts a `customStrategies` property as part of its initializ unleashApiToken: '*:default.56907a2fa53c1d16101d509a10b78e36190b0f918d9f122d', proxySecrets: ['proxy-secret', 'another-proxy-secret', 's1'], refreshInterval: 1000, + // highlight-next-line customStrategies: [new TimeStampStrategy()] }); From 5a307061501e8fe89b87b33bc91ed15d78988aa3 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 3 Jan 2022 13:08:25 +0100 Subject: [PATCH 4/5] docs(fix): fix comparison operator from `>` to `<` --- website/docs/how-to/how-to-use-custom-strategies.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/website/docs/how-to/how-to-use-custom-strategies.md b/website/docs/how-to/how-to-use-custom-strategies.md index 4a4c62a536..34d2e59a76 100644 --- a/website/docs/how-to/how-to-use-custom-strategies.md +++ b/website/docs/how-to/how-to-use-custom-strategies.md @@ -43,7 +43,7 @@ The steps to implement a custom strategy for your client depend on the kind of c } isEnabled(parameters, context) { - return Date.parse(parameters.enableAfter) > Date.now(); + return Date.parse(parameters.enableAfter) < Date.now(); } } ``` @@ -61,7 +61,7 @@ The steps to implement a custom strategy for your client depend on the kind of c } isEnabled(parameters, context) { - return Date.parse(parameters.enableAfter) > Date.now(); + return Date.parse(parameters.enableAfter) < Date.now(); } } @@ -110,7 +110,7 @@ Strategies are stored in separate JavaScript files and loaded into the container } isEnabled(parameters, context) { - return Date.parse(parameters.enableAfter) > Date.now(); + return Date.parse(parameters.enableAfter) < Date.now(); } } @@ -155,7 +155,7 @@ The Unleash Proxy accepts a `customStrategies` property as part of its initializ } isEnabled(parameters, context) { - return Date.parse(parameters.enableAfter) > Date.now(); + return Date.parse(parameters.enableAfter) < Date.now(); } } ``` @@ -172,7 +172,7 @@ The Unleash Proxy accepts a `customStrategies` property as part of its initializ } isEnabled(parameters, context) { - return Date.parse(parameters.enableAfter) > Date.now(); + return Date.parse(parameters.enableAfter) < Date.now(); } } From 0344dc79e4b787e5c5ac631eadc8c4cbfc3b6704 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 3 Jan 2022 13:55:45 +0100 Subject: [PATCH 5/5] docs(typo): pluralize SDK -> SDKs --- website/docs/how-to/how-to-use-custom-strategies.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/how-to/how-to-use-custom-strategies.md b/website/docs/how-to/how-to-use-custom-strategies.md index 34d2e59a76..4059a2c21a 100644 --- a/website/docs/how-to/how-to-use-custom-strategies.md +++ b/website/docs/how-to/how-to-use-custom-strategies.md @@ -83,7 +83,7 @@ The steps to implement a custom strategy for your client depend on the kind of c ### Option B: Implement the strategy for a front-end client SDK {#step-3-b} -Front-end client SDK don't evaluate strategies directly, so you need to implement the **custom strategy in the [Unleash Proxy](../sdks/unleash-proxy.md)**. Depending on how you run the Unleash Proxy, follow one of the below series of steps: +Front-end client SDKs don't evaluate strategies directly, so you need to implement the **custom strategy in the [Unleash Proxy](../sdks/unleash-proxy.md)**. Depending on how you run the Unleash Proxy, follow one of the below series of steps: - If you're running the Unleash Proxy as a Docker container, refer to the [steps for using a containerized Proxy](#step-3-b-docker). - If you're using the Unleash Proxy via Node.js, refer to the [steps for using custom strategies via Node.js](#step-3-b-node).