@lessonkit/xapi 1.0.0 → 1.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/dist/index.cjs CHANGED
@@ -59,10 +59,11 @@ function createInMemoryXAPIQueue() {
59
59
  }
60
60
 
61
61
  // src/client.ts
62
- var import_core2 = require("@lessonkit/core");
62
+ var import_core3 = require("@lessonkit/core");
63
63
 
64
64
  // src/telemetryMap.ts
65
65
  var import_core = require("@lessonkit/core");
66
+ var import_core2 = require("@lessonkit/core");
66
67
 
67
68
  // src/id.ts
68
69
  function cryptoRandomId() {
@@ -90,13 +91,13 @@ function telemetryEventToXAPIStatement(event) {
90
91
  const { courseId } = event;
91
92
  switch (event.name) {
92
93
  case "course_started":
93
- return statementFor((0, import_core.buildLessonkitUrn)({ courseId }), XAPIVerbs.initialized, event.timestamp);
94
+ return statementFor((0, import_core2.buildLessonkitUrn)({ courseId }), XAPIVerbs.initialized, event.timestamp);
94
95
  case "course_completed":
95
- return statementFor((0, import_core.buildLessonkitUrn)({ courseId }), XAPIVerbs.completed, event.timestamp);
96
+ return statementFor((0, import_core2.buildLessonkitUrn)({ courseId }), XAPIVerbs.completed, event.timestamp);
96
97
  case "lesson_started": {
97
98
  const lessonId = event.lessonId;
98
99
  return statementFor(
99
- (0, import_core.buildLessonkitUrn)({ courseId, lessonId }),
100
+ (0, import_core2.buildLessonkitUrn)({ courseId, lessonId }),
100
101
  XAPIVerbs.initialized,
101
102
  event.timestamp
102
103
  );
@@ -119,7 +120,7 @@ function telemetryEventToXAPIStatement(event) {
119
120
  scaled: typeof raw === "number" && typeof max === "number" && max > 0 ? raw / max : void 0
120
121
  };
121
122
  }
122
- return statementFor((0, import_core.buildLessonkitUrn)({ courseId, lessonId }), XAPIVerbs.completed, event.timestamp, {
123
+ return statementFor((0, import_core2.buildLessonkitUrn)({ courseId, lessonId }), XAPIVerbs.completed, event.timestamp, {
123
124
  result: Object.keys(result).length ? result : void 0
124
125
  });
125
126
  }
@@ -133,7 +134,7 @@ function telemetryEventToXAPIStatement(event) {
133
134
  result.success = event.data.correct;
134
135
  }
135
136
  return statementFor(
136
- (0, import_core.buildLessonkitUrn)({ courseId, lessonId, checkId }),
137
+ (0, import_core2.buildLessonkitUrn)({ courseId, lessonId, checkId }),
137
138
  XAPIVerbs.answered,
138
139
  event.timestamp,
139
140
  { result: Object.keys(result).length ? result : void 0 }
@@ -155,7 +156,7 @@ function telemetryEventToXAPIStatement(event) {
155
156
  };
156
157
  }
157
158
  return statementFor(
158
- (0, import_core.buildLessonkitUrn)({ courseId, lessonId, checkId }),
159
+ (0, import_core2.buildLessonkitUrn)({ courseId, lessonId, checkId }),
159
160
  XAPIVerbs.completed,
160
161
  event.timestamp,
161
162
  { result: Object.keys(result).length ? result : void 0 }
@@ -166,13 +167,13 @@ function telemetryEventToXAPIStatement(event) {
166
167
  const blockId = event.data?.blockId;
167
168
  if (!lessonId || !blockId) return null;
168
169
  return statementFor(
169
- (0, import_core.buildLessonkitUrn)({ courseId, lessonId, blockId }),
170
+ (0, import_core2.buildLessonkitUrn)({ courseId, lessonId, blockId }),
170
171
  XAPIVerbs.experienced,
171
172
  event.timestamp
172
173
  );
173
174
  }
174
175
  default:
175
- return null;
176
+ return (0, import_core.assertNever)(event, "Unhandled telemetry event");
176
177
  }
177
178
  }
178
179
  function statementFor(objectId, verb, timestamp, extra) {
@@ -237,12 +238,16 @@ function createXAPIClient(opts) {
237
238
  flush: async () => {
238
239
  if (!transport) return;
239
240
  await queue.flush(transport);
241
+ const flights = [...inflightById.values()];
242
+ if (flights.length > 0) {
243
+ await Promise.allSettled(flights);
244
+ }
240
245
  },
241
246
  startedLesson: ({ lessonId }) => {
242
247
  if (!courseId) return;
243
248
  emit({
244
249
  name: "lesson_started",
245
- timestamp: (0, import_core2.nowIso)(),
250
+ timestamp: (0, import_core3.nowIso)(),
246
251
  courseId,
247
252
  lessonId,
248
253
  data: { lessonId }
@@ -258,7 +263,7 @@ function createXAPIClient(opts) {
258
263
  if (!courseId) return;
259
264
  emit({
260
265
  name: "lesson_completed",
261
- timestamp: (0, import_core2.nowIso)(),
266
+ timestamp: (0, import_core3.nowIso)(),
262
267
  courseId,
263
268
  lessonId,
264
269
  data: { lessonId, durationMs, score, maxScore, success }
@@ -268,7 +273,7 @@ function createXAPIClient(opts) {
268
273
  if (!courseId) return;
269
274
  emit({
270
275
  name: "course_completed",
271
- timestamp: (0, import_core2.nowIso)(),
276
+ timestamp: (0, import_core3.nowIso)(),
272
277
  courseId
273
278
  });
274
279
  }
package/dist/index.d.cts CHANGED
@@ -1,14 +1,32 @@
1
1
  import { LessonId, CourseId, TelemetryEvent } from '@lessonkit/core';
2
2
 
3
+ type XAPIVerbIri = "http://adlnet.gov/expapi/verbs/initialized" | "http://adlnet.gov/expapi/verbs/completed" | "http://adlnet.gov/expapi/verbs/answered" | "http://adlnet.gov/expapi/verbs/experienced";
4
+ type XAPIScore = {
5
+ raw?: number;
6
+ max?: number;
7
+ min?: number;
8
+ scaled?: number;
9
+ };
10
+ type XAPIResult = {
11
+ duration?: string;
12
+ success?: boolean;
13
+ score?: XAPIScore;
14
+ completion?: boolean;
15
+ };
16
+ type XAPIObjectDefinition = {
17
+ name?: Record<string, string>;
18
+ description?: Record<string, string>;
19
+ type?: string;
20
+ };
3
21
  type XAPIStatement = {
4
22
  id: string;
5
23
  timestamp: string;
6
- verb: string;
24
+ verb: XAPIVerbIri;
7
25
  object: {
8
26
  id: string;
9
- definition?: Record<string, unknown>;
27
+ definition?: XAPIObjectDefinition;
10
28
  };
11
- result?: Record<string, unknown>;
29
+ result?: XAPIResult;
12
30
  context?: Record<string, unknown>;
13
31
  };
14
32
  type XAPITransport = (statement: XAPIStatement) => void | Promise<void>;
@@ -48,4 +66,4 @@ declare function createXAPIClient(opts?: {
48
66
  */
49
67
  declare function telemetryEventToXAPIStatement(event: TelemetryEvent): XAPIStatement | null;
50
68
 
51
- export { type XAPIClient, type XAPIQueue, type XAPIStatement, type XAPITransport, createInMemoryXAPIQueue, createXAPIClient, telemetryEventToXAPIStatement };
69
+ export { type XAPIClient, type XAPIObjectDefinition, type XAPIQueue, type XAPIResult, type XAPIScore, type XAPIStatement, type XAPITransport, type XAPIVerbIri, createInMemoryXAPIQueue, createXAPIClient, telemetryEventToXAPIStatement };
package/dist/index.d.ts CHANGED
@@ -1,14 +1,32 @@
1
1
  import { LessonId, CourseId, TelemetryEvent } from '@lessonkit/core';
2
2
 
3
+ type XAPIVerbIri = "http://adlnet.gov/expapi/verbs/initialized" | "http://adlnet.gov/expapi/verbs/completed" | "http://adlnet.gov/expapi/verbs/answered" | "http://adlnet.gov/expapi/verbs/experienced";
4
+ type XAPIScore = {
5
+ raw?: number;
6
+ max?: number;
7
+ min?: number;
8
+ scaled?: number;
9
+ };
10
+ type XAPIResult = {
11
+ duration?: string;
12
+ success?: boolean;
13
+ score?: XAPIScore;
14
+ completion?: boolean;
15
+ };
16
+ type XAPIObjectDefinition = {
17
+ name?: Record<string, string>;
18
+ description?: Record<string, string>;
19
+ type?: string;
20
+ };
3
21
  type XAPIStatement = {
4
22
  id: string;
5
23
  timestamp: string;
6
- verb: string;
24
+ verb: XAPIVerbIri;
7
25
  object: {
8
26
  id: string;
9
- definition?: Record<string, unknown>;
27
+ definition?: XAPIObjectDefinition;
10
28
  };
11
- result?: Record<string, unknown>;
29
+ result?: XAPIResult;
12
30
  context?: Record<string, unknown>;
13
31
  };
14
32
  type XAPITransport = (statement: XAPIStatement) => void | Promise<void>;
@@ -48,4 +66,4 @@ declare function createXAPIClient(opts?: {
48
66
  */
49
67
  declare function telemetryEventToXAPIStatement(event: TelemetryEvent): XAPIStatement | null;
50
68
 
51
- export { type XAPIClient, type XAPIQueue, type XAPIStatement, type XAPITransport, createInMemoryXAPIQueue, createXAPIClient, telemetryEventToXAPIStatement };
69
+ export { type XAPIClient, type XAPIObjectDefinition, type XAPIQueue, type XAPIResult, type XAPIScore, type XAPIStatement, type XAPITransport, type XAPIVerbIri, createInMemoryXAPIQueue, createXAPIClient, telemetryEventToXAPIStatement };
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ function createInMemoryXAPIQueue() {
34
34
  import { nowIso } from "@lessonkit/core";
35
35
 
36
36
  // src/telemetryMap.ts
37
+ import { assertNever } from "@lessonkit/core";
37
38
  import { buildLessonkitUrn } from "@lessonkit/core";
38
39
 
39
40
  // src/id.ts
@@ -144,7 +145,7 @@ function telemetryEventToXAPIStatement(event) {
144
145
  );
145
146
  }
146
147
  default:
147
- return null;
148
+ return assertNever(event, "Unhandled telemetry event");
148
149
  }
149
150
  }
150
151
  function statementFor(objectId, verb, timestamp, extra) {
@@ -209,6 +210,10 @@ function createXAPIClient(opts) {
209
210
  flush: async () => {
210
211
  if (!transport) return;
211
212
  await queue.flush(transport);
213
+ const flights = [...inflightById.values()];
214
+ if (flights.length > 0) {
215
+ await Promise.allSettled(flights);
216
+ }
212
217
  },
213
218
  startedLesson: ({ lessonId }) => {
214
219
  if (!courseId) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lessonkit/xapi",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "private": false,
5
5
  "description": "xAPI statement generation primitives for LessonKit.",
6
6
  "license": "Apache-2.0",
@@ -48,7 +48,7 @@
48
48
  "lint": "echo \"(no lint configured yet)\""
49
49
  },
50
50
  "dependencies": {
51
- "@lessonkit/core": "1.0.0"
51
+ "@lessonkit/core": "1.0.1"
52
52
  },
53
53
  "devDependencies": {
54
54
  "tsup": "^8.5.0",