Add fetchApiRef which implements fetch, plus Backstage token header when available.

This intends to be the basis for other plugins' data fetching needs, so that they can transparently interact with the catalog and other parts of the Backstage ecosystem without explicitly having to deal with authenticating themselves.

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-07-07 17:10:36 +02:00
parent 11151655a0
commit 7927005152
22 changed files with 532 additions and 10 deletions
@@ -34,6 +34,9 @@ import {
OneLoginAuth,
UnhandledErrorForwarder,
AtlassianAuth,
FetchApiBuilder,
IdentityAwareFetchMiddleware,
BackstageProtocolResolverFetchMiddleware,
} from '@backstage/core-app-api';
import {
@@ -42,6 +45,8 @@ import {
analyticsApiRef,
errorApiRef,
discoveryApiRef,
fetchApiRef,
identityApiRef,
oauthRequestApiRef,
googleAuthApiRef,
githubAuthApiRef,
@@ -92,6 +97,24 @@ export const apis = [
deps: { errorApi: errorApiRef },
factory: ({ errorApi }) => WebStorage.create({ errorApi }),
}),
createApiFactory({
api: fetchApiRef,
deps: { identityApi: identityApiRef, discoveryApi: discoveryApiRef },
factory: ({ identityApi, discoveryApi }) => {
return FetchApiBuilder.create()
.with(
new IdentityAwareFetchMiddleware(() => {
return identityApi.getCredentials().then(r => r.token);
}),
)
.with(
new BackstageProtocolResolverFetchMiddleware(plugin => {
return discoveryApi.getBaseUrl(plugin);
}),
)
.build();
},
}),
createApiFactory({
api: oauthRequestApiRef,
deps: {},