Skip to content

Commit eaad0f7

Browse files
authored
fix(growthbook): types (#1439)
1 parent 5cf067c commit eaad0f7

File tree

6 files changed

+58
-42
lines changed

6 files changed

+58
-42
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scaleway/use-growthbook': patch
3+
---
4+
5+
add loadfeature configuration into the providers and moove return function from null to void

packages/use-growthbook/src/AbTestProvider.tsx

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
/* eslint-disable @typescript-eslint/no-unsafe-return */
33
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
5-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
65
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
76
import { GrowthBook, GrowthBookProvider } from '@growthbook/growthbook-react'
8-
import type { ReactNode } from 'react'
9-
import { useCallback, useEffect, useMemo } from 'react'
10-
import type { GrowthBookType } from './types'
7+
import { type ReactNode, useCallback, useEffect, useMemo } from 'react'
8+
import type { Attributes, GrowthBookType, LoadConfig } from './types'
119

1210
export type ToolConfig = {
1311
apiHost: string
@@ -18,57 +16,60 @@ export type ToolConfig = {
1816
export type TrackingCallback = (
1917
experiment: { key: string },
2018
result: { key: string },
21-
) => null
19+
) => void
20+
21+
// TODO: use type from growthbook when it's typed will be export correctly
22+
// export type TrackingCallback = NonNullable<Context['trackingCallback']>
2223

2324
export type AbTestProviderProps = {
2425
children: ReactNode
25-
anonymousId: string
2626
config: ToolConfig
2727
trackingCallback: TrackingCallback
28-
errorCallback: (error: string) => null
28+
errorCallback: (error: Error | string) => void
29+
attributes: Attributes
30+
loadConfig?: LoadConfig
2931
}
3032

3133
const getGrowthBookInstance = ({
3234
config: { apiHost, clientKey, enableDevMode },
33-
anonymousId,
35+
attributes,
3436
trackingCallback,
3537
}: {
3638
config: ToolConfig
37-
anonymousId: string
39+
attributes: Attributes
3840
trackingCallback: TrackingCallback
3941
}): GrowthBookType =>
4042
new GrowthBook({
4143
apiHost,
4244
clientKey,
4345
enableDevMode,
44-
attributes: {
45-
anonymousId,
46-
userId: undefined,
47-
organizationId: undefined,
48-
organizationType: undefined,
49-
},
46+
attributes,
5047
trackingCallback,
5148
})
49+
50+
const defaultLoadConfig = {
51+
autoRefresh: false,
52+
timeout: 500,
53+
}
54+
5255
export const AbTestProvider = ({
5356
children,
5457
config,
55-
anonymousId,
5658
trackingCallback,
5759
errorCallback,
60+
attributes,
61+
loadConfig = defaultLoadConfig,
5862
}: AbTestProviderProps) => {
5963
const growthbook: GrowthBookType = useMemo(
60-
() => getGrowthBookInstance({ config, anonymousId, trackingCallback }),
61-
[trackingCallback, config, anonymousId],
64+
() => getGrowthBookInstance({ config, attributes, trackingCallback }),
65+
[trackingCallback, config, attributes],
6266
)
6367

6468
const loadFeature = useCallback(async () => {
6569
if (config.clientKey) {
66-
await growthbook.loadFeatures({
67-
autoRefresh: false,
68-
timeout: 500,
69-
})
70+
await growthbook.loadFeatures(loadConfig)
7071
}
71-
}, [growthbook, config])
72+
}, [growthbook, config, loadConfig])
7273

7374
useEffect(() => {
7475
loadFeature().catch(errorCallback)

packages/use-growthbook/src/__tests__/AbTestProvider.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const mockGrowthBook = GrowthBook as jest.MockedClass<GrowthBookType>
1010

1111
describe('AbTestProvider', () => {
1212
let trackingCallback: TrackingCallback
13-
let errorCallback: (error: string) => null
13+
let errorCallback: (error: Error | string) => void
1414

1515
beforeEach(() => {
1616
trackingCallback = jest.fn()
@@ -19,16 +19,18 @@ describe('AbTestProvider', () => {
1919
it('should init GrowthBook once', () => {
2020
render(
2121
<AbTestProvider
22-
anonymousId="foo"
2322
config={{
2423
apiHost: 'host',
2524
clientKey: 'clientKey',
2625
enableDevMode: true,
2726
}}
27+
attributes={{
28+
anonymousId: 'foo',
29+
}}
2830
trackingCallback={trackingCallback}
2931
errorCallback={errorCallback}
3032
>
31-
Foo
33+
Children
3234
</AbTestProvider>,
3335
)
3436

@@ -39,16 +41,18 @@ describe('AbTestProvider', () => {
3941
it('should not init GrowthBook when client key is not defined', () => {
4042
render(
4143
<AbTestProvider
42-
anonymousId="foo"
4344
config={{
4445
apiHost: 'host',
4546
clientKey: '',
4647
enableDevMode: true,
4748
}}
49+
attributes={{
50+
anonymousId: 'foo',
51+
}}
4852
trackingCallback={trackingCallback}
4953
errorCallback={errorCallback}
5054
>
51-
Foo
55+
Children
5256
</AbTestProvider>,
5357
)
5458

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
21
export {
3-
FeatureString,
4-
FeaturesReady,
5-
IfFeatureEnabled,
62
useExperiment,
73
useFeature,
84
withRunExperiment,
95
useFeatureIsOn,
106
useFeatureValue,
117
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
128
} from '@growthbook/growthbook-react'
9+
export type {
10+
FeatureString,
11+
FeaturesReady,
12+
IfFeatureEnabled,
13+
Context,
14+
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
15+
} from '@growthbook/growthbook-react'
1316
export { useAbTestAttributes } from './useAbTestAttributes'
1417
export { AbTestProvider } from './AbTestProvider'
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
export type Attributes = Record<string, unknown>
1+
export type Attributes = Record<string, string | number | undefined>
2+
3+
/**
4+
* @param {boolean} [autoRefresh] - false.
5+
* @param {number} [timeout] - 500.
6+
*/
7+
export type LoadConfig = {
8+
autoRefresh: boolean
9+
timeout: number
10+
}
211

312
export type GrowthBookType = {
413
new (...args: unknown[]): GrowthBookType
514
getAttributes: () => Attributes
6-
loadFeatures: ({
7-
autoRefresh,
8-
timeout,
9-
}: {
10-
autoRefresh: boolean
11-
timeout: number
12-
}) => Promise<null>
13-
setAttributes: (attributes: Attributes) => null
15+
loadFeatures: ({ autoRefresh, timeout }: LoadConfig) => Promise<void>
16+
setAttributes: (attributes: Attributes) => void
1417
}

packages/use-growthbook/src/useAbTestAttributes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Attributes, GrowthBookType } from './types'
88

99
export const useAbTestAttributes = (): [
1010
Attributes,
11-
(attributes: Attributes) => null | undefined,
11+
(attributes: Attributes) => void,
1212
] => {
1313
const growthBook = useGrowthBook() as GrowthBookType | null
1414

0 commit comments

Comments
 (0)