make spec of create functions optional

This commit is contained in:
Bowen Tan 2021-12-13 11:35:19 +08:00
parent 451590b581
commit 2362940bb5
3 changed files with 77 additions and 14 deletions

View File

@ -27,10 +27,37 @@ export type RuntimeComponentSpec = Component & {
parsedVersion: Version;
};
export function createComponent(options: Omit<Component, 'kind'>): RuntimeComponentSpec {
return {
...options,
kind: 'Component',
parsedVersion: parseVersion(options.version),
};
// partial some fields, use as param createComponent
export type ComponentDefinition = {
version: string;
metadata: Partial<ComponentMetadata> & { name: string };
spec?: Partial<ComponentSpec>;
};
export function createComponent(options: ComponentDefinition): RuntimeComponentSpec {
return (
{
version: options.version,
kind: ('Component' as any),
parsedVersion: parseVersion(options.version),
metadata: {
description: '',
isDraggable: true,
isResizable: true,
displayName: options.metadata.name,
exampleProperties: {},
exampleSize: [1, 1],
...options.metadata,
},
spec: {
properties: {},
state: {},
methods: [],
styleSlots: [],
slots: [],
events: [],
...options.spec,
},
}
);
}

View File

@ -23,10 +23,27 @@ export type RuntimeModuleSpec = Module & {
parsedVersion: Version;
};
export function createModule(options: Omit<Module, 'kind'>): RuntimeModuleSpec {
// partial some fields, use as param createModule
export type ModuleDefinition = {
version: string;
metadata: Metadata;
spec?: Partial<ModuleSpec>;
};
export function createModule(options: ModuleDefinition): RuntimeModuleSpec {
return {
...options,
version: options.version,
kind: 'Module',
parsedVersion: parseVersion(options.version),
metadata: {
name: options.metadata.name,
description: options.metadata.description,
},
spec: {
properties: {},
events: [],
stateMap: {},
...options.spec
},
};
}

View File

@ -23,10 +23,29 @@ export type RuntimeTraitSpec = Trait & {
parsedVersion: Version;
};
export function createTrait(options: Omit<Trait, 'kind'>): RuntimeTraitSpec {
return {
...options,
kind: 'Trait',
parsedVersion: parseVersion(options.version),
};
// partial some fields, use as param createTrait
export type TraitDefinition = {
version: string;
metadata: Metadata;
spec?: Partial<TraitSpec>;
};
export function createTrait(options: TraitDefinition): RuntimeTraitSpec {
return (
{
version: options.version,
kind: ('Trait' as any),
parsedVersion: parseVersion(options.version),
metadata: {
name: options.metadata.name,
description: options.metadata.description,
},
spec: {
properties: {},
state: {},
methods: [],
...options.spec
},
}
);
}