import { FetchError } from '../utils/FetchHelper';

const CLEAR_DATA_API_URL =
  location.protocol + location.host + '/api/clear_data';

export async function callClearData() {
  try {
    const response = await fetch(CLEAR_DATA_API_URL.toString(), {
      method: 'DELETE',
    });
    // Catch response codes other than 200-class
    if (!response.ok) {
      const errorText = await response.text();
      throw new FetchError(
        errorText || 'Failed to execute the clear_data API method',
        response
      );
    }
  } catch (error) {
    throw new FetchError((error as Error).message);
  }
}
