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

View file

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

View file

@ -1,38 +1,20 @@
import { getAPIUrl } from "@services/config";
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
export async function uploadNewImageFile(file: any, lecture_id: string) {
const HeadersConfig = new Headers();
// Send file thumbnail as form data
const formData = new FormData();
formData.append("file_object", file);
formData.append("lecture_id", lecture_id);
const requestOptions: any = {
method: "POST",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
body: formData,
};
return fetch(`${getAPIUrl()}files/picture`, requestOptions)
return fetch(`${getAPIUrl()}files/picture`, RequestBodyForm("POST", formData))
.then((result) => result.json())
.catch((error) => console.log("error", error));
}
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
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())
.catch((error) => console.log("error", error));
}

View file

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

View file

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