You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case if someone will response with 201(Created), 202(Accepted) or other 2xx status codes - httpApiCall will throw an Error. More than that, in the source code i found that httpApiCall mutates headers parameter and even doesn't use it afterwards.
consthttpApiCall=async(url,{ method ='POST', headers ={}, body })=>{constmimeType='application/json';constcustom={'Content-Type': mimeType, ...headers};if(body)headers['Content-Length']=Buffer.byteLength(body);// Useless code// Should be replaced by: if (body) custom['Content-Length'] = Buffer.byteLength(body);constoptions={ method,headers: custom, body };returnawaitfetch(url,options).then(async(res)=>{constcode=res.status;if(code===200)returnawaitres.json();// Is that normal behavior to ignore other 2xx codes?constdest=`for ${method}${url}`;thrownewError(`HTTP status code ${code}${dest}`);});};
Additional context
I think next realization will fix this issue.
This realization also allows to provide more fetch options.
constDEFAULT_METHOD='POST';constDEFAULT_MIME='application/json';consthttpApiCall=(url,{ headers ={}, ...options})=>{constbody=options.body;options.headers={'Content-Type': DEFAULT_MIME, ...headers};options.method??=DEFAULT_METHOD;if(body)options.headers['Content-Length']=Buffer.byteLength(body);returnfetch(url,options).then(res=>{if(res.status>=200&&res.status<300)returnres.json();thrownewError(`HTTP status code ${res.status} for ${method}${url}`);});};
The text was updated successfully, but these errors were encountered:
Describe the bug
In case if someone will response with 201(Created), 202(Accepted) or other 2xx status codes - httpApiCall will throw an Error. More than that, in the source code i found that httpApiCall mutates headers parameter and even doesn't use it afterwards.
Additional context
I think next realization will fix this issue.
This realization also allows to provide more fetch options.
The text was updated successfully, but these errors were encountered: