xmljslib 1.0.0 → 1.0.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/dist/cli.js CHANGED
@@ -907,8 +907,9 @@ ${indent} */
907
907
  */
908
908
  compileToTypeScript() {
909
909
  let code = "/**\n * \u0422\u0418\u041F\u042B \u0421\u0413\u0415\u041D\u0415\u0420\u0418\u0420\u041E\u0412\u0410\u041D\u042B \u0410\u0412\u0422\u041E\u041C\u0410\u0422\u0418\u0427\u0415\u0421\u041A\u0418 \u041D\u0410 \u041E\u0421\u041D\u041E\u0412\u0415 WSDL/XSD\n * \u041F\u041E\u0414 \u0410\u0420\u0425\u0418\u0422\u0415\u041A\u0422\u0423\u0420\u0423 \u0423\u041C\u041D\u042B\u0425 \u041F\u0420\u0418\u041C\u0418\u0422\u0418\u0412\u041E\u0412\n */\n\n";
910
- code += "type XMLLeaf<T extends string | number | boolean | undefined, Attrs = {}> = Object extends Attrs ? T : (T & Attrs);\n\n";
911
- code += "type XMLBranch<Fields, Attrs = {}> = Fields & Attrs;\n";
910
+ code += "type XMLNamespaces = {[K in `$xmlns${string}`]?: string;};\n\n";
911
+ code += "type XMLLeaf<T extends string | number | boolean | undefined, Attrs = {}> = [keyof Attrs] extends [never] ? T : { $value?: T } & Attrs & XMLNamespaces;\n\n";
912
+ code += "type XMLBranch<Fields, Attrs = {}> = Fields & Attrs & XMLNamespaces;\n";
912
913
  code += "type canBeArray<T> = T | T[];\n\n";
913
914
  code += this.compileMessageInterfaces();
914
915
  if (this.portTypeRegistry.size > 0) {
@@ -978,6 +979,7 @@ ${indent} */
978
979
  code += ` ${attribute.name}?: ${attribute.type};
979
980
  `;
980
981
  }
982
+ code += " [key: `$xmlns${string}`]: string | undefined;\n";
981
983
  code += "}\n\n";
982
984
  }
983
985
  return code;
package/dist/index.cjs CHANGED
@@ -943,8 +943,9 @@ ${indent} */
943
943
  */
944
944
  compileToTypeScript() {
945
945
  let code = "/**\n * \u0422\u0418\u041F\u042B \u0421\u0413\u0415\u041D\u0415\u0420\u0418\u0420\u041E\u0412\u0410\u041D\u042B \u0410\u0412\u0422\u041E\u041C\u0410\u0422\u0418\u0427\u0415\u0421\u041A\u0418 \u041D\u0410 \u041E\u0421\u041D\u041E\u0412\u0415 WSDL/XSD\n * \u041F\u041E\u0414 \u0410\u0420\u0425\u0418\u0422\u0415\u041A\u0422\u0423\u0420\u0423 \u0423\u041C\u041D\u042B\u0425 \u041F\u0420\u0418\u041C\u0418\u0422\u0418\u0412\u041E\u0412\n */\n\n";
946
- code += "type XMLLeaf<T extends string | number | boolean | undefined, Attrs = {}> = Object extends Attrs ? T : (T & Attrs);\n\n";
947
- code += "type XMLBranch<Fields, Attrs = {}> = Fields & Attrs;\n";
946
+ code += "type XMLNamespaces = {[K in `$xmlns${string}`]?: string;};\n\n";
947
+ code += "type XMLLeaf<T extends string | number | boolean | undefined, Attrs = {}> = [keyof Attrs] extends [never] ? T : { $value?: T } & Attrs & XMLNamespaces;\n\n";
948
+ code += "type XMLBranch<Fields, Attrs = {}> = Fields & Attrs & XMLNamespaces;\n";
948
949
  code += "type canBeArray<T> = T | T[];\n\n";
949
950
  code += this.compileMessageInterfaces();
950
951
  if (this.portTypeRegistry.size > 0) {
@@ -1014,6 +1015,7 @@ ${indent} */
1014
1015
  code += ` ${attribute.name}?: ${attribute.type};
1015
1016
  `;
1016
1017
  }
1018
+ code += " [key: `$xmlns${string}`]: string | undefined;\n";
1017
1019
  code += "}\n\n";
1018
1020
  }
1019
1021
  return code;
@@ -1037,7 +1039,7 @@ function isXMLRecord(value) {
1037
1039
  return value !== null && typeof value === "object" && !Array.isArray(value);
1038
1040
  }
