UNPKG

@waline/api

Version:

Client API for Waline comment system

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