feat: reworking zod integration a little bit to reduce the amount of typing you have to do
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -14,17 +14,21 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { TemplateAction } from './types';
|
||||
import { z } from 'zod';
|
||||
import { Schema } from 'jsonschema';
|
||||
|
||||
/**
|
||||
* This function is used to create new template actions to get type safety.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const createTemplateAction = <TInput extends JsonObject>(
|
||||
templateAction: TemplateAction<TInput>,
|
||||
): TemplateAction<TInput> => {
|
||||
export const createTemplateAction = <
|
||||
TParams,
|
||||
TInputSchema extends Schema | z.ZodType = {},
|
||||
>(
|
||||
templateAction: TemplateAction<TParams, TInputSchema>,
|
||||
): TemplateAction<TParams, TInputSchema> => {
|
||||
// TODO(blam): Can add some more validation here to validate the action later on
|
||||
return templateAction;
|
||||
};
|
||||
|
||||
@@ -21,7 +21,7 @@ import { Schema } from 'jsonschema';
|
||||
import { TaskSecrets } from '../tasks/types';
|
||||
import { TemplateInfo } from '@backstage/plugin-scaffolder-common';
|
||||
import { UserEntity } from '@backstage/catalog-model';
|
||||
|
||||
import { z } from 'zod';
|
||||
/**
|
||||
* ActionContext is passed into scaffolder actions.
|
||||
* @public
|
||||
@@ -63,14 +63,23 @@ export type ActionContext<TInput extends JsonObject> = {
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export type TemplateAction<TInput extends JsonObject> = {
|
||||
export type TemplateAction<
|
||||
TParams,
|
||||
TInputSchema extends Schema | z.ZodType = {},
|
||||
> = {
|
||||
id: string;
|
||||
description?: string;
|
||||
examples?: { description: string; example: string }[];
|
||||
supportsDryRun?: boolean;
|
||||
schema?: {
|
||||
input?: Schema;
|
||||
input?: TInputSchema;
|
||||
output?: Schema;
|
||||
};
|
||||
handler: (ctx: ActionContext<TInput>) => Promise<void>;
|
||||
handler: (
|
||||
ctx: ActionContext<
|
||||
TInputSchema extends z.ZodType<any, any, infer IReturn>
|
||||
? IReturn
|
||||
: TParams
|
||||
>,
|
||||
) => Promise<void>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user