xml-model 2.0.0-beta.1 → 2.0.0-beta.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.
@@ -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,4 +1,3 @@
1
- export { model, isModel } from './model';
2
- export type { ModelConstructor } from './model';
1
+ export * from './model';
3
2
  export * from './xml';
4
3
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,21 +1,7 @@
1
- import { isModel, model } from "./model.js";
2
- import "./xml/index.js";
3
- import { XML, ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode } from "./xml/xml-js.js";
4
- import { normalizeCodecOptions, registerDefault } from "./xml/codec.js";
1
+ import { DATA, isModel, model } from "./model.js";
2
+ import XML, { ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode } from "./xml/xml-js.js";
5
3
  import { xml } from "./xml/schema-meta.js";
4
+ import { normalizeCodecOptions, registerDefault } from "./xml/codec.js";
6
5
  import { xmlModel } from "./xml/model.js";
7
- export {
8
- XML,
9
- ZXMLCommentNode,
10
- ZXMLElementNode,
11
- ZXMLNode,
12
- ZXMLRoot,
13
- ZXMLTextNode,
14
- isModel,
15
- model,
16
- normalizeCodecOptions,
17
- registerDefault,
18
- xml,
19
- xmlModel
20
- };
21
- //# sourceMappingURL=index.js.map
6
+ import "./xml/index.js";
7
+ export { DATA, XML, ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode, isModel, model, normalizeCodecOptions, registerDefault, xml, xmlModel };
package/dist/model.js CHANGED
@@ -1,69 +1,75 @@
1
1
  import { z } from "zod";
2
- const schemaSymbol = /* @__PURE__ */ Symbol("model:schema");
3
- const MODEL_MARKER = /* @__PURE__ */ Symbol("model:marker");
4
- const DATA = /* @__PURE__ */ Symbol("model:data");
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). */
5
9
  function isModel(cls) {
6
- return typeof cls === "function" && MODEL_MARKER in cls;
10
+ return typeof cls === "function" && MODEL_MARKER in cls;
7
11
  }
8
12
  function defineFieldAccessors(proto, keys) {
9
- for (const key of keys) {
10
- Object.defineProperty(proto, key, {
11
- get() {
12
- return this[DATA][key];
13
- },
14
- set(v) {
15
- this[DATA][key] = v;
16
- },
17
- enumerable: true,
18
- configurable: true
19
- });
20
- }
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
+ });
21
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
+ */
22
33
  function model(schema) {
23
- class Base {
24
- static dataSchema = schema;
25
- static [MODEL_MARKER] = true;
26
- static schema() {
27
- if (!Object.prototype.hasOwnProperty.call(this, schemaSymbol)) {
28
- const codec = z.codec(this.dataSchema, z.instanceof(this), {
29
- decode: (data) => {
30
- return this.fromData(data);
31
- },
32
- encode: (instance) => {
33
- return instance[DATA];
34
- }
35
- });
36
- this[schemaSymbol] = codec;
37
- }
38
- return this[schemaSymbol];
39
- }
40
- static extend(extension, meta) {
41
- let extended = this.dataSchema.extend(extension);
42
- if (meta) extended = extended.meta(meta);
43
- const Child = class extends this {
44
- static dataSchema = extended;
45
- };
46
- defineFieldAccessors(Child.prototype, Object.keys(extension));
47
- return Child;
48
- }
49
- static fromData(data) {
50
- return new this(data);
51
- }
52
- static toData(instance) {
53
- const data = instance[DATA];
54
- if (!data) throw new Error("failed to retrieve instance data");
55
- return data;
56
- }
57
- constructor(data) {
58
- this[DATA] = data;
59
- }
60
- }
61
- defineFieldAccessors(Base.prototype, Object.keys(schema.def.shape));
62
- return Base;
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;
63
71
  }
64
- export {
65
- DATA,
66
- isModel,
67
- model
68
- };
69
- //# sourceMappingURL=model.js.map
72
+ //#endregion
73
+ export { DATA, isModel, model };
74
+
75
+ //# sourceMappingURL=model.js.map