chore: use RequestBody for everything else

This commit is contained in:
swve 2023-02-04 16:32:29 +01:00
parent ab96e34029
commit 6e037538af
5 changed files with 23 additions and 155 deletions

View file

@ -1,4 +1,5 @@
import { getAPIUrl } from "./config"; import { getAPIUrl } from "./config";
import { RequestBody } from "./utils/requests";
/* /*
This file includes only POST, PUT, DELETE requests This file includes only POST, PUT, DELETE requests
@ -6,36 +7,14 @@ import { getAPIUrl } from "./config";
*/ */
export async function deleteCollection(collection_id: any) { export async function deleteCollection(collection_id: any) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); return fetch(`${getAPIUrl()}collections/${collection_id}`, RequestBody("DELETE", null))
const requestOptions: any = {
method: "DELETE",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
};
return fetch(
`${getAPIUrl()}collections/${collection_id}`,
requestOptions
)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }
// Create a new collection // Create a new collection
export async function createCollection(collection: any) { export async function createCollection(collection: any) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); return fetch(`${getAPIUrl()}collections/`, RequestBody("POST", collection))
const requestOptions: any = {
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: JSON.stringify(collection),
};
return fetch(`${getAPIUrl()}collections/`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }

View file

@ -1,23 +1,13 @@
import { getAPIUrl } from "@services/config"; import { getAPIUrl } from "@services/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
export async function createLecture(data: any, chapter_id: any) { export async function createLecture(data: any, chapter_id: any) {
data.content = {}; data.content = {};
console.log("data", data, chapter_id);
// remove chapter_id from data // remove chapter_id from data
delete data.chapterId; delete data.chapterId;
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); const result: any = await fetch(`${getAPIUrl()}lectures/?coursechapter_id=${chapter_id}`, RequestBody("POST", data))
const requestOptions: any = {
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: JSON.stringify(data),
};
const result: any = await fetch(`${getAPIUrl()}lectures/?coursechapter_id=${chapter_id}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
@ -27,15 +17,9 @@ export async function createLecture(data: any, chapter_id: any) {
} }
export async function createFileLecture(file: File, type: string, data: any, chapter_id: any) { export async function createFileLecture(file: File, type: string, data: any, chapter_id: any) {
const HeadersConfig = new Headers();
// Send file thumbnail as form data // Send file thumbnail as form data
const formData = new FormData(); const formData = new FormData();
formData.append("coursechapter_id", chapter_id); formData.append("coursechapter_id", chapter_id);
console.log("type" , type);
let endpoint = `${getAPIUrl()}lectures/video`; let endpoint = `${getAPIUrl()}lectures/video`;
@ -45,36 +29,17 @@ export async function createFileLecture(file: File, type: string, data: any, cha
endpoint = `${getAPIUrl()}lectures/video`; endpoint = `${getAPIUrl()}lectures/video`;
} }
console.log(); const result: any = await fetch(endpoint, RequestBodyForm("POST", formData))
const requestOptions: any = {
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: formData,
};
const result: any = await fetch(endpoint, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
console.log("result", result); console.log("result", result);
return result; return result;
} }
export async function getLecture(lecture_id: any) { export async function getLecture(lecture_id: any) {
const requestOptions: any = { const result: any = await fetch(`${getAPIUrl()}lectures/${lecture_id}`, RequestBody("GET", null))
method: "GET",
redirect: "follow",
credentials: "include",
};
const result: any = await fetch(`${getAPIUrl()}lectures/${lecture_id}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
@ -82,17 +47,7 @@ export async function getLecture(lecture_id: any) {
} }
export async function updateLecture(data: any, lecture_id: any) { export async function updateLecture(data: any, lecture_id: any) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); const result: any = await fetch(`${getAPIUrl()}lectures/${lecture_id}`, RequestBody("PUT", data))
const requestOptions: any = {
method: "PUT",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: JSON.stringify(data),
};
const result: any = await fetch(`${getAPIUrl()}lectures/${lecture_id}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));

View file

@ -1,38 +1,20 @@
import { getAPIUrl } from "@services/config"; import { getAPIUrl } from "@services/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
export async function uploadNewImageFile(file: any, lecture_id: string) { export async function uploadNewImageFile(file: any, lecture_id: string) {
const HeadersConfig = new Headers();
// Send file thumbnail as form data // Send file thumbnail as form data
const formData = new FormData(); const formData = new FormData();
formData.append("file_object", file); formData.append("file_object", file);
formData.append("lecture_id", lecture_id); formData.append("lecture_id", lecture_id);
const requestOptions: any = { return fetch(`${getAPIUrl()}files/picture`, RequestBodyForm("POST", formData))
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: formData,
};
return fetch(`${getAPIUrl()}files/picture`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }
export async function getImageFile(file_id: string) { export async function getImageFile(file_id: string) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
const requestOptions: any = {
method: "GET",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
};
// todo : add course id to url // todo : add course id to url
return fetch(`${getAPIUrl()}files/picture?file_id=${file_id}`, requestOptions) return fetch(`${getAPIUrl()}files/picture?file_id=${file_id}`, RequestBody("GET", null))
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }

View file

@ -1,38 +1,19 @@
import { getAPIUrl } from "@services/config"; import { getAPIUrl } from "@services/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
export async function uploadNewVideoFile(file: any, lecture_id: string) { export async function uploadNewVideoFile(file: any, lecture_id: string) {
const HeadersConfig = new Headers();
// Send file thumbnail as form data // Send file thumbnail as form data
const formData = new FormData(); const formData = new FormData();
formData.append("file_object", file); formData.append("file_object", file);
formData.append("lecture_id", lecture_id); formData.append("lecture_id", lecture_id);
const requestOptions: any = { return fetch(`${getAPIUrl()}files/video`, RequestBodyForm("POST", formData))
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: formData,
};
return fetch(`${getAPIUrl()}files/video`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }
export async function getVideoFile(file_id: string) { export async function getVideoFile(file_id: string) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); return fetch(`${getAPIUrl()}files/video?file_id=${file_id}`, RequestBody("GET", null))
const requestOptions: any = {
method: "GET",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
};
return fetch(`${getAPIUrl()}files/video?file_id=${file_id}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }

View file

@ -1,4 +1,5 @@
import { getAPIUrl } from "@services/config"; import { getAPIUrl } from "@services/config";
import { RequestBody } from "./utils/requests";
/* /*
This file includes only POST, PUT, DELETE requests This file includes only POST, PUT, DELETE requests
@ -6,49 +7,19 @@ import { getAPIUrl } from "@services/config";
*/ */
export async function createNewOrganization(body: any) { export async function createNewOrganization(body: any) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); return fetch(`${getAPIUrl()}orgs/`, RequestBody("POST", body))
const requestOptions: any = {
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: JSON.stringify(body),
};
return fetch(`${getAPIUrl()}orgs/`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }
export async function deleteOrganizationFromBackend(org_id: any) { export async function deleteOrganizationFromBackend(org_id: any) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" }); return fetch(`${getAPIUrl()}orgs/${org_id}`, RequestBody("DELETE", null))
const requestOptions: any = {
method: "DELETE",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
};
return fetch(`${getAPIUrl()}orgs/${org_id}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }
export async function getOrganizationContextInfo(org_slug: any) {
export async function getOrganizationContextInfo(org_slug : any){ return fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, RequestBody("GET", null))
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
const requestOptions: any = {
method: "GET",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
};
return fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
}
}