chore: refactor the types a little bit

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2025-02-11 13:34:38 +01:00
committed by benjdlambert
parent 4abd0747cb
commit cb6cee1e2f
2 changed files with 56 additions and 66 deletions
@@ -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<TActionInput, TActionOutput, TInputSchema>,
ctx: ActionContext<TActionInput, TActionOutput, TSchemaType>,
) => Promise<void>;
};
@@ -63,6 +64,46 @@ type FlattenOptionalProperties<T extends { [key in string]: unknown }> = 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<TActionInput, TActionOutput, 'v1'>;
/**
* @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<TInputSchema>,
TActionOutput extends JsonObject = z.infer<TOutputSchema>,
>(
action: TemplateActionOptions<
TActionInput,
TActionOutput,
TInputSchema,
TOutputSchema,
'v1'
>,
): TemplateAction<TActionInput, TActionOutput, 'v1'>;
/**
* 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<ReturnType<TOutputSchema[key]>>;
},
TInputSchema,
TOutputSchema
TOutputSchema,
'v2'
>,
): TemplateAction<
FlattenOptionalProperties<{
@@ -89,46 +131,8 @@ export function createTemplateAction<
FlattenOptionalProperties<{
[key in keyof TOutputSchema]: z.output<ReturnType<TOutputSchema[key]>>;
}>,
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<TActionInput, TActionOutput, TInputSchema>;
/**
* @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<TInputSchema>,
TActionOutput extends JsonObject = z.infer<TOutputSchema>,
>(
action: TemplateActionOptions<
TActionInput,
TActionOutput,
TInputSchema,
TOutputSchema
>,
): TemplateAction<TActionInput, TActionOutput, TInputSchema>;
export function createTemplateAction<
TInputParams extends JsonObject = JsonObject,
TOutputParams extends JsonObject = JsonObject,
@@ -169,7 +173,13 @@ export function createTemplateAction<
TInputSchema,
TOutputSchema
>,
): TemplateAction<TActionInput, TActionOutput, TInputSchema, TOutputSchema> {
): TemplateAction<
TActionInput,
TActionOutput,
TInputSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType }
? 'v2'
: 'v1'
> {
const { inputSchema, outputSchema } = parseSchemas(action);
return {
+4 -24
View File
@@ -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<TActionInput, TActionOutput, TSchemaType>,
) => Promise<void>;
};