Fetching Data

Fetching Data with fetchPet.js

  1. Create a new file called fetchPet.js for making API Requests.

  2. Handling errors.

const fetchPet = async ({ queryKey }) => {
  const id = queryKey[1];
  const apiRes = await fetch(`http://pets-v2.dev-apis.com/pets?id=${id}`);

  if (!apiRes.ok) {
    throw new Error(`details/${id} fetch not ok`);
  }

  return apiRes.json();
};

export default fetchPet;

Last updated