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
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
package/.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"type": "node",
|
|
6
|
+
"request": "launch",
|
|
7
|
+
"name": "Mocha All",
|
|
8
|
+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
|
|
9
|
+
"args": [
|
|
10
|
+
"-r",
|
|
11
|
+
"./register-ts-node",
|
|
12
|
+
"--timeout",
|
|
13
|
+
"999999",
|
|
14
|
+
"--colors",
|
|
15
|
+
"${workspaceFolder}/src/**/*.spec.ts"
|
|
16
|
+
],
|
|
17
|
+
"console": "integratedTerminal",
|
|
18
|
+
"internalConsoleOptions": "neverOpen"
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { XMLModelPropertyOptions } from "../model/types";
|
|
2
|
+
import type { XMLModelOptions, XMLModel } from "../model/types";
|
|
3
|
+
declare type defaults<T = any> = {
|
|
4
|
+
fromXML: Required<XMLModelOptions<T>>["fromXML"]["middlewares"][number];
|
|
5
|
+
propertySourceElementsFilter: XMLModelPropertyOptions<T>["isSourceElement"];
|
|
6
|
+
propertyResolveSourceElements: XMLModelPropertyOptions<T>["resolveElements"];
|
|
7
|
+
propertyFromXML: Required<XMLModelPropertyOptions<T>>["fromXML"];
|
|
8
|
+
toXML: Required<XMLModelOptions<T>>["toXML"]["middlewares"][number];
|
|
9
|
+
tagnameFromModel: (model: XMLModel) => string;
|
|
10
|
+
tagnameFromProperty: (property: XMLModelPropertyOptions<T>) => string;
|
|
11
|
+
propertyToXML: Required<XMLModelPropertyOptions<T>>["toXML"];
|
|
12
|
+
};
|
|
13
|
+
export declare const defaults: defaults;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.defaults = void 0;
|
|
7
|
+
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 => {
|
|
8
|
+
let t = __RΦ.t[id];
|
|
9
|
+
if (t === void 0)
|
|
10
|
+
return void 0;
|
|
11
|
+
if (t.RΦ) {
|
|
12
|
+
let r = t.RΦ;
|
|
13
|
+
delete t.RΦ;
|
|
14
|
+
__RΦ.r(t, r(t));
|
|
15
|
+
}
|
|
16
|
+
else if (t.LΦ) {
|
|
17
|
+
let l = t.LΦ();
|
|
18
|
+
delete t.LΦ;
|
|
19
|
+
__RΦ.t[id] = t = l;
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
}, t: {} };
|
|
23
|
+
const model_1 = require("../model");
|
|
24
|
+
const kebabCase_1 = __importDefault(require("lodash/kebabCase"));
|
|
25
|
+
exports.defaults = {
|
|
26
|
+
fromXML() {
|
|
27
|
+
throw new TypeError("you should define 'defaults.fromXML' yourself or provide a 'fromXML' function to @Model() decorator's options");
|
|
28
|
+
},
|
|
29
|
+
propertyResolveSourceElements(context) {
|
|
30
|
+
var _a;
|
|
31
|
+
// We assume context.xml.elements is a single tag containing all the props
|
|
32
|
+
// FIXME: is it safe ?
|
|
33
|
+
const innerElements = ((_a = context.xml.elements[0]) === null || _a === void 0 ? void 0 : _a.elements) || [];
|
|
34
|
+
return innerElements.filter((el) => context.property.isSourceElement(el, context));
|
|
35
|
+
},
|
|
36
|
+
propertySourceElementsFilter(element, context) {
|
|
37
|
+
return context.property.tagname == element.name;
|
|
38
|
+
},
|
|
39
|
+
propertyFromXML(context) {
|
|
40
|
+
// TODO: handle inline
|
|
41
|
+
const prop = context.property;
|
|
42
|
+
const elements = context.elements;
|
|
43
|
+
const type = context.property.reflected.type;
|
|
44
|
+
if (prop.reflected.isOptional && elements.length === 0) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (type.is("class")) {
|
|
48
|
+
const model = (0, model_1.getModel)(type.class);
|
|
49
|
+
return model.fromXML({ elements: context.elements });
|
|
50
|
+
}
|
|
51
|
+
else if (type.is("array")) {
|
|
52
|
+
let arrayEl = {};
|
|
53
|
+
if (!prop.inline &&
|
|
54
|
+
elements.length === 1 &&
|
|
55
|
+
elements[0].name === prop.tagname) {
|
|
56
|
+
// we assume our array is contained in a root tag
|
|
57
|
+
arrayEl = elements[0];
|
|
58
|
+
}
|
|
59
|
+
else if (prop.inline &&
|
|
60
|
+
elements.every((el) => el.name === prop.tagname)) {
|
|
61
|
+
// we assume our array is contained in xml.elements
|
|
62
|
+
arrayEl = { elements };
|
|
63
|
+
}
|
|
64
|
+
if (arrayEl.elements) {
|
|
65
|
+
const elType = type.elementType;
|
|
66
|
+
if (elType.is("class")) {
|
|
67
|
+
const model = (0, model_1.getModel)(elType.class);
|
|
68
|
+
const xmlInstances = arrayEl.elements.map((el) => ({
|
|
69
|
+
elements: [el],
|
|
70
|
+
}));
|
|
71
|
+
return xmlInstances.map((xml) => model.fromXML(xml));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (type.is("union") &&
|
|
76
|
+
type.types.length &&
|
|
77
|
+
type.types[0].is("literal")) {
|
|
78
|
+
const firstType = type.types[0];
|
|
79
|
+
if (firstType.is("literal")) {
|
|
80
|
+
const firstTypeCtor = firstType.value.constructor;
|
|
81
|
+
if (type.types.every((type) => type.is("literal") && type.value.constructor === firstTypeCtor)) {
|
|
82
|
+
// all elements of unions are litteral with same type
|
|
83
|
+
const model = (0, model_1.getModel)(firstTypeCtor);
|
|
84
|
+
return model.fromXML({ elements });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// TODO: should warn ???
|
|
89
|
+
return undefined;
|
|
90
|
+
},
|
|
91
|
+
/* Object -> XML */
|
|
92
|
+
toXML({ properties, model }) {
|
|
93
|
+
const elements = [];
|
|
94
|
+
model.options.properties.options.forEach((prop) => {
|
|
95
|
+
if (prop.name in properties && typeof prop.name !== "symbol") {
|
|
96
|
+
// FIXME: prop.name should never be a symbol anyway
|
|
97
|
+
const _xml = properties[prop.name];
|
|
98
|
+
// overwrite tagnames
|
|
99
|
+
_xml.elements.forEach((el) => {
|
|
100
|
+
(el.name = prop.tagname), // TODO: configurable ?
|
|
101
|
+
elements.push(el);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
elements: [
|
|
107
|
+
{
|
|
108
|
+
type: "element",
|
|
109
|
+
name: model.options.tagname,
|
|
110
|
+
elements,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
tagnameFromModel(model) {
|
|
116
|
+
return (0, kebabCase_1.default)(model.type.name);
|
|
117
|
+
},
|
|
118
|
+
tagnameFromProperty(property) {
|
|
119
|
+
return (0, kebabCase_1.default)(String(property.name));
|
|
120
|
+
},
|
|
121
|
+
propertyToXML(context) {
|
|
122
|
+
const property = context.property;
|
|
123
|
+
const type = property.reflected.type;
|
|
124
|
+
const value = context.value;
|
|
125
|
+
if (property.reflected.isOptional && typeof value === "undefined") {
|
|
126
|
+
return { elements: [] }; // FIXME should return unefined ???
|
|
127
|
+
}
|
|
128
|
+
const getXML = () => {
|
|
129
|
+
if (type.is("class")) {
|
|
130
|
+
const model = (0, model_1.getModel)(type.class);
|
|
131
|
+
return model.toXML(value);
|
|
132
|
+
}
|
|
133
|
+
else if (type.is("array") && type.elementType.is("class")) {
|
|
134
|
+
const elementType = type.elementType;
|
|
135
|
+
if (elementType.is("class")) {
|
|
136
|
+
const model = (0, model_1.getModel)(elementType.class);
|
|
137
|
+
const elements = [];
|
|
138
|
+
value.forEach((el) => elements.push(...model.toXML(el).elements));
|
|
139
|
+
return { elements: [{ type: "element", name: "array", elements }] };
|
|
140
|
+
}
|
|
141
|
+
// TODO: handle other types of array
|
|
142
|
+
}
|
|
143
|
+
else if (type.is("union") &&
|
|
144
|
+
type.types.length &&
|
|
145
|
+
type.types[0].is("literal")) {
|
|
146
|
+
const firstType = type.types[0];
|
|
147
|
+
if (firstType.is("literal")) {
|
|
148
|
+
const firstTypeCtor = firstType.value.constructor;
|
|
149
|
+
if (type.types.every((type) => type.is("literal") && type.value.constructor === firstTypeCtor)) {
|
|
150
|
+
// all elements of unions are litteral with same type
|
|
151
|
+
const model = (0, model_1.getModel)(firstTypeCtor);
|
|
152
|
+
return model.toXML(context.value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// TODO: should warn ???
|
|
157
|
+
return { elements: [] };
|
|
158
|
+
};
|
|
159
|
+
const xml = getXML();
|
|
160
|
+
if (context.property.inline)
|
|
161
|
+
return { elements: xml.elements.map((el) => el.elements || []).flat() };
|
|
162
|
+
else
|
|
163
|
+
return xml;
|
|
164
|
+
},
|
|
165
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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 => {
|
|
4
|
+
let t = __RΦ.t[id];
|
|
5
|
+
if (t === void 0)
|
|
6
|
+
return void 0;
|
|
7
|
+
if (t.RΦ) {
|
|
8
|
+
let r = t.RΦ;
|
|
9
|
+
delete t.RΦ;
|
|
10
|
+
__RΦ.r(t, r(t));
|
|
11
|
+
}
|
|
12
|
+
else if (t.LΦ) {
|
|
13
|
+
let l = t.LΦ();
|
|
14
|
+
delete t.LΦ;
|
|
15
|
+
__RΦ.t[id] = t = l;
|
|
16
|
+
}
|
|
17
|
+
return t;
|
|
18
|
+
}, t: { [4]: { RΦ: t => ({ TΦ: "~" }) }, [1225]: { RΦ: t => ({ TΦ: "O", m: [{ n: "elements", f: "", t: __RΦ.a(1223) }] }) }, [1223]: { RΦ: t => ({ TΦ: "[", e: __RΦ.a(1044) }) }, [1044]: { RΦ: t => ({ TΦ: "O", m: [{ n: "type", f: "", t: __RΦ.a(1045) }, { n: "name", f: "", t: __RΦ.a(13) }, { n: "attributes", f: "?", t: __RΦ.a(1043) }, { n: "elements", f: "", t: __RΦ.a(1049) }] }) }, [1045]: { LΦ: t => "element" }, [13]: { LΦ: t => String }, [1043]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(10), __RΦ.a(1042)] }) }, [10]: { RΦ: t => ({ TΦ: "u" }) }, [1042]: { LΦ: t => require("xml-js/types").IΦAttributes }, [1049]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(166), __RΦ.a(1048)] }) }, [166]: { RΦ: t => ({ TΦ: "O", m: [] }) }, [1048]: { RΦ: t => ({ TΦ: "T", e: [{ t: __RΦ.a(1047) }] }) }, [1047]: { RΦ: t => ({ TΦ: "O", m: [{ n: "type", f: "", t: __RΦ.a(1024) }, { n: "text", f: "", t: __RΦ.a(13) }] }) }, [1024]: { LΦ: t => "text" }, [1246]: { RΦ: t => ({ TΦ: "O", m: [{ n: "elements", f: "", t: __RΦ.a(1223) }] }) }, [14]: { LΦ: t => Number }, [1272]: { RΦ: t => ({ TΦ: "O", m: [{ n: "elements", f: "", t: __RΦ.a(2988) }] }) }, [2988]: { RΦ: t => ({ TΦ: "[", e: __RΦ.a(2986) }) }, [2986]: { RΦ: t => ({ TΦ: "O", m: [{ n: "type", f: "", t: __RΦ.a(13) }, { n: "name", f: "", t: __RΦ.a(13) }] }) }, [20]: { LΦ: t => Boolean } } };
|
|
19
|
+
const model_1 = require("../model");
|
|
20
|
+
const xml_1 = require("../xml");
|
|
21
|
+
// string is <string>value</string>
|
|
22
|
+
(0, model_1.createModel)(String, {
|
|
23
|
+
toXML: __RΦ.f((ctx) => {
|
|
24
|
+
return {
|
|
25
|
+
elements: [(0, xml_1.fromContent)(ctx.object, "string")],
|
|
26
|
+
};
|
|
27
|
+
}, [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(1225))], "toXML"),
|
|
28
|
+
fromXML: __RΦ.f((ctx) => {
|
|
29
|
+
return String((0, xml_1.getContent)(ctx.xml.elements[0]));
|
|
30
|
+
}, [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(13))], "fromXML"),
|
|
31
|
+
});
|
|
32
|
+
// number is <number>value</number>
|
|
33
|
+
(0, model_1.createModel)(Number, {
|
|
34
|
+
toXML: __RΦ.f((ctx) => {
|
|
35
|
+
return {
|
|
36
|
+
elements: [(0, xml_1.fromContent)(String(ctx.object), "number")],
|
|
37
|
+
};
|
|
38
|
+
}, [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(1246))], "toXML"),
|
|
39
|
+
fromXML: __RΦ.f((ctx) => {
|
|
40
|
+
return Number((0, xml_1.getContent)(ctx.xml.elements[0]));
|
|
41
|
+
}, [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(14))], "fromXML"),
|
|
42
|
+
});
|
|
43
|
+
// number is <boolean>value</boolean>
|
|
44
|
+
(0, model_1.createModel)(Boolean, {
|
|
45
|
+
toXML: __RΦ.f((ctx) => {
|
|
46
|
+
return {
|
|
47
|
+
elements: [
|
|
48
|
+
Object.assign({ type: "element", name: "boolean" }, (0, xml_1.fromContent)(String(ctx.object))),
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
}, [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(1272))], "toXML"),
|
|
52
|
+
fromXML: __RΦ.f((ctx) => {
|
|
53
|
+
return Boolean((0, xml_1.getContent)(ctx.xml.elements[0]));
|
|
54
|
+
}, [__RΦ.m("rt:p", [{ n: "ctx", t: () => __RΦ.a(4), v: null }]), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(20))], "fromXML"),
|
|
55
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.XML = exports.Prop = exports.Model = exports.createModel = exports.getModel = exports.defaults = void 0;
|
|
7
|
+
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 => {
|
|
8
|
+
let t = __RΦ.t[id];
|
|
9
|
+
if (t === void 0)
|
|
10
|
+
return void 0;
|
|
11
|
+
if (t.RΦ) {
|
|
12
|
+
let r = t.RΦ;
|
|
13
|
+
delete t.RΦ;
|
|
14
|
+
__RΦ.r(t, r(t));
|
|
15
|
+
}
|
|
16
|
+
else if (t.LΦ) {
|
|
17
|
+
let l = t.LΦ();
|
|
18
|
+
delete t.LΦ;
|
|
19
|
+
__RΦ.t[id] = t = l;
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
}, t: {} };
|
|
23
|
+
var defaults_1 = require("./defaults");
|
|
24
|
+
Object.defineProperty(exports, "defaults", { enumerable: true, get: function () { return defaults_1.defaults; } });
|
|
25
|
+
var model_1 = require("./model");
|
|
26
|
+
Object.defineProperty(exports, "getModel", { enumerable: true, get: function () { return model_1.getModel; } });
|
|
27
|
+
Object.defineProperty(exports, "createModel", { enumerable: true, get: function () { return model_1.createModel; } });
|
|
28
|
+
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return model_1.Model; } });
|
|
29
|
+
Object.defineProperty(exports, "Prop", { enumerable: true, get: function () { return model_1.Prop; } });
|
|
30
|
+
const xml_1 = __importDefault(require("./xml"));
|
|
31
|
+
exports.XML = xml_1.default;
|
|
@@ -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,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolve = exports.MiddlewareChain = 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: { [102]: { RΦ: t => ({ TΦ: "|", t: [__RΦ.a(12), __RΦ.a(99)] }) }, [12]: { RΦ: t => ({ TΦ: "n" }) }, [99]: { TΦ: "5", name: "ChainableOptions" }, [105]: { RΦ: t => ({ TΦ: "[", e: __RΦ.a(104) }) }, [104]: { RΦ: t => ({ TΦ: "O", m: [{ n: "__call", f: "", t: __RΦ.a(4) }] }) }, [4]: { RΦ: t => ({ TΦ: "~" }) }, [108]: { RΦ: t => ({ TΦ: "g", t: __RΦ.a(99), p: [__RΦ.a(106), __RΦ.a(107)] }) }, [106]: { LΦ: t => Object }, [107]: { LΦ: t => Object }, [127]: { RΦ: t => ({ TΦ: "g", t: __RΦ.a(122), p: [__RΦ.a(109), __RΦ.a(22), __RΦ.a(8)] }) }, [122]: { LΦ: t => Object }, [109]: { RΦ: t => ({ TΦ: "O", m: [{ n: "__call", f: "", t: __RΦ.a(4) }] }) }, [22]: { RΦ: t => ({ TΦ: "V" }) }, [8]: { RΦ: t => ({ TΦ: "U" }) }, [139]: { RΦ: t => ({ TΦ: "g", t: __RΦ.a(131), p: [__RΦ.a(142), __RΦ.a(1), __RΦ.a(10)] }) }, [131]: { LΦ: t => Object }, [142]: { RΦ: t => ({ TΦ: "O", m: [{ n: "__call", f: "", t: __RΦ.a(4) }] }) }, [1]: { RΦ: t => ({ TΦ: "~" }) }, [10]: { RΦ: t => ({ TΦ: "u" }) }, [137]: { LΦ: t => Object }, [138]: { LΦ: t => Object } } };
|
|
20
|
+
var IΦChainableOptions = { name: "ChainableOptions", prototype: {}, identity: Symbol("ChainableOptions (interface)") };
|
|
21
|
+
(t => __RΦ.t[99] = t)(IΦChainableOptions);
|
|
22
|
+
__RΦ.m("rt:P", ["parent", "middlewares"])(IΦChainableOptions);
|
|
23
|
+
__RΦ.m("rt:m", [])(IΦChainableOptions);
|
|
24
|
+
__RΦ.m("rt:f", "I")(IΦChainableOptions);
|
|
25
|
+
__RΦ.m("rt:t", () => __RΦ.a(102))(IΦChainableOptions.prototype, "parent");
|
|
26
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "parent");
|
|
27
|
+
__RΦ.m("rt:t", () => __RΦ.a(105))(IΦChainableOptions.prototype, "middlewares");
|
|
28
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "middlewares");
|
|
29
|
+
__RΦ.m("rt:t", () => __RΦ.a(102))(IΦChainableOptions.prototype, "parent");
|
|
30
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "parent");
|
|
31
|
+
__RΦ.m("rt:t", () => __RΦ.a(105))(IΦChainableOptions.prototype, "middlewares");
|
|
32
|
+
__RΦ.m("rt:f", "P")(IΦChainableOptions.prototype, "middlewares");
|
|
33
|
+
function* MiddlewareChain(options) {
|
|
34
|
+
do {
|
|
35
|
+
for (let index = options.middlewares.length - 1; index >= 0; index--) {
|
|
36
|
+
yield options.middlewares[index];
|
|
37
|
+
}
|
|
38
|
+
if (options.parent)
|
|
39
|
+
options = options.parent;
|
|
40
|
+
else
|
|
41
|
+
return;
|
|
42
|
+
} while (true);
|
|
43
|
+
}
|
|
44
|
+
exports.MiddlewareChain = MiddlewareChain;
|
|
45
|
+
__RΦ.m("rt:p", [{ n: "options", t: () => __RΦ.a(108), v: null }])(MiddlewareChain);
|
|
46
|
+
__RΦ.m("rt:f", "F")(MiddlewareChain);
|
|
47
|
+
__RΦ.m("rt:t", () => __RΦ.a(127))(MiddlewareChain);
|
|
48
|
+
function resolve(middlewares, context) {
|
|
49
|
+
const next = __RΦ.f(() => {
|
|
50
|
+
const { value: nextMiddleware, done } = middlewares.next();
|
|
51
|
+
if (done || !nextMiddleware) {
|
|
52
|
+
// previous middlewares should have returned a value before
|
|
53
|
+
// TODO: dedicated error class
|
|
54
|
+
throw new Error("no more next middleware");
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return nextMiddleware(context, next);
|
|
58
|
+
}
|
|
59
|
+
}, [__RΦ.m("rt:p", []), __RΦ.m("rt:f", "F>"), __RΦ.m("rt:t", () => __RΦ.a(138))], "next");
|
|
60
|
+
return next();
|
|
61
|
+
}
|
|
62
|
+
exports.resolve = resolve;
|
|
63
|
+
__RΦ.m("rt:p", [{ n: "middlewares", t: () => __RΦ.a(139), v: null }, { n: "context", t: () => __RΦ.a(137), v: null }])(resolve);
|
|
64
|
+
__RΦ.m("rt:f", "F")(resolve);
|
|
65
|
+
__RΦ.m("rt:t", () => __RΦ.a(138))(resolve);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import type { Constructor } from "typescript-rtti";
|
|
3
|
+
import { XMLModelOptions } from "./types";
|
|
4
|
+
import { XMLRoot } from "../types";
|
|
5
|
+
export interface XMLModelConversionOptions<T> {
|
|
6
|
+
fromXML?: XMLModelOptions<T>["fromXML"]["middlewares"][number];
|
|
7
|
+
tagname?: string;
|
|
8
|
+
toXML?: XMLModelOptions<T>["toXML"]["middlewares"][number];
|
|
9
|
+
}
|
|
10
|
+
export declare class XMLModel<T = any> {
|
|
11
|
+
readonly type: Constructor<T>;
|
|
12
|
+
options: XMLModelOptions<T>;
|
|
13
|
+
constructor(type: Constructor<T>, options: XMLModelConversionOptions<T>);
|
|
14
|
+
fromXML(xml: XMLRoot | string): T;
|
|
15
|
+
toXML(instance: object): XMLRoot;
|
|
16
|
+
get reflectedClass(): import("typescript-rtti").ReflectedClass<Constructor<T>>;
|
|
17
|
+
}
|
|
18
|
+
export declare function createModel<T>(type: Constructor<T>, options: XMLModelConversionOptions<T>): XMLModel<T>;
|
|
19
|
+
declare type ModelID<T> = Constructor<T>;
|
|
20
|
+
export declare const Models: Map<ModelID<unknown>, XMLModel<unknown>>;
|
|
21
|
+
export declare function findModel<T>(id: ModelID<T>): XMLModel<T> | undefined;
|
|
22
|
+
export declare function getModel<T>(id: ModelID<T>): XMLModel<T>;
|
|
23
|
+
declare function ModelDecoratorFactory<T>(options?: XMLModelConversionOptions<T>): (constructor: Constructor<T>) => void;
|
|
24
|
+
export { ModelDecoratorFactory as Model };
|
|
25
|
+
export { Prop } from "./property";
|
|
26
|
+
import "../defaults/models";
|