feat: tweak actions registry format

Signed-off-by: benjdlambert <ben@blam.sh>
This commit is contained in:
benjdlambert
2025-06-11 19:06:35 +02:00
parent f7e7469944
commit 320d12b726
4 changed files with 25 additions and 15 deletions
@@ -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<TInputSchema extends ZodType, TOutputSchema extends ZodType>(
options: ActionsRegistryActionOptions<TInputSchema, TOutputSchema>,
): void {
register<
TInputSchema extends AnyZodObject,
TOutputSchema extends AnyZodObject,
>(options: ActionsRegistryActionOptions<TInputSchema, TOutputSchema>): void {
const id = `${this.metadata.getId()}:${options.name}`;
if (this.actions.has(id)) {
@@ -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' }),
});
},
+8 -5
View File
@@ -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<TInputSchema extends ZodType> = {
export type ActionsRegistryActionContext<TInputSchema extends AnyZodObject> = {
input: z.infer<TInputSchema>;
logger: LoggerService;
credentials: BackstageCredentials;
@@ -38,8 +38,8 @@ export type ActionsRegistryActionContext<TInputSchema extends ZodType> = {
// @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<TInputSchema extends ZodType, TOutputSchema extends ZodType>(
register<
TInputSchema extends AnyZodObject,
TOutputSchema extends AnyZodObject,
>(
options: ActionsRegistryActionOptions<TInputSchema, TOutputSchema>,
): void;
}
@@ -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<TInputSchema extends ZodType> = {
export type ActionsRegistryActionContext<TInputSchema extends AnyZodObject> = {
input: z.infer<TInputSchema>;
logger: LoggerService;
credentials: BackstageCredentials;
@@ -30,8 +30,8 @@ export type ActionsRegistryActionContext<TInputSchema extends ZodType> = {
* @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<TInputSchema extends ZodType, TOutputSchema extends ZodType>(
register<
TInputSchema extends AnyZodObject,
TOutputSchema extends AnyZodObject,
>(
options: ActionsRegistryActionOptions<TInputSchema, TOutputSchema>,
): void;
}