UNPKG

@formbricks/api

Version:

Formbricks-api is an api wrapper for the Formbricks client API

3 lines (2 loc) 4.04 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["formbricks-api"]={})}(this,(function(t){"use strict";var e=Object.defineProperty,i=(t,i,s)=>((t,i,s)=>i in t?e(t,i,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[i]=s)(t,"symbol"!=typeof i?i+"":i,s);const s=t=>({ok:!1,error:t}),n=async(t,e,i,n)=>{const r=new URL(t+e),o=n?JSON.stringify(n):void 0,a=await(c=fetch,async(...t)=>{try{return{ok:!0,data:await c(...t)}}catch(e){return{ok:!1,error:e}}})(r.toString(),{method:i,headers:{"Content-Type":"application/json"},body:o});var c;if(!a.ok)return s(a.error);const d=a.data,l=await d.json();if(!d.ok){const t=l;return s({code:"forbidden"===t.code?"forbidden":"network_error",status:d.status,message:t.message||"Something went wrong",url:r,...Object.keys(t.details).length>0&&{details:t.details}})}return(t=>({ok:!0,data:t}))(l.data)};class r{constructor(t,e){i(this,"apiHost"),i(this,"environmentId"),this.apiHost=t,this.environmentId=e}async update(t){const e={};for(const i in t.attributes)e[i]=String(t.attributes[i]);return n(this.apiHost,`/api/v1/client/${this.environmentId}/contacts/${t.userId}/attributes`,"PUT",{attributes:e})}}class o{constructor(t,e){i(this,"apiHost"),i(this,"environmentId"),this.apiHost=t,this.environmentId=e}async create(t){return n(this.apiHost,`/api/v1/client/${this.environmentId}/displays`,"POST",t)}}class a{constructor(t,e){i(this,"apiHost"),i(this,"environmentId"),this.apiHost=t,this.environmentId=e}async create(t){return n(this.apiHost,`/api/v1/client/${this.environmentId}/responses`,"POST",t)}async update({responseId:t,finished:e,endingId:i,data:s,ttc:r,variables:o,language:a}){return n(this.apiHost,`/api/v1/client/${this.environmentId}/responses/${t}`,"PUT",{finished:e,endingId:i,data:s,ttc:r,variables:o,language:a})}}class c{constructor(t,e){i(this,"apiHost"),i(this,"environmentId"),this.apiHost=t,this.environmentId=e}async uploadFile(t,{allowedFileExtensions:e,surveyId:i}={}){if(!t.name||!t.type||!t.base64)throw new Error("Invalid file object");const s={fileName:t.name,fileType:t.type,allowedFileExtensions:e,surveyId:i},n=await fetch(`${this.apiHost}/api/v1/client/${this.environmentId}/storage`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!n.ok)throw new Error(`Upload failed with status: ${String(n.status)}`);const r=await n.json(),{data:o}=r,{signedUrl:a,fileUrl:c,signingData:d,presignedFields:l,updatedFileName:p}=o;let h={};if(d){const{signature:e,timestamp:s,uuid:n}=d;h={"X-File-Type":t.type,"X-File-Name":encodeURIComponent(p),"X-Survey-ID":i??"","X-Signature":e,"X-Timestamp":String(s),"X-UUID":n}}const u={},f=new FormData;if(l){Object.entries(l).forEach((([t,e])=>{f.append(t,e)}));try{const e=atob(t.base64.split(",")[1]),i=Uint8Array.from([...e].map((t=>t.charCodeAt(0)))),s=new Blob([i],{type:t.type});f.append("file",s)}catch(g){throw console.error(g),new Error("Error uploading file")}}u.fileBase64String=t.base64;let m={};const y=a.replace("http://localhost:3000",this.apiHost);try{m=await fetch(y,{method:"POST",...d?{headers:{...h}}:{},body:l?f:JSON.stringify(u)})}catch(g){console.error("Error uploading file",g)}if(!m.ok){if(d){const t=await m.json(),e=new Error(t.message);throw e.name="FileTooLargeError",e}const t=await m.text();if(l&&t.includes("EntityTooLarge")){const t=new Error("File size exceeds the size limit for your plan");throw t.name="FileTooLargeError",t}throw new Error(`Upload failed with status: ${String(m.status)}`)}return c}}class d{constructor(t){i(this,"response"),i(this,"display"),i(this,"storage"),i(this,"attribute");const{apiHost:e,environmentId:s}=t;this.response=new a(e,s),this.display=new o(e,s),this.attribute=new r(e,s),this.storage=new c(e,s)}}t.FormbricksAPI=class{constructor(t){i(this,"client"),this.client=new d(t)}},Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})})); //# sourceMappingURL=index.umd.cjs.map