Improved types slightly

This commit is contained in:
Sebastian Qvarfordt
2020-06-02 15:36:31 +02:00
parent 8e6df5f575
commit 254d9089be
3 changed files with 16 additions and 17 deletions
+6 -9
View File
@@ -16,7 +16,7 @@
import { CatalogApi } from './types';
import { DescriptorEnvelope } from '../types';
import { Entity } from '@backstage/catalog-model';
import { Entity, Location } from '@backstage/catalog-model';
export class CatalogClient implements CatalogApi {
private apiOrigin: string;
@@ -43,22 +43,19 @@ export class CatalogClient implements CatalogApi {
if (entity) return entity;
throw new Error(`'Entity not found: ${name}`);
}
async getLocationByEntity(entity: Entity): Promise<any> {
const findLocationIdInEntity = (e: Entity) => {
return e.metadata.annotations
? e.metadata.annotations['backstage.io/managed-by-location']
: null;
};
async getLocationByEntity(entity: Entity): Promise<Location | undefined> {
const findLocationIdInEntity = (e: Entity) =>
e.metadata.annotations?.['backstage.io/managed-by-location'];
const locationId = findLocationIdInEntity(entity);
if (!locationId) return null;
if (!locationId) return undefined;
const response = await fetch(
`${this.apiOrigin}${this.basePath}/locations/${locationId}`,
);
if (response.ok) {
const location = await response.json();
if (location) return location;
if (location) return location.data;
}
throw new Error(`'Location not found: ${locationId}`);
}
+2 -2
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { createApiRef } from '@backstage/core';
import { Entity } from '@backstage/catalog-model';
import { Entity, Location } from '@backstage/catalog-model';
export const catalogApiRef = createApiRef<CatalogApi>({
id: 'plugin.catalog.service',
@@ -25,5 +25,5 @@ export const catalogApiRef = createApiRef<CatalogApi>({
export interface CatalogApi {
getEntities(): Promise<Entity[]>;
getEntityByName(name: string): Promise<Entity>;
getLocationByEntity(entity: Entity): Promise<any>;
getLocationByEntity(entity: Entity): Promise<Location | undefined>;
}
@@ -35,7 +35,7 @@ import {
import { Button, makeStyles, Typography, Link } from '@material-ui/core';
import { filterGroups, defaultFilter } from '../../data/filters';
import GitHub from '@material-ui/icons/GitHub';
import { Entity } from '@backstage/catalog-model';
import { Entity, Location } from '@backstage/catalog-model';
const useStyles = makeStyles(theme => ({
contentWrapper: {
@@ -48,6 +48,7 @@ const useStyles = makeStyles(theme => ({
import { catalogApiRef } from '../..';
import { envelopeToComponent } from '../../data/utils';
import { Component } from '../../data/component';
const CatalogPage: FC<{}> = () => {
const catalogApi = useApi(catalogApiRef);
@@ -71,14 +72,12 @@ const CatalogPage: FC<{}> = () => {
};
if (value) {
getLocationDataForEntities(value)
.then(location => location.map(l => l.data))
.then(setLocations);
getLocationDataForEntities(value).then(setLocations);
}
}, [value, catalogApi]);
const actions = [
(rowData: any) => ({
(rowData: Component) => ({
icon: GitHub,
tooltop: 'View on GitHub',
onClick: () => {
@@ -92,7 +91,10 @@ const CatalogPage: FC<{}> = () => {
}),
];
const findLocationForEntity = (entity: Entity, l: any) => {
const findLocationForEntity = (
entity: Entity,
l: Location[],
): Location | undefined => {
const entityLocationId =
entity.metadata.annotations?.['backstage.io/managed-by-location'];
return l.find((location: any) => location.id === entityLocationId);