Skip to content

Commit 0b48782

Browse files
feat(marketplace): use catalogApi to fetch marketplace plugins (#170)
* marketplace: add catalog and backend api clients * use catalogApi client to fetch data
1 parent d33a96e commit 0b48782

File tree

8 files changed

+64
-11
lines changed

8 files changed

+64
-11
lines changed

workspaces/marketplace/plugins/marketplace/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"@backstage/core-components": "^0.15.1",
3939
"@backstage/core-plugin-api": "^1.10.0",
40+
"@backstage/plugin-catalog-react": "^1.14.0",
4041
"@backstage/theme": "^0.6.0",
4142
"@mui/icons-material": "^5.16.7",
4243
"@mui/material": "^5.12.2",

workspaces/marketplace/plugins/marketplace/src/api/MarketplaceClient.tsx renamed to workspaces/marketplace/plugins/marketplace/src/api/MarketplaceBackendClient.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type Options = {
2727
fetchApi: FetchApi;
2828
};
2929

30-
export class MarketplaceClient implements MarketplaceApi {
30+
export class MarketplaceBackendClient implements MarketplaceApi {
3131
private readonly discoveryApi: DiscoveryApi;
3232
private readonly fetchApi: FetchApi;
3333

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 The Backstage Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
MarketplacePluginEntry,
19+
MarketplacePluginList,
20+
} from '@red-hat-developer-hub/backstage-plugin-marketplace-common';
21+
22+
import { MarketplaceApi } from './MarketplaceApi';
23+
import { CatalogApi } from '@backstage/plugin-catalog-react';
24+
25+
type CatalogApiOptions = {
26+
catalogApi: CatalogApi;
27+
};
28+
29+
export class MarketplaceCatalogClient implements MarketplaceApi {
30+
private readonly catalogApi: CatalogApi;
31+
32+
constructor(options: CatalogApiOptions) {
33+
this.catalogApi = options.catalogApi;
34+
}
35+
36+
async getPlugins(): Promise<MarketplacePluginEntry[]> {
37+
const result = await this.catalogApi.getEntities({
38+
filter: {
39+
kind: 'plugin',
40+
},
41+
});
42+
43+
return result?.items as MarketplacePluginEntry[];
44+
}
45+
46+
async getPluginList(): Promise<MarketplacePluginList[]> {
47+
const result = await this.catalogApi.getEntities({
48+
filter: {
49+
kind: 'pluginList',
50+
},
51+
});
52+
return result.items as MarketplacePluginList[];
53+
}
54+
}

workspaces/marketplace/plugins/marketplace/src/api/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { createApiRef } from '@backstage/core-plugin-api';
1818
import { MarketplaceApi } from './MarketplaceApi';
1919

2020
export * from './MarketplaceApi';
21-
export * from './MarketplaceClient';
21+
export * from './MarketplaceBackendClient';
22+
export * from './MarketplaceCatalogClient';
2223

2324
export const marketplaceApiRef = createApiRef<MarketplaceApi>({
2425
id: 'plugin.marketplace.api-ref',

workspaces/marketplace/plugins/marketplace/src/components/MarketplaceCatalogContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const MarketplaceCatalogContent = () => {
3636
>
3737
<Typography variant="h5">
3838
All plugins
39-
{plugins.data ? ` (${plugins.data.length})` : null}
39+
{plugins.data ? ` (${plugins.data?.length})` : null}
4040
</Typography>
4141
<SearchTextField variant="filter" />
4242
</Stack>

workspaces/marketplace/plugins/marketplace/src/hooks/usePlugins.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616
import { useApi } from '@backstage/core-plugin-api';
1717

1818
import { useQuery } from '@tanstack/react-query';
19-
2019
import { marketplaceApiRef } from '../api';
2120

2221
export const usePlugins = () => {
2322
const marketplaceApi = useApi(marketplaceApiRef);
24-
25-
// const [search, setSearch] = useQueryParamState<string | undefined>('q');
26-
2723
return useQuery({
2824
queryKey: ['plugins'],
2925
queryFn: () => marketplaceApi.getPlugins(),

workspaces/marketplace/plugins/marketplace/src/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
* limitations under the License.
1515
*/
1616
import {
17-
createApiFactory,
1817
createPlugin,
1918
createRoutableExtension,
2019
createComponentExtension,
20+
type IconComponent,
21+
createApiFactory,
2122
discoveryApiRef,
2223
fetchApiRef,
23-
type IconComponent,
2424
} from '@backstage/core-plugin-api';
2525

2626
import MUIMarketplaceIcon from '@mui/icons-material/ShoppingBasketOutlined';
2727

2828
import { rootRouteRef } from './routes';
29-
import { marketplaceApiRef, MarketplaceClient } from './api';
29+
import { marketplaceApiRef, MarketplaceBackendClient } from './api';
3030

3131
/**
3232
* Marketplace Plugin
@@ -45,7 +45,7 @@ export const marketplacePlugin = createPlugin({
4545
fetchApi: fetchApiRef,
4646
},
4747
factory: ({ discoveryApi, fetchApi }) =>
48-
new MarketplaceClient({
48+
new MarketplaceBackendClient({
4949
discoveryApi,
5050
fetchApi,
5151
}),

workspaces/marketplace/yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10082,6 +10082,7 @@ __metadata:
1008210082
"@backstage/core-components": ^0.15.1
1008310083
"@backstage/core-plugin-api": ^1.10.0
1008410084
"@backstage/dev-utils": ^1.1.2
10085+
"@backstage/plugin-catalog-react": ^1.14.0
1008510086
"@backstage/test-utils": ^1.7.0
1008610087
"@backstage/theme": ^0.6.0
1008710088
"@mui/icons-material": ^5.16.7

0 commit comments

Comments
 (0)