From 7a16c738b4d011d576638c9704c8af9a9422c4f1 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 7 May 2020 00:48:50 +0300 Subject: [PATCH] removed api utils handleResponse and handleError --- src/api/apiUtils.js | 17 ----------------- src/api/authorApi.js | 4 ++-- src/api/courseApi.js | 17 +++++------------ 3 files changed, 7 insertions(+), 31 deletions(-) delete mode 100644 src/api/apiUtils.js diff --git a/src/api/apiUtils.js b/src/api/apiUtils.js deleted file mode 100644 index a97e14f..0000000 --- a/src/api/apiUtils.js +++ /dev/null @@ -1,17 +0,0 @@ -export async function handleResponse(response) { - if (response.ok) return response.json(); - if (response.status === 400) { - // So, a server-side validation error occurred. - // Server side validation returns a string error message, so parse as text instead of json. - const error = await response.text(); - throw new Error(error); - } - throw new Error("Network response was not ok."); -} - -// In a real app, would likely call an error logging service. -export function handleError(error) { - // eslint-disable-next-line no-console - console.error("API call failed. " + error); - throw error; -} diff --git a/src/api/authorApi.js b/src/api/authorApi.js index 8439475..2689db1 100644 --- a/src/api/authorApi.js +++ b/src/api/authorApi.js @@ -1,6 +1,6 @@ -import { handleResponse, handleError } from "./apiUtils"; +import { get } from "./api"; const baseUrl = process.env.API_URL + "/authors/"; export function getAuthors() { - return fetch(baseUrl).then(handleResponse).catch(handleError); + return get(baseUrl); } diff --git a/src/api/courseApi.js b/src/api/courseApi.js index 134d04c..366accb 100644 --- a/src/api/courseApi.js +++ b/src/api/courseApi.js @@ -1,22 +1,15 @@ -import { handleResponse, handleError } from "./apiUtils"; +import { get, del, post, put } from "./api"; const baseUrl = process.env.API_URL + "/courses/"; export function getCourses() { - return fetch(baseUrl).then(handleResponse).catch(handleError); + return get(baseUrl); } export function saveCourse(course) { - return fetch(baseUrl + (course.id || ""), { - method: course.id ? "PUT" : "POST", // POST for create, PUT to update when id already exists. - headers: { "content-type": "application/json" }, - body: JSON.stringify(course) - }) - .then(handleResponse) - .catch(handleError); + const url = baseUrl + (course.id || ""); + return course.id ? put(url, course) : post(url, course); // POST for create, PUT to update when id already exists. } export function deleteCourse(courseId) { - return fetch(baseUrl + courseId, { method: "DELETE" }) - .then(handleResponse) - .catch(handleError); + return del(baseUrl + courseId); }