Parcourir la source

refactor code

Gildas il y a 1 an
Parent
commit
251e579836
1 fichiers modifiés avec 18 ajouts et 25 suppressions
  1. 18 25
      src/lib/rest-form-api/index.js

+ 18 - 25
src/lib/rest-form-api/index.js

@@ -44,33 +44,26 @@ class RestFormApi {
 	}
 
 	async upload(content, url) {
-
 		this.controller = new AbortController();
-		try {
-			const blob = new Blob([content], { type: "text/html" });
-			const file = new File([blob], "SingleFile.html", { type: "text/html" });
-			let formData = new FormData();
-			if (this.fileFieldName) {
-				formData.append(this.fileFieldName, file);
-			}
-			if (this.urlFieldName) {
-				formData.append(this.urlFieldName, url);
-			}
-			const response = await fetch(this.restApiUrl, {
-				method: "POST",
-				body: formData,
-				headers: this.headers,
-				signal: this.controller.signal
-			});
-			if ([200, 201].includes(response.status)) {
-				// do something with the data?
-				const data = await response.json();
-			} else {
-				throw new Error(await response.text());
-			}
+		const blob = new Blob([content], { type: "text/html" });
+		const file = new File([blob], "SingleFile.html", { type: "text/html" });
+		let formData = new FormData();
+		if (this.fileFieldName) {
+			formData.append(this.fileFieldName, file);
+		}
+		if (this.urlFieldName) {
+			formData.append(this.urlFieldName, url);
 		}
-		catch (e) {
-			throw new Error(e);
+		const response = await fetch(this.restApiUrl, {
+			method: "POST",
+			body: formData,
+			headers: this.headers,
+			signal: this.controller.signal
+		});
+		if ([200, 201].includes(response.status)) {
+			return response.json();
+		} else {
+			throw new Error(await response.text());
 		}
 	}