api.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { Team, UserDTO, UserOrg, UserSession } from '../../types';
  3. import { ChangePasswordFields, ProfileUpdateFields } from './types';
  4. async function changePassword(payload: ChangePasswordFields): Promise<void> {
  5. try {
  6. await getBackendSrv().put('/api/user/password', payload);
  7. } catch (err) {
  8. console.error(err);
  9. }
  10. }
  11. function loadUser(): Promise<UserDTO> {
  12. return getBackendSrv().get('/api/user');
  13. }
  14. function loadTeams(): Promise<Team[]> {
  15. return getBackendSrv().get('/api/user/teams');
  16. }
  17. function loadOrgs(): Promise<UserOrg[]> {
  18. return getBackendSrv().get('/api/user/orgs');
  19. }
  20. function loadSessions(): Promise<UserSession[]> {
  21. return getBackendSrv().get('/api/user/auth-tokens');
  22. }
  23. async function revokeUserSession(tokenId: number): Promise<void> {
  24. await getBackendSrv().post('/api/user/revoke-auth-token', {
  25. authTokenId: tokenId,
  26. });
  27. }
  28. async function setUserOrg(org: UserOrg): Promise<void> {
  29. await getBackendSrv().post('/api/user/using/' + org.orgId, {});
  30. }
  31. async function updateUserProfile(payload: ProfileUpdateFields): Promise<void> {
  32. try {
  33. await getBackendSrv().put('/api/user', payload);
  34. } catch (err) {
  35. console.error(err);
  36. }
  37. }
  38. export const api = {
  39. changePassword,
  40. revokeUserSession,
  41. loadUser,
  42. loadSessions,
  43. loadOrgs,
  44. loadTeams,
  45. setUserOrg,
  46. updateUserProfile,
  47. };