Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(feature-flags): v3 feature flag payloads #50

Merged
merged 9 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PosthogCoreOptions,
PostHogEventProperties,
PostHogPersistedProperty,
JsonType,
} from './types'
import {
assert,
Expand All @@ -32,6 +33,7 @@ export abstract class PostHogCore {
private captureMode: 'form' | 'json'
private sendFeatureFlagEvent: boolean
private flagCallReported: { [key: string]: boolean } = {}
private flagPayloadCallReported: { [key: string]: boolean } = {}
private removeDebugCallback?: () => void

// internal
Expand Down Expand Up @@ -436,6 +438,10 @@ export abstract class PostHogCore {
this.setKnownFeatureFlags(res.featureFlags)
}

if (res.featureFlagPayloads) {
this.setKnownFeatureFlagPayloads(res.featureFlagPayloads)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need upserting here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Actually, can we not duplicate upserting code and move it into the above? wherever flags are set, set the payloads as well, otherwise don't do anything.

Let's always keep flags response as the source of truth, and payloads just follow whatever happens to flags.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, will be easy to make these go out of sync, which is terrible :/


return res
})
.finally(() => {
Expand All @@ -452,6 +458,13 @@ export abstract class PostHogCore {
this._events.emit('featureflags', featureFlags)
}

private setKnownFeatureFlagPayloads(featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads']): void {
this.setPersistedProperty<PostHogDecideResponse['featureFlagPayloads']>(
PostHogPersistedProperty.FeatureFlagPayloads,
featureFlagPayloads
)
}

getFeatureFlag(key: string): boolean | string | undefined {
const featureFlags = this.getFeatureFlags()

Expand All @@ -478,6 +491,33 @@ export abstract class PostHogCore {
return response
}

getFeatureFlagPayload(key: string): JsonType | undefined {
const payloads = this.getFeatureFlagPayloads()

if (!payloads) {
return undefined
}

let response = payloads[key]

if (this.sendFeatureFlagEvent && !this.flagPayloadCallReported[key]) {
this.flagPayloadCallReported[key] = true
this.capture('$feature_flag_payload_called', {
$feature_flag: key,
$feature_flag_payload: response,
})
}

return response
}

getFeatureFlagPayloads(): PostHogDecideResponse['featureFlagPayloads'] | undefined {
let payloads = this.getPersistedProperty<PostHogDecideResponse['featureFlagPayloads']>(
PostHogPersistedProperty.FeatureFlagPayloads
)
return payloads
}

getFeatureFlags(): PostHogDecideResponse['featureFlags'] | undefined {
let flags = this.getPersistedProperty<PostHogDecideResponse['featureFlags']>(PostHogPersistedProperty.FeatureFlags)
const overriddenFlags = this.getPersistedProperty<PostHogDecideResponse['featureFlags']>(
Expand Down
6 changes: 6 additions & 0 deletions posthog-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export enum PostHogPersistedProperty {
DistinctId = 'distinct_id',
Props = 'props',
FeatureFlags = 'feature_flags',
FeatureFlagPayloads = 'feature_flag_payloads',
OverrideFeatureFlags = 'override_feature_flags',
Queue = 'queue',
OptedOut = 'opted_out',
Expand Down Expand Up @@ -86,5 +87,10 @@ export type PostHogDecideResponse = {
featureFlags: {
[key: string]: string | boolean
}
featureFlagPayloads: {
[key: string]: JsonType
}
sessionRecording: boolean
}

export type JsonType = string | number | boolean | null | { [key: string]: JsonType } | Array<JsonType>
35 changes: 35 additions & 0 deletions posthog-core/test/posthog.featureflags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ describe('PostHog Core', () => {
'feature-variant': 'variant',
})

const createMockFeatureFlagPayloads = (): any => ({
'feature-1': {
color: 'blue',
},
'feature-variant': 5,
})

beforeEach(() => {
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 }, (_mocks) => {
_mocks.fetch.mockImplementation((url) => {
Expand All @@ -25,6 +32,7 @@ describe('PostHog Core', () => {
json: () =>
Promise.resolve({
featureFlags: createMockFeatureFlags(),
featureFlagPayloads: createMockFeatureFlagPayloads(),
}),
})
}
Expand All @@ -46,11 +54,19 @@ describe('PostHog Core', () => {
expect(posthog.getFeatureFlags()).toEqual(undefined)
})

it('getFeatureFlagPayloads should return undefined if not loaded', () => {
expect(posthog.getFeatureFlagPayloads()).toEqual(undefined)
})

it('getFeatureFlag should return undefined if not loaded', () => {
expect(posthog.getFeatureFlag('my-flag')).toEqual(undefined)
expect(posthog.getFeatureFlag('feature-1')).toEqual(undefined)
})

it('getFeatureFlagPayload should return undefined if not loaded', () => {
expect(posthog.getFeatureFlagPayload('my-flag')).toEqual(undefined)
})

it('isFeatureEnabled should return undefined if not loaded', () => {
expect(posthog.isFeatureEnabled('my-flag')).toEqual(undefined)
expect(posthog.isFeatureEnabled('feature-1')).toEqual(undefined)
Expand Down Expand Up @@ -97,6 +113,13 @@ describe('PostHog Core', () => {
'feature-2': true,
'feature-variant': 'variant',
})

expect(posthog.getFeatureFlagPayloads()).toEqual({
'feature-1': {
color: 'blue',
},
'feature-variant': 5,
})
})

it('should return the value of a flag', async () => {
Expand All @@ -105,6 +128,15 @@ describe('PostHog Core', () => {
expect(posthog.getFeatureFlag('feature-missing')).toEqual(false)
})

it('should return payload of matched flags only', async () => {
expect(posthog.getFeatureFlagPayload('feature-variant')).toEqual(5)
expect(posthog.getFeatureFlagPayload('feature-1')).toEqual({
color: 'blue',
})

expect(posthog.getFeatureFlagPayload('feature-2')).toEqual(undefined)
})

describe('when errored out', () => {
beforeEach(() => {
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 }, (_mocks) => {
Expand Down Expand Up @@ -140,6 +172,9 @@ describe('PostHog Core', () => {
expect(posthog.isFeatureEnabled('feature-1')).toEqual(undefined)
expect(posthog.isFeatureEnabled('feature-variant')).toEqual(undefined)
expect(posthog.isFeatureEnabled('feature-missing')).toEqual(undefined)

expect(posthog.getFeatureFlagPayloads()).toEqual(undefined)
expect(posthog.getFeatureFlagPayload('feature-1')).toEqual(undefined)
})
})

Expand Down