@@ -166,7 +166,7 @@ type EmptyConfig = {
166166 payload : undefined ;
167167} ;
168168
169- type Feature < TKey extends FeatureKey > = {
169+ export type Feature < TKey extends FeatureKey > = {
170170 isEnabled : boolean ;
171171 isLoading : boolean ;
172172 config : MaterializedFeatures [ TKey ] extends boolean
@@ -195,12 +195,10 @@ type Feature<TKey extends FeatureKey> = {
195195export function useFeature < TKey extends FeatureKey > (
196196 key : TKey ,
197197) : Feature < typeof key > {
198+ const client = useClient ( ) ;
198199 const {
199200 features : { isLoading } ,
200- client,
201- provider,
202201 } = useContext < ProviderContextType > ( ProviderContext ) ;
203- ensureProvider ( provider ) ;
204202
205203 const track = ( ) => client ?. track ( key ) ;
206204 const requestFeedback = ( opts : RequestFeedbackOptions ) =>
@@ -241,8 +239,7 @@ export function useFeature<TKey extends FeatureKey>(
241239 * ```
242240 */
243241export function useTrack ( ) {
244- const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
245- ensureProvider ( provider ) ;
242+ const client = useClient ( ) ;
246243 return ( eventName : string , attributes ?: Record < string , any > | null ) =>
247244 client ?. track ( eventName , attributes ) ;
248245}
@@ -262,8 +259,7 @@ export function useTrack() {
262259 * ```
263260 */
264261export function useRequestFeedback ( ) {
265- const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
266- ensureProvider ( provider ) ;
262+ const client = useClient ( ) ;
267263 return ( options : RequestFeedbackData ) => client ?. requestFeedback ( options ) ;
268264}
269265
@@ -284,8 +280,7 @@ export function useRequestFeedback() {
284280 * ```
285281 */
286282export function useSendFeedback ( ) {
287- const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
288- ensureProvider ( provider ) ;
283+ const client = useClient ( ) ;
289284 return ( opts : UnassignedFeedback ) => client ?. feedback ( opts ) ;
290285}
291286
@@ -303,8 +298,7 @@ export function useSendFeedback() {
303298 * ```
304299 */
305300export function useUpdateUser ( ) {
306- const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
307- ensureProvider ( provider ) ;
301+ const client = useClient ( ) ;
308302 return ( opts : { [ key : string ] : string | number | undefined } ) =>
309303 client ?. updateUser ( opts ) ;
310304}
@@ -323,8 +317,7 @@ export function useUpdateUser() {
323317 * ```
324318 */
325319export function useUpdateCompany ( ) {
326- const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
327- ensureProvider ( provider ) ;
320+ const client = useClient ( ) ;
328321
329322 return ( opts : { [ key : string ] : string | number | undefined } ) =>
330323 client ?. updateCompany ( opts ) ;
@@ -345,16 +338,30 @@ export function useUpdateCompany() {
345338 * ```
346339 */
347340export function useUpdateOtherContext ( ) {
348- const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
349- ensureProvider ( provider ) ;
341+ const client = useClient ( ) ;
350342 return ( opts : { [ key : string ] : string | number | undefined } ) =>
351343 client ?. updateOtherContext ( opts ) ;
352344}
353345
354- function ensureProvider ( provider : boolean ) {
346+ /**
347+ * Returns the current `BucketClient` used by the `BucketProvider`.
348+ *
349+ * This is useful if you need to access the `BucketClient` outside of the `BucketProvider`.
350+ *
351+ * ```ts
352+ * const client = useClient();
353+ * client.on("configCheck", () => {
354+ * console.log("configCheck hook called");
355+ * });
356+ * ```
357+ */
358+ export function useClient ( ) {
359+ const { client, provider } = useContext < ProviderContextType > ( ProviderContext ) ;
355360 if ( ! provider ) {
356361 throw new Error (
357362 "BucketProvider is missing. Please ensure your component is wrapped with a BucketProvider." ,
358363 ) ;
359364 }
365+
366+ return client ;
360367}
0 commit comments