xml-model 1.3.2 → 1.3.3
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/dist/defaults.d.ts +13 -0
- package/dist/defaults.js +36 -1
- package/dist/errors.d.ts +16 -0
- package/dist/model/index.d.ts +48 -0
- package/dist/model/index.js +28 -0
- package/dist/model/property.d.ts +12 -0
- package/dist/model/types.d.ts +35 -0
- package/dist/types.d.ts +8 -0
- package/dist/vite/index.d.ts +36 -9
- package/dist/vite/index.js +9 -1
- package/dist/xml/index.d.ts +47 -0
- package/package.json +1 -1
package/dist/defaults.d.ts
CHANGED
|
@@ -10,6 +10,19 @@ interface Defaults {
|
|
|
10
10
|
tagnameFromProperty<T>(property: XMLModelPropertyOptions<T>): string;
|
|
11
11
|
propertyToXML<T>(context: PropertyToXMLContext<T>): XMLRoot;
|
|
12
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
|
+
*/
|
|
13
26
|
export declare const defaults: Defaults;
|
|
14
27
|
export {};
|
|
15
28
|
//# sourceMappingURL=defaults.d.ts.map
|
package/dist/defaults.js
CHANGED
|
@@ -1,18 +1,36 @@
|
|
|
1
1
|
import { getModel } from "./model/registry.js";
|
|
2
2
|
import { kebabCase } from "./util/kebab-case.js";
|
|
3
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
|
+
*/
|
|
4
9
|
fromXML() {
|
|
5
10
|
throw new TypeError(
|
|
6
11
|
"you should define 'defaults.fromXML' yourself or provide a 'fromXML' function to @Model() decorator's options"
|
|
7
12
|
);
|
|
8
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
|
+
*/
|
|
9
18
|
propertyResolveSourceElements(context) {
|
|
10
19
|
const innerElements = context.xml.elements[0]?.elements || [];
|
|
11
20
|
return innerElements.filter((el) => context.property.isSourceElement(el, context));
|
|
12
21
|
},
|
|
22
|
+
/**
|
|
23
|
+
* Returns `true` when the element's tag name matches the property's tagname.
|
|
24
|
+
*/
|
|
13
25
|
propertySourceElementsFilter(element, context) {
|
|
14
26
|
return context.property.tagname === element.name;
|
|
15
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
|
+
*/
|
|
16
34
|
propertyFromXML(context) {
|
|
17
35
|
const prop = context.property;
|
|
18
36
|
const elements = context.elements;
|
|
@@ -54,7 +72,11 @@ const defaults = {
|
|
|
54
72
|
}
|
|
55
73
|
return void 0;
|
|
56
74
|
},
|
|
57
|
-
|
|
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
|
+
*/
|
|
58
80
|
toXML({ properties, model }) {
|
|
59
81
|
const elements = [];
|
|
60
82
|
model.resolveAllProperties().forEach((prop) => {
|
|
@@ -76,12 +98,25 @@ const defaults = {
|
|
|
76
98
|
]
|
|
77
99
|
};
|
|
78
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
|
+
*/
|
|
79
105
|
tagnameFromModel(model) {
|
|
80
106
|
return kebabCase(model.type.name);
|
|
81
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
|
+
*/
|
|
82
112
|
tagnameFromProperty(property) {
|
|
83
113
|
return kebabCase(String(property.name));
|
|
84
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
|
+
*/
|
|
85
120
|
propertyToXML(context) {
|
|
86
121
|
const property = context.property;
|
|
87
122
|
const type = property.reflected.type;
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,21 +1,37 @@
|
|
|
1
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
|
+
*/
|
|
2
6
|
export declare class FromXMLConversionError<T> extends Error {
|
|
3
7
|
context: Omit<fromXMLContext<T>, "properties">;
|
|
4
8
|
error: unknown;
|
|
5
9
|
name: string;
|
|
6
10
|
constructor(context: Omit<fromXMLContext<T>, "properties">, error: unknown);
|
|
7
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Thrown when a single property's XML → value conversion fails.
|
|
14
|
+
* Extends `FromXMLConversionError` with additional property context.
|
|
15
|
+
*/
|
|
8
16
|
export declare class PropertyFromXMLConversionError<T> extends FromXMLConversionError<T> {
|
|
9
17
|
propertyContext: PropertyFromXMLContext<T>;
|
|
10
18
|
name: string;
|
|
11
19
|
constructor(context: Omit<fromXMLContext<T>, "properties">, propertyContext: PropertyFromXMLContext<T>, error: unknown);
|
|
12
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Thrown when model-level object → XML conversion fails.
|
|
23
|
+
* Wraps the original cause along with the conversion context.
|
|
24
|
+
*/
|
|
13
25
|
export declare class ToXMLConversionError<T> extends Error {
|
|
14
26
|
context: Omit<toXMLContext<T>, "properties">;
|
|
15
27
|
cause: unknown;
|
|
16
28
|
name: string;
|
|
17
29
|
constructor(context: Omit<toXMLContext<T>, "properties">, cause: unknown);
|
|
18
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Thrown when a single property's value → XML conversion fails.
|
|
33
|
+
* Extends `ToXMLConversionError` with additional property context.
|
|
34
|
+
*/
|
|
19
35
|
export declare class PropertyToXMLConversionError<T> extends ToXMLConversionError<T> {
|
|
20
36
|
propertyContext: PropertyToXMLContext<T>;
|
|
21
37
|
name: string;
|
package/dist/model/index.d.ts
CHANGED
|
@@ -1,19 +1,67 @@
|
|
|
1
1
|
import { Constructor } from 'typescript-rtti';
|
|
2
2
|
import { XMLModelOptions, XMLModelPropertyOptions, CreateXMLModelOptions } from './types';
|
|
3
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
|
+
*/
|
|
4
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
|
+
*/
|
|
5
15
|
export declare class XMLModel<T = any> {
|
|
6
16
|
readonly type: Constructor<T>;
|
|
7
17
|
options: XMLModelOptions<T>;
|
|
8
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
|
+
*/
|
|
9
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
|
+
*/
|
|
10
37
|
toXML(instance: unknown): XMLRoot;
|
|
38
|
+
/** The typescript-rtti reflection metadata for the model's class. */
|
|
11
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
|
+
*/
|
|
12
44
|
resolveAllProperties(): Map<string, XMLModelPropertyOptions<any> & {
|
|
13
45
|
model: any;
|
|
14
46
|
}>;
|
|
15
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
|
+
*/
|
|
16
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
|
+
*/
|
|
17
65
|
declare function ModelDecoratorFactory<T>(options?: CreateXMLModelOptions<T>): (constructor: Constructor<T>) => void;
|
|
18
66
|
export { getModel } from './registry';
|
|
19
67
|
export { ModelDecoratorFactory as Model };
|
package/dist/model/index.js
CHANGED
|
@@ -120,6 +120,12 @@ class XMLModel {
|
|
|
120
120
|
property.name
|
|
121
121
|
);
|
|
122
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
|
+
}
|
|
123
129
|
properties.options.set(property.name, options2);
|
|
124
130
|
}
|
|
125
131
|
});
|
|
@@ -154,6 +160,14 @@ class XMLModel {
|
|
|
154
160
|
if (options.fromXML) this.options.fromXML.middlewares.push(options.fromXML);
|
|
155
161
|
if (options.toXML) this.options.toXML.middlewares.push(options.toXML);
|
|
156
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
|
+
*/
|
|
157
171
|
fromXML(xml) {
|
|
158
172
|
const _xml = typeof xml === "string" ? XML.parse(xml) : xml;
|
|
159
173
|
const model = this;
|
|
@@ -170,6 +184,15 @@ class XMLModel {
|
|
|
170
184
|
};
|
|
171
185
|
return resolve(MiddlewareChain(this.options.fromXML), context);
|
|
172
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
|
+
*/
|
|
173
196
|
toXML(instance) {
|
|
174
197
|
const model = this;
|
|
175
198
|
if (instance instanceof this.type || typeof instance !== "undefined" && instance.constructor === this.type) {
|
|
@@ -189,9 +212,14 @@ class XMLModel {
|
|
|
189
212
|
throw new TypeError(`provided object is not an instance of ${this.type.name}`);
|
|
190
213
|
}
|
|
191
214
|
}
|
|
215
|
+
/** The typescript-rtti reflection metadata for the model's class. */
|
|
192
216
|
get reflectedClass() {
|
|
193
217
|
return reflect(this.type);
|
|
194
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
|
+
*/
|
|
195
223
|
resolveAllProperties() {
|
|
196
224
|
const ownProperties = /* @__PURE__ */ new Map();
|
|
197
225
|
const parent = getParentModel(this);
|
package/dist/model/property.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { Constructor } from 'typescript-rtti';
|
|
2
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
|
+
*/
|
|
3
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
|
+
*/
|
|
4
16
|
declare function PropDecoratorFactory<T = any>(options?: CreateXMLModelPropertyOptions<T>): (prototype: any, property: XMLModelProperty<T>) => void;
|
|
5
17
|
export { PropDecoratorFactory as Prop };
|
|
6
18
|
//# sourceMappingURL=property.d.ts.map
|
package/dist/model/types.d.ts
CHANGED
|
@@ -2,57 +2,86 @@ import { ReflectedProperty } from 'typescript-rtti';
|
|
|
2
2
|
import { XMLRoot, XMLElement } from '../types';
|
|
3
3
|
import { Middleware } from '../middleware';
|
|
4
4
|
import { XMLModel } from './index';
|
|
5
|
+
/** The name of a property on type `T` (string keys only). */
|
|
5
6
|
export type XMLModelProperty<T> = Extract<keyof T, string>;
|
|
7
|
+
/** A partial record mapping property names to their runtime values. */
|
|
6
8
|
export type PropertiesRecord<T> = {
|
|
7
9
|
[key in keyof T]?: T[key];
|
|
8
10
|
};
|
|
11
|
+
/** A partial record mapping property names to their XML representations. */
|
|
9
12
|
export type XMLPropertiesRecord<T> = {
|
|
10
13
|
[key in keyof T]?: XMLRoot;
|
|
11
14
|
};
|
|
15
|
+
/** Context passed to a property's `toXML` conversion function. */
|
|
12
16
|
export interface PropertyToXMLContext<T> extends Omit<toXMLContext<T>, "properties"> {
|
|
13
17
|
property: XMLModelPropertyOptions<T>;
|
|
14
18
|
value: T[keyof T];
|
|
15
19
|
}
|
|
20
|
+
/** Context passed to a property's `fromXML` conversion function. */
|
|
16
21
|
export interface PropertyFromXMLContext<T> extends Omit<fromXMLContext<T>, "properties"> {
|
|
17
22
|
property: XMLModelPropertyOptions<T>;
|
|
23
|
+
/** The XML elements resolved as source data for this property. */
|
|
18
24
|
elements: XMLElement[];
|
|
19
25
|
}
|
|
26
|
+
/** Fully resolved runtime options for a single model property. */
|
|
20
27
|
export interface XMLModelPropertyOptions<T> {
|
|
21
28
|
name: keyof T;
|
|
22
29
|
reflected: ReflectedProperty;
|
|
30
|
+
/** XML tag name used for this property. Derived from the property name in kebab-case by default. */
|
|
23
31
|
tagname: string;
|
|
24
32
|
ignored: boolean;
|
|
33
|
+
/** When `true`, array items are serialised/deserialised directly into the parent element without a wrapper tag. */
|
|
25
34
|
inline: boolean;
|
|
35
|
+
/** Override model used to convert this property's value. */
|
|
26
36
|
model?: XMLModel<any>;
|
|
37
|
+
/** Returns `true` when the given element should be considered a source for this property. */
|
|
27
38
|
isSourceElement: (element: XMLElement, context: Omit<PropertyFromXMLContext<T>, "elements">) => boolean;
|
|
39
|
+
/** Collects the XML elements that will be passed to `fromXML` for this property. */
|
|
28
40
|
resolveElements: (context: Omit<PropertyFromXMLContext<T>, "elements">) => XMLElement[];
|
|
29
41
|
fromXML: (context: PropertyFromXMLContext<T>) => T[keyof T];
|
|
30
42
|
toXML: (context: PropertyToXMLContext<T>) => XMLRoot;
|
|
31
43
|
}
|
|
44
|
+
/** User-facing options accepted by the `@Prop()` decorator and `createModel`. */
|
|
32
45
|
export interface CreateXMLModelPropertyOptions<T> {
|
|
46
|
+
/** Override the XML tag name for this property. */
|
|
33
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
|
+
*/
|
|
34
54
|
sourceElements?: string | RegExp | XMLModelPropertyOptions<T>["isSourceElement"];
|
|
55
|
+
/** Custom element resolver; overrides the default source-element resolution logic. */
|
|
35
56
|
resolveElements?: XMLModelPropertyOptions<T>["resolveElements"];
|
|
36
57
|
toXML?: XMLModelPropertyOptions<T>["toXML"];
|
|
37
58
|
fromXML?: XMLModelPropertyOptions<T>["fromXML"];
|
|
59
|
+
/** When `true`, array items are inlined directly into the parent element. */
|
|
38
60
|
inline?: boolean;
|
|
61
|
+
/** When `true`, the property is excluded from XML conversion entirely. */
|
|
39
62
|
ignore?: boolean;
|
|
63
|
+
/** Explicit model to use for this property's value. */
|
|
40
64
|
model?: XMLModelPropertyOptions<T>["model"];
|
|
41
65
|
}
|
|
42
66
|
interface ConversionOptions<C, T> {
|
|
43
67
|
parent: ConversionOptions<C, T> | null;
|
|
44
68
|
middlewares: Middleware<C, T>[];
|
|
45
69
|
}
|
|
70
|
+
/** Context passed to the model-level `fromXML` middleware chain. */
|
|
46
71
|
export interface fromXMLContext<T> {
|
|
47
72
|
xml: XMLRoot;
|
|
73
|
+
/** Lazily computed property values derived from `xml`. */
|
|
48
74
|
properties: PropertiesRecord<T>;
|
|
49
75
|
model: XMLModel<T>;
|
|
50
76
|
}
|
|
77
|
+
/** Context passed to the model-level `toXML` middleware chain. */
|
|
51
78
|
export interface toXMLContext<T> {
|
|
52
79
|
object: T;
|
|
80
|
+
/** Lazily computed per-property XML fragments. */
|
|
53
81
|
properties: XMLPropertiesRecord<T>;
|
|
54
82
|
model: XMLModel<T>;
|
|
55
83
|
}
|
|
84
|
+
/** Internal fully-resolved model options (not the user-facing create options). */
|
|
56
85
|
export interface XMLModelOptions<T> {
|
|
57
86
|
parent?: XMLModel<T>;
|
|
58
87
|
properties: {
|
|
@@ -62,12 +91,18 @@ export interface XMLModelOptions<T> {
|
|
|
62
91
|
};
|
|
63
92
|
fromXML: ConversionOptions<fromXMLContext<T>, T>;
|
|
64
93
|
toXML: ConversionOptions<toXMLContext<T>, XMLRoot>;
|
|
94
|
+
/** XML tag name for the root element of this model. */
|
|
65
95
|
tagname: string;
|
|
66
96
|
}
|
|
97
|
+
/** User-facing options for `createModel` / `@Model()`. */
|
|
67
98
|
export interface CreateXMLModelOptions<T> {
|
|
99
|
+
/** Explicitly set the parent model (otherwise inferred from the prototype chain). */
|
|
68
100
|
parent?: XMLModelOptions<T>["parent"];
|
|
101
|
+
/** Middleware that converts XML into an instance of `T`. Required unless a parent model provides one. */
|
|
69
102
|
fromXML?: XMLModelOptions<T>["fromXML"]["middlewares"][number];
|
|
103
|
+
/** Middleware that converts an instance of `T` into XML. Optional — a default implementation is provided. */
|
|
70
104
|
toXML?: XMLModelOptions<T>["toXML"]["middlewares"][number];
|
|
105
|
+
/** Override the root XML tag name. Defaults to the class name in kebab-case. */
|
|
71
106
|
tagname?: XMLModelOptions<T>["tagname"];
|
|
72
107
|
}
|
|
73
108
|
export { XMLModel };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
export type { Constructor } from 'typescript-rtti';
|
|
2
|
+
/** A record with unknown values, keyed by string, number, or symbol. */
|
|
2
3
|
export type UnknownRecord = Record<string | number | symbol, unknown>;
|
|
4
|
+
/** Any object type. Used instead of `UnknownRecord` because it is compatible with class instances. */
|
|
3
5
|
export type UnknownObject = object;
|
|
6
|
+
/** Key-value map of XML attributes. Values may be strings, numbers, or absent. */
|
|
4
7
|
export interface XMLAttributes {
|
|
5
8
|
[key: string]: string | number | undefined;
|
|
6
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* A single node in the xml-js element tree.
|
|
12
|
+
* Used as the internal representation for all XML parsing and serialisation.
|
|
13
|
+
*/
|
|
7
14
|
export interface XMLElement {
|
|
8
15
|
type?: string;
|
|
9
16
|
name?: string;
|
|
@@ -11,6 +18,7 @@ export interface XMLElement {
|
|
|
11
18
|
elements?: Array<XMLElement>;
|
|
12
19
|
text?: string | number | boolean;
|
|
13
20
|
}
|
|
21
|
+
/** The root of an xml-js document: a wrapper object whose `elements` array holds top-level nodes. */
|
|
14
22
|
export type XMLRoot = {
|
|
15
23
|
elements: XMLElement[];
|
|
16
24
|
};
|
package/dist/vite/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
1
2
|
import { RollupTypescriptOptions } from '@rollup/plugin-typescript';
|
|
2
3
|
/**
|
|
4
|
+
* Vite plugin that preserves class names at build time.
|
|
5
|
+
*
|
|
3
6
|
* When Building, class names are changed but the library relies on them
|
|
4
|
-
* so they need to be preserved
|
|
5
|
-
*
|
|
7
|
+
* so they need to be preserved.
|
|
8
|
+
*
|
|
9
|
+
* @returns A Vite plugin that rewrites mangled class name expressions and enables `keepNames`.
|
|
6
10
|
*/
|
|
7
11
|
export declare function FixClassNames(): {
|
|
8
12
|
name: string;
|
|
@@ -17,26 +21,49 @@ export declare function FixClassNames(): {
|
|
|
17
21
|
};
|
|
18
22
|
};
|
|
19
23
|
};
|
|
24
|
+
/** Options for the `TypescriptRTTI` and `XMLModelVitePlugin` plugins. */
|
|
20
25
|
export type RTTIPluginOptions = {
|
|
21
26
|
/**
|
|
22
|
-
*
|
|
27
|
+
* Options forwarded to `@rollup/plugin-typescript`.
|
|
23
28
|
*
|
|
24
|
-
*
|
|
29
|
+
* The plugin may not work correctly if you override the `transformers` property,
|
|
30
|
+
* as these options are shallow-merged (not deep-merged).
|
|
25
31
|
*/
|
|
26
32
|
typescript?: RollupTypescriptOptions;
|
|
27
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Restrict RTTI transformation to files matching this pattern.
|
|
28
35
|
*
|
|
29
|
-
*
|
|
36
|
+
* When omitted, all files are transformed.
|
|
30
37
|
*/
|
|
31
38
|
include?: RegExp;
|
|
32
|
-
/**
|
|
39
|
+
/** Exclude files matching this pattern from RTTI transformation. */
|
|
33
40
|
exclude?: RegExp;
|
|
34
|
-
/** Print debug logs */
|
|
41
|
+
/** Print debug logs from the RTTI transformer. */
|
|
35
42
|
debug?: boolean;
|
|
36
43
|
};
|
|
44
|
+
/**
|
|
45
|
+
* Vite plugin that applies the `typescript-rtti` TypeScript transformer.
|
|
46
|
+
*
|
|
47
|
+
* This transformer emits the runtime type metadata that xml-model uses to
|
|
48
|
+
* introspect property types without manual annotations.
|
|
49
|
+
*
|
|
50
|
+
* @param options - Plugin options controlling which files are transformed.
|
|
51
|
+
* @returns A configured `@rollup/plugin-typescript` instance with the RTTI transformer injected.
|
|
52
|
+
*/
|
|
37
53
|
export declare function TypescriptRTTI(options?: RTTIPluginOptions): import('rollup').Plugin<any>;
|
|
54
|
+
/** Alias for `RTTIPluginOptions`. */
|
|
38
55
|
export type XMLModelVitePluginOptions = RTTIPluginOptions;
|
|
39
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Main xml-model Vite plugin.
|
|
58
|
+
*
|
|
59
|
+
* Combines `FixClassNames`, `TypescriptRTTI`, and an internal resolver that
|
|
60
|
+
* rewrites the `xml-model/dist/*` import paths emitted by typescript-rtti back
|
|
61
|
+
* to the canonical `xml-model/*` paths exported by the package.
|
|
62
|
+
*
|
|
63
|
+
* @param options - Options forwarded to `TypescriptRTTI`.
|
|
64
|
+
* @returns An array of Vite plugins.
|
|
65
|
+
*/
|
|
66
|
+
export declare function XMLModelVitePlugin(options?: RTTIPluginOptions): (Plugin<any> | import('rollup').Plugin<any> | {
|
|
40
67
|
name: string;
|
|
41
68
|
enforce: "post";
|
|
42
69
|
transform(this: import('rollup').TransformPluginContext, code: string, id: string): {
|
package/dist/vite/index.js
CHANGED
|
@@ -52,7 +52,15 @@ function TypescriptRTTI(options = {}) {
|
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
54
|
function XMLModelVitePlugin(options = {}) {
|
|
55
|
-
|
|
55
|
+
const distResolver = {
|
|
56
|
+
name: "xml-model-resolve-dist",
|
|
57
|
+
enforce: "pre",
|
|
58
|
+
async resolveId(id) {
|
|
59
|
+
const match = id.match(/^xml-model\/dist\/(.*)/);
|
|
60
|
+
if (match) return this.resolve(`xml-model/${match[1]}`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return [FixClassNames(), TypescriptRTTI(options), distResolver];
|
|
56
64
|
}
|
|
57
65
|
export {
|
|
58
66
|
FixClassNames,
|
package/dist/xml/index.d.ts
CHANGED
|
@@ -1,7 +1,34 @@
|
|
|
1
1
|
import { XMLElement, XMLRoot } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Parses an XML string into an `XMLRoot` document tree.
|
|
4
|
+
*
|
|
5
|
+
* @param string - A well-formed XML string.
|
|
6
|
+
* @returns The root of the parsed element tree.
|
|
7
|
+
*/
|
|
2
8
|
export declare function parse(string: string): XMLRoot;
|
|
9
|
+
/**
|
|
10
|
+
* Serialises an `XMLRoot` or `XMLElement` back into an XML string.
|
|
11
|
+
* Delegates to xml-js's `js2xml` function.
|
|
12
|
+
*/
|
|
3
13
|
export declare const stringify: typeof import('xml-js').js2xml;
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the text content from an element that has a single text child node.
|
|
16
|
+
*
|
|
17
|
+
* @param xml - An `XMLElement` expected to contain a single text node.
|
|
18
|
+
* @returns The text value, or an empty string when there are no child elements.
|
|
19
|
+
* @throws {TypeError} When the element has multiple or non-text children.
|
|
20
|
+
*/
|
|
4
21
|
export declare function getContent(xml: XMLElement): string | number | boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a minimal element structure wrapping the given text content.
|
|
24
|
+
* When no tag name is provided, returns a fragment with a text child.
|
|
25
|
+
* When a tag name is provided, returns a full element with the given name and optional attributes.
|
|
26
|
+
*
|
|
27
|
+
* @param content - The text content to wrap (defaults to empty string).
|
|
28
|
+
* @param tag - Optional element tag name.
|
|
29
|
+
* @param attributes - Optional attributes; only valid when `tag` is provided.
|
|
30
|
+
* @throws {TypeError} When `attributes` are provided without a `tag`.
|
|
31
|
+
*/
|
|
5
32
|
export declare function fromContent(content: string): {
|
|
6
33
|
elements: [{
|
|
7
34
|
type: "text";
|
|
@@ -17,9 +44,29 @@ export declare function fromContent(content: string, tag: string, attributes?: X
|
|
|
17
44
|
text: string;
|
|
18
45
|
}] | [];
|
|
19
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Appends a child element to `xml`, initialising the `elements` array if needed.
|
|
49
|
+
*
|
|
50
|
+
* @param xml - The parent element to modify.
|
|
51
|
+
* @param element - The child element to append.
|
|
52
|
+
*/
|
|
20
53
|
export declare function addElement(xml: XMLElement, element: XMLElement): void;
|
|
54
|
+
/**
|
|
55
|
+
* Sets an attribute on an element, initialising the `attributes` map if needed.
|
|
56
|
+
*
|
|
57
|
+
* @param xml - The element to modify.
|
|
58
|
+
* @param attribute - The attribute name.
|
|
59
|
+
* @param value - The attribute value.
|
|
60
|
+
*/
|
|
21
61
|
export declare function setAttribute(xml: XMLElement, attribute: string, value: string): void;
|
|
62
|
+
/**
|
|
63
|
+
* Removes an attribute from an element. Does nothing if the element has no attributes.
|
|
64
|
+
*
|
|
65
|
+
* @param xml - The element to modify.
|
|
66
|
+
* @param attribute - The attribute name to remove.
|
|
67
|
+
*/
|
|
22
68
|
export declare function deleteAttribute(xml: XMLElement, attribute: string): void;
|
|
69
|
+
/** Namespace object bundling all XML utility functions. */
|
|
23
70
|
declare const XML: {
|
|
24
71
|
parse: typeof parse;
|
|
25
72
|
stringify: typeof import('xml-js').js2xml;
|