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/model/index.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { Constructor } from 'typescript-rtti';
|
|
2
|
-
import { XMLModelOptions, XMLModelPropertyOptions, CreateXMLModelOptions } from './types';
|
|
3
|
-
import { XMLRoot } from '../types';
|
|
4
|
-
/**
|
|
5
|
-
* Returns the parent `XMLModel` for the given model, walking the prototype chain
|
|
6
|
-
* if no explicit parent was set in options.
|
|
7
|
-
*/
|
|
8
|
-
export declare function getParentModel(model: XMLModel<any>): XMLModel<any>;
|
|
9
|
-
/**
|
|
10
|
-
* Encapsulates the XML ↔ TypeScript conversion logic for a specific class.
|
|
11
|
-
*
|
|
12
|
-
* Create instances via `createModel` or the `@Model()` decorator rather than
|
|
13
|
-
* calling this constructor directly.
|
|
14
|
-
*/
|
|
15
|
-
export declare class XMLModel<T = any> {
|
|
16
|
-
readonly type: Constructor<T>;
|
|
17
|
-
options: XMLModelOptions<T>;
|
|
18
|
-
constructor(type: Constructor<T>, options: CreateXMLModelOptions<T>);
|
|
19
|
-
/**
|
|
20
|
-
* Converts an XML document (string or parsed `XMLRoot`) into an instance of `T`.
|
|
21
|
-
*
|
|
22
|
-
* @param xml - Raw XML string or a pre-parsed `XMLRoot` object.
|
|
23
|
-
* @returns The converted instance produced by the model's `fromXML` middleware chain.
|
|
24
|
-
* @throws {FromXMLConversionError} When model-level conversion fails.
|
|
25
|
-
* @throws {PropertyFromXMLConversionError} When a property-level conversion fails.
|
|
26
|
-
*/
|
|
27
|
-
fromXML(xml: XMLRoot | string): T;
|
|
28
|
-
/**
|
|
29
|
-
* Converts an instance of `T` into an XML document.
|
|
30
|
-
*
|
|
31
|
-
* @param instance - An instance of the class this model was created for.
|
|
32
|
-
* @returns An `XMLRoot` representing the serialised object.
|
|
33
|
-
* @throws {TypeError} When `instance` is not an instance of the expected type.
|
|
34
|
-
* @throws {ToXMLConversionError} When model-level conversion fails.
|
|
35
|
-
* @throws {PropertyToXMLConversionError} When a property-level conversion fails.
|
|
36
|
-
*/
|
|
37
|
-
toXML(instance: unknown): XMLRoot;
|
|
38
|
-
/** The typescript-rtti reflection metadata for the model's class. */
|
|
39
|
-
get reflectedClass(): import('typescript-rtti').ReflectedClass<Constructor<T>>;
|
|
40
|
-
/**
|
|
41
|
-
* Returns a merged map of all property options for this model, including inherited properties.
|
|
42
|
-
* Own properties override parent properties with the same name.
|
|
43
|
-
*/
|
|
44
|
-
resolveAllProperties(): Map<string, XMLModelPropertyOptions<any> & {
|
|
45
|
-
model: any;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Creates and registers a new `XMLModel` for the given constructor.
|
|
50
|
-
*
|
|
51
|
-
* @param type - The class constructor to create a model for.
|
|
52
|
-
* @param options - Model creation options including `fromXML` and `toXML` middlewares.
|
|
53
|
-
* @returns The newly created `XMLModel`.
|
|
54
|
-
* @throws {TypeError} When a model for this type has already been registered.
|
|
55
|
-
*/
|
|
56
|
-
export declare function createModel<T>(type: Constructor<T>, options: CreateXMLModelOptions<T>): XMLModel<T>;
|
|
57
|
-
/**
|
|
58
|
-
* Decorator factory that registers an `XMLModel` for the decorated class.
|
|
59
|
-
*
|
|
60
|
-
* Provide at minimum a `fromXML` function unless the class inherits from a
|
|
61
|
-
* parent class that already has a model — the default `fromXML` throws.
|
|
62
|
-
*
|
|
63
|
-
* @param options - Optional model creation options.
|
|
64
|
-
*/
|
|
65
|
-
declare function ModelDecoratorFactory<T>(options?: CreateXMLModelOptions<T>): (constructor: Constructor<T>) => void;
|
|
66
|
-
export { getModel } from './registry';
|
|
67
|
-
export { ModelDecoratorFactory as Model };
|
|
68
|
-
export { Prop } from './property';
|
|
69
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/model/index.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import "../_virtual/Reflect.js";
|
|
2
|
-
import { reflect } from "typescript-rtti";
|
|
3
|
-
import { ToXMLConversionError, PropertyToXMLConversionError, FromXMLConversionError, PropertyFromXMLConversionError } from "../errors.js";
|
|
4
|
-
import mergeMaps from "../util/merge-maps.js";
|
|
5
|
-
import { resolve, MiddlewareChain } from "../middleware.js";
|
|
6
|
-
import { getPropertyConversionOptions } from "./property.js";
|
|
7
|
-
import { Prop } from "./property.js";
|
|
8
|
-
import XML from "../xml/index.js";
|
|
9
|
-
import { defaults } from "../defaults.js";
|
|
10
|
-
import { findModel, registrerModel } from "./registry.js";
|
|
11
|
-
import { getModel } from "./registry.js";
|
|
12
|
-
import registerBuiltIns from "./built-ins.js";
|
|
13
|
-
function* ParentChain(constructor) {
|
|
14
|
-
let parent = Object.getPrototypeOf(constructor);
|
|
15
|
-
if (parent === constructor) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
while (parent) {
|
|
19
|
-
yield parent;
|
|
20
|
-
const _parent = Object.getPrototypeOf(constructor);
|
|
21
|
-
if (parent === _parent) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
parent = _parent;
|
|
25
|
-
}
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
function getParentModel(model) {
|
|
29
|
-
if (model.options.parent) return model.options.parent;
|
|
30
|
-
for (const constructor of ParentChain(model.type)) {
|
|
31
|
-
const model2 = findModel(constructor);
|
|
32
|
-
if (model2) {
|
|
33
|
-
return model2;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
class XMLModel {
|
|
39
|
-
constructor(type, options) {
|
|
40
|
-
this.type = type;
|
|
41
|
-
const model = this;
|
|
42
|
-
let parent = void 0;
|
|
43
|
-
const getParent = () => {
|
|
44
|
-
if (typeof parent === "undefined") parent = getParentModel(this);
|
|
45
|
-
return parent;
|
|
46
|
-
};
|
|
47
|
-
let propertiesLoaded = false;
|
|
48
|
-
const properties = {
|
|
49
|
-
options: /* @__PURE__ */ new Map(),
|
|
50
|
-
fromXML: {
|
|
51
|
-
get parent() {
|
|
52
|
-
return getParent()?.options.properties.fromXML || null;
|
|
53
|
-
},
|
|
54
|
-
middlewares: [
|
|
55
|
-
(context, next) => {
|
|
56
|
-
const record = getParent() ? next() : {};
|
|
57
|
-
properties.options.forEach((property) => {
|
|
58
|
-
const xml = context.xml;
|
|
59
|
-
const elements = property.resolveElements({
|
|
60
|
-
model,
|
|
61
|
-
xml,
|
|
62
|
-
property
|
|
63
|
-
});
|
|
64
|
-
const propertyFromXMLContext = {
|
|
65
|
-
model,
|
|
66
|
-
xml: context.xml,
|
|
67
|
-
property,
|
|
68
|
-
elements
|
|
69
|
-
};
|
|
70
|
-
try {
|
|
71
|
-
record[property.name] = property.fromXML(propertyFromXMLContext);
|
|
72
|
-
} catch (error) {
|
|
73
|
-
if (error instanceof FromXMLConversionError) {
|
|
74
|
-
throw error;
|
|
75
|
-
} else {
|
|
76
|
-
throw new PropertyFromXMLConversionError(context, propertyFromXMLContext, error);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
return record;
|
|
81
|
-
}
|
|
82
|
-
]
|
|
83
|
-
},
|
|
84
|
-
toXML: {
|
|
85
|
-
get parent() {
|
|
86
|
-
return getParent()?.options.properties.toXML || null;
|
|
87
|
-
},
|
|
88
|
-
middlewares: [
|
|
89
|
-
(context, next) => {
|
|
90
|
-
const record = getParent() ? next() : {};
|
|
91
|
-
properties.options.forEach((options2) => {
|
|
92
|
-
const propertyToXMLContext = {
|
|
93
|
-
model,
|
|
94
|
-
object: context.object,
|
|
95
|
-
property: options2,
|
|
96
|
-
value: context.object[options2.name]
|
|
97
|
-
};
|
|
98
|
-
try {
|
|
99
|
-
record[options2.name] = options2.toXML(propertyToXMLContext);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
if (error instanceof ToXMLConversionError) {
|
|
102
|
-
throw error;
|
|
103
|
-
} else {
|
|
104
|
-
throw new PropertyToXMLConversionError(context, propertyToXMLContext, error);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
return record;
|
|
109
|
-
}
|
|
110
|
-
]
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
const loadProperties = () => {
|
|
114
|
-
const props = this.reflectedClass.ownProperties.filter(
|
|
115
|
-
(prop) => typeof prop.host.constructor.prototype[prop.name] !== "function"
|
|
116
|
-
);
|
|
117
|
-
props.forEach((property) => {
|
|
118
|
-
const options2 = getPropertyConversionOptions(
|
|
119
|
-
this.type,
|
|
120
|
-
property.name
|
|
121
|
-
);
|
|
122
|
-
if (!options2.ignored) {
|
|
123
|
-
const type2 = options2.reflected?.type;
|
|
124
|
-
if (!options2.model && type2?.is("class") && type2.class === Object) {
|
|
125
|
-
console.warn(
|
|
126
|
-
`[xml-model] Property '${String(property.name)}' on '${this.type.name}' has type Object at runtime. If its declared type is a class, make sure it is imported as a value and not with 'import type'.`
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
properties.options.set(property.name, options2);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
propertiesLoaded = true;
|
|
133
|
-
};
|
|
134
|
-
this.options = {
|
|
135
|
-
get properties() {
|
|
136
|
-
if (!propertiesLoaded) loadProperties();
|
|
137
|
-
return properties;
|
|
138
|
-
},
|
|
139
|
-
fromXML: {
|
|
140
|
-
middlewares: [],
|
|
141
|
-
get parent() {
|
|
142
|
-
return getParent()?.options.fromXML || null;
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
toXML: {
|
|
146
|
-
middlewares: [],
|
|
147
|
-
get parent() {
|
|
148
|
-
return getParent()?.options.toXML || null;
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
get tagname() {
|
|
152
|
-
return options.tagname || defaults.tagnameFromModel(model);
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
if (options.parent) this.options.parent = options.parent;
|
|
156
|
-
if (!getParent()) {
|
|
157
|
-
this.options.fromXML.middlewares.push((ctx) => defaults.fromXML(ctx));
|
|
158
|
-
this.options.toXML.middlewares.push((ctx) => defaults.toXML(ctx));
|
|
159
|
-
}
|
|
160
|
-
if (options.fromXML) this.options.fromXML.middlewares.push(options.fromXML);
|
|
161
|
-
if (options.toXML) this.options.toXML.middlewares.push(options.toXML);
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Converts an XML document (string or parsed `XMLRoot`) into an instance of `T`.
|
|
165
|
-
*
|
|
166
|
-
* @param xml - Raw XML string or a pre-parsed `XMLRoot` object.
|
|
167
|
-
* @returns The converted instance produced by the model's `fromXML` middleware chain.
|
|
168
|
-
* @throws {FromXMLConversionError} When model-level conversion fails.
|
|
169
|
-
* @throws {PropertyFromXMLConversionError} When a property-level conversion fails.
|
|
170
|
-
*/
|
|
171
|
-
fromXML(xml) {
|
|
172
|
-
const _xml = typeof xml === "string" ? XML.parse(xml) : xml;
|
|
173
|
-
const model = this;
|
|
174
|
-
const context = {
|
|
175
|
-
xml: _xml,
|
|
176
|
-
get properties() {
|
|
177
|
-
const propContext = {
|
|
178
|
-
xml: _xml,
|
|
179
|
-
model
|
|
180
|
-
};
|
|
181
|
-
return resolve(MiddlewareChain(model.options.properties.fromXML), propContext);
|
|
182
|
-
},
|
|
183
|
-
model
|
|
184
|
-
};
|
|
185
|
-
return resolve(MiddlewareChain(this.options.fromXML), context);
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Converts an instance of `T` into an XML document.
|
|
189
|
-
*
|
|
190
|
-
* @param instance - An instance of the class this model was created for.
|
|
191
|
-
* @returns An `XMLRoot` representing the serialised object.
|
|
192
|
-
* @throws {TypeError} When `instance` is not an instance of the expected type.
|
|
193
|
-
* @throws {ToXMLConversionError} When model-level conversion fails.
|
|
194
|
-
* @throws {PropertyToXMLConversionError} When a property-level conversion fails.
|
|
195
|
-
*/
|
|
196
|
-
toXML(instance) {
|
|
197
|
-
const model = this;
|
|
198
|
-
if (instance instanceof this.type || typeof instance !== "undefined" && instance.constructor === this.type) {
|
|
199
|
-
const context = {
|
|
200
|
-
object: instance,
|
|
201
|
-
get properties() {
|
|
202
|
-
const propContext = {
|
|
203
|
-
object: instance,
|
|
204
|
-
model
|
|
205
|
-
};
|
|
206
|
-
return resolve(MiddlewareChain(model.options.properties.toXML), propContext);
|
|
207
|
-
},
|
|
208
|
-
model: this
|
|
209
|
-
};
|
|
210
|
-
return resolve(MiddlewareChain(this.options.toXML), context);
|
|
211
|
-
} else {
|
|
212
|
-
throw new TypeError(`provided object is not an instance of ${this.type.name}`);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
/** The typescript-rtti reflection metadata for the model's class. */
|
|
216
|
-
get reflectedClass() {
|
|
217
|
-
return reflect(this.type);
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Returns a merged map of all property options for this model, including inherited properties.
|
|
221
|
-
* Own properties override parent properties with the same name.
|
|
222
|
-
*/
|
|
223
|
-
resolveAllProperties() {
|
|
224
|
-
const ownProperties = /* @__PURE__ */ new Map();
|
|
225
|
-
const parent = getParentModel(this);
|
|
226
|
-
this.options.properties.options.forEach((options, key) => {
|
|
227
|
-
ownProperties.set(
|
|
228
|
-
key,
|
|
229
|
-
new Proxy(options, {
|
|
230
|
-
get: (target, p, reciever) => {
|
|
231
|
-
if (p === "model") return this;
|
|
232
|
-
else return Reflect.get(target, p, reciever);
|
|
233
|
-
}
|
|
234
|
-
})
|
|
235
|
-
// FIXME: is typing ok ?
|
|
236
|
-
);
|
|
237
|
-
});
|
|
238
|
-
const res = parent ? mergeMaps(parent.resolveAllProperties(), ownProperties) : ownProperties;
|
|
239
|
-
return res;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
function createModel(type, options) {
|
|
243
|
-
if (findModel(type)) {
|
|
244
|
-
throw new TypeError(`a model for type ${type.name} already exists`);
|
|
245
|
-
}
|
|
246
|
-
const model = new XMLModel(type, options);
|
|
247
|
-
registrerModel(model);
|
|
248
|
-
return model;
|
|
249
|
-
}
|
|
250
|
-
function ModelDecoratorFactory(options) {
|
|
251
|
-
return function(constructor) {
|
|
252
|
-
if (!findModel(constructor)) createModel(constructor, options || {});
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
registerBuiltIns(createModel);
|
|
256
|
-
export {
|
|
257
|
-
ModelDecoratorFactory as Model,
|
|
258
|
-
Prop,
|
|
259
|
-
XMLModel,
|
|
260
|
-
createModel,
|
|
261
|
-
getModel,
|
|
262
|
-
getParentModel
|
|
263
|
-
};
|
|
264
|
-
//# sourceMappingURL=index.js.map
|
package/dist/model/property.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Constructor } from 'typescript-rtti';
|
|
2
|
-
import { XMLModelProperty, XMLModelPropertyOptions, CreateXMLModelPropertyOptions } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Returns the resolved conversion options for a property, using stored `@Prop()` options
|
|
5
|
-
* if available, or falling back to defaults derived from the property's type information.
|
|
6
|
-
*/
|
|
7
|
-
export declare function getPropertyConversionOptions<T>(constructor: Constructor<T>, property: XMLModelProperty<T>): XMLModelPropertyOptions<T>;
|
|
8
|
-
/**
|
|
9
|
-
* Decorator factory that customises how a class property is converted to and from XML.
|
|
10
|
-
*
|
|
11
|
-
* Applied to properties of classes decorated with `@Model()`. All options are optional —
|
|
12
|
-
* omitting `@Prop()` entirely uses the defaults inferred from the property's TypeScript type.
|
|
13
|
-
*
|
|
14
|
-
* @param options - Property conversion options.
|
|
15
|
-
*/
|
|
16
|
-
declare function PropDecoratorFactory<T = any>(options?: CreateXMLModelPropertyOptions<T>): (prototype: any, property: XMLModelProperty<T>) => void;
|
|
17
|
-
export { PropDecoratorFactory as Prop };
|
|
18
|
-
//# sourceMappingURL=property.d.ts.map
|
package/dist/model/property.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { reflect } from "typescript-rtti";
|
|
2
|
-
import { defaults } from "../defaults.js";
|
|
3
|
-
import isRegExp from "../util/is-regexp.js";
|
|
4
|
-
function resolvePropertyConversionOptions(options, constructor, property) {
|
|
5
|
-
const _options = {
|
|
6
|
-
name: property,
|
|
7
|
-
get reflected() {
|
|
8
|
-
const reflectedClass = reflect(constructor);
|
|
9
|
-
return reflectedClass.getOwnProperty(property) || reflectedClass.getProperty(property);
|
|
10
|
-
},
|
|
11
|
-
get tagname() {
|
|
12
|
-
return options.tagname || defaults.tagnameFromProperty(this);
|
|
13
|
-
},
|
|
14
|
-
inline: !!options.inline,
|
|
15
|
-
ignored: !!options.ignore,
|
|
16
|
-
isSourceElement: (...args) => defaults.propertySourceElementsFilter(...args),
|
|
17
|
-
resolveElements: options.resolveElements ? options.resolveElements : (...args) => defaults.propertyResolveSourceElements(...args),
|
|
18
|
-
fromXML: (context) => (options.fromXML ?? defaults.propertyFromXML)(context),
|
|
19
|
-
toXML: (context) => (options.toXML ?? defaults.propertyToXML)(context)
|
|
20
|
-
};
|
|
21
|
-
if (options?.model) _options.model = options.model;
|
|
22
|
-
if (options?.sourceElements) {
|
|
23
|
-
const _sourceElements = options.sourceElements;
|
|
24
|
-
if (typeof _sourceElements === "string") {
|
|
25
|
-
_options.isSourceElement = (element) => element.name === _sourceElements;
|
|
26
|
-
} else if (isRegExp(_sourceElements)) {
|
|
27
|
-
_options.isSourceElement = (element) => _sourceElements.test(element.name || "");
|
|
28
|
-
} else if (_sourceElements) {
|
|
29
|
-
_options.isSourceElement = _sourceElements;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return _options;
|
|
33
|
-
}
|
|
34
|
-
const PropertyOptions = /* @__PURE__ */ new Map();
|
|
35
|
-
function storePropertyConversionOptions(constructor, property, options) {
|
|
36
|
-
let map = PropertyOptions.get(constructor);
|
|
37
|
-
if (!map) {
|
|
38
|
-
map = /* @__PURE__ */ new Map();
|
|
39
|
-
PropertyOptions.set(constructor, map);
|
|
40
|
-
}
|
|
41
|
-
map.set(property, options);
|
|
42
|
-
}
|
|
43
|
-
function findPropertyConversionOptions(constructor, property) {
|
|
44
|
-
const options = PropertyOptions.get(constructor);
|
|
45
|
-
if (options) {
|
|
46
|
-
return options.get(property);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function getPropertyConversionOptions(constructor, property) {
|
|
50
|
-
const options = findPropertyConversionOptions(constructor, property);
|
|
51
|
-
return options ?? resolvePropertyConversionOptions({}, constructor, property);
|
|
52
|
-
}
|
|
53
|
-
function PropDecoratorFactory(options) {
|
|
54
|
-
return function(prototype, property) {
|
|
55
|
-
const _options = resolvePropertyConversionOptions(
|
|
56
|
-
options ?? {},
|
|
57
|
-
prototype.constructor,
|
|
58
|
-
property
|
|
59
|
-
);
|
|
60
|
-
storePropertyConversionOptions(prototype.constructor, property, _options);
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
export {
|
|
64
|
-
PropDecoratorFactory as Prop,
|
|
65
|
-
getPropertyConversionOptions
|
|
66
|
-
};
|
|
67
|
-
//# sourceMappingURL=property.js.map
|
package/dist/model/registry.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Constructor } from 'typescript-rtti';
|
|
2
|
-
import { XMLModel } from '.';
|
|
3
|
-
type ModelID<T> = Constructor<T>;
|
|
4
|
-
export declare const Models: Map<ModelID<unknown>, XMLModel<unknown>>;
|
|
5
|
-
export declare function registrerModel(model: XMLModel<unknown>): Map<ModelID<unknown>, XMLModel<unknown>>;
|
|
6
|
-
export declare function findModel<T>(id: ModelID<T>): XMLModel<T> | undefined;
|
|
7
|
-
export declare function getModel<T>(id: ModelID<T>): XMLModel<T>;
|
|
8
|
-
export {};
|
|
9
|
-
//# sourceMappingURL=registry.d.ts.map
|
package/dist/model/registry.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const Models = /* @__PURE__ */ new Map();
|
|
2
|
-
function registrerModel(model) {
|
|
3
|
-
return Models.set(model.type, model);
|
|
4
|
-
}
|
|
5
|
-
function findModel(id) {
|
|
6
|
-
return Models.get(id);
|
|
7
|
-
}
|
|
8
|
-
function getModel(id) {
|
|
9
|
-
const model = findModel(id);
|
|
10
|
-
if (model) return model;
|
|
11
|
-
else throw new TypeError(`couldn't find model for type ${id.name}`);
|
|
12
|
-
}
|
|
13
|
-
export {
|
|
14
|
-
Models,
|
|
15
|
-
findModel,
|
|
16
|
-
getModel,
|
|
17
|
-
registrerModel
|
|
18
|
-
};
|
|
19
|
-
//# sourceMappingURL=registry.js.map
|
package/dist/model/types.d.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { ReflectedProperty } from 'typescript-rtti';
|
|
2
|
-
import { XMLRoot, XMLElement } from '../types';
|
|
3
|
-
import { Middleware } from '../middleware';
|
|
4
|
-
import { XMLModel } from './index';
|
|
5
|
-
/** The name of a property on type `T` (string keys only). */
|
|
6
|
-
export type XMLModelProperty<T> = Extract<keyof T, string>;
|
|
7
|
-
/** A partial record mapping property names to their runtime values. */
|
|
8
|
-
export type PropertiesRecord<T> = {
|
|
9
|
-
[key in keyof T]?: T[key];
|
|
10
|
-
};
|
|
11
|
-
/** A partial record mapping property names to their XML representations. */
|
|
12
|
-
export type XMLPropertiesRecord<T> = {
|
|
13
|
-
[key in keyof T]?: XMLRoot;
|
|
14
|
-
};
|
|
15
|
-
/** Context passed to a property's `toXML` conversion function. */
|
|
16
|
-
export interface PropertyToXMLContext<T> extends Omit<toXMLContext<T>, "properties"> {
|
|
17
|
-
property: XMLModelPropertyOptions<T>;
|
|
18
|
-
value: T[keyof T];
|
|
19
|
-
}
|
|
20
|
-
/** Context passed to a property's `fromXML` conversion function. */
|
|
21
|
-
export interface PropertyFromXMLContext<T> extends Omit<fromXMLContext<T>, "properties"> {
|
|
22
|
-
property: XMLModelPropertyOptions<T>;
|
|
23
|
-
/** The XML elements resolved as source data for this property. */
|
|
24
|
-
elements: XMLElement[];
|
|
25
|
-
}
|
|
26
|
-
/** Fully resolved runtime options for a single model property. */
|
|
27
|
-
export interface XMLModelPropertyOptions<T> {
|
|
28
|
-
name: keyof T;
|
|
29
|
-
reflected: ReflectedProperty;
|
|
30
|
-
/** XML tag name used for this property. Derived from the property name in kebab-case by default. */
|
|
31
|
-
tagname: string;
|
|
32
|
-
ignored: boolean;
|
|
33
|
-
/** When `true`, array items are serialised/deserialised directly into the parent element without a wrapper tag. */
|
|
34
|
-
inline: boolean;
|
|
35
|
-
/** Override model used to convert this property's value. */
|
|
36
|
-
model?: XMLModel<any>;
|
|
37
|
-
/** Returns `true` when the given element should be considered a source for this property. */
|
|
38
|
-
isSourceElement: (element: XMLElement, context: Omit<PropertyFromXMLContext<T>, "elements">) => boolean;
|
|
39
|
-
/** Collects the XML elements that will be passed to `fromXML` for this property. */
|
|
40
|
-
resolveElements: (context: Omit<PropertyFromXMLContext<T>, "elements">) => XMLElement[];
|
|
41
|
-
fromXML: (context: PropertyFromXMLContext<T>) => T[keyof T];
|
|
42
|
-
toXML: (context: PropertyToXMLContext<T>) => XMLRoot;
|
|
43
|
-
}
|
|
44
|
-
/** User-facing options accepted by the `@Prop()` decorator and `createModel`. */
|
|
45
|
-
export interface CreateXMLModelPropertyOptions<T> {
|
|
46
|
-
/** Override the XML tag name for this property. */
|
|
47
|
-
tagname?: string;
|
|
48
|
-
/**
|
|
49
|
-
* Controls which XML elements are treated as the source for this property.
|
|
50
|
-
* - `string`: exact tag name match
|
|
51
|
-
* - `RegExp`: tag name pattern match
|
|
52
|
-
* - `function`: custom predicate
|
|
53
|
-
*/
|
|
54
|
-
sourceElements?: string | RegExp | XMLModelPropertyOptions<T>["isSourceElement"];
|
|
55
|
-
/** Custom element resolver; overrides the default source-element resolution logic. */
|
|
56
|
-
resolveElements?: XMLModelPropertyOptions<T>["resolveElements"];
|
|
57
|
-
toXML?: XMLModelPropertyOptions<T>["toXML"];
|
|
58
|
-
fromXML?: XMLModelPropertyOptions<T>["fromXML"];
|
|
59
|
-
/** When `true`, array items are inlined directly into the parent element. */
|
|
60
|
-
inline?: boolean;
|
|
61
|
-
/** When `true`, the property is excluded from XML conversion entirely. */
|
|
62
|
-
ignore?: boolean;
|
|
63
|
-
/** Explicit model to use for this property's value. */
|
|
64
|
-
model?: XMLModelPropertyOptions<T>["model"];
|
|
65
|
-
}
|
|
66
|
-
interface ConversionOptions<C, T> {
|
|
67
|
-
parent: ConversionOptions<C, T> | null;
|
|
68
|
-
middlewares: Middleware<C, T>[];
|
|
69
|
-
}
|
|
70
|
-
/** Context passed to the model-level `fromXML` middleware chain. */
|
|
71
|
-
export interface fromXMLContext<T> {
|
|
72
|
-
xml: XMLRoot;
|
|
73
|
-
/** Lazily computed property values derived from `xml`. */
|
|
74
|
-
properties: PropertiesRecord<T>;
|
|
75
|
-
model: XMLModel<T>;
|
|
76
|
-
}
|
|
77
|
-
/** Context passed to the model-level `toXML` middleware chain. */
|
|
78
|
-
export interface toXMLContext<T> {
|
|
79
|
-
object: T;
|
|
80
|
-
/** Lazily computed per-property XML fragments. */
|
|
81
|
-
properties: XMLPropertiesRecord<T>;
|
|
82
|
-
model: XMLModel<T>;
|
|
83
|
-
}
|
|
84
|
-
/** Internal fully-resolved model options (not the user-facing create options). */
|
|
85
|
-
export interface XMLModelOptions<T> {
|
|
86
|
-
parent?: XMLModel<T>;
|
|
87
|
-
properties: {
|
|
88
|
-
fromXML: ConversionOptions<Omit<fromXMLContext<T>, "properties">, PropertiesRecord<T>>;
|
|
89
|
-
toXML: ConversionOptions<Omit<toXMLContext<T>, "properties">, XMLPropertiesRecord<T>>;
|
|
90
|
-
options: Map<XMLModelProperty<T>, XMLModelPropertyOptions<T>>;
|
|
91
|
-
};
|
|
92
|
-
fromXML: ConversionOptions<fromXMLContext<T>, T>;
|
|
93
|
-
toXML: ConversionOptions<toXMLContext<T>, XMLRoot>;
|
|
94
|
-
/** XML tag name for the root element of this model. */
|
|
95
|
-
tagname: string;
|
|
96
|
-
}
|
|
97
|
-
/** User-facing options for `createModel` / `@Model()`. */
|
|
98
|
-
export interface CreateXMLModelOptions<T> {
|
|
99
|
-
/** Explicitly set the parent model (otherwise inferred from the prototype chain). */
|
|
100
|
-
parent?: XMLModelOptions<T>["parent"];
|
|
101
|
-
/** Middleware that converts XML into an instance of `T`. Required unless a parent model provides one. */
|
|
102
|
-
fromXML?: XMLModelOptions<T>["fromXML"]["middlewares"][number];
|
|
103
|
-
/** Middleware that converts an instance of `T` into XML. Optional — a default implementation is provided. */
|
|
104
|
-
toXML?: XMLModelOptions<T>["toXML"]["middlewares"][number];
|
|
105
|
-
/** Override the root XML tag name. Defaults to the class name in kebab-case. */
|
|
106
|
-
tagname?: XMLModelOptions<T>["tagname"];
|
|
107
|
-
}
|
|
108
|
-
export { XMLModel };
|
|
109
|
-
//# sourceMappingURL=types.d.ts.map
|