@storyblok/js 2.0.5 → 2.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/storyblok-js.js +2 -2
- package/dist/storyblok-js.mjs +414 -466
- package/package.json +8 -8
package/dist/storyblok-js.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(function(h,u){typeof exports=="object"&&typeof module!="undefined"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(h=typeof globalThis!="undefined"?globalThis:h||self,u(h.storyblok={}))})(this,function(h){"use strict";var he=Object.defineProperty,ue=Object.defineProperties;var de=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var pe=Object.prototype.hasOwnProperty,ge=Object.prototype.propertyIsEnumerable;var I=(h,u,g)=>u in h?he(h,u,{enumerable:!0,configurable:!0,writable:!0,value:g}):h[u]=g,f=(h,u)=>{for(var g in u||(u={}))pe.call(u,g)&&I(h,g,u[g]);if(x)for(var g of x(u))ge.call(u,g)&&I(h,g,u[g]);return h},v=(h,u)=>ue(h,de(u));let u=!1;const g=[],_=o=>new Promise((e,t)=>{if(typeof window=="undefined"||(window.storyblokRegisterEvent=r=>{if(window.location===window.parent.location){console.warn("You are not in Draft Mode or in the Visual Editor.");return}u?r():g.push(r)},document.getElementById("storyblok-javascript-bridge")))return;const s=document.createElement("script");s.async=!0,s.src=o,s.id="storyblok-javascript-bridge",s.onerror=r=>t(r),s.onload=r=>{g.forEach(n=>n()),u=!0,e(r)},document.getElementsByTagName("head")[0].appendChild(s)});var C=Object.defineProperty,M=(o,e,t)=>e in o?C(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,c=(o,e,t)=>(M(o,typeof e!="symbol"?e+"":e,t),t);function E(o){return!(o!==o||o===1/0||o===-1/0)}function N(o,e,t){if(!E(e))throw new TypeError("Expected `limit` to be a finite number");if(!E(t))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],n=0;const a=function(){n++;const l=setTimeout(function(){n--,s.length>0&&a(),r=r.filter(function(p){return p!==l})},t);r.indexOf(l)<0&&r.push(l);const d=s.shift();d.resolve(o.apply(d.self,d.args))},i=function(...l){const d=this;return new Promise(function(p,w){s.push({resolve:p,reject:w,args:l,self:d}),n<e&&a()})};return i.abort=function(){r.forEach(clearTimeout),r=[],s.forEach(function(l){l.reject(function(){Error.call(this,"Throttled function aborted"),this.name="AbortError"})}),s.length=0},i}const A=function(o,e){const t={};for(const s in o){const r=o[s];e.indexOf(s)>-1&&r!==null&&(t[s]=r)}return t},L=o=>o==="email",U=()=>({singleTag:"hr"}),B=()=>({tag:"blockquote"}),H=()=>({tag:"ul"}),q=o=>({tag:["pre",{tag:"code",attrs:o.attrs}]}),D=()=>({singleTag:"br"}),J=o=>({tag:`h${o.attrs.level}`}),F=o=>({singleTag:[{tag:"img",attrs:A(o.attrs,["src","alt","title"])}]}),V=()=>({tag:"li"}),z=()=>({tag:"ol"}),G=()=>({tag:"p"}),Y=()=>({tag:"b"}),K=()=>({tag:"strike"}),Q=()=>({tag:"u"}),W=()=>({tag:"strong"}),X=()=>({tag:"code"}),Z=()=>({tag:"i"}),ee=o=>{const e=f({},o.attrs),{linktype:t="url"}=o.attrs;return L(t)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),{tag:[{tag:"a",attrs:e}]}},te=o=>({tag:[{tag:"span",attrs:o.attrs}]}),$={nodes:{horizontal_rule:U,blockquote:B,bullet_list:H,code_block:q,hard_break:D,heading:J,image:F,list_item:V,ordered_list:z,paragraph:G},marks:{bold:Y,strike:K,underline:Q,strong:W,code:X,italic:Z,link:ee,styled:te}},se=function(o){const e={"&":"&","<":"<",">":">",'"':""","'":"'"},t=/[&<>"']/g,s=RegExp(t.source);return o&&s.test(o)?o.replace(t,r=>e[r]):o};class y{constructor(e){c(this,"marks"),c(this,"nodes"),e||(e=$),this.marks=e.marks||[],this.nodes=e.nodes||[]}addNode(e,t){this.nodes[e]=t}addMark(e,t){this.marks[e]=t}render(e){if(e&&e.content&&Array.isArray(e.content)){let t="";return e.content.forEach(s=>{t+=this.renderNode(s)}),t}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(e){const t=[];e.marks&&e.marks.forEach(r=>{const n=this.getMatchingMark(r);n&&t.push(this.renderOpeningTag(n.tag))});const s=this.getMatchingNode(e);return s&&s.tag&&t.push(this.renderOpeningTag(s.tag)),e.content?e.content.forEach(r=>{t.push(this.renderNode(r))}):e.text?t.push(se(e.text)):s&&s.singleTag?t.push(this.renderTag(s.singleTag," /")):s&&s.html&&t.push(s.html),s&&s.tag&&t.push(this.renderClosingTag(s.tag)),e.marks&&e.marks.slice(0).reverse().forEach(r=>{const n=this.getMatchingMark(r);n&&t.push(this.renderClosingTag(n.tag))}),t.join("")}renderTag(e,t){return e.constructor===String?`<${e}${t}>`:e.map(s=>{if(s.constructor===String)return`<${s}${t}>`;{let r=`<${s.tag}`;if(s.attrs)for(const n in s.attrs){const a=s.attrs[n];a!==null&&(r+=` ${n}="${a}"`)}return`${r}${t}>`}}).join("")}renderOpeningTag(e){return this.renderTag(e,"")}renderClosingTag(e){return e.constructor===String?`</${e}>`:e.slice(0).reverse().map(t=>t.constructor===String?`</${t}>`:`</${t.tag}>`).join("")}getMatchingNode(e){const t=this.nodes[e.type];if(typeof t=="function")return t(e)}getMatchingMark(e){const t=this.marks[e.type];if(typeof t=="function")return t(e)}}class j{constructor(){c(this,"isCDNUrl",(e="")=>e.indexOf("/cdn/")>-1),c(this,"getOptionsPage",(e,t=25,s=1)=>v(f({},e),{per_page:t,page:s})),c(this,"delay",e=>new Promise(t=>setTimeout(t,e))),c(this,"arrayFrom",(e=0,t)=>[...Array(e)].map(t)),c(this,"range",(e=0,t=e)=>{const s=Math.abs(t-e)||0,r=e<t?1:-1;return this.arrayFrom(s,(n,a)=>a*r+e)}),c(this,"asyncMap",async(e,t)=>Promise.all(e.map(t))),c(this,"flatMap",(e=[],t)=>e.map(t).reduce((s,r)=>[...s,...r],[]))}stringify(e,t,s){const r=[];for(const n in e){if(!Object.prototype.hasOwnProperty.call(e,n))continue;const a=e[n],i=s?"":encodeURIComponent(n);let l;typeof a=="object"?l=this.stringify(a,t?t+encodeURIComponent("["+i+"]"):i,Array.isArray(a)):l=(t?t+encodeURIComponent("["+i+"]"):i)+"="+encodeURIComponent(a),r.push(l)}return r.join("&")}}var k=(o=>(o.GET="get",o.DELETE="delete",o.POST="post",o.PUT="put",o))(k||{});class re{constructor(e){c(this,"baseURL"),c(this,"timeout"),c(this,"headers"),c(this,"responseInterceptor"),c(this,"ejectInterceptor"),c(this,"url"),c(this,"parameters"),this.baseURL=e.baseURL,this.timeout=e.timeout?e.timeout*1e3:1e3,this.headers=e.headers||[],this.responseInterceptor=e.responseInterceptor,this.ejectInterceptor=!1,this.url="",this.parameters={}}get(e,t){return this.url=e,this.parameters=t,this._methodHandler(k.GET)}post(e,t){return this.url=e,this.parameters=t,this._methodHandler(k.POST)}put(e,t){return this.url=e,this.parameters=t,this._methodHandler(k.PUT)}delete(e,t){return this.url=e,this.parameters=t,this._methodHandler(k.DELETE)}async _responseHandler(e){const t=[],s={data:{},headers:{},status:0,statusText:""};await e.json().then(r=>{s.data=r});for(const r of e.headers.entries())t[r[0]]=r[1];return s.headers=f({},t),s.status=e.status,s.statusText=e.statusText,s}async _methodHandler(e){const t=new URL(`${this.baseURL}${this.url}`);let s=null;if(e==="get"){const i=new j;t.search=i.stringify(this.parameters)}else s=JSON.stringify(this.parameters);const r=new AbortController,{signal:n}=r,a=setTimeout(()=>r.abort(),this.timeout);try{const i=await fetch(`${t}`,{method:e,headers:this.headers,body:s,signal:n});clearTimeout(a);const l=await this._responseHandler(i);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(l)):this._statusHandler(l)}catch(i){return i}}eject(){this.ejectInterceptor=!0}_statusHandler(e){if(/20[01]/g.test(`${e.status}`))return e;throw{message:new Error(e.statusText||`status: ${e.status}`),response:e}}}let b={};const m={};class oe{constructor(e,t){if(c(this,"client"),c(this,"maxRetries"),c(this,"throttle"),c(this,"accessToken"),c(this,"cache"),c(this,"helpers"),c(this,"relations"),c(this,"links"),c(this,"richTextResolver"),c(this,"resolveNestedRelations"),!t){const n=e.region?`-${e.region}`:"",a=e.https===!1?"http":"https";e.oauthToken?t=`${a}://api${n}.storyblok.com/v1`:t=`${a}://api${n}.storyblok.com/v2`}const s=Object.assign({},e.headers);let r=5;e.oauthToken&&(s.Authorization=e.oauthToken,r=3),e.rateLimit&&(r=e.rateLimit),e.richTextSchema?this.richTextResolver=new y(e.richTextSchema):this.richTextResolver=new y,e.componentResolver&&this.setComponentResolver(e.componentResolver),this.maxRetries=e.maxRetries,this.throttle=N(this.throttledRequest,r,1e3),this.accessToken=e.accessToken||"",this.relations={},this.links={},this.cache=e.cache||{clear:"manual"},this.helpers=new j,this.resolveNestedRelations=!1,this.client=new re({baseURL:t,timeout:e.timeout||0,headers:s,responseInterceptor:e.responseInterceptor})}setComponentResolver(e){this.richTextResolver.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})}parseParams(e){return e.version||(e.version="published"),e.token||(e.token=this.getToken()),e.cv||(e.cv=m[e.token]),Array.isArray(e.resolve_relations)&&(e.resolve_relations=e.resolve_relations.join(",")),e}factoryParamOptions(e,t){return this.helpers.isCDNUrl(e)?this.parseParams(t):t}makeRequest(e,t,s,r){const n=this.factoryParamOptions(e,this.helpers.getOptionsPage(t,s,r));return this.cacheResponse(e,n)}get(e,t){t||(t={});const s=`/${e}`,r=this.factoryParamOptions(s,t);return this.cacheResponse(s,r)}async getAll(e,t,s){const r=(t==null?void 0:t.per_page)||25,n=`/${e}`,a=n.split("/"),i=s||a[a.length-1],l=1,d=await this.makeRequest(n,t,r,l),p=d.total?Math.ceil(d.total/r):1,w=await this.helpers.asyncMap(this.helpers.range(l,p),R=>this.makeRequest(n,t,r,R+1));return this.helpers.flatMap([d,...w],R=>Object.values(R.data[i]))}post(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("post",s,t))}put(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("put",s,t))}delete(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("delete",s,t))}getStories(e){return this.get("cdn/stories",e)}getStory(e,t){return this.get(`cdn/stories/${e}`,t)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(e){return JSON.parse(JSON.stringify(e))}_insertLinks(e,t){const s=e[t];s&&s.fieldtype=="multilink"&&s.linktype=="story"&&typeof s.id=="string"&&this.links[s.id]?s.story=this._cleanCopy(this.links[s.id]):s&&s.linktype==="story"&&typeof s.uuid=="string"&&this.links[s.uuid]&&(s.story=this._cleanCopy(this.links[s.uuid]))}_insertRelations(e,t,s){if(s.indexOf(`${e.component}.${t}`)>-1){if(typeof e[t]=="string")this.relations[e[t]]&&(e[t]=this._cleanCopy(this.relations[e[t]]));else if(e[t]&&e[t].constructor===Array){const r=[];e[t].forEach(n=>{this.relations[n]&&r.push(this._cleanCopy(this.relations[n]))}),e[t]=r}}}iterateTree(e,t){const s=r=>{if(r!=null){if(r.constructor===Array)for(let n=0;n<r.length;n++)s(r[n]);else if(r.constructor===Object){if(r._stopResolving)return;for(const n in r)(r.component&&r._uid||r.type==="link")&&(this._insertRelations(r,n,t),this._insertLinks(r,n)),s(r[n])}}};s(e.content)}async resolveLinks(e,t){let s=[];if(e.link_uuids){const r=e.link_uuids.length,n=[],a=50;for(let i=0;i<r;i+=a){const l=Math.min(r,i+a);n.push(e.link_uuids.slice(i,l))}for(let i=0;i<n.length;i++)(await this.getStories({per_page:a,language:t.language,version:t.version,by_uuids:n[i].join(",")})).data.stories.forEach(l=>{s.push(l)})}else s=e.links;s.forEach(r=>{this.links[r.uuid]=v(f({},r),{_stopResolving:!0})})}async resolveRelations(e,t){let s=[];if(e.rel_uuids){const r=e.rel_uuids.length,n=[],a=50;for(let i=0;i<r;i+=a){const l=Math.min(r,i+a);n.push(e.rel_uuids.slice(i,l))}for(let i=0;i<n.length;i++)(await this.getStories({per_page:a,language:t.language,version:t.version,by_uuids:n[i].join(",")})).data.stories.forEach(l=>{s.push(l)})}else s=e.rels;s&&s.length>0&&s.forEach(r=>{this.relations[r.uuid]=v(f({},r),{_stopResolving:!0})})}async resolveStories(e,t){var s,r;let n=[];if(typeof t.resolve_relations<"u"&&t.resolve_relations.length>0&&(n=t.resolve_relations.split(","),await this.resolveRelations(e,t)),t.resolve_links&&["1","story","url"].indexOf(t.resolve_links)>-1&&(((s=e.links)==null?void 0:s.length)||((r=e.link_uuids)==null?void 0:r.length))&&await this.resolveLinks(e,t),this.resolveNestedRelations)for(const a in this.relations)this.iterateTree(this.relations[a],n);e.story?this.iterateTree(e.story,n):e.stories.forEach(a=>{this.iterateTree(a,n)})}cacheResponse(e,t,s){return new Promise((r,n)=>{const a=this.helpers.stringify({url:e,params:t}),i=this.cacheProvider();if(this.cache.clear==="auto"&&t.version==="draft"&&this.flushCache(),t.version==="published"&&e!="/cdn/spaces/me"){const l=i.get(a);if(l)return r(l)}try{(async()=>{var l;const d=await this.throttle("get",e,t);let p={data:d.data,headers:d.headers};if((l=d.headers)!=null&&l["per-page"]&&(p=Object.assign({},p,{perPage:d.headers["per-page"]?parseInt(d.headers["per-page"]):0,total:d.headers["per-page"]?parseInt(d.headers.total):0})),d.status!=200)return n(d);(p.data.story||p.data.stories)&&await this.resolveStories(p.data,t),t.version==="published"&&e!="/cdn/spaces/me"&&i.set(a,p),p.data.cv&&t.token&&(t.version=="draft"&&m[t.token]!=p.data.cv&&this.flushCache(),m[t.token]=p.data.cv),r(p)})()}catch{}})}throttledRequest(e,t,s){return this.client[e](t,s)}cacheVersions(){return m}cacheVersion(){return m[this.accessToken]}setCacheVersion(e){this.accessToken&&(m[this.accessToken]=e)}cacheProvider(){switch(this.cache.type){case"memory":return{get(e){return b[e]},getAll(){return b},set(e,t){b[e]=t},flush(){b={}}};default:return{get(){},getAll(){},set(){},flush(){}}}}flushCache(){return this.cacheProvider().flush(),this}}const ne=(o={})=>{const{apiOptions:e}=o;if(!e.accessToken){console.error("You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication");return}return{storyblokApi:new oe(e)}};var ie=o=>{if(typeof o!="object"||typeof o._editable=="undefined")return{};const e=JSON.parse(o._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(e),"data-blok-uid":e.id+"-"+e.uid}};let T;const S="https://app.storyblok.com/f/storyblok-v2-latest.js",P=(o,e,t={})=>{if(typeof window!="undefined"){if(typeof window.storyblokRegisterEvent=="undefined"){console.error("Storyblok Bridge is disabled. Please enable it to use it. Read https://github.com/storyblok/storyblok-js");return}if(!o){console.warn("Story ID is not defined. Please provide a valid ID.");return}window.storyblokRegisterEvent(()=>{new window.StoryblokBridge(t).on(["input","published","change"],r=>{r.action==="input"&&r.story.id===o?e(r.story):(r.action==="change"||r.action==="published")&&r.storyId===o&&window.location.reload()})})}},ae=(o={})=>{const{bridge:e,accessToken:t,use:s=[],apiOptions:r={},richText:n={}}=o;r.accessToken=r.accessToken||t;const a={bridge:e,apiOptions:r};let i={};return s.forEach(l=>{i=f(f({},i),l(a))}),e!==!1&&_(S),T=new y(n.schema),n.resolver&&O(T,n.resolver),i},O=(o,e)=>{o.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})},le=(o,e,t)=>{let s=t||T;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return o===""?"":o?(e&&(s=new y(e.schema),e.resolver&&O(s,e.resolver)),s.render(o)):(console.warn(`${o} is not a valid Richtext object. This might be because the value of the richtext field is empty.
|
|
1
|
+
(function(d,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(d=typeof globalThis<"u"?globalThis:d||self,p(d.storyblok={}))})(this,function(d){"use strict";let p=!1;const v=[],T=n=>new Promise((e,t)=>{if(typeof window>"u"||(window.storyblokRegisterEvent=r=>{if(window.location===window.parent.location){console.warn("You are not in Draft Mode or in the Visual Editor.");return}p?r():v.push(r)},document.getElementById("storyblok-javascript-bridge")))return;const s=document.createElement("script");s.async=!0,s.src=n,s.id="storyblok-javascript-bridge",s.onerror=r=>t(r),s.onload=r=>{v.forEach(o=>o()),p=!0,e(r)},document.getElementsByTagName("head")[0].appendChild(s)});var S=Object.defineProperty,x=(n,e,t)=>e in n?S(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,l=(n,e,t)=>(x(n,typeof e!="symbol"?e+"":e,t),t);function w(n){return!(n!==n||n===1/0||n===-1/0)}function O(n,e,t){if(!w(e))throw new TypeError("Expected `limit` to be a finite number");if(!w(t))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],o=0;const i=function(){o++;const c=setTimeout(function(){o--,s.length>0&&i(),r=r.filter(function(u){return u!==c})},t);r.indexOf(c)<0&&r.push(c);const h=s.shift();h.resolve(n.apply(h.self,h.args))},a=function(...c){const h=this;return new Promise(function(u,k){s.push({resolve:u,reject:k,args:c,self:h}),o<e&&i()})};return a.abort=function(){r.forEach(clearTimeout),r=[],s.forEach(function(c){c.reject(function(){Error.call(this,"Throttled function aborted"),this.name="AbortError"})}),s.length=0},a}const P=function(n,e){const t={};for(const s in n){const r=n[s];e.indexOf(s)>-1&&r!==null&&(t[s]=r)}return t},I=n=>n==="email",C=()=>({singleTag:"hr"}),A=()=>({tag:"blockquote"}),M=()=>({tag:"ul"}),N=n=>({tag:["pre",{tag:"code",attrs:n.attrs}]}),L=()=>({singleTag:"br"}),U=n=>({tag:`h${n.attrs.level}`}),B=n=>({singleTag:[{tag:"img",attrs:P(n.attrs,["src","alt","title"])}]}),H=()=>({tag:"li"}),q=()=>({tag:"ol"}),D=()=>({tag:"p"}),J=()=>({tag:"b"}),F=()=>({tag:"strike"}),V=()=>({tag:"u"}),z=()=>({tag:"strong"}),Y=()=>({tag:"code"}),K=()=>({tag:"i"}),Q=n=>{const e={...n.attrs},{linktype:t="url"}=n.attrs;return I(t)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),{tag:[{tag:"a",attrs:e}]}},G=n=>({tag:[{tag:"span",attrs:n.attrs}]}),R={nodes:{horizontal_rule:C,blockquote:A,bullet_list:M,code_block:N,hard_break:L,heading:U,image:B,list_item:H,ordered_list:q,paragraph:D},marks:{bold:J,strike:F,underline:V,strong:z,code:Y,italic:K,link:Q,styled:G}},W=function(n){const e={"&":"&","<":"<",">":">",'"':""","'":"'"},t=/[&<>"']/g,s=RegExp(t.source);return n&&s.test(n)?n.replace(t,r=>e[r]):n};class f{constructor(e){l(this,"marks"),l(this,"nodes"),e||(e=R),this.marks=e.marks||[],this.nodes=e.nodes||[]}addNode(e,t){this.nodes[e]=t}addMark(e,t){this.marks[e]=t}render(e){if(e&&e.content&&Array.isArray(e.content)){let t="";return e.content.forEach(s=>{t+=this.renderNode(s)}),t}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(e){const t=[];e.marks&&e.marks.forEach(r=>{const o=this.getMatchingMark(r);o&&t.push(this.renderOpeningTag(o.tag))});const s=this.getMatchingNode(e);return s&&s.tag&&t.push(this.renderOpeningTag(s.tag)),e.content?e.content.forEach(r=>{t.push(this.renderNode(r))}):e.text?t.push(W(e.text)):s&&s.singleTag?t.push(this.renderTag(s.singleTag," /")):s&&s.html&&t.push(s.html),s&&s.tag&&t.push(this.renderClosingTag(s.tag)),e.marks&&e.marks.slice(0).reverse().forEach(r=>{const o=this.getMatchingMark(r);o&&t.push(this.renderClosingTag(o.tag))}),t.join("")}renderTag(e,t){return e.constructor===String?`<${e}${t}>`:e.map(s=>{if(s.constructor===String)return`<${s}${t}>`;{let r=`<${s.tag}`;if(s.attrs)for(const o in s.attrs){const i=s.attrs[o];i!==null&&(r+=` ${o}="${i}"`)}return`${r}${t}>`}}).join("")}renderOpeningTag(e){return this.renderTag(e,"")}renderClosingTag(e){return e.constructor===String?`</${e}>`:e.slice(0).reverse().map(t=>t.constructor===String?`</${t}>`:`</${t.tag}>`).join("")}getMatchingNode(e){const t=this.nodes[e.type];if(typeof t=="function")return t(e)}getMatchingMark(e){const t=this.marks[e.type];if(typeof t=="function")return t(e)}}class _{constructor(){l(this,"isCDNUrl",(e="")=>e.indexOf("/cdn/")>-1),l(this,"getOptionsPage",(e,t=25,s=1)=>({...e,per_page:t,page:s})),l(this,"delay",e=>new Promise(t=>setTimeout(t,e))),l(this,"arrayFrom",(e=0,t)=>[...Array(e)].map(t)),l(this,"range",(e=0,t=e)=>{const s=Math.abs(t-e)||0,r=e<t?1:-1;return this.arrayFrom(s,(o,i)=>i*r+e)}),l(this,"asyncMap",async(e,t)=>Promise.all(e.map(t))),l(this,"flatMap",(e=[],t)=>e.map(t).reduce((s,r)=>[...s,...r],[]))}stringify(e,t,s){const r=[];for(const o in e){if(!Object.prototype.hasOwnProperty.call(e,o))continue;const i=e[o],a=s?"":encodeURIComponent(o);let c;typeof i=="object"?c=this.stringify(i,t?t+encodeURIComponent("["+a+"]"):a,Array.isArray(i)):c=(t?t+encodeURIComponent("["+a+"]"):a)+"="+encodeURIComponent(i),r.push(c)}return r.join("&")}}class X{constructor(e){l(this,"baseURL"),l(this,"timeout"),l(this,"headers"),l(this,"responseInterceptor"),l(this,"fetch"),l(this,"ejectInterceptor"),l(this,"url"),l(this,"parameters"),this.baseURL=e.baseURL,this.headers=e.headers||[],this.timeout=e!=null&&e.timeout?e.timeout*1e3:0,this.responseInterceptor=e.responseInterceptor,this.fetch=(...t)=>e.fetch?e.fetch(...t):fetch(...t),this.ejectInterceptor=!1,this.url="",this.parameters={}}get(e,t){return this.url=e,this.parameters=t,this._methodHandler("get")}post(e,t){return this.url=e,this.parameters=t,this._methodHandler("post")}put(e,t){return this.url=e,this.parameters=t,this._methodHandler("put")}delete(e,t){return this.url=e,this.parameters=t,this._methodHandler("delete")}async _responseHandler(e){const t=[],s={data:{},headers:{},status:0,statusText:""};await e.json().then(r=>{s.data=r});for(const r of e.headers.entries())t[r[0]]=r[1];return s.headers={...t},s.status=e.status,s.statusText=e.statusText,s}async _methodHandler(e){const t=new URL(`${this.baseURL}${this.url}`);let s=null;if(e==="get"){const a=new _;t.search=a.stringify(this.parameters)}else s=JSON.stringify(this.parameters);const r=new AbortController,{signal:o}=r;let i;this.timeout&&(i=setTimeout(()=>r.abort(),this.timeout));try{const a=await this.fetch(`${t}`,{method:e,headers:this.headers,body:s,signal:o});this.timeout&&clearTimeout(i);const c=await this._responseHandler(a);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(c)):this._statusHandler(c)}catch(a){return a}}eject(){this.ejectInterceptor=!0}_statusHandler(e){if(/20[01]/g.test(`${e.status}`))return e;throw{message:new Error(e.statusText||`status: ${e.status}`),response:e}}}let m={};const g={};class Z{constructor(e,t){if(l(this,"client"),l(this,"maxRetries"),l(this,"throttle"),l(this,"accessToken"),l(this,"cache"),l(this,"helpers"),l(this,"relations"),l(this,"links"),l(this,"richTextResolver"),l(this,"resolveNestedRelations"),!t){const o=e.region?`-${e.region}`:"",i=e.https===!1?"http":"https";e.oauthToken?t=`${i}://api${o}.storyblok.com/v1`:t=`${i}://api${o}.storyblok.com/v2`}const s=new Headers;s.set("Content-Type","application/json"),s.set("Accept","application/json"),s.forEach((o,i)=>{e.headers&&e.headers[i]&&s.set(i,e.headers[i])});let r=5;e.oauthToken&&(s.set("Authorization",e.oauthToken),r=3),e.rateLimit&&(r=e.rateLimit),e.richTextSchema?this.richTextResolver=new f(e.richTextSchema):this.richTextResolver=new f,e.componentResolver&&this.setComponentResolver(e.componentResolver),this.maxRetries=e.maxRetries,this.throttle=O(this.throttledRequest,r,1e3),this.accessToken=e.accessToken||"",this.relations={},this.links={},this.cache=e.cache||{clear:"manual"},this.helpers=new _,this.resolveNestedRelations=!1,this.client=new X({baseURL:t,timeout:e.timeout||0,headers:s,responseInterceptor:e.responseInterceptor,fetch:e.fetch})}setComponentResolver(e){this.richTextResolver.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})}parseParams(e){return e.version||(e.version="published"),e.token||(e.token=this.getToken()),e.cv||(e.cv=g[e.token]),Array.isArray(e.resolve_relations)&&(e.resolve_relations=e.resolve_relations.join(",")),e}factoryParamOptions(e,t){return this.helpers.isCDNUrl(e)?this.parseParams(t):t}makeRequest(e,t,s,r){const o=this.factoryParamOptions(e,this.helpers.getOptionsPage(t,s,r));return this.cacheResponse(e,o)}get(e,t){t||(t={});const s=`/${e}`,r=this.factoryParamOptions(s,t);return this.cacheResponse(s,r)}async getAll(e,t,s){const r=(t==null?void 0:t.per_page)||25,o=`/${e}`,i=o.split("/"),a=s||i[i.length-1],c=1,h=await this.makeRequest(o,t,r,c),u=h.total?Math.ceil(h.total/r):1,k=await this.helpers.asyncMap(this.helpers.range(c,u),b=>this.makeRequest(o,t,r,b+1));return this.helpers.flatMap([h,...k],b=>Object.values(b.data[a]))}post(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("post",s,t))}put(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("put",s,t))}delete(e,t){const s=`/${e}`;return Promise.resolve(this.throttle("delete",s,t))}getStories(e){return this.get("cdn/stories",e)}getStory(e,t){return this.get(`cdn/stories/${e}`,t)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(e){return JSON.parse(JSON.stringify(e))}_insertLinks(e,t){const s=e[t];s&&s.fieldtype=="multilink"&&s.linktype=="story"&&typeof s.id=="string"&&this.links[s.id]?s.story=this._cleanCopy(this.links[s.id]):s&&s.linktype==="story"&&typeof s.uuid=="string"&&this.links[s.uuid]&&(s.story=this._cleanCopy(this.links[s.uuid]))}_insertRelations(e,t,s){if(s.indexOf(`${e.component}.${t}`)>-1){if(typeof e[t]=="string")this.relations[e[t]]&&(e[t]=this._cleanCopy(this.relations[e[t]]));else if(e[t]&&e[t].constructor===Array){const r=[];e[t].forEach(o=>{this.relations[o]&&r.push(this._cleanCopy(this.relations[o]))}),e[t]=r}}}iterateTree(e,t){const s=r=>{if(r!=null){if(r.constructor===Array)for(let o=0;o<r.length;o++)s(r[o]);else if(r.constructor===Object){if(r._stopResolving)return;for(const o in r)(r.component&&r._uid||r.type==="link")&&(this._insertRelations(r,o,t),this._insertLinks(r,o)),s(r[o])}}};s(e.content)}async resolveLinks(e,t){let s=[];if(e.link_uuids){const r=e.link_uuids.length,o=[],i=50;for(let a=0;a<r;a+=i){const c=Math.min(r,a+i);o.push(e.link_uuids.slice(a,c))}for(let a=0;a<o.length;a++)(await this.getStories({per_page:i,language:t.language,version:t.version,by_uuids:o[a].join(",")})).data.stories.forEach(c=>{s.push(c)})}else s=e.links;s.forEach(r=>{this.links[r.uuid]={...r,_stopResolving:!0}})}async resolveRelations(e,t){let s=[];if(e.rel_uuids){const r=e.rel_uuids.length,o=[],i=50;for(let a=0;a<r;a+=i){const c=Math.min(r,a+i);o.push(e.rel_uuids.slice(a,c))}for(let a=0;a<o.length;a++)(await this.getStories({per_page:i,language:t.language,version:t.version,by_uuids:o[a].join(",")})).data.stories.forEach(c=>{s.push(c)})}else s=e.rels;s&&s.length>0&&s.forEach(r=>{this.relations[r.uuid]={...r,_stopResolving:!0}})}async resolveStories(e,t){var s,r;let o=[];if(typeof t.resolve_relations<"u"&&t.resolve_relations.length>0&&(typeof t.resolve_relations=="string"&&(o=t.resolve_relations.split(",")),await this.resolveRelations(e,t)),t.resolve_links&&["1","story","url"].indexOf(t.resolve_links)>-1&&((s=e.links)!=null&&s.length||(r=e.link_uuids)!=null&&r.length)&&await this.resolveLinks(e,t),this.resolveNestedRelations)for(const i in this.relations)this.iterateTree(this.relations[i],o);e.story?this.iterateTree(e.story,o):e.stories.forEach(i=>{this.iterateTree(i,o)})}cacheResponse(e,t,s){return new Promise((r,o)=>{const i=this.helpers.stringify({url:e,params:t}),a=this.cacheProvider();if(this.cache.clear==="auto"&&t.version==="draft"&&this.flushCache(),t.version==="published"&&e!="/cdn/spaces/me"){const c=a.get(i);if(c)return r(c)}try{(async()=>{var c;const h=await this.throttle("get",e,t);let u={data:h.data,headers:h.headers};if((c=h.headers)!=null&&c["per-page"]&&(u=Object.assign({},u,{perPage:h.headers["per-page"]?parseInt(h.headers["per-page"]):0,total:h.headers["per-page"]?parseInt(h.headers.total):0})),h.status!=200)return o(h);(u.data.story||u.data.stories)&&await this.resolveStories(u.data,t),t.version==="published"&&e!="/cdn/spaces/me"&&a.set(i,u),u.data.cv&&t.token&&(t.version=="draft"&&g[t.token]!=u.data.cv&&this.flushCache(),g[t.token]=u.data.cv),r(u)})()}catch{}})}throttledRequest(e,t,s){return this.client[e](t,s)}cacheVersions(){return g}cacheVersion(){return g[this.accessToken]}setCacheVersion(e){this.accessToken&&(g[this.accessToken]=e)}cacheProvider(){switch(this.cache.type){case"memory":return{get(e){return m[e]},getAll(){return m},set(e,t){m[e]=t},flush(){m={}}};default:return{get(){},getAll(){},set(){},flush(){}}}}flushCache(){return this.cacheProvider().flush(),this}}const ee=(n={})=>{const{apiOptions:e}=n;if(!e.accessToken){console.error("You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication");return}return{storyblokApi:new Z(e)}},te=n=>{if(typeof n!="object"||typeof n._editable>"u")return{};const e=JSON.parse(n._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(e),"data-blok-uid":e.id+"-"+e.uid}};let y;const j="https://app.storyblok.com/f/storyblok-v2-latest.js",$=(n,e,t={})=>{if(!(typeof window>"u")){if(typeof window.storyblokRegisterEvent>"u"){console.error("Storyblok Bridge is disabled. Please enable it to use it. Read https://github.com/storyblok/storyblok-js");return}if(!n){console.warn("Story ID is not defined. Please provide a valid ID.");return}window.storyblokRegisterEvent(()=>{new window.StoryblokBridge(t).on(["input","published","change"],r=>{r.action==="input"&&r.story.id===n?e(r.story):(r.action==="change"||r.action==="published")&&r.storyId===n&&window.location.reload()})})}},se=(n={})=>{const{bridge:e,accessToken:t,use:s=[],apiOptions:r={},richText:o={}}=n;r.accessToken=r.accessToken||t;const i={bridge:e,apiOptions:r};let a={};return s.forEach(c=>{a={...a,...c(i)}}),e!==!1&&T(j),y=new f(o.schema),o.resolver&&E(y,o.resolver),a},E=(n,e)=>{n.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})},re=(n,e,t)=>{let s=t||y;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return n===""?"":n?(e&&(s=new f(e.schema),e.resolver&&E(s,e.resolver)),s.render(n)):(console.warn(`${n} is not a valid Richtext object. This might be because the value of the richtext field is empty.
|
|
2
2
|
|
|
3
|
-
For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`),"")},
|
|
3
|
+
For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`),"")},oe=()=>T(j);d.RichTextResolver=f,d.RichTextSchema=R,d.apiPlugin=ee,d.loadStoryblokBridge=oe,d.registerStoryblokBridge=$,d.renderRichText=re,d.storyblokEditable=te,d.storyblokInit=se,d.useStoryblokBridge=$,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
package/dist/storyblok-js.mjs
CHANGED
|
@@ -1,125 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b2) => {
|
|
9
|
-
for (var prop in b2 || (b2 = {}))
|
|
10
|
-
if (__hasOwnProp.call(b2, prop))
|
|
11
|
-
__defNormalProp(a, prop, b2[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b2)) {
|
|
14
|
-
if (__propIsEnum.call(b2, prop))
|
|
15
|
-
__defNormalProp(a, prop, b2[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b2) => __defProps(a, __getOwnPropDescs(b2));
|
|
20
|
-
let loaded = false;
|
|
21
|
-
const callbacks = [];
|
|
22
|
-
const loadBridge = (src) => {
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
24
|
-
if (typeof window === "undefined")
|
|
1
|
+
let k = !1;
|
|
2
|
+
const b = [], w = (n) => new Promise((e, t) => {
|
|
3
|
+
if (typeof window > "u" || (window.storyblokRegisterEvent = (r) => {
|
|
4
|
+
if (window.location === window.parent.location) {
|
|
5
|
+
console.warn("You are not in Draft Mode or in the Visual Editor.");
|
|
25
6
|
return;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const script = document.createElement("script");
|
|
39
|
-
script.async = true;
|
|
40
|
-
script.src = src;
|
|
41
|
-
script.id = "storyblok-javascript-bridge";
|
|
42
|
-
script.onerror = (error) => reject(error);
|
|
43
|
-
script.onload = (ev) => {
|
|
44
|
-
callbacks.forEach((cb) => cb());
|
|
45
|
-
loaded = true;
|
|
46
|
-
resolve(ev);
|
|
47
|
-
};
|
|
48
|
-
document.getElementsByTagName("head")[0].appendChild(script);
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
var m = Object.defineProperty;
|
|
52
|
-
var R = (i, t, e) => t in i ? m(i, t, { enumerable: true, configurable: true, writable: true, value: e }) : i[t] = e;
|
|
53
|
-
var l = (i, t, e) => (R(i, typeof t != "symbol" ? t + "" : t, e), e);
|
|
54
|
-
function y(i) {
|
|
55
|
-
return !(i !== i || i === 1 / 0 || i === -1 / 0);
|
|
7
|
+
}
|
|
8
|
+
k ? r() : b.push(r);
|
|
9
|
+
}, document.getElementById("storyblok-javascript-bridge")))
|
|
10
|
+
return;
|
|
11
|
+
const s = document.createElement("script");
|
|
12
|
+
s.async = !0, s.src = n, s.id = "storyblok-javascript-bridge", s.onerror = (r) => t(r), s.onload = (r) => {
|
|
13
|
+
b.forEach((o) => o()), k = !0, e(r);
|
|
14
|
+
}, document.getElementsByTagName("head")[0].appendChild(s);
|
|
15
|
+
});
|
|
16
|
+
var $ = Object.defineProperty, j = (n, e, t) => e in n ? $(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t, l = (n, e, t) => (j(n, typeof e != "symbol" ? e + "" : e, t), t);
|
|
17
|
+
function v(n) {
|
|
18
|
+
return !(n !== n || n === 1 / 0 || n === -1 / 0);
|
|
56
19
|
}
|
|
57
|
-
function
|
|
58
|
-
if (!
|
|
20
|
+
function x(n, e, t) {
|
|
21
|
+
if (!v(e))
|
|
59
22
|
throw new TypeError("Expected `limit` to be a finite number");
|
|
60
|
-
if (!
|
|
23
|
+
if (!v(t))
|
|
61
24
|
throw new TypeError("Expected `interval` to be a finite number");
|
|
62
25
|
const s = [];
|
|
63
|
-
let r = [],
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
return u !==
|
|
26
|
+
let r = [], o = 0;
|
|
27
|
+
const i = function() {
|
|
28
|
+
o++;
|
|
29
|
+
const c = setTimeout(function() {
|
|
30
|
+
o--, s.length > 0 && i(), r = r.filter(function(u) {
|
|
31
|
+
return u !== c;
|
|
69
32
|
});
|
|
70
|
-
},
|
|
71
|
-
r.indexOf(
|
|
33
|
+
}, t);
|
|
34
|
+
r.indexOf(c) < 0 && r.push(c);
|
|
72
35
|
const h = s.shift();
|
|
73
|
-
h.resolve(
|
|
74
|
-
},
|
|
36
|
+
h.resolve(n.apply(h.self, h.args));
|
|
37
|
+
}, a = function(...c) {
|
|
75
38
|
const h = this;
|
|
76
|
-
return new Promise(function(u,
|
|
39
|
+
return new Promise(function(u, f) {
|
|
77
40
|
s.push({
|
|
78
41
|
resolve: u,
|
|
79
|
-
reject:
|
|
80
|
-
args:
|
|
42
|
+
reject: f,
|
|
43
|
+
args: c,
|
|
81
44
|
self: h
|
|
82
|
-
}),
|
|
45
|
+
}), o < e && i();
|
|
83
46
|
});
|
|
84
47
|
};
|
|
85
|
-
return
|
|
86
|
-
r.forEach(clearTimeout), r = [], s.forEach(function(
|
|
87
|
-
|
|
48
|
+
return a.abort = function() {
|
|
49
|
+
r.forEach(clearTimeout), r = [], s.forEach(function(c) {
|
|
50
|
+
c.reject(function() {
|
|
88
51
|
Error.call(this, "Throttled function aborted"), this.name = "AbortError";
|
|
89
52
|
});
|
|
90
53
|
}), s.length = 0;
|
|
91
|
-
},
|
|
54
|
+
}, a;
|
|
92
55
|
}
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
for (const s in
|
|
96
|
-
const r =
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
return
|
|
100
|
-
},
|
|
56
|
+
const E = function(n, e) {
|
|
57
|
+
const t = {};
|
|
58
|
+
for (const s in n) {
|
|
59
|
+
const r = n[s];
|
|
60
|
+
e.indexOf(s) > -1 && r !== null && (t[s] = r);
|
|
61
|
+
}
|
|
62
|
+
return t;
|
|
63
|
+
}, S = (n) => n === "email", O = () => ({
|
|
101
64
|
singleTag: "hr"
|
|
102
|
-
}),
|
|
65
|
+
}), P = () => ({
|
|
103
66
|
tag: "blockquote"
|
|
104
|
-
}),
|
|
67
|
+
}), C = () => ({
|
|
105
68
|
tag: "ul"
|
|
106
|
-
}),
|
|
69
|
+
}), I = (n) => ({
|
|
107
70
|
tag: [
|
|
108
71
|
"pre",
|
|
109
72
|
{
|
|
110
73
|
tag: "code",
|
|
111
|
-
attrs:
|
|
74
|
+
attrs: n.attrs
|
|
112
75
|
}
|
|
113
76
|
]
|
|
114
|
-
}),
|
|
77
|
+
}), A = () => ({
|
|
115
78
|
singleTag: "br"
|
|
116
|
-
}),
|
|
117
|
-
tag: `h${
|
|
118
|
-
}),
|
|
79
|
+
}), N = (n) => ({
|
|
80
|
+
tag: `h${n.attrs.level}`
|
|
81
|
+
}), M = (n) => ({
|
|
119
82
|
singleTag: [
|
|
120
83
|
{
|
|
121
84
|
tag: "img",
|
|
122
|
-
attrs:
|
|
85
|
+
attrs: E(n.attrs, ["src", "alt", "title"])
|
|
123
86
|
}
|
|
124
87
|
]
|
|
125
88
|
}), L = () => ({
|
|
@@ -128,309 +91,305 @@ const b = function(i, t) {
|
|
|
128
91
|
tag: "ol"
|
|
129
92
|
}), H = () => ({
|
|
130
93
|
tag: "p"
|
|
131
|
-
}),
|
|
94
|
+
}), B = () => ({
|
|
132
95
|
tag: "b"
|
|
133
|
-
}),
|
|
96
|
+
}), q = () => ({
|
|
134
97
|
tag: "strike"
|
|
135
|
-
}),
|
|
98
|
+
}), D = () => ({
|
|
136
99
|
tag: "u"
|
|
137
|
-
}),
|
|
100
|
+
}), J = () => ({
|
|
138
101
|
tag: "strong"
|
|
139
|
-
}),
|
|
102
|
+
}), F = () => ({
|
|
140
103
|
tag: "code"
|
|
141
|
-
}),
|
|
104
|
+
}), V = () => ({
|
|
142
105
|
tag: "i"
|
|
143
|
-
}),
|
|
144
|
-
const
|
|
145
|
-
return
|
|
106
|
+
}), z = (n) => {
|
|
107
|
+
const e = { ...n.attrs }, { linktype: t = "url" } = n.attrs;
|
|
108
|
+
return S(t) && (e.href = `mailto:${e.href}`), e.anchor && (e.href = `${e.href}#${e.anchor}`, delete e.anchor), {
|
|
146
109
|
tag: [
|
|
147
110
|
{
|
|
148
111
|
tag: "a",
|
|
149
|
-
attrs:
|
|
112
|
+
attrs: e
|
|
150
113
|
}
|
|
151
114
|
]
|
|
152
115
|
};
|
|
153
|
-
},
|
|
116
|
+
}, Y = (n) => ({
|
|
154
117
|
tag: [
|
|
155
118
|
{
|
|
156
119
|
tag: "span",
|
|
157
|
-
attrs:
|
|
120
|
+
attrs: n.attrs
|
|
158
121
|
}
|
|
159
122
|
]
|
|
160
|
-
}),
|
|
123
|
+
}), K = {
|
|
161
124
|
nodes: {
|
|
162
|
-
horizontal_rule:
|
|
163
|
-
blockquote:
|
|
164
|
-
bullet_list:
|
|
165
|
-
code_block:
|
|
166
|
-
hard_break:
|
|
167
|
-
heading:
|
|
168
|
-
image:
|
|
125
|
+
horizontal_rule: O,
|
|
126
|
+
blockquote: P,
|
|
127
|
+
bullet_list: C,
|
|
128
|
+
code_block: I,
|
|
129
|
+
hard_break: A,
|
|
130
|
+
heading: N,
|
|
131
|
+
image: M,
|
|
169
132
|
list_item: L,
|
|
170
133
|
ordered_list: U,
|
|
171
134
|
paragraph: H
|
|
172
135
|
},
|
|
173
136
|
marks: {
|
|
174
|
-
bold:
|
|
175
|
-
strike:
|
|
176
|
-
underline:
|
|
177
|
-
strong:
|
|
178
|
-
code:
|
|
179
|
-
italic:
|
|
180
|
-
link:
|
|
181
|
-
styled:
|
|
182
|
-
}
|
|
183
|
-
},
|
|
184
|
-
const
|
|
137
|
+
bold: B,
|
|
138
|
+
strike: q,
|
|
139
|
+
underline: D,
|
|
140
|
+
strong: J,
|
|
141
|
+
code: F,
|
|
142
|
+
italic: V,
|
|
143
|
+
link: z,
|
|
144
|
+
styled: Y
|
|
145
|
+
}
|
|
146
|
+
}, Q = function(n) {
|
|
147
|
+
const e = {
|
|
185
148
|
"&": "&",
|
|
186
149
|
"<": "<",
|
|
187
150
|
">": ">",
|
|
188
151
|
'"': """,
|
|
189
152
|
"'": "'"
|
|
190
|
-
},
|
|
191
|
-
return
|
|
153
|
+
}, t = /[&<>"']/g, s = RegExp(t.source);
|
|
154
|
+
return n && s.test(n) ? n.replace(t, (r) => e[r]) : n;
|
|
192
155
|
};
|
|
193
|
-
class
|
|
194
|
-
constructor(
|
|
195
|
-
l(this, "marks");
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
e += this.renderNode(s);
|
|
210
|
-
}), e;
|
|
156
|
+
class g {
|
|
157
|
+
constructor(e) {
|
|
158
|
+
l(this, "marks"), l(this, "nodes"), e || (e = K), this.marks = e.marks || [], this.nodes = e.nodes || [];
|
|
159
|
+
}
|
|
160
|
+
addNode(e, t) {
|
|
161
|
+
this.nodes[e] = t;
|
|
162
|
+
}
|
|
163
|
+
addMark(e, t) {
|
|
164
|
+
this.marks[e] = t;
|
|
165
|
+
}
|
|
166
|
+
render(e) {
|
|
167
|
+
if (e && e.content && Array.isArray(e.content)) {
|
|
168
|
+
let t = "";
|
|
169
|
+
return e.content.forEach((s) => {
|
|
170
|
+
t += this.renderNode(s);
|
|
171
|
+
}), t;
|
|
211
172
|
}
|
|
212
|
-
return console.warn(
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
173
|
+
return console.warn(
|
|
174
|
+
"The render method must receive an object with a content field, which is an array"
|
|
175
|
+
), "";
|
|
176
|
+
}
|
|
177
|
+
renderNode(e) {
|
|
178
|
+
const t = [];
|
|
179
|
+
e.marks && e.marks.forEach((r) => {
|
|
180
|
+
const o = this.getMatchingMark(r);
|
|
181
|
+
o && t.push(this.renderOpeningTag(o.tag));
|
|
219
182
|
});
|
|
220
|
-
const s = this.getMatchingNode(
|
|
221
|
-
return s && s.tag &&
|
|
222
|
-
|
|
223
|
-
}) :
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
}),
|
|
227
|
-
}
|
|
228
|
-
renderTag(
|
|
229
|
-
return
|
|
230
|
-
if (
|
|
231
|
-
return `<${
|
|
183
|
+
const s = this.getMatchingNode(e);
|
|
184
|
+
return s && s.tag && t.push(this.renderOpeningTag(s.tag)), e.content ? e.content.forEach((r) => {
|
|
185
|
+
t.push(this.renderNode(r));
|
|
186
|
+
}) : e.text ? t.push(Q(e.text)) : s && s.singleTag ? t.push(this.renderTag(s.singleTag, " /")) : s && s.html && t.push(s.html), s && s.tag && t.push(this.renderClosingTag(s.tag)), e.marks && e.marks.slice(0).reverse().forEach((r) => {
|
|
187
|
+
const o = this.getMatchingMark(r);
|
|
188
|
+
o && t.push(this.renderClosingTag(o.tag));
|
|
189
|
+
}), t.join("");
|
|
190
|
+
}
|
|
191
|
+
renderTag(e, t) {
|
|
192
|
+
return e.constructor === String ? `<${e}${t}>` : e.map((s) => {
|
|
193
|
+
if (s.constructor === String)
|
|
194
|
+
return `<${s}${t}>`;
|
|
232
195
|
{
|
|
233
|
-
let
|
|
234
|
-
if (
|
|
235
|
-
for (const
|
|
236
|
-
const
|
|
237
|
-
|
|
196
|
+
let r = `<${s.tag}`;
|
|
197
|
+
if (s.attrs)
|
|
198
|
+
for (const o in s.attrs) {
|
|
199
|
+
const i = s.attrs[o];
|
|
200
|
+
i !== null && (r += ` ${o}="${i}"`);
|
|
238
201
|
}
|
|
239
|
-
return `${
|
|
202
|
+
return `${r}${t}>`;
|
|
240
203
|
}
|
|
241
204
|
}).join("");
|
|
242
205
|
}
|
|
243
|
-
renderOpeningTag(
|
|
244
|
-
return this.renderTag(
|
|
206
|
+
renderOpeningTag(e) {
|
|
207
|
+
return this.renderTag(e, "");
|
|
245
208
|
}
|
|
246
|
-
renderClosingTag(
|
|
247
|
-
return
|
|
209
|
+
renderClosingTag(e) {
|
|
210
|
+
return e.constructor === String ? `</${e}>` : e.slice(0).reverse().map((t) => t.constructor === String ? `</${t}>` : `</${t.tag}>`).join("");
|
|
248
211
|
}
|
|
249
|
-
getMatchingNode(
|
|
250
|
-
const
|
|
251
|
-
if (typeof
|
|
252
|
-
return e
|
|
212
|
+
getMatchingNode(e) {
|
|
213
|
+
const t = this.nodes[e.type];
|
|
214
|
+
if (typeof t == "function")
|
|
215
|
+
return t(e);
|
|
253
216
|
}
|
|
254
|
-
getMatchingMark(
|
|
255
|
-
const
|
|
256
|
-
if (typeof
|
|
257
|
-
return e
|
|
217
|
+
getMatchingMark(e) {
|
|
218
|
+
const t = this.marks[e.type];
|
|
219
|
+
if (typeof t == "function")
|
|
220
|
+
return t(e);
|
|
258
221
|
}
|
|
259
222
|
}
|
|
260
223
|
class T {
|
|
261
224
|
constructor() {
|
|
262
|
-
l(this, "isCDNUrl", (
|
|
263
|
-
|
|
264
|
-
per_page:
|
|
225
|
+
l(this, "isCDNUrl", (e = "") => e.indexOf("/cdn/") > -1), l(this, "getOptionsPage", (e, t = 25, s = 1) => ({
|
|
226
|
+
...e,
|
|
227
|
+
per_page: t,
|
|
265
228
|
page: s
|
|
266
|
-
}))
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
l(this, "
|
|
270
|
-
const s = Math.abs(e - t) || 0, r = t < e ? 1 : -1;
|
|
271
|
-
return this.arrayFrom(s, (n, c) => c * r + t);
|
|
272
|
-
});
|
|
273
|
-
l(this, "asyncMap", async (t, e) => Promise.all(t.map(e)));
|
|
274
|
-
l(this, "flatMap", (t = [], e) => t.map(e).reduce((s, r) => [...s, ...r], []));
|
|
229
|
+
})), l(this, "delay", (e) => new Promise((t) => setTimeout(t, e))), l(this, "arrayFrom", (e = 0, t) => [...Array(e)].map(t)), l(this, "range", (e = 0, t = e) => {
|
|
230
|
+
const s = Math.abs(t - e) || 0, r = e < t ? 1 : -1;
|
|
231
|
+
return this.arrayFrom(s, (o, i) => i * r + e);
|
|
232
|
+
}), l(this, "asyncMap", async (e, t) => Promise.all(e.map(t))), l(this, "flatMap", (e = [], t) => e.map(t).reduce((s, r) => [...s, ...r], []));
|
|
275
233
|
}
|
|
276
|
-
stringify(
|
|
234
|
+
stringify(e, t, s) {
|
|
277
235
|
const r = [];
|
|
278
|
-
for (const
|
|
279
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
236
|
+
for (const o in e) {
|
|
237
|
+
if (!Object.prototype.hasOwnProperty.call(e, o))
|
|
280
238
|
continue;
|
|
281
|
-
const
|
|
282
|
-
let
|
|
283
|
-
typeof
|
|
239
|
+
const i = e[o], a = s ? "" : encodeURIComponent(o);
|
|
240
|
+
let c;
|
|
241
|
+
typeof i == "object" ? c = this.stringify(
|
|
242
|
+
i,
|
|
243
|
+
t ? t + encodeURIComponent("[" + a + "]") : a,
|
|
244
|
+
Array.isArray(i)
|
|
245
|
+
) : c = (t ? t + encodeURIComponent("[" + a + "]") : a) + "=" + encodeURIComponent(i), r.push(c);
|
|
284
246
|
}
|
|
285
247
|
return r.join("&");
|
|
286
248
|
}
|
|
287
249
|
}
|
|
288
|
-
var f = /* @__PURE__ */ ((i) => (i.GET = "get", i.DELETE = "delete", i.POST = "post", i.PUT = "put", i))(f || {});
|
|
289
250
|
class G {
|
|
290
|
-
constructor(
|
|
291
|
-
l(this, "baseURL");
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return this.url = t, this.parameters = e, this._methodHandler(f.PUT);
|
|
308
|
-
}
|
|
309
|
-
delete(t, e) {
|
|
310
|
-
return this.url = t, this.parameters = e, this._methodHandler(f.DELETE);
|
|
311
|
-
}
|
|
312
|
-
async _responseHandler(t) {
|
|
313
|
-
const e = [], s = {
|
|
251
|
+
constructor(e) {
|
|
252
|
+
l(this, "baseURL"), l(this, "timeout"), l(this, "headers"), l(this, "responseInterceptor"), l(this, "fetch"), l(this, "ejectInterceptor"), l(this, "url"), l(this, "parameters"), this.baseURL = e.baseURL, this.headers = e.headers || [], this.timeout = e != null && e.timeout ? e.timeout * 1e3 : 0, this.responseInterceptor = e.responseInterceptor, this.fetch = (...t) => e.fetch ? e.fetch(...t) : fetch(...t), this.ejectInterceptor = !1, this.url = "", this.parameters = {};
|
|
253
|
+
}
|
|
254
|
+
get(e, t) {
|
|
255
|
+
return this.url = e, this.parameters = t, this._methodHandler("get");
|
|
256
|
+
}
|
|
257
|
+
post(e, t) {
|
|
258
|
+
return this.url = e, this.parameters = t, this._methodHandler("post");
|
|
259
|
+
}
|
|
260
|
+
put(e, t) {
|
|
261
|
+
return this.url = e, this.parameters = t, this._methodHandler("put");
|
|
262
|
+
}
|
|
263
|
+
delete(e, t) {
|
|
264
|
+
return this.url = e, this.parameters = t, this._methodHandler("delete");
|
|
265
|
+
}
|
|
266
|
+
async _responseHandler(e) {
|
|
267
|
+
const t = [], s = {
|
|
314
268
|
data: {},
|
|
315
269
|
headers: {},
|
|
316
270
|
status: 0,
|
|
317
271
|
statusText: ""
|
|
318
272
|
};
|
|
319
|
-
await
|
|
273
|
+
await e.json().then((r) => {
|
|
320
274
|
s.data = r;
|
|
321
275
|
});
|
|
322
|
-
for (const r of
|
|
323
|
-
|
|
324
|
-
return s.headers =
|
|
276
|
+
for (const r of e.headers.entries())
|
|
277
|
+
t[r[0]] = r[1];
|
|
278
|
+
return s.headers = { ...t }, s.status = e.status, s.statusText = e.statusText, s;
|
|
325
279
|
}
|
|
326
|
-
async _methodHandler(
|
|
327
|
-
const
|
|
280
|
+
async _methodHandler(e) {
|
|
281
|
+
const t = new URL(`${this.baseURL}${this.url}`);
|
|
328
282
|
let s = null;
|
|
329
|
-
if (
|
|
330
|
-
const
|
|
331
|
-
|
|
283
|
+
if (e === "get") {
|
|
284
|
+
const a = new T();
|
|
285
|
+
t.search = a.stringify(this.parameters);
|
|
332
286
|
} else
|
|
333
287
|
s = JSON.stringify(this.parameters);
|
|
334
|
-
const r = new AbortController(), { signal:
|
|
288
|
+
const r = new AbortController(), { signal: o } = r;
|
|
289
|
+
let i;
|
|
290
|
+
this.timeout && (i = setTimeout(() => r.abort(), this.timeout));
|
|
335
291
|
try {
|
|
336
|
-
const
|
|
337
|
-
method:
|
|
292
|
+
const a = await this.fetch(`${t}`, {
|
|
293
|
+
method: e,
|
|
338
294
|
headers: this.headers,
|
|
339
295
|
body: s,
|
|
340
|
-
signal:
|
|
296
|
+
signal: o
|
|
341
297
|
});
|
|
342
|
-
clearTimeout(
|
|
343
|
-
const
|
|
344
|
-
return this.responseInterceptor && !this.ejectInterceptor ? this._statusHandler(this.responseInterceptor(
|
|
345
|
-
} catch (
|
|
346
|
-
return
|
|
298
|
+
this.timeout && clearTimeout(i);
|
|
299
|
+
const c = await this._responseHandler(a);
|
|
300
|
+
return this.responseInterceptor && !this.ejectInterceptor ? this._statusHandler(this.responseInterceptor(c)) : this._statusHandler(c);
|
|
301
|
+
} catch (a) {
|
|
302
|
+
return a;
|
|
347
303
|
}
|
|
348
304
|
}
|
|
349
305
|
eject() {
|
|
350
|
-
this.ejectInterceptor =
|
|
306
|
+
this.ejectInterceptor = !0;
|
|
351
307
|
}
|
|
352
|
-
_statusHandler(
|
|
353
|
-
if (/20[01]/g.test(`${
|
|
354
|
-
return
|
|
308
|
+
_statusHandler(e) {
|
|
309
|
+
if (/20[01]/g.test(`${e.status}`))
|
|
310
|
+
return e;
|
|
355
311
|
throw {
|
|
356
|
-
message: new Error(
|
|
357
|
-
response:
|
|
312
|
+
message: new Error(e.statusText || `status: ${e.status}`),
|
|
313
|
+
response: e
|
|
358
314
|
};
|
|
359
315
|
}
|
|
360
316
|
}
|
|
361
317
|
let p = {};
|
|
362
318
|
const d = {};
|
|
363
|
-
class
|
|
364
|
-
constructor(
|
|
365
|
-
l(this, "client")
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
l(this, "accessToken");
|
|
369
|
-
l(this, "cache");
|
|
370
|
-
l(this, "helpers");
|
|
371
|
-
l(this, "relations");
|
|
372
|
-
l(this, "links");
|
|
373
|
-
l(this, "richTextResolver");
|
|
374
|
-
l(this, "resolveNestedRelations");
|
|
375
|
-
if (!e) {
|
|
376
|
-
const n = t.region ? `-${t.region}` : "", c = t.https === false ? "http" : "https";
|
|
377
|
-
t.oauthToken ? e = `${c}://api${n}.storyblok.com/v1` : e = `${c}://api${n}.storyblok.com/v2`;
|
|
319
|
+
class W {
|
|
320
|
+
constructor(e, t) {
|
|
321
|
+
if (l(this, "client"), l(this, "maxRetries"), l(this, "throttle"), l(this, "accessToken"), l(this, "cache"), l(this, "helpers"), l(this, "relations"), l(this, "links"), l(this, "richTextResolver"), l(this, "resolveNestedRelations"), !t) {
|
|
322
|
+
const o = e.region ? `-${e.region}` : "", i = e.https === !1 ? "http" : "https";
|
|
323
|
+
e.oauthToken ? t = `${i}://api${o}.storyblok.com/v1` : t = `${i}://api${o}.storyblok.com/v2`;
|
|
378
324
|
}
|
|
379
|
-
const s =
|
|
325
|
+
const s = new Headers();
|
|
326
|
+
s.set("Content-Type", "application/json"), s.set("Accept", "application/json"), s.forEach((o, i) => {
|
|
327
|
+
e.headers && e.headers[i] && s.set(i, e.headers[i]);
|
|
328
|
+
});
|
|
380
329
|
let r = 5;
|
|
381
|
-
|
|
382
|
-
baseURL:
|
|
383
|
-
timeout:
|
|
330
|
+
e.oauthToken && (s.set("Authorization", e.oauthToken), r = 3), e.rateLimit && (r = e.rateLimit), e.richTextSchema ? this.richTextResolver = new g(e.richTextSchema) : this.richTextResolver = new g(), e.componentResolver && this.setComponentResolver(e.componentResolver), this.maxRetries = e.maxRetries, this.throttle = x(this.throttledRequest, r, 1e3), this.accessToken = e.accessToken || "", this.relations = {}, this.links = {}, this.cache = e.cache || { clear: "manual" }, this.helpers = new T(), this.resolveNestedRelations = !1, this.client = new G({
|
|
331
|
+
baseURL: t,
|
|
332
|
+
timeout: e.timeout || 0,
|
|
384
333
|
headers: s,
|
|
385
|
-
responseInterceptor:
|
|
334
|
+
responseInterceptor: e.responseInterceptor,
|
|
335
|
+
fetch: e.fetch
|
|
386
336
|
});
|
|
387
337
|
}
|
|
388
|
-
setComponentResolver(
|
|
389
|
-
this.richTextResolver.addNode("blok", (
|
|
338
|
+
setComponentResolver(e) {
|
|
339
|
+
this.richTextResolver.addNode("blok", (t) => {
|
|
390
340
|
let s = "";
|
|
391
|
-
return
|
|
392
|
-
s +=
|
|
341
|
+
return t.attrs.body.forEach((r) => {
|
|
342
|
+
s += e(r.component, r);
|
|
393
343
|
}), {
|
|
394
344
|
html: s
|
|
395
345
|
};
|
|
396
346
|
});
|
|
397
347
|
}
|
|
398
|
-
parseParams(
|
|
399
|
-
return
|
|
348
|
+
parseParams(e) {
|
|
349
|
+
return e.version || (e.version = "published"), e.token || (e.token = this.getToken()), e.cv || (e.cv = d[e.token]), Array.isArray(e.resolve_relations) && (e.resolve_relations = e.resolve_relations.join(",")), e;
|
|
400
350
|
}
|
|
401
|
-
factoryParamOptions(
|
|
402
|
-
return this.helpers.isCDNUrl(
|
|
351
|
+
factoryParamOptions(e, t) {
|
|
352
|
+
return this.helpers.isCDNUrl(e) ? this.parseParams(t) : t;
|
|
403
353
|
}
|
|
404
|
-
makeRequest(
|
|
405
|
-
const
|
|
406
|
-
|
|
354
|
+
makeRequest(e, t, s, r) {
|
|
355
|
+
const o = this.factoryParamOptions(
|
|
356
|
+
e,
|
|
357
|
+
this.helpers.getOptionsPage(t, s, r)
|
|
358
|
+
);
|
|
359
|
+
return this.cacheResponse(e, o);
|
|
407
360
|
}
|
|
408
|
-
get(
|
|
409
|
-
|
|
410
|
-
const s = `/${
|
|
361
|
+
get(e, t) {
|
|
362
|
+
t || (t = {});
|
|
363
|
+
const s = `/${e}`, r = this.factoryParamOptions(s, t);
|
|
411
364
|
return this.cacheResponse(s, r);
|
|
412
365
|
}
|
|
413
|
-
async getAll(
|
|
414
|
-
const r = (
|
|
415
|
-
|
|
366
|
+
async getAll(e, t, s) {
|
|
367
|
+
const r = (t == null ? void 0 : t.per_page) || 25, o = `/${e}`, i = o.split("/"), a = s || i[i.length - 1], c = 1, h = await this.makeRequest(o, t, r, c), u = h.total ? Math.ceil(h.total / r) : 1, f = await this.helpers.asyncMap(
|
|
368
|
+
this.helpers.range(c, u),
|
|
369
|
+
(m) => this.makeRequest(o, t, r, m + 1)
|
|
370
|
+
);
|
|
371
|
+
return this.helpers.flatMap(
|
|
372
|
+
[h, ...f],
|
|
373
|
+
(m) => Object.values(m.data[a])
|
|
374
|
+
);
|
|
416
375
|
}
|
|
417
|
-
post(
|
|
418
|
-
const s = `/${
|
|
419
|
-
return Promise.resolve(this.throttle("post", s,
|
|
376
|
+
post(e, t) {
|
|
377
|
+
const s = `/${e}`;
|
|
378
|
+
return Promise.resolve(this.throttle("post", s, t));
|
|
420
379
|
}
|
|
421
|
-
put(
|
|
422
|
-
const s = `/${
|
|
423
|
-
return Promise.resolve(this.throttle("put", s,
|
|
380
|
+
put(e, t) {
|
|
381
|
+
const s = `/${e}`;
|
|
382
|
+
return Promise.resolve(this.throttle("put", s, t));
|
|
424
383
|
}
|
|
425
|
-
delete(
|
|
426
|
-
const s = `/${
|
|
427
|
-
return Promise.resolve(this.throttle("delete", s,
|
|
384
|
+
delete(e, t) {
|
|
385
|
+
const s = `/${e}`;
|
|
386
|
+
return Promise.resolve(this.throttle("delete", s, t));
|
|
428
387
|
}
|
|
429
|
-
getStories(
|
|
430
|
-
return this.get("cdn/stories",
|
|
388
|
+
getStories(e) {
|
|
389
|
+
return this.get("cdn/stories", e);
|
|
431
390
|
}
|
|
432
|
-
getStory(
|
|
433
|
-
return this.get(`cdn/stories/${
|
|
391
|
+
getStory(e, t) {
|
|
392
|
+
return this.get(`cdn/stories/${e}`, t);
|
|
434
393
|
}
|
|
435
394
|
getToken() {
|
|
436
395
|
return this.accessToken;
|
|
@@ -438,123 +397,129 @@ class Q {
|
|
|
438
397
|
ejectInterceptor() {
|
|
439
398
|
this.client.eject();
|
|
440
399
|
}
|
|
441
|
-
_cleanCopy(
|
|
442
|
-
return JSON.parse(JSON.stringify(
|
|
400
|
+
_cleanCopy(e) {
|
|
401
|
+
return JSON.parse(JSON.stringify(e));
|
|
443
402
|
}
|
|
444
|
-
_insertLinks(
|
|
445
|
-
const s = t
|
|
403
|
+
_insertLinks(e, t) {
|
|
404
|
+
const s = e[t];
|
|
446
405
|
s && s.fieldtype == "multilink" && s.linktype == "story" && typeof s.id == "string" && this.links[s.id] ? s.story = this._cleanCopy(this.links[s.id]) : s && s.linktype === "story" && typeof s.uuid == "string" && this.links[s.uuid] && (s.story = this._cleanCopy(this.links[s.uuid]));
|
|
447
406
|
}
|
|
448
|
-
_insertRelations(
|
|
449
|
-
if (s.indexOf(`${
|
|
450
|
-
if (typeof t
|
|
451
|
-
this.relations[t
|
|
452
|
-
else if (t
|
|
407
|
+
_insertRelations(e, t, s) {
|
|
408
|
+
if (s.indexOf(`${e.component}.${t}`) > -1) {
|
|
409
|
+
if (typeof e[t] == "string")
|
|
410
|
+
this.relations[e[t]] && (e[t] = this._cleanCopy(this.relations[e[t]]));
|
|
411
|
+
else if (e[t] && e[t].constructor === Array) {
|
|
453
412
|
const r = [];
|
|
454
|
-
t
|
|
455
|
-
this.relations[
|
|
456
|
-
}), t
|
|
413
|
+
e[t].forEach((o) => {
|
|
414
|
+
this.relations[o] && r.push(this._cleanCopy(this.relations[o]));
|
|
415
|
+
}), e[t] = r;
|
|
457
416
|
}
|
|
458
417
|
}
|
|
459
418
|
}
|
|
460
|
-
iterateTree(
|
|
419
|
+
iterateTree(e, t) {
|
|
461
420
|
const s = (r) => {
|
|
462
421
|
if (r != null) {
|
|
463
422
|
if (r.constructor === Array)
|
|
464
|
-
for (let
|
|
465
|
-
s(r[
|
|
423
|
+
for (let o = 0; o < r.length; o++)
|
|
424
|
+
s(r[o]);
|
|
466
425
|
else if (r.constructor === Object) {
|
|
467
426
|
if (r._stopResolving)
|
|
468
427
|
return;
|
|
469
|
-
for (const
|
|
470
|
-
(r.component && r._uid || r.type === "link") && (this._insertRelations(
|
|
428
|
+
for (const o in r)
|
|
429
|
+
(r.component && r._uid || r.type === "link") && (this._insertRelations(
|
|
430
|
+
r,
|
|
431
|
+
o,
|
|
432
|
+
t
|
|
433
|
+
), this._insertLinks(r, o)), s(r[o]);
|
|
471
434
|
}
|
|
472
435
|
}
|
|
473
436
|
};
|
|
474
|
-
s(
|
|
437
|
+
s(e.content);
|
|
475
438
|
}
|
|
476
|
-
async resolveLinks(
|
|
439
|
+
async resolveLinks(e, t) {
|
|
477
440
|
let s = [];
|
|
478
|
-
if (
|
|
479
|
-
const r =
|
|
480
|
-
for (let
|
|
481
|
-
const
|
|
482
|
-
|
|
441
|
+
if (e.link_uuids) {
|
|
442
|
+
const r = e.link_uuids.length, o = [], i = 50;
|
|
443
|
+
for (let a = 0; a < r; a += i) {
|
|
444
|
+
const c = Math.min(r, a + i);
|
|
445
|
+
o.push(e.link_uuids.slice(a, c));
|
|
483
446
|
}
|
|
484
|
-
for (let
|
|
447
|
+
for (let a = 0; a < o.length; a++)
|
|
485
448
|
(await this.getStories({
|
|
486
|
-
per_page:
|
|
487
|
-
language:
|
|
488
|
-
version:
|
|
489
|
-
by_uuids:
|
|
490
|
-
})).data.stories.forEach(
|
|
491
|
-
|
|
492
|
-
|
|
449
|
+
per_page: i,
|
|
450
|
+
language: t.language,
|
|
451
|
+
version: t.version,
|
|
452
|
+
by_uuids: o[a].join(",")
|
|
453
|
+
})).data.stories.forEach(
|
|
454
|
+
(c) => {
|
|
455
|
+
s.push(c);
|
|
456
|
+
}
|
|
457
|
+
);
|
|
493
458
|
} else
|
|
494
|
-
s =
|
|
459
|
+
s = e.links;
|
|
495
460
|
s.forEach((r) => {
|
|
496
|
-
this.links[r.uuid] =
|
|
461
|
+
this.links[r.uuid] = { ...r, _stopResolving: !0 };
|
|
497
462
|
});
|
|
498
463
|
}
|
|
499
|
-
async resolveRelations(
|
|
464
|
+
async resolveRelations(e, t) {
|
|
500
465
|
let s = [];
|
|
501
|
-
if (
|
|
502
|
-
const r =
|
|
503
|
-
for (let
|
|
504
|
-
const
|
|
505
|
-
|
|
466
|
+
if (e.rel_uuids) {
|
|
467
|
+
const r = e.rel_uuids.length, o = [], i = 50;
|
|
468
|
+
for (let a = 0; a < r; a += i) {
|
|
469
|
+
const c = Math.min(r, a + i);
|
|
470
|
+
o.push(e.rel_uuids.slice(a, c));
|
|
506
471
|
}
|
|
507
|
-
for (let
|
|
472
|
+
for (let a = 0; a < o.length; a++)
|
|
508
473
|
(await this.getStories({
|
|
509
|
-
per_page:
|
|
510
|
-
language:
|
|
511
|
-
version:
|
|
512
|
-
by_uuids:
|
|
513
|
-
})).data.stories.forEach((
|
|
514
|
-
s.push(
|
|
474
|
+
per_page: i,
|
|
475
|
+
language: t.language,
|
|
476
|
+
version: t.version,
|
|
477
|
+
by_uuids: o[a].join(",")
|
|
478
|
+
})).data.stories.forEach((c) => {
|
|
479
|
+
s.push(c);
|
|
515
480
|
});
|
|
516
481
|
} else
|
|
517
|
-
s =
|
|
482
|
+
s = e.rels;
|
|
518
483
|
s && s.length > 0 && s.forEach((r) => {
|
|
519
|
-
this.relations[r.uuid] =
|
|
484
|
+
this.relations[r.uuid] = { ...r, _stopResolving: !0 };
|
|
520
485
|
});
|
|
521
486
|
}
|
|
522
|
-
async resolveStories(
|
|
523
|
-
var
|
|
524
|
-
let
|
|
525
|
-
if (typeof
|
|
526
|
-
for (const
|
|
527
|
-
this.iterateTree(this.relations[
|
|
528
|
-
|
|
529
|
-
this.iterateTree(
|
|
487
|
+
async resolveStories(e, t) {
|
|
488
|
+
var s, r;
|
|
489
|
+
let o = [];
|
|
490
|
+
if (typeof t.resolve_relations < "u" && t.resolve_relations.length > 0 && (typeof t.resolve_relations == "string" && (o = t.resolve_relations.split(",")), await this.resolveRelations(e, t)), t.resolve_links && ["1", "story", "url"].indexOf(t.resolve_links) > -1 && ((s = e.links) != null && s.length || (r = e.link_uuids) != null && r.length) && await this.resolveLinks(e, t), this.resolveNestedRelations)
|
|
491
|
+
for (const i in this.relations)
|
|
492
|
+
this.iterateTree(this.relations[i], o);
|
|
493
|
+
e.story ? this.iterateTree(e.story, o) : e.stories.forEach((i) => {
|
|
494
|
+
this.iterateTree(i, o);
|
|
530
495
|
});
|
|
531
496
|
}
|
|
532
|
-
cacheResponse(
|
|
533
|
-
return new Promise((r,
|
|
534
|
-
const
|
|
535
|
-
if (this.cache.clear === "auto" &&
|
|
536
|
-
const
|
|
537
|
-
if (
|
|
538
|
-
return r(
|
|
497
|
+
cacheResponse(e, t, s) {
|
|
498
|
+
return new Promise((r, o) => {
|
|
499
|
+
const i = this.helpers.stringify({ url: e, params: t }), a = this.cacheProvider();
|
|
500
|
+
if (this.cache.clear === "auto" && t.version === "draft" && this.flushCache(), t.version === "published" && e != "/cdn/spaces/me") {
|
|
501
|
+
const c = a.get(i);
|
|
502
|
+
if (c)
|
|
503
|
+
return r(c);
|
|
539
504
|
}
|
|
540
505
|
try {
|
|
541
506
|
(async () => {
|
|
542
|
-
var
|
|
543
|
-
const
|
|
544
|
-
let
|
|
545
|
-
if ((
|
|
546
|
-
perPage:
|
|
547
|
-
total:
|
|
548
|
-
})),
|
|
549
|
-
return
|
|
550
|
-
(
|
|
507
|
+
var c;
|
|
508
|
+
const h = await this.throttle("get", e, t);
|
|
509
|
+
let u = { data: h.data, headers: h.headers };
|
|
510
|
+
if ((c = h.headers) != null && c["per-page"] && (u = Object.assign({}, u, {
|
|
511
|
+
perPage: h.headers["per-page"] ? parseInt(h.headers["per-page"]) : 0,
|
|
512
|
+
total: h.headers["per-page"] ? parseInt(h.headers.total) : 0
|
|
513
|
+
})), h.status != 200)
|
|
514
|
+
return o(h);
|
|
515
|
+
(u.data.story || u.data.stories) && await this.resolveStories(u.data, t), t.version === "published" && e != "/cdn/spaces/me" && a.set(i, u), u.data.cv && t.token && (t.version == "draft" && d[t.token] != u.data.cv && this.flushCache(), d[t.token] = u.data.cv), r(u);
|
|
551
516
|
})();
|
|
552
517
|
} catch {
|
|
553
518
|
}
|
|
554
519
|
});
|
|
555
520
|
}
|
|
556
|
-
throttledRequest(
|
|
557
|
-
return this.client[
|
|
521
|
+
throttledRequest(e, t, s) {
|
|
522
|
+
return this.client[e](t, s);
|
|
558
523
|
}
|
|
559
524
|
cacheVersions() {
|
|
560
525
|
return d;
|
|
@@ -562,21 +527,21 @@ class Q {
|
|
|
562
527
|
cacheVersion() {
|
|
563
528
|
return d[this.accessToken];
|
|
564
529
|
}
|
|
565
|
-
setCacheVersion(
|
|
566
|
-
this.accessToken && (d[this.accessToken] =
|
|
530
|
+
setCacheVersion(e) {
|
|
531
|
+
this.accessToken && (d[this.accessToken] = e);
|
|
567
532
|
}
|
|
568
533
|
cacheProvider() {
|
|
569
534
|
switch (this.cache.type) {
|
|
570
535
|
case "memory":
|
|
571
536
|
return {
|
|
572
|
-
get(
|
|
573
|
-
return p[
|
|
537
|
+
get(e) {
|
|
538
|
+
return p[e];
|
|
574
539
|
},
|
|
575
540
|
getAll() {
|
|
576
541
|
return p;
|
|
577
542
|
},
|
|
578
|
-
set(
|
|
579
|
-
p[
|
|
543
|
+
set(e, t) {
|
|
544
|
+
p[e] = t;
|
|
580
545
|
},
|
|
581
546
|
flush() {
|
|
582
547
|
p = {};
|
|
@@ -599,105 +564,88 @@ class Q {
|
|
|
599
564
|
return this.cacheProvider().flush(), this;
|
|
600
565
|
}
|
|
601
566
|
}
|
|
602
|
-
const
|
|
603
|
-
const { apiOptions } =
|
|
604
|
-
if (!
|
|
605
|
-
console.error(
|
|
567
|
+
const X = (n = {}) => {
|
|
568
|
+
const { apiOptions: e } = n;
|
|
569
|
+
if (!e.accessToken) {
|
|
570
|
+
console.error(
|
|
571
|
+
"You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication"
|
|
572
|
+
);
|
|
606
573
|
return;
|
|
607
574
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
var editable = (blok) => {
|
|
612
|
-
if (typeof blok !== "object" || typeof blok._editable === "undefined") {
|
|
575
|
+
return { storyblokApi: new W(e) };
|
|
576
|
+
}, Z = (n) => {
|
|
577
|
+
if (typeof n != "object" || typeof n._editable > "u")
|
|
613
578
|
return {};
|
|
614
|
-
|
|
615
|
-
|
|
579
|
+
const e = JSON.parse(
|
|
580
|
+
n._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, "")
|
|
581
|
+
);
|
|
616
582
|
return {
|
|
617
|
-
"data-blok-c": JSON.stringify(
|
|
618
|
-
"data-blok-uid":
|
|
583
|
+
"data-blok-c": JSON.stringify(e),
|
|
584
|
+
"data-blok-uid": e.id + "-" + e.uid
|
|
619
585
|
};
|
|
620
586
|
};
|
|
621
|
-
let
|
|
622
|
-
const
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
if (event.action === "input" && event.story.id === id) {
|
|
639
|
-
cb(event.story);
|
|
640
|
-
} else if ((event.action === "change" || event.action === "published") && event.storyId === id) {
|
|
641
|
-
window.location.reload();
|
|
642
|
-
}
|
|
587
|
+
let y;
|
|
588
|
+
const R = "https://app.storyblok.com/f/storyblok-v2-latest.js", ee = (n, e, t = {}) => {
|
|
589
|
+
if (!(typeof window > "u")) {
|
|
590
|
+
if (typeof window.storyblokRegisterEvent > "u") {
|
|
591
|
+
console.error(
|
|
592
|
+
"Storyblok Bridge is disabled. Please enable it to use it. Read https://github.com/storyblok/storyblok-js"
|
|
593
|
+
);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (!n) {
|
|
597
|
+
console.warn("Story ID is not defined. Please provide a valid ID.");
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
window.storyblokRegisterEvent(() => {
|
|
601
|
+
new window.StoryblokBridge(t).on(["input", "published", "change"], (r) => {
|
|
602
|
+
r.action === "input" && r.story.id === n ? e(r.story) : (r.action === "change" || r.action === "published") && r.storyId === n && window.location.reload();
|
|
603
|
+
});
|
|
643
604
|
});
|
|
644
|
-
});
|
|
645
|
-
};
|
|
646
|
-
const storyblokInit = (pluginOptions = {}) => {
|
|
647
|
-
const {
|
|
648
|
-
bridge,
|
|
649
|
-
accessToken,
|
|
650
|
-
use = [],
|
|
651
|
-
apiOptions = {},
|
|
652
|
-
richText = {}
|
|
653
|
-
} = pluginOptions;
|
|
654
|
-
apiOptions.accessToken = apiOptions.accessToken || accessToken;
|
|
655
|
-
const options = { bridge, apiOptions };
|
|
656
|
-
let result = {};
|
|
657
|
-
use.forEach((pluginFactory) => {
|
|
658
|
-
result = __spreadValues(__spreadValues({}, result), pluginFactory(options));
|
|
659
|
-
});
|
|
660
|
-
if (bridge !== false) {
|
|
661
|
-
loadBridge(bridgeLatest);
|
|
662
|
-
}
|
|
663
|
-
richTextResolver = new v(richText.schema);
|
|
664
|
-
if (richText.resolver) {
|
|
665
|
-
setComponentResolver(richTextResolver, richText.resolver);
|
|
666
605
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
606
|
+
}, te = (n = {}) => {
|
|
607
|
+
const {
|
|
608
|
+
bridge: e,
|
|
609
|
+
accessToken: t,
|
|
610
|
+
use: s = [],
|
|
611
|
+
apiOptions: r = {},
|
|
612
|
+
richText: o = {}
|
|
613
|
+
} = n;
|
|
614
|
+
r.accessToken = r.accessToken || t;
|
|
615
|
+
const i = { bridge: e, apiOptions: r };
|
|
616
|
+
let a = {};
|
|
617
|
+
return s.forEach((c) => {
|
|
618
|
+
a = { ...a, ...c(i) };
|
|
619
|
+
}), e !== !1 && w(R), y = new g(o.schema), o.resolver && _(y, o.resolver), a;
|
|
620
|
+
}, _ = (n, e) => {
|
|
621
|
+
n.addNode("blok", (t) => {
|
|
622
|
+
let s = "";
|
|
623
|
+
return t.attrs.body.forEach((r) => {
|
|
624
|
+
s += e(r.component, r);
|
|
625
|
+
}), {
|
|
626
|
+
html: s
|
|
677
627
|
};
|
|
678
628
|
});
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
629
|
+
}, se = (n, e, t) => {
|
|
630
|
+
let s = t || y;
|
|
631
|
+
if (!s) {
|
|
632
|
+
console.error(
|
|
633
|
+
"Please initialize the Storyblok SDK before calling the renderRichText function"
|
|
634
|
+
);
|
|
684
635
|
return;
|
|
685
636
|
}
|
|
686
|
-
|
|
687
|
-
return "";
|
|
688
|
-
} else if (!data) {
|
|
689
|
-
console.warn(`${data} is not a valid Richtext object. This might be because the value of the richtext field is empty.
|
|
637
|
+
return n === "" ? "" : n ? (e && (s = new g(e.schema), e.resolver && _(s, e.resolver)), s.render(n)) : (console.warn(`${n} is not a valid Richtext object. This might be because the value of the richtext field is empty.
|
|
690
638
|
|
|
691
|
-
For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`);
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
639
|
+
For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`), "");
|
|
640
|
+
}, re = () => w(R);
|
|
641
|
+
export {
|
|
642
|
+
g as RichTextResolver,
|
|
643
|
+
K as RichTextSchema,
|
|
644
|
+
X as apiPlugin,
|
|
645
|
+
re as loadStoryblokBridge,
|
|
646
|
+
ee as registerStoryblokBridge,
|
|
647
|
+
se as renderRichText,
|
|
648
|
+
Z as storyblokEditable,
|
|
649
|
+
te as storyblokInit,
|
|
650
|
+
ee as useStoryblokBridge
|
|
701
651
|
};
|
|
702
|
-
const loadStoryblokBridge = () => loadBridge(bridgeLatest);
|
|
703
|
-
export { v as RichTextResolver, j as RichTextSchema, apiFactory as apiPlugin, loadStoryblokBridge, useStoryblokBridge as registerStoryblokBridge, renderRichText, editable as storyblokEditable, storyblokInit, useStoryblokBridge };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storyblok/js",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"description": "SDK to integrate Storyblok into your project using JavaScript.",
|
|
5
5
|
"main": "./dist/storyblok-js.js",
|
|
6
6
|
"module": "./dist/storyblok-js.mjs",
|
|
@@ -19,25 +19,25 @@
|
|
|
19
19
|
"build": "vite build && tsc --project tsconfig.json",
|
|
20
20
|
"test": "npm run test:unit && npm run test:e2e",
|
|
21
21
|
"test:unit": "vitest run",
|
|
22
|
-
"test:e2e": "start-server-and-test cy:playground http
|
|
23
|
-
"test:e2e-watch": "start-server-and-test cy:playground http-get://localhost:
|
|
22
|
+
"test:e2e": "start-server-and-test cy:playground http://localhost:5173/ cy:run",
|
|
23
|
+
"test:e2e-watch": "start-server-and-test cy:playground http-get://localhost:5173/ cy:open",
|
|
24
24
|
"cy:playground": "npm run demo --prefix ../playground",
|
|
25
25
|
"cy:run": "cypress run",
|
|
26
26
|
"cy:open": "cypress open",
|
|
27
27
|
"prepublishOnly": "npm run build && cp ../README.md ./"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"storyblok-js-client": "^5.
|
|
30
|
+
"storyblok-js-client": "^5.3.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@tsconfig/recommended": "^1.0.1",
|
|
34
|
-
"cypress": "^
|
|
34
|
+
"cypress": "^12.3.0",
|
|
35
35
|
"eslint-plugin-cypress": "^2.12.1",
|
|
36
36
|
"eslint-plugin-jest": "^26.9.0",
|
|
37
37
|
"isomorphic-fetch": "^3.0.0",
|
|
38
|
-
"start-server-and-test": "^1.
|
|
39
|
-
"vite": "^
|
|
40
|
-
"vitest": "^0.
|
|
38
|
+
"start-server-and-test": "^1.15.2",
|
|
39
|
+
"vite": "^4.0.4",
|
|
40
|
+
"vitest": "^0.27.2"
|
|
41
41
|
},
|
|
42
42
|
"babel": {
|
|
43
43
|
"presets": [
|