xml-model 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.js +6 -0
- package/.gitignore +2 -0
- package/.vscode/launch.json +21 -0
- package/.vscode/settings.json +3 -0
- package/README.md +7 -0
- package/build/main/defaults/index.d.ts +14 -0
- package/build/main/defaults/index.js +165 -0
- package/build/main/defaults/models.d.ts +1 -0
- package/build/main/defaults/models.js +55 -0
- package/build/main/index.d.ts +5 -0
- package/build/main/index.js +31 -0
- package/build/main/middleware.d.ts +9 -0
- package/build/main/middleware.js +65 -0
- package/build/main/model/index.d.ts +26 -0
- package/build/main/model/index.js +304 -0
- package/build/main/model/property.d.ts +5 -0
- package/build/main/model/property.js +95 -0
- package/build/main/model/types.d.ts +69 -0
- package/build/main/model/types.js +214 -0
- package/build/main/model.spec.d.ts +1 -0
- package/build/main/model.spec.js +261 -0
- package/build/main/types.d.ts +8 -0
- package/build/main/types.js +18 -0
- package/build/main/xml.d.ts +31 -0
- package/build/main/xml.js +95 -0
- package/build/module/defaults/index.d.ts +13 -0
- package/build/module/defaults/index.js +71 -0
- package/build/module/defaults/models.d.ts +1 -0
- package/build/module/defaults/models.js +27 -0
- package/build/module/errors.d.ts +3 -0
- package/build/module/errors.js +37 -0
- package/build/module/index.d.ts +4 -0
- package/build/module/index.js +19 -0
- package/build/module/middleware.d.ts +9 -0
- package/build/module/middleware.js +60 -0
- package/build/module/model.d.ts +71 -0
- package/build/module/model.js +466 -0
- package/build/module/model.spec.d.ts +1 -0
- package/build/module/model.spec.js +76 -0
- package/build/module/types.d.ts +13 -0
- package/build/module/types.js +40 -0
- package/build/module/xml-from-object.d.ts +11 -0
- package/build/module/xml-from-object.js +58 -0
- package/build/module/xml-from-object.spec.d.ts +1 -0
- package/build/module/xml-from-object.spec.js +104 -0
- package/build/module/xml-to-object.d.ts +11 -0
- package/build/module/xml-to-object.js +55 -0
- package/build/module/xml.d.ts +15 -0
- package/build/module/xml.js +74 -0
- package/package.json +50 -0
- package/register-ts-node.js +9 -0
- package/src/defaults/index.ts +181 -0
- package/src/defaults/models.ts +45 -0
- package/src/index.ts +6 -0
- package/src/middleware.ts +34 -0
- package/src/model/index.ts +245 -0
- package/src/model/property.ts +104 -0
- package/src/model/types.ts +99 -0
- package/src/model.spec.ts +178 -0
- package/src/types.ts +8 -0
- package/src/xml.ts +80 -0
- package/tsconfig.json +106 -0
- package/tsconfig.module.json +9 -0
- package/yarn.lock +2217 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setAttribute = exports.addElement = exports.fromContent = exports.getContent = exports.stringify = exports.parse = void 0;
|
|
4
|
+
const __RΦ = { m: (k, v) => (t, ...a) => t && Reflect.metadata ? Reflect.metadata(k, v)(t, ...a) : void 0, f: (f, d, n) => (d.forEach(d => d(f)), Object.defineProperty(f, "name", { value: n, writable: false }), f), c: (c, d, dp, dsp, n) => (d.forEach(d => d(c)), dp.forEach(([p, d]) => d(c.prototype, p)), dsp.forEach(([p, d]) => d(c, p)), n ? Object.defineProperty(c, "name", { value: n, writable: false }) : undefined, c), r: (o, a) => (Object.assign(o, a)), a: id => {
|
|
5
|
+
let t = __RΦ.t[id];
|
|
6
|
+
if (t === void 0)
|
|
7
|
+
return void 0;
|
|
8
|
+
if (t.RΦ) {
|
|
9
|
+
let r = t.RΦ;
|
|
10
|
+
delete t.RΦ;
|
|
11
|
+
__RΦ.r(t, r(t));
|
|
12
|
+
}
|
|
13
|
+
else if (t.LΦ) {
|
|
14
|
+
let l = t.LΦ();
|
|
15
|
+
delete t.LΦ;
|
|
16
|
+
__RΦ.t[id] = t = l;
|
|
17
|
+
}
|
|
18
|
+
return t;
|
|
19
|
+
}, t: { [13]: { LΦ: t => String }, [93]: { RΦ: t => ({ TΦ: "O", m: [{ n: "elements", f: "", t: __RΦ.a(92) }] }) }, [92]: { RΦ: t => ({ TΦ: "[", e: __RΦ.a(91) }) }, [91]: { LΦ: t => require("xml-js/types").IΦElement }, [1002]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(10), __RΦ.a(999)] }) }, [10]: { RΦ: t => ({ TΦ: "u" }) }, [999]: { LΦ: t => require("xml-js/types").IΦJS2XML }, [1027]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(10), __RΦ.a(13), __RΦ.a(14), __RΦ.a(17), __RΦ.a(19)] }) }, [14]: { LΦ: t => Number }, [17]: { RΦ: t => ({ TΦ: "0" }) }, [19]: { RΦ: t => ({ TΦ: "1" }) }, [46]: { LΦ: t => "" }, [1043]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(10), __RΦ.a(1042)] }) }, [1042]: { LΦ: t => require("xml-js/types").IΦAttributes }, [22]: { RΦ: t => ({ TΦ: "V" }) } } };
|
|
20
|
+
const xml_js_1 = require("xml-js");
|
|
21
|
+
function parse(string) {
|
|
22
|
+
return (0, xml_js_1.xml2js)(string);
|
|
23
|
+
}
|
|
24
|
+
exports.parse = parse;
|
|
25
|
+
__RΦ.m("rt:p", [{ n: "string", t: () => __RΦ.a(13), v: null }])(parse);
|
|
26
|
+
__RΦ.m("rt:f", "F")(parse);
|
|
27
|
+
__RΦ.m("rt:t", () => __RΦ.a(93))(parse);
|
|
28
|
+
function stringify(xml, options) {
|
|
29
|
+
return (0, xml_js_1.js2xml)(xml, options);
|
|
30
|
+
}
|
|
31
|
+
exports.stringify = stringify;
|
|
32
|
+
__RΦ.m("rt:p", [{ n: "xml", t: () => __RΦ.a(93), v: null }, { n: "options", t: () => __RΦ.a(1002), v: null, f: "?" }])(stringify);
|
|
33
|
+
__RΦ.m("rt:f", "F")(stringify);
|
|
34
|
+
__RΦ.m("rt:t", () => __RΦ.a(13))(stringify);
|
|
35
|
+
function getContent(xml) {
|
|
36
|
+
var _a;
|
|
37
|
+
if (((_a = xml.elements) === null || _a === void 0 ? void 0 : _a.length) === 1) {
|
|
38
|
+
const content = xml.elements[0];
|
|
39
|
+
if (content.type === "text")
|
|
40
|
+
return content.text;
|
|
41
|
+
}
|
|
42
|
+
if (!xml.elements)
|
|
43
|
+
return "";
|
|
44
|
+
throw new TypeError(`can't get text from XMLElement: ${JSON.stringify(xml)}`);
|
|
45
|
+
}
|
|
46
|
+
exports.getContent = getContent;
|
|
47
|
+
__RΦ.m("rt:p", [{ n: "xml", t: () => __RΦ.a(91), v: null }])(getContent);
|
|
48
|
+
__RΦ.m("rt:f", "F")(getContent);
|
|
49
|
+
__RΦ.m("rt:t", () => __RΦ.a(1027))(getContent);
|
|
50
|
+
function fromContent(content = "", tag, attributes) {
|
|
51
|
+
const el = {
|
|
52
|
+
elements: content ? [{ type: "text", text: String(content) }] : [],
|
|
53
|
+
};
|
|
54
|
+
if (tag)
|
|
55
|
+
el.name = tag;
|
|
56
|
+
if (attributes) {
|
|
57
|
+
if (!el.name)
|
|
58
|
+
throw new TypeError("please provide a name if you want to provide attributes");
|
|
59
|
+
el.attributes = attributes;
|
|
60
|
+
}
|
|
61
|
+
if (el.name)
|
|
62
|
+
el.type = "element";
|
|
63
|
+
return el;
|
|
64
|
+
}
|
|
65
|
+
exports.fromContent = fromContent;
|
|
66
|
+
__RΦ.m("rt:p", [{ n: "content", t: () => __RΦ.a(46), v: function () { return ""; } }, { n: "tag", t: () => __RΦ.a(13), v: null, f: "?" }, { n: "attributes", t: () => __RΦ.a(1043), v: null, f: "?" }])(fromContent);
|
|
67
|
+
__RΦ.m("rt:f", "F")(fromContent);
|
|
68
|
+
__RΦ.m("rt:t", () => __RΦ.a(91))(fromContent);
|
|
69
|
+
function addElement(xml, element) {
|
|
70
|
+
if (!xml.elements)
|
|
71
|
+
xml.elements = [];
|
|
72
|
+
xml.elements.push(element);
|
|
73
|
+
}
|
|
74
|
+
exports.addElement = addElement;
|
|
75
|
+
__RΦ.m("rt:p", [{ n: "xml", t: () => __RΦ.a(91), v: null }, { n: "element", t: () => __RΦ.a(91), v: null }])(addElement);
|
|
76
|
+
__RΦ.m("rt:f", "F")(addElement);
|
|
77
|
+
__RΦ.m("rt:t", () => __RΦ.a(22))(addElement);
|
|
78
|
+
function setAttribute(xml, attribute, value) {
|
|
79
|
+
if (!xml.attributes)
|
|
80
|
+
xml.attributes = {};
|
|
81
|
+
xml.attributes[attribute] = value;
|
|
82
|
+
}
|
|
83
|
+
exports.setAttribute = setAttribute;
|
|
84
|
+
__RΦ.m("rt:p", [{ n: "xml", t: () => __RΦ.a(91), v: null }, { n: "attribute", t: () => __RΦ.a(13), v: null }, { n: "value", t: () => __RΦ.a(13), v: null }])(setAttribute);
|
|
85
|
+
__RΦ.m("rt:f", "F")(setAttribute);
|
|
86
|
+
__RΦ.m("rt:t", () => __RΦ.a(22))(setAttribute);
|
|
87
|
+
const XML = {
|
|
88
|
+
parse,
|
|
89
|
+
stringify,
|
|
90
|
+
fromContent,
|
|
91
|
+
getContent,
|
|
92
|
+
addElement,
|
|
93
|
+
setAttribute,
|
|
94
|
+
};
|
|
95
|
+
exports.default = XML;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PropDecoratorOptions, TagnameFromPropertyResolver } from "../model";
|
|
2
|
+
import type { InstantiateHandler } from "../model";
|
|
3
|
+
import { ClassType } from "../types";
|
|
4
|
+
declare type defaults = {
|
|
5
|
+
instantiate: InstantiateHandler<unknown>;
|
|
6
|
+
tagnameFromClass: (ctx: {
|
|
7
|
+
class: ClassType;
|
|
8
|
+
}) => string;
|
|
9
|
+
tagnameFromProperty: TagnameFromPropertyResolver;
|
|
10
|
+
handleProperty: Required<PropDecoratorOptions>["handleProperty"];
|
|
11
|
+
};
|
|
12
|
+
export declare const defaults: defaults;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const __RΦ = { m: (k, v) => (t, ...a) => t && Reflect.metadata ? Reflect.metadata(k, v)(t, ...a) : void 0, f: (f, d, n) => (d.forEach(d => d(f)), Object.defineProperty(f, "name", { value: n, writable: false }), f), c: (c, d, dp, dsp, n) => (d.forEach(d => d(c)), dp.forEach(([p, d]) => d(c.prototype, p)), dsp.forEach(([p, d]) => d(c, p)), n ? Object.defineProperty(c, "name", { value: n, writable: false }) : undefined, c), r: (o, a) => (Object.assign(o, a)), a: id => {
|
|
2
|
+
let t = __RΦ.t[id];
|
|
3
|
+
if (t === void 0)
|
|
4
|
+
return void 0;
|
|
5
|
+
if (t.RΦ) {
|
|
6
|
+
let r = t.RΦ;
|
|
7
|
+
delete t.RΦ;
|
|
8
|
+
__RΦ.r(t, r(t));
|
|
9
|
+
}
|
|
10
|
+
else if (t.LΦ) {
|
|
11
|
+
let l = t.LΦ();
|
|
12
|
+
delete t.LΦ;
|
|
13
|
+
__RΦ.t[id] = t = l;
|
|
14
|
+
}
|
|
15
|
+
return t;
|
|
16
|
+
}, t: { [833]: { RΦ: t => ({ TΦ: "&", t: [__RΦ.a(813), __RΦ.a(832)] }) }, [813]: { LΦ: t => LΦ_0.IΦXMLFromObjectPropHandlerContext }, [832]: { RΦ: t => ({ TΦ: "O", m: [{ n: "type", f: "", t: __RΦ.a(812) }] }) }, [812]: { LΦ: t => Object }, [22]: { RΦ: t => ({ TΦ: "V" }) }, [1044]: { RΦ: t => ({ TΦ: "&", t: [__RΦ.a(813), __RΦ.a(1043)] }) }, [1043]: { RΦ: t => ({ TΦ: "O", m: [{ n: "type", f: "", t: __RΦ.a(812) }] }) }, [4]: { RΦ: t => ({ TΦ: "~" }) } } };
|
|
17
|
+
import { addElement as addXMLElement } from "../xml";
|
|
18
|
+
import { findModel, } from "../model";
|
|
19
|
+
import kebabCase from "lodash/kebabCase";
|
|
20
|
+
import * as LΦ_0 from "../model";
|
|
21
|
+
function handleClassProperty(context) {
|
|
22
|
+
const propModel = findModel(context.type.class);
|
|
23
|
+
if (propModel) {
|
|
24
|
+
const el = propModel.toXMLElement(context.value);
|
|
25
|
+
if (context.tagname)
|
|
26
|
+
el.name = context.tagname;
|
|
27
|
+
addXMLElement(context.xml, el);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
__RΦ.m("rt:p", [{ n: "context", t: () => __RΦ.a(833), v: null }])(handleClassProperty);
|
|
31
|
+
__RΦ.m("rt:f", "F")(handleClassProperty);
|
|
32
|
+
__RΦ.m("rt:t", () => __RΦ.a(22))(handleClassProperty);
|
|
33
|
+
function handleClassArrayProperty(context) {
|
|
34
|
+
const elementModel = findModel(context.type.elementType.class);
|
|
35
|
+
if (elementModel) {
|
|
36
|
+
const xmlEl = {
|
|
37
|
+
type: "element",
|
|
38
|
+
elements: [],
|
|
39
|
+
};
|
|
40
|
+
context.value.forEach(__RΦ.f((arrayEl) => {
|
|
41
|
+
addXMLElement(xmlEl, elementModel.toXMLElement(arrayEl));
|
|
42
|
+
}, [__RΦ.m("rt:p", [{ n: "arrayEl", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(22))], ""));
|
|
43
|
+
if (context.tagname)
|
|
44
|
+
xmlEl.name = context.tagname;
|
|
45
|
+
addXMLElement(context.xml, xmlEl);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
__RΦ.m("rt:p", [{ n: "context", t: () => __RΦ.a(1044), v: null }])(handleClassArrayProperty);
|
|
49
|
+
__RΦ.m("rt:f", "F")(handleClassArrayProperty);
|
|
50
|
+
__RΦ.m("rt:t", () => __RΦ.a(22))(handleClassArrayProperty);
|
|
51
|
+
export const defaults = {
|
|
52
|
+
instantiate() {
|
|
53
|
+
throw new Error("you should define defaults.instantiate yourself or provide an instantiate function to @Model() decorator's options");
|
|
54
|
+
},
|
|
55
|
+
tagnameFromClass(ctx) {
|
|
56
|
+
return kebabCase(ctx.class.name);
|
|
57
|
+
},
|
|
58
|
+
tagnameFromProperty(context) {
|
|
59
|
+
return kebabCase(context.property);
|
|
60
|
+
},
|
|
61
|
+
/* Handle Property */
|
|
62
|
+
handleProperty(context) {
|
|
63
|
+
const type = context.type;
|
|
64
|
+
if (type.is("class")) {
|
|
65
|
+
handleClassProperty(context);
|
|
66
|
+
}
|
|
67
|
+
else if (type.is("array") && type.elementType.is("class")) {
|
|
68
|
+
handleClassArrayProperty(context);
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const __RΦ = { m: (k, v) => (t, ...a) => t && Reflect.metadata ? Reflect.metadata(k, v)(t, ...a) : void 0, f: (f, d, n) => (d.forEach(d => d(f)), Object.defineProperty(f, "name", { value: n, writable: false }), f), c: (c, d, dp, dsp, n) => (d.forEach(d => d(c)), dp.forEach(([p, d]) => d(c.prototype, p)), dsp.forEach(([p, d]) => d(c, p)), n ? Object.defineProperty(c, "name", { value: n, writable: false }) : undefined, c), r: (o, a) => (Object.assign(o, a)), a: id => {
|
|
2
|
+
let t = __RΦ.t[id];
|
|
3
|
+
if (t === void 0)
|
|
4
|
+
return void 0;
|
|
5
|
+
if (t.RΦ) {
|
|
6
|
+
let r = t.RΦ;
|
|
7
|
+
delete t.RΦ;
|
|
8
|
+
__RΦ.r(t, r(t));
|
|
9
|
+
}
|
|
10
|
+
else if (t.LΦ) {
|
|
11
|
+
let l = t.LΦ();
|
|
12
|
+
delete t.LΦ;
|
|
13
|
+
__RΦ.t[id] = t = l;
|
|
14
|
+
}
|
|
15
|
+
return t;
|
|
16
|
+
}, t: { [4]: { RΦ: t => ({ TΦ: "~" }) }, [384]: { LΦ: t => LΦ_0.IΦElement }, [13]: { LΦ: t => String }, [14]: { LΦ: t => Number } } };
|
|
17
|
+
import { createModel } from "../model";
|
|
18
|
+
import { getContent as getXMLContent, fromContent as xmlFromContent, } from "../xml";
|
|
19
|
+
import * as LΦ_0 from "xml-js/types";
|
|
20
|
+
createModel(String, {
|
|
21
|
+
toXML: __RΦ.f((ctx) => (ctx.xml = xmlFromContent(ctx.object)), [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(384))], "toXML"),
|
|
22
|
+
instantiate: __RΦ.f((ctx) => String(getXMLContent(ctx.xml)), [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(13))], "instantiate"),
|
|
23
|
+
});
|
|
24
|
+
createModel(Number, {
|
|
25
|
+
toXML: __RΦ.f((ctx) => (ctx.xml = xmlFromContent(String(ctx.object))), [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(384))], "toXML"),
|
|
26
|
+
instantiate: __RΦ.f((ctx) => Number(getXMLContent(ctx.xml)), [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(14))], "instantiate"),
|
|
27
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const __RΦ = { m: (k, v) => (t, ...a) => t && Reflect.metadata ? Reflect.metadata(k, v)(t, ...a) : void 0, f: (f, d, n) => (d.forEach(d => d(f)), Object.defineProperty(f, "name", { value: n, writable: false }), f), c: (c, d, dp, dsp, n) => (d.forEach(d => d(c)), dp.forEach(([p, d]) => d(c.prototype, p)), dsp.forEach(([p, d]) => d(c, p)), n ? Object.defineProperty(c, "name", { value: n, writable: false }) : undefined, c), r: (o, a) => (Object.assign(o, a)), a: id => {
|
|
2
|
+
let t = __RΦ.t[id];
|
|
3
|
+
if (t === void 0)
|
|
4
|
+
return void 0;
|
|
5
|
+
if (t.RΦ) {
|
|
6
|
+
let r = t.RΦ;
|
|
7
|
+
delete t.RΦ;
|
|
8
|
+
__RΦ.r(t, r(t));
|
|
9
|
+
}
|
|
10
|
+
else if (t.LΦ) {
|
|
11
|
+
let l = t.LΦ();
|
|
12
|
+
delete t.LΦ;
|
|
13
|
+
__RΦ.t[id] = t = l;
|
|
14
|
+
}
|
|
15
|
+
return t;
|
|
16
|
+
}, t: { [13]: { LΦ: t => String }, [14]: { LΦ: t => Number } } };
|
|
17
|
+
export class NoExplicitTypeError extends Error {
|
|
18
|
+
constructor(typeName, propertyKey, parameterIndex, argName) {
|
|
19
|
+
let errorMessage = `Unable to infer XMLModel type from TypeScript reflection system. You need to provide explicit type for `;
|
|
20
|
+
if (argName) {
|
|
21
|
+
errorMessage += `argument named '${argName}' of `;
|
|
22
|
+
}
|
|
23
|
+
else if (parameterIndex !== undefined) {
|
|
24
|
+
errorMessage += `parameter #${parameterIndex} of `;
|
|
25
|
+
}
|
|
26
|
+
errorMessage += `'${propertyKey}' of '${typeName}' class.`;
|
|
27
|
+
super(errorMessage);
|
|
28
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
(t => __RΦ.t[85] = t)(NoExplicitTypeError);
|
|
32
|
+
__RΦ.m("rt:SP", [])(NoExplicitTypeError);
|
|
33
|
+
__RΦ.m("rt:P", [])(NoExplicitTypeError);
|
|
34
|
+
__RΦ.m("rt:Sm", [])(NoExplicitTypeError);
|
|
35
|
+
__RΦ.m("rt:m", [])(NoExplicitTypeError);
|
|
36
|
+
__RΦ.m("rt:p", [{ n: "typeName", t: () => __RΦ.a(13), v: null }, { n: "propertyKey", t: () => __RΦ.a(13), v: null }, { n: "parameterIndex", t: () => __RΦ.a(14), v: null, f: "?" }, { n: "argName", t: () => __RΦ.a(13), v: null, f: "?" }])(NoExplicitTypeError);
|
|
37
|
+
__RΦ.m("rt:f", "Ce")(NoExplicitTypeError);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const __RΦ = { m: (k, v) => (t, ...a) => t && Reflect.metadata ? Reflect.metadata(k, v)(t, ...a) : void 0, f: (f, d, n) => (d.forEach(d => d(f)), Object.defineProperty(f, "name", { value: n, writable: false }), f), c: (c, d, dp, dsp, n) => (d.forEach(d => d(c)), dp.forEach(([p, d]) => d(c.prototype, p)), dsp.forEach(([p, d]) => d(c, p)), n ? Object.defineProperty(c, "name", { value: n, writable: false }) : undefined, c), r: (o, a) => (Object.assign(o, a)), a: id => {
|
|
2
|
+
let t = __RΦ.t[id];
|
|
3
|
+
if (t === void 0)
|
|
4
|
+
return void 0;
|
|
5
|
+
if (t.RΦ) {
|
|
6
|
+
let r = t.RΦ;
|
|
7
|
+
delete t.RΦ;
|
|
8
|
+
__RΦ.r(t, r(t));
|
|
9
|
+
}
|
|
10
|
+
else if (t.LΦ) {
|
|
11
|
+
let l = t.LΦ();
|
|
12
|
+
delete t.LΦ;
|
|
13
|
+
__RΦ.t[id] = t = l;
|
|
14
|
+
}
|
|
15
|
+
return t;
|
|
16
|
+
}, t: {} };
|
|
17
|
+
export { getModel, createModel, Model, Prop } from "./model";
|
|
18
|
+
import XML from "./xml";
|
|
19
|
+
export { XML };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare 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
|
+
declare type MiddlewareChain<C, T> = Iterator<Middleware<C, T>>;
|
|
8
|
+
export declare function resolve<C, T>(middlewares: MiddlewareChain<C, T>, context: C): T;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const __RΦ = { m: (k, v) => (t, ...a) => t && Reflect.metadata ? Reflect.metadata(k, v)(t, ...a) : void 0, f: (f, d, n) => (d.forEach(d => d(f)), Object.defineProperty(f, "name", { value: n, writable: false }), f), c: (c, d, dp, dsp, n) => (d.forEach(d => d(c)), dp.forEach(([p, d]) => d(c.prototype, p)), dsp.forEach(([p, d]) => d(c, p)), n ? Object.defineProperty(c, "name", { value: n, writable: false }) : undefined, c), r: (o, a) => (Object.assign(o, a)), a: id => {
|
|
2
|
+
let t = __RΦ.t[id];
|
|
3
|
+
if (t === void 0)
|
|
4
|
+
return void 0;
|
|
5
|
+
if (t.RΦ) {
|
|
6
|
+
let r = t.RΦ;
|
|
7
|
+
delete t.RΦ;
|
|
8
|
+
__RΦ.r(t, r(t));
|
|
9
|
+
}
|
|
10
|
+
else if (t.LΦ) {
|
|
11
|
+
let l = t.LΦ();
|
|
12
|
+
delete t.LΦ;
|
|
13
|
+
__RΦ.t[id] = t = l;
|
|
14
|
+
}
|
|
15
|
+
return t;
|
|
16
|
+
}, t: { [311]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(12), __RΦ.a(308)] }) }, [12]: { RΦ: t => ({ TΦ: "n" }) }, [308]: { TΦ: "5", name: "ChainableOptions" }, [314]: { RΦ: t => ({ TΦ: "[", e: __RΦ.a(313) }) }, [313]: { RΦ: t => ({ TΦ: "O", m: [{ n: "__call", f: "", t: __RΦ.a(4) }] }) }, [4]: { RΦ: t => ({ TΦ: "~" }) }, [317]: { RΦ: t => ({ TΦ: "g", t: __RΦ.a(308), p: [__RΦ.a(315), __RΦ.a(316)] }) }, [315]: { LΦ: t => Object }, [316]: { LΦ: t => Object }, [331]: { RΦ: t => ({ TΦ: "g", t: __RΦ.a(326), p: [__RΦ.a(318), __RΦ.a(22), __RΦ.a(8)] }) }, [326]: { LΦ: t => Object }, [318]: { RΦ: t => ({ TΦ: "O", m: [{ n: "__call", f: "", t: __RΦ.a(4) }] }) }, [22]: { RΦ: t => ({ TΦ: "V" }) }, [8]: { RΦ: t => ({ TΦ: "U" }) }, [343]: { RΦ: t => ({ TΦ: "g", t: __RΦ.a(335), p: [__RΦ.a(346), __RΦ.a(1), __RΦ.a(10)] }) }, [335]: { LΦ: t => Object }, [346]: { RΦ: t => ({ TΦ: "O", m: [{ n: "__call", f: "", t: __RΦ.a(4) }] }) }, [1]: { RΦ: t => ({ TΦ: "~" }) }, [10]: { RΦ: t => ({ TΦ: "u" }) }, [341]: { LΦ: t => Object }, [342]: { LΦ: t => Object } } };
|
|
17
|
+
var IΦChainableOptions = { name: "ChainableOptions", prototype: {}, identity: Symbol("ChainableOptions (interface)") };
|
|
18
|
+
(t => __RΦ.t[308] = t)(IΦChainableOptions);
|
|
19
|
+
__RΦ.m("rt:P", ["parent", "middlewares"])(IΦChainableOptions);
|
|
20
|
+
__RΦ.m("rt:m", [])(IΦChainableOptions);
|
|
21
|
+
__RΦ.m("rt:f", "I")(IΦChainableOptions);
|
|
22
|
+
__RΦ.m("rt:t", () => __RΦ.a(311))(IΦChainableOptions.prototype, "parent");
|
|
23
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "parent");
|
|
24
|
+
__RΦ.m("rt:t", () => __RΦ.a(314))(IΦChainableOptions.prototype, "middlewares");
|
|
25
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "middlewares");
|
|
26
|
+
__RΦ.m("rt:t", () => __RΦ.a(311))(IΦChainableOptions.prototype, "parent");
|
|
27
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "parent");
|
|
28
|
+
__RΦ.m("rt:t", () => __RΦ.a(314))(IΦChainableOptions.prototype, "middlewares");
|
|
29
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "middlewares");
|
|
30
|
+
export function* MiddlewareChain(options) {
|
|
31
|
+
do {
|
|
32
|
+
for (let index = options.middlewares.length - 1; index >= 0; index--) {
|
|
33
|
+
yield options.middlewares[index];
|
|
34
|
+
}
|
|
35
|
+
if (options.parent)
|
|
36
|
+
options = options.parent;
|
|
37
|
+
else
|
|
38
|
+
return;
|
|
39
|
+
} while (true);
|
|
40
|
+
}
|
|
41
|
+
__RΦ.m("rt:p", [{ n: "options", t: () => __RΦ.a(317), v: null }])(MiddlewareChain);
|
|
42
|
+
__RΦ.m("rt:f", "F")(MiddlewareChain);
|
|
43
|
+
__RΦ.m("rt:t", () => __RΦ.a(331))(MiddlewareChain);
|
|
44
|
+
export function resolve(middlewares, context) {
|
|
45
|
+
const next = __RΦ.f(() => {
|
|
46
|
+
const { value: nextMiddleware, done } = middlewares.next();
|
|
47
|
+
if (done || !nextMiddleware) {
|
|
48
|
+
// previous middlewares should have returned a value before
|
|
49
|
+
// TODO: dedicated error class
|
|
50
|
+
throw new Error("no more next middleware");
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return nextMiddleware(context, next);
|
|
54
|
+
}
|
|
55
|
+
}, [__RΦ.m("rt:p", []), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(342))], "next");
|
|
56
|
+
return next();
|
|
57
|
+
}
|
|
58
|
+
__RΦ.m("rt:p", [{ n: "middlewares", t: () => __RΦ.a(343), v: null }, { n: "context", t: () => __RΦ.a(341), v: null }])(resolve);
|
|
59
|
+
__RΦ.m("rt:f", "F")(resolve);
|
|
60
|
+
__RΦ.m("rt:t", () => __RΦ.a(342))(resolve);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { ReflectedTypeRef } from "typescript-rtti";
|
|
3
|
+
import { XMLToObjectOptions, XMLToObjectMiddlewareContext } from "./xml-to-object";
|
|
4
|
+
import { XMLFromObjectOptions, XMLFromObjectMiddlewareContext } from "./xml-from-object";
|
|
5
|
+
import { ClassType, XMLElement } from "./types";
|
|
6
|
+
declare type ModelProperty = string;
|
|
7
|
+
interface XMLFromObjectPropHandlerContext extends XMLFromObjectMiddlewareContext {
|
|
8
|
+
property: ModelProperty;
|
|
9
|
+
value: unknown;
|
|
10
|
+
type: ReflectedTypeRef;
|
|
11
|
+
tagname: string | null;
|
|
12
|
+
}
|
|
13
|
+
interface XMLToObjectPropHandlerContext extends XMLToObjectMiddlewareContext {
|
|
14
|
+
element: XMLElement;
|
|
15
|
+
}
|
|
16
|
+
export interface PropertyConversionOptions {
|
|
17
|
+
property: ModelProperty;
|
|
18
|
+
type: ReflectedTypeRef;
|
|
19
|
+
forElement: (el: XMLElement) => boolean;
|
|
20
|
+
handleElement: (context: XMLToObjectPropHandlerContext) => void;
|
|
21
|
+
handleProperty: (context: XMLFromObjectMiddlewareContext) => void;
|
|
22
|
+
}
|
|
23
|
+
export interface XMLToObjectInstantiateContext<T> extends XMLToObjectMiddlewareContext {
|
|
24
|
+
class: ClassType;
|
|
25
|
+
}
|
|
26
|
+
export declare type InstantiateHandler<T> = (ctx: XMLToObjectInstantiateContext<T>) => T;
|
|
27
|
+
export interface ObjectConversionOptions {
|
|
28
|
+
fromXML?: XMLToObjectOptions["middlewares"][number];
|
|
29
|
+
instantiate?: InstantiateHandler<unknown>;
|
|
30
|
+
tagname?: string;
|
|
31
|
+
toXML?: XMLFromObjectOptions["middlewares"][number];
|
|
32
|
+
}
|
|
33
|
+
interface ConversionOptions {
|
|
34
|
+
fromXML: XMLToObjectOptions;
|
|
35
|
+
toXML: XMLFromObjectOptions;
|
|
36
|
+
}
|
|
37
|
+
export declare class XMLModel {
|
|
38
|
+
readonly type: ClassType;
|
|
39
|
+
readonly options: ObjectConversionOptions;
|
|
40
|
+
conversionOptions?: ConversionOptions;
|
|
41
|
+
constructor(type: ClassType, options: ObjectConversionOptions);
|
|
42
|
+
private resolveConversionOptions;
|
|
43
|
+
private getConversionOptions;
|
|
44
|
+
recordFromXMLElement(xml: XMLElement): import("./types").UnknownRecord;
|
|
45
|
+
fromXMLElement(xml: XMLElement): unknown;
|
|
46
|
+
toXMLElement(instance: any): XMLElement;
|
|
47
|
+
}
|
|
48
|
+
export declare function createModel(...args: ConstructorParameters<typeof XMLModel>): XMLModel;
|
|
49
|
+
export declare const Models: Map<ClassType<any>, XMLModel>;
|
|
50
|
+
export declare function findModel(type: ClassType): XMLModel | undefined;
|
|
51
|
+
export declare function getModel(type: ClassType): XMLModel;
|
|
52
|
+
declare function ModelDecoratorFactory(options?: ObjectConversionOptions): (constructor: ClassType) => void;
|
|
53
|
+
export { ModelDecoratorFactory as Model };
|
|
54
|
+
export declare type TagnameFromPropertyResolver = (context: {
|
|
55
|
+
property: ModelProperty;
|
|
56
|
+
type: ReflectedTypeRef;
|
|
57
|
+
value: unknown;
|
|
58
|
+
}) => string | null;
|
|
59
|
+
export interface PropDecoratorOptions {
|
|
60
|
+
tagname?: string;
|
|
61
|
+
fromElement?: string | RegExp | ((element: XMLElement) => boolean);
|
|
62
|
+
toTagname?: TagnameFromPropertyResolver | ReturnType<TagnameFromPropertyResolver>;
|
|
63
|
+
toXML?: (context: XMLFromObjectPropHandlerContext) => XMLElement;
|
|
64
|
+
handleProperty?: (context: XMLFromObjectPropHandlerContext) => void;
|
|
65
|
+
fromXML?: (context: XMLToObjectPropHandlerContext) => unknown;
|
|
66
|
+
handleElement?: (context: XMLToObjectPropHandlerContext) => void;
|
|
67
|
+
ignore?: boolean;
|
|
68
|
+
}
|
|
69
|
+
declare function PropDecoratorFactory(options?: PropDecoratorOptions): (prototype: any, property: ModelProperty) => void;
|
|
70
|
+
export { PropDecoratorFactory as Prop };
|
|
71
|
+
import "./defaults/models";
|