diff --git a/plugins/bazaar/src/api.ts b/plugins/bazaar/src/api.ts index b1d954fc7a..ec6dd8ec0a 100644 --- a/plugins/bazaar/src/api.ts +++ b/plugins/bazaar/src/api.ts @@ -19,7 +19,11 @@ import { EntityRef, stringifyEntityRef, } from '@backstage/catalog-model'; -import { createApiRef, IdentityApi } from '@backstage/core-plugin-api'; +import { + createApiRef, + DiscoveryApi, + IdentityApi, +} from '@backstage/core-plugin-api'; import { BazaarProject, Status } from './types'; export const bazaarApiRef = createApiRef({ @@ -29,48 +33,52 @@ export const bazaarApiRef = createApiRef({ export interface BazaarApi { updateMetadata( - baseUrl: string, entity: Entity, name: string, announcement: string, status: Status, ): Promise; - getMetadata(baseUrl: string, entity: Entity): Promise; + getMetadata(entity: Entity): Promise; getMemberCounts( bazaarProjects: BazaarProject[], - baseUrl: string, ): Promise>; - getMembers(baseUrl: string, entity: Entity): Promise; + getMembers(entity: Entity): Promise; - deleteMember(baseUrl: string, entity: Entity): Promise; + deleteMember(entity: Entity): Promise; - deleteMembers(baseUrl: string, entity: Entity): Promise; + deleteMembers(entity: Entity): Promise; - addMember(baseUrl: string, entity: Entity): Promise; + addMember(entity: Entity): Promise; - getEntities(baseUrl: string): Promise; + getEntities(): Promise; - deleteEntity(baseUrl: string, entity: Entity): Promise; + deleteEntity(entity: Entity): Promise; } export class BazaarClient implements BazaarApi { private readonly identityApi: IdentityApi; + private readonly discoveryApi: DiscoveryApi; - constructor(options: { identityApi: IdentityApi }) { + constructor(options: { + identityApi: IdentityApi; + discoveryApi: DiscoveryApi; + }) { this.identityApi = options.identityApi; + this.discoveryApi = options.discoveryApi; } async updateMetadata( - baseUrl: string, entity: Entity, name: string, announcement: string, status: Status, ): Promise { - return await fetch(`${baseUrl}/api/bazaar/metadata`, { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + return await fetch(`${baseUrl}/metadata`, { method: 'PUT', headers: { entity_ref: stringifyEntityRef(entity), @@ -85,8 +93,10 @@ export class BazaarClient implements BazaarApi { }).then(resp => resp.json()); } - async getMetadata(baseUrl: string, entity: Entity): Promise { - return await fetch(`${baseUrl}/api/bazaar/metadata`, { + async getMetadata(entity: Entity): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + return await fetch(`${baseUrl}/metadata`, { method: 'GET', headers: { entity_ref: stringifyEntityRef(entity), @@ -96,11 +106,12 @@ export class BazaarClient implements BazaarApi { async getMemberCounts( bazaarProjects: BazaarProject[], - baseUrl: string, ): Promise> { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const members = new Map(); for (const project of bazaarProjects) { - const response = await fetch(`${baseUrl}/api/bazaar/members`, { + const response = await fetch(`${baseUrl}/members`, { method: 'GET', headers: { entity_ref: project.entityRef as string, @@ -114,8 +125,10 @@ export class BazaarClient implements BazaarApi { return members; } - async getMembers(baseUrl: string, entity: Entity): Promise { - return await fetch(`${baseUrl}/api/bazaar/members`, { + async getMembers(entity: Entity): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + return await fetch(`${baseUrl}/members`, { method: 'GET', headers: { entity_ref: stringifyEntityRef(entity), @@ -123,8 +136,10 @@ export class BazaarClient implements BazaarApi { }).then(resp => resp.json()); } - async addMember(baseUrl: string, entity: Entity): Promise { - await fetch(`${baseUrl}/api/bazaar/member`, { + async addMember(entity: Entity): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + await fetch(`${baseUrl}/member`, { method: 'PUT', headers: { user_id: this.identityApi.getUserId(), @@ -133,8 +148,10 @@ export class BazaarClient implements BazaarApi { }); } - async deleteMember(baseUrl: string, entity: Entity): Promise { - await fetch(`${baseUrl}/api/bazaar/member`, { + async deleteMember(entity: Entity): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + await fetch(`${baseUrl}/member`, { method: 'DELETE', headers: { user_id: this.identityApi.getUserId(), @@ -143,8 +160,10 @@ export class BazaarClient implements BazaarApi { }); } - async deleteMembers(baseUrl: string, entity: Entity): Promise { - await fetch(`${baseUrl}/api/bazaar/members`, { + async deleteMembers(entity: Entity): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + await fetch(`${baseUrl}/members`, { method: 'DELETE', headers: { entity_ref: stringifyEntityRef(entity), @@ -152,21 +171,25 @@ export class BazaarClient implements BazaarApi { }); } - async getEntities(baseUrl: string): Promise { - return await fetch(`${baseUrl}/api/bazaar/entities`, { + async getEntities(): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + return await fetch(`${baseUrl}/entities`, { method: 'GET', }).then(resp => resp.json()); } - async deleteEntity(baseUrl: string, entity: Entity): Promise { - await fetch(`${baseUrl}/api/bazaar/metadata`, { + async deleteEntity(entity: Entity): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + + await fetch(`${baseUrl}/metadata`, { method: 'DELETE', headers: { entity_ref: stringifyEntityRef(entity), }, }); - await fetch(`${baseUrl}/api/bazaar/members`, { + await fetch(`${baseUrl}/members`, { method: 'DELETE', headers: { user_id: this.identityApi.getUserId(), diff --git a/plugins/bazaar/src/components/AddProjectDialog/AddProjectDialog.tsx b/plugins/bazaar/src/components/AddProjectDialog/AddProjectDialog.tsx index a90a56b9c7..2b0b6bb527 100644 --- a/plugins/bazaar/src/components/AddProjectDialog/AddProjectDialog.tsx +++ b/plugins/bazaar/src/components/AddProjectDialog/AddProjectDialog.tsx @@ -17,7 +17,7 @@ import React, { useState, useEffect, Dispatch, SetStateAction } from 'react'; import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; import { SubmitHandler } from 'react-hook-form'; -import { useApi, configApiRef } from '@backstage/core-plugin-api'; +import { useApi } from '@backstage/core-plugin-api'; import { ProjectDialog } from '../ProjectDialog'; import { ProjectSelector } from '../ProjectSelector'; import { BazaarProject, FormValues, Status } from '../../types'; @@ -39,9 +39,6 @@ export const AddProjectDialog = ({ setCatalogEntities, }: Props) => { const bazaarApi = useApi(bazaarApiRef); - const baseUrl = useApi(configApiRef) - .getConfig('backend') - .getString('baseUrl'); const [selectedEntity, setSelectedEntity] = useState( catalogEntities ? catalogEntities[0] : null, ); @@ -83,7 +80,6 @@ export const AddProjectDialog = ({ }); await bazaarApi.updateMetadata( - baseUrl, selectedEntity!, selectedEntity!.metadata.name, formValues.announcement, diff --git a/plugins/bazaar/src/components/DeleteProjectDialog/DeleteProjectDialog.tsx b/plugins/bazaar/src/components/DeleteProjectDialog/DeleteProjectDialog.tsx index 7b00cc5cd1..a1ac9c060b 100644 --- a/plugins/bazaar/src/components/DeleteProjectDialog/DeleteProjectDialog.tsx +++ b/plugins/bazaar/src/components/DeleteProjectDialog/DeleteProjectDialog.tsx @@ -30,7 +30,7 @@ import IconButton from '@material-ui/core/IconButton'; import CloseIcon from '@material-ui/icons/Close'; import Typography from '@material-ui/core/Typography'; import { Entity } from '@backstage/catalog-model'; -import { useApi, configApiRef } from '@backstage/core-plugin-api'; +import { useApi } from '@backstage/core-plugin-api'; import { bazaarApiRef } from '../../api'; const styles = (theme: Theme) => @@ -101,10 +101,6 @@ export const DeleteProjectDialog = ({ handleClose, setIsBazaar, }: Props) => { - const baseUrl = useApi(configApiRef) - .getConfig('backend') - .getString('baseUrl'); - const handleCloseAndClear = () => { handleClose(); }; @@ -112,7 +108,7 @@ export const DeleteProjectDialog = ({ const bazaarApi = useApi(bazaarApiRef); const handleSubmit = async () => { - await bazaarApi.deleteEntity(baseUrl, entity); + await bazaarApi.deleteEntity(entity); setIsBazaar(false); handleCloseAndClear(); }; diff --git a/plugins/bazaar/src/components/EditProjectDialog/EditProjectDialog.tsx b/plugins/bazaar/src/components/EditProjectDialog/EditProjectDialog.tsx index 4f605a55ae..657d9e965e 100644 --- a/plugins/bazaar/src/components/EditProjectDialog/EditProjectDialog.tsx +++ b/plugins/bazaar/src/components/EditProjectDialog/EditProjectDialog.tsx @@ -16,7 +16,7 @@ import React, { useState, useEffect, Dispatch, SetStateAction } from 'react'; import { Entity } from '@backstage/catalog-model'; -import { useApi, configApiRef } from '@backstage/core-plugin-api'; +import { useApi } from '@backstage/core-plugin-api'; import { ProjectDialog } from '../ProjectDialog'; import { BazaarProject, FormValues } from '../../types'; import { bazaarApiRef } from '../../api'; @@ -37,10 +37,6 @@ export const EditProjectDialog = ({ open, handleClose, }: Props) => { - const baseUrl = useApi(configApiRef) - .getConfig('backend') - .getString('baseUrl'); - const [defaultValues, setDefaultValues] = useState({ announcement: bazaarProject.announcement, status: bazaarProject.status, @@ -59,7 +55,6 @@ export const EditProjectDialog = ({ const formValues = getValues(); const updateResponse = await bazaarApi.updateMetadata( - baseUrl, entity!, entity.metadata.name, formValues.announcement, diff --git a/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx b/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx index 9caeb6ce0a..bf72a7eea5 100644 --- a/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx +++ b/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx @@ -47,11 +47,7 @@ import DeleteIcon from '@material-ui/icons/Delete'; import { EditProjectDialog } from '../EditProjectDialog'; import { DeleteProjectDialog } from '../DeleteProjectDialog'; import ExitToAppIcon from '@material-ui/icons/ExitToApp'; -import { - useApi, - identityApiRef, - configApiRef, -} from '@backstage/core-plugin-api'; +import { useApi, identityApiRef } from '@backstage/core-plugin-api'; import { useAsync } from 'react-use'; import { Member, BazaarProject } from '../../types'; import { bazaarApiRef } from '../../api'; @@ -101,12 +97,8 @@ export const EntityBazaarInfoCard = () => { }); const [isBazaar, setIsBazaar] = useState(false); - const baseUrl = useApi(configApiRef) - .getConfig('backend') - .getString('baseUrl'); - const getInitMemberStatus = async () => { - const response = await bazaarApi.getMembers(baseUrl, entity); + const response = await bazaarApi.getMembers(entity); const dbMembers = response.data.map((obj: any) => { const member: Member = { userId: obj.user_id, @@ -128,7 +120,7 @@ export const EntityBazaarInfoCard = () => { }; const getMetadata = async () => { - const response = await bazaarApi.getMetadata(baseUrl, entity); + const response = await bazaarApi.getMetadata(entity); if (response.status !== 404) { setIsBazaar(true); @@ -181,14 +173,14 @@ export const EntityBazaarInfoCard = () => { newMembers.sort(sortMembers); return newMembers; }); - await bazaarApi.addMember(baseUrl, entity); + await bazaarApi.addMember(entity); } else { setMembers( members.filter( (member: Member) => member.userId !== identity.getUserId(), ), ); - await bazaarApi.deleteMember(baseUrl, entity); + await bazaarApi.deleteMember(entity); } }; diff --git a/plugins/bazaar/src/components/SortView/SortView.tsx b/plugins/bazaar/src/components/SortView/SortView.tsx index 268b6c1e6f..15653b2eee 100644 --- a/plugins/bazaar/src/components/SortView/SortView.tsx +++ b/plugins/bazaar/src/components/SortView/SortView.tsx @@ -27,7 +27,7 @@ import { ProjectPreview } from '../ProjectPreview/ProjectPreview'; import { Button, makeStyles, Link } from '@material-ui/core'; import { useAsync } from 'react-use'; import { Entity, EntityRef } from '@backstage/catalog-model'; -import { useApi, configApiRef } from '@backstage/core-plugin-api'; +import { useApi } from '@backstage/core-plugin-api'; import { catalogApiRef, CATALOG_FILTER_EXISTS, @@ -52,9 +52,6 @@ export const SortView = () => { const [bazaarProjects, setBazaarProjects] = useState([]); const bazaarApi = useApi(bazaarApiRef); const catalogApi = useApi(catalogApiRef); - const baseUrl = useApi(configApiRef) - .getConfig('backend') - .getString('baseUrl'); const compareProjectsByDate = ( a: BazaarProject, @@ -78,7 +75,7 @@ export const SortView = () => { fields: ['apiVersion', 'kind', 'metadata', 'spec'], }); - const response = await bazaarApi.getEntities(baseUrl); + const response = await bazaarApi.getEntities(); const dbProjects: BazaarProject[] = []; const bazaarProjectRefs: string[] = []; @@ -94,7 +91,7 @@ export const SortView = () => { bazaarProjectRefs.push(project.entity_ref); }); - setBazaarMembers(await bazaarApi.getMemberCounts(dbProjects, baseUrl)); + setBazaarMembers(await bazaarApi.getMemberCounts(dbProjects)); setBazaarProjects(dbProjects); setCatalogEntities( entities.items.filter((entity: Entity) => { diff --git a/plugins/bazaar/src/plugin.ts b/plugins/bazaar/src/plugin.ts index 5fd884bd35..f96eaf522d 100644 --- a/plugins/bazaar/src/plugin.ts +++ b/plugins/bazaar/src/plugin.ts @@ -20,6 +20,7 @@ import { createPlugin, createRoutableExtension, identityApiRef, + discoveryApiRef, } from '@backstage/core-plugin-api'; import { bazaarApiRef, BazaarClient } from './api'; @@ -33,8 +34,10 @@ export const bazaarPlugin = createPlugin({ api: bazaarApiRef, deps: { identityApi: identityApiRef, + discoveryApi: discoveryApiRef, }, - factory: ({ identityApi }) => new BazaarClient({ identityApi }), + factory: ({ identityApi, discoveryApi }) => + new BazaarClient({ identityApi, discoveryApi }), }), ], });