Skip to content

Commit d945bfb

Browse files
feat(react): react server support (#246)
BREAKING CHANGE: Previously, when you did `useConfidence`, the returned instance would be augmented with extra hook-like functions such as `useFlag`. These have been removed and you should just use the exported top level hooks. --------- Co-authored-by: Nicklas Lundin <[email protected]>
1 parent 5911d7b commit d945bfb

File tree

10 files changed

+422
-351
lines changed

10 files changed

+422
-351
lines changed

api/react-server.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Confidence } from '@spotify-confidence/sdk';
2+
import React, { ReactNode } from 'react';
3+
4+
declare function ConfidenceProvider(props: {
5+
confidence: Confidence;
6+
children?: ReactNode;
7+
}): Promise<React.JSX.Element>;
8+
9+
export { ConfidenceProvider };

api/react.d.ts

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,21 @@
1-
import { EventSender, Trackable, FlagResolver, Confidence, Configuration, Value, Closer, Context, StateObserver, FlagEvaluation } from '@spotify-confidence/sdk';
2-
import { FC, PropsWithChildren } from 'react';
1+
import { ConfidenceOptions, Confidence, Context, FlagEvaluation, Value } from '@spotify-confidence/sdk';
2+
import { FC, PropsWithChildren, FunctionComponent, ReactNode } from 'react';
33

