From e2d82fc3d8e30f1bc16962b79cbaa108424d9ad0 Mon Sep 17 00:00:00 2001 From: blam Date: Fri, 7 Feb 2025 13:23:17 +0100 Subject: [PATCH] chore: flatten the optional properties from zod Signed-off-by: blam --- .../src/actions/mockActionContext.ts | 2 +- .../src/actions/createTemplateAction.ts | 15 +++++++++++++-- plugins/scaffolder-node/src/actions/types.ts | 13 ++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts b/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts index f042416e36..eb03ac0a0f 100644 --- a/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts +++ b/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts @@ -35,7 +35,7 @@ export const createMockActionContext = < TActionOutput extends JsonObject = JsonObject, >( options?: Partial>, -): ActionContext => { +): ActionContext => { const credentials = mockCredentials.user(); const defaultContext = { logger: loggerToWinstonLogger(mockServices.logger.mock()), diff --git a/plugins/scaffolder-node/src/actions/createTemplateAction.ts b/plugins/scaffolder-node/src/actions/createTemplateAction.ts index 02cac0a4b5..3622c05662 100644 --- a/plugins/scaffolder-node/src/actions/createTemplateAction.ts +++ b/plugins/scaffolder-node/src/actions/createTemplateAction.ts @@ -52,6 +52,17 @@ export type TemplateActionOptions< ) => Promise; }; +/** + * @ignore + */ +type FlattenOptionalProperties = Expand< + { + [K in keyof T as undefined extends T[K] ? never : K]: T[K]; + } & { + [K in keyof T as undefined extends T[K] ? K : never]?: T[K]; + } +>; + /** * 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. @@ -72,10 +83,10 @@ export function createTemplateAction< TOutputSchema >, ): TemplateAction< - Expand<{ + FlattenOptionalProperties<{ [key in keyof TInputSchema]: z.output>; }>, - Expand<{ + FlattenOptionalProperties<{ [key in keyof TOutputSchema]: z.output>; }>, TInputSchema diff --git a/plugins/scaffolder-node/src/actions/types.ts b/plugins/scaffolder-node/src/actions/types.ts index d89e61e9e7..9f90df6e08 100644 --- a/plugins/scaffolder-node/src/actions/types.ts +++ b/plugins/scaffolder-node/src/actions/types.ts @@ -42,14 +42,13 @@ export type ActionContext< secrets?: TaskSecrets; workspacePath: string; input: TActionInput; - checkpoint( - key: string, - fn: () => Promise, - ): Promise; + checkpoint(opts: { + key: string; + fn: () => Promise | T; + }): Promise; output( - ...params: { - [K in keyof TActionOutput]: [name: K, value: TActionOutput[K]]; - }[keyof TActionOutput] + name: keyof TActionOutput, + value: TActionOutput[keyof TActionOutput], ): void; /** * Creates a temporary directory for use by the action, which is then cleaned up automatically.