import { getBackendSrv } from '@grafana/runtime'; import { API_ROOT, GRAFANA_API_ROOT } from './constants'; import { PluginDetails, Org, LocalPlugin, RemotePlugin, CatalogPlugin, CatalogPluginDetails } from './types'; import { mergeLocalsAndRemotes, mergeLocalAndRemote } from './helpers'; export async function getCatalogPlugins(): Promise { const [localPlugins, remotePlugins] = await Promise.all([getLocalPlugins(), getRemotePlugins()]); return mergeLocalsAndRemotes(localPlugins, remotePlugins); } export async function getCatalogPlugin(id: string): Promise { const { local, remote } = await getPlugin(id); return mergeLocalAndRemote(local, remote); } export async function getPluginDetails(id: string): Promise { const localPlugins = await getLocalPlugins(); const local = localPlugins.find((p) => p.id === id); const isInstalled = Boolean(local); const [remote, versions] = await Promise.all([getRemotePlugin(id, isInstalled), getPluginVersions(id)]); const dependencies = remote?.json?.dependencies; return { grafanaDependency: dependencies?.grafanaDependency || dependencies?.grafanaVersion || '', pluginDependencies: dependencies?.plugins || [], links: remote?.json?.info.links || local?.info.links || [], readme: remote?.readme, versions, }; } async function getRemotePlugins(): Promise { const res = await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins`); return res.items; } async function getPlugin(slug: string): Promise { const installed = await getLocalPlugins(); const localPlugin = installed?.find((plugin: LocalPlugin) => { return plugin.id === slug; }); const [remote, versions] = await Promise.all([getRemotePlugin(slug, Boolean(localPlugin)), getPluginVersions(slug)]); return { remote: remote, remoteVersions: versions, local: localPlugin, }; } async function getRemotePlugin(id: string, isInstalled: boolean): Promise { try { return await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins/${id}`); } catch (error) { // this might be a plugin that doesn't exist on gcom. error.isHandled = isInstalled; return; } } async function getPluginVersions(id: string): Promise { try { const versions = await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins/${id}/versions`); return versions.items; } catch (error) { return []; } } async function getLocalPlugins(): Promise { const installed = await getBackendSrv().get(`${API_ROOT}`, { embedded: 0 }); return installed; } async function getOrg(slug: string): Promise { const org = await getBackendSrv().get(`${GRAFANA_API_ROOT}/orgs/${slug}`); return { ...org, avatarUrl: `${GRAFANA_API_ROOT}/orgs/${slug}/avatar` }; } export async function installPlugin(id: string, version: string) { return await getBackendSrv().post(`${API_ROOT}/${id}/install`, { version, }); } export async function uninstallPlugin(id: string) { return await getBackendSrv().post(`${API_ROOT}/${id}/uninstall`); } export const api = { getRemotePlugins, getPlugin, getInstalledPlugins: getLocalPlugins, getOrg, installPlugin, uninstallPlugin, };