pluginSettings.ts 858 B

1234567891011121314151617181920212223242526272829303132
  1. import { PluginMeta } from '@grafana/data';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. type PluginCache = {
  4. [key: string]: PluginMeta;
  5. };
  6. const pluginInfoCache: PluginCache = {};
  7. export function getPluginSettings(pluginId: string): Promise<PluginMeta> {
  8. const v = pluginInfoCache[pluginId];
  9. if (v) {
  10. return Promise.resolve(v);
  11. }
  12. return getBackendSrv()
  13. .get(`/api/plugins/${pluginId}/settings`)
  14. .then((settings: any) => {
  15. pluginInfoCache[pluginId] = settings;
  16. return settings;
  17. })
  18. .catch((err: any) => {
  19. return Promise.reject(new Error('Unknown Plugin'));
  20. });
  21. }
  22. export const clearPluginSettingsCache = (pluginId?: string) => {
  23. if (pluginId) {
  24. return delete pluginInfoCache[pluginId];
  25. }
  26. // clear all
  27. return Object.keys(pluginInfoCache).forEach((key) => delete pluginInfoCache[key]);
  28. };