How can we define class level constant and access it in static & instance methods?
class ExternalRequests{
const HEADERS = { "Accept": "application/json, text/plain", "Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}
static get(url){
return fetch(url, {method: 'get', HEADERS})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}
static post(url, data){
return fetch(url, {method: 'post', HEADERS, body: data})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}
static put(url, data){
return fetch(url, {method: 'put', HEADERS, body: data})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}
static delete(){
return fetch(url, {method: 'delete', HEADERS})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}
}
export default ExternalRequests;
ERROR
ERROR in ./externalRequests.js
Module build failed: SyntaxError: Unexpected token (3:8)
1 | class ExternalRequests{
2 |
> 3 | const HEADERS = { "Accept": "application/json, text/plain", "Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}