import { getErrorMessage } from '../Error';

/**
 * Fetch me is a wrapper around fetch that returns the response or an error message
 * and handles the json conversion and fetches with a POST method
 * @param url the url to fetch
 * @param data the data to send
 * @returns
 */
export const fetchMe = async (url: string, data: object) => {
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    });

    return response;
  } catch (error) {
    return getErrorMessage(error);
  }
};
