UNPKG

@directus/sdk-js

Version:

The official Directus SDK for use in JavaScript!

17 lines (14 loc) 17.9 kB
var DirectusSDK=function(t){"use strict";function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=e(t),r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)}; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var s=function(){return(s=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function o(t,e,n,r){return new(n||(n=Promise))((function(i,s){function o(t){try{a(r.next(t))}catch(t){s(t)}}function u(t){try{a(r.throw(t))}catch(t){s(t)}}function a(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,u)}a((r=r.apply(t,e||[])).next())}))}function u(t,e){var n,r,i,s,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return s={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function u(s){return function(u){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;o;)try{if(n=1,r&&(i=2&s[0]?r.return:s[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,s[1])).done)return i;switch(r=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return o.label++,{value:s[1],done:!1};case 5:o.label++,r=s[1],s=[0];continue;case 7:s=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){o=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){o.label=s[1];break}if(6===s[0]&&o.label<i[1]){o.label=i[1],i=s;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(s);break}i[2]&&o.ops.pop(),o.trys.pop();continue}s=e.call(t,o)}catch(t){s=[6,t],r=0}finally{n=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,u])}}}var a=function(){function t(t,e){this.axios=e,this.endpoint=t.startsWith("directus_")?"/"+t.substring(9)+"/":"/items/"+t+"/"}return t.prototype.create=function(t,e){return o(this,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return[4,this.axios.post(this.endpoint,t,{params:e})];case 1:return[2,n.sent().data]}}))}))},t.prototype.read=function(t,e){return o(this,void 0,void 0,(function(){var n,r,i;return u(this,(function(s){switch(s.label){case 0:return n=null,t&&(Array.isArray(t)||"string"==typeof t||"number"==typeof t)&&(n=t),r={},e?r=e:e||"object"!=typeof t||!1!==Array.isArray(t)||(r=t),i=this.endpoint,n&&(i+=n),[4,this.axios.get(i,{params:r})];case 1:return[2,s.sent().data]}}))}))},t.prototype.update=function(t,e,n){return o(this,void 0,void 0,(function(){var r,i;return u(this,(function(s){switch(s.label){case 0:return"string"==typeof t||"number"==typeof t||Array.isArray(t)&&t.every((function(t){return["string","number"].includes(typeof t)}))?(r=t,i=e,[4,this.axios.patch(""+this.endpoint+r,i,{params:n})]):[3,2];case 1:return[2,s.sent().data];case 2:return[4,this.axios.patch(""+this.endpoint,t,{params:e})];case 3:return[2,s.sent().data]}}))}))},t.prototype.delete=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.delete(""+this.endpoint+t)];case 1:return e.sent(),[2]}}))}))},t}(),c=function(){function t(t){var e=this;this.specs={oas:function(){return o(e,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.axios.get("/server/specs/oas")];case 1:return[2,t.sent().data]}}))}))}},this.axios=t}return t.prototype.ping=function(){return o(this,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.axios.get("/server/ping")];case 1:return t.sent(),[2,"pong"]}}))}))},t.prototype.info=function(){return o(this,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.axios.get("/server/info")];case 1:return[2,t.sent().data]}}))}))},t}(),h=function(){function t(t){var e=this;this.random={string:function(t){return void 0===t&&(t=32),o(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.get("/utils/random/string",{params:{length:t}})];case 1:return[2,e.sent().data]}}))}))}},this.hash={generate:function(t){return o(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/utils/hash/generate",{string:t})];case 1:return[2,e.sent().data]}}))}))},verify:function(t,n){return o(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/utils/hash/verify",{string:t,hash:n})];case 1:return[2,e.sent().data]}}))}))}},this.axios=t}return t.prototype.sort=function(t,e,n){return o(this,void 0,void 0,(function(){return u(this,(function(r){switch(r.label){case 0:return[4,this.axios.post("/utils/sort/"+t,{item:e,to:n})];case 1:return r.sent(),[2]}}))}))},t.prototype.revert=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/utils/revert/"+t)];case 1:return e.sent(),[2]}}))}))},t}(),f=function(){function t(t){var e=this;this.comments={create:function(t){return o(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/activity/comments",t)];case 1:return[2,e.sent().data]}}))}))},update:function(t,n){return o(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.patch("/activity/comments/"+t,n)];case 1:return[2,e.sent().data]}}))}))},delete:function(t){return o(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.delete("/activity/comments/"+t)];case 1:return e.sent(),[2]}}))}))}},this.axios=t,this.itemsHandler=new a("directus_activity",t)}return t.prototype.read=function(t,e){return o(this,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return[4,this.itemsHandler.read(t,e)];case 1:return[2,n.sent()]}}))}))},t}(),l=function(t){function e(e){return t.call(this,"directus_folders",e)||this}return i(e,t),e}(a),d=function(t){function e(e){return t.call(this,"directus_permissions",e)||this}return i(e,t),e}(a),p=function(t){function e(e){return t.call(this,"directus_presets",e)||this}return i(e,t),e}(a),v=function(t){function e(e){return t.call(this,"directus_relations",e)||this}return i(e,t),e}(a),b=function(t){function e(e){return t.call(this,"directus_revisions",e)||this}return i(e,t),e}(a),m=function(t){function e(e){return t.call(this,"directus_roles",e)||this}return i(e,t),e}(a),y=function(t){function e(e){var n=t.call(this,"directus_users",e)||this;return n.tfa={enable:function(t){return o(n,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/users/tfa/enable",{password:t})];case 1:return e.sent(),[2]}}))}))},disable:function(t){return o(n,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/users/tfa/disable",{otp:t})];case 1:return e.sent(),[2]}}))}))}},n.me={read:function(t){return o(n,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.get("/users/me",{params:t})];case 1:return[2,e.sent().data]}}))}))},update:function(t,e){return o(n,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return[4,this.axios.patch("/users/me",t,{params:e})];case 1:return[2,n.sent().data]}}))}))}},n}return i(e,t),e.prototype.invite=function(t,e){return o(this,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return[4,this.axios.post("/users/invite",{email:t,role:e})];case 1:return n.sent(),[2]}}))}))},e.prototype.acceptInvite=function(t,e){return o(this,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return[4,this.axios.post("/users/invite/accept",{token:t,password:e})];case 1:return n.sent(),[2]}}))}))},e}(a),g=function(t){function e(e){return t.call(this,"directus_settings",e)||this}return i(e,t),e}(a),w=function(t){function e(e){return t.call(this,"directus_files",e)||this}return i(e,t),e}(a),x=function(t){function e(e){return t.call(this,"directus_collections",e)||this}return i(e,t),e}(a),_=function(t){function e(e){return t.call(this,"directus_fields",e)||this}return i(e,t),e}(a),k=function(){function t(t,e){var n=this;this.autoRefreshTimeout=null,this.accessTokenInitId=null,this.password={request:function(t){return o(n,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.axios.post("/auth/password/request",{email:t})];case 1:return e.sent(),[2]}}))}))},reset:function(t,e){return o(n,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return[4,this.axios.post("/auth/password/reset",{token:t,password:e})];case 1:return n.sent(),[2]}}))}))}},this.axios=t,this.storage=e.storage,this.mode=e.mode,this.autoRefresh=e.autoRefresh,this.accessTokenInitId=this.axios.interceptors.request.use((function(t){return n.initializeAccessToken(t)})),this.autoRefresh&&this.refresh(!0)}return Object.defineProperty(t.prototype,"token",{get:function(){var t,e;return(null===(e=null===(t=this.axios.defaults.headers)||void 0===t?void 0:t.Authorization)||void 0===e?void 0:e.split(" ")[1])||null},set:function(t){var e;null===t?null===(e=this.axios.defaults.headers)||void 0===e||delete e.Authorization:this.axios.defaults.headers=s(s({},this.axios.defaults.headers||{}),{Authorization:"Bearer "+t})},enumerable:!1,configurable:!0}),t.prototype.login=function(t){return o(this,void 0,void 0,(function(){var e,n;return u(this,(function(r){switch(r.label){case 0:return this.removeTimeout(),[4,this.axios.post("/auth/login",s(s({},t),{mode:this.mode}))];case 1:return e=r.sent(),n=e.data.data,this.token=n.access_token,this.expiresAt=Date.now()+n.expires,[4,this.storage.setItem("directus_access_token",this.token)];case 2:return r.sent(),[4,this.storage.setItem("directus_access_token_expires",this.expiresAt)];case 3:return r.sent(),"json"!==this.mode?[3,5]:[4,this.storage.setItem("directus_refresh_token",n.refresh_token)];case 4:r.sent(),r.label=5;case 5:return this.autoRefresh&&this.refresh(!0),[2,e.data]}}))}))},t.prototype.refresh=function(t){return o(this,void 0,void 0,(function(){var e,n,r,i,s,a=this;return u(this,(function(c){switch(c.label){case 0:return this.removeTimeout(),e=this,[4,this.storage.getItem("directus_access_token_expires")];case 1:return e.expiresAt=c.sent(),this.expiresAt?Date.now()+1e4<this.expiresAt&&this.autoRefresh&&(this.autoRefreshTimeout=setTimeout((function(){return a.refresh(!1)}),this.expiresAt-Date.now()-1e4),!t)?[2]:(n={mode:this.mode},"json"!==this.mode?[3,3]:[4,this.storage.getItem("directus_refresh_token")]):[2];case 2:r=c.sent(),n.refresh_token=r,c.label=3;case 3:return this.expiresAt<Date.now()+1e3&&(this.token=null),[4,this.axios.post("/auth/refresh",n).catch((function(t){return o(a,void 0,void 0,(function(){var e;return u(this,(function(n){switch(n.label){case 0:return 401!==(null===(e=t.response)||void 0===e?void 0:e.status)?[3,5]:[4,this.storage.removeItem("directus_access_token")];case 1:return n.sent(),[4,this.storage.removeItem("directus_access_token_expires")];case 2:return n.sent(),"json"!==this.mode?[3,4]:[4,this.storage.removeItem("directus_refresh_token")];case 3:n.sent(),n.label=4;case 4:this.token=null,n.label=5;case 5:throw Promise.reject(t)}}))}))}))];case 4:return i=c.sent(),s=i.data.data,this.token=s.access_token,this.expiresAt=Date.now()+s.expires,[4,this.storage.setItem("directus_access_token",this.token)];case 5:return c.sent(),[4,this.storage.setItem("directus_access_token_expires",this.expiresAt)];case 6:return c.sent(),"json"!==this.mode?[3,8]:[4,this.storage.setItem("directus_refresh_token",i.data.data.refresh_token)];case 7:c.sent(),c.label=8;case 8:return this.autoRefresh&&(this.autoRefreshTimeout=setTimeout((function(){return a.refresh(!1)}),s.expires-1e4)),[2,i.data]}}))}))},t.prototype.logout=function(){return o(this,void 0,void 0,(function(){var t,e;return u(this,(function(n){switch(n.label){case 0:return this.removeTimeout(),t={},"json"!==this.mode?[3,2]:(e=t,[4,this.storage.getItem("directus_refresh_token")]);case 1:e.refresh_token=n.sent(),n.label=2;case 2:return[4,this.axios.post("/auth/logout",t)];case 3:return n.sent(),[4,this.storage.removeItem("directus_access_token")];case 4:return n.sent(),[4,this.storage.removeItem("directus_access_token_expires")];case 5:return n.sent(),"json"!==this.mode?[3,7]:[4,this.storage.removeItem("directus_refresh_token")];case 6:n.sent(),n.label=7;case 7:return this.token=null,[2]}}))}))},t.prototype.initializeAccessToken=function(t){return o(this,void 0,void 0,(function(){var e;return u(this,(function(n){switch(n.label){case 0:return null===this.accessTokenInitId?[3,2]:[4,this.storage.getItem("directus_access_token")];case 1:(e=n.sent())&&(this.token=e,t.headers.Authorization="Bearer "+e),this.axios.interceptors.request.eject(this.accessTokenInitId),this.accessTokenInitId=null,n.label=2;case 2:return[2,t]}}))}))},t.prototype.removeTimeout=function(){null!==this.autoRefreshTimeout&&(clearTimeout(this.autoRefreshTimeout),this.autoRefreshTimeout=null)},t}(),I=function(){function t(){}return t.prototype.getItem=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){return[2,window.localStorage.getItem(t)]}))}))},t.prototype.setItem=function(t,e){return o(this,void 0,void 0,(function(){return u(this,(function(n){return window.localStorage.setItem(t,e),[2]}))}))},t.prototype.removeItem=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){return window.localStorage.removeItem(t),[2]}))}))},t}(),j=function(){function t(){this.values={}}return t.prototype.getItem=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){return[2,this.values[t]]}))}))},t.prototype.setItem=function(t,e){return o(this,void 0,void 0,(function(){return u(this,(function(n){return this.values[t]=e,[2]}))}))},t.prototype.removeItem=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){return delete this.values[t],[2]}))}))},t}();return function(){function t(t,e){var r,i,s,o,u,a;this.axios=n.default.create({baseURL:t,withCredentials:!0}),this.authOptions={storage:null!==(i=null===(r=null==e?void 0:e.auth)||void 0===r?void 0:r.storage)&&void 0!==i?i:"undefined"==typeof window?new j:new I,mode:null!==(o=null===(s=null==e?void 0:e.auth)||void 0===s?void 0:s.mode)&&void 0!==o?o:"cookie",autoRefresh:null!==(a=null===(u=null==e?void 0:e.auth)||void 0===u?void 0:u.autoRefresh)&&void 0!==a&&a},this.auth=new k(this.axios,this.authOptions)}return Object.defineProperty(t.prototype,"url",{get:function(){return this.axios.defaults.baseURL},set:function(t){this.axios.defaults.baseURL=t},enumerable:!1,configurable:!0}),t.prototype.items=function(t){if(t.startsWith("directus_"))throw new Error("You can't read the \""+t+'" collection directly.');return new a(t,this.axios)},Object.defineProperty(t.prototype,"activity",{get:function(){return new f(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"collections",{get:function(){return new x(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"fields",{get:function(){return new _(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"files",{get:function(){return new w(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"folders",{get:function(){return new l(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"permissions",{get:function(){return new d(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"presets",{get:function(){return new p(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"relations",{get:function(){return new v(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"revisions",{get:function(){return new b(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"roles",{get:function(){return new m(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"server",{get:function(){return new c(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"settings",{get:function(){return new g(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"users",{get:function(){return new y(this.axios)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"utils",{get:function(){return new h(this.axios)},enumerable:!1,configurable:!0}),t}()}(axios); //# sourceMappingURL=sdk-js.global.min.js.map