@waline/api
Version:
Client API for Waline comment system
3 lines (2 loc) • 3.1 kB
JavaScript
const m={"Content-Type":"application/json"},s=e=>`${e.replace(/\/?$/,"/")}api/`,c=(e,n="")=>{if(typeof e=="object"&&e.errno)throw new TypeError(`${n} failed with ${e.errno}: ${e.errmsg}`);return e},p=({serverURL:e,lang:n,paths:o,type:a,signal:t})=>fetch(`${s(e)}article?path=${encodeURIComponent(o.join(","))}&type=${encodeURIComponent(a.join(","))}&lang=${n}`,{signal:t}).then(r=>r.json()).then(r=>c(r,"Get counter").data),d=({serverURL:e,lang:n,path:o,type:a,action:t})=>fetch(`${s(e)}article?lang=${n}`,{method:"POST",headers:m,body:JSON.stringify({path:o,type:a,action:t})}).then(r=>r.json()).then(r=>c(r,"Update counter").data),$=({serverURL:e,lang:n,path:o,page:a,pageSize:t,sortBy:r,signal:h,token:i})=>{const l={};return i&&(l.Authorization=`Bearer ${i}`),fetch(`${s(e)}comment?path=${encodeURIComponent(o)}&pageSize=${t}&page=${a}&lang=${n}&sortBy=${r}`,{signal:h,headers:l}).then(g=>g.json()).then(g=>c(g,"Get comment data").data)},u=({serverURL:e,lang:n,token:o,comment:a})=>{const t={"Content-Type":"application/json"};return o&&(t.Authorization=`Bearer ${o}`),fetch(`${s(e)}comment?lang=${n}`,{method:"POST",headers:t,body:JSON.stringify(a)}).then(r=>r.json())},y=({serverURL:e,lang:n,token:o,objectId:a})=>fetch(`${s(e)}comment/${a}?lang=${n}`,{method:"DELETE",headers:{Authorization:`Bearer ${o}`}}).then(t=>t.json()).then(t=>c(t,"Delete comment")),U=({serverURL:e,lang:n,token:o,objectId:a,comment:t})=>fetch(`${s(e)}comment/${a}?lang=${n}`,{method:"PUT",headers:{...m,Authorization:`Bearer ${o}`},body:JSON.stringify(t)}).then(r=>r.json()).then(r=>c(r,"Update comment")),f=({serverURL:e,lang:n,paths:o,signal:a})=>fetch(`${s(e)}comment?type=count&url=${encodeURIComponent(o.join(","))}&lang=${n}`,{signal:a}).then(t=>t.json()).then(t=>c(t,"Get comment count").data),R=({lang:e,serverURL:n})=>{const o=(window.innerWidth-450)/2,a=(window.innerHeight-450)/2,t=window.open(`${n.replace(/\/$/,"")}/ui/login?lng=${encodeURIComponent(e)}`,"_blank",`width=450,height=450,left=${o},top=${a},scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no`);return t?.postMessage({type:"TOKEN",data:null},"*"),new Promise(r=>{const h=({data:i})=>{!i||typeof i!="object"||i.type!=="userInfo"||i.data.token&&(t?.close(),window.removeEventListener("message",h),r(i.data))};window.addEventListener("message",h)})},j=({serverURL:e,lang:n,paths:o,signal:a})=>p({serverURL:e,lang:n,paths:o,type:["time"],signal:a}),v=e=>d({...e,type:"time",action:"inc"}),w=({serverURL:e,lang:n,count:o,signal:a,token:t})=>{const r={};return t&&(r.Authorization=`Bearer ${t}`),fetch(`${s(e)}comment?type=recent&count=${o}&lang=${n}`,{signal:a,headers:r}).then(h=>h.json())},C=({serverURL:e,signal:n,pageSize:o,lang:a})=>fetch(`${s(e)}user?pageSize=${o}&lang=${a}`,{signal:n}).then(t=>t.json()).then(t=>c(t,"user list")).then(t=>t.data);export{u as addComment,y as deleteComment,f as fetchCommentCount,p as getArticleCounter,$ as getComment,j as getPageview,w as getRecentComment,C as getUserList,R as login,d as updateArticleCounter,U as updateComment,v as updatePageview};
//# sourceMappingURL=api.js.map