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/README.md
CHANGED
|
@@ -1,52 +1,56 @@
|
|
|
1
1
|
# XML Model
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**[📖 Documentation](https://mathistld.github.io/xml-model/)**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**[🧑💻 Source on GitHub](https://github.com/MathisTLD/xml-model)**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Installation
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
import { defineConfig } from "vite";
|
|
11
|
-
import XMLModelVitePlugin from "xml-model/vite";
|
|
9
|
+
xml-model requires [Zod v4](https://zod.dev) as a peer dependency.
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// see options in JSDoc
|
|
16
|
-
// note that is tsconfig that includes your source files is not tsconfig.json you MUST use the tsconfig option
|
|
17
|
-
XMLModelVitePlugin(),
|
|
18
|
-
],
|
|
19
|
-
// ... rest of the config
|
|
20
|
-
});
|
|
11
|
+
```bash
|
|
12
|
+
npm install xml-model zod
|
|
21
13
|
```
|
|
22
14
|
|
|
23
|
-
|
|
15
|
+
<!-- #region what-is -->
|
|
24
16
|
|
|
25
|
-
|
|
17
|
+
## What is xml-model?
|
|
26
18
|
|
|
27
|
-
|
|
28
|
-
import { Model, getModel, XML } from "xml-model";
|
|
19
|
+
xml-model lets you define TypeScript classes that map directly to XML documents using [Zod](https://zod.dev) schemas. Annotate fields with `xml.attr()` for XML attributes, or use `xml.prop()` when you need to customise a child element's tagname, inline mode, or matching — plain Zod schemas work as-is for regular child elements. Then parse or serialise with a single method call.
|
|
29
20
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
|
|
21
|
+
```ts
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
import { xmlModel, xml } from "xml-model";
|
|
24
|
+
|
|
25
|
+
class Book extends xmlModel(
|
|
26
|
+
z.object({
|
|
27
|
+
isbn: xml.attr(z.string(), { name: "isbn" }),
|
|
28
|
+
title: z.string(),
|
|
29
|
+
year: z.number(),
|
|
30
|
+
}),
|
|
31
|
+
{ tagname: "book" },
|
|
32
|
+
) {
|
|
33
|
+
label() {
|
|
34
|
+
return `${this.title} (${this.year})`;
|
|
35
|
+
}
|
|
39
36
|
}
|
|
40
37
|
|
|
41
|
-
|
|
38
|
+
// XML → class instance
|
|
39
|
+
const book = Book.fromXML(`
|
|
40
|
+
<book isbn="978-0-7432-7356-5">
|
|
41
|
+
<title>Dune</title>
|
|
42
|
+
<year>1965</year>
|
|
43
|
+
</book>
|
|
44
|
+
`);
|
|
42
45
|
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
book.label(); // "Dune (1965)"
|
|
47
|
+
book instanceof Book; // true
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
console.log(XML.stringify(model.toXML(b))); // <my-class><foo>other</foo></my-class>
|
|
49
|
+
// class instance → XML string
|
|
50
|
+
Book.toXMLString(book);
|
|
51
|
+
// <book isbn="978-0-7432-7356-5"><title>Dune</title><year>1965</year></book>
|
|
50
52
|
```
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
Field names are automatically converted to kebab-case XML tags (`publishedAt` → `<published-at>`). Extend classes with `.extend()` to build inheritance hierarchies — child instances remain `instanceof` the parent and inherit all methods.
|
|
55
|
+
|
|
56
|
+
<!-- #endregion what-is -->
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
|
|
6
|
+
});
|
|
7
|
+
//#endregion
|
|
8
|
+
export { __commonJSMin, __require };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export type {
|
|
3
|
-
export
|
|
4
|
-
export { getModel, createModel, Model, Prop } from './model';
|
|
5
|
-
export * from './errors';
|
|
6
|
-
export * from 'typescript-rtti';
|
|
7
|
-
export { XML };
|
|
1
|
+
export { model, isModel } from './model';
|
|
2
|
+
export type { ModelConstructor } from './model';
|
|
3
|
+
export * from './xml';
|
|
8
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
FromXMLConversionError,
|
|
10
|
-
Model,
|
|
11
|
-
Prop,
|
|
12
|
-
PropertyFromXMLConversionError,
|
|
13
|
-
PropertyToXMLConversionError,
|
|
14
|
-
ToXMLConversionError,
|
|
15
|
-
default2 as XML,
|
|
16
|
-
createModel,
|
|
17
|
-
defaults,
|
|
18
|
-
getModel
|
|
19
|
-
};
|
|
20
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { isModel, model } from "./model.js";
|
|
2
|
+
import XML, { ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode } from "./xml/xml-js.js";
|
|
3
|
+
import { xml } from "./xml/schema-meta.js";
|
|
4
|
+
import { normalizeCodecOptions, registerDefault } from "./xml/codec.js";
|
|
5
|
+
import { xmlModel } from "./xml/model.js";
|
|
6
|
+
import "./xml/index.js";
|
|
7
|
+
export { XML, ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode, isModel, model, normalizeCodecOptions, registerDefault, xml, xmlModel };
|
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { z, GlobalMeta } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Constructor type for model classes.
|
|
4
|
+
*
|
|
5
|
+
* `S` — the ZodObject schema (drives field types)
|
|
6
|
+
* `Inst` — the actual instance type produced by `new`. Defaults to `z.infer<S>`.
|
|
7
|
+
* `extend()` widens it to `InstanceType<Self> & z.infer<ExtendedSchema>`
|
|
8
|
+
* so that parent class methods survive into child instances.
|
|
9
|
+
*/
|
|
10
|
+
export type ModelConstructor<S extends z.ZodObject<any> = z.ZodObject<any>, Inst extends z.infer<S> = z.infer<S>> = {
|
|
11
|
+
new (data: z.infer<S>): Inst;
|
|
12
|
+
readonly dataSchema: S;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a ZodCodec that transforms parsed data into a class instance (and can go the other way around).
|
|
15
|
+
* Use inside xml.prop() or z.array(...).
|
|
16
|
+
*/
|
|
17
|
+
schema<T extends abstract new (...args: any[]) => any>(this: T): z.ZodCodec<S, z.ZodCustom<InstanceType<T>, InstanceType<T>>>;
|
|
18
|
+
/**
|
|
19
|
+
* Override to customise instantiation — e.g. to inject extra constructor arguments.
|
|
20
|
+
* Called by from() instead of `new this(data)` directly.
|
|
21
|
+
*/
|
|
22
|
+
fromData<T extends new (...args: any[]) => any>(this: T, data: z.output<S>): InstanceType<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the raw decoded data object stored on the instance — the same
|
|
25
|
+
* object that was passed to the constructor, including any non-enumerable
|
|
26
|
+
* symbol metadata (e.g. `XML_STATE`) that survived construction.
|
|
27
|
+
*/
|
|
28
|
+
toData<T extends abstract new (...args: any[]) => any>(this: T, instance: InstanceType<T>): z.output<S>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new model class that truly extends this one — inheriting its prototype
|
|
31
|
+
* chain and methods — while adding new schema fields.
|
|
32
|
+
*
|
|
33
|
+
* Pass an optional `meta` object (e.g. `xml.root({ tagname: "car" })`) to attach
|
|
34
|
+
* Zod schema metadata to the extended schema. Multiple codec metas compose with spread:
|
|
35
|
+
* `{ ...xml.root({ tagname: "car" }), ...otherCodec.meta({...}) }`
|
|
36
|
+
*/
|
|
37
|
+
extend<Self extends ModelConstructor<S, Inst>, U extends z.core.$ZodLooseShape>(this: Self, extension: U, meta?: GlobalMeta): Omit<Self, keyof ModelConstructor<S, Inst>> & ModelConstructor<z.ZodObject<z.util.Extend<S["shape"], U>>, InstanceType<Self> & z.infer<z.ZodObject<z.util.Extend<S["shape"], U>>>>;
|
|
38
|
+
};
|
|
39
|
+
/** Stores the raw data object on model instances. */
|
|
40
|
+
export declare const DATA: unique symbol;
|
|
41
|
+
/** Returns true if `cls` is a class produced by `model()` (or a subclass of one). */
|
|
42
|
+
export declare function isModel(cls: unknown): cls is ModelConstructor;
|
|
43
|
+
/**
|
|
44
|
+
* Generic class factory. Creates a class with typed instance properties
|
|
45
|
+
* and codec-agnostic from()/to() methods.
|
|
46
|
+
*
|
|
47
|
+
* Codec-specific factories (e.g. xmlModel) wrap this and inject named helpers.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* class Book extends model(z.object({ title: z.string() })) {}
|
|
51
|
+
*/
|
|
52
|
+
export declare function model<S extends z.ZodObject<any>>(schema: S): ModelConstructor<S>;
|
|
53
|
+
//# sourceMappingURL=model.d.ts.map
|
package/dist/model.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region src/model.ts
|
|
3
|
+
var schemaSymbol = Symbol("model:schema");
|
|
4
|
+
/** Marker placed on every class returned by `model()`. Used by `isModel()`. */
|
|
5
|
+
var MODEL_MARKER = Symbol("model:marker");
|
|
6
|
+
/** Stores the raw data object on model instances. */
|
|
7
|
+
var DATA = Symbol("model:data");
|
|
8
|
+
/** Returns true if `cls` is a class produced by `model()` (or a subclass of one). */
|
|
9
|
+
function isModel(cls) {
|
|
10
|
+
return typeof cls === "function" && MODEL_MARKER in cls;
|
|
11
|
+
}
|
|
12
|
+
function defineFieldAccessors(proto, keys) {
|
|
13
|
+
for (const key of keys) Object.defineProperty(proto, key, {
|
|
14
|
+
get() {
|
|
15
|
+
return this[DATA][key];
|
|
16
|
+
},
|
|
17
|
+
set(v) {
|
|
18
|
+
this[DATA][key] = v;
|
|
19
|
+
},
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Generic class factory. Creates a class with typed instance properties
|
|
26
|
+
* and codec-agnostic from()/to() methods.
|
|
27
|
+
*
|
|
28
|
+
* Codec-specific factories (e.g. xmlModel) wrap this and inject named helpers.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* class Book extends model(z.object({ title: z.string() })) {}
|
|
32
|
+
*/
|
|
33
|
+
function model(schema) {
|
|
34
|
+
class Base {
|
|
35
|
+
static dataSchema = schema;
|
|
36
|
+
static [MODEL_MARKER] = true;
|
|
37
|
+
static schema() {
|
|
38
|
+
if (!Object.prototype.hasOwnProperty.call(this, schemaSymbol)) this[schemaSymbol] = z.codec(this.dataSchema, z.instanceof(this), {
|
|
39
|
+
decode: (data) => {
|
|
40
|
+
return this.fromData(data);
|
|
41
|
+
},
|
|
42
|
+
encode: (instance) => {
|
|
43
|
+
return instance[DATA];
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return this[schemaSymbol];
|
|
47
|
+
}
|
|
48
|
+
static extend(extension, meta) {
|
|
49
|
+
let extended = this.dataSchema.extend(extension);
|
|
50
|
+
if (meta) extended = extended.meta(meta);
|
|
51
|
+
const Child = class extends this {
|
|
52
|
+
static dataSchema = extended;
|
|
53
|
+
};
|
|
54
|
+
defineFieldAccessors(Child.prototype, Object.keys(extension));
|
|
55
|
+
return Child;
|
|
56
|
+
}
|
|
57
|
+
static fromData(data) {
|
|
58
|
+
return new this(data);
|
|
59
|
+
}
|
|
60
|
+
static toData(instance) {
|
|
61
|
+
const data = instance[DATA];
|
|
62
|
+
if (!data) throw new Error("failed to retrieve instance data");
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
constructor(data) {
|
|
66
|
+
this[DATA] = data;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
defineFieldAccessors(Base.prototype, Object.keys(schema.def.shape));
|
|
70
|
+
return Base;
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
export { isModel, model };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=model.js.map
|