forked from RiseUP/riseup-squad18
5 lines
7.6 KiB
JavaScript
5 lines
7.6 KiB
JavaScript
var V=Object.defineProperty,G=Object.defineProperties;var Y=Object.getOwnPropertyDescriptors;var q=Object.getOwnPropertySymbols;var z=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable;var F=i=>{throw TypeError(i)};var j=(i,e,t)=>e in i?V(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,A=(i,e)=>{for(var t in e||(e={}))z.call(e,t)&&j(i,t,e[t]);if(q)for(var t of q(e))Z.call(e,t)&&j(i,t,e[t]);return i},H=(i,e)=>G(i,Y(e));var K=(i,e,t)=>e.has(i)||F("Cannot "+t);var n=(i,e,t)=>(K(i,e,"read from private field"),t?t.call(i):e.get(i)),u=(i,e,t)=>e.has(i)?F("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(i):e.set(i,t),g=(i,e,t,r)=>(K(i,e,"write to private field"),r?r.call(i,t):e.set(i,t),t);var c=(i,e,t)=>new Promise((r,o)=>{var s=p=>{try{h(t.next(p))}catch(f){o(f)}},l=p=>{try{h(t.throw(p))}catch(f){o(f)}},h=p=>p.done?r(p.value):Promise.resolve(p.value).then(s,l);h((t=t.apply(i,e)).next())});import{v4 as se}from"uuid";import X from"crypto-js/enc-base64";import W from"crypto-js/enc-hex";import ee from"crypto-js/hmac-sha256";import te from"crypto-js/sha256";import{ofetch as ie}from"ofetch";var re="6QrJZ7pFCmBZAeIJF7IArvkCz+EtzA0RVcpHkiQIsQyhs7QtCS9P+CueZdHfB2OtJcgX3BbqY9pfpWeAVTqCwQ==";function _(i){return encodeURIComponent(i).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`)}function ne(i){return e=>{let{options:t}=e,r=Math.floor(Date.now()/1e3).toString(),o=Math.random().toString(36).substring(2,15),s=A({},t.query),l=Object.keys(s).sort().map(w=>`${_(w)}=${_(String(s[w]))}`).join("&"),h={"x-timestamp":r,"x-nonce":o},p=Object.keys(h).sort().map(w=>`${w}:${h[w]}`).join(`
|
|
`),f=t.body&&!(t.body instanceof FormData)?JSON.stringify(t.body):"",x=te(f).toString(W),S=[l,p,x].join(`
|
|
`);console.warn(`Client-side Canonical Request V3:
|
|
`,S);let d=X.stringify(ee(S,i)),T=new Headers(t.headers);Object.entries(h).forEach(([w,Q])=>{T.set(w,Q)}),T.set("X-Sign",d),t.headers=T}}function a(i,e,t={}){return i.auth.accessToken&&(t.headers=A({Authorization:`Bearer ${i.auth.accessToken}`},t.headers)),ie(e,H(A({baseURL:i.config.apiBaseUrl},t),{onRequest:ne(re)}))}function B(){var i,e;return(e=(i=document.querySelector('link[rel="icon"]'))==null?void 0:i.href)!=null?e:null}function J(){var i;return(i=document.title)!=null?i:null}function k(i,e,t=localStorage){let r=t.getItem(i),o=e?JSON.stringify(e):null;o?t.setItem(i,o):t.removeItem(i),window.dispatchEvent(new StorageEvent("storage",{key:i,oldValue:r,newValue:o,storageArea:t}))}function N(i,e=localStorage){let t=e.getItem(i);try{return t?JSON.parse(t):null}catch(r){return null}}var y,R,L=class{constructor(e){u(this,y);u(this,R,`lumi-auth-${se()}`);g(this,y,e)}get accessToken(){return N("lumi-access-token")}set accessToken(e){k("lumi-access-token",e)}get user(){return N("lumi-user")}set user(e){k("lumi-user",e)}get isAuthenticated(){return!!this.accessToken}signIn(){let r=(window.screen.width-800)/2,o=(window.screen.height-600)/2,s=window.open(n(this,y).config.authOrigin,n(this,R),`width=800,height=600,left=${r},top=${o}`),l;return new Promise((h,p)=>{if(!s)return p(new Error("Open auth window failed"));let f=setInterval(()=>{s.closed&&p(new Error("Auth window closed"))},1e3),x=d=>{s.closed||(s.focus(),d.stopPropagation(),d.preventDefault())},S=({data:d,origin:T,source:w})=>{if(!(T!==n(this,y).config.authOrigin||w!==s))switch(d==null?void 0:d.type){case"lumi-ready":{s.postMessage({type:"lumi-init",data:{projectId:n(this,y).config.projectId,icon:B(),title:J()}},n(this,y).config.authOrigin);break}case"lumi-sign-in":{if(d.data.projectId!==n(this,y).config.projectId)break;s.close(),window.focus(),this.accessToken=d.data.accessToken,this.user=d.data.user,h(d.data);break}}};window.addEventListener("message",S),document.addEventListener("click",x,!0),l=()=>{clearInterval(f),window.removeEventListener("message",S),document.removeEventListener("click",x,!0)}}).finally(()=>l==null?void 0:l())}signOut(){this.accessToken=null,this.user=null}refreshUser(){return c(this,null,function*(){let e=yield a(n(this,y),"/lm/user/info",{method:"POST"});if(e.code!==200)throw new Error(e.message);return this.user=e.data,e.data})}onAuthChange(e){let t=r=>{(r.key==="lumi-access-token"||r.key==="lumi-user"||r.key===null)&&e({isAuthenticated:this.isAuthenticated,user:this.user})};return window.addEventListener("storage",t),()=>{window.removeEventListener("storage",t)}}};y=new WeakMap,R=new WeakMap;var m,v=class{constructor(e,t){u(this,m);g(this,m,e),this.entityName=t}list(){return c(this,arguments,function*({filter:e,sort:t,limit:r,skip:o}={}){if(r){let s=yield a(n(this,m),this.uri("/find"),{method:"POST",body:{filter:e,sort:t,limit:r,skip:o}});if(s.code!==200)throw new Error(s.message);return s.data}else{let s=yield a(n(this,m),this.uri("/list"),{method:"POST",body:{filter:e,sort:t}});if(s.code!==200)throw new Error(s.message);return{total:s.data.length,list:s.data}}})}get(e){return c(this,null,function*(){let t=yield a(n(this,m),this.uri(`/${e}`),{method:"GET"});if(t.code!==200)throw new Error(t.message);return t.data})}create(e){return c(this,null,function*(){let t=yield a(n(this,m),this.uri(),{method:"POST",body:e});if(t.code!==200)throw new Error(t.message);return t.data})}createMany(e){return c(this,null,function*(){let t=yield a(n(this,m),this.uri("/batch"),{method:"POST",body:e});if(t.code!==200)throw new Error(t.message);return t.data})}update(e,t){return c(this,null,function*(){let r=yield a(n(this,m),this.uri(),{method:"PUT",body:{filter:{_id:e},update:t}});if(r.code!==200)throw new Error(r.message);return r.data})}delete(e){return c(this,null,function*(){let t=yield a(n(this,m),this.uri(`/${e}`),{method:"DELETE"});if(t.code!==200)throw new Error(t.message)})}deleteMany(e){return c(this,null,function*(){let t=yield a(n(this,m),this.uri("/batch-by-ids"),{method:"DELETE",params:{ids:e}});if(t.code!==200)throw new Error(t.message)})}uri(e=""){return`/lm/${n(this,m).config.projectId}/${this.entityName}/documents${e}`}};m=new WeakMap;var I,P=class{constructor(e){u(this,I);return g(this,I,e),new Proxy(this,{get(t,r){return r in t||(t[r]=new v(n(t,I),r)),t[r]}})}};I=new WeakMap;var b=class extends Error{constructor(t,r){super(r);this.name="LumiError";this.code=t}};var C,O=class{constructor(e){u(this,C);g(this,C,e)}send(p){return c(this,arguments,function*({to:e,subject:t,fromName:r,html:o,text:s="",replyTo:l,scheduledAt:h}){if(!e||!t||!o&&!s)throw new Error("Failed to send email: Missing required parameters.");typeof e=="string"&&(e=[e]),typeof l=="string"&&(l=[l]);let f=yield a(n(this,C),`/lm/${n(this,C).config.projectId}/email/send`,{method:"POST",body:{to:e,subject:t,fromName:r,html:o,text:s,replyTo:l,scheduledAt:h}});if(f.code!==200)throw new b(f.code,f.message)})}};C=new WeakMap;var E,D=class{constructor(e){u(this,E);g(this,E,e)}upload(e){return c(this,null,function*(){let t=new FormData;e.forEach(o=>{t.append("files",o)});let r=yield a(n(this,E),`/lm/${n(this,E).config.projectId}/file/batch`,{method:"POST",body:t});if(r.code!==200)throw new b(r.code,r.message);return r.data})}delete(e){return c(this,null,function*(){let t=yield a(n(this,E),`/lm/${n(this,E).config.projectId}/file/batch`,{method:"DELETE",body:{fileUrls:e}});if(t.code!==200)throw new b(t.code,t.message)})}};E=new WeakMap;var U,M=class{constructor(e){u(this,U);g(this,U,e),this.email=new O(e),this.file=new D(e)}};U=new WeakMap;var $=class{constructor(e){this.config=e,this.auth=new L(this),this.entities=new P(this),this.tools=new M(this)}};function _e(i){return new $(i)}export{O as EmailTool,P as EntitiesClient,v as EntityClient,D as FileTool,L as LumiAuthClient,$ as LumiClient,M as ToolsClient,_e as createClient};
|
|
//# sourceMappingURL=index.mjs.map
|