@levo-so/client
Version:
<img alt="Levo" src="https://static.levocdn.com/png/Levo-Logo.png" width="50" height="50">
2 lines (1 loc) • 8.9 kB
JavaScript
import R from"cross-fetch";class D extends Error{details;constructor(G){super(G.code);Object.setPrototypeOf(this,D.prototype),this.details=G}}class ${key;endpoint;workspace;domain;constructor(G){if(this.endpoint=G.endpoint||"https://public-api.levo.so",this.workspace=G.workspace,this.domain=G.domain||"",!G.key.trim())throw new D({code:"common.request.BAD_REQUEST",status:400,title:"API Key is required.",description:"You must provide an API key to use the Levo API.",type:"#"});this.key=G.key}async request(G,J){let Q=G.replaceAll(/^\/+/g,""),K={"Content-Type":"application/json","Levo-Workspace":this.workspace};if(this.key)K["Levo-Api-Key"]=this.key;if(J?.transaction){K["Levo-Transaction"]=J?.transaction?.identifier;let N=J?.headers;if(typeof N==="object"&&!Array.isArray(N)){let Z=N?.cookie;if(Z)Z=`${Z}, ${J?.transaction?.cookie}`;else Z=J?.transaction?.cookie;K.cookie=Z}else K.cookie=J?.transaction?.cookie}let X=await R(`${this.endpoint}/${Q}`,{...J,headers:{...J?.headers||{},...K},signal:AbortSignal.timeout(60000)}),Y={};X.headers.forEach((N,Z)=>{if(typeof Y[Z]==="undefined")Y[Z]=N;else if(Array.isArray(Y[Z]))Y[Z].push(N);else Y[Z]=[Y[Z],N]});let V=await X.text(),B=null;try{B=JSON.parse(V)}catch{}if(!B)throw new D({code:X.status.toString(),title:X.statusText,description:"An unknown error occurred.",status:X.status,type:"unknown",details:{body:V}});if(!X.ok){if(!B.status)throw new D(B.content);throw new D({code:X.status.toString(),title:X.statusText,description:"An unknown error occurred.",status:X.status,type:"unknown",details:{body:B}})}if(!B.status)throw new D(B.content);return{headers:Y,data:B.content}}async get(G,J){return await this.request(G,{...J,method:"GET"})}async post(G,J,Q){return await this.request(G,{...Q,method:"POST",body:JSON.stringify(J||{})})}async put(G,J,Q){return await this.request(G,{...Q,method:"PUT",body:JSON.stringify(J)})}async delete(G,J,Q){return await this.request(G,{...Q,method:"DELETE",body:JSON.stringify(J)})}}class O{client;constructor(G){this.client=G}async count(G,J={},Q){let K=new URLSearchParams;K.set("query",JSON.stringify({where:J}));let{data:X}=await this.client.get(`/v1/bevy/content/${G}/count?${K}`,Q);return X?.data}async aggregate(G,J={},Q){let K=new URLSearchParams;K.set("query",JSON.stringify(J));let{data:X}=await this.client.get(`/v1/bevy/content/${G}/aggregate?${K}`,Q);return X}async findUnique(G,J,Q){let{data:K}=await this.client.get(`/v1/bevy/content/${G}/${J}`,Q);return K.data}async findFirst(G,J={},Q){let K=new URLSearchParams;K.set("query",JSON.stringify({...J,limit:1}));let{data:X}=await this.client.get(`/v1/bevy/content/${G}/query?${K}`,Q);return X.data?.[0]||null}async findMany(G,J={},Q){J.page=J.page||1,J.limit=J.limit||10;let K=new URLSearchParams;K.set("query",JSON.stringify(J));let{data:X}=await this.client.get(`/v1/bevy/content/${G}/query?${K}`,Q);return X}async findManyV2(G,J={},Q){J.page=J.page||1,J.limit=J.limit||10;let K=new URLSearchParams;K.set("query",JSON.stringify(J));let{data:X}=await this.client.get(`/v1/bevy/content/${G}/query-v2?${K}`,Q);return X}async create(G,J,Q){let{data:K}=await this.client.post(`/v1/bevy/content/${G}`,J,Q);return K.data}async createBulk(G,J,Q){let{data:K}=await this.client.post(`/v1/bevy/content/${G}/bulk`,J,Q);return K.data}async createMany(G,J,Q){return this.createBulk(G,J,Q)}async edit(G,J,Q,K){let{data:X}=await this.client.put(`/v1/bevy/content/${G}/${J}`,Q,K);return X.data}async editBulk(G,J,Q,K){let{data:X}=await this.client.put(`/v1/bevy/content/${G}/edit-bulk`,{where:J,data:Q},K);return X.data}async editMany(G,J,Q,K){return this.editBulk(G,J,Q,K)}async increment(G,J,Q,K){let{data:X}=await this.client.put(`/v1/bevy/content/${G}/${J}/increment-entry`,Q,K);return!!X.data}async incrementBulk(G,J,Q,K){let{data:X}=await this.client.put(`/v1/bevy/content/${G}/increment-bulk`,{where:J,data:Q},K);return X.data}async incrementMany(G,J,Q,K){return this.incrementBulk(G,J,Q,K)}async publish(G,J,Q){let{data:K}=await this.client.put(`/v1/bevy/content/${G}/${J}/publish-entry`,{},Q);return!!K.data}async publishBulk(G,J,Q){let{data:K}=await this.client.put(`/v1/bevy/content/${G}/publish-bulk`,J,Q);return K.data}async publishMany(G,J,Q){return this.publishBulk(G,J,Q)}async unpublish(G,J,Q){let{data:K}=await this.client.put(`/v1/bevy/content/${G}/${J}/unpublish-entry`,{},Q);return!!K.data}async unpublishBulk(G,J,Q){let{data:K}=await this.client.put(`/v1/bevy/content/${G}/unpublish-bulk`,J,Q);return K.data}async unpublishMany(G,J,Q){return this.unpublishBulk(G,J,Q)}async remove(G,J,Q){let{data:K}=await this.client.delete(`/v1/bevy/content/${G}/${J}`,Q);return K.data}async removeBulk(G,J,Q){let{data:K}=await this.client.delete(`/v1/bevy/content/${G}/remove-bulk`,J,Q);return K.data}async removeMany(G,J,Q){return this.removeBulk(G,J,Q)}}class F{client;constructor(G){this.client=G}async getMe(G,J){let Q=typeof G==="string"?G:G.tk_m_at;if(!Q)throw new Error("Token is required to perform this action on the user.");let{data:K}=await this.client.get("/v1/membership/auth/get-me",{...J,headers:{Authorization:`Bearer ${Q}`}});return K.data}async updateMe(G,J,Q){let K=typeof G==="string"?G:G.tk_m_at;if(!K)throw new Error("Token is required to perform this action on the user.");let{data:X}=await this.client.put("/v1/membership/auth/update-me",J,{...Q,headers:{Authorization:`Bearer ${K}`}});return X.data}async changePassword(G,J,Q){let K=typeof G==="string"?G:G.tk_m_at;if(!K)throw new Error("Token is required to perform this action on the user.");let{data:X}=await this.client.put("/v1/membership/auth/change-password",J,{...Q,headers:{Authorization:`Bearer ${K}`}});return X.data}async signInWithPassword(G,J){let{data:Q,headers:K}=await this.client.post("/v1/membership/auth/signin-with-password",G,{transaction:J?.transaction,headers:{Origin:J?.domain||this.client.domain||""}}),X=[];if(K?.["set-cookie"]){if(Array.isArray(K["set-cookie"]))X.push(...K["set-cookie"]);else if(typeof K["set-cookie"]==="string")X.push(K["set-cookie"])}return{data:Q.data,cookies:X,headers:K,token:{access_token:Q.meta?.access_token||""}}}async signupWithPassword(G,J){let{data:Q,headers:K}=await this.client.post("/v1/membership/auth/signup-with-password",G,{transaction:J?.transaction,headers:{Origin:J?.domain||this.client.domain||""}}),X=[];if(K?.["set-cookie"]){if(Array.isArray(K["set-cookie"]))X.push(...K["set-cookie"]);else if(typeof K["set-cookie"]==="string")X.push(K["set-cookie"])}return{data:Q.data,cookies:X,headers:K,token:{access_token:Q.meta?.access_token||""}}}async signOut(G,J){let Q=typeof G==="string"?G:G.tk_m_at;if(!Q)throw new Error("Token is required to perform this action on the user.");let{data:K,headers:X}=await this.client.post("/v1/membership/auth/sign-out",{},{headers:{Authorization:`Bearer ${Q}`,Origin:J?.domain||this.client.domain||""}}),Y=[];if(X?.["set-cookie"]){if(Array.isArray(X["set-cookie"]))Y.push(...X["set-cookie"]);else if(typeof X["set-cookie"]==="string")Y.push(X["set-cookie"])}return{data:K.data,cookies:Y,headers:X}}async getOAuthURLs(G,J){let{data:Q}=await this.client.get("/v1/membership/auth/oauth/get-redirect-url",{headers:{Origin:J?.domain||this.client.domain||""}}),K={},X=Object.keys(Q.data);for(let Y of X){let V=new URL(Q.data[Y]);V.searchParams.set("redirect_uri",G),V.searchParams.set("workspace_id",this.client.workspace),K[Y]=V.toString()}return K}async requestOtp(G){let{data:J}=await this.client.post("/v1/membership/auth/request-otp",G);return J.data}async signInWithOtp(G,J){let{data:Q,headers:K}=await this.client.post("/v1/membership/auth/signin-with-otp",G,{transaction:J?.transaction,headers:{Origin:J?.domain||this.client.domain||""}}),X=[];if(K?.["set-cookie"]){if(Array.isArray(K["set-cookie"]))X.push(...K["set-cookie"]);else if(typeof K["set-cookie"]==="string")X.push(K["set-cookie"])}return{data:Q.data,cookies:X,headers:K,token:{access_token:Q.meta?.access_token||""}}}async requestMagicLink(G,J){let{data:Q}=await this.client.post("/v1/membership/auth/request-magic-link",G,{headers:{Origin:J?.domain||this.client.domain||""}});return Q.data}}class A{client;constructor(G){this.client=G}async sendEmail(G){let{data:J}=await this.client.post("/v1/beacon/notification/send-notification",{...G,key:G?.key||"custom"});return J}}class U{#G;content;membership;notification;constructor(G){let J=new $(G);this.content=new O(J),this.membership=new F(J),this.notification=new A(J),this.#G=J}async withTransaction(G){let{data:J,headers:Q}=await this.#G.post("/v1/bevy/content/start-transaction",{}),K={identifier:J.data,cookie:Array.isArray(Q?.["set-cookie"])?Q?.["set-cookie"]?.[0]:Q?.["set-cookie"]||""};try{let X=await G(K);return await this.#G.put("/v1/bevy/content/commit-transaction",{},{transaction:K}),X}catch(X){throw await this.#G.put("/v1/bevy/content/abort-transaction",{},{transaction:K}),X}}}var z=(G)=>{return new U(G)};export{z as createClient,D as LevoError};