@storyblok/js 2.0.12 → 2.0.14
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/storyblok-js.js +2 -2
- package/dist/storyblok-js.mjs +236 -226
- package/package.json +2 -2
package/dist/storyblok-js.js
CHANGED
@@ -1,3 +1,3 @@
|
|
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=o=>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=o,s.id="storyblok-javascript-bridge",s.onerror=r=>t(r),s.onload=r=>{v.forEach(n=>n()),p=!0,e(r)},document.getElementsByTagName("head")[0].appendChild(s)});var S=Object.defineProperty,x=(o,e,t)=>e in o?S(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,c=(o,e,t)=>(x(o,typeof e!="symbol"?e+"":e,t),t);function w(o){return!(o!==o||o===1/0||o===-1/0)}function P(o,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=[],n=0;const i=function(){n++;const l=setTimeout(function(){n--,s.length>0&&i(),r=r.filter(function(u){return u!==l})},t);r.indexOf(l)<0&&r.push(l);const h=s.shift();h.resolve(o.apply(h.self,h.args))},a=function(...l){const h=this;return new Promise(function(u,k){s.push({resolve:u,reject:k,args:l,self:h}),n<e&&i()})};return a.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},a}const O=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},I=o=>o==="email",C=()=>({singleTag:"hr"}),A=()=>({tag:"blockquote"}),M=()=>({tag:"ul"}),N=o=>({tag:["pre",{tag:"code",attrs:o.attrs}]}),L=()=>({singleTag:"br"}),U=o=>({tag:`h${o.attrs.level}`}),B=o=>({singleTag:[{tag:"img",attrs:O(o.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=o=>{const e={...o.attrs},{linktype:t="url"}=o.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=o=>({tag:[{tag:"span",attrs:o.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(o){const e={"&":"&","<":"<",">":">",'"':""","'":"'"},t=/[&<>"']/g,s=RegExp(t.source);return o&&s.test(o)?o.replace(t,r=>e[r]):o};class f{constructor(e){c(this,"marks"),c(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 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(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 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 i=s.attrs[n];i!==null&&(r+=` ${n}="${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(){c(this,"isCDNUrl",(e="")=>e.indexOf("/cdn/")>-1),c(this,"getOptionsPage",(e,t=25,s=1)=>({...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,i)=>i*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 i=e[n],a=s?"":encodeURIComponent(n);let l;typeof i=="object"?l=this.stringify(i,t?t+encodeURIComponent("["+a+"]"):a,Array.isArray(i)):l=(t?t+encodeURIComponent("["+a+"]"):a)+"="+encodeURIComponent(i),r.push(l)}return r.join("&")}}class X{constructor(e){c(this,"baseURL"),c(this,"timeout"),c(this,"headers"),c(this,"responseInterceptor"),c(this,"fetch"),c(this,"ejectInterceptor"),c(this,"url"),c(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:""};e.status!==204&&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){let t=`${this.baseURL}${this.url}`,s=null;if(e==="get"){const l=new _;t=`${this.baseURL}${this.url}?${l.stringify(this.parameters)}`}else s=JSON.stringify(this.parameters);const r=new URL(t),n=new AbortController,{signal:i}=n;let a;this.timeout&&(a=setTimeout(()=>n.abort(),this.timeout));try{const l=await this.fetch(`${r}`,{method:e,headers:this.headers,body:s,signal:i});this.timeout&&clearTimeout(a);const h=await this._responseHandler(l);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(h)):this._statusHandler(h)}catch(l){return l}}eject(){this.ejectInterceptor=!0}_statusHandler(e){const t=/20[0-6]/g;return new Promise((s,r)=>{if(t.test(`${e.status}`))return s(e);const n={message:new Error(e.statusText||`status: ${e.status}`),response:e};r(n)})}}let m={};const g={};class Z{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}`:"",i=e.https===!1?"http":"https";e.oauthToken?t=`${i}://api${n}.storyblok.com/v1`:t=`${i}://api${n}.storyblok.com/v2`}const s=new Headers;s.set("Content-Type","application/json"),s.set("Accept","application/json"),s.forEach((n,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=P(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 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}`,i=n.split("/"),a=s||i[i.length-1],l=1,h=await this.makeRequest(n,t,r,l),u=h.total?Math.ceil(h.total/r):1,k=await this.helpers.asyncMap(this.helpers.range(l,u),b=>this.makeRequest(n,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(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=[],i=50;for(let a=0;a<r;a+=i){const l=Math.min(r,a+i);n.push(e.link_uuids.slice(a,l))}for(let a=0;a<n.length;a++)(await this.getStories({per_page:i,language:t.language,version:t.version,by_uuids:n[a].join(",")})).data.stories.forEach(l=>{s.push(l)})}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,n=[],i=50;for(let a=0;a<r;a+=i){const l=Math.min(r,a+i);n.push(e.rel_uuids.slice(a,l))}for(let a=0;a<n.length;a++)(await this.getStories({per_page:i,language:t.language,version:t.version,by_uuids:n[a].join(",")})).data.stories.forEach(l=>{s.push(l)})}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 n=[];if(typeof t.resolve_relations<"u"&&t.resolve_relations.length>0&&(typeof t.resolve_relations=="string"&&(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&&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],n);e.story?this.iterateTree(e.story,n):e.stories.forEach(i=>{this.iterateTree(i,n)})}cacheResponse(e,t,s){return new Promise((r,n)=>{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 l=a.get(i);if(l)return r(l)}try{(async()=>{var l;const h=await this.throttle("get",e,t);let u={data:h.data,headers:h.headers};if((l=h.headers)!=null&&l["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 n(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=(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 Z(e)}},te=o=>{if(typeof o!="object"||typeof o._editable>"u")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 y;const $="https://app.storyblok.com/f/storyblok-v2-latest.js",j=(o,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(!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()})})}},se=(o={})=>{const{bridge:e,accessToken:t,use:s=[],apiOptions:r={},richText:n={}}=o;r.accessToken=r.accessToken||t;const i={bridge:e,apiOptions:r};let a={};return s.forEach(l=>{a={...a,...l(i)}}),e!==!1&&T($),y=new f(n.schema),n.resolver&&E(y,n.resolver),a},E=(o,e)=>{o.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})},re=(o,e,t)=>{let s=t||y;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return o===""?"":o?(e&&(s=new f(e.schema),e.resolver&&E(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=[],w=o=>new Promise((t,e)=>{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=o,s.id="storyblok-javascript-bridge",s.onerror=r=>e(r),s.onload=r=>{v.forEach(n=>n()),p=!0,t(r)},document.getElementsByTagName("head")[0].appendChild(s)});var E=Object.defineProperty,S=(o,t,e)=>t in o?E(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,c=(o,t,e)=>(S(o,typeof t!="symbol"?t+"":t,e),e);function T(o){return!(o!==o||o===1/0||o===-1/0)}function x(o,t,e){if(!T(t))throw new TypeError("Expected `limit` to be a finite number");if(!T(e))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],n=0;const i=function(){n++;const l=setTimeout(function(){n--,s.length>0&&i(),r=r.filter(function(u){return u!==l})},e);r.indexOf(l)<0&&r.push(l);const h=s.shift();h.resolve(o.apply(h.self,h.args))},a=function(...l){const h=this;return new Promise(function(u,k){s.push({resolve:u,reject:k,args:l,self:h}),n<t&&i()})};return a.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},a}const O=function(o,t){const e={};for(const s in o){const r=o[s];t.indexOf(s)>-1&&r!==null&&(e[s]=r)}return e},I=o=>o==="email",C=()=>({singleTag:"hr"}),A=()=>({tag:"blockquote"}),M=()=>({tag:"ul"}),N=o=>({tag:["pre",{tag:"code",attrs:o.attrs}]}),L=()=>({singleTag:"br"}),U=o=>({tag:`h${o.attrs.level}`}),B=o=>({singleTag:[{tag:"img",attrs:O(o.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=o=>{const t={...o.attrs},{linktype:e="url"}=o.attrs;return I(e)&&(t.href=`mailto:${t.href}`),t.anchor&&(t.href=`${t.href}#${t.anchor}`,delete t.anchor),{tag:[{tag:"a",attrs:t}]}},G=o=>({tag:[{tag:"span",attrs:o.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(o){const t={"&":"&","<":"<",">":">",'"':""","'":"'"},e=/[&<>"']/g,s=RegExp(e.source);return o&&s.test(o)?o.replace(e,r=>t[r]):o};class f{constructor(t){c(this,"marks"),c(this,"nodes"),t||(t=R),this.marks=t.marks||[],this.nodes=t.nodes||[]}addNode(t,e){this.nodes[t]=e}addMark(t,e){this.marks[t]=e}render(t){if(t&&t.content&&Array.isArray(t.content)){let e="";return t.content.forEach(s=>{e+=this.renderNode(s)}),e}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(t){const e=[];t.marks&&t.marks.forEach(r=>{const n=this.getMatchingMark(r);n&&e.push(this.renderOpeningTag(n.tag))});const s=this.getMatchingNode(t);return s&&s.tag&&e.push(this.renderOpeningTag(s.tag)),t.content?t.content.forEach(r=>{e.push(this.renderNode(r))}):t.text?e.push(W(t.text)):s&&s.singleTag?e.push(this.renderTag(s.singleTag," /")):s&&s.html&&e.push(s.html),s&&s.tag&&e.push(this.renderClosingTag(s.tag)),t.marks&&t.marks.slice(0).reverse().forEach(r=>{const n=this.getMatchingMark(r);n&&e.push(this.renderClosingTag(n.tag))}),e.join("")}renderTag(t,e){return t.constructor===String?`<${t}${e}>`:t.map(s=>{if(s.constructor===String)return`<${s}${e}>`;{let r=`<${s.tag}`;if(s.attrs)for(const n in s.attrs){const i=s.attrs[n];i!==null&&(r+=` ${n}="${i}"`)}return`${r}${e}>`}}).join("")}renderOpeningTag(t){return this.renderTag(t,"")}renderClosingTag(t){return t.constructor===String?`</${t}>`:t.slice(0).reverse().map(e=>e.constructor===String?`</${e}>`:`</${e.tag}>`).join("")}getMatchingNode(t){const e=this.nodes[t.type];if(typeof e=="function")return e(t)}getMatchingMark(t){const e=this.marks[t.type];if(typeof e=="function")return e(t)}}class _{constructor(){c(this,"isCDNUrl",(t="")=>t.indexOf("/cdn/")>-1),c(this,"getOptionsPage",(t,e=25,s=1)=>({...t,per_page:e,page:s})),c(this,"delay",t=>new Promise(e=>setTimeout(e,t))),c(this,"arrayFrom",(t=0,e)=>[...Array(t)].map(e)),c(this,"range",(t=0,e=t)=>{const s=Math.abs(e-t)||0,r=t<e?1:-1;return this.arrayFrom(s,(n,i)=>i*r+t)}),c(this,"asyncMap",async(t,e)=>Promise.all(t.map(e))),c(this,"flatMap",(t=[],e)=>t.map(e).reduce((s,r)=>[...s,...r],[]))}stringify(t,e,s){const r=[];for(const n in t){if(!Object.prototype.hasOwnProperty.call(t,n))continue;const i=t[n],a=s?"":encodeURIComponent(n);let l;typeof i=="object"?l=this.stringify(i,e?e+encodeURIComponent("["+a+"]"):a,Array.isArray(i)):l=(e?e+encodeURIComponent("["+a+"]"):a)+"="+encodeURIComponent(i),r.push(l)}return r.join("&")}}class X{constructor(t){c(this,"baseURL"),c(this,"timeout"),c(this,"headers"),c(this,"responseInterceptor"),c(this,"fetch"),c(this,"ejectInterceptor"),c(this,"url"),c(this,"parameters"),this.baseURL=t.baseURL,this.headers=t.headers||[],this.timeout=t!=null&&t.timeout?t.timeout*1e3:0,this.responseInterceptor=t.responseInterceptor,this.fetch=(...e)=>t.fetch?t.fetch(...e):fetch(...e),this.ejectInterceptor=!1,this.url="",this.parameters={}}get(t,e){return this.url=t,this.parameters=e,this._methodHandler("get")}post(t,e){return this.url=t,this.parameters=e,this._methodHandler("post")}put(t,e){return this.url=t,this.parameters=e,this._methodHandler("put")}delete(t,e){return this.url=t,this.parameters=e,this._methodHandler("delete")}async _responseHandler(t){const e=[],s={data:{},headers:{},status:0,statusText:""};t.status!==204&&await t.json().then(r=>{s.data=r});for(const r of t.headers.entries())e[r[0]]=r[1];return s.headers={...e},s.status=t.status,s.statusText=t.statusText,s}async _methodHandler(t){let e=`${this.baseURL}${this.url}`,s=null;if(t==="get"){const l=new _;e=`${this.baseURL}${this.url}?${l.stringify(this.parameters)}`}else s=JSON.stringify(this.parameters);const r=new URL(e),n=new AbortController,{signal:i}=n;let a;this.timeout&&(a=setTimeout(()=>n.abort(),this.timeout));try{const l=await this.fetch(`${r}`,{method:t,headers:this.headers,body:s,signal:i});this.timeout&&clearTimeout(a);const h=await this._responseHandler(l);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(h)):this._statusHandler(h)}catch(l){return{message:l}}}eject(){this.ejectInterceptor=!0}_statusHandler(t){const e=/20[0-6]/g;return new Promise((s,r)=>{if(e.test(`${t.status}`))return s(t);const n={message:new Error(t.statusText),status:t.status,response:t.data.error||t.data.slug};r(n)})}}let m={};const g={};class Z{constructor(t,e){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"),!e){const n=t.region?`-${t.region}`:"",i=t.https===!1?"http":"https";t.oauthToken?e=`${i}://api${n}.storyblok.com/v1`:e=`${i}://api${n}.storyblok.com/v2`}const s=new Headers;s.set("Content-Type","application/json"),s.set("Accept","application/json"),s.forEach((n,i)=>{t.headers&&t.headers[i]&&s.set(i,t.headers[i])});let r=5;t.oauthToken&&(s.set("Authorization",t.oauthToken),r=3),t.rateLimit&&(r=t.rateLimit),t.richTextSchema?this.richTextResolver=new f(t.richTextSchema):this.richTextResolver=new f,t.componentResolver&&this.setComponentResolver(t.componentResolver),this.maxRetries=t.maxRetries,this.throttle=x(this.throttledRequest,r,1e3),this.accessToken=t.accessToken||"",this.relations={},this.links={},this.cache=t.cache||{clear:"manual"},this.helpers=new _,this.resolveNestedRelations=!1,this.client=new X({baseURL:e,timeout:t.timeout||0,headers:s,responseInterceptor:t.responseInterceptor,fetch:t.fetch})}setComponentResolver(t){this.richTextResolver.addNode("blok",e=>{let s="";return e.attrs.body.forEach(r=>{s+=t(r.component,r)}),{html:s}})}parseParams(t){return t.version||(t.version="published"),t.token||(t.token=this.getToken()),t.cv||(t.cv=g[t.token]),Array.isArray(t.resolve_relations)&&(t.resolve_relations=t.resolve_relations.join(",")),t}factoryParamOptions(t,e){return this.helpers.isCDNUrl(t)?this.parseParams(e):e}makeRequest(t,e,s,r){const n=this.factoryParamOptions(t,this.helpers.getOptionsPage(e,s,r));return this.cacheResponse(t,n)}get(t,e){e||(e={});const s=`/${t}`,r=this.factoryParamOptions(s,e);return this.cacheResponse(s,r)}async getAll(t,e,s){const r=(e==null?void 0:e.per_page)||25,n=`/${t}`,i=n.split("/"),a=s||i[i.length-1],l=1,h=await this.makeRequest(n,e,r,l),u=h.total?Math.ceil(h.total/r):1,k=await this.helpers.asyncMap(this.helpers.range(l,u),b=>this.makeRequest(n,e,r,b+1));return this.helpers.flatMap([h,...k],b=>Object.values(b.data[a]))}post(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("post",s,e))}put(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("put",s,e))}delete(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("delete",s,e))}getStories(t){return this.get("cdn/stories",t)}getStory(t,e){return this.get(`cdn/stories/${t}`,e)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(t){return JSON.parse(JSON.stringify(t))}_insertLinks(t,e){const s=t[e];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(t,e,s){if(s.indexOf(`${t.component}.${e}`)>-1){if(typeof t[e]=="string")this.relations[t[e]]&&(t[e]=this._cleanCopy(this.relations[t[e]]));else if(t[e]&&t[e].constructor===Array){const r=[];t[e].forEach(n=>{this.relations[n]&&r.push(this._cleanCopy(this.relations[n]))}),t[e]=r}}}iterateTree(t,e){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,e),this._insertLinks(r,n)),s(r[n])}}};s(t.content)}async resolveLinks(t,e){let s=[];if(t.link_uuids){const r=t.link_uuids.length,n=[],i=50;for(let a=0;a<r;a+=i){const l=Math.min(r,a+i);n.push(t.link_uuids.slice(a,l))}for(let a=0;a<n.length;a++)(await this.getStories({per_page:i,language:e.language,version:e.version,by_uuids:n[a].join(",")})).data.stories.forEach(l=>{s.push(l)})}else s=t.links;s.forEach(r=>{this.links[r.uuid]={...r,_stopResolving:!0}})}async resolveRelations(t,e){let s=[];if(t.rel_uuids){const r=t.rel_uuids.length,n=[],i=50;for(let a=0;a<r;a+=i){const l=Math.min(r,a+i);n.push(t.rel_uuids.slice(a,l))}for(let a=0;a<n.length;a++)(await this.getStories({per_page:i,language:e.language,version:e.version,by_uuids:n[a].join(",")})).data.stories.forEach(l=>{s.push(l)})}else s=t.rels;s&&s.length>0&&s.forEach(r=>{this.relations[r.uuid]={...r,_stopResolving:!0}})}async resolveStories(t,e){var s,r;let n=[];if(typeof e.resolve_relations<"u"&&e.resolve_relations.length>0&&(typeof e.resolve_relations=="string"&&(n=e.resolve_relations.split(",")),await this.resolveRelations(t,e)),e.resolve_links&&["1","story","url"].indexOf(e.resolve_links)>-1&&((s=t.links)!=null&&s.length||(r=t.link_uuids)!=null&&r.length)&&await this.resolveLinks(t,e),this.resolveNestedRelations)for(const i in this.relations)this.iterateTree(this.relations[i],n);t.story?this.iterateTree(t.story,n):t.stories.forEach(i=>{this.iterateTree(i,n)})}async cacheResponse(t,e,s){const r=this.helpers.stringify({url:t,params:e}),n=this.cacheProvider();if(this.cache.clear==="auto"&&e.version==="draft"&&await this.flushCache(),e.version==="published"&&t!="/cdn/spaces/me"){const i=await n.get(r);if(i)return Promise.resolve(i)}return new Promise((i,a)=>{try{(async()=>{var l;const h=await this.throttle("get",t,e);let u={data:h.data,headers:h.headers};if((l=h.headers)!=null&&l["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 a(h);(u.data.story||u.data.stories)&&await this.resolveStories(u.data,e),e.version==="published"&&t!="/cdn/spaces/me"&&await n.set(r,u),u.data.cv&&e.token&&(e.version=="draft"&&g[e.token]!=u.data.cv&&await this.flushCache(),g[e.token]=u.data.cv),i(u)})()}catch{}})}throttledRequest(t,e,s){return this.client[t](e,s)}cacheVersions(){return g}cacheVersion(){return g[this.accessToken]}setCacheVersion(t){this.accessToken&&(g[this.accessToken]=t)}cacheProvider(){switch(this.cache.type){case"memory":return{get(t){return Promise.resolve(m[t])},getAll(){return Promise.resolve(m)},set(t,e){return m[t]=e,Promise.resolve(void 0)},flush(){return m={},Promise.resolve(void 0)}};case"custom":if(this.cache.custom)return this.cache.custom;default:return{get(){return Promise.resolve(void 0)},getAll(){return Promise.resolve(void 0)},set(){return Promise.resolve(void 0)},flush(){return Promise.resolve(void 0)}}}}async flushCache(){return await this.cacheProvider().flush(),this}}const tt=(o={})=>{const{apiOptions:t}=o;if(!t.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(t)}},et=o=>{if(typeof o!="object"||typeof o._editable>"u")return{};const t=JSON.parse(o._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(t),"data-blok-uid":t.id+"-"+t.uid}};let y;const $="https://app.storyblok.com/f/storyblok-v2-latest.js",P=(o,t,e={})=>{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(!o){console.warn("Story ID is not defined. Please provide a valid ID.");return}window.storyblokRegisterEvent(()=>{new window.StoryblokBridge(e).on(["input","published","change"],r=>{r.action==="input"&&r.story.id===o?t(r.story):(r.action==="change"||r.action==="published")&&r.storyId===o&&window.location.reload()})})}},st=(o={})=>{const{bridge:t,accessToken:e,use:s=[],apiOptions:r={},richText:n={}}=o;r.accessToken=r.accessToken||e;const i={bridge:t,apiOptions:r};let a={};return s.forEach(l=>{a={...a,...l(i)}}),t!==!1&&w($),y=new f(n.schema),n.resolver&&j(y,n.resolver),a},j=(o,t)=>{o.addNode("blok",e=>{let s="";return e.attrs.body.forEach(r=>{s+=t(r.component,r)}),{html:s}})},rt=(o,t,e)=>{let s=e||y;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return o===""?"":o?(t&&(s=new f(t.schema),t.resolver&&j(s,t.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.
|
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`),"")},nt=()=>w($);d.RichTextResolver=f,d.RichTextSchema=R,d.apiPlugin=tt,d.loadStoryblokBridge=nt,d.registerStoryblokBridge=P,d.renderRichText=rt,d.storyblokEditable=et,d.storyblokInit=st,d.useStoryblokBridge=P,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
package/dist/storyblok-js.mjs
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
let k = !1;
|
2
|
-
const
|
2
|
+
const v = [], w = (n) => new Promise((t, e) => {
|
3
3
|
if (typeof window > "u" || (window.storyblokRegisterEvent = (r) => {
|
4
4
|
if (window.location === window.parent.location) {
|
5
5
|
console.warn("You are not in Draft Mode or in the Visual Editor.");
|
6
6
|
return;
|
7
7
|
}
|
8
|
-
k ? r() :
|
8
|
+
k ? r() : v.push(r);
|
9
9
|
}, document.getElementById("storyblok-javascript-bridge")))
|
10
10
|
return;
|
11
11
|
const s = document.createElement("script");
|
12
|
-
s.async = !0, s.src = n, s.id = "storyblok-javascript-bridge", s.onerror = (r) =>
|
13
|
-
|
12
|
+
s.async = !0, s.src = n, s.id = "storyblok-javascript-bridge", s.onerror = (r) => e(r), s.onload = (r) => {
|
13
|
+
v.forEach((o) => o()), k = !0, t(r);
|
14
14
|
}, document.getElementsByTagName("head")[0].appendChild(s);
|
15
15
|
});
|
16
|
-
var $ = Object.defineProperty,
|
17
|
-
function
|
16
|
+
var $ = Object.defineProperty, P = (n, t, e) => t in n ? $(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, c = (n, t, e) => (P(n, typeof t != "symbol" ? t + "" : t, e), e);
|
17
|
+
function b(n) {
|
18
18
|
return !(n !== n || n === 1 / 0 || n === -1 / 0);
|
19
19
|
}
|
20
|
-
function
|
21
|
-
if (!
|
20
|
+
function j(n, t, e) {
|
21
|
+
if (!b(t))
|
22
22
|
throw new TypeError("Expected `limit` to be a finite number");
|
23
|
-
if (!
|
23
|
+
if (!b(e))
|
24
24
|
throw new TypeError("Expected `interval` to be a finite number");
|
25
25
|
const s = [];
|
26
26
|
let r = [], o = 0;
|
@@ -30,7 +30,7 @@ function x(n, e, t) {
|
|
30
30
|
o--, s.length > 0 && i(), r = r.filter(function(u) {
|
31
31
|
return u !== l;
|
32
32
|
});
|
33
|
-
},
|
33
|
+
}, e);
|
34
34
|
r.indexOf(l) < 0 && r.push(l);
|
35
35
|
const h = s.shift();
|
36
36
|
h.resolve(n.apply(h.self, h.args));
|
@@ -42,7 +42,7 @@ function x(n, e, t) {
|
|
42
42
|
reject: f,
|
43
43
|
args: l,
|
44
44
|
self: h
|
45
|
-
}), o <
|
45
|
+
}), o < t && i();
|
46
46
|
});
|
47
47
|
};
|
48
48
|
return a.abort = function() {
|
@@ -53,14 +53,14 @@ function x(n, e, t) {
|
|
53
53
|
}), s.length = 0;
|
54
54
|
}, a;
|
55
55
|
}
|
56
|
-
const
|
57
|
-
const
|
56
|
+
const x = function(n, t) {
|
57
|
+
const e = {};
|
58
58
|
for (const s in n) {
|
59
59
|
const r = n[s];
|
60
|
-
|
60
|
+
t.indexOf(s) > -1 && r !== null && (e[s] = r);
|
61
61
|
}
|
62
|
-
return
|
63
|
-
},
|
62
|
+
return e;
|
63
|
+
}, E = (n) => n === "email", S = () => ({
|
64
64
|
singleTag: "hr"
|
65
65
|
}), O = () => ({
|
66
66
|
tag: "blockquote"
|
@@ -82,7 +82,7 @@ const E = function(n, e) {
|
|
82
82
|
singleTag: [
|
83
83
|
{
|
84
84
|
tag: "img",
|
85
|
-
attrs:
|
85
|
+
attrs: x(n.attrs, ["src", "alt", "title"])
|
86
86
|
}
|
87
87
|
]
|
88
88
|
}), L = () => ({
|
@@ -104,12 +104,12 @@ const E = function(n, e) {
|
|
104
104
|
}), V = () => ({
|
105
105
|
tag: "i"
|
106
106
|
}), z = (n) => {
|
107
|
-
const
|
108
|
-
return
|
107
|
+
const t = { ...n.attrs }, { linktype: e = "url" } = n.attrs;
|
108
|
+
return E(e) && (t.href = `mailto:${t.href}`), t.anchor && (t.href = `${t.href}#${t.anchor}`, delete t.anchor), {
|
109
109
|
tag: [
|
110
110
|
{
|
111
111
|
tag: "a",
|
112
|
-
attrs:
|
112
|
+
attrs: t
|
113
113
|
}
|
114
114
|
]
|
115
115
|
};
|
@@ -144,54 +144,54 @@ const E = function(n, e) {
|
|
144
144
|
styled: Y
|
145
145
|
}
|
146
146
|
}, Q = function(n) {
|
147
|
-
const
|
147
|
+
const t = {
|
148
148
|
"&": "&",
|
149
149
|
"<": "<",
|
150
150
|
">": ">",
|
151
151
|
'"': """,
|
152
152
|
"'": "'"
|
153
|
-
},
|
154
|
-
return n && s.test(n) ? n.replace(
|
153
|
+
}, e = /[&<>"']/g, s = RegExp(e.source);
|
154
|
+
return n && s.test(n) ? n.replace(e, (r) => t[r]) : n;
|
155
155
|
};
|
156
156
|
class g {
|
157
|
-
constructor(
|
158
|
-
c(this, "marks"), c(this, "nodes"),
|
157
|
+
constructor(t) {
|
158
|
+
c(this, "marks"), c(this, "nodes"), t || (t = K), this.marks = t.marks || [], this.nodes = t.nodes || [];
|
159
159
|
}
|
160
|
-
addNode(
|
161
|
-
this.nodes[
|
160
|
+
addNode(t, e) {
|
161
|
+
this.nodes[t] = e;
|
162
162
|
}
|
163
|
-
addMark(
|
164
|
-
this.marks[
|
163
|
+
addMark(t, e) {
|
164
|
+
this.marks[t] = e;
|
165
165
|
}
|
166
|
-
render(
|
167
|
-
if (
|
168
|
-
let
|
169
|
-
return
|
170
|
-
|
171
|
-
}),
|
166
|
+
render(t) {
|
167
|
+
if (t && t.content && Array.isArray(t.content)) {
|
168
|
+
let e = "";
|
169
|
+
return t.content.forEach((s) => {
|
170
|
+
e += this.renderNode(s);
|
171
|
+
}), e;
|
172
172
|
}
|
173
173
|
return console.warn(
|
174
174
|
"The render method must receive an object with a content field, which is an array"
|
175
175
|
), "";
|
176
176
|
}
|
177
|
-
renderNode(
|
178
|
-
const
|
179
|
-
|
177
|
+
renderNode(t) {
|
178
|
+
const e = [];
|
179
|
+
t.marks && t.marks.forEach((r) => {
|
180
180
|
const o = this.getMatchingMark(r);
|
181
|
-
o &&
|
181
|
+
o && e.push(this.renderOpeningTag(o.tag));
|
182
182
|
});
|
183
|
-
const s = this.getMatchingNode(
|
184
|
-
return s && s.tag &&
|
185
|
-
|
186
|
-
}) :
|
183
|
+
const s = this.getMatchingNode(t);
|
184
|
+
return s && s.tag && e.push(this.renderOpeningTag(s.tag)), t.content ? t.content.forEach((r) => {
|
185
|
+
e.push(this.renderNode(r));
|
186
|
+
}) : t.text ? e.push(Q(t.text)) : s && s.singleTag ? e.push(this.renderTag(s.singleTag, " /")) : s && s.html && e.push(s.html), s && s.tag && e.push(this.renderClosingTag(s.tag)), t.marks && t.marks.slice(0).reverse().forEach((r) => {
|
187
187
|
const o = this.getMatchingMark(r);
|
188
|
-
o &&
|
189
|
-
}),
|
188
|
+
o && e.push(this.renderClosingTag(o.tag));
|
189
|
+
}), e.join("");
|
190
190
|
}
|
191
|
-
renderTag(
|
192
|
-
return
|
191
|
+
renderTag(t, e) {
|
192
|
+
return t.constructor === String ? `<${t}${e}>` : t.map((s) => {
|
193
193
|
if (s.constructor === String)
|
194
|
-
return `<${s}${
|
194
|
+
return `<${s}${e}>`;
|
195
195
|
{
|
196
196
|
let r = `<${s.tag}`;
|
197
197
|
if (s.attrs)
|
@@ -199,99 +199,99 @@ class g {
|
|
199
199
|
const i = s.attrs[o];
|
200
200
|
i !== null && (r += ` ${o}="${i}"`);
|
201
201
|
}
|
202
|
-
return `${r}${
|
202
|
+
return `${r}${e}>`;
|
203
203
|
}
|
204
204
|
}).join("");
|
205
205
|
}
|
206
|
-
renderOpeningTag(
|
207
|
-
return this.renderTag(
|
206
|
+
renderOpeningTag(t) {
|
207
|
+
return this.renderTag(t, "");
|
208
208
|
}
|
209
|
-
renderClosingTag(
|
210
|
-
return
|
209
|
+
renderClosingTag(t) {
|
210
|
+
return t.constructor === String ? `</${t}>` : t.slice(0).reverse().map((e) => e.constructor === String ? `</${e}>` : `</${e.tag}>`).join("");
|
211
211
|
}
|
212
|
-
getMatchingNode(
|
213
|
-
const
|
214
|
-
if (typeof
|
215
|
-
return t
|
212
|
+
getMatchingNode(t) {
|
213
|
+
const e = this.nodes[t.type];
|
214
|
+
if (typeof e == "function")
|
215
|
+
return e(t);
|
216
216
|
}
|
217
|
-
getMatchingMark(
|
218
|
-
const
|
219
|
-
if (typeof
|
220
|
-
return t
|
217
|
+
getMatchingMark(t) {
|
218
|
+
const e = this.marks[t.type];
|
219
|
+
if (typeof e == "function")
|
220
|
+
return e(t);
|
221
221
|
}
|
222
222
|
}
|
223
223
|
class T {
|
224
224
|
constructor() {
|
225
|
-
c(this, "isCDNUrl", (
|
226
|
-
...
|
227
|
-
per_page:
|
225
|
+
c(this, "isCDNUrl", (t = "") => t.indexOf("/cdn/") > -1), c(this, "getOptionsPage", (t, e = 25, s = 1) => ({
|
226
|
+
...t,
|
227
|
+
per_page: e,
|
228
228
|
page: s
|
229
|
-
})), c(this, "delay", (
|
230
|
-
const s = Math.abs(
|
231
|
-
return this.arrayFrom(s, (o, i) => i * r +
|
232
|
-
}), c(this, "asyncMap", async (
|
229
|
+
})), c(this, "delay", (t) => new Promise((e) => setTimeout(e, t))), c(this, "arrayFrom", (t = 0, e) => [...Array(t)].map(e)), c(this, "range", (t = 0, e = t) => {
|
230
|
+
const s = Math.abs(e - t) || 0, r = t < e ? 1 : -1;
|
231
|
+
return this.arrayFrom(s, (o, i) => i * r + t);
|
232
|
+
}), c(this, "asyncMap", async (t, e) => Promise.all(t.map(e))), c(this, "flatMap", (t = [], e) => t.map(e).reduce((s, r) => [...s, ...r], []));
|
233
233
|
}
|
234
|
-
stringify(
|
234
|
+
stringify(t, e, s) {
|
235
235
|
const r = [];
|
236
|
-
for (const o in
|
237
|
-
if (!Object.prototype.hasOwnProperty.call(
|
236
|
+
for (const o in t) {
|
237
|
+
if (!Object.prototype.hasOwnProperty.call(t, o))
|
238
238
|
continue;
|
239
|
-
const i =
|
239
|
+
const i = t[o], a = s ? "" : encodeURIComponent(o);
|
240
240
|
let l;
|
241
241
|
typeof i == "object" ? l = this.stringify(
|
242
242
|
i,
|
243
|
-
|
243
|
+
e ? e + encodeURIComponent("[" + a + "]") : a,
|
244
244
|
Array.isArray(i)
|
245
|
-
) : l = (
|
245
|
+
) : l = (e ? e + encodeURIComponent("[" + a + "]") : a) + "=" + encodeURIComponent(i), r.push(l);
|
246
246
|
}
|
247
247
|
return r.join("&");
|
248
248
|
}
|
249
249
|
}
|
250
250
|
class G {
|
251
|
-
constructor(
|
252
|
-
c(this, "baseURL"), c(this, "timeout"), c(this, "headers"), c(this, "responseInterceptor"), c(this, "fetch"), c(this, "ejectInterceptor"), c(this, "url"), c(this, "parameters"), this.baseURL =
|
251
|
+
constructor(t) {
|
252
|
+
c(this, "baseURL"), c(this, "timeout"), c(this, "headers"), c(this, "responseInterceptor"), c(this, "fetch"), c(this, "ejectInterceptor"), c(this, "url"), c(this, "parameters"), this.baseURL = t.baseURL, this.headers = t.headers || [], this.timeout = t != null && t.timeout ? t.timeout * 1e3 : 0, this.responseInterceptor = t.responseInterceptor, this.fetch = (...e) => t.fetch ? t.fetch(...e) : fetch(...e), this.ejectInterceptor = !1, this.url = "", this.parameters = {};
|
253
253
|
}
|
254
|
-
get(
|
255
|
-
return this.url =
|
254
|
+
get(t, e) {
|
255
|
+
return this.url = t, this.parameters = e, this._methodHandler("get");
|
256
256
|
}
|
257
|
-
post(
|
258
|
-
return this.url =
|
257
|
+
post(t, e) {
|
258
|
+
return this.url = t, this.parameters = e, this._methodHandler("post");
|
259
259
|
}
|
260
|
-
put(
|
261
|
-
return this.url =
|
260
|
+
put(t, e) {
|
261
|
+
return this.url = t, this.parameters = e, this._methodHandler("put");
|
262
262
|
}
|
263
|
-
delete(
|
264
|
-
return this.url =
|
263
|
+
delete(t, e) {
|
264
|
+
return this.url = t, this.parameters = e, this._methodHandler("delete");
|
265
265
|
}
|
266
|
-
async _responseHandler(
|
267
|
-
const
|
266
|
+
async _responseHandler(t) {
|
267
|
+
const e = [], s = {
|
268
268
|
data: {},
|
269
269
|
headers: {},
|
270
270
|
status: 0,
|
271
271
|
statusText: ""
|
272
272
|
};
|
273
|
-
|
273
|
+
t.status !== 204 && await t.json().then((r) => {
|
274
274
|
s.data = r;
|
275
275
|
});
|
276
|
-
for (const r of
|
277
|
-
|
278
|
-
return s.headers = { ...
|
276
|
+
for (const r of t.headers.entries())
|
277
|
+
e[r[0]] = r[1];
|
278
|
+
return s.headers = { ...e }, s.status = t.status, s.statusText = t.statusText, s;
|
279
279
|
}
|
280
|
-
async _methodHandler(
|
281
|
-
let
|
282
|
-
if (
|
280
|
+
async _methodHandler(t) {
|
281
|
+
let e = `${this.baseURL}${this.url}`, s = null;
|
282
|
+
if (t === "get") {
|
283
283
|
const l = new T();
|
284
|
-
|
284
|
+
e = `${this.baseURL}${this.url}?${l.stringify(
|
285
285
|
this.parameters
|
286
286
|
)}`;
|
287
287
|
} else
|
288
288
|
s = JSON.stringify(this.parameters);
|
289
|
-
const r = new URL(
|
289
|
+
const r = new URL(e), o = new AbortController(), { signal: i } = o;
|
290
290
|
let a;
|
291
291
|
this.timeout && (a = setTimeout(() => o.abort(), this.timeout));
|
292
292
|
try {
|
293
293
|
const l = await this.fetch(`${r}`, {
|
294
|
-
method:
|
294
|
+
method: t,
|
295
295
|
headers: this.headers,
|
296
296
|
body: s,
|
297
297
|
signal: i
|
@@ -300,20 +300,23 @@ class G {
|
|
300
300
|
const h = await this._responseHandler(l);
|
301
301
|
return this.responseInterceptor && !this.ejectInterceptor ? this._statusHandler(this.responseInterceptor(h)) : this._statusHandler(h);
|
302
302
|
} catch (l) {
|
303
|
-
return
|
303
|
+
return {
|
304
|
+
message: l
|
305
|
+
};
|
304
306
|
}
|
305
307
|
}
|
306
308
|
eject() {
|
307
309
|
this.ejectInterceptor = !0;
|
308
310
|
}
|
309
|
-
_statusHandler(
|
310
|
-
const
|
311
|
+
_statusHandler(t) {
|
312
|
+
const e = /20[0-6]/g;
|
311
313
|
return new Promise((s, r) => {
|
312
|
-
if (
|
313
|
-
return s(
|
314
|
+
if (e.test(`${t.status}`))
|
315
|
+
return s(t);
|
314
316
|
const o = {
|
315
|
-
message: new Error(
|
316
|
-
|
317
|
+
message: new Error(t.statusText),
|
318
|
+
status: t.status,
|
319
|
+
response: t.data.error || t.data.slug
|
317
320
|
};
|
318
321
|
r(o);
|
319
322
|
});
|
@@ -322,79 +325,79 @@ class G {
|
|
322
325
|
let p = {};
|
323
326
|
const d = {};
|
324
327
|
class W {
|
325
|
-
constructor(
|
326
|
-
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"), !
|
327
|
-
const o =
|
328
|
-
|
328
|
+
constructor(t, e) {
|
329
|
+
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"), !e) {
|
330
|
+
const o = t.region ? `-${t.region}` : "", i = t.https === !1 ? "http" : "https";
|
331
|
+
t.oauthToken ? e = `${i}://api${o}.storyblok.com/v1` : e = `${i}://api${o}.storyblok.com/v2`;
|
329
332
|
}
|
330
333
|
const s = new Headers();
|
331
334
|
s.set("Content-Type", "application/json"), s.set("Accept", "application/json"), s.forEach((o, i) => {
|
332
|
-
|
335
|
+
t.headers && t.headers[i] && s.set(i, t.headers[i]);
|
333
336
|
});
|
334
337
|
let r = 5;
|
335
|
-
|
336
|
-
baseURL:
|
337
|
-
timeout:
|
338
|
+
t.oauthToken && (s.set("Authorization", t.oauthToken), r = 3), t.rateLimit && (r = t.rateLimit), t.richTextSchema ? this.richTextResolver = new g(t.richTextSchema) : this.richTextResolver = new g(), t.componentResolver && this.setComponentResolver(t.componentResolver), this.maxRetries = t.maxRetries, this.throttle = j(this.throttledRequest, r, 1e3), this.accessToken = t.accessToken || "", this.relations = {}, this.links = {}, this.cache = t.cache || { clear: "manual" }, this.helpers = new T(), this.resolveNestedRelations = !1, this.client = new G({
|
339
|
+
baseURL: e,
|
340
|
+
timeout: t.timeout || 0,
|
338
341
|
headers: s,
|
339
|
-
responseInterceptor:
|
340
|
-
fetch:
|
342
|
+
responseInterceptor: t.responseInterceptor,
|
343
|
+
fetch: t.fetch
|
341
344
|
});
|
342
345
|
}
|
343
|
-
setComponentResolver(
|
344
|
-
this.richTextResolver.addNode("blok", (
|
346
|
+
setComponentResolver(t) {
|
347
|
+
this.richTextResolver.addNode("blok", (e) => {
|
345
348
|
let s = "";
|
346
|
-
return
|
347
|
-
s +=
|
349
|
+
return e.attrs.body.forEach((r) => {
|
350
|
+
s += t(r.component, r);
|
348
351
|
}), {
|
349
352
|
html: s
|
350
353
|
};
|
351
354
|
});
|
352
355
|
}
|
353
|
-
parseParams(
|
354
|
-
return
|
356
|
+
parseParams(t) {
|
357
|
+
return t.version || (t.version = "published"), t.token || (t.token = this.getToken()), t.cv || (t.cv = d[t.token]), Array.isArray(t.resolve_relations) && (t.resolve_relations = t.resolve_relations.join(",")), t;
|
355
358
|
}
|
356
|
-
factoryParamOptions(
|
357
|
-
return this.helpers.isCDNUrl(
|
359
|
+
factoryParamOptions(t, e) {
|
360
|
+
return this.helpers.isCDNUrl(t) ? this.parseParams(e) : e;
|
358
361
|
}
|
359
|
-
makeRequest(
|
362
|
+
makeRequest(t, e, s, r) {
|
360
363
|
const o = this.factoryParamOptions(
|
361
|
-
|
362
|
-
this.helpers.getOptionsPage(
|
364
|
+
t,
|
365
|
+
this.helpers.getOptionsPage(e, s, r)
|
363
366
|
);
|
364
|
-
return this.cacheResponse(
|
367
|
+
return this.cacheResponse(t, o);
|
365
368
|
}
|
366
|
-
get(
|
367
|
-
|
368
|
-
const s = `/${
|
369
|
+
get(t, e) {
|
370
|
+
e || (e = {});
|
371
|
+
const s = `/${t}`, r = this.factoryParamOptions(s, e);
|
369
372
|
return this.cacheResponse(s, r);
|
370
373
|
}
|
371
|
-
async getAll(
|
372
|
-
const r = (
|
374
|
+
async getAll(t, e, s) {
|
375
|
+
const r = (e == null ? void 0 : e.per_page) || 25, o = `/${t}`, i = o.split("/"), a = s || i[i.length - 1], l = 1, h = await this.makeRequest(o, e, r, l), u = h.total ? Math.ceil(h.total / r) : 1, f = await this.helpers.asyncMap(
|
373
376
|
this.helpers.range(l, u),
|
374
|
-
(m) => this.makeRequest(o,
|
377
|
+
(m) => this.makeRequest(o, e, r, m + 1)
|
375
378
|
);
|
376
379
|
return this.helpers.flatMap(
|
377
380
|
[h, ...f],
|
378
381
|
(m) => Object.values(m.data[a])
|
379
382
|
);
|
380
383
|
}
|
381
|
-
post(
|
382
|
-
const s = `/${
|
383
|
-
return Promise.resolve(this.throttle("post", s,
|
384
|
+
post(t, e) {
|
385
|
+
const s = `/${t}`;
|
386
|
+
return Promise.resolve(this.throttle("post", s, e));
|
384
387
|
}
|
385
|
-
put(
|
386
|
-
const s = `/${
|
387
|
-
return Promise.resolve(this.throttle("put", s,
|
388
|
+
put(t, e) {
|
389
|
+
const s = `/${t}`;
|
390
|
+
return Promise.resolve(this.throttle("put", s, e));
|
388
391
|
}
|
389
|
-
delete(
|
390
|
-
const s = `/${
|
391
|
-
return Promise.resolve(this.throttle("delete", s,
|
392
|
+
delete(t, e) {
|
393
|
+
const s = `/${t}`;
|
394
|
+
return Promise.resolve(this.throttle("delete", s, e));
|
392
395
|
}
|
393
|
-
getStories(
|
394
|
-
return this.get("cdn/stories",
|
396
|
+
getStories(t) {
|
397
|
+
return this.get("cdn/stories", t);
|
395
398
|
}
|
396
|
-
getStory(
|
397
|
-
return this.get(`cdn/stories/${
|
399
|
+
getStory(t, e) {
|
400
|
+
return this.get(`cdn/stories/${t}`, e);
|
398
401
|
}
|
399
402
|
getToken() {
|
400
403
|
return this.accessToken;
|
@@ -402,26 +405,26 @@ class W {
|
|
402
405
|
ejectInterceptor() {
|
403
406
|
this.client.eject();
|
404
407
|
}
|
405
|
-
_cleanCopy(
|
406
|
-
return JSON.parse(JSON.stringify(
|
408
|
+
_cleanCopy(t) {
|
409
|
+
return JSON.parse(JSON.stringify(t));
|
407
410
|
}
|
408
|
-
_insertLinks(
|
409
|
-
const s = e
|
411
|
+
_insertLinks(t, e) {
|
412
|
+
const s = t[e];
|
410
413
|
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]));
|
411
414
|
}
|
412
|
-
_insertRelations(
|
413
|
-
if (s.indexOf(`${
|
414
|
-
if (typeof e
|
415
|
-
this.relations[e
|
416
|
-
else if (e
|
415
|
+
_insertRelations(t, e, s) {
|
416
|
+
if (s.indexOf(`${t.component}.${e}`) > -1) {
|
417
|
+
if (typeof t[e] == "string")
|
418
|
+
this.relations[t[e]] && (t[e] = this._cleanCopy(this.relations[t[e]]));
|
419
|
+
else if (t[e] && t[e].constructor === Array) {
|
417
420
|
const r = [];
|
418
|
-
e
|
421
|
+
t[e].forEach((o) => {
|
419
422
|
this.relations[o] && r.push(this._cleanCopy(this.relations[o]));
|
420
|
-
}), e
|
423
|
+
}), t[e] = r;
|
421
424
|
}
|
422
425
|
}
|
423
426
|
}
|
424
|
-
iterateTree(
|
427
|
+
iterateTree(t, e) {
|
425
428
|
const s = (r) => {
|
426
429
|
if (r != null) {
|
427
430
|
if (r.constructor === Array)
|
@@ -434,26 +437,26 @@ class W {
|
|
434
437
|
(r.component && r._uid || r.type === "link") && (this._insertRelations(
|
435
438
|
r,
|
436
439
|
o,
|
437
|
-
|
440
|
+
e
|
438
441
|
), this._insertLinks(r, o)), s(r[o]);
|
439
442
|
}
|
440
443
|
}
|
441
444
|
};
|
442
|
-
s(
|
445
|
+
s(t.content);
|
443
446
|
}
|
444
|
-
async resolveLinks(
|
447
|
+
async resolveLinks(t, e) {
|
445
448
|
let s = [];
|
446
|
-
if (
|
447
|
-
const r =
|
449
|
+
if (t.link_uuids) {
|
450
|
+
const r = t.link_uuids.length, o = [], i = 50;
|
448
451
|
for (let a = 0; a < r; a += i) {
|
449
452
|
const l = Math.min(r, a + i);
|
450
|
-
o.push(
|
453
|
+
o.push(t.link_uuids.slice(a, l));
|
451
454
|
}
|
452
455
|
for (let a = 0; a < o.length; a++)
|
453
456
|
(await this.getStories({
|
454
457
|
per_page: i,
|
455
|
-
language:
|
456
|
-
version:
|
458
|
+
language: e.language,
|
459
|
+
version: e.version,
|
457
460
|
by_uuids: o[a].join(",")
|
458
461
|
})).data.stories.forEach(
|
459
462
|
(l) => {
|
@@ -461,70 +464,70 @@ class W {
|
|
461
464
|
}
|
462
465
|
);
|
463
466
|
} else
|
464
|
-
s =
|
467
|
+
s = t.links;
|
465
468
|
s.forEach((r) => {
|
466
469
|
this.links[r.uuid] = { ...r, _stopResolving: !0 };
|
467
470
|
});
|
468
471
|
}
|
469
|
-
async resolveRelations(
|
472
|
+
async resolveRelations(t, e) {
|
470
473
|
let s = [];
|
471
|
-
if (
|
472
|
-
const r =
|
474
|
+
if (t.rel_uuids) {
|
475
|
+
const r = t.rel_uuids.length, o = [], i = 50;
|
473
476
|
for (let a = 0; a < r; a += i) {
|
474
477
|
const l = Math.min(r, a + i);
|
475
|
-
o.push(
|
478
|
+
o.push(t.rel_uuids.slice(a, l));
|
476
479
|
}
|
477
480
|
for (let a = 0; a < o.length; a++)
|
478
481
|
(await this.getStories({
|
479
482
|
per_page: i,
|
480
|
-
language:
|
481
|
-
version:
|
483
|
+
language: e.language,
|
484
|
+
version: e.version,
|
482
485
|
by_uuids: o[a].join(",")
|
483
486
|
})).data.stories.forEach((l) => {
|
484
487
|
s.push(l);
|
485
488
|
});
|
486
489
|
} else
|
487
|
-
s =
|
490
|
+
s = t.rels;
|
488
491
|
s && s.length > 0 && s.forEach((r) => {
|
489
492
|
this.relations[r.uuid] = { ...r, _stopResolving: !0 };
|
490
493
|
});
|
491
494
|
}
|
492
|
-
async resolveStories(
|
495
|
+
async resolveStories(t, e) {
|
493
496
|
var s, r;
|
494
497
|
let o = [];
|
495
|
-
if (typeof
|
498
|
+
if (typeof e.resolve_relations < "u" && e.resolve_relations.length > 0 && (typeof e.resolve_relations == "string" && (o = e.resolve_relations.split(",")), await this.resolveRelations(t, e)), e.resolve_links && ["1", "story", "url"].indexOf(e.resolve_links) > -1 && ((s = t.links) != null && s.length || (r = t.link_uuids) != null && r.length) && await this.resolveLinks(t, e), this.resolveNestedRelations)
|
496
499
|
for (const i in this.relations)
|
497
500
|
this.iterateTree(this.relations[i], o);
|
498
|
-
|
501
|
+
t.story ? this.iterateTree(t.story, o) : t.stories.forEach((i) => {
|
499
502
|
this.iterateTree(i, o);
|
500
503
|
});
|
501
504
|
}
|
502
|
-
cacheResponse(
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
505
|
+
async cacheResponse(t, e, s) {
|
506
|
+
const r = this.helpers.stringify({ url: t, params: e }), o = this.cacheProvider();
|
507
|
+
if (this.cache.clear === "auto" && e.version === "draft" && await this.flushCache(), e.version === "published" && t != "/cdn/spaces/me") {
|
508
|
+
const i = await o.get(r);
|
509
|
+
if (i)
|
510
|
+
return Promise.resolve(i);
|
511
|
+
}
|
512
|
+
return new Promise((i, a) => {
|
510
513
|
try {
|
511
514
|
(async () => {
|
512
515
|
var l;
|
513
|
-
const h = await this.throttle("get",
|
516
|
+
const h = await this.throttle("get", t, e);
|
514
517
|
let u = { data: h.data, headers: h.headers };
|
515
518
|
if ((l = h.headers) != null && l["per-page"] && (u = Object.assign({}, u, {
|
516
519
|
perPage: h.headers["per-page"] ? parseInt(h.headers["per-page"]) : 0,
|
517
520
|
total: h.headers["per-page"] ? parseInt(h.headers.total) : 0
|
518
521
|
})), h.status != 200)
|
519
|
-
return
|
520
|
-
(u.data.story || u.data.stories) && await this.resolveStories(u.data,
|
522
|
+
return a(h);
|
523
|
+
(u.data.story || u.data.stories) && await this.resolveStories(u.data, e), e.version === "published" && t != "/cdn/spaces/me" && await o.set(r, u), u.data.cv && e.token && (e.version == "draft" && d[e.token] != u.data.cv && await this.flushCache(), d[e.token] = u.data.cv), i(u);
|
521
524
|
})();
|
522
525
|
} catch {
|
523
526
|
}
|
524
527
|
});
|
525
528
|
}
|
526
|
-
throttledRequest(
|
527
|
-
return this.client[
|
529
|
+
throttledRequest(t, e, s) {
|
530
|
+
return this.client[t](e, s);
|
528
531
|
}
|
529
532
|
cacheVersions() {
|
530
533
|
return d;
|
@@ -532,65 +535,72 @@ class W {
|
|
532
535
|
cacheVersion() {
|
533
536
|
return d[this.accessToken];
|
534
537
|
}
|
535
|
-
setCacheVersion(
|
536
|
-
this.accessToken && (d[this.accessToken] =
|
538
|
+
setCacheVersion(t) {
|
539
|
+
this.accessToken && (d[this.accessToken] = t);
|
537
540
|
}
|
538
541
|
cacheProvider() {
|
539
542
|
switch (this.cache.type) {
|
540
543
|
case "memory":
|
541
544
|
return {
|
542
|
-
get(
|
543
|
-
return p[
|
545
|
+
get(t) {
|
546
|
+
return Promise.resolve(p[t]);
|
544
547
|
},
|
545
548
|
getAll() {
|
546
|
-
return p;
|
549
|
+
return Promise.resolve(p);
|
547
550
|
},
|
548
|
-
set(
|
549
|
-
p[
|
551
|
+
set(t, e) {
|
552
|
+
return p[t] = e, Promise.resolve(void 0);
|
550
553
|
},
|
551
554
|
flush() {
|
552
|
-
p = {};
|
555
|
+
return p = {}, Promise.resolve(void 0);
|
553
556
|
}
|
554
557
|
};
|
558
|
+
case "custom":
|
559
|
+
if (this.cache.custom)
|
560
|
+
return this.cache.custom;
|
555
561
|
default:
|
556
562
|
return {
|
557
563
|
get() {
|
564
|
+
return Promise.resolve(void 0);
|
558
565
|
},
|
559
566
|
getAll() {
|
567
|
+
return Promise.resolve(void 0);
|
560
568
|
},
|
561
569
|
set() {
|
570
|
+
return Promise.resolve(void 0);
|
562
571
|
},
|
563
572
|
flush() {
|
573
|
+
return Promise.resolve(void 0);
|
564
574
|
}
|
565
575
|
};
|
566
576
|
}
|
567
577
|
}
|
568
|
-
flushCache() {
|
569
|
-
return this.cacheProvider().flush(), this;
|
578
|
+
async flushCache() {
|
579
|
+
return await this.cacheProvider().flush(), this;
|
570
580
|
}
|
571
581
|
}
|
572
582
|
const X = (n = {}) => {
|
573
|
-
const { apiOptions:
|
574
|
-
if (!
|
583
|
+
const { apiOptions: t } = n;
|
584
|
+
if (!t.accessToken) {
|
575
585
|
console.error(
|
576
586
|
"You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication"
|
577
587
|
);
|
578
588
|
return;
|
579
589
|
}
|
580
|
-
return { storyblokApi: new W(
|
590
|
+
return { storyblokApi: new W(t) };
|
581
591
|
}, Z = (n) => {
|
582
592
|
if (typeof n != "object" || typeof n._editable > "u")
|
583
593
|
return {};
|
584
|
-
const
|
594
|
+
const t = JSON.parse(
|
585
595
|
n._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, "")
|
586
596
|
);
|
587
597
|
return {
|
588
|
-
"data-blok-c": JSON.stringify(
|
589
|
-
"data-blok-uid":
|
598
|
+
"data-blok-c": JSON.stringify(t),
|
599
|
+
"data-blok-uid": t.id + "-" + t.uid
|
590
600
|
};
|
591
601
|
};
|
592
602
|
let y;
|
593
|
-
const R = "https://app.storyblok.com/f/storyblok-v2-latest.js",
|
603
|
+
const R = "https://app.storyblok.com/f/storyblok-v2-latest.js", tt = (n, t, e = {}) => {
|
594
604
|
if (!(typeof window > "u")) {
|
595
605
|
if (typeof window.storyblokRegisterEvent > "u") {
|
596
606
|
console.error(
|
@@ -603,54 +613,54 @@ const R = "https://app.storyblok.com/f/storyblok-v2-latest.js", ee = (n, e, t =
|
|
603
613
|
return;
|
604
614
|
}
|
605
615
|
window.storyblokRegisterEvent(() => {
|
606
|
-
new window.StoryblokBridge(
|
607
|
-
r.action === "input" && r.story.id === n ?
|
616
|
+
new window.StoryblokBridge(e).on(["input", "published", "change"], (r) => {
|
617
|
+
r.action === "input" && r.story.id === n ? t(r.story) : (r.action === "change" || r.action === "published") && r.storyId === n && window.location.reload();
|
608
618
|
});
|
609
619
|
});
|
610
620
|
}
|
611
|
-
},
|
621
|
+
}, et = (n = {}) => {
|
612
622
|
const {
|
613
|
-
bridge:
|
614
|
-
accessToken:
|
623
|
+
bridge: t,
|
624
|
+
accessToken: e,
|
615
625
|
use: s = [],
|
616
626
|
apiOptions: r = {},
|
617
627
|
richText: o = {}
|
618
628
|
} = n;
|
619
|
-
r.accessToken = r.accessToken ||
|
620
|
-
const i = { bridge:
|
629
|
+
r.accessToken = r.accessToken || e;
|
630
|
+
const i = { bridge: t, apiOptions: r };
|
621
631
|
let a = {};
|
622
632
|
return s.forEach((l) => {
|
623
633
|
a = { ...a, ...l(i) };
|
624
|
-
}),
|
625
|
-
}, _ = (n,
|
626
|
-
n.addNode("blok", (
|
634
|
+
}), t !== !1 && w(R), y = new g(o.schema), o.resolver && _(y, o.resolver), a;
|
635
|
+
}, _ = (n, t) => {
|
636
|
+
n.addNode("blok", (e) => {
|
627
637
|
let s = "";
|
628
|
-
return
|
629
|
-
s +=
|
638
|
+
return e.attrs.body.forEach((r) => {
|
639
|
+
s += t(r.component, r);
|
630
640
|
}), {
|
631
641
|
html: s
|
632
642
|
};
|
633
643
|
});
|
634
|
-
},
|
635
|
-
let s =
|
644
|
+
}, st = (n, t, e) => {
|
645
|
+
let s = e || y;
|
636
646
|
if (!s) {
|
637
647
|
console.error(
|
638
648
|
"Please initialize the Storyblok SDK before calling the renderRichText function"
|
639
649
|
);
|
640
650
|
return;
|
641
651
|
}
|
642
|
-
return n === "" ? "" : n ? (
|
652
|
+
return n === "" ? "" : n ? (t && (s = new g(t.schema), t.resolver && _(s, t.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.
|
643
653
|
|
644
654
|
For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`), "");
|
645
|
-
},
|
655
|
+
}, rt = () => w(R);
|
646
656
|
export {
|
647
657
|
g as RichTextResolver,
|
648
658
|
K as RichTextSchema,
|
649
659
|
X as apiPlugin,
|
650
|
-
|
651
|
-
|
652
|
-
|
660
|
+
rt as loadStoryblokBridge,
|
661
|
+
tt as registerStoryblokBridge,
|
662
|
+
st as renderRichText,
|
653
663
|
Z as storyblokEditable,
|
654
|
-
|
655
|
-
|
664
|
+
et as storyblokInit,
|
665
|
+
tt as useStoryblokBridge
|
656
666
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storyblok/js",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.14",
|
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",
|
@@ -27,7 +27,7 @@
|
|
27
27
|
"prepublishOnly": "npm run build && cp ../README.md ./"
|
28
28
|
},
|
29
29
|
"dependencies": {
|
30
|
-
"storyblok-js-client": "^5.
|
30
|
+
"storyblok-js-client": "^5.4.0"
|
31
31
|
},
|
32
32
|
"devDependencies": {
|
33
33
|
"@tsconfig/recommended": "^1.0.1",
|