microcms-js-sdk
Version:
JavaScript SDK Client for microCMS.
1 lines • 19.4 kB
JavaScript
"use strict";function t(t,e){if(e==null||e>t.length)e=t.length;for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function e(t){if(Array.isArray(t))return t}function r(e){if(Array.isArray(e))return t(e)}function n(t,e,r,n,i,o,u){try{var a=t[o](u);var s=a.value}catch(t){r(t);return}if(a.done){e(s)}else{Promise.resolve(s).then(n,i)}}function i(t){return function(){var e=this,r=arguments;return new Promise(function(i,o){var u=t.apply(e,r);function a(t){n(u,i,o,a,s,"next",t)}function s(t){n(u,i,o,a,s,"throw",t)}a(undefined)})}}function o(t,e,r){if(e in t){Object.defineProperty(t,e,{value:r,enumerable:true,configurable:true,writable:true})}else{t[e]=r}return t}function u(t,e){if(e!=null&&typeof Symbol!=="undefined"&&e[Symbol.hasInstance]){return!!e[Symbol.hasInstance](t)}else{return t instanceof e}}function a(t){if(typeof Symbol!=="undefined"&&t[Symbol.iterator]!=null||t["@@iterator"]!=null)return Array.from(t)}function s(t,e){var r=t==null?null:typeof Symbol!=="undefined"&&t[Symbol.iterator]||t["@@iterator"];if(r==null)return;var n=[];var i=true;var o=false;var u,a;try{for(r=r.call(t);!(i=(u=r.next()).done);i=true){n.push(u.value);if(e&&n.length===e)break}}catch(t){o=true;a=t}finally{try{if(!i&&r["return"]!=null)r["return"]()}finally{if(o)throw a}}return n}function c(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function l(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(t){for(var e=1;e<arguments.length;e++){var r=arguments[e]!=null?arguments[e]:{};var n=Object.keys(r);if(typeof Object.getOwnPropertySymbols==="function"){n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))}n.forEach(function(e){o(t,e,r[e])})}return t}function p(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);if(e){n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})}r.push.apply(r,n)}return r}function h(t,e){e=e!=null?e:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(t,Object.getOwnPropertyDescriptors(e))}else{p(Object(e)).forEach(function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))})}return t}function m(t,r){return e(t)||s(t,r)||v(t,r)||c()}function d(t){return r(t)||a(t)||v(t)||l()}function v(e,r){if(!e)return;if(typeof e==="string")return t(e,r);var n=Object.prototype.toString.call(e).slice(8,-1);if(n==="Object"&&e.constructor)n=e.constructor.name;if(n==="Map"||n==="Set")return Array.from(n);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return t(e,r)}function y(t,e){var r,n,i,o,u={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(o[Symbol.iterator]=function(){return this}),o;function a(t){return function(e){return s([t,e])}}function s(o){if(r)throw new TypeError("Generator is already executing.");while(u)try{if(r=1,n&&(i=o[0]&2?n["return"]:o[0]?n["throw"]||((i=n["return"])&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;if(n=0,i)o=[o[0]&2,i.value];switch(o[0]){case 0:case 1:i=o;break;case 4:u.label++;return{value:o[1],done:false};case 5:u.label++;n=o[1];o=[0];continue;case 7:o=u.ops.pop();u.trys.pop();continue;default:if(!(i=u.trys,i=i.length>0&&i[i.length-1])&&(o[0]===6||o[0]===2)){u=0;continue}if(o[0]===3&&(!i||o[1]>i[0]&&o[1]<i[3])){u.label=o[1];break}if(o[0]===6&&u.label<i[1]){u.label=i[1];i=o;break}if(i&&u.label<i[2]){u.label=i[2];u.ops.push(o);break}if(i[2])u.ops.pop();u.trys.pop();continue}o=e.call(t,u)}catch(t){o=[6,t];n=0}finally{r=i=0}if(o[0]&5)throw o[1];return{value:o[0]?o[1]:void 0,done:true}}}var microcms=function(){var t=Object.create;var e=Object.defineProperty;var r=Object.getOwnPropertyDescriptor;var n=Object.getOwnPropertyNames;var o=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty;var s=function(t,e){return function(){return e||t((e={exports:{}}).exports,e),e.exports}},c=function(t,r){for(var n in r)e(t,n,{get:r[n],enumerable:!0})},l=function(t,i,o,u){var s=true,c=false,l=undefined;if(i&&typeof i=="object"||typeof i=="function")try{var f=function(){var n=h.value;!a.call(t,n)&&n!==o&&e(t,n,{get:function(){return i[n]},enumerable:!(u=r(i,n))||u.enumerable})};for(var p=n(i)[Symbol.iterator](),h;!(s=(h=p.next()).done);s=true)f()}catch(t){c=true;l=t}finally{try{if(!s&&p.return!=null){p.return()}}finally{if(c){throw l}}}return t};var p=function(r,n,i){return i=r!=null?t(o(r)):{},l(n||!r||!r.__esModule?e(i,"default",{value:r,enumerable:!0}):i,r)},v=function(t){return l(e({},"__esModule",{value:!0}),t)};var b=s(function(t,e){"use strict";var r=function t(t,e){typeof e=="boolean"&&(e={forever:e}),this._originalTimeouts=JSON.parse(JSON.stringify(t)),this._timeouts=t,this._options=e||{},this._maxRetryTime=e&&e.maxRetryTime||1/0,this._fn=null,this._errors=[],this._attempts=1,this._operationTimeout=null,this._operationTimeoutCb=null,this._timeout=null,this._operationStart=null,this._timer=null,this._options.forever&&(this._cachedTimeouts=this._timeouts.slice(0))};e.exports=r;r.prototype.reset=function(){this._attempts=1,this._timeouts=this._originalTimeouts.slice(0)};r.prototype.stop=function(){this._timeout&&clearTimeout(this._timeout),this._timer&&clearTimeout(this._timer),this._timeouts=[],this._cachedTimeouts=null};r.prototype.retry=function(t){if(this._timeout&&clearTimeout(this._timeout),!t)return!1;var e=new Date().getTime();if(t&&e-this._operationStart>=this._maxRetryTime)return this._errors.push(t),this._errors.unshift(new Error("RetryOperation timeout occurred")),!1;this._errors.push(t);var r=this._timeouts.shift();if(r===void 0)if(this._cachedTimeouts)this._errors.splice(0,this._errors.length-1),r=this._cachedTimeouts.slice(-1);else return!1;var n=this;return this._timer=setTimeout(function(){n._attempts++,n._operationTimeoutCb&&(n._timeout=setTimeout(function(){n._operationTimeoutCb(n._attempts)},n._operationTimeout),n._options.unref&&n._timeout.unref()),n._fn(n._attempts)},r),this._options.unref&&this._timer.unref(),!0};r.prototype.attempt=function(t,e){this._fn=t,e&&(e.timeout&&(this._operationTimeout=e.timeout),e.cb&&(this._operationTimeoutCb=e.cb));var r=this;this._operationTimeoutCb&&(this._timeout=setTimeout(function(){r._operationTimeoutCb()},r._operationTimeout)),this._operationStart=new Date().getTime(),this._fn(this._attempts)};r.prototype.try=function(t){console.log("Using RetryOperation.try() is deprecated"),this.attempt(t)};r.prototype.start=function(t){console.log("Using RetryOperation.start() is deprecated"),this.attempt(t)};r.prototype.start=r.prototype.try;r.prototype.errors=function(){return this._errors};r.prototype.attempts=function(){return this._attempts};r.prototype.mainError=function(){if(this._errors.length===0)return null;for(var t={},e=null,r=0,n=0;n<this._errors.length;n++){var i=this._errors[n],o=i.message,u=(t[o]||0)+1;t[o]=u,u>=r&&(e=i,r=u)}return e}});var w=s(function(t){"use strict";var e=b();t.operation=function(r){var n=t.timeouts(r);return new e(n,{forever:r&&(r.forever||r.retries===1/0),unref:r&&r.unref,maxRetryTime:r&&r.maxRetryTime})};t.timeouts=function(t){if(u(t,Array))return[].concat(t);var e={retries:10,factor:2,minTimeout:1*1e3,maxTimeout:1/0,randomize:!1};for(var r in t)e[r]=t[r];if(e.minTimeout>e.maxTimeout)throw new Error("minTimeout is greater than maxTimeout");for(var n=[],i=0;i<e.retries;i++)n.push(this.createTimeout(i,e));return t&&t.forever&&!n.length&&n.push(this.createTimeout(i,e)),n.sort(function(t,e){return t-e}),n};t.createTimeout=function(t,e){var r=e.randomize?Math.random()+1:1,n=Math.round(r*Math.max(e.minTimeout,1)*Math.pow(e.factor,t));return n=Math.min(n,e.maxTimeout),n};t.wrap=function(e,r,n){if(u(r,Array)&&(n=r,r=null),!n){n=[];for(var i in e)typeof e[i]=="function"&&n.push(i)}for(var o=0;o<n.length;o++){var a=n[o],s=e[a];e[a]=(function(n){var i=t.operation(r),o=Array.prototype.slice.call(arguments,1),u=o.pop();o.push(function(t){i.retry(t)||(t&&(arguments[0]=i.mainError()),u.apply(this,arguments))}),i.attempt(function(){n.apply(e,o)})}).bind(e,s),e[a].options=r}}});var g=s(function(t,e){"use strict";e.exports=w()});var _=s(function(t,e){"use strict";var r=function t(t,e){function r(r,i){var o=e||{},u;"randomize"in o||(o.randomize=!0),u=n.operation(o);function a(t){i(t||new Error("Aborted"))}function s(t,e){if(t.bail){a(t);return}u.retry(t)?o.onRetry&&o.onRetry(t,e):i(u.mainError())}function c(e){var n;try{n=t(a,e)}catch(t){s(t,e);return}Promise.resolve(n).then(r).catch(function(t){s(t,e)})}u.attempt(c)}return new Promise(r)};var n=g();e.exports=r});var T={};c(T,{createClient:function(){return x},createManagementClient:function(){return A}});var q=p(_());var I=function(t){return function(){var e=i(function(e,r){var n;return y(this,function(i){n=new Headers(r===null||r===void 0?void 0:r.headers);return[2,(n.has("X-MICROCMS-API-KEY")||n.set("X-MICROCMS-API-KEY",t),fetch(e,h(f({},r),{headers:n})))]})});return function(t,r){return e.apply(this,arguments)}}()};var O="microcms.io",j="microcms-management.io",E="v1";var P=function(t){return t!==null&&typeof t=="object"},S=function(t){return typeof t=="string"};var R=function(t){if(!P(t))throw new Error("queries is not object");return new URLSearchParams(Object.entries(t).reduce(function(t,e){var r=m(e,2),n=r[0],i=r[1];return i!==void 0&&(t[n]=String(i)),t},{})).toString()};var x=function(t){var e=t.serviceDomain,r=t.apiKey,n=t.retry;if(!e||!r)throw new Error("parameter is required (check serviceDomain and apiKey)");if(!S(e)||!S(r))throw new Error("parameter is not string");var o="https://".concat(e,".").concat(O,"/api/").concat(E),u=function(){var t=i(function(t){var e,u,a,s,c,l,p,m,d;return y(this,function(v){switch(v.label){case 0:e=t.endpoint,u=t.contentId,a=t.queries,s=a===void 0?{}:a,c=t.requestInit;l=I(r),p=R(s),m="".concat(o,"/").concat(e).concat(u?"/".concat(u):"").concat(p?"?".concat(p):""),d=function(){var t=i(function(t){var e,r,n;return y(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);return[4,t.json()];case 1:e=i.sent(),r=e.message;return[2,r!==null&&r!==void 0?r:null];case 2:n=i.sent();return[2,null];case 3:return[2]}})});return function e(e){return t.apply(this,arguments)}}();return[4,(0,q.default)(function(){var t=i(function(t){var e,r,n,i,o,u,a;return y(this,function(s){switch(s.label){case 0:s.trys.push([0,6,,7]);return[4,l(m,h(f({},c),{method:(r=c===null||c===void 0?void 0:c.method)!==null&&r!==void 0?r:"GET"}))];case 1:if(!(e=s.sent(),e.status!==429&&e.status>=400&&e.status<500))return[3,3];return[4,d(e)];case 2:n=s.sent();return[2,t(new Error("fetch API response status: ".concat(e.status).concat(n?"\n message is `".concat(n,"`"):"")))];case 3:if(!!e.ok)return[3,5];return[4,d(e)];case 4:i=s.sent();return[2,Promise.reject(new Error("fetch API response status: ".concat(e.status).concat(i?"\n message is `".concat(i,"`"):"")))];case 5:return[2,(c===null||c===void 0?void 0:c.method)==="DELETE"?void 0:e.json()];case 6:o=s.sent();if(o.data)throw o.data;if((u=o.response)===null||u===void 0?void 0:u.data)throw o.response.data;return[2,Promise.reject(new Error("Network Error.\n Details: ".concat((a=o.message)!==null&&a!==void 0?a:"")))];case 7:return[2]}})});return function(e){return t.apply(this,arguments)}}(),{retries:n?2:0,onRetry:function(t,e){console.log(t),console.log("Waiting for retry (".concat(e,"/",2,")"))},minTimeout:5e3})];case 1:return[2,v.sent()]}})});return function e(e){return t.apply(this,arguments)}}();return{get:function(){var t=i(function(t){var e,r,n,i,o,a;return y(this,function(s){switch(s.label){case 0:e=t.endpoint,r=t.contentId,n=t.queries,i=n===void 0?{}:n,o=t.customRequestInit;if(!e)return[3,2];return[4,u({endpoint:e,contentId:r,queries:i,requestInit:o})];case 1:a=s.sent();return[3,3];case 2:a=Promise.reject(new Error("endpoint is required"));s.label=3;case 3:return[2,a]}})});return function(e){return t.apply(this,arguments)}}(),getList:function(){var t=i(function(t){var e,r,n,i,o;return y(this,function(a){switch(a.label){case 0:e=t.endpoint,r=t.queries,n=r===void 0?{}:r,i=t.customRequestInit;if(!e)return[3,2];return[4,u({endpoint:e,queries:n,requestInit:i})];case 1:o=a.sent();return[3,3];case 2:o=Promise.reject(new Error("endpoint is required"));a.label=3;case 3:return[2,o]}})});return function(e){return t.apply(this,arguments)}}(),getListDetail:function(){var t=i(function(t){var e,r,n,i,o,a;return y(this,function(s){switch(s.label){case 0:e=t.endpoint,r=t.contentId,n=t.queries,i=n===void 0?{}:n,o=t.customRequestInit;if(!e)return[3,2];return[4,u({endpoint:e,contentId:r,queries:i,requestInit:o})];case 1:a=s.sent();return[3,3];case 2:a=Promise.reject(new Error("endpoint is required"));s.label=3;case 3:return[2,a]}})});return function(e){return t.apply(this,arguments)}}(),getObject:function(){var t=i(function(t){var e,r,n,i,o;return y(this,function(a){switch(a.label){case 0:e=t.endpoint,r=t.queries,n=r===void 0?{}:r,i=t.customRequestInit;if(!e)return[3,2];return[4,u({endpoint:e,queries:n,requestInit:i})];case 1:o=a.sent();return[3,3];case 2:o=Promise.reject(new Error("endpoint is required"));a.label=3;case 3:return[2,o]}})});return function(e){return t.apply(this,arguments)}}(),getAllContentIds:function(){var t=i(function(t){var e,r,n,i,o,a,s,c,l,p,m,v,b,w,g,_,T;return y(this,function(y){switch(y.label){case 0:e=t.endpoint,r=t.alternateField,n=t.draftKey,i=t.filters,o=t.orders,a=t.customRequestInit;s={draftKey:n,filters:i,orders:o,limit:100,fields:r!==null&&r!==void 0?r:"id",depth:0};return[4,u({endpoint:e,queries:h(f({},s),{limit:0}),requestInit:a})];case 1:c=y.sent(),l=c.totalCount,p=[],m=0,v=function(t){return new Promise(function(e){return setTimeout(e,t)})},b=function(t){return t.every(function(t){return typeof t=="string"})};y.label=2;case 2:if(!(p.length<l))return[3,7];return[4,u({endpoint:e,queries:h(f({},s),{offset:m}),requestInit:a})];case 3:w=y.sent(),g=w.contents,_=g.map(function(t){return t[r!==null&&r!==void 0?r:"id"]});if(!b(_))throw new Error("The value of the field specified by `alternateField` is not a string.");p=d(p).concat(d(_)),m+=100;T=p.length<l;if(!T)return[3,5];return[4,v(1e3)];case 4:T=y.sent();y.label=5;case 5:T;y.label=6;case 6:return[3,2];case 7:return[2,p]}})});return function(e){return t.apply(this,arguments)}}(),getAllContents:function(){var t=i(function(t){var e,r,n,i,o,a,s,c,l,p,m,d;return y(this,function(v){switch(v.label){case 0:e=t.endpoint,r=t.queries,n=r===void 0?{}:r,i=t.customRequestInit;return[4,u({endpoint:e,queries:h(f({},n),{limit:0}),requestInit:i})];case 1:o=v.sent(),a=o.totalCount,s=[],c=0,l=function(t){return new Promise(function(e){return setTimeout(e,t)})};v.label=2;case 2:if(!(s.length<a))return[3,7];return[4,u({endpoint:e,queries:h(f({},n),{limit:100,offset:c}),requestInit:i})];case 3:p=v.sent(),m=p.contents;s=s.concat(m),c+=100;d=s.length<a;if(!d)return[3,5];return[4,l(1e3)];case 4:d=v.sent();v.label=5;case 5:d;v.label=6;case 6:return[3,2];case 7:return[2,s]}})});return function(e){return t.apply(this,arguments)}}(),create:function(){var t=i(function(t){var e,r,n,i,o,a,s,c;return y(this,function(l){e=t.endpoint,r=t.contentId,n=t.content,i=t.isDraft,o=i===void 0?!1:i,a=t.customRequestInit;if(!e)return[2,Promise.reject(new Error("endpoint is required"))];s=o?{status:"draft"}:{},c=h(f({},a),{method:r?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return[2,u({endpoint:e,contentId:r,queries:s,requestInit:c})]})});return function(e){return t.apply(this,arguments)}}(),update:function(){var t=i(function(t){var e,r,n,i,o,a,s,c;return y(this,function(l){e=t.endpoint,r=t.contentId,n=t.content,i=t.isDraft,o=i===void 0?!1:i,a=t.customRequestInit;if(!e)return[2,Promise.reject(new Error("endpoint is required"))];s=o?{status:"draft"}:{},c=h(f({},a),{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return[2,u({endpoint:e,contentId:r,queries:s,requestInit:c})]})});return function(e){return t.apply(this,arguments)}}(),delete:function(){var t=i(function(t){var e,r,n,i;return y(this,function(o){switch(o.label){case 0:e=t.endpoint,r=t.contentId,n=t.customRequestInit;if(!e)return[2,Promise.reject(new Error("endpoint is required"))];if(!r)return[2,Promise.reject(new Error("contentId is required"))];i=h(f({},n),{method:"DELETE",headers:{},body:void 0});return[4,u({endpoint:e,contentId:r,requestInit:i})];case 1:o.sent();return[2]}})});return function(e){return t.apply(this,arguments)}}()}};var A=function(t){var e=t.serviceDomain,r=t.apiKey;if(!e||!r)throw new Error("parameter is required (check serviceDomain and apiKey)");if(!S(e)||!S(r))throw new Error("parameter is not string");var n=function(){var t=i(function(t){var n,o,u,a,s,c,l,p,m,d,v,b,w,g,_,T;return y(this,function(q){switch(q.label){case 0:n=t.path,o=t.apiVersion,u=t.queries,a=u===void 0?{}:u,s=t.requestInit;c="https://".concat(e,".").concat(j,"/api/").concat(o),l=I(r),p=R(a),m="".concat(c,"/").concat(n).concat(p?"?".concat(p):""),d=function(){var t=i(function(t){var e,r,n;return y(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);return[4,t.json()];case 1:e=i.sent(),r=e.message;return[2,r!==null&&r!==void 0?r:null];case 2:n=i.sent();return[2,null];case 3:return[2]}})});return function e(e){return t.apply(this,arguments)}}();q.label=1;case 1:q.trys.push([1,3,,4]);return[4,l(m,h(f({},s),{method:(b=s===null||s===void 0?void 0:s.method)!==null&&b!==void 0?b:"GET"}))];case 2:v=q.sent();return[3,4];case 3:w=q.sent();if(w.data)throw w.data;if((g=w.response)===null||g===void 0?void 0:g.data)throw w.response.data;return[2,Promise.reject(new Error("Network Error.\n Details: ".concat((_=w.message)!==null&&_!==void 0?_:"")))];case 4:if(!!v.ok)return[3,6];return[4,d(v)];case 5:T=q.sent();return[2,Promise.reject(new Error("fetch API response status: ".concat(v.status).concat(T?"\n message is `".concat(T,"`"):"")))];case 6:return[2,v.json()]}})});return function e(e){return t.apply(this,arguments)}}();return{uploadMedia:function(){var t=i(function(t){var e,r,i,o,a,s,c,l,f,p,h,m;return y(this,function(d){switch(d.label){case 0:e=t.data,r=t.name,i=t.type,o=t.customRequestHeaders;a=new FormData;if(!u(e,Blob))return[3,1];if(e.name)a.set("file",e,e.name);else{if(!r)throw new Error("name is required when data is a Blob");a.set("file",e,r)}return[3,9];case 1:if(!u(e,ReadableStream))return[3,6];if(!r)throw new Error("name is required when data is a ReadableStream");if(!i)throw new Error("type is required when data is a ReadableStream");s=[],c=e.getReader();d.label=2;case 2:return[4,c.read()];case 3:if(!!(l=d.sent()).done)return[3,5];s.push(l.value);d.label=4;case 4:return[3,2];case 5:a.set("file",new Blob(s,{type:i}),r);return[3,9];case 6:if(!(typeof e=="string"||u(e,URL)))return[3,9];f=u(e,URL)?e:new URL(e);return[4,fetch(f.toString(),o?{headers:o}:void 0)];case 7:p=d.sent();return[4,p.blob()];case 8:h=d.sent(),m=new URL(p.url).pathname.split("/").pop();a.set("file",h,r!==null&&r!==void 0?r:m);d.label=9;case 9:return[2,n({path:"media",apiVersion:E,requestInit:{method:"POST",body:a}})]}})});return function(e){return t.apply(this,arguments)}}()}};return v(T)}();//# sourceMappingURL=microcms-js-sdk.js.map