api.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { PluginError, PluginMeta, renderMarkdown } from '@grafana/data';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. import { API_ROOT, GCOM_API_ROOT } from './constants';
  4. import { isLocalPluginVisible, isRemotePluginVisible } from './helpers';
  5. import { LocalPlugin, RemotePlugin, CatalogPluginDetails, Version, PluginVersion } from './types';
  6. export async function getPluginDetails(id: string): Promise<CatalogPluginDetails> {
  7. const remote = await getRemotePlugin(id);
  8. const isPublished = Boolean(remote);
  9. const [localPlugins, versions, localReadme] = await Promise.all([
  10. getLocalPlugins(),
  11. getPluginVersions(id, isPublished),
  12. getLocalPluginReadme(id),
  13. ]);
  14. const local = localPlugins.find((p) => p.id === id);
  15. const dependencies = local?.dependencies || remote?.json?.dependencies;
  16. return {
  17. grafanaDependency: dependencies?.grafanaDependency ?? dependencies?.grafanaVersion ?? '',
  18. pluginDependencies: dependencies?.plugins || [],
  19. links: local?.info.links || remote?.json?.info.links || [],
  20. readme: localReadme || remote?.readme,
  21. versions,
  22. };
  23. }
  24. export async function getRemotePlugins(): Promise<RemotePlugin[]> {
  25. const { items: remotePlugins }: { items: RemotePlugin[] } = await getBackendSrv().get(`${GCOM_API_ROOT}/plugins`);
  26. return remotePlugins.filter(isRemotePluginVisible);
  27. }
  28. export async function getPluginErrors(): Promise<PluginError[]> {
  29. try {
  30. return await getBackendSrv().get(`${API_ROOT}/errors`);
  31. } catch (error) {
  32. return [];
  33. }
  34. }
  35. async function getRemotePlugin(id: string): Promise<RemotePlugin | undefined> {
  36. try {
  37. return await getBackendSrv().get(`${GCOM_API_ROOT}/plugins/${id}`, {});
  38. } catch (error) {
  39. // It can happen that GCOM is not available, in that case we show a limited set of information to the user.
  40. error.isHandled = true;
  41. return;
  42. }
  43. }
  44. async function getPluginVersions(id: string, isPublished: boolean): Promise<Version[]> {
  45. try {
  46. if (!isPublished) {
  47. return [];
  48. }
  49. const versions: { items: PluginVersion[] } = await getBackendSrv().get(`${GCOM_API_ROOT}/plugins/${id}/versions`);
  50. return (versions.items || []).map((v) => ({
  51. version: v.version,
  52. createdAt: v.createdAt,
  53. isCompatible: v.isCompatible,
  54. grafanaDependency: v.grafanaDependency,
  55. }));
  56. } catch (error) {
  57. // It can happen that GCOM is not available, in that case we show a limited set of information to the user.
  58. error.isHandled = true;
  59. return [];
  60. }
  61. }
  62. async function getLocalPluginReadme(id: string): Promise<string> {
  63. try {
  64. const markdown: string = await getBackendSrv().get(`${API_ROOT}/${id}/markdown/help`);
  65. const markdownAsHtml = markdown ? renderMarkdown(markdown) : '';
  66. return markdownAsHtml;
  67. } catch (error) {
  68. error.isHandled = true;
  69. return '';
  70. }
  71. }
  72. export async function getLocalPlugins(): Promise<LocalPlugin[]> {
  73. const localPlugins: LocalPlugin[] = await getBackendSrv().get(`${API_ROOT}`, { embedded: 0 });
  74. return localPlugins.filter(isLocalPluginVisible);
  75. }
  76. export async function installPlugin(id: string) {
  77. // This will install the latest compatible version based on the logic
  78. // on the backend.
  79. return await getBackendSrv().post(`${API_ROOT}/${id}/install`);
  80. }
  81. export async function uninstallPlugin(id: string) {
  82. return await getBackendSrv().post(`${API_ROOT}/${id}/uninstall`);
  83. }
  84. export async function updatePluginSettings(id: string, data: Partial<PluginMeta>) {
  85. const response = await getBackendSrv().datasourceRequest({
  86. url: `/api/plugins/${id}/settings`,
  87. method: 'POST',
  88. data,
  89. });
  90. return response?.data;
  91. }
  92. export const api = {
  93. getRemotePlugins,
  94. getInstalledPlugins: getLocalPlugins,
  95. installPlugin,
  96. uninstallPlugin,
  97. };