diff --git a/plugins/scaffolder-node/src/actions/createTemplateAction.ts b/plugins/scaffolder-node/src/actions/createTemplateAction.ts index b2a525a5d5..29c2d79db9 100644 --- a/plugins/scaffolder-node/src/actions/createTemplateAction.ts +++ b/plugins/scaffolder-node/src/actions/createTemplateAction.ts @@ -38,6 +38,7 @@ export type TemplateActionOptions< | Schema | z.ZodType | { [key in string]: (zImpl: typeof z) => z.ZodType } = Schema, + TSchemaType extends 'v1' | 'v2' = 'v1' | 'v2', > = { id: string; description?: string; @@ -48,7 +49,7 @@ export type TemplateActionOptions< output?: TOutputSchema; }; handler: ( - ctx: ActionContext, + ctx: ActionContext, ) => Promise; }; @@ -63,6 +64,46 @@ type FlattenOptionalProperties = Expand< } >; +/** + * @public + * @deprecated migrate to using the new built in zod schema definitions for schemas + */ +export function createTemplateAction< + TInputParams extends JsonObject = JsonObject, + TOutputParams extends JsonObject = JsonObject, + TInputSchema extends Schema = Schema, + TOutputSchema extends Schema = Schema, + TActionInput extends JsonObject = TInputParams, + TActionOutput extends JsonObject = TOutputParams, +>( + action: TemplateActionOptions< + TActionInput, + TActionOutput, + TInputSchema, + TOutputSchema, + 'v1' + >, +): TemplateAction; +/** + * @public + * @deprecated migrate to using the new built in zod schema definitions for schemas + */ +export function createTemplateAction< + TInputParams extends JsonObject = JsonObject, + TOutputParams extends JsonObject = JsonObject, + TInputSchema extends z.ZodType = z.ZodType, + TOutputSchema extends z.ZodType = z.ZodType, + TActionInput extends JsonObject = z.infer, + TActionOutput extends JsonObject = z.infer, +>( + action: TemplateActionOptions< + TActionInput, + TActionOutput, + TInputSchema, + TOutputSchema, + 'v1' + >, +): TemplateAction; /** * This function is used to create new template actions to get type safety. * Will convert zod schemas to json schemas for use throughout the system. @@ -80,7 +121,8 @@ export function createTemplateAction< [key in keyof TOutputSchema]: z.infer>; }, TInputSchema, - TOutputSchema + TOutputSchema, + 'v2' >, ): TemplateAction< FlattenOptionalProperties<{ @@ -89,46 +131,8 @@ export function createTemplateAction< FlattenOptionalProperties<{ [key in keyof TOutputSchema]: z.output>; }>, - TInputSchema + 'v2' >; -/** - * @public - * @deprecated migrate to using the new built in zod schema definitions for schemas - */ -export function createTemplateAction< - TInputParams extends JsonObject = JsonObject, - TOutputParams extends JsonObject = JsonObject, - TInputSchema extends Schema = Schema, - TOutputSchema extends Schema = Schema, - TActionInput extends JsonObject = TInputParams, - TActionOutput extends JsonObject = TOutputParams, ->( - action: TemplateActionOptions< - TActionInput, - TActionOutput, - TInputSchema, - TOutputSchema - >, -): TemplateAction; -/** - * @public - * @deprecated migrate to using the new built in zod schema definitions for schemas - */ -export function createTemplateAction< - TInputParams extends JsonObject = JsonObject, - TOutputParams extends JsonObject = JsonObject, - TInputSchema extends z.ZodType = z.ZodType, - TOutputSchema extends z.ZodType = z.ZodType, - TActionInput extends JsonObject = z.infer, - TActionOutput extends JsonObject = z.infer, ->( - action: TemplateActionOptions< - TActionInput, - TActionOutput, - TInputSchema, - TOutputSchema - >, -): TemplateAction; export function createTemplateAction< TInputParams extends JsonObject = JsonObject, TOutputParams extends JsonObject = JsonObject, @@ -169,7 +173,13 @@ export function createTemplateAction< TInputSchema, TOutputSchema >, -): TemplateAction { +): TemplateAction< + TActionInput, + TActionOutput, + TInputSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType } + ? 'v2' + : 'v1' +> { const { inputSchema, outputSchema } = parseSchemas(action); return { diff --git a/plugins/scaffolder-node/src/actions/types.ts b/plugins/scaffolder-node/src/actions/types.ts index c5fa4052c4..92a2c7464a 100644 --- a/plugins/scaffolder-node/src/actions/types.ts +++ b/plugins/scaffolder-node/src/actions/types.ts @@ -25,7 +25,6 @@ import { BackstageCredentials, LoggerService, } from '@backstage/backend-plugin-api'; -import { z } from 'zod'; /** * ActionContext is passed into scaffolder actions. * @public @@ -33,15 +32,8 @@ import { z } from 'zod'; export type ActionContext< TActionInput extends JsonObject, TActionOutput extends JsonObject = JsonObject, - TInputSchema extends - | { [key in string]: (zImpl: typeof z) => z.ZodType } - | Schema - | unknown = unknown, - _TOutputSchema extends - | { [key in string]: (zImpl: typeof z) => z.ZodType } - | Schema - | unknown = unknown, -> = TInputSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType } + TSchemaType extends 'v1' | 'v2' = 'v1', +> = TSchemaType extends 'v2' ? { logger: LoggerService; secrets?: TaskSecrets; @@ -176,14 +168,7 @@ export type ActionContext< export type TemplateAction< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, - TInputSchema extends - | { [key in string]: (zImpl: typeof z) => z.ZodType } - | Schema - | unknown = unknown, - TOutputSchema extends - | { [key in string]: (zImpl: typeof z) => z.ZodType } - | Schema - | unknown = unknown, + TSchemaType extends 'v1' | 'v2' = 'v1', > = { id: string; description?: string; @@ -194,11 +179,6 @@ export type TemplateAction< output?: Schema; }; handler: ( - ctx: ActionContext< - TActionInput, - TActionOutput, - TInputSchema, - TOutputSchema - >, + ctx: ActionContext, ) => Promise; };