From 8d4434787e2d8e658be00c0e7341659caa9a9bcd Mon Sep 17 00:00:00 2001 From: Nicky <31034418+nickybondarenko@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:25:17 +0200 Subject: [PATCH] refactor: Standardise example apps (#189) --- .gitignore | 2 +- examples/node/package.json | 2 +- examples/node/src/index.js | 7 +++++-- examples/nodeCJS/src/index.js | 7 +++++-- examples/react18/.env | 1 + examples/react18/src/App.tsx | 9 +++++++-- 6 files changed, 20 insertions(+), 8 deletions(-) create mode 120000 examples/react18/.env diff --git a/.gitignore b/.gitignore index 71381068..a1255fd4 100644 --- a/.gitignore +++ b/.gitignore @@ -15,9 +15,9 @@ examples/*/build examples/*/.next examples/*/out examples/*/*.log* -examples/*/.env* examples/*/*.tsbuildinfo examples/*/next-env.d.ts +examples/.env # logs npm-debug.log* diff --git a/examples/node/package.json b/examples/node/package.json index 790ff0d4..f079dace 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -12,7 +12,7 @@ "@spotify-confidence/sdk": "workspace:*" }, "scripts": { - "dev": "node src/index.js" + "dev": "node --env-file=../.env src/index.js" }, "private": true } diff --git a/examples/node/src/index.js b/examples/node/src/index.js index e0494af0..22152c39 100644 --- a/examples/node/src/index.js +++ b/examples/node/src/index.js @@ -1,7 +1,10 @@ import { Confidence } from '@spotify-confidence/sdk'; +if (!process.env.CLIENT_SECRET) { + console.log('CLIENT_SECRET is not set inb .env'); +} const confidence = Confidence.create({ - clientSecret: 'RxDVTrXvc6op1XxiQ4OaR31dKbJ39aYV', + clientSecret: process.env.CLIENT_SECRET, fetchImplementation: fetch, timeout: 1000, logger: console, @@ -11,7 +14,7 @@ main(); async function main() { confidence.subscribe(state => console.log('state:', state)); confidence.setContext({ targeting_key: 'user-a' }); - const fe = confidence.evaluateFlag('web-sdk-e2e-flag.int', 0); + const fe = confidence.evaluateFlag('tutorial-feature.title', 'Default'); console.log(fe); diff --git a/examples/nodeCJS/src/index.js b/examples/nodeCJS/src/index.js index 42ec9ae6..9b996d81 100644 --- a/examples/nodeCJS/src/index.js +++ b/examples/nodeCJS/src/index.js @@ -1,12 +1,15 @@ const { OpenFeature } = require('@openfeature/server-sdk'); +if (!process.env.CLIENT_SECRET) { + console.log('CLIENT_SECRET is not set inb .env'); +} main(); async function main() { const { createConfidenceServerProvider } = await import('@spotify-confidence/openfeature-server-provider'); const provider = createConfidenceServerProvider({ - clientSecret: 'RxDVTrXvc6op1XxiQ4OaR31dKbJ39aYV', + clientSecret: process.env.CLIENT_SECRET, region: 'eu', fetchImplementation: fetch, timeout: 1000, @@ -17,7 +20,7 @@ async function main() { const client = OpenFeature.getClient(); client - .getBooleanValue('web-sdk-e2e-flag.bool', false, { + .getStringValue('tutorial-feature.title', 'Default', { targetingKey: `user-${Math.random()}`, }) .then(result => { diff --git a/examples/react18/.env b/examples/react18/.env new file mode 120000 index 00000000..4a82335f --- /dev/null +++ b/examples/react18/.env @@ -0,0 +1 @@ +../.env \ No newline at end of file diff --git a/examples/react18/src/App.tsx b/examples/react18/src/App.tsx index 98217f92..abc0d3a9 100644 --- a/examples/react18/src/App.tsx +++ b/examples/react18/src/App.tsx @@ -15,8 +15,13 @@ const handleFailRequestsOnChange = (e: React.ChangeEvent) => { state.failRequests = e.target.checked; }; +if (!process.env.REACT_APP_CLIENT_SECRET) { + console.error('REACT_APP_CLIENT_SECRET not set in .env'); + process.exit(1); +} + const confidence = Confidence.create({ - clientSecret: 'RxDVTrXvc6op1XxiQ4OaR31dKbJ39aYV', + clientSecret: process.env.REACT_APP_CLIENT_SECRET, environment: 'client', timeout: 3000, logger: console, @@ -89,7 +94,7 @@ function Level({ name, children }: { name: string; children?: React.ReactNode }) function Flags() { const confidence = useConfidence(); - const flagData = JSON.stringify(confidence.useEvaluateFlag('web-sdk-e2e-flag.str', 'default'), null, ' '); + const flagData = JSON.stringify(confidence.useEvaluateFlag('tutorial-feature.title', 'Default'), null, ' '); // const flagData = useDeferredValue(confidence.useFlag('web-sdk-e2e-flag.str', 'default')); return (