diff --git a/.changeset/serious-dryers-grin.md b/.changeset/serious-dryers-grin.md new file mode 100644 index 0000000000..24d17f4959 --- /dev/null +++ b/.changeset/serious-dryers-grin.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Fix entity content extension filtering. diff --git a/.changeset/silver-crabs-exercise.md b/.changeset/silver-crabs-exercise.md new file mode 100644 index 0000000000..ff34b5a3c3 --- /dev/null +++ b/.changeset/silver-crabs-exercise.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-api-docs': minor +--- + +Migrate the `api-docs` to the new frontend system. It is experimental and available via alpha subpath. diff --git a/packages/app-next/app-config.yaml b/packages/app-next/app-config.yaml index b07f556742..28d05c902b 100644 --- a/packages/app-next/app-config.yaml +++ b/packages/app-next/app-config.yaml @@ -1,6 +1,7 @@ app: experimental: packages: 'all' # ✨ + routes: bindings: catalog.viewTechDoc: techdocs.docRoot @@ -11,6 +12,7 @@ app: # - apis.plugin.graphiql.browse.gitlab: true - graphiql-endpoint:graphiql/gitlab: true + # Entity page cards - entity-card:catalog/about - entity-card:catalog/labels - entity-card:catalog/links: @@ -21,7 +23,15 @@ app: config: height: 300 - entity-card:azure-devops/readme + - entity-card:api-docs/has-apis + - entity-card:api-docs/consumed-apis + - entity-card:api-docs/provided-apis + - entity-card:api-docs/providing-components + - entity-card:api-docs/consuming-components + # Entity page content + - entity-content:api-docs/definition + - entity-content:api-docs/apis - entity-content:techdocs - entity-content:azure-devops/pipelines - entity-content:azure-devops/pull-requests diff --git a/plugins/api-docs/README-alpha.md b/plugins/api-docs/README-alpha.md new file mode 100644 index 0000000000..cb94144e0c --- /dev/null +++ b/plugins/api-docs/README-alpha.md @@ -0,0 +1,1168 @@ +# Api Docs + +> [!WARNING] +> This documentation is made for those using the experimental new Frontend system. +> If you are not using the new frontend system, please go [here](./README.md). + +This is an extension for the catalog plugin that provides components to discover and display API entities. +APIs define the interface between components, see the [system model](https://backstage.io/docs/features/software-catalog/system-model) for details. +They are defined in machine readable formats and provide a human readable documentation. + +The plugin provides a standalone list of APIs, as well as an integration into the API tab of a catalog entity. + + + + + + + +Right now, the following API formats are supported: + +- [OpenAPI](https://swagger.io/specification/) 2 & 3 +- [AsyncAPI](https://www.asyncapi.com/docs/reference/specification/latest) +- [GraphQL](https://graphql.org/learn/schema/) + +Other formats are displayed as plain text, but this can easily be extended. + +To fill the catalog with APIs, [provide entities of kind API](https://backstage.io/docs/features/software-catalog/descriptor-format#kind-api). +To link that a component provides or consumes an API, see the [`providesApis`](https://backstage.io/docs/features/software-catalog/descriptor-format#specprovidesapis-optional) and [`consumesApis`](https://backstage.io/docs/features/software-catalog/descriptor-format#specconsumesapis-optional) properties on the Component kind. + +## Table of Content + +- [Installation](#installation) +- [Customization](#customization) + - [Packages](#packages) + - [Routes](#routes) + - [Extensions](#extensions) + - [Apis Nav Item](#apis-nav-item) + - [Apis Explorer Page](#apis-explore-page) + - [Apis Entity Cards](#apis-entities-cards) + - [Has Apis Entity Card](#has-apis-entity-card) + - [Definition Entity Card](#definition-entity-card) + - [Provided Apis Entity Card](#provided-apis-entity-card) + - [Consumed Apis Entity Card](#consumed-apis-entity-card) + - [Providing Components Entity Card](#providing-components-entity-card) + - [Consuming Components Entity Card](#consuming-components-entity-card) + - [Apis Entity Contents](#apis-entity-contents) + - [Definition Entity Content](#definition-entity-content) + - [Apis Entity Content](#apis-entity-content) + - [Apis Config Api](#apis-config-api) + - [Custom Api Renderings](#custom-api-renderings) + - [Adding Swagger UI Interceptor](#adding-requestinterceptor-to-swagger-ui) + - [Providing Swagger UI Specific Supported Methods](#provide-specific-supported-methods-to-swagger-ui) + - [Integrations](#integrations) + - [Implementing OAuth 2 Authorization Code flow with Swagger UI](#implementing-oauth-2-authorization-code-flow-with-swagger-ui) + +## Installation + +1. Install the `api-docs` plugin in you Backstage app: + + ```bash + # From your Backstage root directory + yarn --cwd packages/app add @backstage/plugin-api-docs + ``` + +2. Enable which entity cards and tabs you would like to see on the catalog entity page: + + ```yaml + # app-config.yaml + app: + experimental: + # Auto discovering all plugins extensions + packages: all + extensions: + # Enabling some entity Cards + # The cards will be displayed in the same order it appears in this setting list + # Shows a table of components that provides a particular api + - entity-card:api-docs/providing-components: + config: + # Presenting the card ony for entites of kind api + filter: kind:api + # Shows a table of components that consumes a particular api + - entity-card:api-docs/consuming-components: + config: + # Presenting the card ony for entites of kind api + filter: kind:api + # Enabling some Contents + # The contents will be displayed in the same order it appears in this setting list + # Shows a "Definition" tab for entities of kind api + - entity-content:api-docs/definition + # Shows an "Apis" tab for entities of kind component + - entity-content:api-docs/apis + ``` + +3. Then start the app, navigate to an entity's page and see the cards and contents in there; +4. You can also access the Apis explorer page by clicking on the "APIs" sidebar item. + +## Customization + +### Packages + +By default, the `api-docs` can be automatically discovered, and it is also possible to enable this plugin only in certain [environments](https://backstage.io/docs/conf/writing/#configuration-files). For more package configuration options, see [this](https://backstage.io/docs/frontend-system/architecture/app/#feature-discovery) documentation. + +### Routes + +The `api-docs` plugin exposes regular and external routes that can be used to configure route bindings. + +| Key | Type | Description | +| ------------- | -------------- | -------------------------------------- | +| `root` | Regular route | A route ref to the Apis Explorer page. | +| `registerApi` | External route | A route ref to Register Api page. | + +As an example, here is an association between the external register api page and a regular route from another plugin: + +```yaml +# app-config.yaml +app: + routes: + bindings: + # example binding the external register api route + api-docs.registerApi: . +``` + +Additionally, it is possible to point a route from another plugin to the Apis explorer page: + +```yaml +# app-config.yaml +app: + routes: + bindings: + # example binding a external route to the apis explorer page + .: api-docs.root +``` + +Route binding is also possible through code. For more information, see [this](https://backstage.io/docs/frontend-system/architecture/routes#binding-external-route-references) documentation. + +### Extensions + +#### Apis Nav Item + +This [nav item](https://backstage.io/docs/reference/frontend-plugin-api.createnavitemextension) extension adds a link to the Apis Explorer page in the main app sidebar. + +| Kind | Namespace | Name | Id | +| ---------- | ---------- | ---- | ------------------- | +| `nav-item` | `api-docs` | - | `nav-item:api-docs` | + +##### Disable + +This extension is enabled by default when you install the `api-docs` plugin, but you can disable it and prevent it from showing up in the sidebar by setting the following configuration: + +```yaml +# app-config.yaml +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # example disabling the apis docs nav item extension + - nav-item:api-docs: false + # or + # - nav-item:api-docs: + # disabled: true +``` + +To enable the extension again, simple remove the previous `nav-item:api-docs: false` configuration or do: + +```yaml +# app-config.yaml +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - nav-item:api-docs + # or + # - nav-item:api-docs: true + # or + # - nav-item:api-docs: + # disabled: false +``` + +##### Config + +The apis nav item can be customized under the `app.extensions.nav-item:api-docs.config` key in `app-config.yaml`. Configurations include: + +```yaml +# app-config.yaml +# example configuring the apis docs nav item extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - nav-item:api-docs: + config: + # The nav item title text, defaults to "APIs" + title: 'Apis Explorer' + # The nav item path text, defaults to "/api-docs" + path: '/apis-explorer' +``` + +##### Override + +The apis nav item icon can only be changed by overriding the extension, as the icon cannot be changed via the `app-config.yaml` file. + +Here is an example overriding the nav item extension with a custom icon component: + +```tsx +import { createExtensionOverrides, createNavItemExtension } from '@backstage/backstage-plugin-api'; +import { convertLegacyRouteRef } from '@backstage/core-compat-api'; + +export default createExtensionOverrides( + extensions: [ + createNavItemExtension({ + // These namespace is necessary so the system knows that this extension will override the default nav item provided by the 'api-docs' plugin + namespace: 'api-docs', + // It's your choice whether to use the original extension's title or a different one + title: 'APIs', + // To continue pointing to the same page as the original extension, use the same route ref + routeRef: convertLegacyRouteRef(rootRoute), + // Custom icon components are loaded here + loader: () => import('./components').then(m => ) + }) + ] +); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +#### Apis Explore Page + +This `api-docs` plugin installs an "Apis Explore" page extension that helps you visualize apis registered in the Backstage software catalog. + +| Kind | Namespace | Name | Id | +| ------ | ---------- | ---- | --------------- | +| `page` | `api-docs` | - | `page:api-docs` | + +##### Disable/Enable + +The explore page extension is enable by default when you install the `api-docs` plugin, for disabling it, set the configuration below: + +> [!CAUTION] +> The `api-docs` plugin also install a sidebar item that points to this page, remember to disable the nav item as well otherwise it will point to a not found page. + +```yaml +# app-config.yaml +# example disabling the apis docs explorer page +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - page:api-docs: false + # or + # - page:api-docs: + # disabled: true +``` + +To enable the extension again, simple remove the previous `page:api-docs: false` configuration or do: + +```yaml +# app-config.yaml +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - page:api-docs + # or + # - page:api-docs: true + # or + # - page:api-docs: + # disabled: false +``` + +##### Override + +The explorer page implementation can be [overridden](https://backstage.io/docs/frontend-system/architecture/extension-overrides) in situations where its default extension is not customizable enough. + +Here is an example overriding the APIs Explorer page component: + +```tsx +import { createExtensionOverrides, createPageExtension } from '@backstage/backstage-plugin-api'; +import { convertLegacyRouteRef } from '@backstage/core-compat-api'; + +export default createExtensionOverrides( + extensions: [ + createPageExtension({ + // These namespace is necessary so the system knows that this extension will override the default explorer page provided by the 'api-docs' plugin + namespace: 'api-docs', + // Ommitting name since we are overriding a plugin index page + // It's up to you whether to use the original default path or not, but links that are hardcoded to the default path won't work if you change it + defaultPath: '/api-docs', + // Associating the page with a different route ref may result in the sidebar item or external plugin route pointing to an unreachable page + routeRef: convertLegacyRouteRef(rootRoute), + // Custom page components are loaded here + loader: () => import('./components').then(m => ) + }) + ] +); +``` + +#### Apis Entities Cards + +The `api-docs` provide some entity cards you can enable to customize the Software Catalog entity page. + +> [!IMPORTANT] +> The order in which cards are listed in the configuration file will determine the order in which they appear in overview cards and tab lists on entity pages. + +See a complete cards list below: + +##### Has Apis Entity Card + +An [entity card](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders a table of entities that have an api relation with a particular Software catalog entity. + +| Kind | Namespace | Name | Id | +| ------------- | ---------- | ---------- | ------------------------------- | +| `entity-card` | `api-docs` | `has-apis` | `entity-card:api-docs/has-apis` | + +###### Disable + +This card is disabled by default when you install the `api-docs` plugin, but to ensure the card will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the apis docs has apis entity card extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-card:api-docs/has-apis: false + # or + # - entity-card:api-docs/has-apis: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +For now there is only one configuration available for this entity card extension, which is setting an entity filter that determines when the card should be displayed on the entity page. + +Here is an example showing the `has-apis` overview cards only for entities of kind component: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "component" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-card:api-docs/has-apis: + config: + # The default value is a function that verifies it is a components has api pat of relations + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + filter: 'kind:component' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityCardExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityCardExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'has-apis' entity card extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'has-apis', + // Returing a custom card component + loader: () => + import('./components').then(m => ), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +##### Definition Entity Card + +An [entity card](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders an entity api definition widget. + +| Kind | Namespace | Name | Id | +| ------------- | ---------- | ------------ | --------------------------------- | +| `entity-card` | `api-docs` | `definition` | `entity-card:api-docs/definition` | + +###### Disable + +This card is disabled by default when you install the `api-docs` plugin, but to ensure the card will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the definition entity card extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-card:api-docs/definition: false + # or + # - entity-card:api-docs/definition: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +For now there is only one configuration available for this entity card extension, which is setting an entity filter that determines when the card should be displayed on the entity page. + +Here is an example showing the `definition` overview cards only for entities of kind component: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "component" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-card:api-docs/definition: + config: + # Default to 'kind:api' + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + filter: 'kind:component' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityCardExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityCardExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'definition' entity card extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'definition', + // Returing a custom card component + loader: () => + import('./components').then(m => ), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +##### Provided Apis Entity Card + +An [entity card](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders a table of apis provided by a particular Software Catalog Component. + +| Kind | Namespace | Name | Id | +| ------------- | ---------- | --------------- | ------------------------------------ | +| `entity-card` | `api-docs` | `provided-apis` | `entity-card:api-docs/provided-apis` | + +###### Disable + +This card is disabled by default when you install the `api-docs` plugin, but to ensure the card will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the provided apis entity card extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-card:api-docs/provided-apis: false + # or + # - entity-card:api-docs/provided-apis: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +For now there is only one configuration available for this entity card extension, which is setting an entity filter that determines when the card should be displayed on the entity page. + +Here is an example showing the `provided-apis` overview cards only for entities of kind component: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "component" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-card:api-docs/provided-apis: + config: + # Default to 'kind:component' + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + filter: 'kind:component' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityCardExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityCardExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'provided-apis' entity card extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'provided-apis', + // Returing a custom card component + loader: () => + import('./components').then(m => ), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +##### Consumed Apis Entity Card + +An [entity card](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders a table of apis consumed by a particular Software Catalog Component. + +| Kind | Namespace | Name | Id | +| ------------- | ---------- | --------------- | ------------------------------------ | +| `entity-card` | `api-docs` | `consumed-apis` | `entity-card:api-docs/consumed-apis` | + +###### Disable + +This card is disabled by default when you install the `api-docs` plugin, but to ensure the card will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the consumed apis entity card extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-card:api-docs/consumed-apis: false + # or + # - entity-card:api-docs/consumed-apis: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +For now there is only one configuration available for this entity card extension, which is setting an entity filter that determines when the card should be displayed on the entity page. + +Here is an example showing the `consumed-apis` overview cards only for entities of kind component: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "component" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-card:api-docs/consumed-apis: + config: + # Default to 'kind:component' + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + filter: 'kind:component' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityCardExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityCardExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'consumed-apis' entity card extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'consumed-apis', + // Returing a custom card component + loader: () => + import('./components').then(m => ), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +##### Providing Components Entity Card + +An [entity card](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders a table of components that provides a particular Software Catalog api. + +| Kind | Namespace | Name | Id | +| ------------- | ---------- | ---------------------- | ------------------------------------------- | +| `entity-card` | `api-docs` | `providing-components` | `entity-card:api-docs/providing-components` | + +###### Disable + +This card is disabled by default when you install the `api-docs` plugin, but to ensure the card will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the providing components entity card extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-card:api-docs/providing-components: false + # or + # - entity-card:api-docs/providing-components: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +For now there is only one configuration available for this entity card extension, which is setting an entity filter that determines when the card should be displayed on the entity page. + +Here is an example showing the `providing-components` overview cards only for entities of kind component: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "component" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-card:api-docs/providing-components: + config: + # Default to 'kind:api' + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + filter: 'kind:component' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityCardExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityCardExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'providing-components' entity card extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'providing-components', + // Returing a custom card component + loader: () => + import('./components').then(m => ( + + )), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +##### Consuming Components Entity Card + +An [entity card](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders a table of components that consumes a particular Software Catalog api. + +| Kind | Namespace | Name | Id | +| ------------- | ---------- | ---------------------- | ------------------------------------------- | +| `entity-card` | `api-docs` | `consuming-components` | `entity-card:api-docs/consuming-components` | + +###### Disable + +This card is disabled by default when you install the `api-docs` plugin, but to ensure the card will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the consuming components entity card extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-card:api-docs/consuming-components: false + # or + # - entity-card:api-docs/consuming-components: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +For now there is only one configuration available for this entity card extension, which is setting an entity filter that determines when the card should be displayed on the entity page. + +Here is an example showing the `consuming-components` overview cards only for entities of kind component: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "component" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-card:api-docs/consuming-components: + config: + # Default to 'kind:api' + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + filter: 'kind:component' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityCardExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityCardExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'consuming-components' entity card extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'consuming-components', + // Returing a custom card component + loader: () => + import('./components').then(m => ( + + )), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +### Apis Entity Contents + +The `api-docs` provide some entity contents you can enable to customize the Software Catalog entity page. + +> [!IMPORTANT] +> The order in which contents are listed in the configuration file will determine the order in which they appear in overview contents and tabs on entity pages. + +See a complete contents list below: + +##### Definition Entity Content + +An [entity content](https://github.com/backstage/backstage/blob/master/plugins/catalog-react/api-report-alpha.md) extension that renders a tab in the entity page showing a particular entity api definition. + +| Kind | Namespace | Name | Id | +| ---------------- | ---------- | ------------ | ------------------------------------ | +| `entity-content` | `api-docs` | `definition` | `entity-content:api-docs/definition` | + +###### Disable + +This content is disabled by default when you install the `api-docs` plugin, but to ensure the content will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the definition entity content extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-content:api-docs/definition: false + # or + # - entity-content:api-docs/definition: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +There are a few configuration options for this entity content extension, see the list below: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities with kind "api" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-content:api-docs/definition: + config: + # A text-based query used to filter whether the entity contentextension should be rendered or not. + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + # defaults to 'kind:api' + filter: 'kind:api' + # The entity content table title + # defaults to 'Definition' + title: 'Definition' + # the content tab path + # default to "/definition" + path: '/definition' +``` + +###### Override + +Use extension overrides for completely re-implementing the has apis entity card extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityContentExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityContentExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'definition' entity content extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'definition', + // Returing a custom content component + loader: () => + import('./components').then(m => ( + + )), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +##### Apis Entity Content + +An Entity Content extension that renders a tab in the entity page showing a particular entity consumed and provided apis. + +| Kind | Namespace | Name | Id | +| ---------------- | ---------- | ------ | ------------------------------ | +| `entity-content` | `api-docs` | `apis` | `entity-content:api-docs/apis` | + +###### Disable + +This content is disabled by default when you install the `api-docs` plugin, but to ensure the content will always be disabled or enabled regardless of the extension's default definition, add the following configuration: + +```yaml +# app-config.yaml +# example disabling the apis entity content extension +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + # use false as value for disabling the extension and true for enabling + - entity-content:api-docs/apis: false + # or + # - entity-content:api-docs/apis: + # - config: + # # set 'true' for enabling it again + # disabled: true +``` + +###### Config + +There are a few configuration options for this entity content extension, see the list below: + +```yaml +# app-config.yaml +# example setting the extension to only show up for entities of kind "content" +app: + extensions: + # this is the extension id and it follows the naming pattern bellow: + # /: + - entity-content:api-docs/apis: + config: + # A text-based query used to filter whether the entity contentextension should be rendered or not. + # For more information about entity cards filters, check out this pull request + # https://github.com/backstage/backstage/pull/21480 + # defaults to 'kind:component' + filter: 'kind:component' + # The entity content table title + # defaults to 'Definition' + title: 'Definition' + # the content tab path + # default to "/definition" + path: '/definition' +``` + +###### Override + +Use extension overrides for completely re-implementing the apis entity content extension: + +```tsx +import { createExtensionOverrides } from '@backstage/backstage-plugin-api'; +import { createEntityContentExtension } from '@backstage/plugin-catalog-react/alpha'; + +export default createExtensionOverrides({ + extensions: [ + createEntityContentExtension({ + // These namespace and name necessary so the system knows that this extension will override the default 'apis' entity content extension provided by the 'api-docs' plugin + namespace: 'api-docs', + name: 'apis', + // Returing a custom content component + loader: () => + import('./components').then(m => ), + }), + ], +}); +``` + +For more information about where to place extension overrides, see the official [documentation](https://backstage.io/docs/frontend-system/architecture/extension-overrides). + +#### Apis Config Api + +This is an api used by the `api-docs` plugin to get the api definition widget. + +| Kind | Namespace | Name | Id | +| ----- | ------------------------ | ---- | ---------------------------- | +| `api` | `plugin.api-docs.config` | - | `api:plugin.api-docs.config` | + +Changing the widgets returned by this API requires [overriding](https://backstage.io/docs/frontend-system/architecture/extension-overrides) the default extension implementation. Here are a few common override cases: + +##### Custom Api Renderings + +Add support for additional API types by providing a custom implementation for the `apiDocsConfigRef` and also use this to override the rendering of one of the already supported types. + +This is an example with a made-up renderer for SQL schemas: + +```tsx +import { + createExtensionOverrides, + createApiExtenion, + createApiFactory, +} from '@backstage/frontend-plugin-api'; +import { ApiEntity } from '@backstage/catalog-model'; +import { + apiDocsConfigRef, + ApiDefinitionWidget, + defaultDefinitionWidgets, +} from '@backstage/plugin-api-docs'; +import { SqlRenderer } from '...'; + +export default createExtensionOverrides({ + extensions: [ + createApiExtenion({ + factory: createApiFactory({ + api: apiDocsConfigRef, + deps: {}, + factory: () => { + // load the default widgets + const definitionWidgets = defaultDefinitionWidgets(); + return { + getApiDefinitionWidget: (apiEntity: ApiEntity) => { + // custom rendering for sql + if (apiEntity.spec.type === 'sql') { + return { + type: 'sql', + title: 'SQL', + component: definition => ( + + ), + } as ApiDefinitionWidget; + } + + // fallback to the defaults + return definitionWidgets.find( + d => d.type === apiEntity.spec.type, + ); + }, + }; + }, + }), + }), + ], +}); +``` + +##### Adding `requestInterceptor` to Swagger UI + +Override the config api to configure a [`requestInterceptor` for Swagger UI](https://github.com/swagger-api/swagger-ui/tree/master/flavors/swagger-ui-react#requestinterceptor-proptypesfunc) as following: + +```tsx +import { + createExtensionOverrides, + createApiExtenion, + createApiFactory, +} from '@backstage/frontend-plugin-api'; +import { + apiDocsConfigRef, + defaultDefinitionWidgets, + OpenApiDefinitionWidget, +} from '@backstage/plugin-api-docs'; +import { ApiEntity } from '@backstage/catalog-model'; + +export default createExtensionOverrides({ + extensions: [ + createApiExtenion({ + factory: createApiFactory({ + api: apiDocsConfigRef, + deps: {}, + factory: () => { + // Overriding openapi definition widget to add header + const requestInterceptor = (req: any) => { + req.headers.append('myheader', 'wombats'); + return req; + }; + const definitionWidgets = defaultDefinitionWidgets().map(obj => { + if (obj.type === 'openapi') { + return { + ...obj, + component: definition => ( + + ), + }; + } + return obj; + }); + + return { + getApiDefinitionWidget: (apiEntity: ApiEntity) => { + return definitionWidgets.find( + d => d.type === apiEntity.spec.type, + ); + }, + }; + }, + }), + }), + ], +}); +``` + +In the same way as the `requestInterceptor` you can override any property of Swagger UI. + +##### Provide Specific Supported Methods to Swagger UI + +This can be done through utilising the [supportedSubmitMethods prop](https://github.com/swagger-api/swagger-ui/tree/master/flavors/swagger-ui-react#supportedsubmitmethods-proptypesarrayofproptypesoneofget-put-post-delete-options-head-patch-trace). +If you want to limit the HTTP methods available for the `Try It Out` feature of an OpenAPI API component, you will need to override the config api and list the permitted methods for your API in the `supportedSubmitMethods` parameter: + +```tsx +import { + createExtensionOverrides, + createApiExtenion, + createApiFactory, +} from '@backstage/frontend-plugin-api'; +import { + apiDocsConfigRef, + defaultDefinitionWidgets, + OpenApiDefinitionWidget, +} from '@backstage/plugin-api-docs'; +import { ApiEntity } from '@backstage/catalog-model'; + +export default createExtensionOverrides({ + extensions: [ + createApiExtenion({ + factory: createApiFactory({ + api: apiDocsConfigRef, + deps: {}, + factory: () => { + const supportedSubmitMethods = ['get', 'post', 'put', 'delete']; + const definitionWidgets = defaultDefinitionWidgets().map(obj => { + if (obj.type === 'openapi') { + return { + ...obj, + component: definition => ( + + ), + }; + } + return obj; + }); + + return { + getApiDefinitionWidget: (apiEntity: ApiEntity) => { + return definitionWidgets.find( + d => d.type === apiEntity.spec.type, + ); + }, + }; + }, + }), + }), + ], +}); +``` + +N.B. if you wish to disable the `Try It Out` feature for your API, you can provide an empty list to the `supportedSubmitMethods` parameter. + +### Integrations + +#### Implementing OAuth 2 Authorization Code flow with Swagger UI + +##### Adding `oauth2-redirect.html` to support OAuth2 `redirect_uri` route + +The Swagger UI package by expects to have a route to `/oauth2-redirect.html` which processes +the redirect callback for the OAuth2 Authorization Code flow, however, this file is not installed +by this plugin. + +Grab a copy of [`oauth2-redirect.html`](https://github.com/swagger-api/swagger-ui/blob/master/dist/oauth2-redirect.html) +and put it in the `app/public/` directory in order to enable Swagger UI to complete this redirection. + +This also may require you to adjust `Content Security Policy` header settings of your Backstage application, so that the script in `oauth2-redirect.html` can be executed. Since the script is static we can add the hash of it directly to our CSP policy, which we do by adding the following to the `csp` section of the app configuration: + +```yaml +script-src: + - "'self'" + - "'unsafe-eval'" # this is required for scaffolder usage, and ajv validation. + - "'sha256-GeDavzSZ8O71Jggf/pQkKbt52dfZkrdNMQ3e+Ox+AkI='" # oauth2-redirect.html +``` + +##### Configuring your OAuth2 Client + +You'll need to make sure your OAuth2 client has been registered in your OAuth2 Authentication Server (AS) +with the appropriate `redirect_uris`, `scopes` and `grant_types`. For example, if your AS supports +the [OAuth 2.0 Dynamic Client Registration Protocol](https://tools.ietf.org/html/rfc7591), an example +POST request body would look like this: + +```json +{ + "client_name": "Example Backstage api-docs plugin Swagger UI Client", + "redirect_uris": [ + "https://www.getpostman.com/oauth2/callback", + "http://localhost:3000/oauth2-redirect.html" + "https:///oauth2-redirect.html" + ], + "scope": "read_pets write_pets", + "grant_types": [ + "authorization_code" + ] +} +``` + +The above `redirect_uris` are: + +- [Postman](https://www.postman.com/) testing: `https://www.getpostman.com/oauth2/callback` +- Local Backstage app development: `http://localhost:3000/oauth2-redirect.html` +- Backstage app production: `https:///oauth2-redirect.html` + +##### Configuring OAuth2 in your OpenAPI 3.0 schema + +To configure [OAuth 2 Authorization Code](https://swagger.io/docs/specification/authentication/oauth2/) flow +in your OpenAPI 3.0 schema you'll need something like this snippet: + +```yaml +components: + securitySchemes: + oauth: + type: oauth2 + description: OAuth2 service + flows: + authorizationCode: + authorizationUrl: https://api.example.com/oauth2/authorize + tokenUrl: https://api.example.com/oauth2/token + scopes: + read_pets: read your pets + write_pets: modify pets in your account +security: + oauth: + - [read_pets, write_pets] +``` diff --git a/plugins/api-docs/README.md b/plugins/api-docs/README.md index 9b8eb1669e..c9a8073982 100644 --- a/plugins/api-docs/README.md +++ b/plugins/api-docs/README.md @@ -1,5 +1,8 @@ # API Documentation +> Disclaimer: +> If you are looking for documentation on the experimental new frontend system support, please go [here](./README-alpha.md). + This is an extension for the catalog plugin that provides components to discover and display API entities. APIs define the interface between components, see the [system model](https://backstage.io/docs/features/software-catalog/system-model) for details. They are defined in machine readable formats and provide a human readable documentation. diff --git a/plugins/api-docs/api-report-alpha.md b/plugins/api-docs/api-report-alpha.md new file mode 100644 index 0000000000..1d22283884 --- /dev/null +++ b/plugins/api-docs/api-report-alpha.md @@ -0,0 +1,22 @@ +## API Report File for "@backstage/plugin-api-docs" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +import { BackstagePlugin } from '@backstage/frontend-plugin-api'; +import { ExternalRouteRef } from '@backstage/frontend-plugin-api'; +import { RouteRef } from '@backstage/frontend-plugin-api'; + +// @public (undocumented) +const _default: BackstagePlugin< + { + root: RouteRef; + }, + { + registerApi: ExternalRouteRef; + } +>; +export default _default; + +// (No @packageDocumentation comment for this package) +``` diff --git a/plugins/api-docs/package.json b/plugins/api-docs/package.json index 0cdf564de9..d29a389911 100644 --- a/plugins/api-docs/package.json +++ b/plugins/api-docs/package.json @@ -6,9 +6,22 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.esm.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.tsx", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.tsx" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "frontend-plugin" @@ -35,8 +48,10 @@ "dependencies": { "@asyncapi/react-component": "1.2.13", "@backstage/catalog-model": "workspace:^", + "@backstage/core-compat-api": "workspace:^", "@backstage/core-components": "workspace:^", "@backstage/core-plugin-api": "workspace:^", + "@backstage/frontend-plugin-api": "workspace:^", "@backstage/plugin-catalog": "workspace:^", "@backstage/plugin-catalog-common": "workspace:^", "@backstage/plugin-catalog-react": "workspace:^", diff --git a/plugins/api-docs/src/alpha.tsx b/plugins/api-docs/src/alpha.tsx new file mode 100644 index 0000000000..38b8db89c6 --- /dev/null +++ b/plugins/api-docs/src/alpha.tsx @@ -0,0 +1,233 @@ +/* + * Copyright 2024 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 React from 'react'; +import { Grid } from '@material-ui/core'; + +import { + createApiExtension, + createApiFactory, + createNavItemExtension, + createPageExtension, + createPlugin, + createSchemaFromZod, +} from '@backstage/frontend-plugin-api'; + +import { + compatWrapper, + convertLegacyRouteRef, +} from '@backstage/core-compat-api'; +import { useApp } from '@backstage/core-plugin-api'; + +import { + createEntityCardExtension, + createEntityContentExtension, +} from '@backstage/plugin-catalog-react/alpha'; +import { + ApiEntity, + parseEntityRef, + RELATION_HAS_PART, +} from '@backstage/catalog-model'; + +import { defaultDefinitionWidgets } from './components/ApiDefinitionCard'; +import { rootRoute, registerComponentRouteRef } from './routes'; +import { apiDocsConfigRef } from './config'; + +function ApiIcon() { + const app = useApp(); + const KindApiSystemIcon = app.getSystemIcon('kind:api')!; + return ; +} + +const apiDocsNavItem = createNavItemExtension({ + title: 'APIs', + routeRef: convertLegacyRouteRef(rootRoute), + icon: () => compatWrapper(), +}); + +const apiDocsConfigApi = createApiExtension({ + factory: createApiFactory({ + api: apiDocsConfigRef, + deps: {}, + factory: () => { + const definitionWidgets = defaultDefinitionWidgets(); + return { + getApiDefinitionWidget: (apiEntity: ApiEntity) => { + return definitionWidgets.find(d => d.type === apiEntity.spec.type); + }, + }; + }, + }), +}); + +const apiDocsExplorerPage = createPageExtension({ + defaultPath: '/api-docs', + routeRef: convertLegacyRouteRef(rootRoute), + // Mapping DefaultApiExplorerPageProps to config + configSchema: createSchemaFromZod(z => + z.object({ + path: z.string().default('/api-docs'), + initiallySelectedFilter: z.enum(['owned', 'starred', 'all']).optional(), + // Ommiting columns and actions for now as their types are too complex to map to zod + }), + ), + loader: ({ config }) => + import('./components/ApiExplorerPage').then(m => + compatWrapper( + , + ), + ), +}); + +const apiDocsHasApisEntityCard = createEntityCardExtension({ + name: 'has-apis', + // Ommiting configSchema for now + // We are skipping variants and columns are too complex to map to zod + // See: https://github.com/backstage/backstage/pull/22619#discussion_r1477333252 + filter: entity => { + return ( + entity.kind === 'Component' && + entity.relations?.some( + ({ type, targetRef }) => + type.toLocaleLowerCase('en-US') === RELATION_HAS_PART && + parseEntityRef(targetRef).kind === 'API', + )!! + ); + }, + loader: () => + import('./components/ApisCards').then(m => + compatWrapper(), + ), +}); + +const apiDocsDefinitionEntityCard = createEntityCardExtension({ + name: 'definition', + filter: 'kind:api', + loader: () => + import('./components/ApiDefinitionCard').then(m => + compatWrapper(), + ), +}); + +const apiDocsConsumedApisEntityCard = createEntityCardExtension({ + name: 'consumed-apis', + // Ommiting configSchema for now + // We are skipping variants and columns are too complex to map to zod + // See: https://github.com/backstage/backstage/pull/22619#discussion_r1477333252 + filter: 'kind:component', + loader: () => + import('./components/ApisCards').then(m => + compatWrapper(), + ), +}); + +const apiDocsProvidedApisEntityCard = createEntityCardExtension({ + name: 'provided-apis', + // Ommiting configSchema for now + // We are skipping variants and columns are too complex to map to zod + // See: https://github.com/backstage/backstage/pull/22619#discussion_r1477333252 + filter: 'kind:component', + loader: () => + import('./components/ApisCards').then(m => + compatWrapper(), + ), +}); + +const apiDocsConsumingComponentsEntityCard = createEntityCardExtension({ + name: 'consuming-components', + // Ommiting configSchema for now + // We are skipping variants + // See: https://github.com/backstage/backstage/pull/22619#discussion_r1477333252 + filter: 'kind:api', + loader: () => + import('./components/ComponentsCards').then(m => + compatWrapper(), + ), +}); + +const apiDocsProvidingComponentsEntityCard = createEntityCardExtension({ + name: 'providing-components', + // Ommiting configSchema for now + // We are skipping variants + // See: https://github.com/backstage/backstage/pull/22619#discussion_r1477333252 + filter: 'kind:api', + loader: () => + import('./components/ComponentsCards').then(m => + compatWrapper(), + ), +}); + +const apiDocsDefinitionEntityContent = createEntityContentExtension({ + name: 'definition', + defaultPath: '/defintion', + defaultTitle: 'Definition', + filter: 'kind:api', + loader: async () => + import('./components/ApiDefinitionCard').then(m => + compatWrapper( + + + + + , + ), + ), +}); + +const apiDocsApisEntityContent = createEntityContentExtension({ + name: 'apis', + defaultPath: '/apis', + defaultTitle: 'APIs', + filter: 'kind:component', + loader: async () => + import('./components/ApisCards').then(m => + compatWrapper( + + + + + + + + , + ), + ), +}); + +export default createPlugin({ + id: 'api-docs', + routes: { + root: convertLegacyRouteRef(rootRoute), + }, + externalRoutes: { + registerApi: convertLegacyRouteRef(registerComponentRouteRef), + }, + extensions: [ + apiDocsNavItem, + apiDocsConfigApi, + apiDocsExplorerPage, + apiDocsHasApisEntityCard, + apiDocsDefinitionEntityCard, + apiDocsProvidedApisEntityCard, + apiDocsConsumedApisEntityCard, + apiDocsConsumingComponentsEntityCard, + apiDocsProvidingComponentsEntityCard, + apiDocsDefinitionEntityContent, + apiDocsApisEntityContent, + ], +}); diff --git a/plugins/catalog/src/alpha/EntityOverviewPage.tsx b/plugins/catalog/src/alpha/EntityOverviewPage.tsx index 932e13d962..85c318cf3f 100644 --- a/plugins/catalog/src/alpha/EntityOverviewPage.tsx +++ b/plugins/catalog/src/alpha/EntityOverviewPage.tsx @@ -17,8 +17,8 @@ import { Entity } from '@backstage/catalog-model'; import { useEntity } from '@backstage/plugin-catalog-react'; import Grid from '@material-ui/core/Grid'; -import React, { useMemo } from 'react'; -import { parseFilterExpression } from './filter/parseFilterExpression'; +import React from 'react'; +import { FilterWrapper } from './filter/FilterWrapper'; interface EntityOverviewPageProps { cards: Array<{ @@ -28,80 +28,12 @@ interface EntityOverviewPageProps { }>; } -// Keeps track of what filter expression strings that we've seen duplicates of -// with functions, or which emitted parsing errors for so far -const seenParseErrorExpressionStrings = new Set(); -const seenDuplicateExpressionStrings = new Set(); - -// Given an optional filter function and an optional filter expression, make -// sure that at most one of them was given, and return a filter function that -// does the right thing. -function buildFilterFn( - filterFunction?: (entity: Entity) => boolean, - filterExpression?: string, -): (entity: Entity) => boolean { - if ( - filterFunction && - filterExpression && - !seenDuplicateExpressionStrings.has(filterExpression) - ) { - // eslint-disable-next-line no-console - console.warn( - `Duplicate entity filter methods found, both '${filterExpression}' as well as a callback function, which is not permitted - using the callback`, - ); - seenDuplicateExpressionStrings.add(filterExpression); - } - - const filter = filterFunction || filterExpression; - if (!filter) { - return () => true; - } else if (typeof filter === 'function') { - return subject => filter(subject); - } - - const result = parseFilterExpression(filter); - if ( - result.expressionParseErrors.length && - !seenParseErrorExpressionStrings.has(filter) - ) { - // eslint-disable-next-line no-console - console.warn( - `Error(s) in entity filter expression '${filter}'`, - result.expressionParseErrors, - ); - seenParseErrorExpressionStrings.add(filter); - } - - return result.filterFn; -} - -// Handles the memoized parsing of filter expressions for each card -function CardWrapper(props: { - entity: Entity; - element: React.JSX.Element; - filterFunction?: (entity: Entity) => boolean; - filterExpression?: string; -}) { - const { entity, element, filterFunction, filterExpression } = props; - - const filterFn = useMemo( - () => buildFilterFn(filterFunction, filterExpression), - [filterFunction, filterExpression], - ); - - return filterFn(entity) ? ( - - {element} - - ) : null; -} - export function EntityOverviewPage(props: EntityOverviewPageProps) { const { entity } = useEntity(); return ( {props.cards.map((card, index) => ( - + ))} ); diff --git a/plugins/catalog/src/alpha/filter/FilterWrapper.tsx b/plugins/catalog/src/alpha/filter/FilterWrapper.tsx new file mode 100644 index 0000000000..e0e74fe481 --- /dev/null +++ b/plugins/catalog/src/alpha/filter/FilterWrapper.tsx @@ -0,0 +1,88 @@ +/* + * Copyright 2024 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 { Entity } from '@backstage/catalog-model'; +import Grid from '@material-ui/core/Grid'; +import React, { useMemo } from 'react'; +import { parseFilterExpression } from './parseFilterExpression'; + +// Keeps track of what filter expression strings that we've seen duplicates of +// with functions, or which emitted parsing errors for so far +const seenParseErrorExpressionStrings = new Set(); +const seenDuplicateExpressionStrings = new Set(); + +// Given an optional filter function and an optional filter expression, make +// sure that at most one of them was given, and return a filter function that +// does the right thing. +export function buildFilterFn( + filterFunction?: (entity: Entity) => boolean, + filterExpression?: string, +): (entity: Entity) => boolean { + if ( + filterFunction && + filterExpression && + !seenDuplicateExpressionStrings.has(filterExpression) + ) { + // eslint-disable-next-line no-console + console.warn( + `Duplicate entity filter methods found, both '${filterExpression}' as well as a callback function, which is not permitted - using the callback`, + ); + seenDuplicateExpressionStrings.add(filterExpression); + } + + const filter = filterFunction || filterExpression; + if (!filter) { + return () => true; + } else if (typeof filter === 'function') { + return subject => filter(subject); + } + + const result = parseFilterExpression(filter); + if ( + result.expressionParseErrors.length && + !seenParseErrorExpressionStrings.has(filter) + ) { + // eslint-disable-next-line no-console + console.warn( + `Error(s) in entity filter expression '${filter}'`, + result.expressionParseErrors, + ); + seenParseErrorExpressionStrings.add(filter); + } + + return result.filterFn; +} + +// Handles the memoized parsing of filter expressions +export function FilterWrapper(props: { + entity: Entity; + element: React.JSX.Element; + filterFunction?: (entity: Entity) => boolean; + filterExpression?: string; +}) { + const { entity, element, filterFunction, filterExpression } = props; + + const filterFn = useMemo( + () => buildFilterFn(filterFunction, filterExpression), + [filterFunction, filterExpression], + ); + + return filterFn(entity) ? ( + + {element} + + ) : null; +} diff --git a/plugins/catalog/src/alpha/pages.tsx b/plugins/catalog/src/alpha/pages.tsx index 3f40796da9..15f7d6cc91 100644 --- a/plugins/catalog/src/alpha/pages.tsx +++ b/plugins/catalog/src/alpha/pages.tsx @@ -31,6 +31,7 @@ import { import { catalogExtensionData } from '@backstage/plugin-catalog-react/alpha'; import { rootRouteRef } from '../routes'; import { useEntityFromUrl } from '../components/CatalogEntityPage/useEntityFromUrl'; +import { buildFilterFn } from './filter/FilterWrapper'; export const catalogPage = createPageExtension({ defaultPath: '/catalog', @@ -57,24 +58,29 @@ export const catalogEntityPage = createPageExtension({ path: coreExtensionData.routePath, routeRef: coreExtensionData.routeRef.optional(), title: catalogExtensionData.entityContentTitle, + filterFunction: catalogExtensionData.entityFilterFunction.optional(), + filterExpression: catalogExtensionData.entityFilterExpression.optional(), }), }, loader: async ({ inputs }) => { const { EntityLayout } = await import('../components/EntityLayout'); const Component = () => { + const { entity, ...rest } = useEntityFromUrl(); return ( - - - {inputs.contents.map(content => ( - - {content.output.element} - - ))} - + + {entity ? ( + + {inputs.contents + .filter(({ output: { filterFunction, filterExpression } }) => + buildFilterFn(filterFunction, filterExpression)(entity), + ) + .map(({ output: { path, title, element } }) => ( + + {element} + + ))} + + ) : null} ); }; diff --git a/yarn.lock b/yarn.lock index d102c7da55..6a91bd4fa3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4557,9 +4557,11 @@ __metadata: "@backstage/catalog-model": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/core-app-api": "workspace:^" + "@backstage/core-compat-api": "workspace:^" "@backstage/core-components": "workspace:^" "@backstage/core-plugin-api": "workspace:^" "@backstage/dev-utils": "workspace:^" + "@backstage/frontend-plugin-api": "workspace:^" "@backstage/plugin-catalog": "workspace:^" "@backstage/plugin-catalog-common": "workspace:^" "@backstage/plugin-catalog-react": "workspace:^"