From e9673327852493d196430c0c4eddf12e2effe8bb Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 24 Jul 2024 13:40:43 +0200 Subject: [PATCH] chore: small refactor and fixing some of the types Signed-off-by: blam --- packages/frontend-plugin-api/api-report.md | 51 +++++------ .../src/wiring/createExtension.ts | 12 ++- .../wiring/createExtensionBlueprint.test.tsx | 49 +++++++++++ .../src/wiring/createExtensionBlueprint.ts | 86 ++++++++++--------- 4 files changed, 122 insertions(+), 76 deletions(-) diff --git a/packages/frontend-plugin-api/api-report.md b/packages/frontend-plugin-api/api-report.md index 33ac02437c..45ed51b415 100644 --- a/packages/frontend-plugin-api/api-report.md +++ b/packages/frontend-plugin-api/api-report.md @@ -513,7 +513,7 @@ export function createExtension< TInputs extends AnyExtensionInputMap, TConfig, TConfigSchema extends { - [key: string]: z.ZodType; + [key: string]: (zImpl: typeof z) => z.ZodType; }, >( options: CreateExtensionOptions, @@ -525,7 +525,7 @@ export function createExtensionBlueprint< TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, TConfigSchema extends { - [key in string]: z.ZodType; + [key in string]: (zImpl: typeof z) => z.ZodType; }, TDataRefs extends AnyExtensionDataMap = never, >( @@ -540,9 +540,11 @@ export function createExtensionBlueprint< TParams, TInputs, TOutput, - { - [key in keyof TConfigSchema]: z.infer; - }, + string extends keyof TConfigSchema + ? {} + : { + [key in keyof TConfigSchema]: z.infer>; + }, TDataRefs >; @@ -552,7 +554,7 @@ export interface CreateExtensionBlueprintOptions< TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, TConfigSchema extends { - [key in string]: z.ZodType; + [key in string]: (zImpl: typeof z) => z.ZodType; }, TDataRefs extends AnyExtensionDataMap, > { @@ -563,9 +565,7 @@ export interface CreateExtensionBlueprintOptions< }; // (undocumented) config?: { - schema: { - [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; - }; + schema: TConfigSchema; }; // (undocumented) dataRefs?: TDataRefs; @@ -577,7 +577,7 @@ export interface CreateExtensionBlueprintOptions< context: { node: AppNode; config: { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; }; inputs: Expand>; }, @@ -628,7 +628,7 @@ export interface CreateExtensionOptions< TInputs extends AnyExtensionInputMap, TConfig, TConfigSchema extends { - [key: string]: z.ZodType; + [key: string]: (zImpl: typeof z) => z.ZodType; }, > { // (undocumented) @@ -638,9 +638,7 @@ export interface CreateExtensionOptions< }; // (undocumented) config?: { - schema: { - [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; - }; + schema: TConfigSchema; }; // @deprecated (undocumented) configSchema?: PortableSchema; @@ -650,7 +648,7 @@ export interface CreateExtensionOptions< factory(context: { node: AppNode; config: TConfig & { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; }; inputs: Expand>; }): Expand>; @@ -967,7 +965,7 @@ export interface ExtensionBlueprint< dataRefs: TDataRefs; make< TExtensionConfigSchema extends { - [key in string]: z.ZodType; + [key in string]: (zImpl: typeof z) => z.ZodType; }, >( args: { @@ -981,12 +979,9 @@ export interface ExtensionBlueprint< inputs?: TInputs; output?: TOutput; config?: { - schema: { - [key in keyof TExtensionConfigSchema]: ( - zImpl: typeof z, - ) => TExtensionConfigSchema[key]; - } & { - [key in keyof TConfig]?: never; + schema: TExtensionConfigSchema & { + [KName in keyof TConfig]?: `Error: Config key '${KName & + string}' is already defined in parent schema`; }; }; } & ( @@ -1001,11 +996,11 @@ export interface ExtensionBlueprint< ) => Expand>, context: { node: AppNode; - config: { + config: TConfig & { [key in keyof TExtensionConfigSchema]: z.infer< - TExtensionConfigSchema[key] + ReturnType >; - } & TConfig; + }; inputs: Expand>; }, ): Expand>; @@ -1017,7 +1012,7 @@ export interface ExtensionBlueprint< ): ExtensionDefinition< { [key in keyof TExtensionConfigSchema]: z.infer< - TExtensionConfigSchema[key] + ReturnType >; } & TConfig >; @@ -1175,9 +1170,7 @@ export const IconBundleBlueprint: ExtensionBlueprint< {} >; }, - { - [x: string]: any; - }, + {}, { icons: ConfigurableExtensionDataRef< 'core.icons', diff --git a/packages/frontend-plugin-api/src/wiring/createExtension.ts b/packages/frontend-plugin-api/src/wiring/createExtension.ts index 7783f8e14d..ac91557906 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]: z.ZodType }, + TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodType }, > { kind?: string; namespace?: string; @@ -94,14 +94,12 @@ export interface CreateExtensionOptions< /** @deprecated - use `config.schema` instead */ configSchema?: PortableSchema; config?: { - schema: { - [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; - }; + schema: TConfigSchema; }; factory(context: { node: AppNode; config: TConfig & { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; }; inputs: Expand>; }): Expand>; @@ -154,7 +152,7 @@ export function createExtension< TOutput extends AnyExtensionDataMap, TInputs extends AnyExtensionInputMap, TConfig, - TConfigSchema extends { [key: string]: z.ZodType }, + TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodType }, >( options: CreateExtensionOptions, ): ExtensionDefinition { @@ -188,7 +186,7 @@ export function createExtension< return options.factory({ inputs: inputs as Expand>, config: config as TConfig & { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; }, ...rest, }); diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx index 209e9759bd..cb8855d892 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.test.tsx @@ -225,4 +225,53 @@ describe('createExtensionBlueprint', () => { expect('test').toBe('test'); }); + + it('should allow setting config when one was not already defined in the blueprint', () => { + const TestExtensionBlueprint = createExtensionBlueprint({ + kind: 'test-extension', + attachTo: { id: 'test', input: 'default' }, + output: { + element: coreExtensionData.reactElement, + }, + factory(_, { config }) { + // @ts-expect-error + const b = config.something; + + return { + element:
, + }; + }, + }); + + const extension = TestExtensionBlueprint.make({ + name: 'my-extension', + params: { + text: 'Hello, world!', + }, + config: { + schema: { + something: z => z.string(), + defaulted: z => z.string().optional().default('default'), + }, + }, + factory(origFactory, { config }) { + const b: string = config.something; + + unused(b); + + expect(config.something).toBe('something new!'); + expect(config.defaulted).toBe('lolz'); + return origFactory({}); + }, + }); + + expect.assertions(2); + + createExtensionTester(extension, { + config: { + something: 'something new!', + defaulted: 'lolz', + }, + }).render(); + }); }); diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts index 21c10f3c0e..ce3e650342 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 in string]: z.ZodType }, + TConfigSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType }, TDataRefs extends AnyExtensionDataMap, > { kind: string; @@ -43,16 +43,14 @@ export interface CreateExtensionBlueprintOptions< inputs?: TInputs; output: TOutput; config?: { - schema: { - [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; - }; + schema: TConfigSchema; }; factory( params: TParams, context: { node: AppNode; config: { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; }; inputs: Expand>; }, @@ -79,7 +77,11 @@ 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( + make< + TExtensionConfigSchema extends { + [key in string]: (zImpl: typeof z) => z.ZodType; + }, + >( args: { namespace?: string; name?: string; @@ -88,12 +90,9 @@ export interface ExtensionBlueprint< inputs?: TInputs; output?: TOutput; config?: { - schema: { - [key in keyof TExtensionConfigSchema]: ( - zImpl: typeof z, - ) => TExtensionConfigSchema[key]; - } & { - [key in keyof TConfig]?: never; + schema: TExtensionConfigSchema & { + [KName in keyof TConfig]?: `Error: Config key '${KName & + string}' is already defined in parent schema`; }; }; } & ( @@ -108,11 +107,11 @@ export interface ExtensionBlueprint< ) => Expand>, context: { node: AppNode; - config: { + config: TConfig & { [key in keyof TExtensionConfigSchema]: z.infer< - TExtensionConfigSchema[key] + ReturnType >; - } & TConfig; + }; inputs: Expand>; }, ): Expand>; @@ -124,7 +123,7 @@ export interface ExtensionBlueprint< ): ExtensionDefinition< { [key in keyof TExtensionConfigSchema]: z.infer< - TExtensionConfigSchema[key] + ReturnType >; } & TConfig >; @@ -137,7 +136,7 @@ class ExtensionBlueprintImpl< TParams, TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, - TConfigSchema extends { [key in string]: z.ZodType }, + TConfigSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType }, TDataRefs extends AnyExtensionDataMap, > { constructor( @@ -155,7 +154,9 @@ class ExtensionBlueprintImpl< dataRefs: TDataRefs; public make< - TExtensionConfigSchema extends { [key in string]: z.ZodType }, + TExtensionConfigSchema extends { + [key in string]: (zImpl: typeof z) => z.ZodType; + }, >(args: { namespace?: string; name?: string; @@ -165,18 +166,16 @@ class ExtensionBlueprintImpl< output?: TOutput; params?: TParams; config?: { - schema: { - [key in keyof TExtensionConfigSchema]: ( - zImpl: typeof z, - ) => TExtensionConfigSchema[key]; - }; + schema: TExtensionConfigSchema; }; factory?( originalFactory: ( params: TParams, context?: { config?: { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer< + ReturnType + >; }; inputs?: Expand>; }, @@ -185,10 +184,10 @@ class ExtensionBlueprintImpl< node: AppNode; config: { [key in keyof TExtensionConfigSchema]: z.infer< - TExtensionConfigSchema[key] + ReturnType >; } & { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; }; inputs: Expand>; }, @@ -196,23 +195,16 @@ class ExtensionBlueprintImpl< }): ExtensionDefinition< { [key in keyof TExtensionConfigSchema]: z.infer< - TExtensionConfigSchema[key] + ReturnType >; } & { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer>; } > { const schema = { ...this.options.config?.schema, ...args.config?.schema, - } as { - [key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key]; - } & { - [key in keyof TExtensionConfigSchema]: ( - zImpl: typeof z, - ) => TExtensionConfigSchema[key]; - }; - + } as TConfigSchema & TExtensionConfigSchema; return createExtension({ kind: this.options.kind, namespace: args.namespace ?? this.options.namespace, @@ -229,7 +221,9 @@ class ExtensionBlueprintImpl< innerParams: TParams, innerContext?: { config?: { - [key in keyof TConfigSchema]: z.infer; + [key in keyof TConfigSchema]: z.infer< + ReturnType + >; }; inputs?: Expand>; }, @@ -268,7 +262,7 @@ export function createExtensionBlueprint< TParams, TInputs extends AnyExtensionInputMap, TOutput extends AnyExtensionDataMap, - TConfigSchema extends { [key in string]: z.ZodType }, + TConfigSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType }, TDataRefs extends AnyExtensionDataMap = never, >( options: CreateExtensionBlueprintOptions< @@ -282,8 +276,20 @@ export function createExtensionBlueprint< TParams, TInputs, TOutput, - { [key in keyof TConfigSchema]: z.infer }, + string extends keyof TConfigSchema + ? {} + : { [key in keyof TConfigSchema]: z.infer> }, TDataRefs > { - return new ExtensionBlueprintImpl(options); + return new ExtensionBlueprintImpl(options) as ExtensionBlueprint< + TParams, + TInputs, + TOutput, + string extends keyof TConfigSchema + ? {} + : { + [key in keyof TConfigSchema]: z.infer>; + }, + TDataRefs + >; }