-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetch.class.js
More file actions
32 lines (27 loc) · 849 Bytes
/
Fetch.class.js
File metadata and controls
32 lines (27 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"use strict";
export class Fetch {
constructor(url, method = "POST", async = true) {
this.url = url;
this.method = method;
this.async = async;
}
/**
* Returns fetch() response in format specified by attribute.
* @param {string} dataType "text", "json", "formData" or "blob".
*/
async request(dataType = "json") {
let response = await fetch(this.url);
switch (dataType) {
case "text":
return await response.text();
case "json":
return await response.json();
case "formData":
return await response.formData();
case "blob":
return await response.arrayBuffer();
default:
throw new Error("Invalid attribute given");
}
}
}