Source: components/restmodules/MonitoredAreaRestModule.js

import isUUID from 'validator/lib/isUUID';
import { API } from '../../LocalConfiguration';
import { handleErrors, errors} from "./RestModuleAPI";

const monitoredAreaPath = API + '/areas';
/**
 * Monitored Area Rest API
 * @module MonitoredAreaRestModule
 */

/**
 * Fetch All Monitored areas
 * @returns {Promise<void | never>}
 */
export function fetchMonitoredAreaList() {
  return fetch(monitoredAreaPath)
    .then(response => handleErrors(response))
    .then(response => response.json());
}

/**
 * Fetch Monitored Area by ID
 * @param areaId
 * @returns {Promise<void | never>}
 */
export function fetchMonitoredArea(areaId) {
  if (!isUUID(areaId)) {
    throw new Error(errors.invalidUUI);
  }

  return fetch(monitoredAreaPath  + "/" +  areaId)
    .then(response => handleErrors(response))
    .then(response => response.json());
}

/**
 * @param area
 * @returns {Promise<string | never>}
 */
export function createMonitoredArea(area) {
  return fetch(monitoredAreaPath, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(area)
  }).then(response => response.text());
}

/**
 *
 * @param area
 * @returns {Promise<Response | never>}
 */
export function updateMonitoredArea(area) {
  return fetch(monitoredAreaPath, {
    method: 'PUT',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(area)
  }).then(response => response.json());
}

/**
 *
 * @param areaId
 * @returns {Promise<Response>}
 */
export function deleteMonitoredArea(areaId) {
  return fetch(monitoredAreaPath + "/" + areaId, {
    method: 'DELETE',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    }
  })
}