Files
backstage/docs/api/utility-apis.md
T
Esteban Barrios 8f53351a32 Rebase from master (#19)
* Fix client reference

* Fix CatalogClient reference in create-app too

* Move catalog-client dep to create-app

* Add catalog-client dep again..

* Use RequestOptions

* tsc

* Backwards compatible scaffolder

* Fix test

* Avoid importing core

* Use request options

* Increase code coverage

* Type those variables

* use the second mock

* Trying to make tests work on Github too

* Get clean yarn from master

* Only add authorization header if token exists

* Use request options

* Forward authorization header token

* Refactor to use context argument

* Allow the registration of optional locations via the catalog-client

* Make EntityRefLink a React.forwardRef in order to use it as root component in other components like ListItem

* Rework the user flow of the catalog-import plugin

* Update the create-app template

* Add luxon and remove moment

* Port moment to luxon

* Add change set

* Split the step label and content to be able to refactor the stepper from vertical to horizontal in the future

* Fix tsc, prettier and import issues

* Update .changeset/strange-olives-unite.md

Co-authored-by: Himanshu Mishra <himanshu@orkohunter.net>

* Update plugins/auth-backend/src/identity/TokenFactory.ts

Co-authored-by: Himanshu Mishra <himanshu@orkohunter.net>

* Change from minor to patch

* Update DatabaseKeyStore.ts

* feat: added a test for running a simple cypress server

* Add luxon and remove moment

* Port from moment to Luxon

* Add changeset

* Fixes following CR

* Rebased and reinstalled yarn.lock

* Changed to fromSQL

* Fix prettier formatting

* chore: reworking the lock file for the cypress deps

* chore: adding cypress.config

* chore: reworking some more examples

* chore: added some more simple stuff for e2e tests with cypress

* chore: reworking again

* chore: making cypress nice

* chore: added a sample test that works

* chore: reworking tests to use he cypress github-action instead

* chore: reconfigure Cypress run

* chore: don't install - we have deps

* chore: dump video recordings too

* chore: slimming down install script a little

* chore: make pretty

* chore: fixing syntax

* chore: fixing syntax again

* Align card elements with list

* Align card elements with List

* Add changeset

* Removed unused import

* chore: archive video footage with the correct path

* chore: fix lint warnings

* chore: fix prettier again

* chore: move files around for ease

* Added pattern for component name

* Add changeset

* stash: push to stash

* Return AboutCard title to group name

* Refactor children to use shared function

* TechDocs: Add visbility to migrate away from basic setup

* bugfix: [3310] Favoriting a component resets interface to your owned components

update changeset

remove unused import

fix changeset message

fix vale errors?

* Create utc DateTime object directly

* integration: update the gitlab config mandatory fields to match reality

* Resolve review comments

* Remove shadowed variables

* generateEntityDefinitions should return a PartialEntity

* Update roadmap with Backstage Community Sessions

* Make sure that SidebarItems are also active when on sub route

* Add whitespace around variable

* Add changeset

* Add word to dictionary

* Smater duration display

* Add changeset

* chore(deps-dev): bump @graphql-codegen/typescript from 1.18.1 to 1.20.2

Bumps [@graphql-codegen/typescript](https://github.com/dotansimha/graphql-code-generator/tree/HEAD/packages/plugins/typescript/typescript) from 1.18.1 to 1.20.2.
- [Release notes](https://github.com/dotansimha/graphql-code-generator/releases)
- [Changelog](https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/typescript/typescript/CHANGELOG.md)
- [Commits](https://github.com/dotansimha/graphql-code-generator/commits/@graphql-codegen/typescript@1.20.2/packages/plugins/typescript/typescript)

Signed-off-by: dependabot[bot] <support@github.com>

* chore: reworking folder structure to move cypress outside of the project

* chore: updating workflow to point at the right folder now

* chore: fixing cypress build

* microsite: Fix color contrast for pre tags

* Add package name to lockfile.ts error

* Create new-mangos-tap.md

* Changed changeset bump to be patch

* Pass registered Logger in ServiceBuilderImpl to requestLoggingHandler

`requestLoggingHandler` takes an optional `logger` parameter that it can use
to log incoming requests. The `ServiceBuilderImpl` was not passing  on this logger, if
set, to `requestLoggingHandler` middleware

* Add indices on columns referring location(id)

* Add changeset

* Catch catalog errors

* Add changeset

* Fixed parseUrl to output catalogPaths beginning with '/'

* Add configurable OAuth 2.0 scopes

- Add oauth2 config for optional scopes
- Document oauth2 config keys
- Add OAuth2 to demo app list of identity providers

* incorrectly added callbackUrl

* chore: added a simple readme to run some simple tests against a backstage instance

* chore(deps): bump @svgr/plugin-jsx from 5.4.0 to 5.5.0

Bumps [@svgr/plugin-jsx](https://github.com/gregberge/svgr) from 5.4.0 to 5.5.0.
- [Release notes](https://github.com/gregberge/svgr/releases)
- [Changelog](https://github.com/gregberge/svgr/blob/main/CHANGELOG.md)
- [Commits](https://github.com/gregberge/svgr/compare/v5.4.0...v5.5.0)

Signed-off-by: dependabot[bot] <support@github.com>

* chore(deps): bump @types/cors from 2.8.6 to 2.8.9

Bumps [@types/cors](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/cors) from 2.8.6 to 2.8.9.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/cors)

Signed-off-by: dependabot[bot] <support@github.com>

* fix: removing test e2e-test tsconfig.json file

* TechDocs: Preparers will take and return an etag for cache invalidation

* TechDocs: Implement etag based caching for url preparer

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>

* TechDocs: Implement etag based caching for git preparer

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>

* TechDocs: Warn when using legacy git preparer and dir preparer in backstage.io/techdocs-ref

Context: https://github.com/backstage/backstage/issues/4409

* TechDocs: Update tests for proper url preparer caching

* TechDocs: Deduplicate git clone for cache check

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>

* TechDocs: Add changesets for deprecation and proper caching

* TechDocs: Implement caching for dir preparer

* Update the changesets

* scaffolder-backend: remove auth-backend dependency

* catalog-client: rename ApiContext to CatalogRequestOptions + avoid export

* refactor existing usage of ApiContext

* techdocs: don't swallow errors other than NotModifiedError

* catalog-import: removed bonus code

* techdocs: meaningful logs when readTree starts

* Flatten the options of the CatalogImportPage

* inline optional id token auth headers

* Define relationship to software catalog and loose coupling by convention.

* Clarify intentions around bulk vs. incremental index management.

* Break apart backend plugins & clarify indexer/plugin relationship.

* update changeset bump levels

* [Search] documentation update (#4459)

* delete link to issue as it is closed

* replace usage of easy as its very subjective

* Update docs/features/search/architecture.md

Co-authored-by: Adam Harvey <adam.harvey@dxc.com>

* prettier....:

Co-authored-by: Adam Harvey <adam.harvey@dxc.com>

* Own it.

* scaffolder: include backstage identity token in requests

* review feedback tweaks

* docs/apis: update to use named plugin var

* sentry: update plugin instance export name

* clear other field when toggling reason

* changeset

* fix(pagerduty): use the luxon date library

* Don't pass default as a scope to OIDC providers

* docs(TechDocs): Add GitHub Actions CI example with AWS S3

* tech-radar: migrated to new composability API

* cost-insights: migrate to new composability API

* Changeset

* Fix line endings

* Prettier

* Remove unnecessary scopes from OIDC defaultApi

* Bump plugin-auth-backend to a minor change,
add documentation for fixing it

* Fix md syntax error

* Fix Vale spelling error

* Remove defaultScopes from OIDC api

* [ImgBot] Optimize images

/plugins/catalog-import/docs/catalog-import-screenshot.png -- 613.39kb -> 356.48kb (41.88%)

Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com>

* github/workflows: use lax config checks

* Use commented-out example value for scope

* feat(pagerduty): add changeset

* Apply suggestions from code review

Co-authored-by: Rémi Doreau <32459935+ayshiff@users.noreply.github.com>

* feat(catalog): add entity links card component

* update entity links changeset

* Fix prettier

* chore(deps-dev): bump @types/http-proxy from 1.17.4 to 1.17.5

Bumps [@types/http-proxy](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/http-proxy) from 1.17.4 to 1.17.5.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/http-proxy)

Signed-off-by: dependabot[bot] <support@github.com>

* Revert "Merge pull request #3953 from ayshiff/feature/techdocs-aws-sdkv3"

This reverts commit 1c7771871e, reversing
changes made to 65d5c8e6fe.

* Revert "Merge pull request #4035 from ayshiff/feature/catalog-backend-aws-sdk-v3"

This reverts commit 1df9134648, reversing
changes made to d45a510069.

* Add changeset

* feat: support custom app icons

* backend-common: implement UrlReader.search for the other providers too

* Remove changeset

* docs(TechDocs): Improvements to the CI example

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>

* fix(catalog): entity links incorrect wrapping

* Remove unused functions and luxon

* Removed old imports

* Fix broken links in documentation (#4418)

* Update IdentityApi.md

* Update docs/reference/utility-apis/IdentityApi.md

* Update docs/reference/utility-apis/IdentityApi.md

* ran yarn docgen

* Use JS Date instead of Datetime

* Add line

* Prettier fix

* Move parseDate function

* Version Packages

* chore: fix the create-app version

* Limit the props that are forwarded to the Link component in the EntityRefLink

* Use routed tabs to link to every settings page

* fix up yarn after release

* chore: add lockfile to words

* catalog-info: add links

* Export Select component from core

I saw the Select component on storybook and went
to use it but it seems it's not exported. Any chance
it could be exported?

* bug: filepath can be returned as undefined from `git-url-parse` let's default to empty

* chore: might as well do this for all parsing

* chore: changeset

* Add changeset for fixing requestLoggingHandler

* chore(deps-dev): bump @storybook/addon-actions from 6.1.11 to 6.1.17

Bumps [@storybook/addon-actions](https://github.com/storybookjs/storybook/tree/HEAD/addons/actions) from 6.1.11 to 6.1.17.
- [Release notes](https://github.com/storybookjs/storybook/releases)
- [Changelog](https://github.com/storybookjs/storybook/blob/next/CHANGELOG.md)
- [Commits](https://github.com/storybookjs/storybook/commits/v6.1.17/addons/actions)

Signed-off-by: dependabot[bot] <support@github.com>

* Port AboutCard

* Fix tests

* Revert EntityPage changes

* docs: Merge auth glossary with main glossary

* Remove the "Move repository" menu entry from the catalog page, as it's just a placeholder

It will be easy to bring it back later, but for now it just confuses users that it's not doing anything. It's also hard to remove for integrators.

* make the template cards conform to mui standard

Co-authored-by: Erik Larsson <erik.larsson@schibsted.com>
Co-authored-by: Dominik Henneke <dominik.henneke@sda-se.com>
Co-authored-by: Nils Streijffert <nstreijffert@spotify.com>
Co-authored-by: Nils Streijffert <nils.streijffert@gmail.com>
Co-authored-by: Himanshu Mishra <himanshu@orkohunter.net>
Co-authored-by: blam <ben@blam.sh>
Co-authored-by: Nir Gazit <nir.gzt@gmail.com>
Co-authored-by: Adam Harvey <adam.harvey@dxc.com>
Co-authored-by: NHI TRAN <nhid@ntran.io>
Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Oliver Sand <oliver.sand@sda-se.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Debajyoti Halder <rondebajyoti@gmail.com>
Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Co-authored-by: Gowind <petrovgovind@gmail.com>
Co-authored-by: Alan Crosswell <alan@columbia.edu>
Co-authored-by: Eric Peterson <ericpeterson@spotify.com>
Co-authored-by: Emma Indal <emma.indahl@gmail.com>
Co-authored-by: Ryan Vazquez <ryanv@spotify.com>
Co-authored-by: Ryan Vazquez <ryanmvazquez@gmail.com>
Co-authored-by: Remi <remi.d45@gmail.com>
Co-authored-by: Ryan Manny <rmanny@apptio.com>
Co-authored-by: ImgBotApp <ImgBotHelp@gmail.com>
Co-authored-by: Rémi Doreau <32459935+ayshiff@users.noreply.github.com>
Co-authored-by: Andrew Thauer <6507159+andrewthauer@users.noreply.github.com>
Co-authored-by: Joel Low <joel@joelsplace.sg>
Co-authored-by: Eric Peterson <iamEAP@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Iain Billett <iain@roadie.io>
2021-02-12 16:08:46 +01:00

11 KiB

id, title, description
id title description
utility-apis Utility APIs Backstage Utility APIs

Introduction

Backstage Plugins strive to be self-contained, with as much functionality as possible residing within the plugin itself and its backend APIs. There will however always be a need for plugins to communicate outside of its boundaries, both with other plugins and the app itself.

Backstage provides two primary methods for plugins to communicate across their boundaries in client-side code. The first one being the createPlugin API and the registration hooks passed to the register method, and the second one being Utility APIs. While the createPlugin API is focused on the initialization plugins and the app, the Utility APIs provide ways for plugins to communicate during their entire life cycle.

Consuming APIs

Each Utility API is tied to an ApiRef instance, which is a global singleton object without any additional state or functionality, its only purpose is to reference Utility APIs. ApiRefs are created using createApiRef, which is exported by @backstage/core. There are many predefined Utility APIs defined in @backstage/core, and they're all exported with a name of the pattern *ApiRef, for example errorApiRef.

To access one of the Utility APIs inside a React component, use the useApi hook exported by @backstage/core, or the withApis HOC if you prefer class components. For example, the ErrorApi can be accessed like this:

import React from 'react';
import { useApi, errorApiRef } from '@backstage/core';

export const MyComponent = () => {
  const errorApi = useApi(errorApiRef);

  // Signal to the app that something went wrong, and display the error to the user.
  const handleError = error => {
    errorApi.post(error);
  };

  // the rest of the component ...
};

Note that there is no explicit type given for ErrorApi. This is because the errorApiRef has the type embedded, and useApi is able to infer the type.

Also note that consuming Utility APIs is not limited to plugins, it can be done from any component inside Backstage, including the ones in @backstage/core. The only requirement is that they are beneath the AppProvider in the react tree.

Supplying APIs

API Factories

APIs are registered in the form of ApiFactories, which encapsulate the process of instantiating an API. It is a collection of three things: the ApiRef of the API to instantiate, a list of all required dependencies, and a factory function that returns a new API instance.

For example, this is the default ApiFactory for the ErrorApi:

createApiFactory({
  api: errorApiRef,
  deps: { alertApi: alertApiRef },
  factory: ({ alertApi }) =>
    new ErrorAlerter(alertApi, new ErrorApiForwarder()),
});

In this example the errorApiRef is our API, which encapsulates the ErrorApi type. The alertApiRef is our single dependency, which we give the name alertApi, and is then passed on to the factory function, which returns an implementation of the ErrorApi.

The createApiFactory function is a thin wrapper that enables TypeScript type inference. You may notice that there are no type annotations in the above example, and that is because we're able to infer all types from the ApiRefs. TypeScript will make sure that the return value of the factory function matches the type embedded in api's ApiRef, in this case the ErrorApi. It will also match the types between the deps and the parameters of the factory function, again using the type embedded within the ApiRefs.

Registering API Factories

The responsibility for adding Utility APIs to a Backstage app lies in three different locations: the Backstage core library, each plugin included in the app, and the app itself.

Core APIs

Starting with the Backstage core library, it provides implementations for all of the core APIs. The core APIs are the ones exported by @backstage/core, such as the errorApiRef and configApiRef. You can find a full list of them here.

The core APIs are loaded for any app created with createApp from @backstage/core, which means that there is no step that needs to be taken to include these APIs in an app.

Plugin APIs

In addition to the core APIs, plugins can define and export their own APIs. While doing so they should usually also provide default implementations of their own APIs, for example, the catalog plugin exports catalogApiRef, and also supplies a default ApiFactory of that API using the CatalogClient. There is one restriction to plugin-provided API Factories: plugins may not supply factories for core APIs, trying to do so will cause the app to refuse to start.

Plugins supply their APIs through the apis option of createPlugin, for example:

export const techdocsPlugin = createPlugin({
  id: 'techdocs',
  apis: [
    createApiFactory({
      api: techdocsStorageApiRef,
      deps: { configApi: configApiRef },
      factory({ configApi }) {
        return new TechDocsStorageApi({
          apiOrigin: configApi.getString('techdocs.storageUrl'),
        });
      },
    }),
  ],
});

App APIs

Lastly, the app itself is the final point where APIs can be added, and what has the final say in what APIs will be loaded at runtime. The app may override the factories for any of the core or plugin APIs, with the exception of the config, app theme, and identity APIs. These are static APIs that are tied into the createApp implementation, and therefore not possible to override.

Overriding APIs is useful for apps that want to switch out behavior to tailor it to their environment. In some cases plugins may also export multiple implementations of the same API, where they each have their own different requirements on for example backend storage and surrounding environment.

Supplying APIs to the app works just like for plugins:

const app = createApp({
  apis: [
    /* ApiFactories */
  ],
  // ... other options
});

A common pattern is to export a list of all APIs from apis.ts, next to App.tsx. See the example app in this repo for an example.

Custom implementations of Utility APIs

Defining a custom implementation of a utility API is easy, you simply need to export a class that implements the target API, for example:

export class IgnoringErrorApi implements ErrorApi {
  post(error: Error, context?: ErrorContext) {
    // ignore error
  }
}

The IgnoringErrorApi would then be imported in the app, and wired up like this:

builder.add(errorApiRef, new IgnoringErrorApi());

Note that the above line will cause an error if IgnoreErrorApi does not fully implement the ErrorApi, as it is checked by the type embedded in the errorApiRef at compile time.

Defining custom Utility APIs

Plugins are free to define their own Utility APIs. Simply define the TypeScript interface for the API, and create an ApiRef using createApiRef exported from @backstage/core. Also be sure to provide at least one implementation of the API, and to declare a default factory for the API in createPlugin.

Custom Utility APIs can be either public or private, which is up to the plugin to choose. Private APIs do not expose an external API surface, and it's therefore possible to make breaking changes to the API without affecting other users of the plugin. If an API is made public however, it opens up for other plugins to make use of the API, and it also makes it possible for users for your plugin to override the API in the app. It is however important to maintain backwards compatibility of public APIs, as you may otherwise break apps that are using your plugin.

To make an API public, simply export the ApiRef of the API, and any associated types. To make an API private, just avoid exporting the ApiRef, but still be sure to supply a default factory to createPlugin.

Private APIs are useful for plugins that want to depend on other APIs outside of React components, but not have to expose an entire API surface to maintain. When using private APIs, it is fine to use the typeof of an implementing class as the type parameter passed to createApiRef, while public APIs should always define a separate TypeScript interface type.

Plugins may depend on APIs from other plugins, both in React components and as dependencies to API factories. Do however be sure to not cause circular dependencies between plugins.

Architecture

The ApiRef instances mentioned above provide a point of indirection between consumers and producers of Utility APIs. It allows for plugins and components to depend on APIs in a type-safe way, without having a direct reference to a concrete implementation of the APIs. The Apps are also given a lot of flexibility in what implementations to provide. As long as they adhere to the contract established by an ApiRef, they are free to choose any implementation they want.

The figure below shows the relationship between different Apps, that provide different implementations of the FooApi. Components within Plugins then access the FooApi via the fooApiRef.

Figure showing the relationship between utility APIs, the apps that provide them, and the plugins that consume them

The current method for connecting Utility API providers and consumers is via the React tree using an ApiProvider, which is added to the AppProvider of the App. In the future there may potentially be more ways to do this, in ways that are not tied to React. A design goal of the Utility APIs was to not have them directly tied to React.

The indirection provided by Utility APIs also makes it straightforward to test components that depend on APIs, and to provide a standard common development environment for plugins. A proper test wrapper with mocked API implementations is not yet ready, but it will be provided as a part of @backstage/test-utils. It will provide mocked variants of APIs, with additional methods for asserting a component's interaction with the API.

The common development environment for plugins is included in @backstage/dev-utils, where the exported createDevApp function creates an application with implementations for all core APIs already present. Contrary to the method for wiring up Utility API implementations in an app created with createApp, createDevApp uses automatic dependency injection. This is to make it possible to replace any API implementation, and having that be reflected in dependents of that API.