actions.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {Client} from 'sentry/api';
  2. import {MetricRule, SavedMetricRule} from './types';
  3. function isSavedRule(rule: MetricRule): rule is SavedMetricRule {
  4. return !!rule.id;
  5. }
  6. /**
  7. * Add a new rule or update an existing rule
  8. *
  9. * @param api API Client
  10. * @param orgId Organization slug
  11. * @param rule Saved or Unsaved Metric Rule
  12. * @param query Query parameters for the request eg - referrer
  13. */
  14. export function addOrUpdateRule(
  15. api: Client,
  16. orgId: string,
  17. projectId: string,
  18. rule: MetricRule,
  19. query?: object | any
  20. ) {
  21. const isExisting = isSavedRule(rule);
  22. const endpoint = `/projects/${orgId}/${projectId}/alert-rules/${
  23. isSavedRule(rule) ? `${rule.id}/` : ''
  24. }`;
  25. const method = isExisting ? 'PUT' : 'POST';
  26. return api.requestPromise(endpoint, {
  27. method,
  28. data: rule,
  29. query,
  30. includeAllArgs: true,
  31. });
  32. }
  33. /**
  34. * Delete an existing rule
  35. *
  36. * @param api API Client
  37. * @param orgId Organization slug
  38. * @param rule Saved or Unsaved Metric Rule
  39. */
  40. export function deleteRule(
  41. api: Client,
  42. orgId: string,
  43. rule: SavedMetricRule
  44. ): Promise<void> {
  45. return api.requestPromise(`/organizations/${orgId}/alert-rules/${rule.id}/`, {
  46. method: 'DELETE',
  47. });
  48. }