xml-model 1.3.3 → 2.0.0-beta.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.
- package/README.md +39 -35
- package/dist/_virtual/_rolldown/runtime.js +8 -0
- package/dist/index.d.ts +3 -7
- package/dist/index.js +7 -20
- package/dist/model.d.ts +53 -0
- package/dist/model.js +75 -0
- package/dist/node_modules/sax/lib/sax.js +1249 -0
- package/dist/node_modules/xml-js/lib/array-helper.js +13 -0
- package/dist/node_modules/xml-js/lib/index.js +19 -0
- package/dist/node_modules/xml-js/lib/js2xml.js +258 -0
- package/dist/node_modules/xml-js/lib/json2xml.js +22 -0
- package/dist/node_modules/xml-js/lib/options-helper.js +33 -0
- package/dist/node_modules/xml-js/lib/xml2js.js +246 -0
- package/dist/node_modules/xml-js/lib/xml2json.js +26 -0
- package/dist/util/kebab-case.js +24 -13
- package/dist/util/zod.d.ts +4 -0
- package/dist/util/zod.js +21 -0
- package/dist/xml/codec.d.ts +87 -0
- package/dist/xml/codec.js +339 -0
- package/dist/xml/examples.d.ts +188 -0
- package/dist/xml/index.d.ts +5 -79
- package/dist/xml/index.js +4 -57
- package/dist/xml/model.d.ts +18 -0
- package/dist/xml/model.js +33 -0
- package/dist/xml/schema-meta.d.ts +57 -0
- package/dist/xml/schema-meta.js +88 -0
- package/dist/xml/xml-js.d.ts +280 -3
- package/dist/xml/xml-js.js +121 -8
- package/package.json +27 -42
- package/dist/_virtual/Reflect.js +0 -8
- package/dist/_virtual/Reflect2.js +0 -5
- package/dist/_virtual/_commonjsHelpers.js +0 -47
- package/dist/defaults.d.ts +0 -28
- package/dist/defaults.js +0 -165
- package/dist/errors.d.ts +0 -40
- package/dist/errors.js +0 -45
- package/dist/middleware.d.ts +0 -10
- package/dist/middleware.js +0 -25
- package/dist/model/built-ins.d.ts +0 -3
- package/dist/model/built-ins.js +0 -43
- package/dist/model/index.d.ts +0 -69
- package/dist/model/index.js +0 -264
- package/dist/model/property.d.ts +0 -18
- package/dist/model/property.js +0 -67
- package/dist/model/registry.d.ts +0 -9
- package/dist/model/registry.js +0 -19
- package/dist/model/types.d.ts +0 -109
- package/dist/node_modules/reflect-metadata/Reflect.js +0 -806
- package/dist/types.d.ts +0 -25
- package/dist/util/is-regexp.d.ts +0 -12
- package/dist/util/is-regexp.js +0 -8
- package/dist/util/merge-maps.d.ts +0 -2
- package/dist/util/merge-maps.js +0 -23
- package/dist/vite/index.d.ts +0 -80
- package/dist/vite/index.js +0 -71
- package/dist/vite/node_modules/typescript-rtti/dist.esm/common/format.js +0 -105
- package/dist/vite/node_modules/typescript-rtti/dist.esm/common/index.js +0 -55
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/api-call-transformer.js +0 -152
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/class-analyzer.js +0 -83
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/compile-error.js +0 -8
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/import-analyzer.js +0 -89
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/interface-analyzer.js +0 -58
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/visitor-base.js +0 -93
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/declarations-emitter.js +0 -31
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/encode-parameter.js +0 -64
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/find-relative-path.js +0 -41
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/flags.js +0 -43
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/forward-ref.js +0 -20
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/get-exports-for-symbol.js +0 -64
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/index.js +0 -130
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/legacy-decorator.js +0 -10
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/legacy-type-encoder.js +0 -82
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/literal-node.js +0 -9
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-collector.js +0 -56
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-decorator.js +0 -80
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-emitter.js +0 -425
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-encoder.js +0 -212
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/rt-helper.js +0 -96
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/rtti-visitor-base.js +0 -28
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/serialize.js +0 -31
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/type-encoder.js +0 -76
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/type-literal.js +0 -499
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/utils.js +0 -906
- package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/workarounds.js +0 -7
package/dist/xml/xml-js.js
CHANGED
|
@@ -1,9 +1,122 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { require_lib } from "../node_modules/xml-js/lib/index.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/xml/xml-js.ts
|
|
4
|
+
var import_lib = require_lib();
|
|
5
|
+
var ZXMLElementNode = z.object({
|
|
6
|
+
type: z.literal("element"),
|
|
7
|
+
name: z.string(),
|
|
8
|
+
attributes: z.record(z.string(), z.string()).optional(),
|
|
9
|
+
get elements() {
|
|
10
|
+
return z.array(ZXMLNode).optional();
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var ZXMLCommentNode = z.object({
|
|
14
|
+
type: z.literal("comment"),
|
|
15
|
+
comment: z.string()
|
|
16
|
+
});
|
|
17
|
+
var ZXMLTextNode = z.object({
|
|
18
|
+
type: z.literal("text"),
|
|
19
|
+
text: z.string()
|
|
20
|
+
});
|
|
21
|
+
var ZXMLNode = z.discriminatedUnion("type", [
|
|
22
|
+
ZXMLElementNode,
|
|
23
|
+
ZXMLCommentNode,
|
|
24
|
+
ZXMLTextNode
|
|
25
|
+
]);
|
|
26
|
+
var ZXMLRoot = z.object({ elements: z.array(ZXMLNode) });
|
|
27
|
+
function parse(xml, options = {}) {
|
|
28
|
+
const strippedOptions = { ...options };
|
|
29
|
+
delete strippedOptions["compact"];
|
|
30
|
+
const res = (0, import_lib.xml2js)(xml, strippedOptions);
|
|
31
|
+
if ("elements" in res) return res;
|
|
32
|
+
throw new Error("Got empty XML");
|
|
33
|
+
}
|
|
34
|
+
function stringify(xml, options = {}) {
|
|
35
|
+
return (0, import_lib.js2xml)(xml, options);
|
|
36
|
+
}
|
|
37
|
+
function isRoot(xml) {
|
|
38
|
+
return Object.keys(xml).length === 1 && "elements" in xml && Array.isArray(xml.elements);
|
|
39
|
+
}
|
|
40
|
+
function elementFromRoot(root) {
|
|
41
|
+
return root.elements.find((el) => el.type === "element");
|
|
42
|
+
}
|
|
43
|
+
function isEmpty(xml) {
|
|
44
|
+
return Object.keys(xml).length === 0;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Extracts the text content from an element that has a single text child node.
|
|
48
|
+
*
|
|
49
|
+
* @param xml - An `XMLElement` expected to contain a single text node.
|
|
50
|
+
* @returns The text value, or an empty string when there are no child elements.
|
|
51
|
+
* @throws {TypeError} When the element has multiple or non-text children.
|
|
52
|
+
*/
|
|
53
|
+
function getContent(xml) {
|
|
54
|
+
if (xml.elements?.length === 1) {
|
|
55
|
+
const content = xml.elements[0];
|
|
56
|
+
if (content.type === "text") return content.text;
|
|
57
|
+
}
|
|
58
|
+
if (!xml.elements) return "";
|
|
59
|
+
throw new TypeError(`can't get text from XMLElement: ${JSON.stringify(xml)}`);
|
|
60
|
+
}
|
|
61
|
+
function fromContent(content = "", tag, attributes) {
|
|
62
|
+
const elements = content ? [{
|
|
63
|
+
type: "text",
|
|
64
|
+
text: String(content)
|
|
65
|
+
}] : [];
|
|
66
|
+
if (!tag) return { elements };
|
|
67
|
+
const el = {
|
|
68
|
+
type: "element",
|
|
69
|
+
name: tag,
|
|
70
|
+
elements
|
|
71
|
+
};
|
|
72
|
+
if (attributes) el.attributes = attributes;
|
|
73
|
+
return el;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Appends a child element to `xml`, initialising the `elements` array if needed.
|
|
77
|
+
*
|
|
78
|
+
* @param xml - The parent element to modify.
|
|
79
|
+
* @param element - The child element to append.
|
|
80
|
+
*/
|
|
81
|
+
function addElement(xml, element) {
|
|
82
|
+
if (!xml.elements) xml.elements = [];
|
|
83
|
+
xml.elements.push(element);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sets an attribute on an element, initialising the `attributes` map if needed.
|
|
87
|
+
*
|
|
88
|
+
* @param xml - The element to modify.
|
|
89
|
+
* @param attribute - The attribute name.
|
|
90
|
+
* @param value - The attribute value.
|
|
91
|
+
*/
|
|
92
|
+
function setAttribute(xml, attribute, value) {
|
|
93
|
+
if (!xml.attributes) xml.attributes = {};
|
|
94
|
+
xml.attributes[attribute] = value;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Removes an attribute from an element. Does nothing if the element has no attributes.
|
|
98
|
+
*
|
|
99
|
+
* @param xml - The element to modify.
|
|
100
|
+
* @param attribute - The attribute name to remove.
|
|
101
|
+
*/
|
|
102
|
+
function deleteAttribute(xml, attribute) {
|
|
103
|
+
if (!xml.attributes) return;
|
|
104
|
+
delete xml.attributes[attribute];
|
|
105
|
+
}
|
|
106
|
+
/** Namespace object bundling all XML utility functions. */
|
|
107
|
+
var XML = {
|
|
108
|
+
parse,
|
|
109
|
+
stringify,
|
|
110
|
+
isRoot,
|
|
111
|
+
elementFromRoot,
|
|
112
|
+
isEmpty,
|
|
113
|
+
fromContent,
|
|
114
|
+
getContent,
|
|
115
|
+
addElement,
|
|
116
|
+
setAttribute,
|
|
117
|
+
deleteAttribute
|
|
5
118
|
};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
//# sourceMappingURL=xml-js.js.map
|
|
119
|
+
//#endregion
|
|
120
|
+
export { ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode, XML as default };
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=xml-js.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xml-model",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
4
|
"description": "allows transparent XML <-> Object conversion in typescript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "MathisTLD",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
13
|
},
|
|
14
|
-
"./
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"import": "./dist/
|
|
14
|
+
"./xml": {
|
|
15
|
+
"types": "./dist/xml/index.d.ts",
|
|
16
|
+
"import": "./dist/xml/index.js"
|
|
17
17
|
},
|
|
18
18
|
"./*": {
|
|
19
19
|
"types": "./dist/*.d.ts",
|
|
@@ -21,51 +21,36 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"scripts": {
|
|
24
|
-
"preversion": "
|
|
24
|
+
"preversion": "vp test run",
|
|
25
25
|
"version": "npm run build",
|
|
26
26
|
"postversion": "git push && git push --tags",
|
|
27
|
-
"build": "
|
|
28
|
-
"dev": "
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"lint
|
|
32
|
-
"
|
|
33
|
-
"fmt
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"typescript-rtti": "0.9.6",
|
|
38
|
-
"xml-js": "^1.6.11"
|
|
27
|
+
"build": "vp build",
|
|
28
|
+
"dev": "vp build --watch",
|
|
29
|
+
"docs": "vp dev",
|
|
30
|
+
"test": "vp test",
|
|
31
|
+
"lint": "vp lint",
|
|
32
|
+
"lint:fix": "vp lint --fix",
|
|
33
|
+
"fmt": "vp fmt",
|
|
34
|
+
"fmt:check": "vp fmt --check",
|
|
35
|
+
"prepare": "vp config"
|
|
39
36
|
},
|
|
37
|
+
"dependencies": {},
|
|
40
38
|
"devDependencies": {
|
|
41
|
-
"@rollup/plugin-typescript": "*",
|
|
42
39
|
"@types/node": "^24.10.11",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"oxfmt": "^0.36.0",
|
|
47
|
-
"oxlint": "^1.51.0",
|
|
48
|
-
"reflect-metadata": "^0.1.14",
|
|
49
|
-
"typescript": "*",
|
|
50
|
-
"vite": "*",
|
|
40
|
+
"marmotte": "^0.4.4",
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"vite-plus": "latest",
|
|
51
43
|
"vitepress": "^2.0.0-alpha.16",
|
|
52
|
-
"
|
|
44
|
+
"vitepress-plugin-llms": "^1.11.0",
|
|
45
|
+
"vitest": "npm:@voidzero-dev/vite-plus-test@latest",
|
|
46
|
+
"xml-js": "^1.6.11"
|
|
53
47
|
},
|
|
54
48
|
"peerDependencies": {
|
|
55
|
-
"
|
|
56
|
-
"tslib": "^2.8.1",
|
|
57
|
-
"typescript": "~5.1.6",
|
|
58
|
-
"vite": "^7.3.1"
|
|
49
|
+
"zod": "^4.1.0"
|
|
59
50
|
},
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
},
|
|
64
|
-
"vite": {
|
|
65
|
-
"optional": true
|
|
66
|
-
}
|
|
51
|
+
"overrides": {
|
|
52
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
53
|
+
"vitest": "npm:@voidzero-dev/vite-plus-test@latest"
|
|
67
54
|
},
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
}
|
|
55
|
+
"packageManager": "npm@11.12.0"
|
|
56
|
+
}
|
package/dist/_virtual/Reflect.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { getDefaultExportFromCjs } from "./_commonjsHelpers.js";
|
|
2
|
-
import { __require as require_Reflect } from "../node_modules/reflect-metadata/Reflect.js";
|
|
3
|
-
var _ReflectExports = require_Reflect();
|
|
4
|
-
const Reflect = /* @__PURE__ */ getDefaultExportFromCjs(_ReflectExports);
|
|
5
|
-
export {
|
|
6
|
-
Reflect as default
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=Reflect.js.map
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
2
|
-
function getDefaultExportFromCjs(x) {
|
|
3
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
4
|
-
}
|
|
5
|
-
function getDefaultExportFromNamespaceIfPresent(n) {
|
|
6
|
-
return n && Object.prototype.hasOwnProperty.call(n, "default") ? n["default"] : n;
|
|
7
|
-
}
|
|
8
|
-
function getDefaultExportFromNamespaceIfNotNamed(n) {
|
|
9
|
-
return n && Object.prototype.hasOwnProperty.call(n, "default") && Object.keys(n).length === 1 ? n["default"] : n;
|
|
10
|
-
}
|
|
11
|
-
function getAugmentedNamespace(n) {
|
|
12
|
-
if (Object.prototype.hasOwnProperty.call(n, "__esModule")) return n;
|
|
13
|
-
var f = n.default;
|
|
14
|
-
if (typeof f == "function") {
|
|
15
|
-
var a = function a2() {
|
|
16
|
-
var isInstance = false;
|
|
17
|
-
try {
|
|
18
|
-
isInstance = this instanceof a2;
|
|
19
|
-
} catch {
|
|
20
|
-
}
|
|
21
|
-
if (isInstance) {
|
|
22
|
-
return Reflect.construct(f, arguments, this.constructor);
|
|
23
|
-
}
|
|
24
|
-
return f.apply(this, arguments);
|
|
25
|
-
};
|
|
26
|
-
a.prototype = f.prototype;
|
|
27
|
-
} else a = {};
|
|
28
|
-
Object.defineProperty(a, "__esModule", { value: true });
|
|
29
|
-
Object.keys(n).forEach(function(k) {
|
|
30
|
-
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
31
|
-
Object.defineProperty(a, k, d.get ? d : {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
get: function() {
|
|
34
|
-
return n[k];
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
return a;
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
commonjsGlobal,
|
|
42
|
-
getAugmentedNamespace,
|
|
43
|
-
getDefaultExportFromCjs,
|
|
44
|
-
getDefaultExportFromNamespaceIfNotNamed,
|
|
45
|
-
getDefaultExportFromNamespaceIfPresent
|
|
46
|
-
};
|
|
47
|
-
//# sourceMappingURL=_commonjsHelpers.js.map
|
package/dist/defaults.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { fromXMLContext, PropertyFromXMLContext, PropertyToXMLContext, toXMLContext, XMLModelPropertyOptions, XMLModel } from './model/types';
|
|
2
|
-
import { XMLElement, XMLRoot } from './types';
|
|
3
|
-
interface Defaults {
|
|
4
|
-
fromXML<T>(context: fromXMLContext<T>): T;
|
|
5
|
-
propertySourceElementsFilter<T>(...args: Parameters<XMLModelPropertyOptions<T>["isSourceElement"]>): boolean;
|
|
6
|
-
propertyResolveSourceElements<T>(context: Omit<PropertyFromXMLContext<T>, "elements">): XMLElement[];
|
|
7
|
-
propertyFromXML<T>(context: PropertyFromXMLContext<T>): T[keyof T];
|
|
8
|
-
toXML<T>(context: toXMLContext<T>): XMLRoot;
|
|
9
|
-
tagnameFromModel: (model: XMLModel) => string;
|
|
10
|
-
tagnameFromProperty<T>(property: XMLModelPropertyOptions<T>): string;
|
|
11
|
-
propertyToXML<T>(context: PropertyToXMLContext<T>): XMLRoot;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Global defaults used by all models and properties when no override is provided.
|
|
15
|
-
*
|
|
16
|
-
* You can mutate individual entries to change library-wide behaviour — for example,
|
|
17
|
-
* replace `defaults.fromXML` to provide a base deserialization strategy instead of
|
|
18
|
-
* having to specify `fromXML` on every `@Model()`.
|
|
19
|
-
*
|
|
20
|
-
* Key behaviours:
|
|
21
|
-
* - `fromXML` — throws by default; every root model **must** provide its own implementation.
|
|
22
|
-
* - `tagnameFromModel` / `tagnameFromProperty` — convert class/property names to kebab-case.
|
|
23
|
-
* - `propertyFromXML` — handles class, array, and union-of-literal property types via runtime reflection.
|
|
24
|
-
* - `propertyToXML` — serialises class, array, and union-of-literal values; respects `inline`.
|
|
25
|
-
*/
|
|
26
|
-
export declare const defaults: Defaults;
|
|
27
|
-
export {};
|
|
28
|
-
//# sourceMappingURL=defaults.d.ts.map
|
package/dist/defaults.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import { getModel } from "./model/registry.js";
|
|
2
|
-
import { kebabCase } from "./util/kebab-case.js";
|
|
3
|
-
const defaults = {
|
|
4
|
-
/**
|
|
5
|
-
* Default model-level `fromXML` handler.
|
|
6
|
-
* Always throws — models must supply their own `fromXML` or inherit one from a parent.
|
|
7
|
-
* @throws {TypeError}
|
|
8
|
-
*/
|
|
9
|
-
fromXML() {
|
|
10
|
-
throw new TypeError(
|
|
11
|
-
"you should define 'defaults.fromXML' yourself or provide a 'fromXML' function to @Model() decorator's options"
|
|
12
|
-
);
|
|
13
|
-
},
|
|
14
|
-
/**
|
|
15
|
-
* Collects the XML elements that are the source data for a property.
|
|
16
|
-
* Assumes `xml.elements[0]` is the wrapper element that contains all property tags.
|
|
17
|
-
*/
|
|
18
|
-
propertyResolveSourceElements(context) {
|
|
19
|
-
const innerElements = context.xml.elements[0]?.elements || [];
|
|
20
|
-
return innerElements.filter((el) => context.property.isSourceElement(el, context));
|
|
21
|
-
},
|
|
22
|
-
/**
|
|
23
|
-
* Returns `true` when the element's tag name matches the property's tagname.
|
|
24
|
-
*/
|
|
25
|
-
propertySourceElementsFilter(element, context) {
|
|
26
|
-
return context.property.tagname === element.name;
|
|
27
|
-
},
|
|
28
|
-
/**
|
|
29
|
-
* Converts resolved XML elements into a typed property value.
|
|
30
|
-
* Handles class types, arrays of class types, and union-of-literal types.
|
|
31
|
-
* Returns `undefined` for optional properties with no matching elements,
|
|
32
|
-
* and also for unrecognised types.
|
|
33
|
-
*/
|
|
34
|
-
propertyFromXML(context) {
|
|
35
|
-
const prop = context.property;
|
|
36
|
-
const elements = context.elements;
|
|
37
|
-
if (prop.reflected.isOptional && elements.length === 0) {
|
|
38
|
-
return void 0;
|
|
39
|
-
}
|
|
40
|
-
if (prop.model) {
|
|
41
|
-
return prop.model.fromXML({ elements });
|
|
42
|
-
}
|
|
43
|
-
const type = context.property.reflected.type;
|
|
44
|
-
if (type.is("class")) {
|
|
45
|
-
const model = getModel(type.class);
|
|
46
|
-
return model.fromXML({ elements: context.elements });
|
|
47
|
-
} else if (type.is("array")) {
|
|
48
|
-
let arrayEl = {};
|
|
49
|
-
if (!prop.inline && elements.length === 1 && elements[0].name === prop.tagname) {
|
|
50
|
-
arrayEl = elements[0];
|
|
51
|
-
} else if (prop.inline) {
|
|
52
|
-
arrayEl = { elements };
|
|
53
|
-
}
|
|
54
|
-
const els = arrayEl.elements || [];
|
|
55
|
-
const elType = type.elementType;
|
|
56
|
-
if (elType.is("class")) {
|
|
57
|
-
const model = getModel(elType.class);
|
|
58
|
-
const xmlInstances = els.map((el) => ({
|
|
59
|
-
elements: [el]
|
|
60
|
-
}));
|
|
61
|
-
return xmlInstances.map((xml) => model.fromXML(xml));
|
|
62
|
-
}
|
|
63
|
-
} else if (type.is("union") && type.types.length && type.types[0].is("literal")) {
|
|
64
|
-
const firstType = type.types[0];
|
|
65
|
-
if (firstType.is("literal")) {
|
|
66
|
-
const firstTypeCtor = firstType.value.constructor;
|
|
67
|
-
if (type.types.every((type2) => type2.is("literal") && type2.value.constructor === firstTypeCtor)) {
|
|
68
|
-
const model = getModel(firstTypeCtor);
|
|
69
|
-
return model.fromXML({ elements });
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return void 0;
|
|
74
|
-
},
|
|
75
|
-
/**
|
|
76
|
-
* Default model-level `toXML` handler.
|
|
77
|
-
* Collects serialised property fragments and wraps them in a root element
|
|
78
|
-
* named after the model's tagname.
|
|
79
|
-
*/
|
|
80
|
-
toXML({ properties, model }) {
|
|
81
|
-
const elements = [];
|
|
82
|
-
model.resolveAllProperties().forEach((prop) => {
|
|
83
|
-
if (prop.name in properties && typeof prop.name !== "symbol") {
|
|
84
|
-
const _xml = properties[prop.name];
|
|
85
|
-
_xml.elements.forEach((el) => {
|
|
86
|
-
el.name = prop.tagname;
|
|
87
|
-
elements.push(el);
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
return {
|
|
92
|
-
elements: [
|
|
93
|
-
{
|
|
94
|
-
type: "element",
|
|
95
|
-
name: model.options.tagname,
|
|
96
|
-
elements
|
|
97
|
-
}
|
|
98
|
-
]
|
|
99
|
-
};
|
|
100
|
-
},
|
|
101
|
-
/**
|
|
102
|
-
* Derives the XML tag name for a model from its class name, converted to kebab-case.
|
|
103
|
-
* e.g. `MyClass` → `my-class`.
|
|
104
|
-
*/
|
|
105
|
-
tagnameFromModel(model) {
|
|
106
|
-
return kebabCase(model.type.name);
|
|
107
|
-
},
|
|
108
|
-
/**
|
|
109
|
-
* Derives the XML tag name for a property from its name, converted to kebab-case.
|
|
110
|
-
* e.g. `myProp` → `my-prop`.
|
|
111
|
-
*/
|
|
112
|
-
tagnameFromProperty(property) {
|
|
113
|
-
return kebabCase(String(property.name));
|
|
114
|
-
},
|
|
115
|
-
/**
|
|
116
|
-
* Converts a typed property value to an `XMLRoot` fragment.
|
|
117
|
-
* Handles class types, arrays of class types, and union-of-literal types.
|
|
118
|
-
* When the property has `inline: true`, array items are flattened into the parent element.
|
|
119
|
-
*/
|
|
120
|
-
propertyToXML(context) {
|
|
121
|
-
const property = context.property;
|
|
122
|
-
const type = property.reflected.type;
|
|
123
|
-
const value = context.value;
|
|
124
|
-
if (property.reflected.isOptional && typeof value === "undefined") {
|
|
125
|
-
return { elements: [] };
|
|
126
|
-
}
|
|
127
|
-
if (property.model) {
|
|
128
|
-
return property.model.toXML(value);
|
|
129
|
-
}
|
|
130
|
-
const getXML = () => {
|
|
131
|
-
if (type.is("class")) {
|
|
132
|
-
const model = getModel(type.class);
|
|
133
|
-
return model.toXML(value);
|
|
134
|
-
} else if (type.is("array")) {
|
|
135
|
-
const elementType = type.elementType;
|
|
136
|
-
if (elementType.is("class")) {
|
|
137
|
-
const model = getModel(elementType.class);
|
|
138
|
-
const elements = [];
|
|
139
|
-
value.forEach((el) => elements.push(...model.toXML(el).elements));
|
|
140
|
-
return { elements: [{ type: "element", name: "array", elements }] };
|
|
141
|
-
}
|
|
142
|
-
} else if (type.is("union") && type.types.length && type.types[0].is("literal")) {
|
|
143
|
-
const firstType = type.types[0];
|
|
144
|
-
if (firstType.is("literal")) {
|
|
145
|
-
const firstTypeCtor = firstType.value.constructor;
|
|
146
|
-
if (type.types.every(
|
|
147
|
-
(type2) => type2.is("literal") && type2.value.constructor === firstTypeCtor
|
|
148
|
-
)) {
|
|
149
|
-
const model = getModel(firstTypeCtor);
|
|
150
|
-
return model.toXML(context.value);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return { elements: [] };
|
|
155
|
-
};
|
|
156
|
-
const xml = getXML();
|
|
157
|
-
if (context.property.inline)
|
|
158
|
-
return { elements: xml.elements.map((el) => el.elements || []).flat() };
|
|
159
|
-
else return xml;
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
export {
|
|
163
|
-
defaults
|
|
164
|
-
};
|
|
165
|
-
//# sourceMappingURL=defaults.js.map
|
package/dist/errors.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { fromXMLContext, PropertyFromXMLContext, PropertyToXMLContext, toXMLContext } from './model/types';
|
|
2
|
-
/**
|
|
3
|
-
* Thrown when model-level XML → object conversion fails.
|
|
4
|
-
* Wraps the original error along with the conversion context.
|
|
5
|
-
*/
|
|
6
|
-
export declare class FromXMLConversionError<T> extends Error {
|
|
7
|
-
context: Omit<fromXMLContext<T>, "properties">;
|
|
8
|
-
error: unknown;
|
|
9
|
-
name: string;
|
|
10
|
-
constructor(context: Omit<fromXMLContext<T>, "properties">, error: unknown);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Thrown when a single property's XML → value conversion fails.
|
|
14
|
-
* Extends `FromXMLConversionError` with additional property context.
|
|
15
|
-
*/
|
|
16
|
-
export declare class PropertyFromXMLConversionError<T> extends FromXMLConversionError<T> {
|
|
17
|
-
propertyContext: PropertyFromXMLContext<T>;
|
|
18
|
-
name: string;
|
|
19
|
-
constructor(context: Omit<fromXMLContext<T>, "properties">, propertyContext: PropertyFromXMLContext<T>, error: unknown);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Thrown when model-level object → XML conversion fails.
|
|
23
|
-
* Wraps the original cause along with the conversion context.
|
|
24
|
-
*/
|
|
25
|
-
export declare class ToXMLConversionError<T> extends Error {
|
|
26
|
-
context: Omit<toXMLContext<T>, "properties">;
|
|
27
|
-
cause: unknown;
|
|
28
|
-
name: string;
|
|
29
|
-
constructor(context: Omit<toXMLContext<T>, "properties">, cause: unknown);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Thrown when a single property's value → XML conversion fails.
|
|
33
|
-
* Extends `ToXMLConversionError` with additional property context.
|
|
34
|
-
*/
|
|
35
|
-
export declare class PropertyToXMLConversionError<T> extends ToXMLConversionError<T> {
|
|
36
|
-
propertyContext: PropertyToXMLContext<T>;
|
|
37
|
-
name: string;
|
|
38
|
-
constructor(context: Omit<toXMLContext<T>, "properties">, propertyContext: PropertyToXMLContext<T>, cause: unknown);
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
class FromXMLConversionError extends Error {
|
|
2
|
-
constructor(context, error) {
|
|
3
|
-
const message = `[Model: ${context.model.type.name}] failed to convert from XML`;
|
|
4
|
-
super(message);
|
|
5
|
-
this.context = context;
|
|
6
|
-
this.error = error;
|
|
7
|
-
this.name = "FromXMLConversionError";
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
class PropertyFromXMLConversionError extends FromXMLConversionError {
|
|
11
|
-
constructor(context, propertyContext, error) {
|
|
12
|
-
super(context, error);
|
|
13
|
-
this.propertyContext = propertyContext;
|
|
14
|
-
this.name = "PropertyFromXMLConversionError";
|
|
15
|
-
this.message = `[Model: ${context.model.type.name}] failed to convert prop <${String(
|
|
16
|
-
propertyContext.property.name
|
|
17
|
-
)}> from XML`;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
class ToXMLConversionError extends Error {
|
|
21
|
-
constructor(context, cause) {
|
|
22
|
-
const message = `[Model: ${context.model.type.name}] failed to convert to XML`;
|
|
23
|
-
super(message);
|
|
24
|
-
this.context = context;
|
|
25
|
-
this.cause = cause;
|
|
26
|
-
this.name = "ToXMLConversionError";
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
class PropertyToXMLConversionError extends ToXMLConversionError {
|
|
30
|
-
constructor(context, propertyContext, cause) {
|
|
31
|
-
super(context, cause);
|
|
32
|
-
this.propertyContext = propertyContext;
|
|
33
|
-
this.name = "PropertyToXMLConversionError";
|
|
34
|
-
this.message = `[Model: ${context.model.type.name}] failed to convert prop <${String(
|
|
35
|
-
propertyContext.property.name
|
|
36
|
-
)}> to XML`;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
FromXMLConversionError,
|
|
41
|
-
PropertyFromXMLConversionError,
|
|
42
|
-
PropertyToXMLConversionError,
|
|
43
|
-
ToXMLConversionError
|
|
44
|
-
};
|
|
45
|
-
//# sourceMappingURL=errors.js.map
|
package/dist/middleware.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export type Middleware<C, T> = (context: C, next: () => T) => T;
|
|
2
|
-
interface ChainableOptions<C, T> {
|
|
3
|
-
parent: ChainableOptions<C, T> | null;
|
|
4
|
-
middlewares: Middleware<C, T>[];
|
|
5
|
-
}
|
|
6
|
-
export declare function MiddlewareChain<C, T>(options: ChainableOptions<C, T>): Generator<Middleware<C, T>, void, unknown>;
|
|
7
|
-
type MiddlewareChain<C, T> = Iterator<Middleware<C, T>>;
|
|
8
|
-
export declare function resolve<C, T>(middlewares: MiddlewareChain<C, T>, context: C): T;
|
|
9
|
-
export {};
|
|
10
|
-
//# sourceMappingURL=middleware.d.ts.map
|
package/dist/middleware.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
function* MiddlewareChain(options) {
|
|
2
|
-
do {
|
|
3
|
-
for (let index = options.middlewares.length - 1; index >= 0; index--) {
|
|
4
|
-
yield options.middlewares[index];
|
|
5
|
-
}
|
|
6
|
-
if (options.parent) options = options.parent;
|
|
7
|
-
else return;
|
|
8
|
-
} while (true);
|
|
9
|
-
}
|
|
10
|
-
function resolve(middlewares, context) {
|
|
11
|
-
const next = () => {
|
|
12
|
-
const { value: nextMiddleware, done } = middlewares.next();
|
|
13
|
-
if (done || !nextMiddleware) {
|
|
14
|
-
throw new Error("no more next middleware");
|
|
15
|
-
} else {
|
|
16
|
-
return nextMiddleware(context, next);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
return next();
|
|
20
|
-
}
|
|
21
|
-
export {
|
|
22
|
-
MiddlewareChain,
|
|
23
|
-
resolve
|
|
24
|
-
};
|
|
25
|
-
//# sourceMappingURL=middleware.js.map
|
package/dist/model/built-ins.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { getContent, fromContent } from "../xml/index.js";
|
|
2
|
-
function registerBuiltIns(create) {
|
|
3
|
-
create(String, {
|
|
4
|
-
toXML(ctx) {
|
|
5
|
-
return {
|
|
6
|
-
elements: [fromContent(ctx.object, "string")]
|
|
7
|
-
};
|
|
8
|
-
},
|
|
9
|
-
fromXML(ctx) {
|
|
10
|
-
return String(getContent(ctx.xml.elements[0]));
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
create(Number, {
|
|
14
|
-
toXML(ctx) {
|
|
15
|
-
return {
|
|
16
|
-
elements: [fromContent(String(ctx.object), "number")]
|
|
17
|
-
};
|
|
18
|
-
},
|
|
19
|
-
fromXML(ctx) {
|
|
20
|
-
return Number(getContent(ctx.xml.elements[0]));
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
create(Boolean, {
|
|
24
|
-
toXML(ctx) {
|
|
25
|
-
return {
|
|
26
|
-
elements: [
|
|
27
|
-
{
|
|
28
|
-
type: "element",
|
|
29
|
-
name: "boolean",
|
|
30
|
-
...fromContent(String(ctx.object))
|
|
31
|
-
}
|
|
32
|
-
]
|
|
33
|
-
};
|
|
34
|
-
},
|
|
35
|
-
fromXML(ctx) {
|
|
36
|
-
return Boolean(getContent(ctx.xml.elements[0]));
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
registerBuiltIns as default
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=built-ins.js.map
|