I want to implement fetch
method by myself with promises with builder pattern, the backend is fine. I tested it with postman and it works fine. I don't know what could be, I tried everything but I don't get any data.
I think the problem is that it doesn't properly transform my data to JSON or something.
Any help will be appreciated.
class Fetch {
constructor(){
this.url = null;
this.result = null;
this.method = null;
this.header = null;
this.body = null;
}
call(){
return new Promise((resolve, reject) => {
fetch(this.url,
{method: this.method, header: this.header, body: this.body})
.then(response => response.json())
.then(data => {
this.result = data;
console.log(data);
resolve(data);
})
})
}
}
class FetchBuilder {
constructor(){
this.customFetch = new Fetch();
}
request(url){
this.flag = true;
this.customFetch.url = url;
return this;
}
method(method){
this.customFetch.method = method;
return this;
}
header(header){
this.customFetch.header = header;
return this;
}
body(body){
if(!this.flag){
this.customFetch.body = JSON.stringify(body);
}
return this;
}
query(obj){
}
send(){
this.customFetch.call();
}
}
const fetchObj = new FetchBuilder();
fetchObj.request('https://node-app-test-picsart.herokuapp.com/signin')
.method('POST')
.header({
'Content-Type': 'application/json;charset=utf-8'
})
.body({
email: 'bro@gmail.com',
password: 'bro'
})
.send()