@kaskad/definition 0.0.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 +7 -0
- package/esm2022/index.js +7 -0
- package/esm2022/index.js.map +1 -0
- package/esm2022/kaskad-definition.js +5 -0
- package/esm2022/kaskad-definition.js.map +1 -0
- package/esm2022/lib/definition-store.js +144 -0
- package/esm2022/lib/definition-store.js.map +1 -0
- package/esm2022/lib/types/command.js +1 -0
- package/esm2022/lib/types/command.js.map +1 -0
- package/esm2022/lib/types/component.js +1 -0
- package/esm2022/lib/types/component.js.map +1 -0
- package/esm2022/lib/types/shape.js +1 -0
- package/esm2022/lib/types/shape.js.map +1 -0
- package/esm2022/lib/types/variant-shape.js +1 -0
- package/esm2022/lib/types/variant-shape.js.map +1 -0
- package/esm2022/lib/util/memoize.js +15 -0
- package/esm2022/lib/util/memoize.js.map +1 -0
- package/index.d.ts +6 -0
- package/kaskad-definition.d.ts +5 -0
- package/lib/definition-store.d.ts +40 -0
- package/lib/types/command.d.ts +7 -0
- package/lib/types/component.d.ts +6 -0
- package/lib/types/shape.d.ts +4 -0
- package/lib/types/variant-shape.d.ts +5 -0
- package/lib/util/memoize.d.ts +1 -0
- package/package.json +27 -0
package/README.md
ADDED
package/esm2022/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './lib/types/shape';
|
|
2
|
+
export * from './lib/types/variant-shape';
|
|
3
|
+
export * from './lib/types/command';
|
|
4
|
+
export * from './lib/types/component';
|
|
5
|
+
export * from './lib/util/memoize';
|
|
6
|
+
export * from './lib/definition-store';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/definition/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC","sourcesContent":["export * from './lib/types/shape';\nexport * from './lib/types/variant-shape';\nexport * from './lib/types/command';\nexport * from './lib/types/component';\nexport * from './lib/util/memoize';\nexport * from './lib/definition-store';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kaskad-definition.js","sourceRoot":"","sources":["../../../../libs/definition/src/kaskad-definition.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,SAAS,CAAC","sourcesContent":["/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Delimiters } from '@kaskad/config';
|
|
2
|
+
import { findChildValueType } from '@kaskad/types';
|
|
3
|
+
import { memoize } from './util/memoize';
|
|
4
|
+
export class DefinitionStore {
|
|
5
|
+
static instance;
|
|
6
|
+
shapes = {};
|
|
7
|
+
variantShapes = {};
|
|
8
|
+
components = {};
|
|
9
|
+
commands = {};
|
|
10
|
+
constructor() {
|
|
11
|
+
this.getComponentTraits = memoize(this.getComponentTraits.bind(this));
|
|
12
|
+
this.getComputedComponentContract = memoize(this.getComputedComponentContract.bind(this));
|
|
13
|
+
}
|
|
14
|
+
static getInstance() {
|
|
15
|
+
if (!DefinitionStore.instance) {
|
|
16
|
+
DefinitionStore.instance = new DefinitionStore();
|
|
17
|
+
}
|
|
18
|
+
return DefinitionStore.instance;
|
|
19
|
+
}
|
|
20
|
+
static reset() {
|
|
21
|
+
DefinitionStore.instance = new DefinitionStore();
|
|
22
|
+
return DefinitionStore.instance;
|
|
23
|
+
}
|
|
24
|
+
setShape(shapeType, properties) {
|
|
25
|
+
this.shapes[shapeType] = {
|
|
26
|
+
properties,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
getShape(shapeType) {
|
|
30
|
+
const definition = this.shapes[shapeType];
|
|
31
|
+
if (!definition) {
|
|
32
|
+
throw new Error(`Shape "${shapeType}" is not registered.`);
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
type: 'object',
|
|
36
|
+
fields: definition.properties,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
setVariantShape(shapeType, commonProperties) {
|
|
40
|
+
this.variantShapes[shapeType] = {
|
|
41
|
+
commonProperties,
|
|
42
|
+
kinds: {},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
setVariantShapeKind(shapeType, kind, properties) {
|
|
46
|
+
const definition = this.variantShapes[shapeType];
|
|
47
|
+
if (!definition) {
|
|
48
|
+
throw new Error(`VariantShape "${shapeType}" is not registered`);
|
|
49
|
+
}
|
|
50
|
+
definition.kinds[kind] = properties;
|
|
51
|
+
}
|
|
52
|
+
getVariantShape(shapeType, kind) {
|
|
53
|
+
const definition = this.variantShapes[shapeType];
|
|
54
|
+
if (!definition) {
|
|
55
|
+
throw new Error(`VariantShape "${shapeType}" is not registered.`);
|
|
56
|
+
}
|
|
57
|
+
const kindProperties = definition.kinds[kind];
|
|
58
|
+
return {
|
|
59
|
+
type: 'object',
|
|
60
|
+
fields: {
|
|
61
|
+
...definition.commonProperties,
|
|
62
|
+
...kindProperties,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
setComponent(componentType, componentDefinition) {
|
|
67
|
+
this.components[componentType] = componentDefinition;
|
|
68
|
+
}
|
|
69
|
+
setComponents(componentDefinitions) {
|
|
70
|
+
for (const [componentType, componentDefinition] of Object.entries(componentDefinitions)) {
|
|
71
|
+
this.setComponent(componentType, componentDefinition);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getComponent(componentType) {
|
|
75
|
+
const componentDefinition = this.components[componentType];
|
|
76
|
+
if (!componentDefinition) {
|
|
77
|
+
throw new Error(`Component with type "${componentType}" is not found.`);
|
|
78
|
+
}
|
|
79
|
+
return componentDefinition;
|
|
80
|
+
}
|
|
81
|
+
getComponentTraits(componentType) {
|
|
82
|
+
const description = this.getComponent(componentType);
|
|
83
|
+
const traits = description.traits || [];
|
|
84
|
+
const superTraits = traits.flatMap((trait) => this.getComponentTraits(trait));
|
|
85
|
+
const result = [...superTraits, ...traits, componentType];
|
|
86
|
+
return [...new Set(result)];
|
|
87
|
+
}
|
|
88
|
+
getComputedComponentContract(componentType) {
|
|
89
|
+
const traits = this.getComponentTraits(componentType);
|
|
90
|
+
const contracts = traits.map((trait) => this.getComponent(trait).properties);
|
|
91
|
+
const propMap = {};
|
|
92
|
+
for (const contract of contracts) {
|
|
93
|
+
for (const [key, prop] of Object.entries(contract)) {
|
|
94
|
+
if (!propMap[key]) {
|
|
95
|
+
propMap[key] = prop;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const existingProp = propMap[key];
|
|
99
|
+
if (existingProp) {
|
|
100
|
+
propMap[key] = { ...existingProp, ...prop };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const [key, prop] of Object.entries(propMap)) {
|
|
105
|
+
if (!prop.valueType) {
|
|
106
|
+
throw new Error(`Property "${key}" in component "${componentType}" has no valueType.`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return structuredClone(propMap);
|
|
110
|
+
}
|
|
111
|
+
getNodeValueType(componentType, path) {
|
|
112
|
+
const [node, ...rest] = path.split(Delimiters.NodePath);
|
|
113
|
+
const description = this.getComputedComponentContract(componentType)[node];
|
|
114
|
+
if (!description) {
|
|
115
|
+
throw new Error(`Property "${path}" is not found in component "${componentType}".`);
|
|
116
|
+
}
|
|
117
|
+
return findChildValueType(description.valueType, rest);
|
|
118
|
+
}
|
|
119
|
+
hasComponentWithType(componentType) {
|
|
120
|
+
return !!this.components[componentType];
|
|
121
|
+
}
|
|
122
|
+
getComponentTypes() {
|
|
123
|
+
return Object.keys(this.components);
|
|
124
|
+
}
|
|
125
|
+
setCommands(definitions) {
|
|
126
|
+
for (const [commandType, definition] of Object.entries(definitions)) {
|
|
127
|
+
this.setCommand(commandType, definition);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
setCommand(commandType, definition) {
|
|
131
|
+
this.commands[commandType] = definition;
|
|
132
|
+
}
|
|
133
|
+
getCommand(commandType) {
|
|
134
|
+
const commandDefinition = this.commands[commandType];
|
|
135
|
+
if (!commandDefinition) {
|
|
136
|
+
throw new Error(`Command with type "${commandType}" is not found.`);
|
|
137
|
+
}
|
|
138
|
+
return commandDefinition;
|
|
139
|
+
}
|
|
140
|
+
getCommandNames() {
|
|
141
|
+
return Object.keys(this.commands);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=definition-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definition-store.js","sourceRoot":"","sources":["../../../../../libs/definition/src/lib/definition-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAA0C,MAAM,eAAe,CAAC;AAM3F,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,MAAM,OAAO,eAAe;IAClB,MAAM,CAAC,QAAQ,CAAkB;IACzC,MAAM,GAA6C,EAAE,CAAC;IACtD,aAAa,GAAoD,EAAE,CAAC;IACpE,UAAU,GAAqD,EAAE,CAAC;IAClE,QAAQ,GAAiD,EAAE,CAAC;IAE5D;QACE,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACtE,IAAI,CAAC,4BAA4B,GAAG,OAAO,CAAC,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5F,CAAC;IAED,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,CAAC;YAC9B,eAAe,CAAC,QAAQ,GAAG,IAAI,eAAe,EAAE,CA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Schema, ObjectValueType, ValueType } from '@kaskad/types';\n\nimport { CommandDefinition } from './types/command';\nimport { ComponentDefinition } from './types/component';\nimport { ShapeDefinition } from './types/shape';\nimport { VariantShapeDefinition } from './types/variant-shape';\nimport { memoize } from './util/memoize';\n\nexport class DefinitionStore {\n private static instance: DefinitionStore;\n shapes: { [shapeType: string]: ShapeDefinition } = {};\n variantShapes: { [shapeType: string]: VariantShapeDefinition } = {};\n components: { [componentType: string]: ComponentDefinition } = {};\n commands: { [commandType: string]: CommandDefinition } = {};\n\n private constructor() {\n this.getComponentTraits = memoize(this.getComponentTraits.bind(this));\n this.getComputedComponentContract = memoize(this.getComputedComponentContract.bind(this));\n }\n\n static getInstance(): DefinitionStore {\n if (!DefinitionStore.instance) {\n DefinitionStore.instance = new DefinitionStore();\n }\n return DefinitionStore.instance;\n }\n\n static reset() {\n DefinitionStore.instance = new DefinitionStore();\n\n return DefinitionStore.instance;\n }\n\n setShape(shapeType: string, properties: Record<string, ValueType>) {\n this.shapes[shapeType] = {\n properties,\n };\n }\n\n getShape(shapeType: string): ObjectValueType {\n const definition = this.shapes[shapeType];\n if (!definition) {\n throw new Error(`Shape \"${shapeType}\" is not registered.`);\n }\n\n return {\n type: 'object',\n fields: definition.properties,\n };\n }\n\n setVariantShape(shapeType: string, commonProperties: Record<string, ValueType>) {\n this.variantShapes[shapeType] = {\n commonProperties,\n kinds: {},\n };\n }\n\n setVariantShapeKind(shapeType: string, kind: string, properties: Record<string, ValueType>): void {\n const definition = this.variantShapes[shapeType];\n if (!definition) {\n throw new Error(`VariantShape \"${shapeType}\" is not registered`);\n }\n definition.kinds[kind] = properties;\n }\n\n getVariantShape(shapeType: string, kind: string): ObjectValueType {\n const definition = this.variantShapes[shapeType];\n if (!definition) {\n throw new Error(`VariantShape \"${shapeType}\" is not registered.`);\n }\n\n const kindProperties = definition.kinds[kind];\n\n return {\n type: 'object',\n fields: {\n ...definition.commonProperties,\n ...kindProperties,\n },\n };\n }\n\n setComponent(componentType: string, componentDefinition: ComponentDefinition): void {\n this.components[componentType] = componentDefinition;\n }\n\n setComponents(componentDefinitions: Record<string, ComponentDefinition>): void {\n for (const [componentType, componentDefinition] of Object.entries(componentDefinitions)) {\n this.setComponent(componentType, componentDefinition);\n }\n }\n\n getComponent(componentType: string): ComponentDefinition {\n const componentDefinition = this.components[componentType];\n if (!componentDefinition) {\n throw new Error(`Component with type \"${componentType}\" is not found.`);\n }\n return componentDefinition;\n }\n\n getComponentTraits(componentType: string): string[] {\n const description = this.getComponent(componentType);\n const traits = description.traits || [];\n const superTraits = traits.flatMap((trait) => this.getComponentTraits(trait));\n const result = [...superTraits, ...traits, componentType];\n\n return [...new Set(result)];\n }\n\n getComputedComponentContract(componentType: string): Record<string, NodeSchema> {\n const traits = this.getComponentTraits(componentType);\n const contracts = traits.map((trait) => this.getComponent(trait).properties);\n\n const propMap: Record<string, NodeSchema> = {};\n\n for (const contract of contracts) {\n for (const [key, prop] of Object.entries(contract)) {\n if (!propMap[key]) {\n propMap[key] = prop;\n continue;\n }\n\n const existingProp = propMap[key];\n if (existingProp) {\n propMap[key] = { ...existingProp, ...prop };\n }\n }\n }\n\n for (const [key, prop] of Object.entries(propMap)) {\n if (!prop.valueType) {\n throw new Error(`Property \"${key}\" in component \"${componentType}\" has no valueType.`);\n }\n }\n\n return structuredClone(propMap);\n }\n\n getNodeValueType(componentType: string, path: string): ValueType {\n const [node, ...rest] = path.split(Delimiters.NodePath);\n\n const description = this.getComputedComponentContract(componentType)[node];\n if (!description) {\n throw new Error(`Property \"${path}\" is not found in component \"${componentType}\".`);\n }\n return findChildValueType(description.valueType, rest);\n }\n\n hasComponentWithType(componentType: string): boolean {\n return !!this.components[componentType];\n }\n\n getComponentTypes(): string[] {\n return Object.keys(this.components);\n }\n\n setCommands(definitions: Record<string, CommandDefinition>): void {\n for (const [commandType, definition] of Object.entries(definitions)) {\n this.setCommand(commandType, definition);\n }\n }\n\n setCommand(commandType: string, definition: CommandDefinition): void {\n this.commands[commandType] = definition;\n }\n\n getCommand(commandType: string): CommandDefinition {\n const commandDefinition = this.commands[commandType];\n if (!commandDefinition) {\n throw new Error(`Command with type \"${commandType}\" is not found.`);\n }\n\n return commandDefinition;\n }\n\n getCommandNames(): string[] {\n return Object.keys(this.commands);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command.js","sourceRoot":"","sources":["../../../../../../libs/definition/src/lib/types/command.ts"],"names":[],"mappings":"","sourcesContent":["export type CommandContext = {\n componentId: string;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type CommandExecute<TResult> = (ctx: CommandContext, ...params: any[]) => TResult | Promise<TResult>;\n\nexport type CommandDefinition = {\n execute: CommandExecute<unknown>;\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=component.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component.js","sourceRoot":"","sources":["../../../../../../libs/definition/src/lib/types/component.ts"],"names":[],"mappings":"","sourcesContent":["import { NodeSchema } from '@kaskad/types';\n\nexport interface ComponentDefinition {\n readonly traits?: string[];\n readonly properties: Record<string, NodeSchema>;\n readonly defaultSlot?: string;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.js","sourceRoot":"","sources":["../../../../../../libs/definition/src/lib/types/shape.ts"],"names":[],"mappings":"","sourcesContent":["import { ValueType } from '@kaskad/types';\n\nexport interface ShapeDefinition {\n properties: Record<string, ValueType>;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=variant-shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variant-shape.js","sourceRoot":"","sources":["../../../../../../libs/definition/src/lib/types/variant-shape.ts"],"names":[],"mappings":"","sourcesContent":["import { ValueType } from '@kaskad/types';\n\nexport interface VariantShapeDefinition {\n commonProperties: Record<string, ValueType>;\n kinds: Record<string, Record<string, ValueType>>;\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic memoize requires `any` for function parameter contravariance
|
|
2
|
+
export function memoize(fn) {
|
|
3
|
+
const cache = new Map();
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- must match generic constraint
|
|
5
|
+
return function (...args) {
|
|
6
|
+
const key = args.join(',');
|
|
7
|
+
if (cache.has(key)) {
|
|
8
|
+
return structuredClone(cache.get(key));
|
|
9
|
+
}
|
|
10
|
+
const result = fn.apply(this, args);
|
|
11
|
+
cache.set(key, result);
|
|
12
|
+
return structuredClone(result);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=memoize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoize.js","sourceRoot":"","sources":["../../../../../../libs/definition/src/lib/util/memoize.ts"],"names":[],"mappings":"AAAA,sIAAsI;AACtI,MAAM,UAAU,OAAO,CAAwC,EAAK;IAClE,MAAM,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;IAExB,+FAA+F;IAC/F,OAAO,UAAyB,GAAG,IAAW;QAC5C,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAE3B,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACnB,OAAO,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACzC,CAAC;QAED,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACvB,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;IACjC,CAAM,CAAC;AACT,CAAC","sourcesContent":["// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic memoize requires `any` for function parameter contravariance\nexport function memoize<T extends (...args: any[]) => unknown>(fn: T): T {\n const cache = new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- must match generic constraint\n return function (this: unknown, ...args: any[]) {\n const key = args.join(',');\n\n if (cache.has(key)) {\n return structuredClone(cache.get(key));\n }\n\n const result = fn.apply(this, args);\n cache.set(key, result);\n return structuredClone(result);\n } as T;\n}\n"]}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NodeSchema, ObjectValueType, ValueType } from '@kaskad/types';
|
|
2
|
+
import { CommandDefinition } from './types/command';
|
|
3
|
+
import { ComponentDefinition } from './types/component';
|
|
4
|
+
import { ShapeDefinition } from './types/shape';
|
|
5
|
+
import { VariantShapeDefinition } from './types/variant-shape';
|
|
6
|
+
export declare class DefinitionStore {
|
|
7
|
+
private static instance;
|
|
8
|
+
shapes: {
|
|
9
|
+
[shapeType: string]: ShapeDefinition;
|
|
10
|
+
};
|
|
11
|
+
variantShapes: {
|
|
12
|
+
[shapeType: string]: VariantShapeDefinition;
|
|
13
|
+
};
|
|
14
|
+
components: {
|
|
15
|
+
[componentType: string]: ComponentDefinition;
|
|
16
|
+
};
|
|
17
|
+
commands: {
|
|
18
|
+
[commandType: string]: CommandDefinition;
|
|
19
|
+
};
|
|
20
|
+
private constructor();
|
|
21
|
+
static getInstance(): DefinitionStore;
|
|
22
|
+
static reset(): DefinitionStore;
|
|
23
|
+
setShape(shapeType: string, properties: Record<string, ValueType>): void;
|
|
24
|
+
getShape(shapeType: string): ObjectValueType;
|
|
25
|
+
setVariantShape(shapeType: string, commonProperties: Record<string, ValueType>): void;
|
|
26
|
+
setVariantShapeKind(shapeType: string, kind: string, properties: Record<string, ValueType>): void;
|
|
27
|
+
getVariantShape(shapeType: string, kind: string): ObjectValueType;
|
|
28
|
+
setComponent(componentType: string, componentDefinition: ComponentDefinition): void;
|
|
29
|
+
setComponents(componentDefinitions: Record<string, ComponentDefinition>): void;
|
|
30
|
+
getComponent(componentType: string): ComponentDefinition;
|
|
31
|
+
getComponentTraits(componentType: string): string[];
|
|
32
|
+
getComputedComponentContract(componentType: string): Record<string, NodeSchema>;
|
|
33
|
+
getNodeValueType(componentType: string, path: string): ValueType;
|
|
34
|
+
hasComponentWithType(componentType: string): boolean;
|
|
35
|
+
getComponentTypes(): string[];
|
|
36
|
+
setCommands(definitions: Record<string, CommandDefinition>): void;
|
|
37
|
+
setCommand(commandType: string, definition: CommandDefinition): void;
|
|
38
|
+
getCommand(commandType: string): CommandDefinition;
|
|
39
|
+
getCommandNames(): string[];
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function memoize<T extends (...args: any[]) => unknown>(fn: T): T;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kaskad/definition",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"peerDependencies": {
|
|
5
|
+
"@angular/core": "21.1.3",
|
|
6
|
+
"@kaskad/types": "0.0.1",
|
|
7
|
+
"@kaskad/config": "0.0.1",
|
|
8
|
+
"vite": "7.3.1",
|
|
9
|
+
"@analogjs/vite-plugin-angular": "2.1.3",
|
|
10
|
+
"@nx/vite": "22.4.5"
|
|
11
|
+
},
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"module": "esm2022/kaskad-definition.js",
|
|
14
|
+
"typings": "kaskad-definition.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./package.json": {
|
|
17
|
+
"default": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./kaskad-definition.d.ts",
|
|
21
|
+
"default": "./esm2022/kaskad-definition.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"tslib": "^2.3.0"
|
|
26
|
+
}
|
|
27
|
+
}
|