4-
/**
5-
* Confidence React instance
6-
* @public
7-
*/
8-
declare class ConfidenceReact implements EventSender, Trackable, FlagResolver {
9-
/**
10-
* Confidence Delegate
11-
* @internal */
12-
readonly delegate: Confidence;
13-
constructor(delegate: Confidence);
14-
/** Return configurations of the Confidence instance */
15-
get config(): Configuration;
16-
/**
17-
* Current serialized Context
18-
* @internal */
19-
get contextState(): string;
20-
/**
21-
* Tracks an event
22-
* @param name - event name
23-
* @param message - data to track */
24-
track(name: string, message?: Value.Struct): void;
25-
/**
26-
* Tracks an event
27-
* @param manager - trackable manager */
28-
track(manager: Trackable.Manager): Closer;
29-
/** Returns context of the current Confidence instance */
30-
getContext(): Context;
31-
/** Set Confidence context */
32-
setContext(context: Context, { transition }?: {
33-
transition?: boolean | undefined;
34-
}): void;
35-
/** Subscribe to flag changes in Confidence */
36-
subscribe(onStateChange?: StateObserver | undefined): () => void;
37-
/** Clears context of current Confidence instance */
38-
clearContext({ transition }?: {
39-
transition?: boolean | undefined;
40-
}): void;
41-
/**
42-
* Creates a new ConfidenceReact instance with context
43-
* @param context - Confidence context
44-
* @returns ConfidenceReact instance
45-
*/
46-
withContext(context: Context): ConfidenceReact;
47-
/** Evaluates a flag */
48-
evaluateFlag(path: string, defaultValue: string): FlagEvaluation<string>;
49-
evaluateFlag(path: string, defaultValue: boolean): FlagEvaluation<boolean>;
50-
evaluateFlag(path: string, defaultValue: number): FlagEvaluation<number>;
51-
evaluateFlag<T extends Value>(path: string, defaultValue: T): FlagEvaluation<T>;
52-
/** Returns flag value for a given flag */
53-
getFlag(path: string, defaultValue: string): Promise<string>;
54-
getFlag(path: string, defaultValue: boolean): Promise<boolean>;
55-
getFlag(path: string, defaultValue: number): Promise<number>;
56-
getFlag<T extends Value>(path: string, defaultValue: T): Promise<T>;
57-
/** Hook to access Context */
58-
useContext(): Context;
59-
/** Hook to access the WithContext functionality. Returns a ConfidenceReact instance with the passed context. */
60-
useWithContext(context: Context): ConfidenceReact;
61-
/** Hook to use EvaluateFlag functionality */
62-
useEvaluateFlag(path: string, defaultValue: string): FlagEvaluation<string>;
63-
useEvaluateFlag(path: string, defaultValue: number): FlagEvaluation<number>;
64-
useEvaluateFlag(path: string, defaultValue: boolean): FlagEvaluation<boolean>;
65-
useEvaluateFlag<T extends Value>(path: string, defaultValue: T): FlagEvaluation<T>;
66-
/** Hook to use getFlag functionality */
67-
useFlag(path: string, defaultValue: string): string;
68-
useFlag(path: string, defaultValue: number): number;
69-
useFlag(path: string, defaultValue: boolean): boolean;
70-
useFlag<T extends Value>(path: string, defaultValue: T): T;
71-
private assertContext;
72-
}
4+
declare const ManagedConfidenceProvider: FC<PropsWithChildren<{
5+
options: ConfidenceOptions;
6+
}>>;
737
/**
748
* Confidence Provider for React
759
* @public
7610
*/
77-
type ConfidenceProvider = FC<PropsWithChildren<{
11+
interface ConfidenceProvider extends FunctionComponent<{
7812
confidence: Confidence;
79-
}>> & {
13+
children?: ReactNode;
14+
}> {
8015
WithContext: FC<PropsWithChildren<{
8116
context: Context;
8217
}>>;
83-
};
18+
}
8419
/**
8520
* Confidence Provider for React
8621
* @public
@@ -90,31 +25,31 @@ declare const ConfidenceProvider: ConfidenceProvider;
9025
* Enables using Confidence
9126
* @public
9227
*/
93-
declare const useConfidence: () => ConfidenceReact;
28+
declare const useConfidence: () => Confidence;
9429
/**
9530
* Use with given Confidence Context
9631
* @public
9732
*/
98-
declare function useWithContext(context: Context, parent?: ConfidenceReact): ConfidenceReact;
33+
declare function useWithContext(context: Context, parent?: Confidence): Confidence;
9934
/**
10035
* Use Confidence Context
10136
* @public
10237
*/
103-
declare function useConfidenceContext(confidence?: ConfidenceReact): Context;
38+
declare function useConfidenceContext(confidence?: Confidence): Context;
10439
/**
10540
* Use EvaluateFlag
10641
* @public */
107-
declare function useEvaluateFlag(path: string, defaultValue: string, confidence?: ConfidenceReact): FlagEvaluation<string>;
108-
declare function useEvaluateFlag(path: string, defaultValue: number, confidence?: ConfidenceReact): FlagEvaluation<number>;
109-
declare function useEvaluateFlag(path: string, defaultValue: boolean, confidence?: ConfidenceReact): FlagEvaluation<boolean>;
110-
declare function useEvaluateFlag<T extends Value>(path: string, defaultValue: T, confidence?: ConfidenceReact): FlagEvaluation<T>;
42+
declare function useEvaluateFlag(path: string, defaultValue: string, confidence?: Confidence): FlagEvaluation<string>;
43+
declare function useEvaluateFlag(path: string, defaultValue: number, confidence?: Confidence): FlagEvaluation<number>;
44+
declare function useEvaluateFlag(path: string, defaultValue: boolean, confidence?: Confidence): FlagEvaluation<boolean>;
45+
declare function useEvaluateFlag<T extends Value>(path: string, defaultValue: T, confidence?: Confidence): FlagEvaluation<T>;
11146
/**
11247
* Use Flag
11348
* @public
11449
*/
115-
declare function useFlag(path: string, defaultValue: string, confidence?: ConfidenceReact): string;
116-
declare function useFlag(path: string, defaultValue: number, confidence?: ConfidenceReact): number;
117-
declare function useFlag(path: string, defaultValue: boolean, confidence?: ConfidenceReact): boolean;
118-
declare function useFlag<T extends Value>(path: string, defaultValue: T, confidence?: ConfidenceReact): T;
50+
declare function useFlag(path: string, defaultValue: string, confidence?: Confidence): string;
51+
declare function useFlag(path: string, defaultValue: number, confidence?: Confidence): number;
52+
declare function useFlag(path: string, defaultValue: boolean, confidence?: Confidence): boolean;
53+
declare function useFlag<T extends Value>(path: string, defaultValue: T, confidence?: Confidence): T;
11954

120-
export { ConfidenceProvider, ConfidenceReact, useConfidence, useConfidenceContext, useEvaluateFlag, useFlag, useWithContext };
55+
export { ConfidenceProvider, ManagedConfidenceProvider, useConfidence, useConfidenceContext, useEvaluateFlag, useFlag, useWithContext };

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"@spotify/tsconfig": "^15.0.0",
3434
"@swc/core": "^1.4.13",
3535
"@types/jest": "^29.5.3",
36-
"@types/react": "^18.2.17",
3736
"@typescript-eslint/eslint-plugin": "^5.62.0",
3837
"@typescript-eslint/parser": "^5.62.0",
3938
"@yarnpkg/types": "^4.0.0",

0 commit comments

Comments
 (0)