@vaadin/hilla-models 24.8.13 → 24.8.15
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/builders.js.map +1 -1
- package/package.json +2 -2
package/builders.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builders.js","sourceRoot":"","sources":["src/builders.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,aAAa,EACb,IAAI,EACJ,KAAK,EACL,KAAK,EACL,MAAM,GAKP,MAAM,YAAY,CAAC;AAEpB,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC;AAS1C,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;AAgC/B,MAAM,OAAO,gBAAgB;IAKR,CAAC,MAAM,CAAC,CAAe;IAO1C,YAAY,IAAW,EAAE,oBAAiD;QACxE,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAE5B,IAAI,oBAAoB,EAAE,CAAC;YACzB,IAAI,CAAC,oBAAoB,CAAC,oBAAoB,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAQD,IAAI,CAAC,KAAoB;QACvB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC9B,OAAO,IAAI,CAAC;IACd,CAAC;IAeD,MAAM,CACJ,GAAO,EACP,KAAkC;QAElC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QACzC,OAAO,IAAW,CAAC;IACrB,CAAC;IAWD,oBAAoB,CAAC,oBAAiD;QACpE,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE;YACzB,GAAG;gBACD,OAAO,oBAAoB,CAAC,IAAI,CAAC,CAAC;YACpC,CAAC;SACF,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IAUD,IAAI,CAAC,IAAY;QACf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KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAClD;qBACE,MAAM,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;qBAC5B,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;qBAC/B,MAAM,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;qBACvC,KAAK,EAAE,CAAC;gBAEX,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YACpB,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,IAAW,CAAC;IACrB,CAAC;CAQF","sourcesContent":["import type { EmptyObject } from 'type-fest';\nimport type { ObjectModel } from './core';\nimport {\n $defaultValue,\n $key,\n $meta,\n $name,\n $owner,\n type AnyObject,\n type DefaultValueProvider,\n type Model,\n type ModelMetadata,\n} from './model.js';\n\nconst { create, defineProperty } = Object;\n\n/**\n * The options for creating the model property.\n */\nexport type ModelBuilderPropertyOptions = Readonly<{\n meta?: ModelMetadata;\n}>;\n\nconst $model = Symbol('model');\n\n/**\n * The flags for the model constructor that allow to determine specific characteristics of the model.\n */\nexport type Flags = {\n /**\n * Defines if the model is named.\n */\n named: boolean;\n\n /**\n * The keys of the self-referencing properties.\n *\n * @remarks\n * The problem of self-reference models is that they cannot have intermediate\n * type because during the property definition, the model itself is in the\n * middle of construction. That's why we define the specific type-only flag\n * that allows us to know which model properties are self-referenced. We can\n * safely set it in the end of building using this flag.\n */\n selfRefKeys: keyof any;\n};\n\n/**\n * A builder class for creating all basic models.\n *\n * @typeParam V - The final value type of the model.\n * @typeParam EX - The extra properties of the model.\n * @typeParam F - The flags for the model constructor that allow to determine\n * specific characteristics of the model.\n */\nexport class CoreModelBuilder<\n V,\n EX extends AnyObject = EmptyObject,\n F extends Flags = { named: false; selfRefKeys: never },\n> {\n protected readonly [$model]: Model<V, EX>;\n\n /**\n * @param base - The base model to extend.\n * @param defaultValueProvider - The function that provides the default value\n * for the model.\n */\n constructor(base: Model, defaultValueProvider?: (model: Model<V, EX>) => V) {\n this[$model] = create(base);\n\n if (defaultValueProvider) {\n this.defaultValueProvider(defaultValueProvider);\n }\n }\n\n /**\n * Appends metadata to the model.\n *\n * @param value - The metadata to append.\n * @returns The current builder instance.\n */\n meta(value: ModelMetadata): this {\n this.define($meta, { value });\n return this;\n }\n\n /**\n * Defines a new property on the model. The property serves the purposes of\n * storing the extra data for specific types of models.\n *\n * @remarks\n * The key of the property should be a symbol to avoid conflicts with\n * properties defined via\n * {@link ObjectModelBuilder}.\n *\n * @param key - The key of the property.\n * @param value - The descriptor of the property.\n * @returns The current builder instance.\n */\n define<DK extends symbol, DV>(\n key: DK,\n value: TypedPropertyDescriptor<DV>,\n ): CoreModelBuilder<V, EX & Readonly<Record<DK, DV>>, F> {\n defineProperty(this[$model], key, value);\n return this as any;\n }\n\n /**\n * Sets the default value provider for the model. This is an alternative way\n * to provide the default value for the model if for some reason using the\n * constructor parameter is undesired.\n *\n * @param defaultValueProvider - The function that provides the default value\n * for the model.\n * @returns The current builder instance.\n */\n defaultValueProvider(defaultValueProvider: DefaultValueProvider<V, EX>): this {\n this.define($defaultValue, {\n get(this: Model<V, EX>) {\n return defaultValueProvider(this);\n },\n });\n return this;\n }\n\n /**\n * Sets the name of the model. The name is used for debugging purposes and is\n * displayed in the string representation. Setting the name is required;\n * otherwise, the {@link CoreModelBuilder.build} method won't be available.\n *\n * @param name - The name of the model.\n * @returns The current builder instance.\n */\n name(name: string): CoreModelBuilder<V, EX, { named: true; selfRefKeys: F['selfRefKeys'] }> {\n return this.define($name, { value: name }) as any;\n }\n\n /**\n * Builds the model. On the typing level, it checks if all the model parts are\n * set correctly, and raises an error if not.\n *\n * @returns The model.\n */\n build(this: F['named'] extends true ? this : never): Model<V, EX> {\n return this[$model];\n }\n}\n\n/**\n * A registry for the property models of the object model. Since the property\n * registration is lazy, we cannot store the property models directly on the\n * object model, so the registry plays a role of a private storage for them.\n *\n * @internal\n */\nconst propertyRegistry = new WeakMap<Model, Record<string, Model>>();\n\n/**\n * A builder class for creating object models.\n *\n * @typeParam V - The final value type of the model.\n * @typeParam CV - The current value type of the model. It changes as the model\n * is being built and defines if the\n * {@link ObjectModelBuilder.build} method can be called.\n * @typeParam EX - The extra properties of the model.\n * @typeParam F - The flags for the model constructor that allow to determine\n * specific characteristics of the model.\n */\nexport class ObjectModelBuilder<\n V extends AnyObject,\n CV extends AnyObject = EmptyObject,\n EX extends AnyObject = EmptyObject,\n F extends Flags = { named: false; selfRefKeys: never },\n> extends CoreModelBuilder<V, EX, F> {\n constructor(base: Model) {\n super(base, (m) => {\n const result = create(null);\n\n // eslint-disable-next-line no-restricted-syntax\n for (const key in m) {\n defineProperty(result, key, {\n enumerable: true,\n get: () => (m[key as keyof Model<V, EX>] as Model)[$defaultValue],\n });\n }\n\n return result;\n });\n }\n\n /**\n * The method that should follow the {@link m.extend} method. It allows to\n * declare the extension for the model and properly name it.\n *\n * @param name - The name of the model.\n */\n object<NV extends AnyObject>(\n this: F['named'] extends false ? this : never,\n name: string,\n ): ObjectModelBuilder<NV & V, CV, EX, { named: true; selfRefKeys: F['selfRefKeys'] }> {\n return this.name(name) as any;\n }\n\n /**\n * {@inheritDoc CoreModelBuilder.define}\n */\n declare ['define']: <DK extends symbol, DV>(\n key: DK,\n value: TypedPropertyDescriptor<DV>,\n ) => ObjectModelBuilder<V, CV, EX & Readonly<Record<DK, DV>>, F>;\n\n /**\n * {@inheritDoc CoreModelBuilder.meta}\n */\n declare ['meta']: (value: ModelMetadata) => this;\n\n /**\n * Defines a new model property on the model. Unlike the\n * {@link ObjectModelBuilder.define}, this property is public and allows the\n * user to interact with the model data structure. It also updates the current\n * value type of the model to make it closer to the final value type.\n *\n * @param key - The key of the property.\n * @param model - The model of the property value. You can also provide a\n * function that produces the model based on the current model.\n * @param options - Additional options for the property.\n *\n * @returns The current builder instance updated with the new property type.\n * In case there is a self-referencing property, the {@link Flags.selfRefKeys}\n * flag for the specific property is set.\n */\n property<PK extends string & keyof V, EXK extends AnyObject = EmptyObject>(\n key: PK,\n model: Model<V[PK], EXK> | ((model: Model<V, EX & Readonly<Record<PK, Model<V[PK], EXK>>>>) => Model<V[PK], EXK>),\n options?: ModelBuilderPropertyOptions,\n ): // It is a workaround for the self-referencing models.\n // If the type of the model property is not the model itself,\n Extract<V[PK], V> extends never\n ? // Then we simply extend the model with the property, and update the\n // current value type of the model.\n ObjectModelBuilder<V, CV & Readonly<Record<PK, V[PK]>>, EX & Readonly<Record<PK, Model<V[PK], EXK>>>, F>\n : // Otherwise, we set a flag of the model that it contains a self-reference\n // property.\n ObjectModelBuilder<\n V,\n CV & Readonly<Record<PK, V[PK]>>,\n EX,\n {\n // Just inheriting the current flag.\n named: F['named'];\n // Adding the property name to all existing self-referencing\n // properties.\n selfRefKeys: F['selfRefKeys'] | PK;\n }\n > {\n defineProperty(this[$model], key, {\n enumerable: true,\n get(this: Model<V, EX & Readonly<Record<PK, Model<V[PK], EXK>>>>) {\n if (!propertyRegistry.has(this)) {\n propertyRegistry.set(this, {});\n }\n\n const props = propertyRegistry.get(this)!;\n\n props[key] ??= new CoreModelBuilder<V[PK], EXK, { named: true; selfRefKeys: never }>(\n typeof model === 'function' ? model(this) : model,\n )\n .define($key, { value: key })\n .define($owner, { value: this })\n .define($meta, { value: options?.meta })\n .build();\n\n return props[key];\n },\n });\n\n return this as any;\n }\n\n /**\n * {@inheritDoc CoreModelBuilder.build}\n */\n declare build: (\n this: F['named'] extends true ? (CV extends V ? this : never) : never,\n ) => ObjectModel<V, EX, F['selfRefKeys']>;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"builders.js","sourceRoot":"","sources":["src/builders.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,aAAa,EACb,IAAI,EACJ,KAAK,EACL,KAAK,EACL,MAAM,GAKP,MAAM,YAAY,CAAC;AAEpB,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC;AAS1C,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Content":["import type { EmptyObject } from 'type-fest';\nimport type { ObjectModel } from './core';\nimport {\n $defaultValue,\n $key,\n $meta,\n $name,\n $owner,\n type AnyObject,\n type DefaultValueProvider,\n type Model,\n type ModelMetadata,\n} from './model.js';\n\nconst { create, defineProperty } = Object;\n\n/**\n * The options for creating the model property.\n */\nexport type ModelBuilderPropertyOptions = Readonly<{\n meta?: ModelMetadata;\n}>;\n\nconst $model = Symbol('model');\n\n/**\n * The flags for the model constructor that allow to determine specific characteristics of the model.\n */\nexport type Flags = {\n /**\n * Defines if the model is named.\n */\n named: boolean;\n\n /**\n * The keys of the self-referencing properties.\n *\n * @remarks\n * The problem of self-reference models is that they cannot have intermediate\n * type because during the property definition, the model itself is in the\n * middle of construction. That's why we define the specific type-only flag\n * that allows us to know which model properties are self-referenced. We can\n * safely set it in the end of building using this flag.\n */\n selfRefKeys: keyof any;\n};\n\n/**\n * A builder class for creating all basic models.\n *\n * @typeParam V - The final value type of the model.\n * @typeParam EX - The extra properties of the model.\n * @typeParam F - The flags for the model constructor that allow to determine\n * specific characteristics of the model.\n */\nexport class CoreModelBuilder<\n V,\n EX extends AnyObject = EmptyObject,\n F extends Flags = { named: false; selfRefKeys: never },\n> {\n protected readonly [$model]: Model<V, EX>;\n\n /**\n * @param base - The base model to extend.\n * @param defaultValueProvider - The function that provides the default value\n * for the model.\n */\n constructor(base: Model, defaultValueProvider?: (model: Model<V, EX>) => V) {\n this[$model] = create(base);\n\n if (defaultValueProvider) {\n this.defaultValueProvider(defaultValueProvider);\n }\n }\n\n /**\n * Appends metadata to the model.\n *\n * @param value - The metadata to append.\n * @returns The current builder instance.\n */\n meta(value: ModelMetadata): this {\n this.define($meta, { value });\n return this;\n }\n\n /**\n * Defines a new property on the model. The property serves the purposes of\n * storing the extra data for specific types of models.\n *\n * @remarks\n * The key of the property should be a symbol to avoid conflicts with\n * properties defined via\n * {@link ObjectModelBuilder}.\n *\n * @param key - The key of the property.\n * @param value - The descriptor of the property.\n * @returns The current builder instance.\n */\n define<DK extends symbol, DV>(\n key: DK,\n value: TypedPropertyDescriptor<DV>,\n ): CoreModelBuilder<V, EX & Readonly<Record<DK, DV>>, F> {\n defineProperty(this[$model], key, value);\n return this as any;\n }\n\n /**\n * Sets the default value provider for the model. This is an alternative way\n * to provide the default value for the model if for some reason using the\n * constructor parameter is undesired.\n *\n * @param defaultValueProvider - The function that provides the default value\n * for the model.\n * @returns The current builder instance.\n */\n defaultValueProvider(defaultValueProvider: DefaultValueProvider<V, EX>): this {\n this.define($defaultValue, {\n get(this: Model<V, EX>) {\n return defaultValueProvider(this);\n },\n });\n return this;\n }\n\n /**\n * Sets the name of the model. The name is used for debugging purposes and is\n * displayed in the string representation. Setting the name is required;\n * otherwise, the {@link CoreModelBuilder.build} method won't be available.\n *\n * @param name - The name of the model.\n * @returns The current builder instance.\n */\n name(name: string): CoreModelBuilder<V, EX, { named: true; selfRefKeys: F['selfRefKeys'] }> {\n return this.define($name, { value: name }) as any;\n }\n\n /**\n * Builds the model. On the typing level, it checks if all the model parts are\n * set correctly, and raises an error if not.\n *\n * @returns The model.\n */\n build(this: F['named'] extends true ? this : never): Model<V, EX> {\n return this[$model];\n }\n}\n\n/**\n * A registry for the property models of the object model. Since the property\n * registration is lazy, we cannot store the property models directly on the\n * object model, so the registry plays a role of a private storage for them.\n *\n * @internal\n */\nconst propertyRegistry = new WeakMap<Model, Record<string, Model>>();\n\n/**\n * A builder class for creating object models.\n *\n * @typeParam V - The final value type of the model.\n * @typeParam CV - The current value type of the model. It changes as the model\n * is being built and defines if the\n * {@link ObjectModelBuilder.build} method can be called.\n * @typeParam EX - The extra properties of the model.\n * @typeParam F - The flags for the model constructor that allow to determine\n * specific characteristics of the model.\n */\nexport class ObjectModelBuilder<\n V extends AnyObject,\n CV extends AnyObject = EmptyObject,\n EX extends AnyObject = EmptyObject,\n F extends Flags = { named: false; selfRefKeys: never },\n> extends CoreModelBuilder<V, EX, F> {\n constructor(base: Model) {\n super(base, (m) => {\n const result = create(null);\n\n // eslint-disable-next-line no-restricted-syntax\n for (const key in m) {\n defineProperty(result, key, {\n enumerable: true,\n get: () => (m[key as keyof Model<V, EX>] as Model)[$defaultValue],\n });\n }\n\n return result;\n });\n }\n\n /**\n * The method that should follow the {@link m.extend} method. It allows to\n * declare the extension for the model and properly name it.\n *\n * @param name - The name of the model.\n */\n object<NV extends AnyObject>(\n this: F['named'] extends false ? this : never,\n name: string,\n ): ObjectModelBuilder<NV & V, CV, EX, { named: true; selfRefKeys: F['selfRefKeys'] }> {\n return this.name(name) as any;\n }\n\n /**\n * {@inheritDoc CoreModelBuilder.define}\n */\n declare ['define']: <DK extends symbol, DV>(\n key: DK,\n value: TypedPropertyDescriptor<DV>,\n ) => ObjectModelBuilder<V, CV, EX & Readonly<Record<DK, DV>>, F>;\n\n /**\n * {@inheritDoc CoreModelBuilder.meta}\n */\n declare ['meta']: (value: ModelMetadata) => this;\n\n /**\n * Defines a new model property on the model. Unlike the\n * {@link ObjectModelBuilder.define}, this property is public and allows the\n * user to interact with the model data structure. It also updates the current\n * value type of the model to make it closer to the final value type.\n *\n * @param key - The key of the property.\n * @param model - The model of the property value. You can also provide a\n * function that produces the model based on the current model.\n * @param options - Additional options for the property.\n *\n * @returns The current builder instance updated with the new property type.\n * In case there is a self-referencing property, the {@link Flags.selfRefKeys}\n * flag for the specific property is set.\n */\n property<PK extends string & keyof V, EXK extends AnyObject = EmptyObject>(\n key: PK,\n model: Model<V[PK], EXK> | ((model: Model<V, EX & Readonly<Record<PK, Model<V[PK], EXK>>>>) => Model<V[PK], EXK>),\n options?: ModelBuilderPropertyOptions,\n ): // It is a workaround for the self-referencing models.\n // If the type of the model property is not the model itself,\n Extract<V[PK], V> extends never\n ? // Then we simply extend the model with the property, and update the\n // current value type of the model.\n ObjectModelBuilder<V, CV & Readonly<Record<PK, V[PK]>>, EX & Readonly<Record<PK, Model<V[PK], EXK>>>, F>\n : // Otherwise, we set a flag of the model that it contains a self-reference\n // property.\n ObjectModelBuilder<\n V,\n CV & Readonly<Record<PK, V[PK]>>,\n EX,\n {\n // Just inheriting the current flag.\n named: F['named'];\n // Adding the property name to all existing self-referencing\n // properties.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents\n selfRefKeys: F['selfRefKeys'] | PK;\n }\n > {\n defineProperty(this[$model], key, {\n enumerable: true,\n get(this: Model<V, EX & Readonly<Record<PK, Model<V[PK], EXK>>>>) {\n if (!propertyRegistry.has(this)) {\n propertyRegistry.set(this, {});\n }\n\n const props = propertyRegistry.get(this)!;\n\n props[key] ??= new CoreModelBuilder<V[PK], EXK, { named: true; selfRefKeys: never }>(\n typeof model === 'function' ? model(this) : model,\n )\n .define($key, { value: key })\n .define($owner, { value: this })\n .define($meta, { value: options?.meta })\n .build();\n\n return props[key];\n },\n });\n\n return this as any;\n }\n\n /**\n * {@inheritDoc CoreModelBuilder.build}\n */\n declare build: (\n this: F['named'] extends true ? (CV extends V ? this : never) : never,\n ) => ObjectModel<V, EX, F['selfRefKeys']>;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/hilla-models",
|
|
3
|
-
"version": "24.8.
|
|
3
|
+
"version": "24.8.15",
|
|
4
4
|
"description": "Generative form models for Hilla",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@vaadin/hilla-lit-form": "24.8.
|
|
46
|
+
"@vaadin/hilla-lit-form": "24.8.15"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"react": "18 || 19",
|