@nexussdk/sdk 0.0.1
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.
- package/.turbo/turbo-build.log +45 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +151 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.global.js +9 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react.cjs +2 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.mts +249 -0
- package/dist/react.d.ts +249 -0
- package/dist/react.mjs +2 -0
- package/dist/react.mjs.map +1 -0
- package/package.json +51 -0
- package/src/index.ts +27 -0
- package/src/nexus.ts +208 -0
- package/src/react.tsx +204 -0
- package/tsconfig.json +10 -0
- package/tsup.config.ts +40 -0
package/src/nexus.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Unified Nexus singleton facade wrapping Flags and Tracker clients.
|
|
3
|
+
* @module @nexus/sdk/nexus
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { NexusFlagsClient } from '@nexussdk/flags';
|
|
7
|
+
import type { NexusFlagsOptions } from '@nexussdk/flags';
|
|
8
|
+
import { NexusTrackerClient } from '@nexussdk/tracker';
|
|
9
|
+
import type { NexusTrackerOptions } from '@nexussdk/tracker';
|
|
10
|
+
import type { UserContext } from '@nexussdk/contracts';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Unified initialization options for the Nexus SDK umbrella.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Nexus.init({
|
|
17
|
+
* apiKey: 'pk_live_...',
|
|
18
|
+
* baseUrl: 'http://localhost:8080',
|
|
19
|
+
* user: { id: 'usr_12345', country: 'VN' },
|
|
20
|
+
* environment: 'production',
|
|
21
|
+
* autoCapture: true,
|
|
22
|
+
* });
|
|
23
|
+
*/
|
|
24
|
+
export interface NexusInitOptions {
|
|
25
|
+
/** Public API key. Resolved from env if omitted. */
|
|
26
|
+
apiKey?: string;
|
|
27
|
+
/** Base URL for all API calls. */
|
|
28
|
+
baseUrl?: string;
|
|
29
|
+
/** Initial user context for flag targeting and error attribution. */
|
|
30
|
+
user?: UserContext;
|
|
31
|
+
/** Target environment for telemetry routing. Defaults to 'production'. */
|
|
32
|
+
environment?: string;
|
|
33
|
+
/** Global tags attached to all telemetry events. */
|
|
34
|
+
tags?: Record<string, string>;
|
|
35
|
+
/** Toggle automated global error capture. Defaults to true. */
|
|
36
|
+
autoCapture?: boolean;
|
|
37
|
+
/** Additional flags-specific options. */
|
|
38
|
+
flags?: Partial<NexusFlagsOptions>;
|
|
39
|
+
/** Additional tracker-specific options. */
|
|
40
|
+
tracker?: Partial<NexusTrackerOptions>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The Nexus singleton class — the primary unified entry point for the SDK.
|
|
45
|
+
*
|
|
46
|
+
* Provides access to both the feature flags client and the error tracker client.
|
|
47
|
+
* Initialize once, then use throughout your application.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // Initialize (call once at app startup)
|
|
51
|
+
* Nexus.init({ apiKey: 'pk_live_...' });
|
|
52
|
+
*
|
|
53
|
+
* // Feature flags
|
|
54
|
+
* const showBanner = Nexus.isEnabled('promo_banner_v2', false);
|
|
55
|
+
*
|
|
56
|
+
* // Error tracking
|
|
57
|
+
* Nexus.captureError(new Error('Something went wrong'));
|
|
58
|
+
*
|
|
59
|
+
* // Update user context
|
|
60
|
+
* await Nexus.identify({ id: 'usr_12345', country: 'VN' });
|
|
61
|
+
*/
|
|
62
|
+
export class Nexus {
|
|
63
|
+
private static instance: Nexus | null = null;
|
|
64
|
+
|
|
65
|
+
/** The underlying feature flags client instance. */
|
|
66
|
+
public readonly flags: NexusFlagsClient;
|
|
67
|
+
/** The underlying error tracker client instance. */
|
|
68
|
+
public readonly tracker: NexusTrackerClient;
|
|
69
|
+
|
|
70
|
+
private constructor(options: NexusInitOptions = {}) {
|
|
71
|
+
const { apiKey, baseUrl, user, environment, tags, autoCapture, flags: flagsOpts, tracker: trackerOpts } = options;
|
|
72
|
+
|
|
73
|
+
this.flags = new NexusFlagsClient({
|
|
74
|
+
apiKey,
|
|
75
|
+
baseUrl,
|
|
76
|
+
user,
|
|
77
|
+
...flagsOpts,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
this.tracker = new NexusTrackerClient({
|
|
81
|
+
apiKey,
|
|
82
|
+
baseUrl,
|
|
83
|
+
environment,
|
|
84
|
+
tags,
|
|
85
|
+
autoCapture,
|
|
86
|
+
...trackerOpts,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Initializes the Nexus SDK singleton.
|
|
92
|
+
* Must be called before any other SDK methods.
|
|
93
|
+
* Safe to call multiple times — returns existing instance after first init.
|
|
94
|
+
*
|
|
95
|
+
* @param options - SDK configuration options.
|
|
96
|
+
* @returns The initialized Nexus singleton instance.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* const nexus = Nexus.init({ apiKey: 'pk_live_...' });
|
|
100
|
+
*/
|
|
101
|
+
public static init(options: NexusInitOptions = {}): Nexus {
|
|
102
|
+
if (!Nexus.instance) {
|
|
103
|
+
Nexus.instance = new Nexus(options);
|
|
104
|
+
}
|
|
105
|
+
return Nexus.instance;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns the current Nexus singleton instance.
|
|
110
|
+
*
|
|
111
|
+
* @returns The active Nexus instance.
|
|
112
|
+
* @throws {Error} If `Nexus.init()` has not been called yet.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* const nexus = Nexus.getInstance();
|
|
116
|
+
* nexus.flags.isEnabled('checkout_v2');
|
|
117
|
+
*/
|
|
118
|
+
public static getInstance(): Nexus {
|
|
119
|
+
if (!Nexus.instance) {
|
|
120
|
+
throw new Error('[Nexus SDK] Not initialized. Call Nexus.init({ apiKey: "..." }) first.');
|
|
121
|
+
}
|
|
122
|
+
return Nexus.instance;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Convenience method: Check if a feature flag is enabled.
|
|
127
|
+
*
|
|
128
|
+
* @param key - Flag identifier.
|
|
129
|
+
* @param defaultValue - Fallback if flag is missing.
|
|
130
|
+
* @returns Boolean enabled state.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* if (Nexus.isEnabled('checkout_redesign')) { ... }
|
|
134
|
+
*/
|
|
135
|
+
public static isEnabled(key: string, defaultValue = false): boolean {
|
|
136
|
+
return Nexus.getInstance().flags.isEnabled(key, defaultValue);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Convenience method: Get a flag variant value.
|
|
141
|
+
*
|
|
142
|
+
* @param key - Flag identifier.
|
|
143
|
+
* @param variantKey - Variant property name.
|
|
144
|
+
* @param defaultValue - Fallback value.
|
|
145
|
+
* @returns Variant value cast to type T.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* const rate = Nexus.getVariant<number>('promo_banner_v2', 'discount_rate', 10);
|
|
149
|
+
*/
|
|
150
|
+
public static getVariant<T = unknown>(key: string, variantKey: string, defaultValue?: T): T {
|
|
151
|
+
return Nexus.getInstance().flags.getVariant<T>(key, variantKey, defaultValue);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Convenience method: Capture an error manually.
|
|
156
|
+
*
|
|
157
|
+
* @param error - Error instance, string, or unknown value.
|
|
158
|
+
* @param extra - Optional metadata tags.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* Nexus.captureError(new TypeError('Cannot read properties of null'));
|
|
162
|
+
*/
|
|
163
|
+
public static captureError(error: unknown, extra?: Record<string, unknown>): void {
|
|
164
|
+
Nexus.getInstance().tracker.captureError(error, extra);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Convenience method: Update user context for both flags and tracker.
|
|
169
|
+
*
|
|
170
|
+
* @param user - New user context (merged with existing).
|
|
171
|
+
* @returns Promise resolving after flags refresh.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* await Nexus.identify({ id: 'usr_12345', country: 'VN' });
|
|
175
|
+
*/
|
|
176
|
+
public static async identify(user: UserContext): Promise<void> {
|
|
177
|
+
const instance = Nexus.getInstance();
|
|
178
|
+
instance.tracker.setUser(user);
|
|
179
|
+
await instance.flags.identify(user);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Resets user context to anonymous state (e.g. on logout).
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* Nexus.reset(); // called on user logout
|
|
187
|
+
*/
|
|
188
|
+
public static reset(): void {
|
|
189
|
+
const instance = Nexus.getInstance();
|
|
190
|
+
instance.tracker.setUser(null);
|
|
191
|
+
instance.flags.reset();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Gracefully tears down both clients, closing SSE connections and flushing pending events.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* await Nexus.destroy();
|
|
199
|
+
*/
|
|
200
|
+
public static async destroy(): Promise<void> {
|
|
201
|
+
if (Nexus.instance) {
|
|
202
|
+
await Nexus.instance.tracker.flush();
|
|
203
|
+
Nexus.instance.tracker.destroy();
|
|
204
|
+
Nexus.instance.flags.destroy();
|
|
205
|
+
Nexus.instance = null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
package/src/react.tsx
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview React Context Provider and hooks for the Nexus SDK umbrella.
|
|
5
|
+
* @module @nexus/sdk/react
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {
|
|
9
|
+
createContext,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
useRef,
|
|
14
|
+
useState,
|
|
15
|
+
} from 'react';
|
|
16
|
+
import { Nexus } from './nexus.js';
|
|
17
|
+
import type { NexusInitOptions } from './nexus.js';
|
|
18
|
+
import type { FlagEvaluationResult } from '@nexussdk/contracts';
|
|
19
|
+
import type { NexusFlagsClient } from '@nexussdk/flags';
|
|
20
|
+
import type { NexusTrackerClient } from '@nexussdk/tracker';
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Context Definition
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
interface NexusContextValue {
|
|
27
|
+
flags: NexusFlagsClient;
|
|
28
|
+
tracker: NexusTrackerClient;
|
|
29
|
+
nexus: Nexus;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const NexusContext = createContext<NexusContextValue | null>(null);
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Provider
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Props for the NexusProvider component.
|
|
40
|
+
*/
|
|
41
|
+
export interface NexusProviderProps extends NexusInitOptions {
|
|
42
|
+
/** Child components that will have access to Nexus context. */
|
|
43
|
+
children?: React.ReactNode | any;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* React Context Provider that initializes the Nexus SDK and makes it available
|
|
48
|
+
* to all descendant components via `useFlag` and `useNexus` hooks.
|
|
49
|
+
*
|
|
50
|
+
* Mount once at the root of your application (e.g. in `layout.tsx`).
|
|
51
|
+
*
|
|
52
|
+
* @param props - Provider configuration options (see {@link NexusProviderProps}).
|
|
53
|
+
* @returns Provider-wrapped children.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // app/layout.tsx
|
|
57
|
+
* import { NexusProvider } from '@nexussdk/sdk/react';
|
|
58
|
+
*
|
|
59
|
+
* export default function RootLayout({ children }) {
|
|
60
|
+
* return (
|
|
61
|
+
* <html>
|
|
62
|
+
* <body>
|
|
63
|
+
* <NexusProvider apiKey={process.env.NEXT_PUBLIC_NEXUS_API_KEY}>
|
|
64
|
+
* {children}
|
|
65
|
+
* </NexusProvider>
|
|
66
|
+
* </body>
|
|
67
|
+
* </html>
|
|
68
|
+
* );
|
|
69
|
+
* }
|
|
70
|
+
*/
|
|
71
|
+
export function NexusProvider({ children, ...initOptions }: NexusProviderProps): React.ReactElement {
|
|
72
|
+
const nexusRef = useRef<Nexus | null>(null);
|
|
73
|
+
|
|
74
|
+
if (!nexusRef.current) {
|
|
75
|
+
nexusRef.current = Nexus.init(initOptions);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const contextValue = useMemo<NexusContextValue>(
|
|
79
|
+
() => ({
|
|
80
|
+
flags: nexusRef.current!.flags,
|
|
81
|
+
tracker: nexusRef.current!.tracker,
|
|
82
|
+
nexus: nexusRef.current!,
|
|
83
|
+
}),
|
|
84
|
+
[],
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
return () => {
|
|
89
|
+
void Nexus.destroy();
|
|
90
|
+
};
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
return React.createElement(NexusContext.Provider, { value: contextValue }, children);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// Hooks
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns the full Nexus SDK instance from context.
|
|
102
|
+
*
|
|
103
|
+
* @returns Object with `flags`, `tracker`, and `nexus` properties.
|
|
104
|
+
* @throws {Error} If called outside of a `NexusProvider`.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const { flags, tracker } = useNexus();
|
|
108
|
+
* const enabled = flags.isEnabled('checkout_v2');
|
|
109
|
+
* tracker.captureError(new Error('something failed'));
|
|
110
|
+
*/
|
|
111
|
+
export function useNexus(): NexusContextValue {
|
|
112
|
+
const ctx = useContext(NexusContext);
|
|
113
|
+
if (!ctx) {
|
|
114
|
+
throw new Error('[Nexus SDK] useNexus() must be called inside a <NexusProvider>.');
|
|
115
|
+
}
|
|
116
|
+
return ctx;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Return value of the `useFlag` hook.
|
|
121
|
+
*/
|
|
122
|
+
export interface UseFlagResult {
|
|
123
|
+
/** Whether the flag is currently enabled for the current user. */
|
|
124
|
+
enabled: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Retrieves a specific variant value from this flag.
|
|
127
|
+
*
|
|
128
|
+
* @param variantKey - Property name inside the variants object.
|
|
129
|
+
* @param defaultValue - Fallback if variant is not found.
|
|
130
|
+
* @returns Typed variant value.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* const { enabled, getVariant } = useFlag('promo_banner_v2');
|
|
134
|
+
* const rate = getVariant<number>('discount_rate', 10);
|
|
135
|
+
*/
|
|
136
|
+
getVariant: <T = unknown>(variantKey: string, defaultValue?: T) => T;
|
|
137
|
+
/** Full evaluation result including reason and version. */
|
|
138
|
+
result: FlagEvaluationResult | null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* React hook for subscribing to a feature flag's real-time state.
|
|
143
|
+
*
|
|
144
|
+
* Automatically re-renders when the flag changes via SSE updates.
|
|
145
|
+
* Returns a stable result without triggering additional re-renders if unchanged.
|
|
146
|
+
*
|
|
147
|
+
* @param key - Flag programmatic identifier.
|
|
148
|
+
* @param defaultEnabled - Fallback boolean if flag is not yet evaluated. Defaults to false.
|
|
149
|
+
* @returns {@link UseFlagResult} with `enabled`, `getVariant`, and `result`.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* function CheckoutButton() {
|
|
153
|
+
* const { enabled, getVariant } = useFlag('checkout_redesign');
|
|
154
|
+
* const theme = getVariant<string>('theme', 'default');
|
|
155
|
+
*
|
|
156
|
+
* return (
|
|
157
|
+
* <button className={enabled ? `btn-${theme}` : 'btn-default'}>
|
|
158
|
+
* {enabled ? 'New Checkout' : 'Checkout'}
|
|
159
|
+
* </button>
|
|
160
|
+
* );
|
|
161
|
+
* }
|
|
162
|
+
*/
|
|
163
|
+
export function useFlag(key: string, defaultEnabled = false): UseFlagResult {
|
|
164
|
+
const { flags } = useNexus();
|
|
165
|
+
|
|
166
|
+
const [result, setResult] = useState<FlagEvaluationResult | null>(() => {
|
|
167
|
+
const cached = flags.isEnabled(key) !== defaultEnabled
|
|
168
|
+
? { key, enabled: flags.isEnabled(key), variants: {}, reason: 'DEFAULT_ENABLED' as const, version: 0 }
|
|
169
|
+
: null;
|
|
170
|
+
return cached;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
// Get current state immediately
|
|
175
|
+
const currentEnabled = flags.isEnabled(key, defaultEnabled);
|
|
176
|
+
if (currentEnabled !== (result?.enabled ?? defaultEnabled)) {
|
|
177
|
+
setResult({
|
|
178
|
+
key,
|
|
179
|
+
enabled: currentEnabled,
|
|
180
|
+
variants: {},
|
|
181
|
+
reason: 'DEFAULT_ENABLED',
|
|
182
|
+
version: 0,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Subscribe to real-time updates
|
|
187
|
+
const unsubscribe = flags.onFlagChange(key, (newResult) => {
|
|
188
|
+
setResult(newResult);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return unsubscribe;
|
|
192
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
193
|
+
}, [key]);
|
|
194
|
+
|
|
195
|
+
return useMemo(
|
|
196
|
+
() => ({
|
|
197
|
+
enabled: result?.enabled ?? flags.isEnabled(key, defaultEnabled),
|
|
198
|
+
getVariant: <T = unknown>(variantKey: string, def?: T): T =>
|
|
199
|
+
flags.getVariant<T>(key, variantKey, def),
|
|
200
|
+
result,
|
|
201
|
+
}),
|
|
202
|
+
[result, key, flags, defaultEnabled],
|
|
203
|
+
);
|
|
204
|
+
}
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup';
|
|
2
|
+
|
|
3
|
+
export default defineConfig([
|
|
4
|
+
// Main bundle (no React)
|
|
5
|
+
{
|
|
6
|
+
entry: ['src/index.ts'],
|
|
7
|
+
format: ['esm', 'cjs', 'iife'],
|
|
8
|
+
globalName: 'Nexus',
|
|
9
|
+
dts: true,
|
|
10
|
+
splitting: false,
|
|
11
|
+
sourcemap: true,
|
|
12
|
+
clean: true,
|
|
13
|
+
minify: true,
|
|
14
|
+
treeshake: true,
|
|
15
|
+
target: 'es2022',
|
|
16
|
+
external: ['react', 'react-dom'],
|
|
17
|
+
outExtension({ format }) {
|
|
18
|
+
return {
|
|
19
|
+
js: format === 'esm' ? '.mjs' : format === 'cjs' ? '.cjs' : '.global.js',
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
// React sub-bundle
|
|
24
|
+
{
|
|
25
|
+
entry: { react: 'src/react.tsx' },
|
|
26
|
+
format: ['esm', 'cjs'],
|
|
27
|
+
dts: true,
|
|
28
|
+
splitting: false,
|
|
29
|
+
sourcemap: true,
|
|
30
|
+
minify: true,
|
|
31
|
+
treeshake: true,
|
|
32
|
+
target: 'es2022',
|
|
33
|
+
external: ['react', 'react-dom', '@nexussdk/flags', '@nexussdk/tracker', '@nexussdk/contracts'],
|
|
34
|
+
outExtension({ format }) {
|
|
35
|
+
return {
|
|
36
|
+
js: format === 'esm' ? '.mjs' : '.cjs',
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
]);
|