@lessonkit/xapi 1.4.0 → 1.5.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.
package/dist/index.d.cts CHANGED
@@ -40,7 +40,7 @@ type XAPIQueue = {
40
40
  /** Statement id currently being delivered via flush, if any. */
41
41
  getHeadInFlightId?: () => string | undefined;
42
42
  };
43
- type XAPIExitTransport = (statement: XAPIStatement) => void;
43
+ type XAPIExitTransport = (statement: XAPIStatement) => void | Promise<void>;
44
44
  type XAPIClient = {
45
45
  send: (statement: XAPIStatement) => void;
46
46
  flush: () => Promise<void>;
@@ -67,6 +67,8 @@ type InMemoryXAPIQueueOptions = {
67
67
  onDepth?: (size: number) => void;
68
68
  /** Called when an oldest statement is dropped because the queue is at maxSize. */
69
69
  onCap?: () => void;
70
+ /** Called when a statement cannot be enqueued because the queue is full and the head is in-flight. */
71
+ onOverflow?: (statement: XAPIStatement) => void;
70
72
  /** Failures at queue head before skipping (default 10). */
71
73
  maxHeadFailures?: number;
72
74
  /** Called when the queue head is skipped after repeated transport failures. */
@@ -74,6 +76,10 @@ type InMemoryXAPIQueueOptions = {
74
76
  };
75
77
  declare function createInMemoryXAPIQueue(opts?: InMemoryXAPIQueueOptions): XAPIQueue;
76
78
 
79
+ /**
80
+ * Imperative xAPI client with in-memory queue, retry flush, and optional pagehide delivery.
81
+ * Prefer wiring transport via `LessonkitProvider` config from `@lessonkit/react` in React apps.
82
+ */
77
83
  declare function createXAPIClient(opts?: {
78
84
  transport?: XAPITransport;
79
85
  /** Keepalive transport for pagehide flush (e.g. from createFetchTransport). */
@@ -84,15 +90,31 @@ declare function createXAPIClient(opts?: {
84
90
  queue?: XAPIQueue;
85
91
  /** When creating the default in-memory queue (max size 1000 unless overridden). */
86
92
  maxQueueSize?: number;
93
+ /** Consecutive head failures before skip (default queue only). */
94
+ maxHeadFailures?: number;
87
95
  onQueueDepth?: (size: number) => void;
88
96
  onQueueCap?: () => void;
97
+ onHeadSkipped?: (statement: XAPIStatement, err: unknown) => void;
89
98
  /** Called when transport fails after retries (statement is re-queued). */
90
99
  onTransportError?: (err: unknown) => void;
100
+ /** Called when telemetry → xAPI mapping fails. */
101
+ onMappingError?: (err: unknown) => void;
91
102
  }): XAPIClient;
103
+ /** @internal Reset dead-letter storage between tests. */
104
+ declare function resetXAPIDeadLetterForTests(): void;
105
+
106
+ type AssertSafeLrsUrlOptions = {
107
+ /** Allow loopback, RFC1918, link-local, and metadata IPs (default false). */
108
+ allowPrivateHosts?: boolean;
109
+ };
110
+ /** Validate an LRS or analytics proxy URL before browser fetch transport use. */
111
+ declare function assertSafeLrsUrl(url: string, opts?: AssertSafeLrsUrlOptions): void;
92
112
 
93
113
  type CreateFetchTransportOptions = {
94
114
  /** LRS or proxy endpoint (POST). */
95
115
  url: string;
116
+ /** Allow loopback and private-network hosts (default false). */
117
+ allowPrivateHosts?: boolean;
96
118
  /** Per-request timeout (default 30_000 ms). Uses AbortSignal.timeout when available. */
97
119
  timeoutMs?: number;
98
120
  /** Static headers merged into each request (e.g. Authorization from a short-lived token). */
@@ -137,10 +159,13 @@ type FetchBatchSinkBundle = {
137
159
  */
138
160
  declare function createFetchBatchSink(opts: CreateFetchBatchSinkOptions): FetchBatchSinkBundle;
139
161
 
162
+ declare function loadDeadLetterStatements(): XAPIStatement[];
163
+ declare function persistDeadLetterStatement(statement: XAPIStatement): void;
164
+
140
165
  /**
141
166
  * Map a LessonKit telemetry event to an xAPI statement, or null if the event should not emit xAPI.
142
167
  * `lesson_time_on_task` returns null (companion metric; lesson_completed carries duration).
143
168
  */
144
169
  declare function telemetryEventToXAPIStatement(event: TelemetryEvent): XAPIStatement | null;
145
170
 
146
- export { type CreateFetchBatchSinkOptions, type CreateFetchTransportOptions, type FetchBatchSinkBundle, FetchHttpError, type FetchTransportBundle, type InMemoryXAPIQueueOptions, type XAPIClient, type XAPIExitTransport, type XAPIObjectDefinition, type XAPIQueue, type XAPIResult, type XAPIScore, type XAPIStatement, type XAPITransport, type XAPIVerbIri, createFetchBatchSink, createFetchTransport, createInMemoryXAPIQueue, createXAPIClient, isRetryableFetchError, isRetryableFetchHttpStatus, telemetryEventToXAPIStatement };
171
+ export { type AssertSafeLrsUrlOptions, type CreateFetchBatchSinkOptions, type CreateFetchTransportOptions, type FetchBatchSinkBundle, FetchHttpError, type FetchTransportBundle, type InMemoryXAPIQueueOptions, type XAPIClient, type XAPIExitTransport, type XAPIObjectDefinition, type XAPIQueue, type XAPIResult, type XAPIScore, type XAPIStatement, type XAPITransport, type XAPIVerbIri, assertSafeLrsUrl, createFetchBatchSink, createFetchTransport, createInMemoryXAPIQueue, createXAPIClient, isRetryableFetchError, isRetryableFetchHttpStatus, loadDeadLetterStatements, persistDeadLetterStatement, resetXAPIDeadLetterForTests, telemetryEventToXAPIStatement };
package/dist/index.d.ts CHANGED
@@ -40,7 +40,7 @@ type XAPIQueue = {
40
40
  /** Statement id currently being delivered via flush, if any. */
41
41
  getHeadInFlightId?: () => string | undefined;
42
42
  };
43
- type XAPIExitTransport = (statement: XAPIStatement) => void;
43
+ type XAPIExitTransport = (statement: XAPIStatement) => void | Promise<void>;
44
44
  type XAPIClient = {
45
45
  send: (statement: XAPIStatement) => void;
46
46
  flush: () => Promise<void>;
@@ -67,6 +67,8 @@ type InMemoryXAPIQueueOptions = {
67
67
  onDepth?: (size: number) => void;
68
68
  /** Called when an oldest statement is dropped because the queue is at maxSize. */
69
69
  onCap?: () => void;
70
+ /** Called when a statement cannot be enqueued because the queue is full and the head is in-flight. */
71
+ onOverflow?: (statement: XAPIStatement) => void;
70
72
  /** Failures at queue head before skipping (default 10). */
71
73
  maxHeadFailures?: number;
72
74
  /** Called when the queue head is skipped after repeated transport failures. */
@@ -74,6 +76,10 @@ type InMemoryXAPIQueueOptions = {
74
76
  };
75
77
  declare function createInMemoryXAPIQueue(opts?: InMemoryXAPIQueueOptions): XAPIQueue;
76
78
 
79
+ /**
80
+ * Imperative xAPI client with in-memory queue, retry flush, and optional pagehide delivery.
81
+ * Prefer wiring transport via `LessonkitProvider` config from `@lessonkit/react` in React apps.
82
+ */
77
83
  declare function createXAPIClient(opts?: {
78
84
  transport?: XAPITransport;
79
85
  /** Keepalive transport for pagehide flush (e.g. from createFetchTransport). */
@@ -84,15 +90,31 @@ declare function createXAPIClient(opts?: {
84
90
  queue?: XAPIQueue;
85
91
  /** When creating the default in-memory queue (max size 1000 unless overridden). */
86
92
  maxQueueSize?: number;
93
+ /** Consecutive head failures before skip (default queue only). */
94
+ maxHeadFailures?: number;
87
95
  onQueueDepth?: (size: number) => void;
88
96
  onQueueCap?: () => void;
97
+ onHeadSkipped?: (statement: XAPIStatement, err: unknown) => void;
89
98
  /** Called when transport fails after retries (statement is re-queued). */
90
99
  onTransportError?: (err: unknown) => void;
100
+ /** Called when telemetry → xAPI mapping fails. */
101
+ onMappingError?: (err: unknown) => void;
91
102
  }): XAPIClient;
103
+ /** @internal Reset dead-letter storage between tests. */
104
+ declare function resetXAPIDeadLetterForTests(): void;
105
+
106
+ type AssertSafeLrsUrlOptions = {
107
+ /** Allow loopback, RFC1918, link-local, and metadata IPs (default false). */
108
+ allowPrivateHosts?: boolean;
109
+ };
110
+ /** Validate an LRS or analytics proxy URL before browser fetch transport use. */
111
+ declare function assertSafeLrsUrl(url: string, opts?: AssertSafeLrsUrlOptions): void;
92
112
 
93
113
  type CreateFetchTransportOptions = {
94
114
  /** LRS or proxy endpoint (POST). */
95
115
  url: string;
116
+ /** Allow loopback and private-network hosts (default false). */
117
+ allowPrivateHosts?: boolean;
96
118
  /** Per-request timeout (default 30_000 ms). Uses AbortSignal.timeout when available. */
97
119
  timeoutMs?: number;
98
120
  /** Static headers merged into each request (e.g. Authorization from a short-lived token). */
@@ -137,10 +159,13 @@ type FetchBatchSinkBundle = {
137
159
  */
138
160
  declare function createFetchBatchSink(opts: CreateFetchBatchSinkOptions): FetchBatchSinkBundle;
139
161
 
162
+ declare function loadDeadLetterStatements(): XAPIStatement[];
163
+ declare function persistDeadLetterStatement(statement: XAPIStatement): void;
164
+
140
165
  /**
141
166
  * Map a LessonKit telemetry event to an xAPI statement, or null if the event should not emit xAPI.
142
167
  * `lesson_time_on_task` returns null (companion metric; lesson_completed carries duration).
143
168
  */
144
169
  declare function telemetryEventToXAPIStatement(event: TelemetryEvent): XAPIStatement | null;
145
170
 
146
- export { type CreateFetchBatchSinkOptions, type CreateFetchTransportOptions, type FetchBatchSinkBundle, FetchHttpError, type FetchTransportBundle, type InMemoryXAPIQueueOptions, type XAPIClient, type XAPIExitTransport, type XAPIObjectDefinition, type XAPIQueue, type XAPIResult, type XAPIScore, type XAPIStatement, type XAPITransport, type XAPIVerbIri, createFetchBatchSink, createFetchTransport, createInMemoryXAPIQueue, createXAPIClient, isRetryableFetchError, isRetryableFetchHttpStatus, telemetryEventToXAPIStatement };
171
+ export { type AssertSafeLrsUrlOptions, type CreateFetchBatchSinkOptions, type CreateFetchTransportOptions, type FetchBatchSinkBundle, FetchHttpError, type FetchTransportBundle, type InMemoryXAPIQueueOptions, type XAPIClient, type XAPIExitTransport, type XAPIObjectDefinition, type XAPIQueue, type XAPIResult, type XAPIScore, type XAPIStatement, type XAPITransport, type XAPIVerbIri, assertSafeLrsUrl, createFetchBatchSink, createFetchTransport, createInMemoryXAPIQueue, createXAPIClient, isRetryableFetchError, isRetryableFetchHttpStatus, loadDeadLetterStatements, persistDeadLetterStatement, resetXAPIDeadLetterForTests, telemetryEventToXAPIStatement };