Skip to content

Commit

Permalink
feat(flags): Add remote config feature flag method that bypasses cach…
Browse files Browse the repository at this point in the history
…ed values (#1729)

* feat(flags): Add remote config feature flag method that bypasses cached values

* Update src/posthog-featureflags.ts

Co-authored-by: Phil Haack <[email protected]>

---------

Co-authored-by: Phil Haack <[email protected]>
  • Loading branch information
havenbarnes and haacked authored Feb 14, 2025
1 parent 4676b05 commit 5ec2fa0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/posthog-featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
JsonType,
Compression,
EarlyAccessFeature,
RemoteConfigFeatureFlagCallback,
} from './types'
import { PostHogPersistence } from './posthog-persistence'

Expand Down Expand Up @@ -359,6 +360,37 @@ export class PostHogFeatureFlags {
return payloads[key]
}

/*
* Fetches the payload for a remote config feature flag. This method will bypass any cached values and fetch the latest
* value from the PostHog API.
*
* Note: Encrypted remote config payloads will not be redacted, nor decrypted in the response.
*
* ### Usage:
*
* getRemoteConfigPayload("home-page-welcome-message", (payload) => console.log(`Fetched remote config: ${payload}`))
*
* @param {String} key Key of the feature flag.
* @param {Function} [callback] The callback function will be called once the remote config feature flag payload has been fetched.
*/
getRemoteConfigPayload(key: string, callback: RemoteConfigFeatureFlagCallback): void {
const token = this.instance.config.token
this.instance._send_request({
method: 'POST',
url: this.instance.requestRouter.endpointFor('api', '/decide/?v=3'),
data: {
distinct_id: this.instance.get_distinct_id(),
token,
},
compression: this.instance.config.disable_compression ? undefined : Compression.Base64,
timeout: this.instance.config.feature_flag_request_timeout_ms,
callback: (response) => {
const flagPayloads = response.json?.['featureFlagPayloads']
callback(flagPayloads?.[key] || undefined)
},
})
}

/*
* See if feature flag is enabled for user.
*
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ export type FeatureFlagsCallback = (
}
) => void

export type RemoteConfigFeatureFlagCallback = (payload: JsonType) => void

export interface PersistentStore {
is_supported: () => boolean
error: (error: any) => void
Expand Down

0 comments on commit 5ec2fa0

Please sign in to comment.