Skip to content

Commit

Permalink
chore: flagsmith on flagsmith sdk urls (#4436)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg authored Aug 14, 2024
1 parent a5197d1 commit 9e25990
Show file tree
Hide file tree
Showing 38 changed files with 244 additions and 64 deletions.
2 changes: 1 addition & 1 deletion frontend/common/code-help/create-user/create-user-curl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (envId, { USER_ID }, userId) => `// Identify/create user
curl -i 'https://edge.api.flagsmith.com/api/v1/identities/?identifier=${
curl -i '${Project.flagsmithClientAPI}identities/?identifier=${
userId || USER_ID
}' \\
-H 'x-environment-key: ${envId}'
Expand Down
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-ios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, USER_ID },
Expand All @@ -8,7 +10,10 @@ func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Flagsmith.shared.apiKey = "${envId}"
Flagsmith.shared.apiKey = "${envId}"${
Constants.isCustomFlagsmithUrl &&
`\n Flagsmith.shared.baseURL = "${Project.flagsmithClientAPI}"\n`
}
// This will create a user in the dashboard if they don't already exist
// Check for a feature
Expand Down
9 changes: 8 additions & 1 deletion frontend/common/code-help/create-user/create-user-java.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, LIB_NAME_JAVA, USER_ID },
userId,
) => `${LIB_NAME_JAVA} ${LIB_NAME} = ${LIB_NAME_JAVA}
.newBuilder()
.setApiKey("${envId}")
.setApiKey("${envId}")${
Constants.isCustomFlagsmithUrl &&
`\n .withConfiguration(FlagsmithConfig.builder()
.baseUri("${Project.flagsmithClientAPI}")
.build())`
}
.build();
// Identify the user
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
14 changes: 10 additions & 4 deletions frontend/common/code-help/create-user/create-user-node.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, USER_ID },
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, NPM_NODE_CLIENT, USER_ID },
userId,
) => `const Flagsmith = require('flagsmith-nodejs');
) => `import Flagsmith from "${NPM_NODE_CLIENT}"; // Add this line if you're using ${LIB_NAME} via npm
const flagsmith = new Flagsmith(
const ${LIB_NAME} = new Flagsmith({${
Constants.isCustomFlagsmithUrl &&
`\n apiUrl: '${Project.flagsmithClientAPI}',`
}
environmentKey: '${envId}'
);
});
// Identify the user
const flags = await flagsmith.getIdentityFlags('${
Expand Down
6 changes: 5 additions & 1 deletion frontend/common/code-help/create-user/create-user-php.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, USER_ID },
userId,
) => `use Flagsmith\\Flagsmith;
$flagsmith = new Flagsmith('${envId}');
$flagsmith = new Flagsmith('${envId}'${
Constants.isCustomFlagsmithUrl && `,\n '${Project.flagsmithClientAPI}'\n`
});
// Identify the user
$flags = $flagsmith->getIdentityFlags('${userId}', $traits);
Expand Down
7 changes: 6 additions & 1 deletion frontend/common/code-help/create-user/create-user-python.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, USER_ID },
userId,
) => `from flagsmith import Flagsmith
flagsmith = Flagsmith(environment_key="${envId}")
flagsmith = Flagsmith(environment_key="${envId}"${
Constants.isCustomFlagsmithUrl &&
`,\n api_url="${Project.flagsmithClientAPI}"\n`
})
# Identify the user
identity_flags = flagsmith.get_identity_flags(identifier="${
Expand Down
16 changes: 11 additions & 5 deletions frontend/common/code-help/create-user/create-user-react.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, TRAIT_NAME, USER_ID },
{ FEATURE_NAME, FEATURE_NAME_ALT, NPM_CLIENT, TRAIT_NAME, USER_ID },
userId,
) => `
// Option 1: Initialise with an identity
import { FlagsmithProvider } from 'flagsmith/react';
import { FlagsmithProvider } from '${NPM_CLIENT}/react';
export default function App() {
return (
&lt;FlagsmithProvider
options={{
environmentID: '${envId}',
environmentID: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n api: '${Project.flagsmithClientAPI}',`
: ''
}
identity: '${userId || USER_ID}',
traits: {${TRAIT_NAME}: 21},
}}
Expand All @@ -22,8 +28,8 @@ export default function App() {
// Option 2: Identify after initialising
import flagsmith from '${LIB_NAME}';
import { useFlags, useFlagsmith } from 'flagsmith/react';
import flagsmith from '${NPM_CLIENT}';
import { useFlags, useFlagsmith } from '${NPM_CLIENT}/react';
export default function HomePage() {
const flags = useFlags(['${FEATURE_NAME}','${FEATURE_NAME_ALT}']); // only causes re-render if specified flag values / traits change
Expand Down
8 changes: 6 additions & 2 deletions frontend/common/code-help/create-user/create-user-ruby.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, USER_ID },
userId,
) => `require "flagsmith"
$flagsmith = Flagsmith::Client.new(
environment_key: '${envId}'
)
environment_key="${envId}"${
Constants.isCustomFlagsmithUrl &&
`,\n api_url="${Project.flagsmithClientAPI}"\n`
})
// Identify the user
$flags = $flagsmith.get_identity_flags('${userId || USER_ID}')
Expand Down
7 changes: 6 additions & 1 deletion frontend/common/code-help/create-user/create-user-rust.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, USER_ID },
userId,
) => `
use flagsmith::{Flag, Flagsmith, FlagsmithOptions};
let options = FlagsmithOptions {..Default::default()};
let options = FlagsmithOptions {${
Constants.isCustomFlagsmithUrl &&
`api_url: "${Project.flagsmithClientAPI}".to_string(),\n`
}..Default::default()};
let flagsmith = Flagsmith::new(
"${envId}".to_string(),
options,
Expand Down
2 changes: 1 addition & 1 deletion frontend/common/code-help/init/init-curl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (envId) => `
curl -i 'https://edge.api.flagsmith.com/api/v1/flags/' \\
curl -i '${Project.flagsmithClientAPI}flags/' \\
-H 'x-environment-key: ${envId}'
`
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
7 changes: 6 additions & 1 deletion frontend/common/code-help/init/init-go.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 }, customFeature) => `
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialise the Flagsmith client
client := flagsmith.NewClient('${envId}', flagsmith.WithContext(ctx))
client := flagsmith.NewClient('${envId}',${
Constants.isCustomFlagsmithUrl &&
`\nflagsmith.WithBaseURL("${Project.flagsmithClientAPI}"),\n`
}flagsmith.WithContext(ctx))
// The method below triggers a network request
flags, _ := client.GetEnvironmentFlags()
Expand Down
7 changes: 6 additions & 1 deletion frontend/common/code-help/init/init-ios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT },
Expand All @@ -6,7 +8,10 @@ module.exports = (
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Flagsmith.shared.apiKey = "${envId}"
Flagsmith.shared.apiKey = "${envId}"${
Constants.isCustomFlagsmithUrl &&
`\n Flagsmith.shared.baseURL = "${Project.flagsmithClientAPI}"\n`
}
// Check for a feature
Flagsmith.shared
.hasFeatureFlag(withID: "${FEATURE_NAME}", forIdentity: nil) { (result) in
Expand Down
8 changes: 7 additions & 1 deletion frontend/common/code-help/init/init-java.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import Constants from 'common/constants'
module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT, LIB_NAME, LIB_NAME_JAVA },
customFeature,
) => `${LIB_NAME_JAVA} ${LIB_NAME} = ${LIB_NAME_JAVA}
.newBuilder()
.setApiKey("${envId}")
.setApiKey("${envId}")${
Constants.isCustomFlagsmithUrl &&
`\n .withConfiguration(FlagsmithConfig.builder()
.baseUri("${Project.flagsmithClientAPI}")
.build())`
}
.build();
Flags flags = flagsmith.getEnvironmentFlags();
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
Loading

0 comments on commit 9e25990

Please sign in to comment.