import { BE_GESTIONMESSAGERIE_MESSAGES_URL } from '@/utils/Url';

const requestResponseHandler = async (response) => {
  const body = await response.json();
  if (response.ok) {
    return body;
  }
  // Throw an error with the JSON body containing the form errors
  const error = new Error(body?.errorMessage || 'Une erreur est survenue');
  error.details = body;
  throw error;
};

export const fetchMessages = (id) => {
  return fetch(`${BE_GESTIONMESSAGERIE_MESSAGES_URL}/${id}`, {
    headers: {
      'Content-Type': 'application/json',
    },
  }).then(requestResponseHandler);
};
