tessera-learn 0.2.1 → 0.2.2

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.
@@ -19,27 +19,6 @@ export class LMSAdapterError extends Error {
19
19
  }
20
20
  }
21
21
 
22
- function missingApiError(
23
- standard: 'scorm12' | 'scorm2004' | 'cmi5',
24
- ): LMSAdapterError {
25
- const label =
26
- standard === 'scorm12'
27
- ? 'SCORM 1.2'
28
- : standard === 'scorm2004'
29
- ? 'SCORM 2004'
30
- : 'cmi5';
31
- const detail =
32
- standard === 'cmi5'
33
- ? 'No cmi5 launch parameters (fetch / endpoint / activityId / actor) on the URL.'
34
- : `No ${label} API object found in the window.parent or window.opener chain.`;
35
- return new LMSAdapterError(
36
- standard,
37
- `Tessera: this course is configured for ${label} but ${detail} ` +
38
- `The course must be launched from an LMS that provides the ${label} runtime. ` +
39
- `If you are testing locally, run \`npm run dev\` instead, or set export.standard to "web".`,
40
- );
41
- }
42
-
43
22
  export interface CreateAdapterOptions {
44
23
  /**
45
24
  * When true, a missing LMS API falls back to `WebAdapter` with a console
@@ -49,44 +28,68 @@ export interface CreateAdapterOptions {
49
28
  allowFallback?: boolean;
50
29
  }
51
30
 
52
- /**
53
- * Select the appropriate persistence adapter based on course config.
54
- *
55
- * In production builds, an LMS-configured course (scorm12/scorm2004/cmi5)
56
- * will throw `LMSAdapterError` if the matching LMS API isn't reachable —
57
- * we fail loud so a misconfigured launch is visible immediately rather
58
- * than silently losing tracking to localStorage.
59
- *
60
- * In dev mode, missing APIs warn and fall back to `WebAdapter` so authors
61
- * can still iterate locally.
62
- */
63
31
  type LMSStandard = 'scorm12' | 'scorm2004' | 'cmi5';
64
32
 
65
- /** Per-standard LMS detection. `detect` returns an adapter when the LMS runtime is reachable, else null. */
33
+ /** Per-standard LMS wiring: `detect` returns an adapter when the LMS runtime is reachable, else null. Labels are the single source for the dev warning and production error. */
66
34
  const LMS_ADAPTERS: Record<
67
35
  LMSStandard,
68
- { detect: () => PersistenceAdapter | null; label: string }
36
+ {
37
+ detect: () => PersistenceAdapter | null;
38
+ warnLabel: string;
39
+ name: string;
40
+ missingDetail: string;
41
+ }
69
42
  > = {
70
43
  scorm12: {
71
44
  detect: () => {
72
45
  const api = findSCORM12API();
73
46
  return api ? new SCORM12Adapter(api) : null;
74
47
  },
75
- label: 'SCORM 1.2 API',
48
+ warnLabel: 'SCORM 1.2 API',
49
+ name: 'SCORM 1.2',
50
+ missingDetail:
51
+ 'No SCORM 1.2 API object found in the window.parent or window.opener chain.',
76
52
  },
77
53
  scorm2004: {
78
54
  detect: () => {
79
55
  const api = findSCORM2004API();
80
56
  return api ? new SCORM2004Adapter(api) : null;
81
57
  },
82
- label: 'SCORM 2004 API',
58
+ warnLabel: 'SCORM 2004 API',
59
+ name: 'SCORM 2004',
60
+ missingDetail:
61
+ 'No SCORM 2004 API object found in the window.parent or window.opener chain.',
83
62
  },
84
63
  cmi5: {
85
64
  detect: () => (hasCMI5LaunchParams() ? new CMI5Adapter() : null),
86
- label: 'cmi5 launch parameters',
65
+ warnLabel: 'cmi5 launch parameters',
66
+ name: 'cmi5',
67
+ missingDetail:
68
+ 'No cmi5 launch parameters (fetch / endpoint / activityId / actor) on the URL.',
87
69
  },
88
70
  };
89
71
 
72
+ function missingApiError(standard: LMSStandard): LMSAdapterError {
73
+ const { name, missingDetail } = LMS_ADAPTERS[standard];
74
+ return new LMSAdapterError(
75
+ standard,
76
+ `Tessera: this course is configured for ${name} but ${missingDetail} ` +
77
+ `The course must be launched from an LMS that provides the ${name} runtime. ` +
78
+ `If you are testing locally, run \`npm run dev\` instead, or set export.standard to "web".`,
79
+ );
80
+ }
81
+
82
+ /**
83
+ * Select the appropriate persistence adapter based on course config.
84
+ *
85
+ * In production builds, an LMS-configured course (scorm12/scorm2004/cmi5)
86
+ * will throw `LMSAdapterError` if the matching LMS API isn't reachable —
87
+ * we fail loud so a misconfigured launch is visible immediately rather
88
+ * than silently losing tracking to localStorage.
89
+ *
90
+ * In dev mode, missing APIs warn and fall back to `WebAdapter` so authors
91
+ * can still iterate locally.
92
+ */
90
93
  export function createAdapter(
91
94
  config: CourseConfig,
92
95
  options: CreateAdapterOptions = {},
@@ -103,7 +106,7 @@ export function createAdapter(
103
106
  if (adapter) return adapter;
104
107
  if (!allowFallback) throw missingApiError(standard);
105
108
  console.warn(
106
- `Tessera (dev): ${entry.label} not found — falling back to localStorage`,
109
+ `Tessera (dev): ${entry.warnLabel} not found — falling back to localStorage`,
107
110
  );
108
111
  }
109
112
  return new WebAdapter(config);
@@ -29,7 +29,7 @@ const SCORM12_DIALECT: ScormDialect<SCORM12API> = {
29
29
  responseField: 'student_response',
30
30
  timestampField: 'time',
31
31
  timestamp: () => new Date().toTimeString().slice(0, 8),
32
- typeValue: (t) => scorm12Type(t),
32
+ typeValue: scorm12Type,
33
33
  resultLabels: { correct: 'correct', incorrect: 'wrong' },
34
34
  format: SCORM12_INTERACTION_FORMAT,
35
35
  },