1039
1041
  function getXMLAttributes(value) {
1040
- return Object.entries(value).filter(([key]) => key.startsWith("$") && key !== "$value").map(([key, attrValue]) => {
1042
+ return Object.entries(value).filter(([key]) => key.startsWith("$") && key !== "$value" && key !== "$namespace").map(([key, attrValue]) => {
1041
1043
  const name = key.slice(1);
1042
1044
  if (!name || !/^[A-Za-z_][\w.:-]*$/.test(name)) {
1043
1045
  throw new Error(`Invalid XML attribute name: ${name}`);
@@ -1058,6 +1060,13 @@ function serializeXMLElement(tag, value) {
1058
1060
  if (Array.isArray(value)) {
1059
1061
  return value.map((item) => serializeXMLElement(tag, item)).join("");
1060
1062
  }
1063
+ const namespace = isXMLRecord(value) ? value.$namespace : void 0;
1064
+ if (namespace !== void 0) {
1065
+ if (typeof namespace !== "string" || !/^[A-Za-z_][\w.-]*$/.test(namespace)) {
1066
+ throw new Error(`Invalid XML namespace prefix: ${namespace}`);
1067
+ }
1068
+ tag = `${namespace}:${tag}`;
1069
+ }
1061
1070
  let attributes = "";
1062
1071
  let content = "";
1063
1072
  if (isXMLNode(value)) {
package/dist/index.d.cts CHANGED
@@ -139,6 +139,26 @@ declare class WSDLTypeGenerator {
139
139
  interface XMLBuilderOptions {
140
140
  declaration?: boolean;
141
141
  }
142
+ /**
143
+ * Technical XML properties used by XMLBuilder during serialization.
144
+ */
145
+ type XMLMetadata = {
146
+ /**
147
+ * Namespace prefix applied to the current XML element.
148
+ * The prefix is added to both opening and closing tags.
149
+ * Example: $namespace: 'soap' produces <soap:Element>.
150
+ */
151
+ $namespace?: string;
152
+ } & {
153
+ [K in `$xmlns${string}`]?: string;
154
+ };
155
+ /**
156
+ * Recursively extends an object type with XMLMetadata properties.
157
+ * Preserves primitive types and adds XMLMetadata to objects at every nesting level.
158
+ */
159
+ type WithXMLMetadata<T> = T extends readonly (infer U)[] ? WithXMLMetadata<U>[] : T extends object ? {
160
+ [K in keyof T]: WithXMLMetadata<T[K]>;
161
+ } & XMLMetadata : T;
142
162
  type XMLPrimitive = string | number | boolean | null | undefined;
143
163
  interface XMLBuilderNode {
144
164
  [key: string]: XMLPrimitive | XMLBuilderNode | XMLBuilderNode[] | undefined;
@@ -146,7 +166,7 @@ interface XMLBuilderNode {
146
166
  type XMLBuilderData = Record<string, XMLBuilderNode | XMLPrimitive | XMLBuilderNode[]>;
147
167
  declare class XMLBuilder<T extends object = Record<string, any>> {
148
168
  private $input;
149
- constructor(obj: T);
169
+ constructor(obj: WithXMLMetadata<T>);
150
170
  toXML(config?: XMLBuilderOptions): string;
151
171
  }
152
172
 
package/dist/index.d.ts CHANGED
@@ -139,6 +139,26 @@ declare class WSDLTypeGenerator {
139
139
  interface XMLBuilderOptions {
140
140
  declaration?: boolean;
141
141
  }
142
+ /**
143
+ * Technical XML properties used by XMLBuilder during serialization.
144
+ */
145
+ type XMLMetadata = {
146
+ /**
147
+ * Namespace prefix applied to the current XML element.
148
+ * The prefix is added to both opening and closing tags.
149
+ * Example: $namespace: 'soap' produces <soap:Element>.
150
+ */
151
+ $namespace?: string;
152
+ } & {
153
+ [K in `$xmlns${string}`]?: string;
154
+ };
155
+ /**
156
+ * Recursively extends an object type with XMLMetadata properties.
157
+ * Preserves primitive types and adds XMLMetadata to objects at every nesting level.
158
+ */
159
+ type WithXMLMetadata<T> = T extends readonly (infer U)[] ? WithXMLMetadata<U>[] : T extends object ? {
160
+ [K in keyof T]: WithXMLMetadata<T[K]>;
161
+ } & XMLMetadata : T;
142
162
  type XMLPrimitive = string | number | boolean | null | undefined;
143
163
  interface XMLBuilderNode {
144
164
  [key: string]: XMLPrimitive | XMLBuilderNode | XMLBuilderNode[] | undefined;
@@ -146,7 +166,7 @@ interface XMLBuilderNode {
146
166
  type XMLBuilderData = Record<string, XMLBuilderNode | XMLPrimitive | XMLBuilderNode[]>;
147
167
  declare class XMLBuilder<T extends object = Record<string, any>> {
148
168
  private $input;
149
- constructor(obj: T);
169
+ constructor(obj: WithXMLMetadata<T>);
150
170
  toXML(config?: XMLBuilderOptions): string;
151
171
  }
152
172
 
package/dist/index.js CHANGED
@@ -905,8 +905,9 @@ ${indent} */
905
905
  */
906
906
  compileToTypeScript() {
907
907
  let code = "/**\n * \u0422\u0418\u041F\u042B \u0421\u0413\u0415\u041D\u0415\u0420\u0418\u0420\u041E\u0412\u0410\u041D\u042B \u0410\u0412\u0422\u041E\u041C\u0410\u0422\u0418\u0427\u0415\u0421\u041A\u0418 \u041D\u0410 \u041E\u0421\u041D\u041E\u0412\u0415 WSDL/XSD\n * \u041F\u041E\u0414 \u0410\u0420\u0425\u0418\u0422\u0415\u041A\u0422\u0423\u0420\u0423 \u0423\u041C\u041D\u042B\u0425 \u041F\u0420\u0418\u041C\u0418\u0422\u0418\u0412\u041E\u0412\n */\n\n";
908
- code += "type XMLLeaf<T extends string | number | boolean | undefined, Attrs = {}> = Object extends Attrs ? T : (T & Attrs);\n\n";
909
- code += "type XMLBranch<Fields, Attrs = {}> = Fields & Attrs;\n";
908
+ code += "type XMLNamespaces = {[K in `$xmlns${string}`]?: string;};\n\n";
909
+ code += "type XMLLeaf<T extends string | number | boolean | undefined, Attrs = {}> = [keyof Attrs] extends [never] ? T : { $value?: T } & Attrs & XMLNamespaces;\n\n";
910
+ code += "type XMLBranch<Fields, Attrs = {}> = Fields & Attrs & XMLNamespaces;\n";
910
911
  code += "type canBeArray<T> = T | T[];\n\n";
911
912
  code += this.compileMessageInterfaces();
912
913
  if (this.portTypeRegistry.size > 0) {
@@ -976,6 +977,7 @@ ${indent} */
976
977
  code += ` ${attribute.name}?: ${attribute.type};
977
978
  `;
978
979
  }
980
+ code += " [key: `$xmlns${string}`]: string | undefined;\n";
979
981
  code += "}\n\n";
980
982
  }
981
983
  return code;
@@ -999,7 +1001,7 @@ function isXMLRecord(value) {
999
1001
  return value !== null && typeof value === "object" && !Array.isArray(value);
1000
1002
  }
1001
1003
  function getXMLAttributes(value) {
1002
- return Object.entries(value).filter(([key]) => key.startsWith("$") && key !== "$value").map(([key, attrValue]) => {
1004
+ return Object.entries(value).filter(([key]) => key.startsWith("$") && key !== "$value" && key !== "$namespace").map(([key, attrValue]) => {
1003
1005
  const name = key.slice(1);
1004
1006
  if (!name || !/^[A-Za-z_][\w.:-]*$/.test(name)) {
1005
1007
  throw new Error(`Invalid XML attribute name: ${name}`);
@@ -1020,6 +1022,13 @@ function serializeXMLElement(tag, value) {
1020
1022
  if (Array.isArray(value)) {
1021
1023
  return value.map((item) => serializeXMLElement(tag, item)).join("");
1022
1024
  }
1025
+ const namespace = isXMLRecord(value) ? value.$namespace : void 0;
1026
+ if (namespace !== void 0) {
1027
+ if (typeof namespace !== "string" || !/^[A-Za-z_][\w.-]*$/.test(namespace)) {
1028
+ throw new Error(`Invalid XML namespace prefix: ${namespace}`);
1029
+ }
1030
+ tag = `${namespace}:${tag}`;
1031
+ }
1023
1032
  let attributes = "";
1024
1033
  let content = "";
1025
1034
  if (isXMLNode(value)) {
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "engines": {
17
17
  "node": ">=20"
18
18
  },
19
- "version": "1.0.0",
19
+ "version": "1.0.2",
20
20
  "private": false,
21
21
  "type": "module",
22
22
  "bin": {
@@ -59,4 +59,4 @@
59
59
  "test:verbose": "vitest",
60
60
  "test:coverage": "vitest run --silent --coverage"
61
61
  }
62
- }
62
+ }