@backstage/plugin-permission-react 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @backstage/plugin-permission-react
2
2
 
3
+ ## 0.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 1ed305728b: Bump `node-fetch` to version 2.6.7 and `cross-fetch` to version 3.1.5
8
+ - c77c5c7eb6: Added `backstage.role` to `package.json`
9
+ - Updated dependencies
10
+ - @backstage/core-plugin-api@0.6.1
11
+ - @backstage/plugin-permission-common@0.5.0
12
+ - @backstage/config@0.1.14
13
+
3
14
  ## 0.3.0
4
15
 
5
16
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -23,9 +23,16 @@ declare type AsyncPermissionResult = {
23
23
  error?: Error;
24
24
  };
25
25
  /**
26
- * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional
27
- * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See
28
- * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.
26
+ * React hook utility for authorization. Given a
27
+ * {@link @backstage/plugin-permission-common#Permission} and an optional
28
+ * resourceRef, it will return whether or not access is allowed (for the given
29
+ * resource, if resourceRef is provided). See
30
+ * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for
31
+ * more details.
32
+ *
33
+ * Note: This hook uses stale-while-revalidate to help avoid flicker in UI
34
+ * elements that would be conditionally rendered based on the `allowed` result
35
+ * of this hook.
29
36
  * @public
30
37
  */
31
38
  declare const usePermission: (permission: Permission, resourceRef?: string | undefined) => AsyncPermissionResult;
package/dist/index.esm.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
2
  import { Route } from 'react-router';
3
3
  import { createApiRef, useApi, useApp } from '@backstage/core-plugin-api';
4
- import useAsync from 'react-use/lib/useAsync';
5
4
  import { PermissionClient, AuthorizeResult } from '@backstage/plugin-permission-common';
5
+ import useSWR from 'swr';
6
6
 
