@emit-vision/sdk-js 0.1.0 → 0.3.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +51 -40
  2. package/dist/index.js +733 -421
  3. package/package.json +2 -2
package/dist/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- export type EmitVisionUser = {
1
+ type EmitVisionUser = {
2
2
  id?: string;
3
3
  email?: string;
4
4
  username?: string;
5
5
  };
6
- export type EmitVisionTraits = Omit<EmitVisionUser, "id">;
7
- export type DeploymentContext = {
6
+ type EmitVisionTraits = Omit<EmitVisionUser, "id">;
7
+ type DeploymentContext = {
8
8
  deploymentId?: string;
9
9
  buildId?: string;
10
10
  };
11
- export type FeatureFlagsContext = Record<string, string | number | boolean | null>;
12
- export type FlagEvaluationOptions = {
11
+ type FeatureFlagsContext = Record<string, string | number | boolean | null>;
12
+ type FlagEvaluationOptions = {
13
13
  /** Opaque key used to bucket the caller. Must be provided explicitly — the SDK never reads email or username automatically. */
14
14
  evaluationKey: string;
15
15
  /** Overrides the environment set at init time for this evaluation request. */
@@ -19,13 +19,19 @@ export type FlagEvaluationOptions = {
19
19
  /** Limit evaluation to specific flag keys. Omit to evaluate all active flags. */
20
20
  flagKeys?: string[];
21
21
  };
22
- export type AutoCaptureOptions = {
22
+ type AutoCaptureOptions = {
23
23
  errors?: boolean;
24
24
  unhandledRejections?: boolean;
25
25
  /** Automatically fire a `$flag_exposure` event for each flag after evaluation. Default: true. */
26
26
  flagExposures?: boolean;
27
+ /**
28
+ * Automatically fire a `$page_view` event on init and on browser navigation
29
+ * (popstate + history.pushState/replaceState). Default: false.
30
+ * The event includes `{ page: window.location.pathname }` in properties.
31
+ */
32
+ pageViews?: boolean;
27
33
  };
28
- export type CaptureOptions = {
34
+ type CaptureOptions = {
29
35
  eventId?: string;
30
36
  timestamp?: string;
31
37
  environment?: string;
@@ -37,7 +43,7 @@ export type CaptureOptions = {
37
43
  featureFlags?: FeatureFlagsContext;
38
44
  tags?: Record<string, string>;
39
45
  };
40
- export type EmitVisionOptions = {
46
+ type EmitVisionOptions = {
41
47
  apiKey?: string;
42
48
  dsn?: string;
43
49
  endpoint?: string;
@@ -56,10 +62,18 @@ export type EmitVisionOptions = {
56
62
  fetchImpl?: typeof fetch;
57
63
  /** TTL in milliseconds for in-memory flag evaluation cache. Default: 60 000 (1 minute). */
58
64
  flagEvalTtlMs?: number;
65
+ /** Initial delay (ms) before retrying after a flush failure. Default: 1000. Doubles each consecutive failure up to {@link retryBackoffMaxMs}. Set to 0 to retry immediately on every tick. */
66
+ retryBackoffInitialMs?: number;
67
+ /** Maximum delay (ms) between flush retries. Default: 30000. */
68
+ retryBackoffMaxMs?: number;
69
+ /** Disable the SDK after this many consecutive flush failures: the queue is dropped, future captures become no-ops, and a single warning is logged. Default: 20. Set to 0 to disable the cap and retry forever. */
70
+ maxConsecutiveFlushFailures?: number;
59
71
  };
60
- type RequiredEmitVisionOptions = Required<Pick<EmitVisionOptions, "apiKey" | "endpoint" | "flushIntervalMs" | "batchSize" | "fetchImpl" | "flushOnCapture" | "flagEvalTtlMs">> & Pick<EmitVisionOptions, "environment" | "release" | "sessionId" | "label" | "deployment" | "featureFlags" | "debug"> & {
72
+
73
+ type RequiredEmitVisionOptions = Required<Pick<EmitVisionOptions, "apiKey" | "endpoint" | "flushIntervalMs" | "batchSize" | "fetchImpl" | "flushOnCapture" | "flagEvalTtlMs" | "retryBackoffInitialMs" | "retryBackoffMaxMs" | "maxConsecutiveFlushFailures">> & Pick<EmitVisionOptions, "environment" | "release" | "sessionId" | "label" | "deployment" | "featureFlags" | "debug"> & {
61
74
  autoCapture: Required<AutoCaptureOptions> & {
62
75
  flagExposures: boolean;
76
+ pageViews: boolean;
63
77
  };
64
78
  };
65
79
  declare class EmitVisionClient {
@@ -73,7 +87,8 @@ declare class EmitVisionClient {
73
87
  private timer;
74
88
  private flushScheduled;
75
89
  private readonly fetchImpl;
76
- private readonly flagCache;
90
+ private readonly flagEval;
91
+ private readonly retry;
77
92
  constructor(options: RequiredEmitVisionOptions);
78
93
  captureEvent(name: string, properties?: Record<string, unknown>, options?: CaptureOptions): void;
79
94
  captureError(error: unknown, options?: CaptureOptions): void;
@@ -81,49 +96,45 @@ declare class EmitVisionClient {
81
96
  setContext(context: Record<string, unknown>): void;
82
97
  setDeploymentContext(context: DeploymentContext): void;
83
98
  setFeatureFlags(featureFlags: FeatureFlagsContext): void;
84
- /**
85
- * Fetch and evaluate all active flags for the given evaluation key.
86
- * Results are cached for `ttlMs` (defaults to `flagEvalTtlMs` from init options).
87
- * On network failure returns an empty object and does not throw.
88
- * Evaluated variants are automatically merged into the SDK's feature-flag context.
89
- */
90
99
  evaluateFlags(opts: FlagEvaluationOptions): Promise<FeatureFlagsContext>;
91
- /**
92
- * Return a single flag's value, evaluated for the given key.
93
- * Returns `fallback` on network failure or when the flag is not found — never throws.
94
- */
95
100
  getFlag<T extends string | number | boolean | null>(flagKey: string, fallback: T, opts: FlagEvaluationOptions): Promise<T>;
96
- /**
97
- * Bypass the cache and re-fetch evaluations for the given key.
98
- * Useful after a flag change in the dashboard that should apply immediately.
99
- */
100
101
  refreshFlags(opts: FlagEvaluationOptions): Promise<FeatureFlagsContext>;
102
+ capturePageView(path?: string): void;
101
103
  captureExposure(flagKey: string, variantKey: string, variantValue?: string | number | boolean | null, reason?: string, environment?: string): void;
102
- private fetchAndCacheFlags;
103
104
  setTags(tags: Record<string, string>): void;
104
105
  flush(): Promise<void>;
106
+ private flushChunk;
107
+ private handleFlushFailure;
108
+ private stopBackgroundWork;
105
109
  close(): void;
110
+ private enqueueMetaEvent;
106
111
  private enqueue;
107
112
  private scheduleFlush;
108
113
  private withDefaults;
109
114
  private mergeDeployment;
110
115
  private mergeFeatureFlags;
111
116
  private readonly handleWindowError;
117
+ private readonly handlePopState;
118
+ private patchHistoryMethod;
119
+ private restoreHistoryMethod;
112
120
  private readonly handleUnhandledRejection;
113
121
  private debug;
114
122
  }
115
- export declare function init(options: EmitVisionOptions): EmitVisionClient;
116
- export declare function captureEvent(name: string, properties?: Record<string, unknown>, options?: CaptureOptions): void;
117
- export declare function captureError(error: unknown, options?: CaptureOptions): void;
118
- export declare function identify(user: EmitVisionUser): void;
119
- export declare function identify(userId: string, traits?: EmitVisionTraits): void;
120
- export declare function setContext(context: Record<string, unknown>): void;
121
- export declare function setDeploymentContext(context: DeploymentContext): void;
122
- export declare function setFeatureFlags(featureFlags: FeatureFlagsContext): void;
123
- export declare function setTags(tags: Record<string, string>): void;
124
- export declare function flush(): Promise<void>;
125
- export declare function evaluateFlags(options: FlagEvaluationOptions): Promise<FeatureFlagsContext>;
126
- export declare function getFlag<T extends string | number | boolean | null>(flagKey: string, fallback: T, options: FlagEvaluationOptions): Promise<T>;
127
- export declare function refreshFlags(options: FlagEvaluationOptions): Promise<FeatureFlagsContext>;
128
- export declare function captureExposure(flagKey: string, variantKey: string, variantValue?: string | number | boolean | null, reason?: string, environment?: string): void;
129
- export {};
123
+
124
+ declare function init(options: EmitVisionOptions): EmitVisionClient;
125
+ declare function captureEvent(name: string, properties?: Record<string, unknown>, options?: CaptureOptions): void;
126
+ declare function captureError(error: unknown, options?: CaptureOptions): void;
127
+ declare function capturePageView(path?: string): void;
128
+ declare function identify(user: EmitVisionUser): void;
129
+ declare function identify(userId: string, traits?: EmitVisionTraits): void;
130
+ declare function setContext(context: Record<string, unknown>): void;
131
+ declare function setDeploymentContext(context: DeploymentContext): void;
132
+ declare function setFeatureFlags(featureFlags: FeatureFlagsContext): void;
133
+ declare function setTags(tags: Record<string, string>): void;
134
+ declare function flush(): Promise<void>;
135
+ declare function evaluateFlags(options: FlagEvaluationOptions): Promise<FeatureFlagsContext>;
136
+ declare function getFlag<T extends string | number | boolean | null>(flagKey: string, fallback: T, options: FlagEvaluationOptions): Promise<T>;
137
+ declare function refreshFlags(options: FlagEvaluationOptions): Promise<FeatureFlagsContext>;
138
+ declare function captureExposure(flagKey: string, variantKey: string, variantValue?: string | number | boolean | null, reason?: string, environment?: string): void;
139
+
140
+ export { type AutoCaptureOptions, type CaptureOptions, type DeploymentContext, type EmitVisionOptions, type EmitVisionTraits, type EmitVisionUser, type FeatureFlagsContext, type FlagEvaluationOptions, captureError, captureEvent, captureExposure, capturePageView, evaluateFlags, flush, getFlag, identify, init, refreshFlags, setContext, setDeploymentContext, setFeatureFlags, setTags };