Skip to content

Commit

Permalink
Add js, flutter and nextjs self hosted api
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg committed Jul 9, 2024
1 parent 56089fb commit 26c517b
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 15 deletions.
13 changes: 11 additions & 2 deletions frontend/common/code-help/create-user/create-user-flutter.js
Original file line number Diff line number Diff line change
@@ -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>[
Flag.seed('feature', enabled: true),
Expand All @@ -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>[
// Flag.seed('feature', enabled: true),
Expand Down
7 changes: 6 additions & 1 deletion frontend/common/code-help/create-user/create-user-js.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Constants from 'common/constants'
module.exports = (
envId,
{
Expand All @@ -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) => { /* ... */ },
Expand Down
13 changes: 11 additions & 2 deletions frontend/common/code-help/create-user/create-user-next.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Constants from 'common/constants'
module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, TRAIT_NAME, USER_ID },
Expand Down Expand Up @@ -25,7 +26,11 @@ export default function App({ Component, pageProps, flagsmithState } {
&lt;FlagsmithProvider
serverState={flagsmithState}
options={{
environmentID: '${envId}',
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
: ''
}
}}
flagsmith={flagsmith}&gt;
&lt;Component {...pageProps} />
Expand All @@ -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('${
Expand Down
13 changes: 11 additions & 2 deletions frontend/common/code-help/init/init-flutter.js
Original file line number Diff line number Diff line change
@@ -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>[
Flag.seed('feature', enabled: true),
Expand All @@ -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>[
// Flag.seed('feature', enabled: true),
Expand Down
3 changes: 2 additions & 1 deletion frontend/common/code-help/init/init-js.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Constants from 'common/constants'
module.exports = (
envId,
{ FEATURE_FUNCTION, FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, NPM_CLIENT },
customFeature,
) => `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;
Expand Down
13 changes: 11 additions & 2 deletions frontend/common/code-help/init/init-next.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Constants from 'common/constants'
module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, NPM_CLIENT },
Expand All @@ -10,7 +11,11 @@ export default function App({ Component, pageProps, flagsmithState } {
&lt;FlagsmithProvider
serverState={flagsmithState}
options={{
environmentID: '${envId}',
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
: ''
}
}}
flagsmith={flagsmith}&gt;
&lt;Component {...pageProps} />
Expand All @@ -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() }
}
Expand Down
8 changes: 7 additions & 1 deletion frontend/common/code-help/traits/traits-js.js
Original file line number Diff line number Diff line change
@@ -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) => { /* ... */ },
Expand Down
13 changes: 11 additions & 2 deletions frontend/common/code-help/traits/traits-next.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Constants from 'common/constants'
module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, TRAIT_NAME },
Expand Down Expand Up @@ -33,7 +34,11 @@ export default function App({ Component, pageProps, flagsmithState } {
&lt;FlagsmithProvider
serverState={flagsmithState}
options={{
environmentID: '${envId}',
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
: ''
}
}}
flagsmith={flagsmith}&gt;
&lt;Component {...pageProps} />
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down

0 comments on commit 26c517b

Please sign in to comment.