diff --git a/.changeset/tidy-ads-cover.md b/.changeset/tidy-ads-cover.md new file mode 100644 index 0000000000..98434fb480 --- /dev/null +++ b/.changeset/tidy-ads-cover.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-permission-node': patch +--- + +Improved type inference when passing a `PermissionResourceRef` to `createPermissionRule`. diff --git a/plugins/permission-node/report.api.md b/plugins/permission-node/report.api.md index 9131d6e2a9..71298a7d81 100644 --- a/plugins/permission-node/report.api.md +++ b/plugins/permission-node/report.api.md @@ -201,20 +201,16 @@ export function createPermissionResourceRef(): { // @public export function createPermissionRule< - TResource, - TQuery, - TQueryOutput extends TQuery, - TResourceType extends string, + TRef extends PermissionResourceRef, TParams extends PermissionRuleParams = undefined, >( - rule: CreatePermissionRuleOptions< - TResource, - TQuery, - TQueryOutput, - TResourceType, - TParams - >, -): PermissionRule; + rule: CreatePermissionRuleOptions, +): PermissionRule< + TRef['TResource'], + TRef['TQuery'], + TRef['resourceType'], + TParams +>; // @public @deprecated export function createPermissionRule< @@ -228,19 +224,18 @@ export function createPermissionRule< // @public (undocumented) export type CreatePermissionRuleOptions< - TResource, - TQuery, - TQueryOutput extends TQuery, - TResourceType extends string, + TRef extends PermissionResourceRef, TParams extends PermissionRuleParams, -> = { - name: string; - description: string; - resourceRef: PermissionResourceRef; - paramsSchema?: z.ZodSchema; - apply(resource: TResource, params: NoInfer_2): boolean; - toQuery(params: NoInfer_2): PermissionCriteria; -}; +> = TRef extends PermissionResourceRef + ? { + name: string; + description: string; + resourceRef: TRef; + paramsSchema?: z.ZodSchema; + apply(resource: IResource, params: NoInfer_2): boolean; + toQuery(params: NoInfer_2): PermissionCriteria; + } + : never; // @public export const isAndCriteria: ( diff --git a/plugins/permission-node/src/integration/createPermissionRule.ts b/plugins/permission-node/src/integration/createPermissionRule.ts index 4625fb5af8..ba6f41145b 100644 --- a/plugins/permission-node/src/integration/createPermissionRule.ts +++ b/plugins/permission-node/src/integration/createPermissionRule.ts @@ -27,36 +27,35 @@ import { NoInfer } from './util'; * @public */ export type CreatePermissionRuleOptions< - TResource, - TQuery, - TQueryOutput extends TQuery, - TResourceType extends string, + TRef extends PermissionResourceRef, TParams extends PermissionRuleParams, -> = { - name: string; - description: string; +> = TRef extends PermissionResourceRef + ? { + name: string; + description: string; - resourceRef: PermissionResourceRef; + resourceRef: TRef; - /** - * A ZodSchema that reflects the structure of the parameters that are passed to - */ - paramsSchema?: z.ZodSchema; + /** + * A ZodSchema that reflects the structure of the parameters that are passed to + */ + paramsSchema?: z.ZodSchema; - /** - * Apply this rule to a resource already loaded from a backing data source. The params are - * arguments supplied for the rule; for example, a rule could be `isOwner` with entityRefs as the - * params. - */ - apply(resource: TResource, params: NoInfer): boolean; + /** + * Apply this rule to a resource already loaded from a backing data source. The params are + * arguments supplied for the rule; for example, a rule could be `isOwner` with entityRefs as the + * params. + */ + apply(resource: IResource, params: NoInfer): boolean; - /** - * Translate this rule to criteria suitable for use in querying a backing data store. The criteria - * can be used for loading a collection of resources efficiently with conditional criteria already - * applied. - */ - toQuery(params: NoInfer): PermissionCriteria; -}; + /** + * Translate this rule to criteria suitable for use in querying a backing data store. The criteria + * can be used for loading a collection of resources efficiently with conditional criteria already + * applied. + */ + toQuery(params: NoInfer): PermissionCriteria; + } + : never; /** * Helper function to create a {@link PermissionRule} for a specific resource type using a {@link PermissionResourceRef}. @@ -64,20 +63,16 @@ export type CreatePermissionRuleOptions< * @public */ export function createPermissionRule< - TResource, - TQuery, - TQueryOutput extends TQuery, - TResourceType extends string, + TRef extends PermissionResourceRef, TParams extends PermissionRuleParams = undefined, >( - rule: CreatePermissionRuleOptions< - TResource, - TQuery, - TQueryOutput, - TResourceType, - TParams - >, -): PermissionRule; + rule: CreatePermissionRuleOptions, +): PermissionRule< + TRef['TResource'], + TRef['TQuery'], + TRef['resourceType'], + TParams +>; /** * Helper function to ensure that {@link PermissionRule} definitions are typed correctly. * @@ -95,22 +90,19 @@ export function createPermissionRule< export function createPermissionRule< TResource, TQuery, - TQueryOutput extends TQuery, TResourceType extends string, + TRef extends PermissionResourceRef, TParams extends PermissionRuleParams = undefined, >( rule: | PermissionRule - | CreatePermissionRuleOptions< - TResource, - TQuery, - TQueryOutput, - TResourceType, - TParams - >, + | CreatePermissionRuleOptions, ): PermissionRule { if ('resourceRef' in rule) { - return { ...rule, resourceType: rule.resourceRef.resourceType }; + return { + ...rule, + resourceType: rule.resourceRef.resourceType as TResourceType, + } as PermissionRule; } return rule; }