7
7
  const permissionApiRef = createApiRef({
8
8
  id: "plugin.permission.api"
@@ -26,20 +26,17 @@ class IdentityPermissionApi {
26
26
 
27
27
  const usePermission = (permission, resourceRef) => {
28
28
  const permissionApi = useApi(permissionApiRef);
29
- const { loading, error, value } = useAsync(async () => {
30
- const { result } = await permissionApi.authorize({
31
- permission,
32
- resourceRef
33
- });
29
+ const { data, error } = useSWR({ permission, resourceRef }, async (args) => {
30
+ const { result } = await permissionApi.authorize(args);
34
31
  return result;
35
- }, [permissionApi, permission, resourceRef]);
36
- if (loading) {
37
- return { loading: true, allowed: false };
38
- }
32
+ });
39
33
  if (error) {
40
34
  return { error, loading: false, allowed: false };
41
35
  }
42
- return { loading: false, allowed: value === AuthorizeResult.ALLOW };
36
+ if (data === void 0) {
37
+ return { loading: true, allowed: false };
38
+ }
39
+ return { loading: false, allowed: data === AuthorizeResult.ALLOW };
43
40
  };
44
41
 
45
42
  const PermissionedRoute = (props) => {
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeQuery,\n AuthorizeDecision,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeQuery): Promise<AuthorizeDecision>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeQuery,\n AuthorizeDecision,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n config: Config;\n discovery: DiscoveryApi;\n identity: IdentityApi;\n }) {\n const { config, discovery, identity } = options;\n const permissionClient = new PermissionClient({ discovery, config });\n return new IdentityPermissionApi(permissionClient, identity);\n }\n\n async authorize(request: AuthorizeQuery): Promise<AuthorizeDecision> {\n const response = await this.permissionClient.authorize(\n [request],\n await this.identityApi.getCredentials(),\n );\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport useAsync from 'react-use/lib/useAsync';\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n\n const { loading, error, value } = useAsync(async () => {\n const { result } = await permissionApi.authorize({\n permission,\n resourceRef,\n });\n\n return result;\n }, [permissionApi, permission, resourceRef]);\n\n if (loading) {\n return { loading: true, allowed: false };\n }\n if (error) {\n return { error, loading: false, allowed: false };\n }\n return { loading: false, allowed: value === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":[],"mappings":";;;;;;MAqCa,mBAA0C,aAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,QAAQ,WAAW,aAAa;AACxC,UAAM,mBAAmB,IAAI,iBAAiB,EAAE,WAAW;AAC3D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAqD;AACnE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAC3C,CAAC,UACD,MAAM,KAAK,YAAY;AAEzB,WAAO,SAAS;AAAA;AAAA;;MCdP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgB,OAAO;AAE7B,QAAM,EAAE,SAAS,OAAO,UAAU,SAAS,YAAY;AACrD,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AAAA,MAC/C;AAAA,MACA;AAAA;AAGF,WAAO;AAAA,KACN,CAAC,eAAe,YAAY;AAE/B,MAAI,SAAS;AACX,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,SAAO,EAAE,SAAS,OAAO,SAAS,UAAU,gBAAgB;AAAA;;MC9BjD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAM;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,6CAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,6CAAQ,OAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeQuery,\n AuthorizeDecision,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeQuery): Promise<AuthorizeDecision>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeQuery,\n AuthorizeDecision,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n config: Config;\n discovery: DiscoveryApi;\n identity: IdentityApi;\n }) {\n const { config, discovery, identity } = options;\n const permissionClient = new PermissionClient({ discovery, config });\n return new IdentityPermissionApi(permissionClient, identity);\n }\n\n async authorize(request: AuthorizeQuery): Promise<AuthorizeDecision> {\n const response = await this.permissionClient.authorize(\n [request],\n await this.identityApi.getCredentials(),\n );\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\nimport useSWR from 'swr';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utility for authorization. Given a\n * {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given\n * resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for\n * more details.\n *\n * Note: This hook uses stale-while-revalidate to help avoid flicker in UI\n * elements that would be conditionally rendered based on the `allowed` result\n * of this hook.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n const { data, error } = useSWR({ permission, resourceRef }, async args => {\n const { result } = await permissionApi.authorize(args);\n return result;\n });\n\n if (error) {\n return { error, loading: false, allowed: false };\n }\n if (data === undefined) {\n return { loading: true, allowed: false };\n }\n return { loading: false, allowed: data === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":[],"mappings":";;;;;;MAqCa,mBAA0C,aAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,QAAQ,WAAW,aAAa;AACxC,UAAM,mBAAmB,IAAI,iBAAiB,EAAE,WAAW;AAC3D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAqD;AACnE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAC3C,CAAC,UACD,MAAM,KAAK,YAAY;AAEzB,WAAO,SAAS;AAAA;AAAA;;MCPP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgB,OAAO;AAC7B,QAAM,EAAE,MAAM,UAAU,OAAO,EAAE,YAAY,eAAe,OAAM,SAAQ;AACxE,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AACjD,WAAO;AAAA;AAGT,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,MAAI,SAAS,QAAW;AACtB,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,SAAO,EAAE,SAAS,OAAO,SAAS,SAAS,gBAAgB;AAAA;;MChChD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAM;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,6CAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,6CAAQ,OAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-permission-react",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "main": "dist/index.esm.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -9,6 +9,9 @@
9
9
  "main": "dist/index.esm.js",
10
10
  "types": "dist/index.d.ts"
11
11
  },
12
+ "backstage": {
13
+ "role": "web-library"
14
+ },
12
15
  "homepage": "https://backstage.io",
13
16
  "repository": {
14
17
  "type": "git",
@@ -19,28 +22,30 @@
19
22
  "backstage"
20
23
  ],
21
24
  "scripts": {
22
- "build": "backstage-cli build",
23
- "lint": "backstage-cli lint",
24
- "test": "backstage-cli test",
25
- "prepack": "backstage-cli prepack",
26
- "postpack": "backstage-cli postpack",
27
- "clean": "backstage-cli clean"
25
+ "build": "backstage-cli package build",
26
+ "lint": "backstage-cli package lint",
27
+ "test": "backstage-cli package test",
28
+ "prepack": "backstage-cli package prepack",
29
+ "postpack": "backstage-cli package postpack",
30
+ "clean": "backstage-cli package clean",
31
+ "start": "backstage-cli package start"
28
32
  },
29
33
  "dependencies": {
30
- "@backstage/config": "^0.1.13",
31
- "@backstage/core-plugin-api": "^0.6.0",
32
- "@backstage/plugin-permission-common": "^0.4.0",
33
- "cross-fetch": "^3.0.6",
34
+ "@backstage/config": "^0.1.14",
35
+ "@backstage/core-plugin-api": "^0.6.1",
36
+ "@backstage/plugin-permission-common": "^0.5.0",
37
+ "cross-fetch": "^3.1.5",
34
38
  "react-router": "6.0.0-beta.0",
35
- "react-use": "^17.2.4"
39
+ "react-use": "^17.2.4",
40
+ "swr": "^1.1.2"
36
41
  },
37
42
  "peerDependencies": {
38
43
  "@types/react": "^16.13.1 || ^17.0.0",
39
44
  "react": "^16.13.1 || ^17.0.0"
40
45
  },
41
46
  "devDependencies": {
42
- "@backstage/cli": "^0.12.0",
43
- "@backstage/test-utils": "^0.2.3",
47
+ "@backstage/cli": "^0.14.0",
48
+ "@backstage/test-utils": "^0.2.5",
44
49
  "@testing-library/jest-dom": "^5.10.1",
45
50
  "@testing-library/react": "^11.2.5",
46
51
  "@types/jest": "^26.0.7"
@@ -48,5 +53,5 @@
48
53
  "files": [
49
54
  "dist"
50
55
  ],
51
- "gitHead": "600d6e3c854bbfb12a0078ca6f726d1c0d1fea0b"
56
+ "gitHead": "4805c3d13ce9bfc369e53c271b1b95e722b3b4dc"
52
57
  }
package/dist/index.cjs.js DELETED
@@ -1,75 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
- var reactRouter = require('react-router');
7
- var corePluginApi = require('@backstage/core-plugin-api');
8
- var useAsync = require('react-use/lib/useAsync');
9
- var pluginPermissionCommon = require('@backstage/plugin-permission-common');
10
-
11
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
-
13
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
14
- var useAsync__default = /*#__PURE__*/_interopDefaultLegacy(useAsync);
15
-
16
- const permissionApiRef = corePluginApi.createApiRef({
17
- id: "plugin.permission.api"
18
- });
19
-
20
- class IdentityPermissionApi {
21
- constructor(permissionClient, identityApi) {
22
- this.permissionClient = permissionClient;
23
- this.identityApi = identityApi;
24
- }
25
- static create(options) {
26
- const { config, discovery, identity } = options;
27
- const permissionClient = new pluginPermissionCommon.PermissionClient({ discovery, config });
28
- return new IdentityPermissionApi(permissionClient, identity);
29
- }
30
- async authorize(request) {
31
- const response = await this.permissionClient.authorize([request], await this.identityApi.getCredentials());
32
- return response[0];
33
- }
34
- }
35
-
36
- const usePermission = (permission, resourceRef) => {
37
- const permissionApi = corePluginApi.useApi(permissionApiRef);
38
- const { loading, error, value } = useAsync__default["default"](async () => {
39
- const { result } = await permissionApi.authorize({
40
- permission,
41
- resourceRef
42
- });
43
- return result;
44
- }, [permissionApi, permission, resourceRef]);
45
- if (loading) {
46
- return { loading: true, allowed: false };
47
- }
48
- if (error) {
49
- return { error, loading: false, allowed: false };
50
- }
51
- return { loading: false, allowed: value === pluginPermissionCommon.AuthorizeResult.ALLOW };
52
- };
53
-
54
- const PermissionedRoute = (props) => {
55
- const { permission, resourceRef, errorComponent, ...otherProps } = props;
56
- const permissionResult = usePermission(permission, resourceRef);
57
- const app = corePluginApi.useApp();
58
- const { NotFoundErrorPage } = app.getComponents();
59
- let shownElement = errorComponent === void 0 ? /* @__PURE__ */ React__default["default"].createElement(NotFoundErrorPage, null) : errorComponent;
60
- if (permissionResult.loading) {
61
- shownElement = null;
62
- } else if (permissionResult.allowed) {
63
- shownElement = props.element;
64
- }
65
- return /* @__PURE__ */ React__default["default"].createElement(reactRouter.Route, {
66
- ...otherProps,
67
- element: shownElement
68
- });
69
- };
70
-
71
- exports.IdentityPermissionApi = IdentityPermissionApi;
72
- exports.PermissionedRoute = PermissionedRoute;
73
- exports.permissionApiRef = permissionApiRef;
74
- exports.usePermission = usePermission;
75
- //# sourceMappingURL=index.cjs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeQuery,\n AuthorizeDecision,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeQuery): Promise<AuthorizeDecision>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeQuery,\n AuthorizeDecision,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n config: Config;\n discovery: DiscoveryApi;\n identity: IdentityApi;\n }) {\n const { config, discovery, identity } = options;\n const permissionClient = new PermissionClient({ discovery, config });\n return new IdentityPermissionApi(permissionClient, identity);\n }\n\n async authorize(request: AuthorizeQuery): Promise<AuthorizeDecision> {\n const response = await this.permissionClient.authorize(\n [request],\n await this.identityApi.getCredentials(),\n );\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport useAsync from 'react-use/lib/useAsync';\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n\n const { loading, error, value } = useAsync(async () => {\n const { result } = await permissionApi.authorize({\n permission,\n resourceRef,\n });\n\n return result;\n }, [permissionApi, permission, resourceRef]);\n\n if (loading) {\n return { loading: true, allowed: false };\n }\n if (error) {\n return { error, loading: false, allowed: false };\n }\n return { loading: false, allowed: value === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":["createApiRef","PermissionClient","useApi","useAsync","AuthorizeResult","useApp","Route"],"mappings":";;;;;;;;;;;;;;;MAqCa,mBAA0CA,2BAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,QAAQ,WAAW,aAAa;AACxC,UAAM,mBAAmB,IAAIC,wCAAiB,EAAE,WAAW;AAC3D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAqD;AACnE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAC3C,CAAC,UACD,MAAM,KAAK,YAAY;AAEzB,WAAO,SAAS;AAAA;AAAA;;MCdP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgBC,qBAAO;AAE7B,QAAM,EAAE,SAAS,OAAO,UAAUC,6BAAS,YAAY;AACrD,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AAAA,MAC/C;AAAA,MACA;AAAA;AAGF,WAAO;AAAA,KACN,CAAC,eAAe,YAAY;AAE/B,MAAI,SAAS;AACX,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,SAAO,EAAE,SAAS,OAAO,SAAS,UAAUC,uCAAgB;AAAA;;MC9BjD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAMC;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,iEAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,iEAAQC,mBAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;;;;"}