diff --git a/packages/backend-defaults/src/entrypoints/actionsRegistry/DefaultActionsRegistryService.ts b/packages/backend-defaults/src/entrypoints/actionsRegistry/DefaultActionsRegistryService.ts index 17523a2ee5..2ecf4d5006 100644 --- a/packages/backend-defaults/src/entrypoints/actionsRegistry/DefaultActionsRegistryService.ts +++ b/packages/backend-defaults/src/entrypoints/actionsRegistry/DefaultActionsRegistryService.ts @@ -24,7 +24,7 @@ import { } from '@backstage/backend-plugin-api'; import PromiseRouter from 'express-promise-router'; import { Router, json } from 'express'; -import { z, ZodType } from 'zod'; +import { z, AnyZodObject } from 'zod'; import zodToJsonSchema from 'zod-to-json-schema'; import { ForwardedError, @@ -142,9 +142,10 @@ export class DefaultActionsRegistryService implements ActionsRegistryService { return router; } - register( - options: ActionsRegistryActionOptions, - ): void { + register< + TInputSchema extends AnyZodObject, + TOutputSchema extends AnyZodObject, + >(options: ActionsRegistryActionOptions): void { const id = `${this.metadata.getId()}:${options.name}`; if (this.actions.has(id)) { diff --git a/packages/backend-defaults/src/entrypoints/actionsRegistry/actionsRegistryServiceFactory.test.ts b/packages/backend-defaults/src/entrypoints/actionsRegistry/actionsRegistryServiceFactory.test.ts index 5d47ff055d..9d6674c0cf 100644 --- a/packages/backend-defaults/src/entrypoints/actionsRegistry/actionsRegistryServiceFactory.test.ts +++ b/packages/backend-defaults/src/entrypoints/actionsRegistry/actionsRegistryServiceFactory.test.ts @@ -181,7 +181,7 @@ describe('actionsRegistryServiceFactory', () => { }); }); - it('should allow registering of actions with no schema', async () => { + it('should forces registration of input and output schema as objects', async () => { const pluginSubject = createBackendPlugin({ pluginId: 'my-plugin', register(reg) { @@ -195,9 +195,12 @@ describe('actionsRegistryServiceFactory', () => { title: 'Test', description: 'Test', schema: { + // @ts-expect-error - z.undefined is not a valid schema input: z => z.undefined(), + // @ts-expect-error - z.string is not a valid schema output: z => z.string(), }, + // @ts-expect-error - output is not a valid, needs to be an object action: async () => ({ output: 'ok' }), }); }, diff --git a/packages/backend-plugin-api/report.api.md b/packages/backend-plugin-api/report.api.md index 68597dcbe1..3b24fcb012 100644 --- a/packages/backend-plugin-api/report.api.md +++ b/packages/backend-plugin-api/report.api.md @@ -3,6 +3,7 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { AnyZodObject } from 'zod'; import { AuthorizePermissionRequest } from '@backstage/plugin-permission-common'; import { AuthorizePermissionResponse } from '@backstage/plugin-permission-common'; import { Config } from '@backstage/config'; @@ -27,10 +28,9 @@ import { Readable } from 'stream'; import type { Request as Request_2 } from 'express'; import type { Response as Response_2 } from 'express'; import { z } from 'zod'; -import { ZodType } from 'zod'; // @public (undocumented) -export type ActionsRegistryActionContext = { +export type ActionsRegistryActionContext = { input: z.infer; logger: LoggerService; credentials: BackstageCredentials; @@ -38,8 +38,8 @@ export type ActionsRegistryActionContext = { // @public (undocumented) export type ActionsRegistryActionOptions< - TInputSchema extends ZodType, - TOutputSchema extends ZodType, + TInputSchema extends AnyZodObject, + TOutputSchema extends AnyZodObject, > = { name: string; title: string; @@ -60,7 +60,10 @@ export type ActionsRegistryActionOptions< // @public (undocumented) export interface ActionsRegistryService { // (undocumented) - register( + register< + TInputSchema extends AnyZodObject, + TOutputSchema extends AnyZodObject, + >( options: ActionsRegistryActionOptions, ): void; } diff --git a/packages/backend-plugin-api/src/services/definitions/ActionsRegistryService.ts b/packages/backend-plugin-api/src/services/definitions/ActionsRegistryService.ts index 3044c84ed8..44df71f41b 100644 --- a/packages/backend-plugin-api/src/services/definitions/ActionsRegistryService.ts +++ b/packages/backend-plugin-api/src/services/definitions/ActionsRegistryService.ts @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { z, ZodType } from 'zod'; +import { z, AnyZodObject } from 'zod'; import { LoggerService } from './LoggerService'; import { BackstageCredentials } from './AuthService'; /** * @public */ -export type ActionsRegistryActionContext = { +export type ActionsRegistryActionContext = { input: z.infer; logger: LoggerService; credentials: BackstageCredentials; @@ -30,8 +30,8 @@ export type ActionsRegistryActionContext = { * @public */ export type ActionsRegistryActionOptions< - TInputSchema extends ZodType, - TOutputSchema extends ZodType, + TInputSchema extends AnyZodObject, + TOutputSchema extends AnyZodObject, > = { name: string; title: string; @@ -53,7 +53,10 @@ export type ActionsRegistryActionOptions< * @public */ export interface ActionsRegistryService { - register( + register< + TInputSchema extends AnyZodObject, + TOutputSchema extends AnyZodObject, + >( options: ActionsRegistryActionOptions, ): void; }