diff --git a/.changeset/healthy-toes-laugh.md b/.changeset/healthy-toes-laugh.md new file mode 100644 index 0000000000..e0db38e5e3 --- /dev/null +++ b/.changeset/healthy-toes-laugh.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-permission-node': patch +--- + +Add helpers for creating PermissionRules with inferred types diff --git a/plugins/permission-node/api-report.md b/plugins/permission-node/api-report.md index 25a8be06c8..e743f18311 100644 --- a/plugins/permission-node/api-report.md +++ b/plugins/permission-node/api-report.md @@ -95,6 +95,22 @@ export const createPermissionIntegrationRouter: (options: { getResource: (resourceRef: string) => Promise; }) => Router; +// @public +export const createPermissionRule: < + TResource, + TQuery, + TParams extends unknown[], +>( + rule: PermissionRule, +) => PermissionRule; + +// @public +export const makeCreatePermissionRule: () => < + TParams extends unknown[], +>( + rule: PermissionRule, +) => PermissionRule; + // @public export interface PermissionPolicy { // (undocumented) diff --git a/plugins/permission-node/src/integration/createPermissionRule.ts b/plugins/permission-node/src/integration/createPermissionRule.ts new file mode 100644 index 0000000000..f3a0519a70 --- /dev/null +++ b/plugins/permission-node/src/integration/createPermissionRule.ts @@ -0,0 +1,45 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { PermissionRule } from '../types'; + +/** + * Helper function to ensure that {@link PermissionRule} definitions are typed correctly. + * + * @public + */ +export const createPermissionRule = < + TResource, + TQuery, + TParams extends unknown[], +>( + rule: PermissionRule, +) => rule; + +/** + * Helper for making plugin-specific createPermissionRule functions, that have + * the TResource and TQuery type parameters populated but infer the params from + * the supplied rule. This helps ensure that rules created for this plugin use + * consistent types for the resource and query. + * + * @public + */ +export const makeCreatePermissionRule = + () => + ( + rule: PermissionRule, + ) => + createPermissionRule(rule); diff --git a/plugins/permission-node/src/integration/index.ts b/plugins/permission-node/src/integration/index.ts index f070d57c8f..978342e4ed 100644 --- a/plugins/permission-node/src/integration/index.ts +++ b/plugins/permission-node/src/integration/index.ts @@ -18,3 +18,4 @@ export * from './createConditionFactory'; export * from './createConditionExports'; export * from './createConditionTransformer'; export * from './createPermissionIntegrationRouter'; +export * from './createPermissionRule';