@pie-qti/transform-core 0.2.0 → 1.0.0-beta.5
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/README.md +0 -2
- package/dist/config/module-loader.d.ts +21 -0
- package/dist/config/module-loader.js +27 -0
- package/dist/config/module-loader.js.map +1 -0
- package/dist/config/plugin-loader.js +2 -16
- package/dist/config/plugin-loader.js.map +1 -1
- package/dist/config/vendor-registry.d.ts +9 -8
- package/dist/config/vendor-registry.js +28 -73
- package/dist/config/vendor-registry.js.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -3
- package/dist/index.js.map +1 -1
- package/dist/orchestration/activities/detect-format-activity.js +11 -8
- package/dist/orchestration/activities/detect-format-activity.js.map +1 -1
- package/dist/orchestration/activities/index.d.ts +1 -1
- package/dist/orchestration/activities/index.js.map +1 -1
- package/package.json +5 -6
- package/dist/utils/errors.d.ts +0 -78
- package/dist/utils/errors.js +0 -159
- package/dist/utils/errors.js.map +0 -1
- package/dist/utils/validator.d.ts +0 -59
- package/dist/utils/validator.js +0 -161
- package/dist/utils/validator.js.map +0 -1
package/README.md
CHANGED
|
@@ -39,7 +39,6 @@ const output = await handle.result();
|
|
|
39
39
|
- **Engine** — `TransformEngine` (`use`, `transform`, `transformBatch`)
|
|
40
40
|
- **Registries** — plugin registry and format-detector registry; built-in `qti-detector` and `pie-detector`
|
|
41
41
|
- **Configuration** — `loadFromFile`, `loadFromEnv`, `merge` (config loader), the plugin loader, and the vendor registry (subpath exports `./config/*`)
|
|
42
|
-
- **Validation** — `PieItemValidator`, `validatePieItemStructure` (see [VALIDATION.md](./VALIDATION.md))
|
|
43
42
|
- **Orchestration** — `InMemoryOrchestrator` plus the workflow/activity definitions transforms run on
|
|
44
43
|
|
|
45
44
|
Types live in [`@pie-qti/transform-types`](../types) and are re-exported from the root entry.
|
|
@@ -48,5 +47,4 @@ Types live in [`@pie-qti/transform-types`](../types) and are re-exported from th
|
|
|
48
47
|
|
|
49
48
|
- [`@pie-qti/to-pie`](../to-pie) — QTI → PIE plugin
|
|
50
49
|
- [`@pie-qti/pie-to-qti2`](../pie-to-qti2) — PIE → QTI 2.2 plugin
|
|
51
|
-
- [`@pie-qti/storage`](../storage) — filesystem/S3/database storage backends
|
|
52
50
|
- [`@pie-qti/transform-cli`](../../tools/cli) — CLI driving this engine
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configured Module Loader
|
|
3
|
+
* Shared machinery behind loadPlugin() and the vendor extension loader.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The shape every config-driven module entry shares (PluginConfig and
|
|
7
|
+
* VendorExtensionModuleConfig are structurally this).
|
|
8
|
+
*/
|
|
9
|
+
export interface ConfiguredModuleConfig {
|
|
10
|
+
/** NPM package or file path to the module */
|
|
11
|
+
module: string;
|
|
12
|
+
/** Named export from the module (if not default export) */
|
|
13
|
+
export?: string;
|
|
14
|
+
/** Constructor options to pass to the instantiated class */
|
|
15
|
+
options?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Dynamically import a configured module, pick its export, and instantiate it.
|
|
19
|
+
* `kind` names the consumer in the error message ("plugin", "vendor extension").
|
|
20
|
+
*/
|
|
21
|
+
export declare function loadConfiguredModule<T = unknown>(config: ConfiguredModuleConfig, kind: string): Promise<T>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configured Module Loader
|
|
3
|
+
* Shared machinery behind loadPlugin() and the vendor extension loader.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Dynamically import a configured module, pick its export, and instantiate it.
|
|
7
|
+
* `kind` names the consumer in the error message ("plugin", "vendor extension").
|
|
8
|
+
*/
|
|
9
|
+
export async function loadConfiguredModule(config, kind) {
|
|
10
|
+
try {
|
|
11
|
+
// Dynamic import of the module (Vite cannot statically analyze this)
|
|
12
|
+
const module = await import(/* @vite-ignore */ config.module);
|
|
13
|
+
// Get the export (default or named)
|
|
14
|
+
const exportName = config.export || 'default';
|
|
15
|
+
const ModuleClass = module[exportName];
|
|
16
|
+
if (!ModuleClass) {
|
|
17
|
+
throw new Error(`Export "${exportName}" not found in module ${config.module}`);
|
|
18
|
+
}
|
|
19
|
+
// Instantiate with options
|
|
20
|
+
const instance = new ModuleClass(config.options || {});
|
|
21
|
+
return instance;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
throw new Error(`Failed to load ${kind} from ${config.module}: ${error.message}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=module-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-loader.js","sourceRoot":"","sources":["../../src/config/module-loader.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAiBH;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACzC,MAA8B,EAC9B,IAAY;IAEZ,IAAI,CAAC;QACJ,qEAAqE;QACrE,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAE9D,oCAAoC;QACpC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,IAAI,SAAS,CAAC;QAC9C,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;QAEvC,IAAI,CAAC,WAAW,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CACd,WAAW,UAAU,yBAAyB,MAAM,CAAC,MAAM,EAAE,CAC7D,CAAC;QACH,CAAC;QAED,2BAA2B;QAC3B,MAAM,QAAQ,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;QACvD,OAAO,QAAa,CAAC;IACtB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CACd,kBAAkB,IAAI,SAAS,MAAM,CAAC,MAAM,KAAM,KAAe,CAAC,OAAO,EAAE,CAC3E,CAAC;IACH,CAAC;AACF,CAAC","sourcesContent":["/**\n * Configured Module Loader\n * Shared machinery behind loadPlugin() and the vendor extension loader.\n */\n\n/**\n * The shape every config-driven module entry shares (PluginConfig and\n * VendorExtensionModuleConfig are structurally this).\n */\nexport interface ConfiguredModuleConfig {\n\t/** NPM package or file path to the module */\n\tmodule: string;\n\n\t/** Named export from the module (if not default export) */\n\texport?: string;\n\n\t/** Constructor options to pass to the instantiated class */\n\toptions?: Record<string, unknown>;\n}\n\n/**\n * Dynamically import a configured module, pick its export, and instantiate it.\n * `kind` names the consumer in the error message (\"plugin\", \"vendor extension\").\n */\nexport async function loadConfiguredModule<T = unknown>(\n\tconfig: ConfiguredModuleConfig,\n\tkind: string,\n): Promise<T> {\n\ttry {\n\t\t// Dynamic import of the module (Vite cannot statically analyze this)\n\t\tconst module = await import(/* @vite-ignore */ config.module);\n\n\t\t// Get the export (default or named)\n\t\tconst exportName = config.export || 'default';\n\t\tconst ModuleClass = module[exportName];\n\n\t\tif (!ModuleClass) {\n\t\t\tthrow new Error(\n\t\t\t\t`Export \"${exportName}\" not found in module ${config.module}`,\n\t\t\t);\n\t\t}\n\n\t\t// Instantiate with options\n\t\tconst instance = new ModuleClass(config.options || {});\n\t\treturn instance as T;\n\t} catch (error) {\n\t\tthrow new Error(\n\t\t\t`Failed to load ${kind} from ${config.module}: ${(error as Error).message}`,\n\t\t);\n\t}\n}\n"]}
|
|
@@ -2,26 +2,12 @@
|
|
|
2
2
|
* Plugin Loader
|
|
3
3
|
* Dynamically loads and registers plugins and format detectors
|
|
4
4
|
*/
|
|
5
|
+
import { loadConfiguredModule } from './module-loader.js';
|
|
5
6
|
/**
|
|
6
7
|
* Load a plugin or format detector from a module config
|
|
7
8
|
*/
|
|
8
9
|
export async function loadPlugin(config) {
|
|
9
|
-
|
|
10
|
-
// Dynamic import of the module (Vite cannot statically analyze this)
|
|
11
|
-
const module = await import(/* @vite-ignore */ config.module);
|
|
12
|
-
// Get the export (default or named)
|
|
13
|
-
const exportName = config.export || 'default';
|
|
14
|
-
const PluginClass = module[exportName];
|
|
15
|
-
if (!PluginClass) {
|
|
16
|
-
throw new Error(`Export "${exportName}" not found in module ${config.module}`);
|
|
17
|
-
}
|
|
18
|
-
// Instantiate with options
|
|
19
|
-
const instance = new PluginClass(config.options || {});
|
|
20
|
-
return instance;
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
throw new Error(`Failed to load plugin from ${config.module}: ${error.message}`);
|
|
24
|
-
}
|
|
10
|
+
return loadConfiguredModule(config, 'plugin');
|
|
25
11
|
}
|
|
26
12
|
/**
|
|
27
13
|
* Load and register all plugins from configuration
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-loader.js","sourceRoot":"","sources":["../../src/config/plugin-loader.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"plugin-loader.js","sourceRoot":"","sources":["../../src/config/plugin-loader.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC/B,MAAoB;IAEpB,OAAO,oBAAoB,CAAI,MAAM,EAAE,QAAQ,CAAC,CAAC;AAClD,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC3C,MAAuB,EACvB,YAAyC;IAEzC,IAAI,CAAC,YAAY,EAAE,CAAC;QACnB,OAAO;IACR,CAAC;IAED,KAAK,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC;QACpE,KAAK,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YAC9D,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,MAAM,UAAU,CAAkB,MAAM,CAAC,CAAC;gBACzD,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACpB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,OAAO,CAAC,IAAI,CACX,yBAAyB,YAAY,IAAI,YAAY,GAAG,EACxD,KAAK,CACL,CAAC;YACH,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACxC,MAAuB,EACvB,eAAgC;IAEhC,IAAI,CAAC,eAAe,EAAE,CAAC;QACtB,OAAO;IACR,CAAC;IAED,KAAK,MAAM,MAAM,IAAI,eAAe,EAAE,CAAC;QACtC,IAAI,CAAC;YACJ,MAAM,QAAQ,GAAG,MAAM,UAAU,CAAiB,MAAM,CAAC,CAAC;YAC1D,MAAM,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC;QACzC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO,CAAC,IAAI,CAAC,kCAAkC,MAAM,CAAC,MAAM,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["/**\n * Plugin Loader\n * Dynamically loads and registers plugins and format detectors\n */\n\nimport type {\n\tTransformPlugin,\n\tTransformConfig,\n\tPluginConfig,\n} from '@pie-qti/transform-types';\nimport type { TransformEngine } from '../engine/transform-engine.js';\nimport type { FormatDetector } from '../registry/format-detector-registry.js';\nimport { loadConfiguredModule } from './module-loader.js';\n\n/**\n * Load a plugin or format detector from a module config\n */\nexport async function loadPlugin<T = unknown>(\n\tconfig: PluginConfig,\n): Promise<T> {\n\treturn loadConfiguredModule<T>(config, 'plugin');\n}\n\n/**\n * Load and register all plugins from configuration\n */\nexport async function loadAndRegisterPlugins(\n\tengine: TransformEngine,\n\tpluginConfig?: TransformConfig['plugins'],\n): Promise<void> {\n\tif (!pluginConfig) {\n\t\treturn;\n\t}\n\n\tfor (const [sourceFormat, targets] of Object.entries(pluginConfig)) {\n\t\tfor (const [targetFormat, config] of Object.entries(targets)) {\n\t\t\ttry {\n\t\t\t\tconst plugin = await loadPlugin<TransformPlugin>(config);\n\t\t\t\tengine.use(plugin);\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`Failed to load plugin ${sourceFormat}→${targetFormat}:`,\n\t\t\t\t\terror,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Load and register format detectors from configuration\n */\nexport async function loadFormatDetectors(\n\tengine: TransformEngine,\n\tdetectorConfigs?: PluginConfig[],\n): Promise<void> {\n\tif (!detectorConfigs) {\n\t\treturn;\n\t}\n\n\tfor (const config of detectorConfigs) {\n\t\ttry {\n\t\t\tconst detector = await loadPlugin<FormatDetector>(config);\n\t\t\tengine.registerFormatDetector(detector);\n\t\t} catch (error) {\n\t\t\tconsole.warn(`Failed to load format detector ${config.module}:`, error);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -2,17 +2,18 @@
|
|
|
2
2
|
* Vendor Extension Registry
|
|
3
3
|
* Dynamically loads and registers vendor extensions from configuration
|
|
4
4
|
*/
|
|
5
|
-
import type { VendorExtensionConfig } from '@pie-qti/transform-types';
|
|
5
|
+
import type { AssetResolver, CssClassExtractor, MetadataExtractor, VendorDetector, VendorExtensionConfig, VendorTransformer } from '@pie-qti/transform-types';
|
|
6
6
|
/**
|
|
7
|
-
* Plugin interface with vendor extension registration methods
|
|
8
|
-
*
|
|
7
|
+
* Plugin interface with vendor extension registration methods. The hook
|
|
8
|
+
* contracts live in @pie-qti/transform-types so that config-driven vendor
|
|
9
|
+
* modules are type-checked here, at the framework boundary.
|
|
9
10
|
*/
|
|
10
11
|
export interface VendorExtensionPlugin {
|
|
11
|
-
registerVendorDetector?: (detector:
|
|
12
|
-
registerVendorTransformer?: (transformer:
|
|
13
|
-
registerAssetResolver?: (resolver:
|
|
14
|
-
registerCssClassExtractor?: (extractor:
|
|
15
|
-
registerMetadataExtractor?: (extractor:
|
|
12
|
+
registerVendorDetector?: (detector: VendorDetector) => void;
|
|
13
|
+
registerVendorTransformer?: (transformer: VendorTransformer) => void;
|
|
14
|
+
registerAssetResolver?: (resolver: AssetResolver) => void;
|
|
15
|
+
registerCssClassExtractor?: (extractor: CssClassExtractor) => void;
|
|
16
|
+
registerMetadataExtractor?: (extractor: MetadataExtractor) => void;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Register vendor extensions on a plugin from configuration
|
|
@@ -2,90 +2,45 @@
|
|
|
2
2
|
* Vendor Extension Registry
|
|
3
3
|
* Dynamically loads and registers vendor extensions from configuration
|
|
4
4
|
*/
|
|
5
|
+
import { loadConfiguredModule } from './module-loader.js';
|
|
5
6
|
/**
|
|
6
7
|
* Load a vendor extension module
|
|
7
8
|
*/
|
|
8
9
|
async function loadVendorModule(config) {
|
|
9
|
-
|
|
10
|
-
// Dynamic import of the module (Vite cannot statically analyze this)
|
|
11
|
-
const module = await import(/* @vite-ignore */ config.module);
|
|
12
|
-
// Get the export (default or named)
|
|
13
|
-
const exportName = config.export || 'default';
|
|
14
|
-
const ExtensionClass = module[exportName];
|
|
15
|
-
if (!ExtensionClass) {
|
|
16
|
-
throw new Error(`Export "${exportName}" not found in module ${config.module}`);
|
|
17
|
-
}
|
|
18
|
-
// Instantiate with options
|
|
19
|
-
const instance = new ExtensionClass(config.options || {});
|
|
20
|
-
return instance;
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
throw new Error(`Failed to load vendor extension from ${config.module}: ${error.message}`);
|
|
24
|
-
}
|
|
10
|
+
return loadConfiguredModule(config, 'vendor extension');
|
|
25
11
|
}
|
|
26
12
|
/**
|
|
27
|
-
*
|
|
13
|
+
* Load and register one kind of vendor extension. A kind without configured
|
|
14
|
+
* modules, or whose hook the plugin does not implement, is skipped.
|
|
28
15
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
for (const detectorConfig of config.detectors) {
|
|
33
|
-
try {
|
|
34
|
-
const detector = await loadVendorModule(detectorConfig);
|
|
35
|
-
plugin.registerVendorDetector(detector);
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
console.warn(`Failed to load vendor detector ${detectorConfig.module}:`, error);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
16
|
+
async function registerExtensionKind(moduleConfigs, register, label) {
|
|
17
|
+
if (!moduleConfigs || !register) {
|
|
18
|
+
return;
|
|
41
19
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const transformer = await loadVendorModule(transformerConfig);
|
|
47
|
-
plugin.registerVendorTransformer(transformer);
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
console.warn(`Failed to load vendor transformer ${transformerConfig.module}:`, error);
|
|
51
|
-
}
|
|
20
|
+
for (const moduleConfig of moduleConfigs) {
|
|
21
|
+
try {
|
|
22
|
+
register(await loadVendorModule(moduleConfig));
|
|
52
23
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (config.assetResolvers && plugin.registerAssetResolver) {
|
|
56
|
-
for (const resolverConfig of config.assetResolvers) {
|
|
57
|
-
try {
|
|
58
|
-
const resolver = await loadVendorModule(resolverConfig);
|
|
59
|
-
plugin.registerAssetResolver(resolver);
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
console.warn(`Failed to load asset resolver ${resolverConfig.module}:`, error);
|
|
63
|
-
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.warn(`Failed to load ${label} ${moduleConfig.module}:`, error);
|
|
64
26
|
}
|
|
65
27
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const extractor = await loadVendorModule(extractorConfig);
|
|
83
|
-
plugin.registerMetadataExtractor(extractor);
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
console.warn(`Failed to load metadata extractor ${extractorConfig.module}:`, error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Register vendor extensions on a plugin from configuration
|
|
31
|
+
*/
|
|
32
|
+
export async function registerExtensions(plugin, config) {
|
|
33
|
+
// One thunk per extension kind; each binds its concrete register hook so the
|
|
34
|
+
// loaded module is type-checked against that hook's parameter type.
|
|
35
|
+
const kinds = [
|
|
36
|
+
() => registerExtensionKind(config.detectors, plugin.registerVendorDetector?.bind(plugin), 'vendor detector'),
|
|
37
|
+
() => registerExtensionKind(config.transformers, plugin.registerVendorTransformer?.bind(plugin), 'vendor transformer'),
|
|
38
|
+
() => registerExtensionKind(config.assetResolvers, plugin.registerAssetResolver?.bind(plugin), 'asset resolver'),
|
|
39
|
+
() => registerExtensionKind(config.cssClassExtractors, plugin.registerCssClassExtractor?.bind(plugin), 'CSS class extractor'),
|
|
40
|
+
() => registerExtensionKind(config.metadataExtractors, plugin.registerMetadataExtractor?.bind(plugin), 'metadata extractor'),
|
|
41
|
+
];
|
|
42
|
+
for (const registerKind of kinds) {
|
|
43
|
+
await registerKind();
|
|
89
44
|
}
|
|
90
45
|
}
|
|
91
46
|
//# sourceMappingURL=vendor-registry.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vendor-registry.js","sourceRoot":"","sources":["../../src/config/vendor-registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"vendor-registry.js","sourceRoot":"","sources":["../../src/config/vendor-registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAe1D;;GAEG;AACH,KAAK,UAAU,gBAAgB,CAC9B,M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,qBAAqB,CACpB,MAAM,CAAC,kBAAkB,EACzB,MAAM,CAAC,yBAAyB,EAAE,IAAI,CAAC,MAAM,CAAC,EAC9C,oBAAoB,CACpB;KACF,CAAC;IAEF,KAAK,MAAM,YAAY,IAAI,KAAK,EAAE,CAAC;QAClC,MAAM,YAAY,EAAE,CAAC;IACtB,CAAC;AACF,CAAC","sourcesContent":["/**\n * Vendor Extension Registry\n * Dynamically loads and registers vendor extensions from configuration\n */\n\nimport type {\n\tAssetResolver,\n\tCssClassExtractor,\n\tMetadataExtractor,\n\tVendorDetector,\n\tVendorExtensionConfig,\n\tVendorExtensionModuleConfig,\n\tVendorTransformer,\n} from '@pie-qti/transform-types';\nimport { loadConfiguredModule } from './module-loader.js';\n\n/**\n * Plugin interface with vendor extension registration methods. The hook\n * contracts live in @pie-qti/transform-types so that config-driven vendor\n * modules are type-checked here, at the framework boundary.\n */\nexport interface VendorExtensionPlugin {\n\tregisterVendorDetector?: (detector: VendorDetector) => void;\n\tregisterVendorTransformer?: (transformer: VendorTransformer) => void;\n\tregisterAssetResolver?: (resolver: AssetResolver) => void;\n\tregisterCssClassExtractor?: (extractor: CssClassExtractor) => void;\n\tregisterMetadataExtractor?: (extractor: MetadataExtractor) => void;\n}\n\n/**\n * Load a vendor extension module\n */\nasync function loadVendorModule<T = unknown>(\n\tconfig: VendorExtensionModuleConfig,\n): Promise<T> {\n\treturn loadConfiguredModule<T>(config, 'vendor extension');\n}\n\n/**\n * Load and register one kind of vendor extension. A kind without configured\n * modules, or whose hook the plugin does not implement, is skipped.\n */\nasync function registerExtensionKind<T>(\n\tmoduleConfigs: VendorExtensionModuleConfig[] | undefined,\n\tregister: ((instance: T) => void) | undefined,\n\tlabel: string,\n): Promise<void> {\n\tif (!moduleConfigs || !register) {\n\t\treturn;\n\t}\n\n\tfor (const moduleConfig of moduleConfigs) {\n\t\ttry {\n\t\t\tregister(await loadVendorModule<T>(moduleConfig));\n\t\t} catch (error) {\n\t\t\tconsole.warn(`Failed to load ${label} ${moduleConfig.module}:`, error);\n\t\t}\n\t}\n}\n\n/**\n * Register vendor extensions on a plugin from configuration\n */\nexport async function registerExtensions(\n\tplugin: VendorExtensionPlugin,\n\tconfig: VendorExtensionConfig,\n): Promise<void> {\n\t// One thunk per extension kind; each binds its concrete register hook so the\n\t// loaded module is type-checked against that hook's parameter type.\n\tconst kinds: Array<() => Promise<void>> = [\n\t\t() =>\n\t\t\tregisterExtensionKind(\n\t\t\t\tconfig.detectors,\n\t\t\t\tplugin.registerVendorDetector?.bind(plugin),\n\t\t\t\t'vendor detector',\n\t\t\t),\n\t\t() =>\n\t\t\tregisterExtensionKind(\n\t\t\t\tconfig.transformers,\n\t\t\t\tplugin.registerVendorTransformer?.bind(plugin),\n\t\t\t\t'vendor transformer',\n\t\t\t),\n\t\t() =>\n\t\t\tregisterExtensionKind(\n\t\t\t\tconfig.assetResolvers,\n\t\t\t\tplugin.registerAssetResolver?.bind(plugin),\n\t\t\t\t'asset resolver',\n\t\t\t),\n\t\t() =>\n\t\t\tregisterExtensionKind(\n\t\t\t\tconfig.cssClassExtractors,\n\t\t\t\tplugin.registerCssClassExtractor?.bind(plugin),\n\t\t\t\t'CSS class extractor',\n\t\t\t),\n\t\t() =>\n\t\t\tregisterExtensionKind(\n\t\t\t\tconfig.metadataExtractors,\n\t\t\t\tplugin.registerMetadataExtractor?.bind(plugin),\n\t\t\t\t'metadata extractor',\n\t\t\t),\n\t];\n\n\tfor (const registerKind of kinds) {\n\t\tawait registerKind();\n\t}\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,4 @@ export * from './detectors/pie-detector.js';
|
|
|
12
12
|
export * from './config/config-loader.js';
|
|
13
13
|
export * from './config/plugin-loader.js';
|
|
14
14
|
export * from './config/vendor-registry.js';
|
|
15
|
-
export * from './utils/validator.js';
|
|
16
|
-
export * from './utils/errors.js';
|
|
17
15
|
export * from './orchestration/index.js';
|
package/dist/index.js
CHANGED
|
@@ -15,9 +15,6 @@ export * from './detectors/pie-detector.js';
|
|
|
15
15
|
export * from './config/config-loader.js';
|
|
16
16
|
export * from './config/plugin-loader.js';
|
|
17
17
|
export * from './config/vendor-registry.js';
|
|
18
|
-
// Utilities
|
|
19
|
-
export * from './utils/validator.js';
|
|
20
|
-
export * from './utils/errors.js';
|
|
21
18
|
// Orchestration
|
|
22
19
|
export * from './orchestration/index.js';
|
|
23
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,SAAS;AACT,cAAc,8BAA8B,CAAC;AAE7C,WAAW;AACX,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wCAAwC,CAAC;AAEvD,YAAY;AACZ,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAE5C,gBAAgB;AAChB,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAE5C,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,SAAS;AACT,cAAc,8BAA8B,CAAC;AAE7C,WAAW;AACX,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wCAAwC,CAAC;AAEvD,YAAY;AACZ,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAE5C,gBAAgB;AAChB,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAE5C,gBAAgB;AAChB,cAAc,0BAA0B,CAAC","sourcesContent":["/**\n * @pie-qti/transform-core\n *\n * Core transformation engine and plugin system\n */\n\n// Re-export types from transform-types\nexport type * from '@pie-qti/transform-types';\n\n// Engine\nexport * from './engine/transform-engine.js';\n\n// Registry\nexport * from './registry/plugin-registry.js';\nexport * from './registry/format-detector-registry.js';\n\n// Detectors\nexport * from './detectors/qti-detector.js';\nexport * from './detectors/pie-detector.js';\n\n// Configuration\nexport * from './config/config-loader.js';\nexport * from './config/plugin-loader.js';\nexport * from './config/vendor-registry.js';\n\n// Orchestration\nexport * from './orchestration/index.js';\n"]}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Detects the format of content (QTI 2.2, PIE, etc.)
|
|
5
5
|
*/
|
|
6
|
+
import { detectQtiVersion } from '@pie-qti/qti-common/version-detection';
|
|
6
7
|
/**
|
|
7
8
|
* Activity that detects content format
|
|
8
9
|
*/
|
|
@@ -12,7 +13,7 @@ export const DetectFormatActivity = {
|
|
|
12
13
|
async execute(context, input) {
|
|
13
14
|
context.log('debug', 'Detecting content format');
|
|
14
15
|
const content = typeof input.content === 'string' ? input.content : JSON.stringify(input.content);
|
|
15
|
-
// Check for PIE format (JSON-based)
|
|
16
|
+
// Check for PIE format (JSON-based) first: the narrowest signal.
|
|
16
17
|
if (typeof input.content === 'object' || content.trim().startsWith('{')) {
|
|
17
18
|
try {
|
|
18
19
|
const obj = typeof input.content === 'object' ? input.content : JSON.parse(content);
|
|
@@ -28,16 +29,18 @@ export const DetectFormatActivity = {
|
|
|
28
29
|
// Not valid JSON, continue
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// Check for QTI 3.0 format
|
|
37
|
-
if (content.includes('xmlns="http://www.imsglobal.org/xsd/imsqtiasi_v3p0"')) {
|
|
32
|
+
// QTI version detection is delegated to the canonical detector in qti-common, which checks
|
|
33
|
+
// the namespace before element names. A QTI 3.0 item also contains camelCase
|
|
34
|
+
// `<assessmentItem` markup vocabulary, so an element-name sniff alone misreads it as QTI 2.2.
|
|
35
|
+
const version = detectQtiVersion(content);
|
|
36
|
+
if (version === '3.0') {
|
|
38
37
|
context.log('info', 'Detected QTI 3.0 format');
|
|
39
38
|
return { format: 'qti30', confidence: 0.9 };
|
|
40
39
|
}
|
|
40
|
+
if (version === '2.0' || version === '2.1' || version === '2.2') {
|
|
41
|
+
context.log('info', 'Detected QTI 2.x format');
|
|
42
|
+
return { format: 'qti22', confidence: 0.9 };
|
|
43
|
+
}
|
|
41
44
|
throw new Error('Unable to detect content format');
|
|
42
45
|
},
|
|
43
46
|
timeout: 10000, // 10 seconds
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detect-format-activity.js","sourceRoot":"","sources":["../../../src/orchestration/activities/detect-format-activity.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"detect-format-activity.js","sourceRoot":"","sources":["../../../src/orchestration/activities/detect-format-activity.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAC;AAYzE;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAoD;IACnF,IAAI,EAAE,eAAe;IACrB,IAAI,EAAE,eAAe;IAErB,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK;QAC1B,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,0BAA0B,CAAC,CAAC;QAEjD,MAAM,OAAO,GAAG,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAElG,iEAAiE;QACjE,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACxE,IAAI,CAAC;gBACH,MAAM,GAAG,GAAG,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBACpF,IACE,GAAG;oBACH,OAAO,GAAG,KAAK,QAAQ;oBACvB,IAAI,IAAI,GAAG;oBACX,SAAS,IAAI,GAAG,EAChB,CAAC;oBACD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC;oBAC3C,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;gBAC5C,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,2BAA2B;YAC7B,CAAC;QACH,CAAC;QAED,2FAA2F;QAC3F,6EAA6E;QAC7E,8FAA8F;QAC9F,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,OAAO,KAAK,KAAK,EAAE,CAAC;YACtB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;YAC/C,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;QAC9C,CAAC;QACD,IAAI,OAAO,KAAK,KAAK,IAAI,OAAO,KAAK,KAAK,IAAI,OAAO,KAAK,KAAK,EAAE,CAAC;YAChE,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;YAC/C,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;QAC9C,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,aAAa;IAC7B,WAAW,EAAE;QACX,WAAW,EAAE,CAAC,EAAE,wCAAwC;QACxD,eAAe,EAAE,IAAI;QACrB,WAAW,EAAE,IAAI;QACjB,kBAAkB,EAAE,CAAC;KACtB;CACF,CAAC","sourcesContent":["/**\n * Detect Format Activity\n *\n * Detects the format of content (QTI 2.2, PIE, etc.)\n */\n\nimport { detectQtiVersion } from '@pie-qti/qti-common/version-detection';\nimport type { Activity, TransformFormat } from '@pie-qti/transform-types';\n\nexport interface DetectFormatInput {\n content: string | object;\n}\n\nexport interface DetectFormatOutput {\n format: TransformFormat;\n confidence: number;\n}\n\n/**\n * Activity that detects content format\n */\nexport const DetectFormatActivity: Activity<DetectFormatInput, DetectFormatOutput> = {\n type: 'detect-format',\n name: 'Detect Format',\n\n async execute(context, input) {\n context.log('debug', 'Detecting content format');\n\n const content = typeof input.content === 'string' ? input.content : JSON.stringify(input.content);\n\n // Check for PIE format (JSON-based) first: the narrowest signal.\n if (typeof input.content === 'object' || content.trim().startsWith('{')) {\n try {\n const obj = typeof input.content === 'object' ? input.content : JSON.parse(content);\n if (\n obj &&\n typeof obj === 'object' &&\n 'id' in obj &&\n 'element' in obj\n ) {\n context.log('info', 'Detected PIE format');\n return { format: 'pie', confidence: 0.9 };\n }\n } catch {\n // Not valid JSON, continue\n }\n }\n\n // QTI version detection is delegated to the canonical detector in qti-common, which checks\n // the namespace before element names. A QTI 3.0 item also contains camelCase\n // `<assessmentItem` markup vocabulary, so an element-name sniff alone misreads it as QTI 2.2.\n const version = detectQtiVersion(content);\n if (version === '3.0') {\n context.log('info', 'Detected QTI 3.0 format');\n return { format: 'qti30', confidence: 0.9 };\n }\n if (version === '2.0' || version === '2.1' || version === '2.2') {\n context.log('info', 'Detected QTI 2.x format');\n return { format: 'qti22', confidence: 0.9 };\n }\n\n throw new Error('Unable to detect content format');\n },\n\n timeout: 10000, // 10 seconds\n retryPolicy: {\n maxAttempts: 1, // Format detection shouldn't be retried\n initialInterval: 1000,\n maxInterval: 1000,\n backoffCoefficient: 1,\n },\n};\n"]}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
export { DetectFormatActivity } from './detect-format-activity.js';
|
|
5
5
|
export type { DetectFormatInput, DetectFormatOutput } from './detect-format-activity.js';
|
|
6
6
|
export { DetectVendorActivity } from './detect-vendor-activity.js';
|
|
7
|
-
export type { DetectVendorInput, DetectVendorOutput
|
|
7
|
+
export type { DetectVendorInput, DetectVendorOutput } from './detect-vendor-activity.js';
|
|
8
8
|
export { ParseXmlActivity } from './parse-xml-activity.js';
|
|
9
9
|
export type { ParseXmlInput, ParseXmlOutput } from './parse-xml-activity.js';
|
|
10
10
|
export { ReadContentActivity } from './read-content-activity.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/orchestration/activities/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAGnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/orchestration/activities/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAGnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAKnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAG3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGjE,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAG/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC","sourcesContent":["/**\n * Activity definitions for transformation pipeline\n */\n\nexport { DetectFormatActivity } from './detect-format-activity.js';\nexport type { DetectFormatInput, DetectFormatOutput } from './detect-format-activity.js';\n\nexport { DetectVendorActivity } from './detect-vendor-activity.js';\n// VendorInfo is intentionally not re-exported here: the canonical\n// vendor-detection contract of that name lives in @pie-qti/transform-types.\nexport type { DetectVendorInput, DetectVendorOutput } from './detect-vendor-activity.js';\n\nexport { ParseXmlActivity } from './parse-xml-activity.js';\nexport type { ParseXmlInput, ParseXmlOutput } from './parse-xml-activity.js';\n\nexport { ReadContentActivity } from './read-content-activity.js';\nexport type { ReadContentInput, ReadContentOutput } from './read-content-activity.js';\n\nexport { TransformQtiToPieActivity } from './transform-qti-to-pie-activity.js';\nexport type { TransformQtiToPieInput, TransformQtiToPieOutput } from './transform-qti-to-pie-activity.js';\n\nexport { ValidateQtiActivity } from './validate-qti-activity.js';\nexport type { ValidateQtiInput, ValidateQtiOutput } from './validate-qti-activity.js';\n\nexport { WriteContentActivity } from './write-content-activity.js';\nexport type { WriteContentInput, WriteContentOutput } from './write-content-activity.js';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-qti/transform-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.5",
|
|
4
4
|
"description": "Core transformation engine and plugin system for PIE Transform",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -39,14 +39,13 @@
|
|
|
39
39
|
],
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@pie-qti/
|
|
43
|
-
"@pie-qti/
|
|
44
|
-
"@pie-qti/
|
|
45
|
-
"@pie-qti/transform-types": "0.2.0",
|
|
46
|
-
"ajv": "^8.20.0",
|
|
42
|
+
"@pie-qti/logger": "1.0.0-beta.5",
|
|
43
|
+
"@pie-qti/qti-common": "1.0.0-beta.5",
|
|
44
|
+
"@pie-qti/transform-types": "1.0.0-beta.5",
|
|
47
45
|
"node-html-parser": "^9.0.5"
|
|
48
46
|
},
|
|
49
47
|
"devDependencies": {
|
|
48
|
+
"@pie-qti/test-utils": "1.0.0-beta.5",
|
|
50
49
|
"typescript": "^5.9.3"
|
|
51
50
|
},
|
|
52
51
|
"repository": {
|
package/dist/utils/errors.d.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error Utilities
|
|
3
|
-
* Helpers for creating and handling categorized transform errors
|
|
4
|
-
*/
|
|
5
|
-
import type { TransformError } from '@pie-qti/transform-types';
|
|
6
|
-
import { ErrorCategory } from '@pie-qti/transform-types';
|
|
7
|
-
/**
|
|
8
|
-
* Options for creating a transform error
|
|
9
|
-
*/
|
|
10
|
-
export interface CreateErrorOptions {
|
|
11
|
-
message: string;
|
|
12
|
-
code?: string;
|
|
13
|
-
itemId?: string;
|
|
14
|
-
category: ErrorCategory;
|
|
15
|
-
recoverable?: boolean;
|
|
16
|
-
cause?: Error;
|
|
17
|
-
context?: Record<string, unknown>;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Create a categorized transform error
|
|
21
|
-
*/
|
|
22
|
-
export declare function createError(options: CreateErrorOptions): TransformError;
|
|
23
|
-
/**
|
|
24
|
-
* Create a validation error (user input issue)
|
|
25
|
-
*/
|
|
26
|
-
export declare function createValidationError(message: string, options?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>): TransformError;
|
|
27
|
-
/**
|
|
28
|
-
* Create a configuration error (setup issue)
|
|
29
|
-
*/
|
|
30
|
-
export declare function createConfigurationError(message: string, options?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>): TransformError;
|
|
31
|
-
/**
|
|
32
|
-
* Create an internal error (bug or unexpected state)
|
|
33
|
-
*/
|
|
34
|
-
export declare function createInternalError(message: string, options?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>): TransformError;
|
|
35
|
-
/**
|
|
36
|
-
* Create an external error (service/network failure)
|
|
37
|
-
*/
|
|
38
|
-
export declare function createExternalError(message: string, options?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>): TransformError;
|
|
39
|
-
/**
|
|
40
|
-
* Check if error should be retried
|
|
41
|
-
*/
|
|
42
|
-
export declare function shouldRetry(error: TransformError): boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Check if error should be shown to user
|
|
45
|
-
*/
|
|
46
|
-
export declare function shouldShowToUser(error: TransformError): boolean;
|
|
47
|
-
/**
|
|
48
|
-
* Check if error requires ops team notification
|
|
49
|
-
*/
|
|
50
|
-
export declare function shouldNotifyOps(error: TransformError): boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Get suggested action for error
|
|
53
|
-
*/
|
|
54
|
-
export declare function getSuggestedAction(error: TransformError): string;
|
|
55
|
-
/**
|
|
56
|
-
* Wrap a caught error as a TransformError
|
|
57
|
-
*/
|
|
58
|
-
export declare function wrapError(caught: unknown, category: ErrorCategory, context?: Record<string, unknown>): TransformError;
|
|
59
|
-
/**
|
|
60
|
-
* Format error for logging
|
|
61
|
-
*/
|
|
62
|
-
export declare function formatError(error: TransformError): string;
|
|
63
|
-
/**
|
|
64
|
-
* Check if error is validation error
|
|
65
|
-
*/
|
|
66
|
-
export declare function isValidationError(error: TransformError): boolean;
|
|
67
|
-
/**
|
|
68
|
-
* Check if error is configuration error
|
|
69
|
-
*/
|
|
70
|
-
export declare function isConfigurationError(error: TransformError): boolean;
|
|
71
|
-
/**
|
|
72
|
-
* Check if error is internal error
|
|
73
|
-
*/
|
|
74
|
-
export declare function isInternalError(error: TransformError): boolean;
|
|
75
|
-
/**
|
|
76
|
-
* Check if error is external error
|
|
77
|
-
*/
|
|
78
|
-
export declare function isExternalError(error: TransformError): boolean;
|
package/dist/utils/errors.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error Utilities
|
|
3
|
-
* Helpers for creating and handling categorized transform errors
|
|
4
|
-
*/
|
|
5
|
-
import { ErrorCategory } from '@pie-qti/transform-types';
|
|
6
|
-
/**
|
|
7
|
-
* Create a categorized transform error
|
|
8
|
-
*/
|
|
9
|
-
export function createError(options) {
|
|
10
|
-
return {
|
|
11
|
-
message: options.message,
|
|
12
|
-
code: options.code,
|
|
13
|
-
itemId: options.itemId,
|
|
14
|
-
category: options.category,
|
|
15
|
-
recoverable: options.recoverable ?? isRecoverableByDefault(options.category),
|
|
16
|
-
cause: options.cause,
|
|
17
|
-
context: options.context,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Create a validation error (user input issue)
|
|
22
|
-
*/
|
|
23
|
-
export function createValidationError(message, options) {
|
|
24
|
-
return createError({
|
|
25
|
-
message,
|
|
26
|
-
category: ErrorCategory.VALIDATION,
|
|
27
|
-
recoverable: true,
|
|
28
|
-
...options,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Create a configuration error (setup issue)
|
|
33
|
-
*/
|
|
34
|
-
export function createConfigurationError(message, options) {
|
|
35
|
-
return createError({
|
|
36
|
-
message,
|
|
37
|
-
category: ErrorCategory.CONFIGURATION,
|
|
38
|
-
recoverable: false,
|
|
39
|
-
...options,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Create an internal error (bug or unexpected state)
|
|
44
|
-
*/
|
|
45
|
-
export function createInternalError(message, options) {
|
|
46
|
-
return createError({
|
|
47
|
-
message,
|
|
48
|
-
category: ErrorCategory.INTERNAL,
|
|
49
|
-
recoverable: false,
|
|
50
|
-
...options,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Create an external error (service/network failure)
|
|
55
|
-
*/
|
|
56
|
-
export function createExternalError(message, options) {
|
|
57
|
-
return createError({
|
|
58
|
-
message,
|
|
59
|
-
category: ErrorCategory.EXTERNAL,
|
|
60
|
-
recoverable: true,
|
|
61
|
-
...options,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Check if error is recoverable by default based on category
|
|
66
|
-
*/
|
|
67
|
-
function isRecoverableByDefault(category) {
|
|
68
|
-
switch (category) {
|
|
69
|
-
case ErrorCategory.VALIDATION:
|
|
70
|
-
case ErrorCategory.EXTERNAL:
|
|
71
|
-
return true;
|
|
72
|
-
case ErrorCategory.CONFIGURATION:
|
|
73
|
-
case ErrorCategory.INTERNAL:
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Check if error should be retried
|
|
79
|
-
*/
|
|
80
|
-
export function shouldRetry(error) {
|
|
81
|
-
return error.recoverable && error.category === ErrorCategory.EXTERNAL;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Check if error should be shown to user
|
|
85
|
-
*/
|
|
86
|
-
export function shouldShowToUser(error) {
|
|
87
|
-
return error.category === ErrorCategory.VALIDATION;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Check if error requires ops team notification
|
|
91
|
-
*/
|
|
92
|
-
export function shouldNotifyOps(error) {
|
|
93
|
-
return (error.category === ErrorCategory.CONFIGURATION ||
|
|
94
|
-
error.category === ErrorCategory.INTERNAL);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Get suggested action for error
|
|
98
|
-
*/
|
|
99
|
-
export function getSuggestedAction(error) {
|
|
100
|
-
switch (error.category) {
|
|
101
|
-
case ErrorCategory.VALIDATION:
|
|
102
|
-
return 'Fix the input and try again';
|
|
103
|
-
case ErrorCategory.CONFIGURATION:
|
|
104
|
-
return 'Check configuration and credentials';
|
|
105
|
-
case ErrorCategory.INTERNAL:
|
|
106
|
-
return 'Contact support with error details';
|
|
107
|
-
case ErrorCategory.EXTERNAL:
|
|
108
|
-
return 'Retry the operation after a delay';
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Wrap a caught error as a TransformError
|
|
113
|
-
*/
|
|
114
|
-
export function wrapError(caught, category, context) {
|
|
115
|
-
const cause = caught instanceof Error ? caught : new Error(String(caught));
|
|
116
|
-
return createError({
|
|
117
|
-
message: cause.message,
|
|
118
|
-
category,
|
|
119
|
-
cause,
|
|
120
|
-
context,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Format error for logging
|
|
125
|
-
*/
|
|
126
|
-
export function formatError(error) {
|
|
127
|
-
const parts = [
|
|
128
|
-
`[${error.category.toUpperCase()}]`,
|
|
129
|
-
error.code ? `[${error.code}]` : '',
|
|
130
|
-
error.itemId ? `[item:${error.itemId}]` : '',
|
|
131
|
-
error.message,
|
|
132
|
-
];
|
|
133
|
-
return parts.filter(Boolean).join(' ');
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Check if error is validation error
|
|
137
|
-
*/
|
|
138
|
-
export function isValidationError(error) {
|
|
139
|
-
return error.category === ErrorCategory.VALIDATION;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Check if error is configuration error
|
|
143
|
-
*/
|
|
144
|
-
export function isConfigurationError(error) {
|
|
145
|
-
return error.category === ErrorCategory.CONFIGURATION;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Check if error is internal error
|
|
149
|
-
*/
|
|
150
|
-
export function isInternalError(error) {
|
|
151
|
-
return error.category === ErrorCategory.INTERNAL;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Check if error is external error
|
|
155
|
-
*/
|
|
156
|
-
export function isExternalError(error) {
|
|
157
|
-
return error.category === ErrorCategory.EXTERNAL;
|
|
158
|
-
}
|
|
159
|
-
//# sourceMappingURL=errors.js.map
|
package/dist/utils/errors.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAezD;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,OAA2B;IACtD,OAAO;QACN,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,sBAAsB,CAAC,OAAO,CAAC,QAAQ,CAAC;QAC5E,KAAK,EAAE,OAAO,CAAC,KAAK;QAC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AC,CAAC,CAAC,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE;QACnC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,EAAE;QAC5C,KAAK,CAAC,OAAO;KACb,CAAC;IAEF,OAAO,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACxC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAqB;IACtD,OAAO,KAAK,CAAC,QAAQ,KAAK,aAAa,CAAC,UAAU,CAAC;AACpD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAqB;IACzD,OAAO,KAAK,CAAC,QAAQ,KAAK,aAAa,CAAC,aAAa,CAAC;AACvD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,KAAqB;IACpD,OAAO,KAAK,CAAC,QAAQ,KAAK,aAAa,CAAC,QAAQ,CAAC;AAClD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,KAAqB;IACpD,OAAO,KAAK,CAAC,QAAQ,KAAK,aAAa,CAAC,QAAQ,CAAC;AAClD,CAAC","sourcesContent":["/**\n * Error Utilities\n * Helpers for creating and handling categorized transform errors\n */\n\nimport type { TransformError } from '@pie-qti/transform-types';\nimport { ErrorCategory } from '@pie-qti/transform-types';\n\n/**\n * Options for creating a transform error\n */\nexport interface CreateErrorOptions {\n\tmessage: string;\n\tcode?: string;\n\titemId?: string;\n\tcategory: ErrorCategory;\n\trecoverable?: boolean;\n\tcause?: Error;\n\tcontext?: Record<string, unknown>;\n}\n\n/**\n * Create a categorized transform error\n */\nexport function createError(options: CreateErrorOptions): TransformError {\n\treturn {\n\t\tmessage: options.message,\n\t\tcode: options.code,\n\t\titemId: options.itemId,\n\t\tcategory: options.category,\n\t\trecoverable: options.recoverable ?? isRecoverableByDefault(options.category),\n\t\tcause: options.cause,\n\t\tcontext: options.context,\n\t};\n}\n\n/**\n * Create a validation error (user input issue)\n */\nexport function createValidationError(\n\tmessage: string,\n\toptions?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>,\n): TransformError {\n\treturn createError({\n\t\tmessage,\n\t\tcategory: ErrorCategory.VALIDATION,\n\t\trecoverable: true,\n\t\t...options,\n\t});\n}\n\n/**\n * Create a configuration error (setup issue)\n */\nexport function createConfigurationError(\n\tmessage: string,\n\toptions?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>,\n): TransformError {\n\treturn createError({\n\t\tmessage,\n\t\tcategory: ErrorCategory.CONFIGURATION,\n\t\trecoverable: false,\n\t\t...options,\n\t});\n}\n\n/**\n * Create an internal error (bug or unexpected state)\n */\nexport function createInternalError(\n\tmessage: string,\n\toptions?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>,\n): TransformError {\n\treturn createError({\n\t\tmessage,\n\t\tcategory: ErrorCategory.INTERNAL,\n\t\trecoverable: false,\n\t\t...options,\n\t});\n}\n\n/**\n * Create an external error (service/network failure)\n */\nexport function createExternalError(\n\tmessage: string,\n\toptions?: Partial<Omit<CreateErrorOptions, 'message' | 'category'>>,\n): TransformError {\n\treturn createError({\n\t\tmessage,\n\t\tcategory: ErrorCategory.EXTERNAL,\n\t\trecoverable: true,\n\t\t...options,\n\t});\n}\n\n/**\n * Check if error is recoverable by default based on category\n */\nfunction isRecoverableByDefault(category: ErrorCategory): boolean {\n\tswitch (category) {\n\t\tcase ErrorCategory.VALIDATION:\n\t\tcase ErrorCategory.EXTERNAL:\n\t\t\treturn true;\n\t\tcase ErrorCategory.CONFIGURATION:\n\t\tcase ErrorCategory.INTERNAL:\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Check if error should be retried\n */\nexport function shouldRetry(error: TransformError): boolean {\n\treturn error.recoverable && error.category === ErrorCategory.EXTERNAL;\n}\n\n/**\n * Check if error should be shown to user\n */\nexport function shouldShowToUser(error: TransformError): boolean {\n\treturn error.category === ErrorCategory.VALIDATION;\n}\n\n/**\n * Check if error requires ops team notification\n */\nexport function shouldNotifyOps(error: TransformError): boolean {\n\treturn (\n\t\terror.category === ErrorCategory.CONFIGURATION ||\n\t\terror.category === ErrorCategory.INTERNAL\n\t);\n}\n\n/**\n * Get suggested action for error\n */\nexport function getSuggestedAction(error: TransformError): string {\n\tswitch (error.category) {\n\t\tcase ErrorCategory.VALIDATION:\n\t\t\treturn 'Fix the input and try again';\n\t\tcase ErrorCategory.CONFIGURATION:\n\t\t\treturn 'Check configuration and credentials';\n\t\tcase ErrorCategory.INTERNAL:\n\t\t\treturn 'Contact support with error details';\n\t\tcase ErrorCategory.EXTERNAL:\n\t\t\treturn 'Retry the operation after a delay';\n\t}\n}\n\n/**\n * Wrap a caught error as a TransformError\n */\nexport function wrapError(\n\tcaught: unknown,\n\tcategory: ErrorCategory,\n\tcontext?: Record<string, unknown>,\n): TransformError {\n\tconst cause = caught instanceof Error ? caught : new Error(String(caught));\n\n\treturn createError({\n\t\tmessage: cause.message,\n\t\tcategory,\n\t\tcause,\n\t\tcontext,\n\t});\n}\n\n/**\n * Format error for logging\n */\nexport function formatError(error: TransformError): string {\n\tconst parts = [\n\t\t`[${error.category.toUpperCase()}]`,\n\t\terror.code ? `[${error.code}]` : '',\n\t\terror.itemId ? `[item:${error.itemId}]` : '',\n\t\terror.message,\n\t];\n\n\treturn parts.filter(Boolean).join(' ');\n}\n\n/**\n * Check if error is validation error\n */\nexport function isValidationError(error: TransformError): boolean {\n\treturn error.category === ErrorCategory.VALIDATION;\n}\n\n/**\n * Check if error is configuration error\n */\nexport function isConfigurationError(error: TransformError): boolean {\n\treturn error.category === ErrorCategory.CONFIGURATION;\n}\n\n/**\n * Check if error is internal error\n */\nexport function isInternalError(error: TransformError): boolean {\n\treturn error.category === ErrorCategory.INTERNAL;\n}\n\n/**\n * Check if error is external error\n */\nexport function isExternalError(error: TransformError): boolean {\n\treturn error.category === ErrorCategory.EXTERNAL;\n}\n"]}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities for PIE items and QTI content
|
|
3
|
-
*/
|
|
4
|
-
import type { ValidationResult as BaseValidationResult, PieItem } from '@pie-qti/transform-types';
|
|
5
|
-
/**
|
|
6
|
-
* Detailed validation error with path information
|
|
7
|
-
*/
|
|
8
|
-
export interface ValidationError {
|
|
9
|
-
path: string;
|
|
10
|
-
message: string;
|
|
11
|
-
value?: any;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Extended validation result with detailed error information
|
|
15
|
-
*/
|
|
16
|
-
export interface DetailedValidationResult extends BaseValidationResult {
|
|
17
|
-
detailedErrors?: ValidationError[];
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* PIE Item Validator
|
|
21
|
-
* Validates PIE items against JSON schemas
|
|
22
|
-
*/
|
|
23
|
-
export declare class PieItemValidator {
|
|
24
|
-
private ajv;
|
|
25
|
-
private schemas;
|
|
26
|
-
constructor();
|
|
27
|
-
/**
|
|
28
|
-
* Register a PIE element schema
|
|
29
|
-
* @param elementType - Element type (e.g., '@pie-element/multiple-choice')
|
|
30
|
-
* @param schema - JSON Schema object
|
|
31
|
-
*/
|
|
32
|
-
registerSchema(elementType: string, schema: object): void;
|
|
33
|
-
/**
|
|
34
|
-
* Load a schema from the schemas package
|
|
35
|
-
* @param elementType - Element type (e.g., '@pie-element/multiple-choice')
|
|
36
|
-
*/
|
|
37
|
-
loadSchemaForElement(elementType: string): Promise<void>;
|
|
38
|
-
/**
|
|
39
|
-
* Load a schema from a file path (for custom schemas)
|
|
40
|
-
* @param elementType - Element type
|
|
41
|
-
* @param schemaPath - Path to pie-schema.json
|
|
42
|
-
*/
|
|
43
|
-
loadSchemaFromFile(elementType: string, schemaPath: string): Promise<void>;
|
|
44
|
-
/**
|
|
45
|
-
* Validate a PIE item
|
|
46
|
-
* @param item - PIE item to validate
|
|
47
|
-
* @returns Validation result
|
|
48
|
-
*/
|
|
49
|
-
validate(item: PieItem): DetailedValidationResult;
|
|
50
|
-
/**
|
|
51
|
-
* Validate multiple items
|
|
52
|
-
*/
|
|
53
|
-
validateMany(items: PieItem[]): DetailedValidationResult[];
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Basic structural validation for PIE items
|
|
57
|
-
* Used when schemas are not available
|
|
58
|
-
*/
|
|
59
|
-
export declare function validatePieItemStructure(item: any): DetailedValidationResult;
|
package/dist/utils/validator.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities for PIE items and QTI content
|
|
3
|
-
*/
|
|
4
|
-
import { loadSchema } from '@pie-qti/element-schemas';
|
|
5
|
-
import Ajv from 'ajv';
|
|
6
|
-
/**
|
|
7
|
-
* PIE Item Validator
|
|
8
|
-
* Validates PIE items against JSON schemas
|
|
9
|
-
*/
|
|
10
|
-
export class PieItemValidator {
|
|
11
|
-
ajv;
|
|
12
|
-
schemas;
|
|
13
|
-
constructor() {
|
|
14
|
-
this.ajv = new Ajv({
|
|
15
|
-
allErrors: true,
|
|
16
|
-
verbose: true,
|
|
17
|
-
strict: false // PIE schemas may not be strict
|
|
18
|
-
});
|
|
19
|
-
this.schemas = new Map();
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Register a PIE element schema
|
|
23
|
-
* @param elementType - Element type (e.g., '@pie-element/multiple-choice')
|
|
24
|
-
* @param schema - JSON Schema object
|
|
25
|
-
*/
|
|
26
|
-
registerSchema(elementType, schema) {
|
|
27
|
-
this.schemas.set(elementType, schema);
|
|
28
|
-
this.ajv.addSchema(schema, elementType);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Load a schema from the schemas package
|
|
32
|
-
* @param elementType - Element type (e.g., '@pie-element/multiple-choice')
|
|
33
|
-
*/
|
|
34
|
-
async loadSchemaForElement(elementType) {
|
|
35
|
-
try {
|
|
36
|
-
// Extract element name from full element type
|
|
37
|
-
// e.g., '@pie-element/multiple-choice' -> 'multiple-choice'
|
|
38
|
-
const elementName = elementType.replace('@pie-element/', '');
|
|
39
|
-
const schema = await loadSchema(elementName);
|
|
40
|
-
this.registerSchema(elementType, schema);
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
throw new Error(`Failed to load schema for ${elementType}: ${error.message}`);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Load a schema from a file path (for custom schemas)
|
|
48
|
-
* @param elementType - Element type
|
|
49
|
-
* @param schemaPath - Path to pie-schema.json
|
|
50
|
-
*/
|
|
51
|
-
async loadSchemaFromFile(elementType, schemaPath) {
|
|
52
|
-
try {
|
|
53
|
-
const fs = await import('node:fs/promises');
|
|
54
|
-
const schemaContent = await fs.readFile(schemaPath, 'utf-8');
|
|
55
|
-
const schema = JSON.parse(schemaContent);
|
|
56
|
-
this.registerSchema(elementType, schema);
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
throw new Error(`Failed to load schema from ${schemaPath}: ${error.message}`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Validate a PIE item
|
|
64
|
-
* @param item - PIE item to validate
|
|
65
|
-
* @returns Validation result
|
|
66
|
-
*/
|
|
67
|
-
validate(item) {
|
|
68
|
-
const errors = [];
|
|
69
|
-
// Validate basic structure
|
|
70
|
-
if (!item.id) {
|
|
71
|
-
errors.push({ path: 'id', message: 'Item ID is required' });
|
|
72
|
-
}
|
|
73
|
-
if (!item.uuid) {
|
|
74
|
-
errors.push({ path: 'uuid', message: 'Item UUID is required' });
|
|
75
|
-
}
|
|
76
|
-
if (!item.config) {
|
|
77
|
-
errors.push({ path: 'config', message: 'Item config is required' });
|
|
78
|
-
return {
|
|
79
|
-
valid: false,
|
|
80
|
-
errors: errors.map(e => `${e.path}: ${e.message}`),
|
|
81
|
-
detailedErrors: errors
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
// Validate models against their schemas
|
|
85
|
-
if (item.config.models) {
|
|
86
|
-
for (let i = 0; i < item.config.models.length; i++) {
|
|
87
|
-
const model = item.config.models[i];
|
|
88
|
-
if (!model.element) {
|
|
89
|
-
errors.push({
|
|
90
|
-
path: `config.models[${i}].element`,
|
|
91
|
-
message: 'Model element type is required'
|
|
92
|
-
});
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
// Check if we have a schema for this element type
|
|
96
|
-
const schema = this.schemas.get(model.element);
|
|
97
|
-
if (schema) {
|
|
98
|
-
const validate = this.ajv.compile(schema);
|
|
99
|
-
const valid = validate(model);
|
|
100
|
-
if (!valid && validate.errors) {
|
|
101
|
-
for (const error of validate.errors) {
|
|
102
|
-
errors.push({
|
|
103
|
-
path: `config.models[${i}]${error.instancePath}`,
|
|
104
|
-
message: error.message || 'Validation error',
|
|
105
|
-
value: error.data,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return {
|
|
113
|
-
valid: errors.length === 0,
|
|
114
|
-
errors: errors.length > 0 ? errors.map(e => `${e.path}: ${e.message}`) : undefined,
|
|
115
|
-
detailedErrors: errors.length > 0 ? errors : undefined,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Validate multiple items
|
|
120
|
-
*/
|
|
121
|
-
validateMany(items) {
|
|
122
|
-
return items.map(item => this.validate(item));
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Basic structural validation for PIE items
|
|
127
|
-
* Used when schemas are not available
|
|
128
|
-
*/
|
|
129
|
-
export function validatePieItemStructure(item) {
|
|
130
|
-
const errors = [];
|
|
131
|
-
if (typeof item !== 'object' || item === null) {
|
|
132
|
-
errors.push({ path: '', message: 'Item must be an object' });
|
|
133
|
-
return {
|
|
134
|
-
valid: false,
|
|
135
|
-
errors: ['Item must be an object'],
|
|
136
|
-
detailedErrors: errors
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
// Check required fields
|
|
140
|
-
const requiredFields = ['id', 'uuid', 'config'];
|
|
141
|
-
for (const field of requiredFields) {
|
|
142
|
-
if (!(field in item)) {
|
|
143
|
-
errors.push({ path: field, message: `${field} is required` });
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
// Check config structure
|
|
147
|
-
if (item.config) {
|
|
148
|
-
if (!Array.isArray(item.config.models)) {
|
|
149
|
-
errors.push({ path: 'config.models', message: 'config.models must be an array' });
|
|
150
|
-
}
|
|
151
|
-
if (typeof item.config.elements !== 'object') {
|
|
152
|
-
errors.push({ path: 'config.elements', message: 'config.elements must be an object' });
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return {
|
|
156
|
-
valid: errors.length === 0,
|
|
157
|
-
errors: errors.length > 0 ? errors.map(e => `${e.path}: ${e.message}`) : undefined,
|
|
158
|
-
detailedErrors: errors.length > 0 ? errors : undefined,
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
//# sourceMappingURL=validator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/utils/validator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAoB,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAExE,OAAO,GAAG,MAAM,KAAK,CAAC;AAkBtB;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IACnB,GAAG,CAAM;IACT,OAAO,CAAsB;IAErC;QACE,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC;YACjB,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,IAAI;YACb,MAAM,EAAE,KAAK,CAAC,gCAAgC;SAC/C,CAAC,CAAC;QACH,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,cAAc,CAAC,WAAmB,EAAE,MAAc;Q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K,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS;QAClF,cAAc,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;KACvD,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Validation utilities for PIE items and QTI content\n */\n\nimport { type ElementType, loadSchema } from '@pie-qti/element-schemas';\nimport type { ValidationResult as BaseValidationResult, PieItem } from '@pie-qti/transform-types';\nimport Ajv from 'ajv';\n\n/**\n * Detailed validation error with path information\n */\nexport interface ValidationError {\n path: string;\n message: string;\n value?: any;\n}\n\n/**\n * Extended validation result with detailed error information\n */\nexport interface DetailedValidationResult extends BaseValidationResult {\n detailedErrors?: ValidationError[];\n}\n\n/**\n * PIE Item Validator\n * Validates PIE items against JSON schemas\n */\nexport class PieItemValidator {\n private ajv: Ajv;\n private schemas: Map<string, object>;\n\n constructor() {\n this.ajv = new Ajv({\n allErrors: true,\n verbose: true,\n strict: false // PIE schemas may not be strict\n });\n this.schemas = new Map();\n }\n\n /**\n * Register a PIE element schema\n * @param elementType - Element type (e.g., '@pie-element/multiple-choice')\n * @param schema - JSON Schema object\n */\n registerSchema(elementType: string, schema: object): void {\n this.schemas.set(elementType, schema);\n this.ajv.addSchema(schema, elementType);\n }\n\n /**\n * Load a schema from the schemas package\n * @param elementType - Element type (e.g., '@pie-element/multiple-choice')\n */\n async loadSchemaForElement(elementType: string): Promise<void> {\n try {\n // Extract element name from full element type\n // e.g., '@pie-element/multiple-choice' -> 'multiple-choice'\n const elementName = elementType.replace('@pie-element/', '') as ElementType;\n const schema = await loadSchema(elementName);\n this.registerSchema(elementType, schema);\n } catch (error) {\n throw new Error(`Failed to load schema for ${elementType}: ${(error as Error).message}`);\n }\n }\n\n /**\n * Load a schema from a file path (for custom schemas)\n * @param elementType - Element type\n * @param schemaPath - Path to pie-schema.json\n */\n async loadSchemaFromFile(elementType: string, schemaPath: string): Promise<void> {\n try {\n const fs = await import('node:fs/promises');\n const schemaContent = await fs.readFile(schemaPath, 'utf-8');\n const schema = JSON.parse(schemaContent);\n this.registerSchema(elementType, schema);\n } catch (error) {\n throw new Error(`Failed to load schema from ${schemaPath}: ${(error as Error).message}`);\n }\n }\n\n /**\n * Validate a PIE item\n * @param item - PIE item to validate\n * @returns Validation result\n */\n validate(item: PieItem): DetailedValidationResult {\n const errors: ValidationError[] = [];\n\n // Validate basic structure\n if (!item.id) {\n errors.push({ path: 'id', message: 'Item ID is required' });\n }\n if (!item.uuid) {\n errors.push({ path: 'uuid', message: 'Item UUID is required' });\n }\n if (!item.config) {\n errors.push({ path: 'config', message: 'Item config is required' });\n return {\n valid: false,\n errors: errors.map(e => `${e.path}: ${e.message}`),\n detailedErrors: errors\n };\n }\n\n // Validate models against their schemas\n if (item.config.models) {\n for (let i = 0; i < item.config.models.length; i++) {\n const model = item.config.models[i];\n if (!model.element) {\n errors.push({\n path: `config.models[${i}].element`,\n message: 'Model element type is required'\n });\n continue;\n }\n\n // Check if we have a schema for this element type\n const schema = this.schemas.get(model.element);\n if (schema) {\n const validate = this.ajv.compile(schema);\n const valid = validate(model);\n\n if (!valid && validate.errors) {\n for (const error of validate.errors) {\n errors.push({\n path: `config.models[${i}]${error.instancePath}`,\n message: error.message || 'Validation error',\n value: error.data,\n });\n }\n }\n }\n }\n }\n\n return {\n valid: errors.length === 0,\n errors: errors.length > 0 ? errors.map(e => `${e.path}: ${e.message}`) : undefined,\n detailedErrors: errors.length > 0 ? errors : undefined,\n };\n }\n\n /**\n * Validate multiple items\n */\n validateMany(items: PieItem[]): DetailedValidationResult[] {\n return items.map(item => this.validate(item));\n }\n}\n\n/**\n * Basic structural validation for PIE items\n * Used when schemas are not available\n */\nexport function validatePieItemStructure(item: any): DetailedValidationResult {\n const errors: ValidationError[] = [];\n\n if (typeof item !== 'object' || item === null) {\n errors.push({ path: '', message: 'Item must be an object' });\n return {\n valid: false,\n errors: ['Item must be an object'],\n detailedErrors: errors\n };\n }\n\n // Check required fields\n const requiredFields = ['id', 'uuid', 'config'];\n for (const field of requiredFields) {\n if (!(field in item)) {\n errors.push({ path: field, message: `${field} is required` });\n }\n }\n\n // Check config structure\n if (item.config) {\n if (!Array.isArray(item.config.models)) {\n errors.push({ path: 'config.models', message: 'config.models must be an array' });\n }\n if (typeof item.config.elements !== 'object') {\n errors.push({ path: 'config.elements', message: 'config.elements must be an object' });\n }\n }\n\n return {\n valid: errors.length === 0,\n errors: errors.length > 0 ? errors.map(e => `${e.path}: ${e.message}`) : undefined,\n detailedErrors: errors.length > 0 ? errors : undefined,\n };\n}\n"]}
|