removed api utils handleResponse and handleError
parent
3b7eede714
commit
7a16c738b4
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue