grafana/public/app/features/plugins/admin/api.ts
Marcus Andersson ff56ea6ea6
Plugins: Combine local and remote plugins into one structure (#36859)
* adding some structure to combine the local and remote into one type.

* feat(catalog): map local and remote responses to catalog plugin

* feat(catalog): render CatalogPlugins in list

* refactor(catalog): update usePluginsByFilter to work with new data structure

* refactor(catalog): move helper functions into helpers file. delete redundent usePlugins hook

* feat(catalog): create CatalogPluginDetails and pass to PluginDetails

* feat(catalog): update types and components for plugin installation

* chore(catalog): comment so not to forget to move code out of api layer

* fix(catalog): make sure all filter shows gcom and installed

* fix(catalog): fix up getCatalogPlugin logic for only locally available plugins

* refactor(catalog): create getCatalogPluginDetails helper. Move usage to hook

* revert(catalog): put back small logos in PluginList

* revert(catalog): put back small logo for PluginDetails page

* fix(catalog): prevent useDebounce from triggering when SearchField mounts

* chore(catalog): add coment explaining reason for usedebouncewithoutfirstrender

* refactor(catalog): replace reduce with filter to remove duplicate array of all plugins

* refactor(catalog): update types for useDebounceWithoutFirstRender

* chore(catalog): remove commented out import

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
2021-07-20 15:20:24 +02:00

73 lines
2.0 KiB
TypeScript

import { getBackendSrv } from '@grafana/runtime';
import { API_ROOT, GRAFANA_API_ROOT } from './constants';
import { Plugin, PluginDetails, Org, LocalPlugin } from './types';
async function getRemotePlugins(): Promise<Plugin[]> {
const res = await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins`);
return res.items;
}
async function getPlugin(slug: string): Promise<PluginDetails> {
const installed = await getInstalledPlugins();
const localPlugin = installed?.find((plugin: LocalPlugin) => {
return plugin.id === slug;
});
const [remote, versions] = await Promise.all([getRemotePlugin(slug, localPlugin), getPluginVersions(slug)]);
return {
remote: remote,
remoteVersions: versions,
local: localPlugin,
};
}
async function getRemotePlugin(slug: string, local: LocalPlugin | undefined): Promise<Plugin | undefined> {
try {
return await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins/${slug}`);
} catch (error) {
// this might be a plugin that doesn't exist on gcom.
error.isHandled = !!local;
return;
}
}
async function getPluginVersions(id: string): Promise<any[]> {
try {
const versions = await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins/${id}/versions`);
return versions.items;
} catch (error) {
return [];
}
}
async function getInstalledPlugins(): Promise<LocalPlugin[]> {
const installed = await getBackendSrv().get(`${API_ROOT}`, { embedded: 0 });
return installed;
}
async function getOrg(slug: string): Promise<Org> {
const org = await getBackendSrv().get(`${GRAFANA_API_ROOT}/orgs/${slug}`);
return { ...org, avatarUrl: `${GRAFANA_API_ROOT}/orgs/${slug}/avatar` };
}
async function installPlugin(id: string, version: string) {
return await getBackendSrv().post(`${API_ROOT}/${id}/install`, {
version,
});
}
async function uninstallPlugin(id: string) {
return await getBackendSrv().post(`${API_ROOT}/${id}/uninstall`);
}
export const api = {
getRemotePlugins,
getPlugin,
getInstalledPlugins,
getOrg,
installPlugin,
uninstallPlugin,
};