From 26c517b17d74d530f58cff4719e76e67c4283c9b Mon Sep 17 00:00:00 2001 From: kyle-ssg Date: Tue, 9 Jul 2024 13:50:09 +0100 Subject: [PATCH] Add js, flutter and nextjs self hosted api --- .../code-help/create-user/create-user-flutter.js | 13 +++++++++++-- .../common/code-help/create-user/create-user-js.js | 7 ++++++- .../code-help/create-user/create-user-next.js | 13 +++++++++++-- frontend/common/code-help/init/init-flutter.js | 13 +++++++++++-- frontend/common/code-help/init/init-js.js | 3 ++- frontend/common/code-help/init/init-next.js | 13 +++++++++++-- frontend/common/code-help/traits/traits-js.js | 8 +++++++- frontend/common/code-help/traits/traits-next.js | 13 +++++++++++-- frontend/common/constants.ts | 4 ++-- 9 files changed, 72 insertions(+), 15 deletions(-) diff --git a/frontend/common/code-help/create-user/create-user-flutter.js b/frontend/common/code-help/create-user/create-user-flutter.js index a6f5f5aae9e7..c639c79ed466 100644 --- a/frontend/common/code-help/create-user/create-user-flutter.js +++ b/frontend/common/code-help/create-user/create-user-flutter.js @@ -1,9 +1,14 @@ +import Constants from 'common/constants' module.exports = ( envId, { FEATURE_NAME, FEATURE_NAME_ALT, USER_ID }, userId, ) => `final flagsmithClient = FlagsmithClient( - apiKey: '${envId}' + apiKey: '${envId}',${ + Constants.isCustomFlagsmithUrl + ? `\n baseURI: '${Project.flagsmithClientAPI}',` + : '' +} config: config, seeds: [ Flag.seed('feature', enabled: true), @@ -12,7 +17,11 @@ module.exports = ( //if you prefer async initialization then you should use //final flagsmithClient = await FlagsmithClient.init( -// apiKey: 'YOUR_ENV_API_KEY', +// apiKey: '${envId}',${ + Constants.isCustomFlagsmithUrl + ? `\n// baseURI: '${Project.flagsmithClientAPI}',` + : '' +} // config: config, // seeds: [ // Flag.seed('feature', enabled: true), diff --git a/frontend/common/code-help/create-user/create-user-js.js b/frontend/common/code-help/create-user/create-user-js.js index cf1ae6d72b25..eaa8aad92bf1 100644 --- a/frontend/common/code-help/create-user/create-user-js.js +++ b/frontend/common/code-help/create-user/create-user-js.js @@ -1,3 +1,4 @@ +import Constants from 'common/constants' module.exports = ( envId, { @@ -13,7 +14,11 @@ module.exports = ( // Option 1: initialise with an identity and traits ${LIB_NAME}.init({ - environmentID: "${envId}", + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} identity: "${userId || USER_ID}", traits: { "${TRAIT_NAME}": 21 }, onChange: (oldFlags, params) => { /* ... */ }, diff --git a/frontend/common/code-help/create-user/create-user-next.js b/frontend/common/code-help/create-user/create-user-next.js index 36ee38e9313c..796ace1b04c1 100644 --- a/frontend/common/code-help/create-user/create-user-next.js +++ b/frontend/common/code-help/create-user/create-user-next.js @@ -1,3 +1,4 @@ +import Constants from 'common/constants' module.exports = ( envId, { FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, TRAIT_NAME, USER_ID }, @@ -25,7 +26,11 @@ export default function App({ Component, pageProps, flagsmithState } { <FlagsmithProvider serverState={flagsmithState} options={{ - environmentID: '${envId}', + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} }} flagsmith={flagsmith}> <Component {...pageProps} /> @@ -36,7 +41,11 @@ export default function App({ Component, pageProps, flagsmithState } { MyApp.getInitialProps = async () => { // calls page's \`getInitialProps\` and fills \`appProps.pageProps\` await flagsmith.init({ // fetches flags on the server - environmentID, + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} preventFetch: true }); await flagsmith.identify('${ diff --git a/frontend/common/code-help/init/init-flutter.js b/frontend/common/code-help/init/init-flutter.js index 295807e415de..bcd3c531b0df 100644 --- a/frontend/common/code-help/init/init-flutter.js +++ b/frontend/common/code-help/init/init-flutter.js @@ -1,10 +1,15 @@ +import Constants from 'common/constants' module.exports = ( envId, { FEATURE_NAME, FEATURE_NAME_ALT }, ) => `//In your application, initialise the Flagsmith client with your API key: final flagsmithClient = FlagsmithClient( - apiKey: '${envId}' + apiKey: '${envId}',${ + Constants.isCustomFlagsmithUrl + ? `\n baseURI: '${Project.flagsmithClientAPI}',` + : '' +} config: config, seeds: [ Flag.seed('feature', enabled: true), @@ -13,7 +18,11 @@ final flagsmithClient = FlagsmithClient( //if you prefer async initialization then you should use //final flagsmithClient = await FlagsmithClient.init( -// apiKey: 'YOUR_ENV_API_KEY', +// apiKey: '${envId}',${ + Constants.isCustomFlagsmithUrl + ? `\n// baseURI: '${Project.flagsmithClientAPI}',` + : '' +} // config: config, // seeds: [ // Flag.seed('feature', enabled: true), diff --git a/frontend/common/code-help/init/init-js.js b/frontend/common/code-help/init/init-js.js index 7dbfa8b11008..def8e9a2867e 100644 --- a/frontend/common/code-help/init/init-js.js +++ b/frontend/common/code-help/init/init-js.js @@ -1,3 +1,4 @@ +import Constants from 'common/constants' module.exports = ( envId, { FEATURE_FUNCTION, FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, NPM_CLIENT }, @@ -5,7 +6,7 @@ module.exports = ( ) => `import ${LIB_NAME} from "${NPM_CLIENT}"; // Add this line if you're using ${LIB_NAME} via npm ${LIB_NAME}.init({ - environmentID: "${envId}", + environmentID: "${envId}",${Constants.isCustomFlagsmithUrl ? `\n api: "${Project.flagsmithClientAPI}",` : ''} onChange: (oldFlags, params) => { // Occurs whenever flags are changed // Determines if the update came from the server or local cached storage const { isFromServer } = params; diff --git a/frontend/common/code-help/init/init-next.js b/frontend/common/code-help/init/init-next.js index b5c679c899f9..07e0047ad0e9 100644 --- a/frontend/common/code-help/init/init-next.js +++ b/frontend/common/code-help/init/init-next.js @@ -1,3 +1,4 @@ +import Constants from 'common/constants' module.exports = ( envId, { FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, NPM_CLIENT }, @@ -10,7 +11,11 @@ export default function App({ Component, pageProps, flagsmithState } { <FlagsmithProvider serverState={flagsmithState} options={{ - environmentID: '${envId}', + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} }} flagsmith={flagsmith}> <Component {...pageProps} /> @@ -20,7 +25,11 @@ export default function App({ Component, pageProps, flagsmithState } { App.getInitialProps = async () => { await flagsmith.init({ // fetches flags on the server and passes them to the App - environmentID, + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} }); return { flagsmithState: flagsmith.getState() } } diff --git a/frontend/common/code-help/traits/traits-js.js b/frontend/common/code-help/traits/traits-js.js index fb3a03819957..11f8708cf5a3 100644 --- a/frontend/common/code-help/traits/traits-js.js +++ b/frontend/common/code-help/traits/traits-js.js @@ -1,10 +1,16 @@ +import Constants from 'common/constants' + module.exports = ( envId, { LIB_NAME, TRAIT_NAME, USER_ID }, userId, ) => `// Option 1: initialise with an identity and traits ${LIB_NAME}.init({ - environmentID: "${envId}", + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} identity: "${userId || USER_ID}", traits: { "${TRAIT_NAME}": 21 }, onChange: (oldFlags, params) => { /* ... */ }, diff --git a/frontend/common/code-help/traits/traits-next.js b/frontend/common/code-help/traits/traits-next.js index 9db7062b7884..11618d3b1752 100644 --- a/frontend/common/code-help/traits/traits-next.js +++ b/frontend/common/code-help/traits/traits-next.js @@ -1,3 +1,4 @@ +import Constants from 'common/constants' module.exports = ( envId, { FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, TRAIT_NAME }, @@ -33,7 +34,11 @@ export default function App({ Component, pageProps, flagsmithState } { <FlagsmithProvider serverState={flagsmithState} options={{ - environmentID: '${envId}', + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} }} flagsmith={flagsmith}> <Component {...pageProps} /> @@ -44,7 +49,11 @@ export default function App({ Component, pageProps, flagsmithState } { MyApp.getInitialProps = async () => { // calls page's \`getInitialProps\` and fills \`appProps.pageProps\` await flagsmith.init({ // fetches flags on the server - environmentID, + environmentID: "${envId}",${ + Constants.isCustomFlagsmithUrl + ? `\n api: "${Project.flagsmithClientAPI}",` + : '' +} preventFetch: true }); await flagsmith.identify('${USER_ID}', {${TRAIT_NAME}: 21}); // Will hydrate the app with the user's flags diff --git a/frontend/common/constants.ts b/frontend/common/constants.ts index 2323d7118aa5..84897e5c43b6 100644 --- a/frontend/common/constants.ts +++ b/frontend/common/constants.ts @@ -439,8 +439,8 @@ export default { githubIssue: 'GitHub Issue', githubPR: 'Github PR', }, - isCustomFlagsmithUrl: - Project.flagsmithClientAPI !== 'https://edge.api.flagsmith.com/api/v1/', + isCustomFlagsmithUrl: true, + // Project.flagsmithClientAPI !== 'https://edge.api.flagsmith.com/api/v1/', modals: { 'PAYMENT': 'Payment Modal', },