removed api utils handleResponse and handleError

master
Tudor Stanciu 2020-05-07 00:48:50 +03:00
parent 3b7eede714
commit 7a16c738b4
3 changed files with 7 additions and 31 deletions

View File

@ -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;
}

View File

@ -1,6 +1,6 @@
import { handleResponse, handleError } from "./apiUtils"; import { get } from "./api";
const baseUrl = process.env.API_URL + "/authors/"; const baseUrl = process.env.API_URL + "/authors/";
export function getAuthors() { export function getAuthors() {
return fetch(baseUrl).then(handleResponse).catch(handleError); return get(baseUrl);
} }

View File

@ -1,22 +1,15 @@
import { handleResponse, handleError } from "./apiUtils"; import { get, del, post, put } from "./api";
const baseUrl = process.env.API_URL + "/courses/"; const baseUrl = process.env.API_URL + "/courses/";
export function getCourses() { export function getCourses() {
return fetch(baseUrl).then(handleResponse).catch(handleError); return get(baseUrl);
} }
export function saveCourse(course) { export function saveCourse(course) {
return fetch(baseUrl + (course.id || ""), { const url = baseUrl + (course.id || "");
method: course.id ? "PUT" : "POST", // POST for create, PUT to update when id already exists. return course.id ? put(url, course) : post(url, course); // POST for create, PUT to update when id already exists.
headers: { "content-type": "application/json" },
body: JSON.stringify(course)
})
.then(handleResponse)
.catch(handleError);
} }
export function deleteCourse(courseId) { export function deleteCourse(courseId) {
return fetch(baseUrl + courseId, { method: "DELETE" }) return del(baseUrl + courseId);
.then(handleResponse)
.catch(handleError);
} }