@launchdarkly/react-sdk 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"inputs":{"src/client/LDClient.ts":{"bytes":3178,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@launchdarkly/js-client-sdk","kind":"import-statement","external":true}],"format":"esm"},"src/client/LDOptions.ts":{"bytes":3039,"imports":[{"path":"@launchdarkly/js-client-sdk","kind":"import-statement","external":true},{"path":"./LDClient","kind":"import-statement","external":true}],"format":"esm"},"src/client/provider/LDReactContext.tsx":{"bytes":656,"imports":[{"path":"react","kind":"import-statement","external":true}],"format":"esm"},"../browser/dist/index.js":{"bytes":87384,"imports":[],"format":"esm"},"src/client/LDReactClient.tsx":{"bytes":6302,"imports":[{"path":"../browser/dist/index.js","kind":"import-statement","original":"@launchdarkly/js-client-sdk"},{"path":"./LDClient","kind":"import-statement","external":true},{"path":"./LDOptions","kind":"import-statement","external":true},{"path":"<runtime>","kind":"import-statement","external":true}],"format":"esm"},"src/client/provider/LDReactProvider.tsx":{"bytes":4490,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@launchdarkly/js-client-sdk","kind":"import-statement","external":true},{"path":"../LDOptions","kind":"import-statement","external":true},{"path":"src/client/LDReactClient.tsx","kind":"import-statement","original":"../LDReactClient"},{"path":"src/client/provider/LDReactContext.tsx","kind":"import-statement","original":"./LDReactContext"},{"path":"<runtime>","kind":"import-statement","external":true}],"format":"esm"},"src/client/deprecated-hooks/flagKeyUtils.ts":{"bytes":710,"imports":[],"format":"esm"},"src/client/deprecated-hooks/useFlags.ts":{"bytes":3849,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"src/client/provider/LDReactContext.tsx","kind":"import-statement","original":"../provider/LDReactContext"},{"path":"src/client/deprecated-hooks/flagKeyUtils.ts","kind":"import-statement","original":"./flagKeyUtils"}],"format":"esm"},"src/client/deprecated-hooks/index.ts":{"bytes":39,"imports":[{"path":"src/client/deprecated-hooks/useFlags.ts","kind":"import-statement","original":"./useFlags"}],"format":"esm"},"src/client/hooks/useInitializationStatus.ts":{"bytes":1060,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"src/client/provider/LDReactContext.tsx","kind":"import-statement","original":"../provider/LDReactContext"}],"format":"esm"},"src/client/hooks/useLDClient.ts":{"bytes":613,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"src/client/provider/LDReactContext.tsx","kind":"import-statement","original":"../provider/LDReactContext"}],"format":"esm"},"src/client/hooks/useVariationCore.ts":{"bytes":1520,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"src/client/provider/LDReactContext.tsx","kind":"import-statement","original":"../provider/LDReactContext"}],"format":"esm"},"src/client/hooks/useVariation.ts":{"bytes":2669,"imports":[{"path":"src/client/hooks/useVariationCore.ts","kind":"import-statement","original":"./useVariationCore"}],"format":"esm"},"src/client/hooks/useVariationDetail.ts":{"bytes":3180,"imports":[{"path":"src/client/hooks/useVariationCore.ts","kind":"import-statement","original":"./useVariationCore"}],"format":"esm"},"src/client/hooks/index.ts":{"bytes":451,"imports":[{"path":"src/client/hooks/useInitializationStatus.ts","kind":"import-statement","original":"./useInitializationStatus"},{"path":"src/client/hooks/useLDClient.ts","kind":"import-statement","original":"./useLDClient"},{"path":"src/client/hooks/useVariation.ts","kind":"import-statement","original":"./useVariation"},{"path":"src/client/hooks/useVariationDetail.ts","kind":"import-statement","original":"./useVariationDetail"}],"format":"esm"},"src/client/index.ts":{"bytes":399,"imports":[{"path":"src/client/LDClient.ts","kind":"import-statement","original":"./LDClient"},{"path":"src/client/LDOptions.ts","kind":"import-statement","original":"./LDOptions"},{"path":"src/client/provider/LDReactContext.tsx","kind":"import-statement","original":"./provider/LDReactContext"},{"path":"src/client/provider/LDReactProvider.tsx","kind":"import-statement","original":"./provider/LDReactProvider"},{"path":"src/client/LDReactClient.tsx","kind":"import-statement","original":"./LDReactClient"},{"path":"src/client/deprecated-hooks/index.ts","kind":"import-statement","original":"./deprecated-hooks"},{"path":"src/client/hooks/index.ts","kind":"import-statement","original":"./hooks"}],"format":"esm"}},"outputs":{"dist/index.js":{"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true}],"exports":["LDReactContext","createClient","createLDReactProvider","createLDReactProviderWithClient","initLDReactContext","useBoolVariation","useBoolVariationDetail","useFlags","useInitializationStatus","useJsonVariation","useJsonVariationDetail","useLDClient","useNumberVariation","useNumberVariationDetail","useStringVariation","useStringVariationDetail"],"entryPoint":"src/client/index.ts","inputs":{"src/client/index.ts":{"bytesInOutput":0},"src/client/provider/LDReactContext.tsx":{"bytesInOutput":80},"src/client/provider/LDReactProvider.tsx":{"bytesInOutput":802},"../browser/dist/index.js":{"bytesInOutput":87201},"src/client/LDReactClient.tsx":{"bytesInOutput":1807},"src/client/deprecated-hooks/useFlags.ts":{"bytesInOutput":901},"src/client/deprecated-hooks/flagKeyUtils.ts":{"bytesInOutput":233},"src/client/deprecated-hooks/index.ts":{"bytesInOutput":0},"src/client/hooks/useInitializationStatus.ts":{"bytesInOutput":156},"src/client/hooks/index.ts":{"bytesInOutput":0},"src/client/hooks/useLDClient.ts":{"bytesInOutput":90},"src/client/hooks/useVariationCore.ts":{"bytesInOutput":393},"src/client/hooks/useVariation.ts":{"bytesInOutput":264},"src/client/hooks/useVariationDetail.ts":{"bytesInOutput":288}},"bytes":93092}}}
@@ -0,0 +1 @@
1
+ "use strict";var t=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var p=(r,i)=>{for(var e in i)t(r,e,{get:i[e],enumerable:!0})},L=(r,i,e,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let n of u(i))!m.call(r,n)&&n!==e&&t(r,n,{get:()=>i[n],enumerable:!(o=S(i,n))||o.enumerable});return r};var f=r=>L(t({},"__esModule",{value:!0}),r);var b={};p(b,{createLDServerSession:()=>v,createLDServerWrapper:()=>D,useLDServerSession:()=>V});module.exports=f(b);var a=require("react"),s=(0,a.cache)(()=>({session:null}));function l(){return typeof window=="undefined"}function D(r,i){if(!l())throw new Error("createLDServerWrapper must only be called on the server. Ensure this module is not imported from client components.");return{initialized:()=>r.initialized(),getContext:()=>i,boolVariation:(e,o)=>r.boolVariation(e,i,o),numberVariation:(e,o)=>r.numberVariation(e,i,o),stringVariation:(e,o)=>r.stringVariation(e,i,o),jsonVariation:(e,o)=>r.jsonVariation(e,i,o),boolVariationDetail:(e,o)=>r.boolVariationDetail(e,i,o),numberVariationDetail:(e,o)=>r.numberVariationDetail(e,i,o),stringVariationDetail:(e,o)=>r.stringVariationDetail(e,i,o),jsonVariationDetail:(e,o)=>r.jsonVariationDetail(e,i,o),allFlagsState:e=>r.allFlagsState(i,e)}}function v(r,i){let e=D(r,i);return s().session=e,e}function V(){if(!l())throw new Error("useLDServerSession must only be called on the server. Ensure this module is not imported from client components.");let{session:r}=s();return r||null}0&&(module.exports={createLDServerSession,createLDServerWrapper,useLDServerSession});
@@ -0,0 +1,197 @@
1
+ import { LDContext, LDEvaluationDetailTyped, LDFlagsStateOptions, LDFlagsState } from '@launchdarkly/js-server-sdk-common';
2
+
3
+ /**
4
+ * A per-request evaluation scope that binds an {@link LDServerBaseClient} to a specific
5
+ * {@link LDContext}.
6
+ *
7
+ * @remarks
8
+ * This is idiomatic for React Server Components, where the context comes from the incoming
9
+ * request (headers, cookies, auth tokens) and does not change during the render.
10
+ *
11
+ * Create a session with {@link createLDServerSession}.
12
+ */
13
+ interface LDServerSession {
14
+ /**
15
+ * Tests whether the underlying server client has completed initialization.
16
+ *
17
+ * @returns True if the client has successfully initialized.
18
+ */
19
+ initialized(): boolean;
20
+ /**
21
+ * Returns the context bound to this session.
22
+ */
23
+ getContext(): LDContext;
24
+ /**
25
+ * Determines the boolean variation of a feature flag for this session's context.
26
+ */
27
+ boolVariation(key: string, defaultValue: boolean): Promise<boolean>;
28
+ /**
29
+ * Determines the numeric variation of a feature flag for this session's context.
30
+ */
31
+ numberVariation(key: string, defaultValue: number): Promise<number>;
32
+ /**
33
+ * Determines the string variation of a feature flag for this session's context.
34
+ */
35
+ stringVariation(key: string, defaultValue: string): Promise<string>;
36
+ /**
37
+ * Determines the JSON variation of a feature flag for this session's context.
38
+ *
39
+ * This version is preferred in TypeScript because it returns `unknown` instead of `any`,
40
+ * requiring an explicit cast before use.
41
+ */
42
+ jsonVariation(key: string, defaultValue: unknown): Promise<unknown>;
43
+ /**
44
+ * Determines the boolean variation of a feature flag, along with evaluation details.
45
+ */
46
+ boolVariationDetail(key: string, defaultValue: boolean): Promise<LDEvaluationDetailTyped<boolean>>;
47
+ /**
48
+ * Determines the numeric variation of a feature flag, along with evaluation details.
49
+ */
50
+ numberVariationDetail(key: string, defaultValue: number): Promise<LDEvaluationDetailTyped<number>>;
51
+ /**
52
+ * Determines the string variation of a feature flag, along with evaluation details.
53
+ */
54
+ stringVariationDetail(key: string, defaultValue: string): Promise<LDEvaluationDetailTyped<string>>;
55
+ /**
56
+ * Determines the JSON variation of a feature flag, along with evaluation details.
57
+ */
58
+ jsonVariationDetail(key: string, defaultValue: unknown): Promise<LDEvaluationDetailTyped<unknown>>;
59
+ /**
60
+ * Builds an object encapsulating the state of all feature flags for this session's context.
61
+ *
62
+ * The most common use case is bootstrapping client-side flags from a back-end service.
63
+ * Call `toJSON()` on the returned object to get the data structure used by the client SDK.
64
+ */
65
+ allFlagsState(options?: LDFlagsStateOptions): Promise<LDFlagsState>;
66
+ }
67
+
68
+ /**
69
+ * A minimal structural interface that any LaunchDarkly server SDK that can be used with
70
+ * {@link createLDServerSession} should satisfy.
71
+ *
72
+ * @remarks
73
+ * This interface decouples the React SDK from the concrete `LDClient` type in
74
+ * `@launchdarkly/js-server-sdk-common`, allowing edge SDKs and other custom
75
+ * server client implementations to be used with {@link createLDServerSession}.
76
+ *
77
+ * @see {@link https://launchdarkly.github.io/js-core/packages/shared/sdk-server/docs/interfaces/LDClient.html}
78
+ * for more information.
79
+ */
80
+ interface LDServerBaseClient {
81
+ /**
82
+ * Tests whether the client has completed initialization.
83
+ *
84
+ * @returns True if the client has successfully initialized.
85
+ */
86
+ initialized(): boolean;
87
+ /**
88
+ * Determines the boolean variation of a feature flag for a context.
89
+ */
90
+ boolVariation(key: string, context: LDContext, defaultValue: boolean): Promise<boolean>;
91
+ /**
92
+ * Determines the numeric variation of a feature flag for a context.
93
+ */
94
+ numberVariation(key: string, context: LDContext, defaultValue: number): Promise<number>;
95
+ /**
96
+ * Determines the string variation of a feature flag for a context.
97
+ */
98
+ stringVariation(key: string, context: LDContext, defaultValue: string): Promise<string>;
99
+ /**
100
+ * Determines the JSON variation of a feature flag for a context.
101
+ *
102
+ * This version is preferred in TypeScript because it returns `unknown` instead of `any`,
103
+ * requiring an explicit cast before use.
104
+ */
105
+ jsonVariation(key: string, context: LDContext, defaultValue: unknown): Promise<unknown>;
106
+ /**
107
+ * Determines the boolean variation of a feature flag, along with evaluation details.
108
+ */
109
+ boolVariationDetail(key: string, context: LDContext, defaultValue: boolean): Promise<LDEvaluationDetailTyped<boolean>>;
110
+ /**
111
+ * Determines the numeric variation of a feature flag, along with evaluation details.
112
+ */
113
+ numberVariationDetail(key: string, context: LDContext, defaultValue: number): Promise<LDEvaluationDetailTyped<number>>;
114
+ /**
115
+ * Determines the string variation of a feature flag, along with evaluation details.
116
+ */
117
+ stringVariationDetail(key: string, context: LDContext, defaultValue: string): Promise<LDEvaluationDetailTyped<string>>;
118
+ /**
119
+ * Determines the JSON variation of a feature flag, along with evaluation details.
120
+ */
121
+ jsonVariationDetail(key: string, context: LDContext, defaultValue: unknown): Promise<LDEvaluationDetailTyped<unknown>>;
122
+ /**
123
+ * Builds an object encapsulating the state of all feature flags for a given context.
124
+ */
125
+ allFlagsState(context: LDContext, options?: LDFlagsStateOptions): Promise<LDFlagsState>;
126
+ }
127
+
128
+ /**
129
+ * Creates a LaunchDarkly server SDK client that is scoped to a specific context.
130
+ *
131
+ * @remarks
132
+ * **NOTE:** We recommend using the {@link createLDServerSession} function to create your server session
133
+ * instead of directly calling this function.
134
+ *
135
+ * This function is provided to allow the caller to have more control over handling their scoped LD client.
136
+ * If using this function, the caller is responsible for managing the lifecycle of the created wrapped client.
137
+ *
138
+ * @throws {Error} If called in a browser environment. This function must only be called on the
139
+ * server. Ensure the module that calls this is not imported from client components.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * // lib/ld-server.ts
144
+ * import { init } from '@launchdarkly/node-server-sdk';
145
+ * import { createLDServerSession } from '@launchdarkly/react-sdk/server';
146
+ *
147
+ * const ldBaseClient = await init(process.env.LAUNCHDARKLY_SDK_KEY || '');
148
+ * export const serverSession = createLDServerSession(ldBaseClient, defaultContext);
149
+ * ```
150
+ *
151
+ * @param client Any LaunchDarkly server SDK client that satisfies {@link LDServerBaseClient}.
152
+ * @param context The context to bind to this session. Typically resolved from the request
153
+ * (e.g. from auth tokens, cookies, or headers).
154
+ * @returns An {@link LDServerSession} scoped to the given context.
155
+ */
156
+ declare function createLDServerWrapper(client: LDServerBaseClient, context: LDContext): LDServerSession;
157
+ /**
158
+ * Creates a per-request evaluation scope by binding an {@link LDServerBaseClient} to a specific
159
+ * context.
160
+ *
161
+ * @param client Any LaunchDarkly server SDK client that satisfies {@link LDServerBaseClient}.
162
+ * @param context The context to bind to this session. Typically resolved from the request
163
+ * (e.g. from auth tokens, cookies, or headers).
164
+ * @returns An {@link LDServerSession} scoped to the given context.
165
+ */
166
+ declare function createLDServerSession(client: LDServerBaseClient, context: LDContext): LDServerSession;
167
+ /**
168
+ * Returns the {@link LDServerSession} scoped to the current request.
169
+ *
170
+ * @remarks
171
+ * **NOTE:** This function is only used to retrieve the session stored by {@link createLDServerSession}.
172
+ * You must call {@link createLDServerSession} before calling this function or it will return a null value.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * // app.tsx (entry point)
177
+ * import { createLDServerSession } from '@launchdarkly/react-sdk/server';
178
+ * const session = createLDServerSession(client, context);
179
+ *
180
+ * // component.ts
181
+ * import { useLDServerSession } from '@launchdarkly/react-sdk/server';
182
+ *
183
+ * export default function MyComponent() {
184
+ * const session = useLDServerSession();
185
+ * if (session) {
186
+ * const flagValue = await session.boolVariation('my-flag', false);
187
+ * return <div>{flagValue ? 'Yes' : 'No'}</div>;
188
+ * }
189
+ * return <div>Loading...</div>;
190
+ * }
191
+ * ```
192
+ *
193
+ * @returns The {@link LDServerSession} scoped to the current request, or `null` if no session has been created.
194
+ */
195
+ declare function useLDServerSession(): LDServerSession | null;
196
+
197
+ export { type LDServerBaseClient, type LDServerSession, createLDServerSession, createLDServerWrapper, useLDServerSession };
@@ -0,0 +1,197 @@
1
+ import { LDContext, LDEvaluationDetailTyped, LDFlagsStateOptions, LDFlagsState } from '@launchdarkly/js-server-sdk-common';
2
+
3
+ /**
4
+ * A per-request evaluation scope that binds an {@link LDServerBaseClient} to a specific
5
+ * {@link LDContext}.
6
+ *
7
+ * @remarks
8
+ * This is idiomatic for React Server Components, where the context comes from the incoming
9
+ * request (headers, cookies, auth tokens) and does not change during the render.
10
+ *
11
+ * Create a session with {@link createLDServerSession}.
12
+ */
13
+ interface LDServerSession {
14
+ /**
15
+ * Tests whether the underlying server client has completed initialization.
16
+ *
17
+ * @returns True if the client has successfully initialized.
18
+ */
19
+ initialized(): boolean;
20
+ /**
21
+ * Returns the context bound to this session.
22
+ */
23
+ getContext(): LDContext;
24
+ /**
25
+ * Determines the boolean variation of a feature flag for this session's context.
26
+ */
27
+ boolVariation(key: string, defaultValue: boolean): Promise<boolean>;
28
+ /**
29
+ * Determines the numeric variation of a feature flag for this session's context.
30
+ */
31
+ numberVariation(key: string, defaultValue: number): Promise<number>;
32
+ /**
33
+ * Determines the string variation of a feature flag for this session's context.
34
+ */
35
+ stringVariation(key: string, defaultValue: string): Promise<string>;
36
+ /**
37
+ * Determines the JSON variation of a feature flag for this session's context.
38
+ *
39
+ * This version is preferred in TypeScript because it returns `unknown` instead of `any`,
40
+ * requiring an explicit cast before use.
41
+ */
42
+ jsonVariation(key: string, defaultValue: unknown): Promise<unknown>;
43
+ /**
44
+ * Determines the boolean variation of a feature flag, along with evaluation details.
45
+ */
46
+ boolVariationDetail(key: string, defaultValue: boolean): Promise<LDEvaluationDetailTyped<boolean>>;
47
+ /**
48
+ * Determines the numeric variation of a feature flag, along with evaluation details.
49
+ */
50
+ numberVariationDetail(key: string, defaultValue: number): Promise<LDEvaluationDetailTyped<number>>;
51
+ /**
52
+ * Determines the string variation of a feature flag, along with evaluation details.
53
+ */
54
+ stringVariationDetail(key: string, defaultValue: string): Promise<LDEvaluationDetailTyped<string>>;
55
+ /**
56
+ * Determines the JSON variation of a feature flag, along with evaluation details.
57
+ */
58
+ jsonVariationDetail(key: string, defaultValue: unknown): Promise<LDEvaluationDetailTyped<unknown>>;
59
+ /**
60
+ * Builds an object encapsulating the state of all feature flags for this session's context.
61
+ *
62
+ * The most common use case is bootstrapping client-side flags from a back-end service.
63
+ * Call `toJSON()` on the returned object to get the data structure used by the client SDK.
64
+ */
65
+ allFlagsState(options?: LDFlagsStateOptions): Promise<LDFlagsState>;
66
+ }
67
+
68
+ /**
69
+ * A minimal structural interface that any LaunchDarkly server SDK that can be used with
70
+ * {@link createLDServerSession} should satisfy.
71
+ *
72
+ * @remarks
73
+ * This interface decouples the React SDK from the concrete `LDClient` type in
74
+ * `@launchdarkly/js-server-sdk-common`, allowing edge SDKs and other custom
75
+ * server client implementations to be used with {@link createLDServerSession}.
76
+ *
77
+ * @see {@link https://launchdarkly.github.io/js-core/packages/shared/sdk-server/docs/interfaces/LDClient.html}
78
+ * for more information.
79
+ */
80
+ interface LDServerBaseClient {
81
+ /**
82
+ * Tests whether the client has completed initialization.
83
+ *
84
+ * @returns True if the client has successfully initialized.
85
+ */
86
+ initialized(): boolean;
87
+ /**
88
+ * Determines the boolean variation of a feature flag for a context.
89
+ */
90
+ boolVariation(key: string, context: LDContext, defaultValue: boolean): Promise<boolean>;
91
+ /**
92
+ * Determines the numeric variation of a feature flag for a context.
93
+ */
94
+ numberVariation(key: string, context: LDContext, defaultValue: number): Promise<number>;
95
+ /**
96
+ * Determines the string variation of a feature flag for a context.
97
+ */
98
+ stringVariation(key: string, context: LDContext, defaultValue: string): Promise<string>;
99
+ /**
100
+ * Determines the JSON variation of a feature flag for a context.
101
+ *
102
+ * This version is preferred in TypeScript because it returns `unknown` instead of `any`,
103
+ * requiring an explicit cast before use.
104
+ */
105
+ jsonVariation(key: string, context: LDContext, defaultValue: unknown): Promise<unknown>;
106
+ /**
107
+ * Determines the boolean variation of a feature flag, along with evaluation details.
108
+ */
109
+ boolVariationDetail(key: string, context: LDContext, defaultValue: boolean): Promise<LDEvaluationDetailTyped<boolean>>;
110
+ /**
111
+ * Determines the numeric variation of a feature flag, along with evaluation details.
112
+ */
113
+ numberVariationDetail(key: string, context: LDContext, defaultValue: number): Promise<LDEvaluationDetailTyped<number>>;
114
+ /**
115
+ * Determines the string variation of a feature flag, along with evaluation details.
116
+ */
117
+ stringVariationDetail(key: string, context: LDContext, defaultValue: string): Promise<LDEvaluationDetailTyped<string>>;
118
+ /**
119
+ * Determines the JSON variation of a feature flag, along with evaluation details.
120
+ */
121
+ jsonVariationDetail(key: string, context: LDContext, defaultValue: unknown): Promise<LDEvaluationDetailTyped<unknown>>;
122
+ /**
123
+ * Builds an object encapsulating the state of all feature flags for a given context.
124
+ */
125
+ allFlagsState(context: LDContext, options?: LDFlagsStateOptions): Promise<LDFlagsState>;
126
+ }
127
+
128
+ /**
129
+ * Creates a LaunchDarkly server SDK client that is scoped to a specific context.
130
+ *
131
+ * @remarks
132
+ * **NOTE:** We recommend using the {@link createLDServerSession} function to create your server session
133
+ * instead of directly calling this function.
134
+ *
135
+ * This function is provided to allow the caller to have more control over handling their scoped LD client.
136
+ * If using this function, the caller is responsible for managing the lifecycle of the created wrapped client.
137
+ *
138
+ * @throws {Error} If called in a browser environment. This function must only be called on the
139
+ * server. Ensure the module that calls this is not imported from client components.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * // lib/ld-server.ts
144
+ * import { init } from '@launchdarkly/node-server-sdk';
145
+ * import { createLDServerSession } from '@launchdarkly/react-sdk/server';
146
+ *
147
+ * const ldBaseClient = await init(process.env.LAUNCHDARKLY_SDK_KEY || '');
148
+ * export const serverSession = createLDServerSession(ldBaseClient, defaultContext);
149
+ * ```
150
+ *
151
+ * @param client Any LaunchDarkly server SDK client that satisfies {@link LDServerBaseClient}.
152
+ * @param context The context to bind to this session. Typically resolved from the request
153
+ * (e.g. from auth tokens, cookies, or headers).
154
+ * @returns An {@link LDServerSession} scoped to the given context.
155
+ */
156
+ declare function createLDServerWrapper(client: LDServerBaseClient, context: LDContext): LDServerSession;
157
+ /**
158
+ * Creates a per-request evaluation scope by binding an {@link LDServerBaseClient} to a specific
159
+ * context.
160
+ *
161
+ * @param client Any LaunchDarkly server SDK client that satisfies {@link LDServerBaseClient}.
162
+ * @param context The context to bind to this session. Typically resolved from the request
163
+ * (e.g. from auth tokens, cookies, or headers).
164
+ * @returns An {@link LDServerSession} scoped to the given context.
165
+ */
166
+ declare function createLDServerSession(client: LDServerBaseClient, context: LDContext): LDServerSession;
167
+ /**
168
+ * Returns the {@link LDServerSession} scoped to the current request.
169
+ *
170
+ * @remarks
171
+ * **NOTE:** This function is only used to retrieve the session stored by {@link createLDServerSession}.
172
+ * You must call {@link createLDServerSession} before calling this function or it will return a null value.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * // app.tsx (entry point)
177
+ * import { createLDServerSession } from '@launchdarkly/react-sdk/server';
178
+ * const session = createLDServerSession(client, context);
179
+ *
180
+ * // component.ts
181
+ * import { useLDServerSession } from '@launchdarkly/react-sdk/server';
182
+ *
183
+ * export default function MyComponent() {
184
+ * const session = useLDServerSession();
185
+ * if (session) {
186
+ * const flagValue = await session.boolVariation('my-flag', false);
187
+ * return <div>{flagValue ? 'Yes' : 'No'}</div>;
188
+ * }
189
+ * return <div>Loading...</div>;
190
+ * }
191
+ * ```
192
+ *
193
+ * @returns The {@link LDServerSession} scoped to the current request, or `null` if no session has been created.
194
+ */
195
+ declare function useLDServerSession(): LDServerSession | null;
196
+
197
+ export { type LDServerBaseClient, type LDServerSession, createLDServerSession, createLDServerWrapper, useLDServerSession };
package/dist/server.js ADDED
@@ -0,0 +1 @@
1
+ import{cache as a}from"react";var n=a(()=>({session:null}));function t(){return typeof window=="undefined"}function s(r,o){if(!t())throw new Error("createLDServerWrapper must only be called on the server. Ensure this module is not imported from client components.");return{initialized:()=>r.initialized(),getContext:()=>o,boolVariation:(e,i)=>r.boolVariation(e,o,i),numberVariation:(e,i)=>r.numberVariation(e,o,i),stringVariation:(e,i)=>r.stringVariation(e,o,i),jsonVariation:(e,i)=>r.jsonVariation(e,o,i),boolVariationDetail:(e,i)=>r.boolVariationDetail(e,o,i),numberVariationDetail:(e,i)=>r.numberVariationDetail(e,o,i),stringVariationDetail:(e,i)=>r.stringVariationDetail(e,o,i),jsonVariationDetail:(e,i)=>r.jsonVariationDetail(e,o,i),allFlagsState:e=>r.allFlagsState(o,e)}}function D(r,o){let e=s(r,o);return n().session=e,e}function S(){if(!t())throw new Error("useLDServerSession must only be called on the server. Ensure this module is not imported from client components.");let{session:r}=n();return r||null}export{D as createLDServerSession,s as createLDServerWrapper,S as useLDServerSession};
package/package.json CHANGED
@@ -1,5 +1,78 @@
1
1
  {
2
2
  "name": "@launchdarkly/react-sdk",
3
- "version": "0.0.0",
4
- "description": ""
5
- }
3
+ "version": "0.1.0",
4
+ "description": "LaunchDarkly SDK for React frameworks",
5
+ "homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/launchdarkly/js-core.git"
9
+ },
10
+ "license": "Apache-2.0",
11
+ "packageManager": "yarn@4.2.2",
12
+ "keywords": [
13
+ "launchdarkly",
14
+ "react",
15
+ "isomorphic",
16
+ "nextjs",
17
+ "remix"
18
+ ],
19
+ "scripts": {
20
+ "clean": "rm -rf dist",
21
+ "build": "tsup",
22
+ "test": "npx jest --ci",
23
+ "lint": "eslint . --ext .ts,.tsx",
24
+ "prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore"
25
+ },
26
+ "type": "module",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js",
31
+ "require": "./dist/index.cjs",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./server": {
35
+ "types": "./dist/server.d.ts",
36
+ "import": "./dist/server.js",
37
+ "require": "./dist/server.cjs",
38
+ "default": "./dist/server.js"
39
+ }
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "devDependencies": {
45
+ "@testing-library/jest-dom": "^6.6.3",
46
+ "@testing-library/react": "^16.0.0",
47
+ "@trivago/prettier-plugin-sort-imports": "^4.1.1",
48
+ "@types/jest": "^29.5.0",
49
+ "@types/react": "^19.2.14",
50
+ "@types/react-dom": "^19.2.3",
51
+ "@typescript-eslint/eslint-plugin": "^6.20.0",
52
+ "@typescript-eslint/parser": "^6.20.0",
53
+ "eslint": "^8.45.0",
54
+ "eslint-config-airbnb-base": "^15.0.0",
55
+ "eslint-config-airbnb-typescript": "^17.1.0",
56
+ "eslint-config-prettier": "^8.8.0",
57
+ "eslint-plugin-import": "^2.27.5",
58
+ "eslint-plugin-jest": "^27.6.3",
59
+ "eslint-plugin-prettier": "^5.0.0",
60
+ "jest": "^30.2.0",
61
+ "jest-environment-jsdom": "^30.0.0",
62
+ "prettier": "^3.8.1",
63
+ "react": "^19.2.4",
64
+ "react-dom": "^19.2.4",
65
+ "tsup": "^8.5.1",
66
+ "tsup-plugin-prepend-directive": "^0.1.2",
67
+ "typedoc": "0.25.0",
68
+ "typescript": "5.1.6"
69
+ },
70
+ "dependencies": {
71
+ "@launchdarkly/js-client-sdk": "^4.4.1",
72
+ "@launchdarkly/js-server-sdk-common": "^2.18.3"
73
+ },
74
+ "peerDependencies": {
75
+ "react": ">=18.0.0",
76
+ "react-dom": ">=18.0.0"
77
+ }
78
+ }