Skip to content

Commit

Permalink
feat: allow accessing Flagsmith instance
Browse files Browse the repository at this point in the history
via `flagsmithInstance` property and `useFlagsmithInstance` function
  • Loading branch information
FloEdelmann authored and jhoermann committed Jul 10, 2024
1 parent 44396c9 commit 4e4980d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ useFlagsmith(options)

For `options` see [Flagsmith initialization options](https://docs.flagsmith.com/clients/javascript#initialisation-options).

Then you can access the flags, traits and loading status inside your child components:
Then you can access the flags, traits, loading status and the Flagsmith instance itself inside your child components:

```ts
import { useFlags, useTraits, useFlagsmithLoading } from 'flagsmith-vue'

const flags = useFlags(['flag1', 'flag2', ...])
const traits = useTraits(['trait1', 'trait2', ...])
const flagsmithLoading = useFlagsmithLoading()
const { setTraits } = useFlagsmithInstance()
```

## License
Expand Down
14 changes: 13 additions & 1 deletion __tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { computed, defineComponent, nextTick } from 'vue'
import { useFlags, useFlagsmith, useFlagsmithLoading, useTraits } from '../src/index'
import {
useFlags,
useFlagsmith,
useFlagsmithInstance,
useFlagsmithLoading,
useTraits,
} from '../src/index'
import { mount } from '@vue/test-utils'
import flagsmith from 'flagsmith'
import type { IFlagsmithFeature } from 'flagsmith/types'
Expand All @@ -23,12 +29,17 @@ const ChildComponent = defineComponent({
class="child-component--loading">
Source: {{ source }}
</div>
<div
class="child-component--state">
Source: {{ getState() }}
</div>
</div>`,
setup() {
const { is_visible: isVisibleFeature } = useFlags(['is_visible'])
const isVisible = computed(() => isVisibleFeature.value?.value)
const { test_trait: testTrait } = useTraits(['test_trait'])
const { error, isFetching, isLoading, source } = useFlagsmithLoading()
const { getState } = useFlagsmithInstance()

return {
isVisible,
Expand All @@ -37,6 +48,7 @@ const ChildComponent = defineComponent({
isFetching,
isLoading,
source,
getState,
}
},
})
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ITraits,
LoadingState,
FlagSource,
IFlagsmith,
} from 'flagsmith/types'
import { computed, inject, provide, ref } from 'vue'
import type { ComputedRef, InjectionKey, Ref } from 'vue'
Expand All @@ -15,6 +16,7 @@ export interface FlagsmithHelper<F extends string = string, T extends string = s
flags: Ref<IFlags<F> | undefined>
traits: Ref<ITraits<T> | undefined>
loadingState: Ref<LoadingState | undefined>
flagsmithInstance: IFlagsmith<F, T>
}
const FlagsmithInjectionKey: InjectionKey<FlagsmithHelper> = Symbol('FlagsmithInjectionKey')
const injectHelper = <F extends string = string, T extends string = string>(
Expand Down Expand Up @@ -50,6 +52,7 @@ export const useFlagsmith = <F extends string = string, T extends string = strin
flags,
traits,
loadingState,
flagsmithInstance,

Check failure on line 55 in src/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/index.ts#L55

Unsafe assignment of an `any` value.
}
provide(FlagsmithInjectionKey, helper)

Expand Down Expand Up @@ -93,3 +96,10 @@ export const useFlagsmithLoading = <F extends string = string, T extends string
source: computed(() => loadingState.value?.source ?? ('NONE' as FlagSource)),
}
}

export const useFlagsmithInstance = <F extends string = string, T extends string = string>(
flagsmithHelper?: FlagsmithHelper<F, T>
): IFlagsmith<F, T> => {
const { flagsmithInstance } = injectHelper(flagsmithHelper)

Check failure on line 103 in src/index.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/index.ts#L103

Unsafe array destructuring of a tuple element with an `any` value.
return flagsmithInstance
}

0 comments on commit 4e4980d

Please sign in to comment.