actions.tsx 855 B

1234567891011121314151617181920212223242526272829303132333435
  1. import type {Client} from 'sentry/api';
  2. import type {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. rule: MetricRule,
  18. query?: object | any
  19. ) {
  20. const isExisting = isSavedRule(rule);
  21. const endpoint = isExisting
  22. ? `/organizations/${orgId}/alert-rules/${rule.id}/`
  23. : `/organizations/${orgId}/alert-rules/`;
  24. const method = isExisting ? 'PUT' : 'POST';
  25. return api.requestPromise(endpoint, {
  26. method,
  27. data: rule,
  28. query,
  29. includeAllArgs: true,
  30. });
  31. }