/**
 * This program is based on https://escuelavue.es/en/devtips/vue-3-composable-functions
 * An open API endpoint that calls 50 dummy users is: https://randomuser.me/api/?results=50
 *
 */

import { ref, readonly } from "vue";

export function useFetch() {
  const results = ref(null);
  const error = ref(null);

	const makeFetch = async (url) => {	
    try {
			const request = await (await fetch(url, {
				headers: {
					"X-WP-Nonce": jsData.nonce,
					"Content-Type": "application/json",
				},
			})).json();
			
      results.value = request;
    } catch (err) {
      console.log('useFetch', err);
      error.value = err;
    }
  };

  return {
    makeFetch,
    results: readonly(results),
    error,
  };
} // useFetch()
