diff --git a/packages/frontend-plugin-api/api-report.md b/packages/frontend-plugin-api/api-report.md index 3c082a7694..d6a91c0c99 100644 --- a/packages/frontend-plugin-api/api-report.md +++ b/packages/frontend-plugin-api/api-report.md @@ -931,36 +931,40 @@ export interface ExtensionBlueprint< > { // (undocumented) dataRefs: TDataRefs; - // (undocumented) - make(args: { - namespace?: string; - name?: string; - attachTo?: { - id: string; - input: string; - }; - disabled?: boolean; - inputs?: TInputs; - output?: TOutput; - configSchema?: PortableSchema; - params: TParams; - factory?( - params: TParams, - context: { - node: AppNode; - config: TConfig; - inputs: Expand>; - orignalFactory( - params?: TParams, - context?: { - node?: AppNode; - config?: TConfig; - inputs?: Expand>; - }, - ): Expand>; - }, - ): Expand>; - }): ExtensionDefinition; + make( + args: { + namespace?: string; + name?: string; + attachTo?: { + id: string; + input: string; + }; + disabled?: boolean; + inputs?: TInputs; + output?: TOutput; + configSchema?: PortableSchema; + } & ( + | { + factory( + originalFactory: ( + params: TParams, + context?: { + config?: TConfig; + inputs?: Expand>; + }, + ) => Expand>, + context: { + node: AppNode; + config: TConfig; + inputs: Expand>; + }, + ): Expand>; + } + | { + params: TParams; + } + ), + ): ExtensionDefinition; } // @public (undocumented) diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx index 87b3cec152..d2373518a3 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx @@ -88,20 +88,17 @@ describe('createExtensionBlueprint', () => { const extension = TestExtensionBlueprint.make({ name: 'my-extension', - params: { - text: 'Hello, world!', - }, - factory(params: { text: string }) { - return { - element:

{params.text}

, - }; + factory(origFactory) { + return origFactory({ + text: 'Hello, world!', + }); }, }); expect(extension).toBeDefined(); const { container } = createExtensionTester(extension).render(); - expect(container.querySelector('h2')).toHaveTextContent('Hello, world!'); + expect(container.querySelector('h1')).toHaveTextContent('Hello, world!'); }); it('should allow exporting the dataRefs from the extension blueprint', () => { diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts index b504db5f03..fb9081f7f8 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts @@ -67,32 +67,43 @@ export interface ExtensionBlueprint< > { dataRefs: TDataRefs; - make(args: { - namespace?: string; - name?: string; - attachTo?: { id: string; input: string }; - disabled?: boolean; - inputs?: TInputs; - output?: TOutput; - configSchema?: PortableSchema; - params: TParams; - factory?( - params: TParams, - context: { - node: AppNode; - config: TConfig; - inputs: Expand>; - orignalFactory( - params?: TParams, - context?: { - node?: AppNode; - config?: TConfig; - inputs?: Expand>; - }, - ): Expand>; - }, - ): Expand>; - }): ExtensionDefinition; + /** + * Creates a new extension from the blueprint. + * + * You must either pass `params` directly, or define a `factory` that can + * optionally call the original factory with the same params. + */ + make( + args: { + namespace?: string; + name?: string; + attachTo?: { id: string; input: string }; + disabled?: boolean; + inputs?: TInputs; + output?: TOutput; + configSchema?: PortableSchema; + } & ( + | { + factory( + originalFactory: ( + params: TParams, + context?: { + config?: TConfig; + inputs?: Expand>; + }, + ) => Expand>, + context: { + node: AppNode; + config: TConfig; + inputs: Expand>; + }, + ): Expand>; + } + | { + params: TParams; + } + ), + ): ExtensionDefinition; } /** @@ -127,21 +138,19 @@ class ExtensionBlueprintImpl< inputs?: TInputs; output?: TOutput; configSchema?: PortableSchema; - params: TParams; + params?: TParams; factory?( - params: TParams, + originalFactory: ( + params: TParams, + context?: { + config?: TConfig; + inputs?: Expand>; + }, + ) => Expand>, context: { node: AppNode; config: TConfig; inputs: Expand>; - orignalFactory( - params?: TParams, - context?: { - node?: AppNode; - config?: TConfig; - inputs?: Expand>; - }, - ): Expand>; }, ): Expand>; }): ExtensionDefinition { @@ -156,30 +165,33 @@ class ExtensionBlueprintImpl< configSchema: args.configSchema ?? this.options.configSchema, // TODO: some config merging or smth factory: ({ node, config, inputs }) => { if (args.factory) { - return args.factory(args.params, { - node, - config, - inputs, - orignalFactory: ( - innerParams?: TParams, + return args.factory( + ( + innerParams: TParams, innerContext?: { config?: TConfig; inputs?: Expand>; }, ) => - this.options.factory(innerParams ?? args.params, { + this.options.factory(innerParams, { node, config: innerContext?.config ?? config, inputs: innerContext?.inputs ?? inputs, }), + { + node, + config, + inputs, + }, + ); + } else if (args.params) { + return this.options.factory(args.params, { + node, + config, + inputs, }); } - - return this.options.factory(args.params, { - node, - config, - inputs, - }); + throw new Error('Either params or factory must be provided'); }, }); }