Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Sep 12, 2024
1 parent 7d3ed7c commit 02aab76
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 17 deletions.
32 changes: 18 additions & 14 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,48 +724,52 @@ export abstract class PostHogCore extends PostHogCoreStateless {
}

protected setupBootstrap(options?: Partial<PostHogCoreOptions>): void {
const bootstrap = options?.bootstrap
if (!bootstrap) {
return
}

// bootstrap options are only set if no persisted values are found
// this is to prevent overwriting existing values
if (options?.bootstrap?.distinctId) {
if (options?.bootstrap?.isIdentifiedId) {
if (bootstrap.distinctId) {
if (bootstrap.isIdentifiedId) {
const distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId)

if (!distinctId) {
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, options.bootstrap.distinctId)
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, bootstrap.distinctId)
}
} else {
const anonymousId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId)

if (!anonymousId) {
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, options.bootstrap.distinctId)
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, bootstrap.distinctId)
}
}
}

if (options?.bootstrap?.featureFlags) {
const bootstrapFlags = Object.keys(options.bootstrap?.featureFlags || {})
.filter((flag) => !!options.bootstrap?.featureFlags?.[flag])
const bootstrapfeatureFlags = bootstrap.featureFlags
if (bootstrapfeatureFlags && Object.keys(bootstrapfeatureFlags).length) {
const bootstrapFlags = Object.keys(bootstrapfeatureFlags)
.filter((flag) => !!bootstrapfeatureFlags[flag])
.reduce(
(res: Record<string, string | boolean>, key) => (
(res[key] = options.bootstrap?.featureFlags?.[key] || false), res
),
(res: Record<string, string | boolean>, key) => ((res[key] = bootstrapfeatureFlags[key] || false), res),
{}
)

if (bootstrapFlags.length) {
if (Object.keys(bootstrapFlags).length) {
const currentFlags =
this.getPersistedProperty<PostHogDecideResponse['featureFlags']>(PostHogPersistedProperty.FeatureFlags) || {}
const newFeatureFlags = { ...bootstrapFlags, ...currentFlags }
this.setKnownFeatureFlags(newFeatureFlags)
}

const bootstrapFlagPayloads = options?.bootstrap?.featureFlagPayloads
if (bootstrapFlagPayloads && bootstrapFlagPayloads?.length) {
const bootstrapFlagPayloads = bootstrap.featureFlagPayloads
if (bootstrapFlagPayloads && Object.keys(bootstrapFlagPayloads).length) {
const currentFlagPayloads =
this.getPersistedProperty<PostHogDecideResponse['featureFlagPayloads']>(
PostHogPersistedProperty.FeatureFlagPayloads
) || {}
const newFeatureFlagPayloads = { bootstrapFlagPayloads, ...currentFlagPayloads }
const newFeatureFlagPayloads = { ...bootstrapFlagPayloads, ...currentFlagPayloads }
this.setKnownFeatureFlagPayloads(newFeatureFlagPayloads)
}
}
Expand Down
64 changes: 64 additions & 0 deletions posthog-core/test/posthog.featureflags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,68 @@ describe('PostHog Core', () => {
})
})
})

describe('bootstapped do not overwrite values', () => {
beforeEach(() => {
;[posthog, mocks] = createTestClient(
'TEST_API_KEY',
{
flushAt: 1,
bootstrap: {
distinctId: 'tomato',
featureFlags: { 'bootstrap-1': 'variant-1', enabled: true, disabled: false },
featureFlagPayloads: {
'bootstrap-1': {
some: 'key',
},
enabled: 200,
},
},
},
(_mocks) => {
_mocks.fetch.mockImplementation((url) => {
if (url.includes('/decide/')) {
return Promise.resolve({
status: 200,
text: () => Promise.resolve('ok'),
json: () =>
Promise.resolve({
featureFlags: createMockFeatureFlags(),
featureFlagPayloads: createMockFeatureFlagPayloads(),
}),
})
}

return Promise.resolve({
status: 200,
text: () => Promise.resolve('ok'),
json: () =>
Promise.resolve({
status: 'ok',
}),
})
})
},
{
distinct_id: '123',
feature_flags: { 'bootstrap-1': 'variant-2' },
feature_flag_payloads: { 'bootstrap-1': { some: 'other-key' } },
}
)
})

it('distinct id should not be overwritten if already there', () => {
expect(posthog.getDistinctId()).toEqual('123')
})

it('flags should not be overwritten if already there', () => {
expect(posthog.getFeatureFlag('bootstrap-1')).toEqual('variant-2')
})

it('flag payloads should not be overwritten if already there', () => {
expect(posthog.getFeatureFlagPayload('bootstrap-1')).toEqual({
some: 'other-key',
})
})
})
})
6 changes: 3 additions & 3 deletions posthog-core/test/test-utils/PostHogCoreTestClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PostHogCore, PostHogCoreOptions, PostHogFetchOptions, PostHogFetchResponse } from '../../src'
import { JsonType, PostHogCore, PostHogCoreOptions, PostHogFetchOptions, PostHogFetchResponse } from '../../src'

const version = '2.0.0-alpha'

Expand Down Expand Up @@ -42,9 +42,9 @@ export class PostHogCoreTestClient extends PostHogCore {
export const createTestClient = (
apiKey: string,
options?: PostHogCoreOptions,
setupMocks?: (mocks: PostHogCoreTestClientMocks) => void
setupMocks?: (mocks: PostHogCoreTestClientMocks) => void,
storageCache: { [key: string]: string | JsonType } = {}
): [PostHogCoreTestClient, PostHogCoreTestClientMocks] => {
const storageCache: { [key: string]: string | undefined } = {}
const mocks = {
fetch: jest.fn<Promise<PostHogFetchResponse>, [string, PostHogFetchOptions]>(),
storage: {
Expand Down

0 comments on commit 02aab76

Please sign in to comment.