Skip to content

Commit fa88d65

Browse files
committed
feat: add debug plugin
- Emit structured debug events to window - Optional console logging - Respects debug.enabled config - Listens to experiences:* events - Exposes debug.log() and debug.isEnabled() Closes #4
1 parent 0f4e801 commit fa88d65

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Debug Plugin
3+
*
4+
* Emits structured debug events to window and optionally logs to console.
5+
* Useful for debugging and Chrome extension integration.
6+
*/
7+
8+
import type { PluginFunction } from '@lytics/sdk-kit';
9+
10+
export interface DebugPluginConfig {
11+
debug?: {
12+
enabled?: boolean;
13+
console?: boolean;
14+
window?: boolean;
15+
};
16+
}
17+
18+
export interface DebugPlugin {
19+
log(message: string, data?: unknown): void;
20+
isEnabled(): boolean;
21+
}
22+
23+
/**
24+
* Debug Plugin
25+
*
26+
* Listens to all SDK events and emits them as window events for debugging.
27+
* Also optionally logs to console.
28+
*
29+
* @example
30+
* ```typescript
31+
* import { createInstance } from '@prosdevlab/experience-sdk';
32+
* import { debugPlugin } from '@prosdevlab/experience-sdk-plugins';
33+
*
34+
* const sdk = createInstance({ debug: { enabled: true, console: true } });
35+
* sdk.use(debugPlugin);
36+
* ```
37+
*/
38+
export const debugPlugin: PluginFunction = (plugin, instance, config) => {
39+
plugin.ns('debug');
40+
41+
// Set defaults
42+
plugin.defaults({
43+
debug: {
44+
enabled: false,
45+
console: false,
46+
window: true,
47+
},
48+
});
49+
50+
// Helper to check if debug is enabled
51+
const isEnabled = (): boolean => config.get('debug.enabled') ?? false;
52+
const shouldLogConsole = (): boolean => config.get('debug.console') ?? false;
53+
const shouldEmitWindow = (): boolean => config.get('debug.window') ?? true;
54+
55+
// Log function
56+
const log = (message: string, data?: unknown): void => {
57+
if (!isEnabled()) return;
58+
59+
const timestamp = new Date().toISOString();
60+
const logData = {
61+
timestamp,
62+
message,
63+
data,
64+
};
65+
66+
// Console logging
67+
if (shouldLogConsole()) {
68+
console.log(`[experiences] ${message}`, data || '');
69+
}
70+
71+
// Window event emission
72+
if (shouldEmitWindow() && typeof window !== 'undefined') {
73+
const event = new CustomEvent('experience-sdk:debug', {
74+
detail: logData,
75+
});
76+
window.dispatchEvent(event);
77+
}
78+
};
79+
80+
// Expose debug API
81+
plugin.expose({
82+
debug: {
83+
log,
84+
isEnabled,
85+
},
86+
});
87+
88+
// If debug is enabled, listen to all events
89+
if (isEnabled()) {
90+
// Listen to experiences:* events
91+
instance.on('experiences:ready', () => {
92+
log('SDK initialized and ready');
93+
});
94+
95+
instance.on('experiences:registered', (payload) => {
96+
log('Experience registered', payload);
97+
});
98+
99+
instance.on('experiences:evaluated', (payload) => {
100+
log('Experience evaluated', payload);
101+
});
102+
103+
// Listen to all events using wildcard
104+
instance.on('*', (eventName, ...args) => {
105+
// Skip if already logged above
106+
if (
107+
eventName === 'experiences:ready' ||
108+
eventName === 'experiences:registered' ||
109+
eventName === 'experiences:evaluated'
110+
) {
111+
return;
112+
}
113+
log(`Event: ${eventName}`, args);
114+
});
115+
}
116+
};
117+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Debug Plugin - Barrel Export
3+
*/
4+
5+
export type { DebugPlugin, DebugPluginConfig } from './debug';
6+
export { debugPlugin } from './debug';
7+

packages/plugins/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// Placeholder for plugins package
2-
// Will be implemented in Phase 0
1+
/**
2+
* Experience SDK Plugins
3+
*
4+
* Official plugins for Experience SDK
5+
*/
36

4-
export {};
7+
export * from './debug';

0 commit comments

Comments
 (0)