@storyblok/js 2.0.6 → 2.0.8
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 +415 -471
- 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 ce=Object.defineProperty,he=Object.defineProperties;var ue=Object.getOwnPropertyDescriptors;var O=Object.getOwnPropertySymbols;var de=Object.prototype.hasOwnProperty,pe=Object.prototype.propertyIsEnumerable;var P=(h,u,g)=>u in h?ce(h,u,{enumerable:!0,configurable:!0,writable:!0,value:g}):h[u]=g,f=(h,u)=>{for(var g in u||(u={}))de.call(u,g)&&P(h,g,u[g]);if(O)for(var g of O(u))pe.call(u,g)&&P(h,g,u[g]);return h},b=(h,u)=>he(h,ue(u));let u=!1;const g=[],R=n=>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=n,s.id="storyblok-javascript-bridge",s.onerror=r=>t(r),s.onload=r=>{g.forEach(o=>o()),u=!0,e(r)},document.getElementsByTagName("head")[0].appendChild(s)});var I=Object.defineProperty,C=(n,e,t)=>e in n?I(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,c=(n,e,t)=>(C(n,typeof e!="symbol"?e+"":e,t),t);function _(n){return!(n!==n||n===1/0||n===-1/0)}function M(n,e,t){if(!_(e))throw new TypeError("Expected `limit` to be a finite number");if(!_(t))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],o=0;const i=function(){o++;const l=setTimeout(function(){o--,s.length>0&&i(),r=r.filter(function(p){return p!==l})},t);r.indexOf(l)<0&&r.push(l);const d=s.shift();d.resolve(n.apply(d.self,d.args))},a=function(...l){const d=this;return new Promise(function(p,T){s.push({resolve:p,reject:T,args:l,self:d}),o<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 A=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},N=n=>n==="email",L=()=>({singleTag:"hr"}),U=()=>({tag:"blockquote"}),B=()=>({tag:"ul"}),H=n=>({tag:["pre",{tag:"code",attrs:n.attrs}]}),q=()=>({singleTag:"br"}),D=n=>({tag:`h${n.attrs.level}`}),J=n=>({singleTag:[{tag:"img",attrs:A(n.attrs,["src","alt","title"])}]}),F=()=>({tag:"li"}),V=()=>({tag:"ol"}),z=()=>({tag:"p"}),Y=()=>({tag:"b"}),K=()=>({tag:"strike"}),Q=()=>({tag:"u"}),G=()=>({tag:"strong"}),W=()=>({tag:"code"}),X=()=>({tag:"i"}),Z=n=>{const e=f({},n.attrs),{linktype:t="url"}=n.attrs;return N(t)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),{tag:[{tag:"a",attrs:e}]}},ee=n=>({tag:[{tag:"span",attrs:n.attrs}]}),j={nodes:{horizontal_rule:L,blockquote:U,bullet_list:B,code_block:H,hard_break:q,heading:D,image:J,list_item:F,ordered_list:V,paragraph:z},marks:{bold:Y,strike:K,underline:Q,strong:G,code:W,italic:X,link:Z,styled:ee}},te=function(n){const e={"&":"&","<":"<",">":">",'"':""","'":"'"},t=/[&<>"']/g,s=RegExp(t.source);return n&&s.test(n)?n.replace(t,r=>e[r]):n};class m{constructor(e){c(this,"marks"),c(this,"nodes"),e||(e=j),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(te(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(){c(this,"isCDNUrl",(e="")=>e.indexOf("/cdn/")>-1),c(this,"getOptionsPage",(e,t=25,s=1)=>b(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,(o,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 o in e){if(!Object.prototype.hasOwnProperty.call(e,o))continue;const i=e[o],a=s?"":encodeURIComponent(o);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 se{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.timeout?e.timeout*1e3:1e3,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=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 a=new $;t.search=a.stringify(this.parameters)}else s=JSON.stringify(this.parameters);const r=new AbortController,{signal:o}=r,i=setTimeout(()=>r.abort(),this.timeout);try{const a=await this.fetch(`${t}`,{method:e,headers:this.headers,body:s,signal:o});clearTimeout(i);const l=await this._responseHandler(a);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(l)):this._statusHandler(l)}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 k={};const y={};class re{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 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 m(e.richTextSchema):this.richTextResolver=new m,e.componentResolver&&this.setComponentResolver(e.componentResolver),this.maxRetries=e.maxRetries,this.throttle=M(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 se({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=y[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],l=1,d=await this.makeRequest(o,t,r,l),p=d.total?Math.ceil(d.total/r):1,T=await this.helpers.asyncMap(this.helpers.range(l,p),w=>this.makeRequest(o,t,r,w+1));return this.helpers.flatMap([d,...T],w=>Object.values(w.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 l=Math.min(r,a+i);o.push(e.link_uuids.slice(a,l))}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(l=>{s.push(l)})}else s=e.links;s.forEach(r=>{this.links[r.uuid]=b(f({},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 l=Math.min(r,a+i);o.push(e.rel_uuids.slice(a,l))}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(l=>{s.push(l)})}else s=e.rels;s&&s.length>0&&s.forEach(r=>{this.relations[r.uuid]=b(f({},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?void 0:s.length)||((r=e.link_uuids)==null?void 0: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 l=a.get(i);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 o(d);(p.data.story||p.data.stories)&&await this.resolveStories(p.data,t),t.version==="published"&&e!="/cdn/spaces/me"&&a.set(i,p),p.data.cv&&t.token&&(t.version=="draft"&&y[t.token]!=p.data.cv&&this.flushCache(),y[t.token]=p.data.cv),r(p)})()}catch{}})}throttledRequest(e,t,s){return this.client[e](t,s)}cacheVersions(){return y}cacheVersion(){return y[this.accessToken]}setCacheVersion(e){this.accessToken&&(y[this.accessToken]=e)}cacheProvider(){switch(this.cache.type){case"memory":return{get(e){return k[e]},getAll(){return k},set(e,t){k[e]=t},flush(){k={}}};default:return{get(){},getAll(){},set(){},flush(){}}}}flushCache(){return this.cacheProvider().flush(),this}}const oe=(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 re(e)}};var ne=n=>{if(typeof n!="object"||typeof n._editable=="undefined")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 v;const E="https://app.storyblok.com/f/storyblok-v2-latest.js",S=(n,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(!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()})})}},ie=(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(l=>{a=f(f({},a),l(i))}),e!==!1&&R(E),v=new m(o.schema),o.resolver&&x(v,o.resolver),a},x=(n,e)=>{n.addNode("blok",t=>{let s="";return t.attrs.body.forEach(r=>{s+=e(r.component,r)}),{html:s}})},ae=(n,e,t)=>{let s=t||v;if(!s){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return n===""?"":n?(e&&(s=new m(e.schema),e.resolver&&x(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.
|
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:""};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){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|04]/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,440 +1,395 @@
|
|
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 T = (c, t, e) => t in c ? m(c, t, { enumerable: true, configurable: true, writable: true, value: e }) : c[t] = e;
|
53
|
-
var l = (c, t, e) => (T(c, typeof t != "symbol" ? t + "" : t, e), e);
|
54
|
-
function k(c) {
|
55
|
-
return !(c !== c || c === 1 / 0 || c === -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 = [],
|
26
|
+
let r = [], o = 0;
|
64
27
|
const i = function() {
|
65
|
-
|
66
|
-
const
|
67
|
-
|
68
|
-
return u !==
|
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 = () => ({
|
126
89
|
tag: "li"
|
127
|
-
}),
|
90
|
+
}), U = () => ({
|
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
|
-
}), z = (
|
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:
|
169
|
-
list_item:
|
170
|
-
ordered_list:
|
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,
|
132
|
+
list_item: L,
|
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:
|
137
|
+
bold: B,
|
138
|
+
strike: q,
|
139
|
+
underline: D,
|
140
|
+
strong: J,
|
141
|
+
code: F,
|
142
|
+
italic: V,
|
180
143
|
link: z,
|
181
|
-
styled:
|
144
|
+
styled: Y
|
182
145
|
}
|
183
|
-
},
|
184
|
-
const
|
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
|
-
class
|
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, i) => i * 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 i =
|
282
|
-
let
|
283
|
-
typeof i == "object" ?
|
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
|
-
class
|
289
|
-
constructor(
|
290
|
-
l(this, "baseURL");
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
put(t, e) {
|
307
|
-
return this.url = t, this.parameters = e, this._methodHandler("put");
|
308
|
-
}
|
309
|
-
delete(t, e) {
|
310
|
-
return this.url = t, this.parameters = e, this._methodHandler("delete");
|
311
|
-
}
|
312
|
-
async _responseHandler(t) {
|
313
|
-
const e = [], s = {
|
250
|
+
class G {
|
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
|
+
e.status !== 204 && 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(i);
|
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|04]/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
|
-
let
|
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}` : "", i = t.https === false ? "http" : "https";
|
377
|
-
t.oauthToken ? e = `${i}://api${n}.storyblok.com/v1` : e = `${i}://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
325
|
const s = new Headers();
|
380
|
-
s.set("Content-Type", "application/json"), s.set("Accept", "application/json"), s.forEach((
|
381
|
-
|
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]);
|
382
328
|
});
|
383
329
|
let r = 5;
|
384
|
-
|
385
|
-
baseURL:
|
386
|
-
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,
|
387
333
|
headers: s,
|
388
|
-
responseInterceptor:
|
389
|
-
fetch:
|
334
|
+
responseInterceptor: e.responseInterceptor,
|
335
|
+
fetch: e.fetch
|
390
336
|
});
|
391
337
|
}
|
392
|
-
setComponentResolver(
|
393
|
-
this.richTextResolver.addNode("blok", (
|
338
|
+
setComponentResolver(e) {
|
339
|
+
this.richTextResolver.addNode("blok", (t) => {
|
394
340
|
let s = "";
|
395
|
-
return
|
396
|
-
s +=
|
341
|
+
return t.attrs.body.forEach((r) => {
|
342
|
+
s += e(r.component, r);
|
397
343
|
}), {
|
398
344
|
html: s
|
399
345
|
};
|
400
346
|
});
|
401
347
|
}
|
402
|
-
parseParams(
|
403
|
-
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;
|
404
350
|
}
|
405
|
-
factoryParamOptions(
|
406
|
-
return this.helpers.isCDNUrl(
|
351
|
+
factoryParamOptions(e, t) {
|
352
|
+
return this.helpers.isCDNUrl(e) ? this.parseParams(t) : t;
|
407
353
|
}
|
408
|
-
makeRequest(
|
409
|
-
const
|
410
|
-
|
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);
|
411
360
|
}
|
412
|
-
get(
|
413
|
-
|
414
|
-
const s = `/${
|
361
|
+
get(e, t) {
|
362
|
+
t || (t = {});
|
363
|
+
const s = `/${e}`, r = this.factoryParamOptions(s, t);
|
415
364
|
return this.cacheResponse(s, r);
|
416
365
|
}
|
417
|
-
async getAll(
|
418
|
-
const r = (
|
419
|
-
|
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
|
+
);
|
420
375
|
}
|
421
|
-
post(
|
422
|
-
const s = `/${
|
423
|
-
return Promise.resolve(this.throttle("post", s,
|
376
|
+
post(e, t) {
|
377
|
+
const s = `/${e}`;
|
378
|
+
return Promise.resolve(this.throttle("post", s, t));
|
424
379
|
}
|
425
|
-
put(
|
426
|
-
const s = `/${
|
427
|
-
return Promise.resolve(this.throttle("put", s,
|
380
|
+
put(e, t) {
|
381
|
+
const s = `/${e}`;
|
382
|
+
return Promise.resolve(this.throttle("put", s, t));
|
428
383
|
}
|
429
|
-
delete(
|
430
|
-
const s = `/${
|
431
|
-
return Promise.resolve(this.throttle("delete", s,
|
384
|
+
delete(e, t) {
|
385
|
+
const s = `/${e}`;
|
386
|
+
return Promise.resolve(this.throttle("delete", s, t));
|
432
387
|
}
|
433
|
-
getStories(
|
434
|
-
return this.get("cdn/stories",
|
388
|
+
getStories(e) {
|
389
|
+
return this.get("cdn/stories", e);
|
435
390
|
}
|
436
|
-
getStory(
|
437
|
-
return this.get(`cdn/stories/${
|
391
|
+
getStory(e, t) {
|
392
|
+
return this.get(`cdn/stories/${e}`, t);
|
438
393
|
}
|
439
394
|
getToken() {
|
440
395
|
return this.accessToken;
|
@@ -442,123 +397,129 @@ class Q {
|
|
442
397
|
ejectInterceptor() {
|
443
398
|
this.client.eject();
|
444
399
|
}
|
445
|
-
_cleanCopy(
|
446
|
-
return JSON.parse(JSON.stringify(
|
400
|
+
_cleanCopy(e) {
|
401
|
+
return JSON.parse(JSON.stringify(e));
|
447
402
|
}
|
448
|
-
_insertLinks(
|
449
|
-
const s = t
|
403
|
+
_insertLinks(e, t) {
|
404
|
+
const s = e[t];
|
450
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]));
|
451
406
|
}
|
452
|
-
_insertRelations(
|
453
|
-
if (s.indexOf(`${
|
454
|
-
if (typeof t
|
455
|
-
this.relations[t
|
456
|
-
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) {
|
457
412
|
const r = [];
|
458
|
-
t
|
459
|
-
this.relations[
|
460
|
-
}), t
|
413
|
+
e[t].forEach((o) => {
|
414
|
+
this.relations[o] && r.push(this._cleanCopy(this.relations[o]));
|
415
|
+
}), e[t] = r;
|
461
416
|
}
|
462
417
|
}
|
463
418
|
}
|
464
|
-
iterateTree(
|
419
|
+
iterateTree(e, t) {
|
465
420
|
const s = (r) => {
|
466
421
|
if (r != null) {
|
467
422
|
if (r.constructor === Array)
|
468
|
-
for (let
|
469
|
-
s(r[
|
423
|
+
for (let o = 0; o < r.length; o++)
|
424
|
+
s(r[o]);
|
470
425
|
else if (r.constructor === Object) {
|
471
426
|
if (r._stopResolving)
|
472
427
|
return;
|
473
|
-
for (const
|
474
|
-
(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]);
|
475
434
|
}
|
476
435
|
}
|
477
436
|
};
|
478
|
-
s(
|
437
|
+
s(e.content);
|
479
438
|
}
|
480
|
-
async resolveLinks(
|
439
|
+
async resolveLinks(e, t) {
|
481
440
|
let s = [];
|
482
|
-
if (
|
483
|
-
const r =
|
484
|
-
for (let
|
485
|
-
const
|
486
|
-
|
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));
|
487
446
|
}
|
488
|
-
for (let
|
447
|
+
for (let a = 0; a < o.length; a++)
|
489
448
|
(await this.getStories({
|
490
449
|
per_page: i,
|
491
|
-
language:
|
492
|
-
version:
|
493
|
-
by_uuids:
|
494
|
-
})).data.stories.forEach(
|
495
|
-
|
496
|
-
|
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
|
+
);
|
497
458
|
} else
|
498
|
-
s =
|
459
|
+
s = e.links;
|
499
460
|
s.forEach((r) => {
|
500
|
-
this.links[r.uuid] =
|
461
|
+
this.links[r.uuid] = { ...r, _stopResolving: !0 };
|
501
462
|
});
|
502
463
|
}
|
503
|
-
async resolveRelations(
|
464
|
+
async resolveRelations(e, t) {
|
504
465
|
let s = [];
|
505
|
-
if (
|
506
|
-
const r =
|
507
|
-
for (let
|
508
|
-
const
|
509
|
-
|
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));
|
510
471
|
}
|
511
|
-
for (let
|
472
|
+
for (let a = 0; a < o.length; a++)
|
512
473
|
(await this.getStories({
|
513
474
|
per_page: i,
|
514
|
-
language:
|
515
|
-
version:
|
516
|
-
by_uuids:
|
517
|
-
})).data.stories.forEach((
|
518
|
-
s.push(
|
475
|
+
language: t.language,
|
476
|
+
version: t.version,
|
477
|
+
by_uuids: o[a].join(",")
|
478
|
+
})).data.stories.forEach((c) => {
|
479
|
+
s.push(c);
|
519
480
|
});
|
520
481
|
} else
|
521
|
-
s =
|
482
|
+
s = e.rels;
|
522
483
|
s && s.length > 0 && s.forEach((r) => {
|
523
|
-
this.relations[r.uuid] =
|
484
|
+
this.relations[r.uuid] = { ...r, _stopResolving: !0 };
|
524
485
|
});
|
525
486
|
}
|
526
|
-
async resolveStories(
|
527
|
-
var
|
528
|
-
let
|
529
|
-
if (typeof
|
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)
|
530
491
|
for (const i in this.relations)
|
531
|
-
this.iterateTree(this.relations[i],
|
532
|
-
|
533
|
-
this.iterateTree(i,
|
492
|
+
this.iterateTree(this.relations[i], o);
|
493
|
+
e.story ? this.iterateTree(e.story, o) : e.stories.forEach((i) => {
|
494
|
+
this.iterateTree(i, o);
|
534
495
|
});
|
535
496
|
}
|
536
|
-
cacheResponse(
|
537
|
-
return new Promise((r,
|
538
|
-
const i = this.helpers.stringify({ url:
|
539
|
-
if (this.cache.clear === "auto" &&
|
540
|
-
const
|
541
|
-
if (
|
542
|
-
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);
|
543
504
|
}
|
544
505
|
try {
|
545
506
|
(async () => {
|
546
|
-
var
|
547
|
-
const
|
548
|
-
let
|
549
|
-
if ((
|
550
|
-
perPage:
|
551
|
-
total:
|
552
|
-
})),
|
553
|
-
return
|
554
|
-
(
|
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);
|
555
516
|
})();
|
556
517
|
} catch {
|
557
518
|
}
|
558
519
|
});
|
559
520
|
}
|
560
|
-
throttledRequest(
|
561
|
-
return this.client[
|
521
|
+
throttledRequest(e, t, s) {
|
522
|
+
return this.client[e](t, s);
|
562
523
|
}
|
563
524
|
cacheVersions() {
|
564
525
|
return d;
|
@@ -566,24 +527,24 @@ class Q {
|
|
566
527
|
cacheVersion() {
|
567
528
|
return d[this.accessToken];
|
568
529
|
}
|
569
|
-
setCacheVersion(
|
570
|
-
this.accessToken && (d[this.accessToken] =
|
530
|
+
setCacheVersion(e) {
|
531
|
+
this.accessToken && (d[this.accessToken] = e);
|
571
532
|
}
|
572
533
|
cacheProvider() {
|
573
534
|
switch (this.cache.type) {
|
574
535
|
case "memory":
|
575
536
|
return {
|
576
|
-
get(
|
577
|
-
return
|
537
|
+
get(e) {
|
538
|
+
return p[e];
|
578
539
|
},
|
579
540
|
getAll() {
|
580
|
-
return
|
541
|
+
return p;
|
581
542
|
},
|
582
|
-
set(
|
583
|
-
|
543
|
+
set(e, t) {
|
544
|
+
p[e] = t;
|
584
545
|
},
|
585
546
|
flush() {
|
586
|
-
|
547
|
+
p = {};
|
587
548
|
}
|
588
549
|
};
|
589
550
|
default:
|
@@ -603,105 +564,88 @@ class Q {
|
|
603
564
|
return this.cacheProvider().flush(), this;
|
604
565
|
}
|
605
566
|
}
|
606
|
-
const
|
607
|
-
const { apiOptions } =
|
608
|
-
if (!
|
609
|
-
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
|
+
);
|
610
573
|
return;
|
611
574
|
}
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
var editable = (blok) => {
|
616
|
-
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")
|
617
578
|
return {};
|
618
|
-
|
619
|
-
|
579
|
+
const e = JSON.parse(
|
580
|
+
n._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, "")
|
581
|
+
);
|
620
582
|
return {
|
621
|
-
"data-blok-c": JSON.stringify(
|
622
|
-
"data-blok-uid":
|
583
|
+
"data-blok-c": JSON.stringify(e),
|
584
|
+
"data-blok-uid": e.id + "-" + e.uid
|
623
585
|
};
|
624
586
|
};
|
625
|
-
let
|
626
|
-
const
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
if (event.action === "input" && event.story.id === id) {
|
643
|
-
cb(event.story);
|
644
|
-
} else if ((event.action === "change" || event.action === "published") && event.storyId === id) {
|
645
|
-
window.location.reload();
|
646
|
-
}
|
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
|
+
});
|
647
604
|
});
|
648
|
-
});
|
649
|
-
};
|
650
|
-
const storyblokInit = (pluginOptions = {}) => {
|
651
|
-
const {
|
652
|
-
bridge,
|
653
|
-
accessToken,
|
654
|
-
use = [],
|
655
|
-
apiOptions = {},
|
656
|
-
richText = {}
|
657
|
-
} = pluginOptions;
|
658
|
-
apiOptions.accessToken = apiOptions.accessToken || accessToken;
|
659
|
-
const options = { bridge, apiOptions };
|
660
|
-
let result = {};
|
661
|
-
use.forEach((pluginFactory) => {
|
662
|
-
result = __spreadValues(__spreadValues({}, result), pluginFactory(options));
|
663
|
-
});
|
664
|
-
if (bridge !== false) {
|
665
|
-
loadBridge(bridgeLatest);
|
666
|
-
}
|
667
|
-
richTextResolver = new y(richText.schema);
|
668
|
-
if (richText.resolver) {
|
669
|
-
setComponentResolver(richTextResolver, richText.resolver);
|
670
605
|
}
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
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
|
681
627
|
};
|
682
628
|
});
|
683
|
-
}
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
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
|
+
);
|
688
635
|
return;
|
689
636
|
}
|
690
|
-
|
691
|
-
return "";
|
692
|
-
} else if (!data) {
|
693
|
-
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.
|
694
638
|
|
695
|
-
For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`);
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
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
|
705
651
|
};
|
706
|
-
const loadStoryblokBridge = () => loadBridge(bridgeLatest);
|
707
|
-
export { y as RichTextResolver, F 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.8",
|
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.2
|
30
|
+
"storyblok-js-client": "^5.3.2"
|
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": [
|