chore: supporting overriding using a function

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-08-05 16:15:07 +02:00
parent 765d91aa0b
commit 6ec1f48baf
2 changed files with 27 additions and 9 deletions
@@ -328,18 +328,20 @@ describe('createExtensionBlueprint', () => {
});
it('should allow providing callback for properties to set with params', () => {
type TestParams = { test: string };
const Blueprint = createExtensionBlueprint({
kind: 'test-extension',
attachTo: { id: 'test', input: 'default' },
name: (params: TestParams) => `${params.test}-name`,
output: [coreExtensionData.reactElement],
namespace: props => props.test,
name: props => `${props.test}-name`,
namespace: (props: TestParams) => props.test,
config: {
schema: props => ({
schema: (props: TestParams) => ({
test: z => z.string().default(props.test),
}),
},
factory(params: { test: string }) {
factory(params: TestParams) {
return [coreExtensionData.reactElement(<div>{params.test}</div>)];
},
});
@@ -359,7 +361,7 @@ describe('createExtensionBlueprint', () => {
"additionalProperties": false,
"properties": {
"test": {
"default": "test",
"default": "hello",
"type": "string",
},
},
@@ -47,12 +47,13 @@ export type CreateExtensionBlueprintOptions<
UFactoryOutput extends ExtensionDataValue<any, any>,
TDataRefs extends { [name in string]: AnyExtensionDataRef },
> = {
kind: string | ((params: TParams) => string);
kind: string;
namespace?: string | ((params: TParams) => string);
attachTo: { id: string; input: string };
disabled?: boolean;
inputs?: TInputs;
output: Array<UOutput>;
name?: string | ((params: TParams) => string);
config?: {
schema: TConfigSchema | ((params: TParams) => TConfigSchema);
};
@@ -250,15 +251,30 @@ class ExtensionBlueprintImpl<
>
>
> {
const optionsSchema =
typeof this.options.config?.schema === 'function'
? this.options.config?.schema(args.params!)
: this.options.config?.schema;
const schema = {
...this.options.config?.schema,
...optionsSchema,
...args.config?.schema,
} as TConfigSchema & TExtensionConfigSchema;
const namespace =
typeof this.options.namespace === 'function'
? this.options.namespace(args.params!)
: this.options.namespace;
const name =
typeof this.options.name === 'function'
? this.options.name(args.params!)
: this.options.name;
return createExtension({
kind: this.options.kind,
namespace: args.namespace ?? this.options.namespace,
name: args.name,
namespace: args.namespace ?? namespace,
name: args.name ?? name,
attachTo: args.attachTo ?? this.options.attachTo,
disabled: args.disabled ?? this.options.disabled,
inputs: args.inputs ?? this.options.inputs,