xml-model 1.3.3 → 2.0.0-beta.1
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/index.d.ts +3 -7
- package/dist/index.js +18 -17
- package/dist/model.d.ts +53 -0
- package/dist/model.js +69 -0
- 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 +385 -0
- package/dist/xml/examples.d.ts +188 -0
- package/dist/xml/index.d.ts +5 -79
- package/dist/xml/index.js +14 -54
- package/dist/xml/model.d.ts +18 -0
- package/dist/xml/model.js +37 -0
- package/dist/xml/schema-meta.d.ts +57 -0
- package/dist/xml/schema-meta.js +96 -0
- package/dist/xml/xml-js.d.ts +138 -3
- package/dist/xml/xml-js.js +89 -5
- package/package.json +10 -23
- 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/xml/index.js
CHANGED
|
@@ -1,57 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
const stringify = XMLJS.stringify;
|
|
6
|
-
function getContent(xml) {
|
|
7
|
-
if (xml.elements?.length === 1) {
|
|
8
|
-
const content = xml.elements[0];
|
|
9
|
-
if (content.type === "text") return content.text;
|
|
10
|
-
}
|
|
11
|
-
if (!xml.elements) return "";
|
|
12
|
-
throw new TypeError(`can't get text from XMLElement: ${JSON.stringify(xml)}`);
|
|
13
|
-
}
|
|
14
|
-
function fromContent(content = "", tag, attributes) {
|
|
15
|
-
const el = {
|
|
16
|
-
elements: content ? [{ type: "text", text: String(content) }] : []
|
|
17
|
-
};
|
|
18
|
-
if (tag) el.name = tag;
|
|
19
|
-
if (attributes) {
|
|
20
|
-
if (!el.name) throw new TypeError("please provide a name if you want to provide attributes");
|
|
21
|
-
el.attributes = attributes;
|
|
22
|
-
}
|
|
23
|
-
if (el.name) el.type = "element";
|
|
24
|
-
return el;
|
|
25
|
-
}
|
|
26
|
-
function addElement(xml, element) {
|
|
27
|
-
if (!xml.elements) xml.elements = [];
|
|
28
|
-
xml.elements.push(element);
|
|
29
|
-
}
|
|
30
|
-
function setAttribute(xml, attribute, value) {
|
|
31
|
-
if (!xml.attributes) xml.attributes = {};
|
|
32
|
-
xml.attributes[attribute] = value;
|
|
33
|
-
}
|
|
34
|
-
function deleteAttribute(xml, attribute) {
|
|
35
|
-
if (!xml.attributes) return;
|
|
36
|
-
delete xml.attributes[attribute];
|
|
37
|
-
}
|
|
38
|
-
const XML = {
|
|
39
|
-
parse,
|
|
40
|
-
stringify,
|
|
41
|
-
fromContent,
|
|
42
|
-
getContent,
|
|
43
|
-
addElement,
|
|
44
|
-
setAttribute,
|
|
45
|
-
deleteAttribute
|
|
46
|
-
};
|
|
1
|
+
import { XML, ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode } from "./xml-js.js";
|
|
2
|
+
import { xml } from "./schema-meta.js";
|
|
3
|
+
import { normalizeCodecOptions, registerDefault } from "./codec.js";
|
|
4
|
+
import { xmlModel } from "./model.js";
|
|
47
5
|
export {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
6
|
+
XML,
|
|
7
|
+
ZXMLCommentNode,
|
|
8
|
+
ZXMLElementNode,
|
|
9
|
+
ZXMLNode,
|
|
10
|
+
ZXMLRoot,
|
|
11
|
+
ZXMLTextNode,
|
|
12
|
+
normalizeCodecOptions,
|
|
13
|
+
registerDefault,
|
|
14
|
+
xml,
|
|
15
|
+
xmlModel
|
|
56
16
|
};
|
|
57
17
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { XMLElement, StringifyOptions, XMLRoot } from './xml-js';
|
|
3
|
+
import { ModelConstructor } from '../model';
|
|
4
|
+
import { UserCodecOptions } from './codec';
|
|
5
|
+
/**
|
|
6
|
+
* Constructor type for xmlModel classes.
|
|
7
|
+
* Extends ModelConstructor with XML-specific helpers and a typed extend().
|
|
8
|
+
*/
|
|
9
|
+
export type XmlModelConstructor<S extends z.ZodObject<any> = z.ZodObject<any>, Inst extends z.infer<S> = z.infer<S>> = ModelConstructor<S, Inst> & {
|
|
10
|
+
/** Returns a new instance parsed from an XML string or XMLRoot. */
|
|
11
|
+
fromXML<T extends abstract new (...args: any[]) => any>(this: T, xmlInput: string | XMLRoot | XMLElement): InstanceType<T>;
|
|
12
|
+
/** Converts an instance to an XMLRoot document tree. */
|
|
13
|
+
toXML(instance: z.infer<S>): XMLRoot;
|
|
14
|
+
/** Converts an instance to an XML string. */
|
|
15
|
+
toXMLString(instance: z.infer<S>, options?: StringifyOptions): string;
|
|
16
|
+
};
|
|
17
|
+
export declare function xmlModel<S extends z.ZodObject<any>>(schema: S, options?: UserCodecOptions<S>): XmlModelConstructor<S>;
|
|
18
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import "zod";
|
|
2
|
+
import { XML } from "./xml-js.js";
|
|
3
|
+
import { model } from "../model.js";
|
|
4
|
+
import { decode, XML_STATE, encode } from "./codec.js";
|
|
5
|
+
import { root } from "./schema-meta.js";
|
|
6
|
+
function xmlModel(schema, options) {
|
|
7
|
+
const _schema = options ? root(schema, options) : schema;
|
|
8
|
+
return class extends model(_schema) {
|
|
9
|
+
static fromXML(input) {
|
|
10
|
+
if (typeof input === "string") {
|
|
11
|
+
input = XML.parse(input);
|
|
12
|
+
}
|
|
13
|
+
if (XML.isRoot(input)) {
|
|
14
|
+
input = XML.elementFromRoot(input);
|
|
15
|
+
}
|
|
16
|
+
const schema2 = this.dataSchema;
|
|
17
|
+
const inputData = decode(this.dataSchema, input);
|
|
18
|
+
const xmlState = inputData[XML_STATE];
|
|
19
|
+
const parsed = schema2.parse(inputData);
|
|
20
|
+
parsed[XML_STATE] = xmlState;
|
|
21
|
+
return this.fromData(parsed);
|
|
22
|
+
}
|
|
23
|
+
static toXML(instance) {
|
|
24
|
+
const data = this.toData(instance);
|
|
25
|
+
const element = encode(this.dataSchema, data);
|
|
26
|
+
return { elements: [element] };
|
|
27
|
+
}
|
|
28
|
+
static toXMLString(instance, options2 = {}) {
|
|
29
|
+
const xml = this.toXML(instance);
|
|
30
|
+
return XML.stringify(xml, options2);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
xmlModel
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { UserCodecOptions, PropertyDecodingContext, PropertyEncodingContext } from './codec';
|
|
3
|
+
import { XMLElement } from './xml-js';
|
|
4
|
+
declare module "zod" {
|
|
5
|
+
interface GlobalMeta {
|
|
6
|
+
"@@xml-model"?: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
type UserRootOptions<S extends z.ZodType = z.ZodType> = {
|
|
10
|
+
tagname?: string | UserCodecOptions<S>["tagname"];
|
|
11
|
+
decode?: UserCodecOptions<S>["decode"];
|
|
12
|
+
encode?: UserCodecOptions<S>["encode"];
|
|
13
|
+
};
|
|
14
|
+
type UserPropOptions = {
|
|
15
|
+
tagname?: string | UserCodecOptions["propertyTagname"];
|
|
16
|
+
inline?: boolean;
|
|
17
|
+
match?: RegExp | ((el: XMLElement) => boolean);
|
|
18
|
+
decode?: (ctx: PropertyDecodingContext) => Partial<Record<string, unknown>> | undefined;
|
|
19
|
+
encode?: (ctx: PropertyEncodingContext) => XMLElement | undefined;
|
|
20
|
+
};
|
|
21
|
+
export declare function root<S extends z.ZodType>(schema: S, options: UserRootOptions<S>): S;
|
|
22
|
+
export declare function root(options: UserRootOptions): z.GlobalMeta;
|
|
23
|
+
/**
|
|
24
|
+
* Annotate a field schema with XML child-element options.
|
|
25
|
+
*
|
|
26
|
+
* **`xml.prop()` with no options is a no-op.** The codec already iterates all
|
|
27
|
+
* ZodObject fields and defaults the tag name to `kebabCase(fieldKey)`. Wrap a
|
|
28
|
+
* schema in `xml.prop()` only when you need to customise at least one of:
|
|
29
|
+
* `tagname`, `inline`, `match`, `decode`, or `encode`.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // ✅ Needed — custom tagname
|
|
33
|
+
* xml.prop(z.string(), { tagname: "pub-date" })
|
|
34
|
+
*
|
|
35
|
+
* // ✅ Needed — inline (children promoted to parent)
|
|
36
|
+
* xml.prop(z.array(ItemSchema), { inline: true })
|
|
37
|
+
*
|
|
38
|
+
* // ⚠️ Redundant — equivalent to plain z.string()
|
|
39
|
+
* xml.prop(z.string())
|
|
40
|
+
*/
|
|
41
|
+
export declare function prop<PS extends z.ZodType>(schema: PS, options: UserPropOptions): PS;
|
|
42
|
+
export declare function prop(options: UserPropOptions): z.GlobalMeta;
|
|
43
|
+
type AttributePropOptions = {
|
|
44
|
+
name?: string;
|
|
45
|
+
};
|
|
46
|
+
export declare function attr<PS extends z.ZodType>(schema: PS, options?: AttributePropOptions): PS;
|
|
47
|
+
export declare function attr(options?: AttributePropOptions): z.GlobalMeta;
|
|
48
|
+
/** Namespace object for XML metadata helpers. */
|
|
49
|
+
export declare const xml: {
|
|
50
|
+
root: typeof root;
|
|
51
|
+
prop: typeof prop;
|
|
52
|
+
attr: typeof attr;
|
|
53
|
+
};
|
|
54
|
+
export declare function getOwnUserOptions<S extends z.ZodType>(schema: S): UserCodecOptions<S>;
|
|
55
|
+
export declare function getUserOptions<S extends z.ZodType>(schema: S): UserCodecOptions<S>;
|
|
56
|
+
export {};
|
|
57
|
+
//# sourceMappingURL=schema-meta.d.ts.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import "zod";
|
|
2
|
+
import { isZodType, getParentSchema } from "../util/zod.js";
|
|
3
|
+
const metaKey = "@@xml-model";
|
|
4
|
+
function setMeta(schema, partial) {
|
|
5
|
+
const existing = schema.meta()?.[metaKey] ?? {};
|
|
6
|
+
return schema.meta({ [metaKey]: { ...existing, ...partial } });
|
|
7
|
+
}
|
|
8
|
+
function normalizePropOptions(options) {
|
|
9
|
+
if (!options) return {};
|
|
10
|
+
const partial = {};
|
|
11
|
+
if (options.tagname !== void 0)
|
|
12
|
+
partial.propertyTagname = options.tagname;
|
|
13
|
+
if (options.inline !== void 0) partial.inlineProperty = options.inline;
|
|
14
|
+
if (options.match !== void 0)
|
|
15
|
+
partial.propertyMatch = options.match;
|
|
16
|
+
if (options.decode !== void 0) {
|
|
17
|
+
const userDecode = options.decode;
|
|
18
|
+
partial.decodeAsProperty = function(ctx) {
|
|
19
|
+
const res = userDecode(ctx);
|
|
20
|
+
if (typeof res !== "undefined") {
|
|
21
|
+
Object.assign(ctx.result, res);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (options.encode !== void 0) {
|
|
26
|
+
const userEncode = options.encode;
|
|
27
|
+
partial.encodeAsProperty = function(ctx) {
|
|
28
|
+
const { property } = ctx;
|
|
29
|
+
const res = userEncode(ctx);
|
|
30
|
+
if (typeof res === "undefined") return;
|
|
31
|
+
if (property.options.inlineProperty) {
|
|
32
|
+
ctx.result.elements.push(...res.elements);
|
|
33
|
+
} else {
|
|
34
|
+
res.name = property.tagname;
|
|
35
|
+
ctx.result.elements.push(res);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return partial;
|
|
40
|
+
}
|
|
41
|
+
function root(optionsOrSchema, options) {
|
|
42
|
+
if (isZodType(optionsOrSchema)) {
|
|
43
|
+
return setMeta(optionsOrSchema, options ?? {});
|
|
44
|
+
} else {
|
|
45
|
+
return { [metaKey]: optionsOrSchema };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function prop(optionsOrSchema, options) {
|
|
49
|
+
if (isZodType(optionsOrSchema)) {
|
|
50
|
+
return setMeta(optionsOrSchema, normalizePropOptions(options));
|
|
51
|
+
} else {
|
|
52
|
+
return { [metaKey]: normalizePropOptions(optionsOrSchema) };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function attr(optionsOrSchema, options) {
|
|
56
|
+
const opts = isZodType(optionsOrSchema) ? options ?? {} : optionsOrSchema ?? {};
|
|
57
|
+
const partial = {
|
|
58
|
+
decodeAsProperty(ctx) {
|
|
59
|
+
const { name, options: propOptions } = ctx.property;
|
|
60
|
+
const attrName = opts.name ?? name;
|
|
61
|
+
const attrValue = ctx.xml?.attributes[attrName];
|
|
62
|
+
ctx.result[name] = propOptions.schema.parse(attrValue);
|
|
63
|
+
},
|
|
64
|
+
encodeAsProperty(ctx) {
|
|
65
|
+
const { value, name } = ctx.property;
|
|
66
|
+
const attrName = opts.name ?? name;
|
|
67
|
+
ctx.result.attributes[attrName] = value.toString();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
if (isZodType(optionsOrSchema)) {
|
|
71
|
+
return setMeta(optionsOrSchema, partial);
|
|
72
|
+
} else {
|
|
73
|
+
return { [metaKey]: partial };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const xml = { root, prop, attr };
|
|
77
|
+
function getOwnUserOptions(schema) {
|
|
78
|
+
const meta = schema.meta();
|
|
79
|
+
return meta?.[metaKey] ?? {};
|
|
80
|
+
}
|
|
81
|
+
function getUserOptions(schema) {
|
|
82
|
+
const own = getOwnUserOptions(schema);
|
|
83
|
+
const parentSchema = getParentSchema(schema);
|
|
84
|
+
if (!parentSchema) return own;
|
|
85
|
+
const parentOptions = getUserOptions(parentSchema);
|
|
86
|
+
return { ...parentOptions, ...own };
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
attr,
|
|
90
|
+
getOwnUserOptions,
|
|
91
|
+
getUserOptions,
|
|
92
|
+
prop,
|
|
93
|
+
root,
|
|
94
|
+
xml
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=schema-meta.js.map
|
package/dist/xml/xml-js.d.ts
CHANGED
|
@@ -1,7 +1,142 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Options, Element } from 'xml-js';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const ZXMLElementNode: z.ZodObject<{
|
|
4
|
+
type: z.ZodLiteral<"element">;
|
|
5
|
+
name: z.ZodString;
|
|
6
|
+
attributes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
7
|
+
elements: z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject</*elided*/ any, z.core.$strip>, z.ZodObject<{
|
|
8
|
+
type: z.ZodLiteral<"comment">;
|
|
9
|
+
comment: z.ZodString;
|
|
10
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
11
|
+
type: z.ZodLiteral<"text">;
|
|
12
|
+
text: z.ZodString;
|
|
13
|
+
}, z.core.$strip>], "type">>>;
|
|
14
|
+
}, z.core.$strip>;
|
|
15
|
+
export type XMLElementNode = z.infer<typeof ZXMLElementNode>;
|
|
16
|
+
export declare const ZXMLCommentNode: z.ZodObject<{
|
|
17
|
+
type: z.ZodLiteral<"comment">;
|
|
18
|
+
comment: z.ZodString;
|
|
19
|
+
}, z.core.$strip>;
|
|
20
|
+
export type XMLCommentNode = z.infer<typeof ZXMLCommentNode>;
|
|
21
|
+
export declare const ZXMLTextNode: z.ZodObject<{
|
|
22
|
+
type: z.ZodLiteral<"text">;
|
|
23
|
+
text: z.ZodString;
|
|
24
|
+
}, z.core.$strip>;
|
|
25
|
+
export type XMLTextNode = z.infer<typeof ZXMLTextNode>;
|
|
26
|
+
export type XMLNode = XMLElementNode | XMLCommentNode | XMLTextNode;
|
|
27
|
+
export declare const ZXMLNode: z.ZodDiscriminatedUnion<[
|
|
28
|
+
typeof ZXMLElementNode,
|
|
29
|
+
typeof ZXMLCommentNode,
|
|
30
|
+
typeof ZXMLTextNode
|
|
31
|
+
], "type">;
|
|
32
|
+
/**
|
|
33
|
+
* A single node in the xml-js element tree.
|
|
34
|
+
* Used as the internal representation for all XML parsing and serialization.
|
|
35
|
+
*/
|
|
36
|
+
export type XMLElement = XMLElementNode;
|
|
37
|
+
export declare const ZXMLRoot: z.ZodObject<{
|
|
38
|
+
elements: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
39
|
+
type: z.ZodLiteral<"element">;
|
|
40
|
+
name: z.ZodString;
|
|
41
|
+
attributes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
42
|
+
elements: z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject</*elided*/ any, z.core.$strip>, z.ZodObject<{
|
|
43
|
+
type: z.ZodLiteral<"comment">;
|
|
44
|
+
comment: z.ZodString;
|
|
45
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
46
|
+
type: z.ZodLiteral<"text">;
|
|
47
|
+
text: z.ZodString;
|
|
48
|
+
}, z.core.$strip>], "type">>>;
|
|
49
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
50
|
+
type: z.ZodLiteral<"comment">;
|
|
51
|
+
comment: z.ZodString;
|
|
52
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
53
|
+
type: z.ZodLiteral<"text">;
|
|
54
|
+
text: z.ZodString;
|
|
55
|
+
}, z.core.$strip>], "type">>;
|
|
56
|
+
}, z.core.$strip>;
|
|
57
|
+
/** The root of an xml-js document: a wrapper object whose `elements` array holds top-level nodes.
|
|
58
|
+
*
|
|
59
|
+
* **the `elements` array contains AT MOST one node with type `element`**
|
|
60
|
+
*/
|
|
61
|
+
export type XMLRoot = {
|
|
62
|
+
elements: XMLNode[];
|
|
63
|
+
};
|
|
64
|
+
type EmptyObj = Record<PropertyKey, never>;
|
|
65
|
+
export type XMLVoid = EmptyObj;
|
|
66
|
+
export type ParseOptions = Omit<Options.XML2JS, "compact">;
|
|
67
|
+
declare function parse(xml: string, options?: ParseOptions): XMLRoot;
|
|
68
|
+
export type StringifyOptions = Options.JS2XML;
|
|
69
|
+
declare function stringify(xml: XMLRoot, options?: StringifyOptions): string;
|
|
70
|
+
declare function isRoot(xml: Element): xml is XMLRoot;
|
|
71
|
+
declare function elementFromRoot(root: XMLRoot): XMLElementNode | undefined;
|
|
72
|
+
declare function isEmpty(xml: Element): xml is XMLVoid;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts the text content from an element that has a single text child node.
|
|
75
|
+
*
|
|
76
|
+
* @param xml - An `XMLElement` expected to contain a single text node.
|
|
77
|
+
* @returns The text value, or an empty string when there are no child elements.
|
|
78
|
+
* @throws {TypeError} When the element has multiple or non-text children.
|
|
79
|
+
*/
|
|
80
|
+
declare function getContent(xml: XMLElement): string;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a minimal element structure wrapping the given text content.
|
|
83
|
+
* When no tag name is provided, returns a fragment with a text child.
|
|
84
|
+
* When a tag name is provided, returns a full element with the given name and optional attributes.
|
|
85
|
+
*
|
|
86
|
+
* @param content - The text content to wrap (defaults to empty string).
|
|
87
|
+
* @param tag - Optional element tag name.
|
|
88
|
+
* @param attributes - Optional attributes; only valid when `tag` is provided.
|
|
89
|
+
* @throws {TypeError} When `attributes` are provided without a `tag`.
|
|
90
|
+
*/
|
|
91
|
+
declare function fromContent(content: string): {
|
|
92
|
+
elements: [{
|
|
93
|
+
type: "text";
|
|
94
|
+
text: string;
|
|
95
|
+
}] | [];
|
|
96
|
+
};
|
|
97
|
+
declare function fromContent(content: string, tag: string, attributes?: XMLElement["attributes"]): {
|
|
98
|
+
type: "element";
|
|
99
|
+
name: string;
|
|
100
|
+
attributes?: XMLElement["attributes"];
|
|
101
|
+
elements: [{
|
|
102
|
+
type: "text";
|
|
103
|
+
text: string;
|
|
104
|
+
}] | [];
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Appends a child element to `xml`, initialising the `elements` array if needed.
|
|
108
|
+
*
|
|
109
|
+
* @param xml - The parent element to modify.
|
|
110
|
+
* @param element - The child element to append.
|
|
111
|
+
*/
|
|
112
|
+
declare function addElement(xml: XMLElement, element: XMLElement): void;
|
|
113
|
+
/**
|
|
114
|
+
* Sets an attribute on an element, initialising the `attributes` map if needed.
|
|
115
|
+
*
|
|
116
|
+
* @param xml - The element to modify.
|
|
117
|
+
* @param attribute - The attribute name.
|
|
118
|
+
* @param value - The attribute value.
|
|
119
|
+
*/
|
|
120
|
+
declare function setAttribute(xml: XMLElement, attribute: string, value: string): void;
|
|
121
|
+
/**
|
|
122
|
+
* Removes an attribute from an element. Does nothing if the element has no attributes.
|
|
123
|
+
*
|
|
124
|
+
* @param xml - The element to modify.
|
|
125
|
+
* @param attribute - The attribute name to remove.
|
|
126
|
+
*/
|
|
127
|
+
declare function deleteAttribute(xml: XMLElement, attribute: string): void;
|
|
128
|
+
/** Namespace object bundling all XML utility functions. */
|
|
129
|
+
export declare const XML: {
|
|
3
130
|
parse: typeof parse;
|
|
4
131
|
stringify: typeof stringify;
|
|
132
|
+
isRoot: typeof isRoot;
|
|
133
|
+
elementFromRoot: typeof elementFromRoot;
|
|
134
|
+
isEmpty: typeof isEmpty;
|
|
135
|
+
fromContent: typeof fromContent;
|
|
136
|
+
getContent: typeof getContent;
|
|
137
|
+
addElement: typeof addElement;
|
|
138
|
+
setAttribute: typeof setAttribute;
|
|
139
|
+
deleteAttribute: typeof deleteAttribute;
|
|
5
140
|
};
|
|
6
|
-
export default
|
|
141
|
+
export default XML;
|
|
7
142
|
//# sourceMappingURL=xml-js.d.ts.map
|
package/dist/xml/xml-js.js
CHANGED
|
@@ -1,9 +1,93 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { xml2js, js2xml } from "xml-js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
const ZXMLElementNode = z.object({
|
|
4
|
+
type: z.literal("element"),
|
|
5
|
+
// TODO: test if required
|
|
6
|
+
name: z.string(),
|
|
7
|
+
attributes: z.record(z.string(), z.string()).optional(),
|
|
8
|
+
get elements() {
|
|
9
|
+
return z.array(ZXMLNode).optional();
|
|
10
|
+
}
|
|
11
|
+
// TODO: other fields
|
|
12
|
+
});
|
|
13
|
+
const ZXMLCommentNode = z.object({ type: z.literal("comment"), comment: z.string() });
|
|
14
|
+
const ZXMLTextNode = z.object({ type: z.literal("text"), text: z.string() });
|
|
15
|
+
const ZXMLNode = z.discriminatedUnion("type", [ZXMLElementNode, ZXMLCommentNode, ZXMLTextNode]);
|
|
16
|
+
const ZXMLRoot = z.object({ elements: z.array(ZXMLNode) });
|
|
17
|
+
function parse(xml, options = {}) {
|
|
18
|
+
const strippedOptions = { ...options };
|
|
19
|
+
delete strippedOptions["compact"];
|
|
20
|
+
const res = xml2js(xml, strippedOptions);
|
|
21
|
+
if ("elements" in res) return res;
|
|
22
|
+
throw new Error("Got empty XML");
|
|
23
|
+
}
|
|
24
|
+
function stringify(xml, options = {}) {
|
|
25
|
+
return js2xml(xml, options);
|
|
26
|
+
}
|
|
27
|
+
function isRoot(xml) {
|
|
28
|
+
const keys = Object.keys(xml);
|
|
29
|
+
return keys.length === 1 && Array.isArray(xml.elements);
|
|
30
|
+
}
|
|
31
|
+
function elementFromRoot(root) {
|
|
32
|
+
return root.elements.find((el) => el.type === "element");
|
|
33
|
+
}
|
|
34
|
+
function isEmpty(xml) {
|
|
35
|
+
return Object.keys(xml).length === 0;
|
|
36
|
+
}
|
|
37
|
+
function getContent(xml) {
|
|
38
|
+
if (xml.elements?.length === 1) {
|
|
39
|
+
const content = xml.elements[0];
|
|
40
|
+
if (content.type === "text") return content.text;
|
|
41
|
+
}
|
|
42
|
+
if (!xml.elements) return "";
|
|
43
|
+
throw new TypeError(`can't get text from XMLElement: ${JSON.stringify(xml)}`);
|
|
44
|
+
}
|
|
45
|
+
function fromContent(content = "", tag, attributes) {
|
|
46
|
+
const elements = content ? [{ type: "text", text: String(content) }] : [];
|
|
47
|
+
if (!tag) {
|
|
48
|
+
return { elements };
|
|
49
|
+
}
|
|
50
|
+
const el = {
|
|
51
|
+
type: "element",
|
|
52
|
+
name: tag,
|
|
53
|
+
elements
|
|
54
|
+
};
|
|
55
|
+
if (attributes) {
|
|
56
|
+
el.attributes = attributes;
|
|
57
|
+
}
|
|
58
|
+
return el;
|
|
59
|
+
}
|
|
60
|
+
function addElement(xml, element) {
|
|
61
|
+
if (!xml.elements) xml.elements = [];
|
|
62
|
+
xml.elements.push(element);
|
|
63
|
+
}
|
|
64
|
+
function setAttribute(xml, attribute, value) {
|
|
65
|
+
if (!xml.attributes) xml.attributes = {};
|
|
66
|
+
xml.attributes[attribute] = value;
|
|
67
|
+
}
|
|
68
|
+
function deleteAttribute(xml, attribute) {
|
|
69
|
+
if (!xml.attributes) return;
|
|
70
|
+
delete xml.attributes[attribute];
|
|
71
|
+
}
|
|
72
|
+
const XML = {
|
|
73
|
+
parse,
|
|
74
|
+
stringify,
|
|
75
|
+
isRoot,
|
|
76
|
+
elementFromRoot,
|
|
77
|
+
isEmpty,
|
|
78
|
+
fromContent,
|
|
79
|
+
getContent,
|
|
80
|
+
addElement,
|
|
81
|
+
setAttribute,
|
|
82
|
+
deleteAttribute
|
|
5
83
|
};
|
|
6
84
|
export {
|
|
7
|
-
|
|
85
|
+
XML,
|
|
86
|
+
ZXMLCommentNode,
|
|
87
|
+
ZXMLElementNode,
|
|
88
|
+
ZXMLNode,
|
|
89
|
+
ZXMLRoot,
|
|
90
|
+
ZXMLTextNode,
|
|
91
|
+
XML as default
|
|
8
92
|
};
|
|
9
93
|
//# sourceMappingURL=xml-js.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xml-model",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"description": "allows transparent XML <-> Object conversion in typescript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "MathisTLD",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
13
|
},
|
|
14
|
-
"./
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"import": "./dist/
|
|
14
|
+
"./xml": {
|
|
15
|
+
"types": "./dist/xml/index.d.ts",
|
|
16
|
+
"import": "./dist/xml/index.js"
|
|
17
17
|
},
|
|
18
18
|
"./*": {
|
|
19
19
|
"types": "./dist/*.d.ts",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"postversion": "git push && git push --tags",
|
|
27
27
|
"build": "vite build",
|
|
28
28
|
"dev": "vite build --watch",
|
|
29
|
+
"docs": "vite dev",
|
|
29
30
|
"test": "vitest",
|
|
30
31
|
"lint": "oxlint",
|
|
31
32
|
"lint:fix": "oxlint --fix",
|
|
@@ -34,38 +35,24 @@
|
|
|
34
35
|
"prepare": "husky"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"typescript-rtti": "0.9.6",
|
|
38
38
|
"xml-js": "^1.6.11"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@rollup/plugin-typescript": "*",
|
|
42
41
|
"@types/node": "^24.10.11",
|
|
43
42
|
"husky": "^9.1.7",
|
|
44
43
|
"lint-staged": "^16.2.7",
|
|
45
|
-
"marmotte": "^0.4.
|
|
44
|
+
"marmotte": "^0.4.4",
|
|
46
45
|
"oxfmt": "^0.36.0",
|
|
47
46
|
"oxlint": "^1.51.0",
|
|
48
|
-
"
|
|
49
|
-
"typescript": "*",
|
|
50
|
-
"vite": "*",
|
|
47
|
+
"typescript": "^5.9.3",
|
|
51
48
|
"vitepress": "^2.0.0-alpha.16",
|
|
49
|
+
"vitepress-plugin-llms": "^1.11.0",
|
|
52
50
|
"vitest": "^4.0.18"
|
|
53
51
|
},
|
|
54
52
|
"peerDependencies": {
|
|
55
|
-
"
|
|
56
|
-
"tslib": "^2.8.1",
|
|
57
|
-
"typescript": "~5.1.6",
|
|
58
|
-
"vite": "^7.3.1"
|
|
59
|
-
},
|
|
60
|
-
"peerDependenciesMeta": {
|
|
61
|
-
"@rollup/plugin-typescript": {
|
|
62
|
-
"optional": true
|
|
63
|
-
},
|
|
64
|
-
"vite": {
|
|
65
|
-
"optional": true
|
|
66
|
-
}
|
|
53
|
+
"zod": "^4.1.0"
|
|
67
54
|
},
|
|
68
55
|
"lint-staged": {
|
|
69
56
|
"*": "oxfmt --no-error-on-unmatched-pattern"
|
|
70
57
|
}
|
|
71
|
-
}
|
|
58
|
+
}
|
package/dist/_virtual/Reflect.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { getDefaultExportFromCjs } from "./_commonjsHelpers.js";
|
|
2
|
-
import { __require as require_Reflect } from "../node_modules/reflect-metadata/Reflect.js";
|
|
3
|
-
var _ReflectExports = require_Reflect();
|
|
4
|
-
const Reflect = /* @__PURE__ */ getDefaultExportFromCjs(_ReflectExports);
|
|
5
|
-
export {
|
|
6
|
-
Reflect as default
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=Reflect.js.map
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
2
|
-
function getDefaultExportFromCjs(x) {
|
|
3
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
4
|
-
}
|
|
5
|
-
function getDefaultExportFromNamespaceIfPresent(n) {
|
|
6
|
-
return n && Object.prototype.hasOwnProperty.call(n, "default") ? n["default"] : n;
|
|
7
|
-
}
|
|
8
|
-
function getDefaultExportFromNamespaceIfNotNamed(n) {
|
|
9
|
-
return n && Object.prototype.hasOwnProperty.call(n, "default") && Object.keys(n).length === 1 ? n["default"] : n;
|
|
10
|
-
}
|
|
11
|
-
function getAugmentedNamespace(n) {
|
|
12
|
-
if (Object.prototype.hasOwnProperty.call(n, "__esModule")) return n;
|
|
13
|
-
var f = n.default;
|
|
14
|
-
if (typeof f == "function") {
|
|
15
|
-
var a = function a2() {
|
|
16
|
-
var isInstance = false;
|
|
17
|
-
try {
|
|
18
|
-
isInstance = this instanceof a2;
|
|
19
|
-
} catch {
|
|
20
|
-
}
|
|
21
|
-
if (isInstance) {
|
|
22
|
-
return Reflect.construct(f, arguments, this.constructor);
|
|
23
|
-
}
|
|
24
|
-
return f.apply(this, arguments);
|
|
25
|
-
};
|
|
26
|
-
a.prototype = f.prototype;
|
|
27
|
-
} else a = {};
|
|
28
|
-
Object.defineProperty(a, "__esModule", { value: true });
|
|
29
|
-
Object.keys(n).forEach(function(k) {
|
|
30
|
-
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
31
|
-
Object.defineProperty(a, k, d.get ? d : {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
get: function() {
|
|
34
|
-
return n[k];
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
return a;
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
commonjsGlobal,
|
|
42
|
-
getAugmentedNamespace,
|
|
43
|
-
getDefaultExportFromCjs,
|
|
44
|
-
getDefaultExportFromNamespaceIfNotNamed,
|
|
45
|
-
getDefaultExportFromNamespaceIfPresent
|
|
46
|
-
};
|
|
47
|
-
//# sourceMappingURL=_commonjsHelpers.js.map
|