chore: wip

Signed-off-by: blam <ben@blam.sh>

Signed-off-by: benjdlambert <ben@blam.sh>
This commit is contained in:
blam
2025-02-18 09:03:23 +01:00
committed by benjdlambert
parent c6ae5b2e33
commit 3ece40f8c4
9 changed files with 55 additions and 29 deletions
@@ -16,7 +16,7 @@
import { ActionContext, TemplateAction } from './types';
import { z } from 'zod';
import { Schema } from 'jsonschema';
import { JSONSchema7 } from 'json-schema';
import { Expand, JsonObject } from '@backstage/types';
import { parseSchemas } from './util';
@@ -31,13 +31,13 @@ export type TemplateActionOptions<
TActionInput extends JsonObject = {},
TActionOutput extends JsonObject = {},
TInputSchema extends
| Schema
| JSONSchema7
| z.ZodType
| { [key in string]: (zImpl: typeof z) => z.ZodType } = Schema,
| { [key in string]: (zImpl: typeof z) => z.ZodType } = JSONSchema7,
TOutputSchema extends
| Schema
| JSONSchema7
| z.ZodType
| { [key in string]: (zImpl: typeof z) => z.ZodType } = Schema,
| { [key in string]: (zImpl: typeof z) => z.ZodType } = JSONSchema7,
TSchemaType extends 'v1' | 'v2' = 'v1' | 'v2',
> = {
id: string;
@@ -71,8 +71,8 @@ type FlattenOptionalProperties<T extends { [key in string]: unknown }> = Expand<
export function createTemplateAction<
TInputParams extends JsonObject = JsonObject,
TOutputParams extends JsonObject = JsonObject,
TInputSchema extends Schema = Schema,
TOutputSchema extends Schema = Schema,
TInputSchema extends JSONSchema7 = JSONSchema7,
TOutputSchema extends JSONSchema7 = JSONSchema7,
TActionInput extends JsonObject = TInputParams,
TActionOutput extends JsonObject = TOutputParams,
>(
@@ -137,11 +137,11 @@ export function createTemplateAction<
TInputParams extends JsonObject = JsonObject,
TOutputParams extends JsonObject = JsonObject,
TInputSchema extends
| Schema
| JSONSchema7
| z.ZodType
| { [key in string]: (zImpl: typeof z) => z.ZodType } = {},
TOutputSchema extends
| Schema
| JSONSchema7
| z.ZodType
| { [key in string]: (zImpl: typeof z) => z.ZodType } = {},
TActionInput extends JsonObject = TInputSchema extends z.ZodType<
@@ -180,14 +180,16 @@ export function createTemplateAction<
? 'v2'
: 'v1'
> {
const { inputSchema, outputSchema } = parseSchemas(action);
const { inputSchema, outputSchema } = parseSchemas(
action as TemplateActionOptions<any, any, any>,
);
return {
...action,
schema: {
...action.schema,
input: inputSchema as TInputSchema,
output: outputSchema as TOutputSchema,
input: inputSchema,
output: outputSchema,
},
};
}
+3 -3
View File
@@ -20,7 +20,7 @@ import { JsonObject, JsonValue } from '@backstage/types';
import { TaskSecrets } from '../tasks';
import { TemplateInfo } from '@backstage/plugin-scaffolder-common';
import { UserEntity } from '@backstage/catalog-model';
import { Schema } from 'jsonschema';
import { JSONSchema7 } from 'json-schema';
import {
BackstageCredentials,
LoggerService,
@@ -175,8 +175,8 @@ export type TemplateAction<
examples?: { description: string; example: string }[];
supportsDryRun?: boolean;
schema?: {
input?: Schema;
output?: Schema;
input?: JSONSchema7;
output?: JSONSchema7;
};
handler: (
ctx: ActionContext<TActionInput, TActionOutput, TSchemaType>,
+9 -6
View File
@@ -21,6 +21,7 @@ import { ScmIntegrationRegistry } from '@backstage/integration';
import { TemplateActionOptions } from './createTemplateAction';
import zodToJsonSchema from 'zod-to-json-schema';
import { z } from 'zod';
import { JSONSchema7 } from 'json-schema';
/**
* @public
@@ -142,16 +143,18 @@ const isNativeZodSchema = (
);
};
export const parseSchemas = (action: TemplateActionOptions<any, any, any>) => {
export const parseSchemas = (
action: TemplateActionOptions,
): { inputSchema?: JSONSchema7; outputSchema?: JSONSchema7 } => {
if (!action.schema) {
return { inputSchema: undefined, outputSchema: undefined };
}
if (isZodSchema(action.schema.input)) {
return {
inputSchema: zodToJsonSchema(action.schema.input),
inputSchema: zodToJsonSchema(action.schema.input) as JSONSchema7,
outputSchema: isZodSchema(action.schema.output)
? zodToJsonSchema(action.schema.output)
? (zodToJsonSchema(action.schema.output) as JSONSchema7)
: undefined,
};
}
@@ -164,15 +167,15 @@ export const parseSchemas = (action: TemplateActionOptions<any, any, any>) => {
);
return {
inputSchema: zodToJsonSchema(input),
inputSchema: zodToJsonSchema(input) as JSONSchema7,
outputSchema: isNativeZodSchema(action.schema.output)
? zodToJsonSchema(
? (zodToJsonSchema(
z.object(
Object.fromEntries(
Object.entries(action.schema.output).map(([k, v]) => [k, v(z)]),
),
),
)
) as JSONSchema7)
: undefined,
};
}