From 6e037538afbbfbd9c65e0bc16495492bce2ee8ec Mon Sep 17 00:00:00 2001 From: swve Date: Sat, 4 Feb 2023 16:32:29 +0100 Subject: [PATCH] chore: use RequestBody for everything else --- front/services/collections.ts | 29 +++------------- front/services/courses/lectures.ts | 55 +++--------------------------- front/services/files/images.ts | 26 +++----------- front/services/files/video.ts | 27 +++------------ front/services/orgs.ts | 41 ++++------------------ 5 files changed, 23 insertions(+), 155 deletions(-) diff --git a/front/services/collections.ts b/front/services/collections.ts index 96f8a87d..839ba84d 100644 --- a/front/services/collections.ts +++ b/front/services/collections.ts @@ -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)); -} \ No newline at end of file +} diff --git a/front/services/courses/lectures.ts b/front/services/courses/lectures.ts index c46c351a..2f961f09 100644 --- a/front/services/courses/lectures.ts +++ b/front/services/courses/lectures.ts @@ -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)); diff --git a/front/services/files/images.ts b/front/services/files/images.ts index e661469e..35f449f4 100644 --- a/front/services/files/images.ts +++ b/front/services/files/images.ts @@ -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)); -} \ No newline at end of file +} diff --git a/front/services/files/video.ts b/front/services/files/video.ts index 622e69c0..eaa10e22 100644 --- a/front/services/files/video.ts +++ b/front/services/files/video.ts @@ -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)); -} \ No newline at end of file +} diff --git a/front/services/orgs.ts b/front/services/orgs.ts index b7b945ff..44e74cdb 100644 --- a/front/services/orgs.ts +++ b/front/services/orgs.ts @@ -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)); - -} \ No newline at end of file +}