12 lines
1,015 B
JavaScript
12 lines
1,015 B
JavaScript
// Lifecycle-bound requests shared by User Panel pages. No automatic write retries.
|
|
export function createUserAPI(fetcher=fetch,parentSignal=new AbortController().signal){
|
|
const bounded=async(path,options={})=>fetcher(path,{credentials:'same-origin',cache:'no-store',...options,signal:AbortSignal.any([parentSignal,...(options.signal?[options.signal]:[]),AbortSignal.timeout(20000)])});
|
|
const request=async(path,body)=>{
|
|
let response,data;
|
|
try{response=await bounded(path,body===undefined?{}:{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});data=await response.json();}
|
|
catch(error){throw Object.assign(Error(error.name==='TimeoutError'?'The service did not confirm the result in time.':error.message||'Service unavailable.'),{uncertain:body!==undefined});}
|
|
if(!response.ok)throw Object.assign(Error(data.message||'Request rejected.'),{uncertain:body!==undefined&&response.status>=500,status:response.status});
|
|
return data;
|
|
};
|
|
return {fetcher:bounded,request};
|
|
}
|

Staff