deta
Version:
Deta library for Javascript
2 lines (1 loc) • 15.4 kB
JavaScript
var e,t=function(){return(t=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function r(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{a(n.next(e))}catch(e){i(e)}}function u(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,u)}a((n=n.apply(e,t||[])).next())}))}function n(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}!function(e){e[e.AuthToken=0]="AuthToken",e[e.ProjectKey=1]="ProjectKey",e[e.DummyKey=2]="DummyKey"}(e||(e={}));var o={BASE:":protocol://:host/v1/:project_id/:base_name",DRIVE:":protocol://:host/v1/:project_id/:drive_name"};var i,s={base:function(t,r){var n,i,s,u="undefined"!=typeof window&&t===e.DummyKey?window.location.host+"/__space/v0/base":void 0,a="undefined"!=typeof process?null===""?void 0:"".trim():void 0,c=null!==(s=null!==(i=null!==(n=null==(r=(null==r?void 0:r.trim())?r:void 0)?void 0:r.trim())&&void 0!==n?n:u)&&void 0!==i?i:a)&&void 0!==s?s:"database.deta.sh",l=(null==u?void 0:u.startsWith("localhost"))?"http":"https";return o.BASE.replace(":protocol",l).replace(":host",c)},drive:function(t,r){var n,i,s,u="undefined"!=typeof window&&t===e.DummyKey?window.location.host+"/__space/v0/drive":void 0,a="undefined"!=typeof process?null===""?void 0:"".trim():void 0,c=null!==(s=null!==(i=null!==(n=null==(r=(null==r?void 0:r.trim())?r:void 0)?void 0:r.trim())&&void 0!==n?n:u)&&void 0!==i?i:a)&&void 0!==s?s:"drive.deta.sh",l=(null==u?void 0:u.startsWith("localhost"))?"http":"https";return o.DRIVE.replace(":protocol",l).replace(":host",c)}};!function(e){e.Put="PUT",e.Get="GET",e.Post="POST",e.Patch="PATCH",e.Delete="DELETE"}(i||(i={}));var u=function(){function o(t,r,n){this.requestConfig={baseURL:n,headers:r===e.AuthToken?{Authorization:t}:{"X-API-Key":t}}}return o.prototype.put=function(e,s){return r(this,void 0,void 0,(function(){return n(this,(function(r){return[2,o.fetch(e,t(t({},this.requestConfig),{body:s,method:i.Put}))]}))}))},o.prototype.delete=function(e,s){return r(this,void 0,void 0,(function(){return n(this,(function(r){return[2,o.fetch(e,t(t({},this.requestConfig),{body:s,method:i.Delete}))]}))}))},o.prototype.get=function(e,s){return r(this,void 0,void 0,(function(){return n(this,(function(r){return[2,o.fetch(e,t(t({},this.requestConfig),{method:i.Get,blobResponse:null==s?void 0:s.blobResponse}))]}))}))},o.prototype.post=function(e,s){return r(this,void 0,void 0,(function(){return n(this,(function(r){return[2,o.fetch(e,t(t({},this.requestConfig),{body:s.payload,method:i.Post,headers:t(t({},this.requestConfig.headers),s.headers)}))]}))}))},o.prototype.patch=function(e,s){return r(this,void 0,void 0,(function(){return n(this,(function(r){return[2,o.fetch(e,t(t({},this.requestConfig),{body:s,method:i.Patch}))]}))}))},o.fetch=function(e,o){var i,s;return r(this,void 0,void 0,(function(){var r,u,a,c,l,p,d,h;return n(this,(function(n){switch(n.label){case 0:return n.trys.push([0,7,,8]),r=o.body instanceof Uint8Array?o.body:JSON.stringify(o.body),u=(null===(i=null==o?void 0:o.headers)||void 0===i?void 0:i["Content-Type"])||"application/json",a=t(t({},o.headers),{"Content-Type":u}),[4,fetch(""+o.baseURL+e,{body:r,headers:a,method:o.method})];case 1:return(c=n.sent()).ok?[3,3]:[4,c.json()];case 2:return l=n.sent(),p=(null===(s=null==l?void 0:l.errors)||void 0===s?void 0:s[0])||"Something went wrong",[2,{status:c.status,error:new Error(p)}];case 3:return o.blobResponse?[4,c.blob()]:[3,5];case 4:return d=n.sent(),[2,{status:c.status,response:d}];case 5:return[4,c.json()];case 6:return h=n.sent(),[2,{status:c.status,response:h}];case 7:return n.sent(),[2,{status:500,error:new Error("Something went wrong")}];case 8:return[2]}}))}))},o}(),a={PUT_ITEMS:"/items",QUERY_ITEMS:"/query",INSERT_ITEMS:"/items",GET_ITEMS:"/items/:key",PATCH_ITEMS:"/items/:key",DELETE_ITEMS:"/items/:key"},c={GET_FILE:"/files/download?name=:name",DELETE_FILES:"/files",LIST_FILES:"/files?prefix=:prefix&recursive=:recursive&limit=:limit&last=:last",INIT_CHUNK_UPLOAD:"/uploads?name=:name",UPLOAD_FILE_CHUNK:"/uploads/:uid/parts?name=:name&part=:part",COMPLETE_FILE_UPLOAD:"/uploads/:uid?name=:name"};function l(e){return"[object Object]"===Object.prototype.toString.call(e)}var p,d=function(){function e(e){this.date=e||new Date}return e.prototype.addSeconds=function(e){return this.date=new Date(this.date.getTime()+1e3*e),this},e.prototype.getEpochSeconds=function(){return Math.floor(this.date.getTime()/1e3)},e}();function h(e){return"number"==typeof e}!function(e){e.Set="set",e.Trim="trim",e.Increment="increment",e.Append="append",e.Prepend="prepend"}(p||(p={}));var f=function(e,t){this.operation=e,this.value=t};function v(e){return null==e}var y=function(){function e(){}return e.prototype.trim=function(){return new f(p.Trim)},e.prototype.increment=function(e){return void 0===e&&(e=1),new f(p.Increment,e)},e.prototype.append=function(e){return new f(p.Append,Array.isArray(e)?e:[e])},e.prototype.prepend=function(e){return new f(p.Prepend,Array.isArray(e)?e:[e])},e}();function m(e,t){return v(e)&&v(t)?{}:v(e)||v(t)?v(e)?h(t)||t instanceof Date?t instanceof Date?{ttl:new d(t).getEpochSeconds()}:{ttl:t}:{error:new Error("option expireAt should have a value of type number or Date")}:h(e)?{ttl:(new d).addSeconds(e).getEpochSeconds()}:{error:new Error("option expireIn should have a value of type number")}:{error:new Error("can't set both expireIn and expireAt options")}}var w="__expires",E=function(){function e(e,t,r,n,o){var i=s.base(t,o).replace(":base_name",n).replace(":project_id",r);this.requests=new u(e,t,i),this.util=new y}return e.prototype.put=function(e,o,i){var s,u;return r(this,void 0,void 0,(function(){var r,c,p,d,h,f,y,E;return n(this,(function(n){switch(n.label){case 0:if(r=m(null==i?void 0:i.expireIn,null==i?void 0:i.expireAt),c=r.ttl,p=r.error)throw p;return d=[t(t(t({},l(e)?e:{value:e}),o&&{key:o}),!v(c)&&(E={},E[w]=c,E))],[4,this.requests.put(a.PUT_ITEMS,{items:d})];case 1:if(h=n.sent(),f=h.response,y=h.error)throw y;return[2,(null===(u=null===(s=null==f?void 0:f.processed)||void 0===s?void 0:s.items)||void 0===u?void 0:u[0])||null]}}))}))},e.prototype.get=function(e){return r(this,void 0,void 0,(function(){var t,r,o,i,s,u;return n(this,(function(n){switch(n.label){case 0:if(!(t=null==e?void 0:e.trim()))throw new Error("Key is empty");return r=encodeURIComponent(t),[4,this.requests.get(a.GET_ITEMS.replace(":key",r))];case 1:if(o=n.sent(),i=o.status,s=o.response,(u=o.error)&&404!==i)throw u;return 200===i?[2,s]:[2,null]}}))}))},e.prototype.delete=function(e){return r(this,void 0,void 0,(function(){var t,r,o;return n(this,(function(n){switch(n.label){case 0:if(!(t=null==e?void 0:e.trim()))throw new Error("Key is empty");return r=encodeURIComponent(t),[4,this.requests.delete(a.DELETE_ITEMS.replace(":key",r))];case 1:if(o=n.sent().error)throw o;return[2,null]}}))}))},e.prototype.insert=function(e,o,i){return r(this,void 0,void 0,(function(){var r,s,u,c,p,d,h,f,y,E;return n(this,(function(n){switch(n.label){case 0:if(r=m(null==i?void 0:i.expireIn,null==i?void 0:i.expireAt),s=r.ttl,u=r.error)throw u;return c=t(t(t({},l(e)?e:{value:e}),o&&{key:o}),!v(s)&&((E={})[w]=s,E)),[4,this.requests.post(a.INSERT_ITEMS,{payload:{item:c}})];case 1:if(p=n.sent(),d=p.status,h=p.response,(f=p.error)&&409===d)throw y=o||c.key,new Error("Item with key "+y+" already exists");if(f)throw f;return[2,h]}}))}))},e.prototype.putMany=function(e,o){return r(this,void 0,void 0,(function(){var r,i,s,u,c,p,d;return n(this,(function(n){switch(n.label){case 0:if(!(e instanceof Array))throw new Error("Items must be an array");if(!e.length)throw new Error("Items can't be empty");if(e.length>25)throw new Error("We can't put more than 25 items at a time");if(r=m(null==o?void 0:o.expireIn,null==o?void 0:o.expireAt),i=r.ttl,s=r.error)throw s;return u=e.map((function(e){var r,n=l(e)?e:{value:e};return t(t({},n),!v(i)&&((r={})[w]=i,r))})),[4,this.requests.put(a.PUT_ITEMS,{items:u})];case 1:if(c=n.sent(),p=c.response,d=c.error)throw d;return[2,p]}}))}))},e.prototype.update=function(e,o,i){return r(this,void 0,void 0,(function(){var r,s,u,c,l,d,h,y;return n(this,(function(n){switch(n.label){case 0:if(!(r=null==o?void 0:o.trim()))throw new Error("Key is empty");if(s=m(null==i?void 0:i.expireIn,null==i?void 0:i.expireAt),u=s.ttl,c=s.error)throw c;return l={set:t({},!v(u)&&(y={},y[w]=u,y)),increment:{},append:{},prepend:{},delete:[]},Object.entries(e).forEach((function(e){var t=e[0],r=e[1],n=r instanceof f?r:new f(p.Set,r),o=n.operation,i=n.value;switch(o){case p.Trim:l.delete.push(t);break;default:l[o][t]=i}})),d=encodeURIComponent(r),[4,this.requests.patch(a.PATCH_ITEMS.replace(":key",d),l)];case 1:if(h=n.sent().error)throw h;return[2,null]}}))}))},e.prototype.fetch=function(e,t){return void 0===e&&(e=[]),r(this,void 0,void 0,(function(){var r,o,i,s,u,c,l,p,d,h,f,v,y,m,w;return n(this,(function(n){switch(n.label){case 0:return o=(r=t||{}).limit,i=void 0===o?1e3:o,s=r.last,u=void 0===s?"":s,c=r.desc,l=void 0!==c&&c?"desc":"",p={query:Array.isArray(e)?e:[e],limit:i,last:u,sort:l},[4,this.requests.post(a.QUERY_ITEMS,{payload:p})];case 1:if(d=n.sent(),h=d.response,f=d.error)throw f;return v=h.items,y=h.paging,m=y.size,w=y.last,[2,{items:v,count:m,last:w}]}}))}))},e}();function b(){return"undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node}var T=function(){function e(e,t,r,n,o){var i=s.drive(t,o).replace(":drive_name",n).replace(":project_id",r);this.requests=new u(e,t,i)}return e.prototype.get=function(e){return r(this,void 0,void 0,(function(){var t,r,o,i,s,u;return n(this,(function(n){switch(n.label){case 0:if(!(t=null==e?void 0:e.trim()))throw new Error("Name is empty");return r=encodeURIComponent(t),[4,this.requests.get(c.GET_FILE.replace(":name",r),{blobResponse:!0})];case 1:if(o=n.sent(),i=o.status,s=o.response,u=o.error,404===i&&u)return[2,null];if(u)throw u;return[2,s]}}))}))},e.prototype.delete=function(e){var t;return r(this,void 0,void 0,(function(){var r,o,i,s;return n(this,(function(n){switch(n.label){case 0:if(!(null==e?void 0:e.trim()))throw new Error("Name is empty");return r={names:[e]},[4,this.requests.delete(c.DELETE_FILES,r)];case 1:if(o=n.sent(),i=o.response,s=o.error)throw s;return[2,(null===(t=null==i?void 0:i.deleted)||void 0===t?void 0:t[0])||e]}}))}))},e.prototype.deleteMany=function(e){return r(this,void 0,void 0,(function(){var t,r,o,i,s;return n(this,(function(n){switch(n.label){case 0:if(!e.length)throw new Error("Names can't be empty");if(e.length>1e3)throw new Error("We can't delete more than 1000 items at a time");return t={names:e},[4,this.requests.delete(c.DELETE_FILES,t)];case 1:if(r=n.sent(),o=r.status,i=r.response,s=r.error,400===o&&s)throw new Error("Names can't be empty");if(s)throw s;return[2,i]}}))}))},e.prototype.list=function(e){return r(this,void 0,void 0,(function(){var t,r,o,i,s,u,a,l,p,d,h,f;return n(this,(function(n){switch(n.label){case 0:return r=(t=e||{}).recursive,o=void 0===r||r,i=t.prefix,s=void 0===i?"":i,u=t.limit,a=void 0===u?1e3:u,l=t.last,p=void 0===l?"":l,[4,this.requests.get(c.LIST_FILES.replace(":prefix",s).replace(":recursive",o.toString()).replace(":limit",a.toString()).replace(":last",p))];case 1:if(d=n.sent(),h=d.response,f=d.error)throw f;return[2,h]}}))}))},e.prototype.put=function(e,t){return r(this,void 0,void 0,(function(){var r,o,i,s,u,a,c;return n(this,(function(n){switch(n.label){case 0:if(!(r=null==e?void 0:e.trim()))throw new Error("Name is empty");if(o=encodeURIComponent(r),t.path&&t.data)throw new Error("Please only provide data or a path. Not both");if(!t.path&&!t.data)throw new Error("Please provide data or a path. Both are empty");if(t.path&&!b())throw new Error("Can't use path in browser environment");return i=new Uint8Array,t.path?[4,require("fs").promises.readFile(t.path)]:[3,2];case 1:s=n.sent(),i=new Uint8Array(s),n.label=2;case 2:if(t.data)if(b()&&t.data instanceof Buffer)i=function(e){for(var t=new Uint8Array(e.length),r=0;r<e.length;r+=1)t[r]=e[r];return t}(t.data);else if("string"==typeof(l=t.data)||l instanceof String)i=function(e){for(var t=new Uint8Array(e.length),r=0;r<e.length;r+=1)t[r]=e.charCodeAt(r);return t}(t.data);else{if(!(t.data instanceof Uint8Array))throw new Error("Unsupported data format, expected data to be one of: string | Uint8Array | Buffer");i=t.data}return[4,this.upload(o,i,t.contentType||"binary/octet-stream")];case 3:if(u=n.sent(),a=u.response,c=u.error)throw c;return[2,a]}var l}))}))},e.prototype.upload=function(e,t,o){return r(this,void 0,void 0,(function(){var r,i,s,u,a,l,p,d,h,f,v,y,m,w;return n(this,(function(n){switch(n.label){case 0:return r=t.byteLength,i=10485760,[4,this.requests.post(c.INIT_CHUNK_UPLOAD.replace(":name",e),{headers:{"Content-Type":o}})];case 1:if(s=n.sent(),u=s.response,a=s.error)return[2,{error:a}];l=u.upload_id,p=u.name,d=1,h=0,n.label=2;case 2:return h<r?(f=h,v=Math.min(h+i,r),y=t.slice(f,v),[4,this.requests.post(c.UPLOAD_FILE_CHUNK.replace(":uid",l).replace(":name",e).replace(":part",d.toString()),{payload:y,headers:{"Content-Type":o}})]):[3,5];case 3:if(m=n.sent().error)return[2,{error:m}];d+=1,n.label=4;case 4:return h+=i,[3,2];case 5:return[4,this.requests.patch(c.COMPLETE_FILE_UPLOAD.replace(":uid",l).replace(":name",e))];case 6:return(w=n.sent().error)?[2,{error:w}]:[2,{response:p}]}}))}))},e}(),I=function(){function e(e,t,r){this.key=e,this.type=t,this.projectId=r}return e.prototype.Base=function(e,t){var r=null==e?void 0:e.trim();if(!r)throw new Error("Base name is not defined");return new E(this.key,this.type,this.projectId,r,t)},e.prototype.Drive=function(e,t){var r=null==e?void 0:e.trim();if(!r)throw new Error("Drive name is not defined");return new T(this.key,this.type,this.projectId,r,t)},e}();function _(t,r){var n=null==r?void 0:r.trim(),o=null==t?void 0:t.trim();if(n&&o)return new I(n,e.AuthToken,o);var i=o||("undefined"!=typeof process?null===""?void 0:"".trim():void 0);if(i)return new I(i,e.ProjectKey,i.split("_")[0]);if("undefined"!=typeof window)return new I("dummy",e.DummyKey,"dummy");throw new Error("Project key is not defined")}function g(e,t){return _().Base(e,t)}function S(e,t){return _().Drive(e,t)}export{g as Base,_ as Deta,S as Drive};