diff --git a/packages/frontend-plugin-api/src/wiring/createExtension.ts b/packages/frontend-plugin-api/src/wiring/createExtension.ts index 3733e44957..61223ca8e8 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtension.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtension.ts @@ -82,7 +82,7 @@ export interface CreateExtensionOptions< TOutput extends AnyExtensionDataMap, TInputs extends AnyExtensionInputMap, TConfig, - TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodTypeAny }, + TConfigSchema extends { [key: string]: z.ZodTypeAny }, > { kind?: string; namespace?: string; @@ -94,7 +94,9 @@ export interface CreateExtensionOptions< /** @deprecated - use `config.schema` instead */ configSchema?: PortableSchema; config?: { - schema: TConfigSchema; + schema: { + [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; + }; }; factory(context: { node: AppNode; @@ -104,7 +106,10 @@ export interface CreateExtensionOptions< } /** @public */ -export interface ExtensionDefinition { +export interface ExtensionDefinition< + TConfig, + TConfigSchema = never /* TODO -> refactor */, +> { $$type: '@backstage/ExtensionDefinition'; readonly kind?: string; readonly namespace?: string; @@ -153,7 +158,7 @@ export function createExtension< TOutput extends AnyExtensionDataMap, TInputs extends AnyExtensionInputMap, TConfig, - TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodTypeAny }, + TConfigSchema extends { [key: string]: z.ZodTypeAny }, >( options: CreateExtensionOptions, ): ExtensionDefinition { diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts index 61227c7dc1..9cd5689a95 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts @@ -33,7 +33,7 @@ export interface CreateExtensionBlueprintOptions< TParams, TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, - TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodTypeAny }, + TConfigSchema extends { [key: string]: z.ZodTypeAny }, TDataRefs extends AnyExtensionDataMap, > { kind: string; @@ -43,15 +43,17 @@ export interface CreateExtensionBlueprintOptions< inputs?: TInputs; output: TOutput; config?: { - schema: TConfigSchema; + schema: { + [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; + }; }; factory( params: TParams, context: { node: AppNode; - config: { - [key in keyof TConfigSchema]: z.infer>; - }; + config: Expand<{ + [key in keyof TConfigSchema]: z.infer; + }>; inputs: Expand>; }, ): Expand>; @@ -66,7 +68,7 @@ export interface ExtensionBlueprint< TParams, TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, - TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodTypeAny }, + TConfigSchema extends { [key: string]: z.ZodTypeAny }, TDataRefs extends AnyExtensionDataMap, > { dataRefs: TDataRefs; @@ -77,11 +79,7 @@ export interface ExtensionBlueprint< * You must either pass `params` directly, or define a `factory` that can * optionally call the original factory with the same params. */ - make< - TExtensionConfigSchema extends { - [key: string]: (zImpl: typeof z) => z.ZodTypeAny; - }, - >( + make( args: { namespace?: string; name?: string; @@ -90,7 +88,11 @@ export interface ExtensionBlueprint< inputs?: TInputs; output?: TOutput; config?: { - schema: TExtensionConfigSchema; + schema: { + [key in keyof TExtensionConfigSchema]: ( + zImpl: typeof z, + ) => TExtensionConfigSchema[key]; + }; }; } & ( | { @@ -98,21 +100,23 @@ export interface ExtensionBlueprint< originalFactory: ( params: TParams, context?: { - config?: { - [key in keyof TConfigSchema]: z.infer< - ReturnType - >; - }; + config?: Expand<{ + [key in keyof TConfigSchema]: z.infer; + }>; inputs?: Expand>; }, ) => Expand>, context: { node: AppNode; - config: { - [key in keyof TExtensionConfigSchema]: z.infer< - ReturnType - >; - }; + config: Expand< + { + [key in keyof TExtensionConfigSchema]: z.infer< + TExtensionConfigSchema[key] + >; + } & { + [key in keyof TConfigSchema]: z.infer; + } + >; inputs: Expand>; }, ): Expand>; @@ -124,7 +128,7 @@ export interface ExtensionBlueprint< ): ExtensionDefinition< { [key in keyof TExtensionConfigSchema]: z.infer< - ReturnType + TExtensionConfigSchema[key] >; }, TExtensionConfigSchema @@ -138,7 +142,7 @@ class ExtensionBlueprintImpl< TParams, TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, - TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodTypeAny }, + TConfigSchema extends { [key: string]: z.ZodTypeAny }, TDataRefs extends AnyExtensionDataMap, > { constructor( @@ -156,7 +160,7 @@ class ExtensionBlueprintImpl< dataRefs: TDataRefs; public make< - TExtensionConfigSchema extends TConfigSchema = TConfigSchema, + TExtensionConfigSchema extends { [key: string]: z.ZodTypeAny }, >(args: { namespace?: string; name?: string; @@ -166,37 +170,45 @@ class ExtensionBlueprintImpl< output?: TOutput; params?: TParams; config?: { - schema: TConfigSchema; + schema: { + [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; + }; }; factory?( originalFactory: ( params: TParams, context?: { - config?: { - [key in keyof TConfigSchema]: z.infer< - ReturnType - >; - }; + config?: Expand<{ + [key in keyof TConfigSchema]: z.infer; + }>; inputs?: Expand>; }, ) => Expand>, context: { node: AppNode; - config: { - [key in keyof TExtensionConfigSchema]: z.infer< - ReturnType - >; - }; + config: Expand< + { + [key in keyof TExtensionConfigSchema]: z.infer< + TExtensionConfigSchema[key] + >; + } & { + [key in keyof TConfigSchema]: z.infer; + } + >; inputs: Expand>; }, ): Expand>; }): ExtensionDefinition< - { - [key in keyof TExtensionConfigSchema]: z.infer< - ReturnType - >; - }, - TExtensionConfigSchema + Expand< + { + [key in keyof TExtensionConfigSchema]: z.infer< + TExtensionConfigSchema[key] + >; + } & { + [key in keyof TConfigSchema]: z.infer; + } + >, + Expand > { return createExtension({ kind: this.options.kind, @@ -206,18 +218,31 @@ class ExtensionBlueprintImpl< disabled: args.disabled ?? this.options.disabled, inputs: args.inputs ?? this.options.inputs, output: args.output ?? this.options.output, - config: args.config, + config: { + schema: { + ...this.options.config?.schema, + ...args.config, + } as Expand< + { + [key in keyof TConfigSchema]: ( + zImpl: typeof z, + ) => TConfigSchema[key]; + } & { + [key in keyof TExtensionConfigSchema]: ( + zImpl: typeof z, + ) => TExtensionConfigSchema[key]; + } + >, + }, factory: ({ node, config, inputs }) => { if (args.factory) { return args.factory( ( innerParams: TParams, innerContext?: { - config?: { - [key in keyof TConfigSchema]: z.infer< - ReturnType - >; - }; + config?: Expand<{ + [key in keyof TConfigSchema]: z.infer; + }>; inputs?: Expand>; }, ) => @@ -255,7 +280,7 @@ export function createExtensionBlueprint< TParams, TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, - TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodTypeAny }, + TConfigSchema extends { [key: string]: z.ZodTypeAny }, TDataRefs extends AnyExtensionDataMap = never, >( options: CreateExtensionBlueprintOptions< @@ -266,5 +291,5 @@ export function createExtensionBlueprint< TDataRefs >, ): ExtensionBlueprint { - return new ExtensionBlueprintImpl(options); + return new ExtensionBlueprintImpl(options) as any; }