web/website/control-panel/public/cp-v2/cp-v2-user-api.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

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};
}