From 6ec1f48baf47f3a529c92698224d4864fd2bb9c6 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 5 Aug 2024 16:15:07 +0200 Subject: [PATCH] chore: supporting overriding using a function Signed-off-by: blam --- .../wiring/createExtensionBlueprint.test.tsx | 12 ++++++---- .../src/wiring/createExtensionBlueprint.ts | 24 +++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx index 38e92fc26a..dfdbfface6 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx @@ -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(
{params.test}
)]; }, }); @@ -359,7 +361,7 @@ describe('createExtensionBlueprint', () => { "additionalProperties": false, "properties": { "test": { - "default": "test", + "default": "hello", "type": "string", }, }, diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts index 4a8f4a7c46..46db278bf8 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts @@ -47,12 +47,13 @@ export type CreateExtensionBlueprintOptions< UFactoryOutput extends ExtensionDataValue, 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; + 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,