buildx-connect
Version:
Official JavaScript/TypeScript SDK for Buildx low-code platform
3 lines (2 loc) • 16.7 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("axios");class t{constructor(t){this._accessToken=null,this._refreshToken=null,this.config=t,this.axiosInstance=e.create({baseURL:t.apiEndpoint,timeout:3e4}),this.axiosInstance.interceptors.request.use(e=>(e.headers["X-API-Key"]=this.config.apiKey,e),e=>Promise.reject(e)),this.axiosInstance.interceptors.response.use(e=>e,e=>this.handleError(e))}updateConfig(e){this.config=e,this.axiosInstance.defaults.baseURL=e.apiEndpoint}setAccessToken(e){this._accessToken=e}getAccessToken(){return this._accessToken}setRefreshToken(e){this._refreshToken=e}getRefreshToken(){return this._refreshToken}clearTokens(){this._accessToken=null,this._refreshToken=null}isAuthenticated(){return null!==this._accessToken}async getHeaders(e=!1){const t=e?this._refreshToken:this._accessToken,s={"Content-Type":"application/json","X-API-Key":this.config.apiKey};return t&&(s.Authorization=`Bearer ${t}`),s}getPublicHeaders(){return{"Content-Type":"application/json","X-API-Key":this.config.apiKey}}async get(e,t){const s=await this.getHeaders();return(await this.axiosInstance.get(e,{...t,headers:{...s,...t?.headers}})).data}async post(e,t,s){const i=await this.getHeaders();return(await this.axiosInstance.post(e,t,{...s,headers:{...i,...s?.headers}})).data}async put(e,t,s){const i=await this.getHeaders();return(await this.axiosInstance.put(e,t,{...s,headers:{...i,...s?.headers}})).data}async patch(e,t,s){const i=await this.getHeaders();return(await this.axiosInstance.patch(e,t,{...s,headers:{...i,...s?.headers}})).data}async delete(e,t){const s=await this.getHeaders();return(await this.axiosInstance.delete(e,{...t,headers:{...s,...t?.headers}})).data}handleError(e){if(e.response){const{status:t,data:s}=e.response;return s&&"object"==typeof s?{error:s.error||"RequestError",message:s.message||s.description||"Something went wrong",statusCode:t,success:!1}:{error:"RequestError",message:`Request failed with status ${t}`,statusCode:t,success:!1}}return"Network Error"===e.message?{error:"NetworkError",message:"Network connection failed",statusCode:500,success:!1}:{error:"RequestError",message:e.message||"Unknown error occurred",statusCode:500,success:!1}}buildProjectUrl(e,t=""){const s=e||this.config.projectId;if(!s)throw new Error("Project ID is required");return`/project/${s}${t}`}buildOrgUrl(e,t=""){const s=e||this.config.organizationId;if(!s)throw new Error("Organization ID is required");return`/${s}${t}`}buildQueryString(e){const t=new URLSearchParams;Object.entries(e).forEach(([e,s])=>{null!=s&&("object"==typeof s?t.append(e,JSON.stringify(s)):t.append(e,String(s)))});const s=t.toString();return s?`?${s}`:""}}class s{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}setAccessToken(e){this.baseService.setAccessToken(e)}getAccessToken(){return this.baseService.getAccessToken()}setRefreshToken(e){this.baseService.setRefreshToken(e)}getRefreshToken(){return this.baseService.getRefreshToken()}clearTokens(){this.baseService.clearTokens()}isAuthenticated(){return this.baseService.isAuthenticated()}storeTokens(e){"access_token"in e&&e.access_token&&this.baseService.setAccessToken(e.access_token),"refresh_token"in e&&e.refresh_token&&this.baseService.setRefreshToken(e.refresh_token)}isErrorResponse(e){return e&&"object"==typeof e&&"success"in e&&!1===e.success}async login(e,t){const s=this.baseService.buildProjectUrl(t,"/auth/login"),i=await this.baseService.post(s,e);return this.isErrorResponse(i)||this.storeTokens(i),i}async loginWithGoogle(e,t){const s=this.baseService.buildProjectUrl(t,"/auth/google"),i=await this.baseService.post(s,e);return this.isErrorResponse(i)||this.storeTokens(i),i}async signup(e,t){const s=this.baseService.buildProjectUrl(t,"/auth/signup"),i=await this.baseService.post(s,e);return this.isErrorResponse(i)||this.storeTokens(i),i}async requestEotp(e,t){const s=this.baseService.buildProjectUrl(t,"/auth/eotp/auth/request");return await this.baseService.post(s,e)}async verifyEotp(e,t){const s=this.baseService.buildProjectUrl(t,"/auth/eotp/auth/verify"),i=await this.baseService.post(s,e);return this.isErrorResponse(i)||this.storeTokens(i),i}async getCurrentUser(e){const t=this.baseService.buildProjectUrl(e,"/auth/me");return await this.baseService.get(t)}async refreshToken(e){const t=this.baseService.buildProjectUrl(e,"/auth/token"),s=await this.baseService.post(t,{});return this.isErrorResponse(s)||"access_token"in s&&s.access_token&&this.storeTokens(s),s}async updatePassword(e,t){const s=this.baseService.buildProjectUrl(t,"/auth/password/update");return await this.baseService.post(s,{password:e})}async adminUpdatePassword(e,t,s){const i=this.baseService.buildProjectUrl(s,"/auth/password/update");return await this.baseService.post(i,{user_id:e,password:t})}async updatePushToken(e,t){const s=this.baseService.buildProjectUrl(t,"/users/push-tokens");return await this.baseService.post(s,{token:e})}async listUsers(e){const t=e?this.baseService.buildOrgUrl(e,"/auth/users"):"/auth/users";return await this.baseService.get(t)}async lookupUsers(e){const t=e?this.baseService.buildOrgUrl(e,"/auth/users/lookup"):"/auth/users/lookup";return await this.baseService.get(t)}async getUser(e,t){const s=t?this.baseService.buildOrgUrl(t,`/auth/user/${e}`):`/auth/user/${e}`;return await this.baseService.get(s)}async deleteUser(e,t){const s=this.baseService.buildProjectUrl(t,`/auth/user/${e}/delete`);return await this.baseService.post(s,{})}}class i{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async list(){return await this.baseService.get("/projects")}async get(e){return await this.baseService.get(`/project/${e}`)}async create(e){return await this.baseService.post("/projects",e)}async update(e,t){return await this.baseService.patch(`/project/${e}`,t)}async deleteById(e){return await this.baseService.delete(`/project/${e}`)}async backup(e){const t=await this.baseService.axiosInstance.get(`/project/${e}/backup`,{responseType:"blob",headers:await this.baseService.getHeaders()});if("undefined"!=typeof window){const s=window.URL.createObjectURL(t.data),i=document.createElement("a");i.href=s,i.setAttribute("download",`backup_${e}.buildx`),document.body.appendChild(i),i.click(),document.body.removeChild(i),window.URL.revokeObjectURL(s)}return{success:!0,message:"Backup downloaded successfully"}}async restore(e,t){const s=new FormData;s.append("new_project_id",e),s.append("backup_file",t);const i={...await this.baseService.getHeaders(),"Content-Type":"multipart/form-data"};return await this.baseService.post("/project/import",s,{headers:i})}}class r{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async list(e,t=!1,s=!1){const i=this.baseService.buildProjectUrl(e,"/collections"),r={};t&&(r.with_stats=!0);const a=this.baseService.buildQueryString(r),c=a?`${i}${a}`:i,n=await this.baseService.get(c);return Array.isArray(n)&&!s?n.filter(e=>e.collection_id&&!e.collection_id.startsWith("buildx_")):n}async getSchema(e,t,s=1){const i=this.baseService.buildProjectUrl(t,`/collection/${e}`),r={};s>1&&(r.depth=s);const a=this.baseService.buildQueryString(r),c=a?`${i}${a}`:i;return await this.baseService.get(c)}async set(e,t){const s=this.baseService.buildProjectUrl(t,"/collections");return await this.baseService.post(s,e)}async deleteCollection(e,t){const s=this.baseService.buildProjectUrl(t,`/collections/${e}`);return await this.baseService.delete(s)}async query(e,t,s){const i=this.baseService.buildProjectUrl(s,`/${e}`),r={};t&&(t.filter&&(r.filter=t.filter),t.jsonFilter&&(r.jsonFilter=t.jsonFilter),t.select&&(r.select=Array.isArray(t.select)?t.select.join(","):t.select),t.projection&&(r.projection=t.projection),t.options&&(r.options=t.options),t.sort&&(r.sort=t.sort),t.limit&&(r.limit=t.limit));const a=this.baseService.buildQueryString(r),c=a?`${i}${a}`:i;return await this.baseService.get(c)}async lookup(e,t,s){const i=this.baseService.buildProjectUrl(s,`/${e}`),r={};t&&(t.filter&&(r.filter=t.filter),t.jsonFilter&&(r.jsonFilter=t.jsonFilter),t.projection&&(r.projection=t.projection),t.options&&(r.options=t.options));const a=this.baseService.buildQueryString(r),c=a?`${i}${a}`:i,n=await this.baseService.get(c);return Array.isArray(n)?n.map(e=>({value:e._id,label:e._display||e.label||e.title||e.name||e.username||e.description||""})):n}async getDocument(e,t,s,i){const r=this.baseService.buildProjectUrl(i,`/${e}/${t}`),a={};s&&s.length>0&&(a.populate=s.join(","));const c=this.baseService.buildQueryString(a),n=c?`${r}${c}`:r;return await this.baseService.get(n)}async createDocument(e,t,s){const i=this.baseService.buildProjectUrl(s,`/${e}`);return await this.baseService.post(i,t)}async updateDocument(e,t,s,i){const r=this.baseService.buildProjectUrl(i,`/${e}/${t}`);return await this.baseService.patch(r,s)}async deleteDocument(e,t,s){const i=this.baseService.buildProjectUrl(s,`/${e}/${t}`);return await this.baseService.delete(i)}async deleteByFilter(e,t,s){const i=this.baseService.buildProjectUrl(s,`/${e}/delete`);return await this.baseService.post(i,{filter:t})}async import(e,t,s,i){const r=new FormData;r.append("mapping",JSON.stringify(s)),r.append("file",t);const a=this.baseService.buildProjectUrl(i,`/${e}/import`),c={...await this.baseService.getHeaders(),"Content-Type":"multipart/form-data"};return await this.baseService.post(a,r,{headers:c})}async getDataTypes(e){const t=this.baseService.buildProjectUrl(e,"/collections/types");return await this.baseService.get(t)}async validate(e,t){const s=this.baseService.buildProjectUrl(t,`/${e}/validate`);return await this.baseService.post(s,null)}async migrate(e,t){const s=this.baseService.buildProjectUrl(t,`/${e}/migrate`);return await this.baseService.post(s,null)}}class a{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async upload(e,t,s){const i=new FormData;t&&""!==t&&i.append("prefix",t),i.append("file",e),i.append("filename",e.name||"file");const r=this.baseService.buildProjectUrl(s,"/storage/upload"),a={...await this.baseService.getHeaders(),"Content-Type":"multipart/form-data; charset=utf-8"};return await this.baseService.post(r,i,{headers:a})}async uploadToCollection(e,t,s,i){const r=new FormData;s&&""!==s&&r.append("prefix",s),r.append("file",e),r.append("filename",e.name||"file"),r.append("collection_id",t);const a=this.baseService.buildProjectUrl(i,"/storage/upload"),c={...await this.baseService.getHeaders(),"Content-Type":"multipart/form-data; charset=utf-8"};return await this.baseService.post(a,r,{headers:c})}async list(e,t){const s=this.baseService.buildProjectUrl(t,`/storage?path=${encodeURIComponent(e)}`);return await this.baseService.get(s)}async getSize(e,t){const s=this.baseService.buildProjectUrl(t,`/storage/size?path=${encodeURIComponent(e)}`);return await this.baseService.get(s)}async delete(e,t){const s=this.baseService.buildProjectUrl(t,`/storage?path=${encodeURIComponent(e)}`);return await this.baseService.delete(s)}}class c{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async getTypes(){return await this.baseService.get("/flows/types")}async run(e,t=null,s="root",i={},r){const a=this.baseService.buildProjectUrl(r,`/flows/${e}/run`);return await this.baseService.post(a,{session_id:t,state:s,args:i})}async getGptFlowSuggestions(e,t){const s=this.baseService.buildProjectUrl(t,"/ai/gpt/flow/node");return await this.baseService.post(s,{instruction:e})}async getGptCollectionSuggestions(e,t){const s=this.baseService.buildProjectUrl(t,"/ai/gpt/collections");return await this.baseService.post(s,{instruction:e})}async getGptLifecycleSuggestions(e,t){const s=this.baseService.buildProjectUrl(t,"/ai/gpt/lifecycle");return await this.baseService.post(s,{instruction:e})}}class n{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async preview(e,t,s){const i=this.baseService.buildProjectUrl(s,"/templates/preview");return await this.baseService.post(i,{template:e,data:t})}async render(e,t,s){const i=this.baseService.buildProjectUrl(s,`/templates/${e}/render`);return await this.baseService.post(i,t)}async renderPDF(e,t,s){const i=this.baseService.buildProjectUrl(s,`/templates/${e}/pdf`),r={...await this.baseService.getHeaders(),responseType:"blob"},a=await this.baseService.axiosInstance.post(i,t,{headers:r});if("undefined"!=typeof window){const e=window.URL.createObjectURL(a.data),t=document.createElement("a");t.href=e,t.setAttribute("download","document.pdf"),document.body.appendChild(t),t.click(),document.body.removeChild(t),window.URL.revokeObjectURL(e)}return{success:!0,message:"PDF downloaded successfully"}}}class o{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async list(e){const t=this.baseService.buildProjectUrl(e,"/functions");return await this.baseService.get(t)}async getByName(e,t){const s=this.baseService.buildProjectUrl(t,`/functions/${e}`);return await this.baseService.get(s)}async update(e,t,s){const i=this.baseService.buildProjectUrl(s,`/functions/${e}`);return await this.baseService.post(i,t)}async getLogs(e,t){const s=this.baseService.buildProjectUrl(t,`/functions/${e}/logs`);return await this.baseService.get(s)}}class u{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async getCollection(e,t){const s=this.baseService.buildProjectUrl(t,`/buildx/collection/${e}`);return await this.baseService.get(s)}async getDocument(e,t,s){const i=this.baseService.buildProjectUrl(s,`/buildx/${e}/${t}`),r=await this.baseService.get(i);return"object"==typeof r&&null!==r&&(r._display=r._display||r.label||r.title||r.name||r.username||r.description||""),r}async query(e,t,s){const i=this.baseService.buildProjectUrl(s,`/buildx/${e}`),r={};t&&(t.filter&&(r.filter=t.filter),t.jsonFilter&&(r.jsonFilter=t.jsonFilter),t.select&&(r.select=Array.isArray(t.select)?t.select.join(","):t.select),t.projection&&(r.projection=t.projection),t.options&&(r.options=t.options),t.sort&&(r.sort=t.sort),t.limit&&(r.limit=t.limit));const a=this.baseService.buildQueryString(r),c=a?`${i}${a}`:i,n=await this.baseService.get(c);return Array.isArray(n)&&n.forEach(e=>{e._display=e._display||e.label||e.title||e.name||e.username||e.description||""}),n}async create(e,t,s){const i=this.baseService.buildProjectUrl(s,`/buildx/${e}`);return await this.baseService.post(i,t)}async update(e,t,s,i){const r=this.baseService.buildProjectUrl(i,`/buildx/${e}/${t}`);return await this.baseService.patch(r,s)}async deleteObject(e,t,s){const i=this.baseService.buildProjectUrl(s,`/buildx/${e}/${t}`);return await this.baseService.delete(i)}}class l{constructor(e){this.baseService=new t(e)}updateConfig(e){this.baseService.updateConfig(e)}async list(e){const t=e?this.baseService.buildOrgUrl(e,"/auth/api-keys"):"/auth/api-keys";return await this.baseService.get(t)}async get(e,t){const s=t?this.baseService.buildOrgUrl(t,`/auth/api-key/${e}`):`/auth/api-key/${e}`;return await this.baseService.get(s)}async getSecret(e,t){const s=t?this.baseService.buildOrgUrl(t,`/auth/api-key/${e}/secret`):`/auth/api-key/${e}/secret`;return await this.baseService.get(s)}}class h{constructor(e){this.config={apiEndpoint:e.apiEndpoint.replace(/\/$/,""),apiKey:e.apiKey,projectId:e.projectId,organizationId:e.organizationId},this._auth=new s(this.config),this._projects=new i(this.config),this._collections=new r(this.config),this._storage=new a(this.config),this._flows=new c(this.config),this._templates=new n(this.config),this._functions=new o(this.config),this._buildxObjects=new u(this.config),this._apiKeys=new l(this.config)}getConfig(){return{...this.config}}updateConfig(e){this.config={...this.config,...e},this._auth.updateConfig(this.config),this._projects.updateConfig(this.config),this._collections.updateConfig(this.config),this._storage.updateConfig(this.config),this._flows.updateConfig(this.config),this._templates.updateConfig(this.config),this._functions.updateConfig(this.config),this._buildxObjects.updateConfig(this.config),this._apiKeys.updateConfig(this.config)}auth(){return this._auth}projects(){return this._projects}collections(){return this._collections}storage(){return this._storage}flows(){return this._flows}templates(){return this._templates}functions(){return this._functions}buildxObjects(){return this._buildxObjects}apiKeys(){return this._apiKeys}}exports.ApiKeys=l,exports.Auth=s,exports.Buildx=h,exports.BuildxObjects=u,exports.Collections=r,exports.Flows=c,exports.Functions=o,exports.Projects=i,exports.Storage=a,exports.Templates=n,exports.default=h;
//# sourceMappingURL=index.js.map