diff --git a/plugins/scaffolder-node/src/actions/createTemplateAction.ts b/plugins/scaffolder-node/src/actions/createTemplateAction.ts index a083b2ab88..d33afdaca5 100644 --- a/plugins/scaffolder-node/src/actions/createTemplateAction.ts +++ b/plugins/scaffolder-node/src/actions/createTemplateAction.ts @@ -20,18 +20,18 @@ import { z } from 'zod'; import zodToJsonSchema from 'zod-to-json-schema'; import type { InferActionType, - NewActionContext, - NewTemplateAction, - OldActionContext, - OldTemplateAction, + ActionContextV2, + TemplateActionV2, + ActionContextV1, + TemplateActionV1, TemplateExample, } from './types'; /** - * @deprecated migrate to {@link NewTemplateActionOptions} + * @deprecated migrate to {@link TemplateActionOptionsV2} * @public */ -export type OldTemplateActionOptions< +export type TemplateActionOptionsV1< TInputParams extends JsonObject = JsonObject, TOutputParams extends JsonObject = JsonObject, TInputSchema extends Schema | z.ZodType = Schema, @@ -59,15 +59,13 @@ export type OldTemplateActionOptions< input?: TInputSchema; output?: TOutputSchema; }; - handler: ( - ctx: OldActionContext, - ) => Promise; + handler: (ctx: ActionContextV1) => Promise; }; /** * @public */ -export type NewTemplateActionOptions< +export type TemplateActionOptionsV2< TInputParams extends Record< PropertyKey, (zod: typeof z) => z.ZodType @@ -86,7 +84,7 @@ export type NewTemplateActionOptions< output: TOutputParams; }; handler: ( - ctx: NewActionContext< + ctx: ActionContextV2< InferActionType, InferActionType >, @@ -94,14 +92,36 @@ export type NewTemplateActionOptions< }; /** + * @deprecated migrate to {@link TemplateActionOptionsV2} * @public */ export type TemplateActionOptions< - TInputParams extends Record z.ZodType>, - TOutputParams extends Record z.ZodType>, -> = - | OldTemplateActionOptions - | NewTemplateActionOptions; + TInputParams extends JsonObject = JsonObject, + TOutputParams extends JsonObject = JsonObject, + TInputSchema extends Schema | z.ZodType = Schema, + TOutputSchema extends Schema | z.ZodType = Schema, + TActionInput extends JsonObject = TInputSchema extends z.ZodType< + any, + any, + infer IReturn + > + ? IReturn + : TInputParams, + TActionOutput extends JsonObject = TOutputSchema extends z.ZodType< + any, + any, + infer IReturn + > + ? IReturn + : TOutputParams, +> = TemplateActionOptionsV1< + TInputParams, + TOutputParams, + TInputSchema, + TOutputSchema, + TActionInput, + TActionOutput +>; function isZod(schema?: Schema | z.ZodType): schema is z.ZodType { return !!(schema && 'safeParseAsync' in schema); @@ -118,10 +138,10 @@ function transformZodRecordToObject( /** * 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. - * @deprecated migrate to {@link newCreateTemplateAction} + * @deprecated migrate to {@link createTemplateActionV2} * @public */ -export function oldCreateTemplateAction< +export function createTemplateActionV1< TInputParams extends JsonObject = JsonObject, TOutputParams extends JsonObject = JsonObject, TInputSchema extends Schema | z.ZodType = {}, @@ -141,7 +161,7 @@ export function oldCreateTemplateAction< ? IReturn : TOutputParams, >( - action: OldTemplateActionOptions< + action: TemplateActionOptionsV1< TInputParams, TOutputParams, TInputSchema, @@ -149,7 +169,7 @@ export function oldCreateTemplateAction< TActionInput, TActionOutput >, -): OldTemplateAction { +): TemplateActionV1 { const inputSchema = action.schema && action.schema.input && isZod(action.schema.input) ? (zodToJsonSchema(action.schema.input) as Schema) @@ -175,12 +195,12 @@ export function oldCreateTemplateAction< * Will convert zod schemas to json schemas for use throughout the system. * @public */ -export function newCreateTemplateAction< +export function createTemplateActionV2< TInputParams extends Record z.ZodType>, TOutputParams extends Record z.ZodType>, >( - action: NewTemplateActionOptions, -): NewTemplateAction< + action: TemplateActionOptionsV2, +): TemplateActionV2< InferActionType, InferActionType > { @@ -197,9 +217,9 @@ export function newCreateTemplateAction< }; } -function isOldAction( - action: OldTemplateActionOptions | NewTemplateActionOptions, -): action is OldTemplateActionOptions { +function isV1Action( + action: TemplateActionOptionsV1 | TemplateActionOptionsV2, +): action is TemplateActionOptionsV1 { return ( isZod(action.schema?.input) || typeof action.schema?.input === 'string' || @@ -217,15 +237,15 @@ export function createTemplateAction< TInputParams extends JsonObject = JsonObject, TOutputParams extends JsonObject = JsonObject, TAction extends - | OldTemplateActionOptions - | NewTemplateActionOptions = OldTemplateActionOptions, - TReturn = TAction extends OldTemplateActionOptions - ? Prettify> - : Prettify, + | TemplateActionOptionsV1 + | TemplateActionOptionsV2 = TemplateActionOptionsV1, + TReturn = TAction extends TemplateActionOptionsV1 + ? Prettify> + : Prettify, >(action: TAction): TReturn { - if (isOldAction(action)) { - return oldCreateTemplateAction(action) as TReturn; + if (isV1Action(action)) { + return createTemplateActionV1(action) as TReturn; } - return newCreateTemplateAction(action) as TReturn; + return createTemplateActionV2(action) as TReturn; } diff --git a/plugins/scaffolder-node/src/actions/index.ts b/plugins/scaffolder-node/src/actions/index.ts index 9ac9d9a5a4..c531977bf9 100644 --- a/plugins/scaffolder-node/src/actions/index.ts +++ b/plugins/scaffolder-node/src/actions/index.ts @@ -16,8 +16,8 @@ export { createTemplateAction, - type NewTemplateActionOptions, - type OldTemplateActionOptions, + type TemplateActionOptionsV2, + type TemplateActionOptionsV1, type TemplateActionOptions, } from './createTemplateAction'; export { @@ -35,12 +35,12 @@ export { } from './gitHelpers'; export type { ActionContext, + ActionContextV1, + ActionContextV2, InferActionType, - NewActionContext, - NewTemplateAction, - OldActionContext, - OldTemplateAction, TemplateAction, + TemplateActionV1, + TemplateActionV2, TemplateExample, } from './types'; export { getRepoSourceDirectory, parseRepoUrl } from './util'; diff --git a/plugins/scaffolder-node/src/actions/types.ts b/plugins/scaffolder-node/src/actions/types.ts index e554baf2f7..676f96e543 100644 --- a/plugins/scaffolder-node/src/actions/types.ts +++ b/plugins/scaffolder-node/src/actions/types.ts @@ -47,11 +47,11 @@ export type InferActionType< }>; /** - * OldActionContext is passed into scaffolder actions. - * @deprecated migrate to {@link NewActionContext} + * ActionContextV1 is passed into scaffolder actions. + * @deprecated migrate to {@link ActionContextV2} * @public */ -export type OldActionContext< +export type ActionContextV1< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, > = { @@ -124,10 +124,10 @@ export type OldActionContext< }; /** - * NewActionContext is passed into scaffolder actions. + * ActionContextV2 is passed into scaffolder actions. * @public */ -export type NewActionContext< +export type ActionContextV2< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, > = { @@ -190,20 +190,19 @@ export type NewActionContext< }; /** + * @deprecated migrate to {@link ActionContextV2} * @public */ export type ActionContext< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, -> = - | OldActionContext - | NewActionContext; +> = ActionContextV2; /** - * @deprecated migrate to {@link NewTemplateAction} + * @deprecated migrate to {@link TemplateActionV2} * @public */ -export type OldTemplateAction< +export type TemplateActionV1< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, > = { @@ -215,15 +214,13 @@ export type OldTemplateAction< input?: Schema; output?: Schema; }; - handler: ( - ctx: OldActionContext, - ) => Promise; + handler: (ctx: ActionContextV1) => Promise; }; /** * @public */ -export type NewTemplateAction< +export type TemplateActionV2< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, > = { @@ -235,17 +232,14 @@ export type NewTemplateAction< input: Schema; output: Schema; }; - handler: ( - ctx: NewActionContext, - ) => Promise; + handler: (ctx: ActionContextV2) => Promise; }; /** + * @deprecated migrate to {@link ActionContextV2} * @public */ export type TemplateAction< TActionInput extends JsonObject = JsonObject, TActionOutput extends JsonObject = JsonObject, -> = - | OldTemplateAction - | NewTemplateAction; +> = TemplateActionV1;