@storyblok/react 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,866 @@
1
+ let R = !1;
2
+ const _ = [], E = (n) => new Promise((t, e) => {
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.");
6
+ return;
7
+ }
8
+ R ? r() : _.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) => e(r), s.onload = (r) => {
13
+ _.forEach((i) => i()), R = !0, t(r);
14
+ }, document.getElementsByTagName("head")[0].appendChild(s);
15
+ });
16
+ var S = Object.defineProperty, C = (n, t, e) => t in n ? S(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, h = (n, t, e) => (C(n, typeof t != "symbol" ? t + "" : t, e), e);
17
+ function j(n) {
18
+ return !(n !== n || n === 1 / 0 || n === -1 / 0);
19
+ }
20
+ function O(n, t, e) {
21
+ if (!j(t))
22
+ throw new TypeError("Expected `limit` to be a finite number");
23
+ if (!j(e))
24
+ throw new TypeError("Expected `interval` to be a finite number");
25
+ const s = [];
26
+ let r = [], i = 0;
27
+ const o = function() {
28
+ i++;
29
+ const a = setTimeout(function() {
30
+ i--, s.length > 0 && o(), r = r.filter(function(u) {
31
+ return u !== a;
32
+ });
33
+ }, e);
34
+ r.indexOf(a) < 0 && r.push(a);
35
+ const l = s.shift();
36
+ l.resolve(n.apply(l.self, l.args));
37
+ }, c = function(...a) {
38
+ const l = this;
39
+ return new Promise(function(u, d) {
40
+ s.push({
41
+ resolve: u,
42
+ reject: d,
43
+ args: a,
44
+ self: l
45
+ }), i < t && o();
46
+ });
47
+ };
48
+ return c.abort = function() {
49
+ r.forEach(clearTimeout), r = [], s.forEach(function(a) {
50
+ a.reject(function() {
51
+ Error.call(this, "Throttled function aborted"), this.name = "AbortError";
52
+ });
53
+ }), s.length = 0;
54
+ }, c;
55
+ }
56
+ const A = function(n, t) {
57
+ const e = {};
58
+ for (const s in n) {
59
+ const r = n[s];
60
+ t.indexOf(s) > -1 && r !== null && (e[s] = r);
61
+ }
62
+ return e;
63
+ }, N = (n) => n === "email", M = () => ({
64
+ singleTag: "hr"
65
+ }), L = () => ({
66
+ tag: "blockquote"
67
+ }), z = () => ({
68
+ tag: "ul"
69
+ }), U = (n) => ({
70
+ tag: [
71
+ "pre",
72
+ {
73
+ tag: "code",
74
+ attrs: n.attrs
75
+ }
76
+ ]
77
+ }), q = () => ({
78
+ singleTag: "br"
79
+ }), H = (n) => ({
80
+ tag: `h${n.attrs.level}`
81
+ }), B = (n) => ({
82
+ singleTag: [
83
+ {
84
+ tag: "img",
85
+ attrs: A(n.attrs, ["src", "alt", "title"])
86
+ }
87
+ ]
88
+ }), V = () => ({
89
+ tag: "li"
90
+ }), D = () => ({
91
+ tag: "ol"
92
+ }), J = () => ({
93
+ tag: "p"
94
+ }), F = (n) => ({
95
+ tag: [
96
+ {
97
+ tag: "span",
98
+ attrs: {
99
+ "data-type": "emoji",
100
+ "data-name": n.attrs.name,
101
+ emoji: n.attrs.emoji
102
+ }
103
+ }
104
+ ]
105
+ }), Y = () => ({
106
+ tag: "b"
107
+ }), K = () => ({
108
+ tag: "strike"
109
+ }), W = () => ({
110
+ tag: "u"
111
+ }), G = () => ({
112
+ tag: "strong"
113
+ }), Q = () => ({
114
+ tag: "code"
115
+ }), X = () => ({
116
+ tag: "i"
117
+ }), Z = (n) => {
118
+ const t = { ...n.attrs }, { linktype: e = "url" } = n.attrs;
119
+ if (N(e) && (t.href = `mailto:${t.href}`), t.anchor && (t.href = `${t.href}#${t.anchor}`, delete t.anchor), t.custom) {
120
+ for (const s in t.custom)
121
+ t[s] = t.custom[s];
122
+ delete t.custom;
123
+ }
124
+ return {
125
+ tag: [
126
+ {
127
+ tag: "a",
128
+ attrs: t
129
+ }
130
+ ]
131
+ };
132
+ }, tt = (n) => ({
133
+ tag: [
134
+ {
135
+ tag: "span",
136
+ attrs: n.attrs
137
+ }
138
+ ]
139
+ }), et = () => ({
140
+ tag: "sub"
141
+ }), st = () => ({
142
+ tag: "sup"
143
+ }), rt = (n) => ({
144
+ tag: [
145
+ {
146
+ tag: "span",
147
+ attrs: n.attrs
148
+ }
149
+ ]
150
+ }), it = (n) => {
151
+ var t;
152
+ return (t = n.attrs) != null && t.color ? {
153
+ tag: [
154
+ {
155
+ tag: "span",
156
+ attrs: {
157
+ style: `background-color:${n.attrs.color};`
158
+ }
159
+ }
160
+ ]
161
+ } : {
162
+ tag: ""
163
+ };
164
+ }, ot = (n) => {
165
+ var t;
166
+ return (t = n.attrs) != null && t.color ? {
167
+ tag: [
168
+ {
169
+ tag: "span",
170
+ attrs: {
171
+ style: `color:${n.attrs.color}`
172
+ }
173
+ }
174
+ ]
175
+ } : {
176
+ tag: ""
177
+ };
178
+ }, nt = {
179
+ nodes: {
180
+ horizontal_rule: M,
181
+ blockquote: L,
182
+ bullet_list: z,
183
+ code_block: U,
184
+ hard_break: q,
185
+ heading: H,
186
+ image: B,
187
+ list_item: V,
188
+ ordered_list: D,
189
+ paragraph: J,
190
+ emoji: F
191
+ },
192
+ marks: {
193
+ bold: Y,
194
+ strike: K,
195
+ underline: W,
196
+ strong: G,
197
+ code: Q,
198
+ italic: X,
199
+ link: Z,
200
+ styled: tt,
201
+ subscript: et,
202
+ superscript: st,
203
+ anchor: rt,
204
+ highlight: it,
205
+ textStyle: ot
206
+ }
207
+ }, at = function(n) {
208
+ const t = {
209
+ "&": "&amp;",
210
+ "<": "&lt;",
211
+ ">": "&gt;",
212
+ '"': "&quot;",
213
+ "'": "&#39;"
214
+ }, e = /[&<>"']/g, s = RegExp(e.source);
215
+ return n && s.test(n) ? n.replace(e, (r) => t[r]) : n;
216
+ };
217
+ class y {
218
+ constructor(t) {
219
+ h(this, "marks"), h(this, "nodes"), t || (t = nt), this.marks = t.marks || [], this.nodes = t.nodes || [];
220
+ }
221
+ addNode(t, e) {
222
+ this.nodes[t] = e;
223
+ }
224
+ addMark(t, e) {
225
+ this.marks[t] = e;
226
+ }
227
+ render(t, e = { optimizeImages: !1 }) {
228
+ if (t && t.content && Array.isArray(t.content)) {
229
+ let s = "";
230
+ return t.content.forEach((r) => {
231
+ s += this.renderNode(r);
232
+ }), e.optimizeImages ? this.optimizeImages(s, e.optimizeImages) : s;
233
+ }
234
+ return console.warn(
235
+ `The render method must receive an Object with a "content" field.
236
+ The "content" field must be an array of nodes as the type ISbRichtext.
237
+ ISbRichtext:
238
+ content?: ISbRichtext[]
239
+ marks?: ISbRichtext[]
240
+ attrs?: any
241
+ text?: string
242
+ type: string
243
+
244
+ Example:
245
+ {
246
+ content: [
247
+ {
248
+ content: [
249
+ {
250
+ text: 'Hello World',
251
+ type: 'text'
252
+ }
253
+ ],
254
+ type: 'paragraph'
255
+ }
256
+ ],
257
+ type: 'doc'
258
+ }`
259
+ ), "";
260
+ }
261
+ optimizeImages(t, e) {
262
+ let s = 0, r = 0, i = "", o = "";
263
+ typeof e != "boolean" && (typeof e.width == "number" && e.width > 0 && (i += `width="${e.width}" `, s = e.width), typeof e.height == "number" && e.height > 0 && (i += `height="${e.height}" `, r = e.height), (e.loading === "lazy" || e.loading === "eager") && (i += `loading="${e.loading}" `), typeof e.class == "string" && e.class.length > 0 && (i += `class="${e.class}" `), e.filters && (typeof e.filters.blur == "number" && e.filters.blur >= 0 && e.filters.blur <= 100 && (o += `:blur(${e.filters.blur})`), typeof e.filters.brightness == "number" && e.filters.brightness >= -100 && e.filters.brightness <= 100 && (o += `:brightness(${e.filters.brightness})`), e.filters.fill && (e.filters.fill.match(/[0-9A-Fa-f]{6}/g) || e.filters.fill === "transparent") && (o += `:fill(${e.filters.fill})`), e.filters.format && ["webp", "png", "jpeg"].includes(e.filters.format) && (o += `:format(${e.filters.format})`), typeof e.filters.grayscale == "boolean" && e.filters.grayscale && (o += ":grayscale()"), typeof e.filters.quality == "number" && e.filters.quality >= 0 && e.filters.quality <= 100 && (o += `:quality(${e.filters.quality})`), e.filters.rotate && [90, 180, 270].includes(e.filters.rotate) && (o += `:rotate(${e.filters.rotate})`), o.length > 0 && (o = "/filters" + o))), i.length > 0 && (t = t.replace(/<img/g, `<img ${i.trim()}`));
264
+ const c = s > 0 || r > 0 || o.length > 0 ? `${s}x${r}${o}` : "";
265
+ return t = t.replace(
266
+ /a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,
267
+ `a.storyblok.com/f/$1/$2.$3/m/${c}`
268
+ ), typeof e != "boolean" && (e.sizes || e.srcset) && (t = t.replace(/<img.*?src=["|'](.*?)["|']/g, (a) => {
269
+ var l, u;
270
+ const d = a.match(
271
+ /a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g
272
+ );
273
+ if (d && d.length > 0) {
274
+ const g = {
275
+ srcset: (l = e.srcset) == null ? void 0 : l.map((p) => {
276
+ if (typeof p == "number")
277
+ return `//${d}/m/${p}x0${o} ${p}w`;
278
+ if (typeof p == "object" && p.length === 2) {
279
+ let v = 0, T = 0;
280
+ return typeof p[0] == "number" && (v = p[0]), typeof p[1] == "number" && (T = p[1]), `//${d}/m/${v}x${T}${o} ${v}w`;
281
+ }
282
+ }).join(", "),
283
+ sizes: (u = e.sizes) == null ? void 0 : u.map((p) => p).join(", ")
284
+ };
285
+ let b = "";
286
+ return g.srcset && (b += `srcset="${g.srcset}" `), g.sizes && (b += `sizes="${g.sizes}" `), a.replace(/<img/g, `<img ${b.trim()}`);
287
+ }
288
+ return a;
289
+ })), t;
290
+ }
291
+ renderNode(t) {
292
+ const e = [];
293
+ t.marks && t.marks.forEach((r) => {
294
+ const i = this.getMatchingMark(r);
295
+ i && i.tag !== "" && e.push(this.renderOpeningTag(i.tag));
296
+ });
297
+ const s = this.getMatchingNode(t);
298
+ return s && s.tag && e.push(this.renderOpeningTag(s.tag)), t.content ? t.content.forEach((r) => {
299
+ e.push(this.renderNode(r));
300
+ }) : t.text ? e.push(at(t.text)) : s && s.singleTag ? e.push(this.renderTag(s.singleTag, " /")) : s && s.html ? e.push(s.html) : t.type === "emoji" && e.push(this.renderEmoji(t)), s && s.tag && e.push(this.renderClosingTag(s.tag)), t.marks && t.marks.slice(0).reverse().forEach((r) => {
301
+ const i = this.getMatchingMark(r);
302
+ i && i.tag !== "" && e.push(this.renderClosingTag(i.tag));
303
+ }), e.join("");
304
+ }
305
+ renderTag(t, e) {
306
+ return t.constructor === String ? `<${t}${e}>` : t.map((s) => {
307
+ if (s.constructor === String)
308
+ return `<${s}${e}>`;
309
+ {
310
+ let r = `<${s.tag}`;
311
+ if (s.attrs)
312
+ for (const i in s.attrs) {
313
+ const o = s.attrs[i];
314
+ o !== null && (r += ` ${i}="${o}"`);
315
+ }
316
+ return `${r}${e}>`;
317
+ }
318
+ }).join("");
319
+ }
320
+ renderOpeningTag(t) {
321
+ return this.renderTag(t, "");
322
+ }
323
+ renderClosingTag(t) {
324
+ return t.constructor === String ? `</${t}>` : t.slice(0).reverse().map((e) => e.constructor === String ? `</${e}>` : `</${e.tag}>`).join("");
325
+ }
326
+ getMatchingNode(t) {
327
+ const e = this.nodes[t.type];
328
+ if (typeof e == "function")
329
+ return e(t);
330
+ }
331
+ getMatchingMark(t) {
332
+ const e = this.marks[t.type];
333
+ if (typeof e == "function")
334
+ return e(t);
335
+ }
336
+ renderEmoji(t) {
337
+ if (t.attrs.emoji)
338
+ return t.attrs.emoji;
339
+ const e = [
340
+ {
341
+ tag: "img",
342
+ attrs: {
343
+ src: t.attrs.fallbackImage,
344
+ draggable: "false",
345
+ loading: "lazy",
346
+ align: "absmiddle"
347
+ }
348
+ }
349
+ ];
350
+ return this.renderTag(e, " /");
351
+ }
352
+ }
353
+ class w {
354
+ constructor() {
355
+ h(this, "isCDNUrl", (t = "") => t.indexOf("/cdn/") > -1), h(this, "getOptionsPage", (t, e = 25, s = 1) => ({
356
+ ...t,
357
+ per_page: e,
358
+ page: s
359
+ })), h(this, "delay", (t) => new Promise((e) => setTimeout(e, t))), h(this, "arrayFrom", (t = 0, e) => [...Array(t)].map(e)), h(this, "range", (t = 0, e = t) => {
360
+ const s = Math.abs(e - t) || 0, r = t < e ? 1 : -1;
361
+ return this.arrayFrom(s, (i, o) => o * r + t);
362
+ }), h(this, "asyncMap", async (t, e) => Promise.all(t.map(e))), h(this, "flatMap", (t = [], e) => t.map(e).reduce((s, r) => [...s, ...r], []));
363
+ }
364
+ /**
365
+ * @method stringify
366
+ * @param {Object} params
367
+ * @param {String} prefix
368
+ * @param {Boolean} isArray
369
+ * @return {String} Stringified object
370
+ */
371
+ stringify(t, e, s) {
372
+ const r = [];
373
+ for (const i in t) {
374
+ if (!Object.prototype.hasOwnProperty.call(t, i))
375
+ continue;
376
+ const o = t[i], c = s ? "" : encodeURIComponent(i);
377
+ let a;
378
+ typeof o == "object" ? a = this.stringify(
379
+ o,
380
+ e ? e + encodeURIComponent("[" + c + "]") : c,
381
+ Array.isArray(o)
382
+ ) : a = (e ? e + encodeURIComponent("[" + c + "]") : c) + "=" + encodeURIComponent(o), r.push(a);
383
+ }
384
+ return r.join("&");
385
+ }
386
+ /**
387
+ * @method getRegionURL
388
+ * @param {String} regionCode region code, could be eu, us or cn
389
+ * @return {String} The base URL of the region
390
+ */
391
+ getRegionURL(t) {
392
+ const e = "api.storyblok.com", s = "api-us.storyblok.com", r = "app.storyblokchina.cn";
393
+ switch (t) {
394
+ case "us":
395
+ return s;
396
+ case "cn":
397
+ return r;
398
+ default:
399
+ return e;
400
+ }
401
+ }
402
+ }
403
+ class lt {
404
+ constructor(t) {
405
+ h(this, "baseURL"), h(this, "timeout"), h(this, "headers"), h(this, "responseInterceptor"), h(this, "fetch"), h(this, "ejectInterceptor"), h(this, "url"), h(this, "parameters"), this.baseURL = t.baseURL, this.headers = t.headers || new 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 = {};
406
+ }
407
+ /**
408
+ *
409
+ * @param url string
410
+ * @param params ISbStoriesParams
411
+ * @returns Promise<ISbResponse | Error>
412
+ */
413
+ get(t, e) {
414
+ return this.url = t, this.parameters = e, this._methodHandler("get");
415
+ }
416
+ post(t, e) {
417
+ return this.url = t, this.parameters = e, this._methodHandler("post");
418
+ }
419
+ put(t, e) {
420
+ return this.url = t, this.parameters = e, this._methodHandler("put");
421
+ }
422
+ delete(t, e) {
423
+ return this.url = t, this.parameters = e, this._methodHandler("delete");
424
+ }
425
+ async _responseHandler(t) {
426
+ const e = [], s = {
427
+ data: {},
428
+ headers: {},
429
+ status: 0,
430
+ statusText: ""
431
+ };
432
+ t.status !== 204 && await t.json().then((r) => {
433
+ s.data = r;
434
+ });
435
+ for (const r of t.headers.entries())
436
+ e[r[0]] = r[1];
437
+ return s.headers = { ...e }, s.status = t.status, s.statusText = t.statusText, s;
438
+ }
439
+ async _methodHandler(t) {
440
+ let e = `${this.baseURL}${this.url}`, s = null;
441
+ if (t === "get") {
442
+ const a = new w();
443
+ e = `${this.baseURL}${this.url}?${a.stringify(
444
+ this.parameters
445
+ )}`;
446
+ } else
447
+ s = JSON.stringify(this.parameters);
448
+ const r = new URL(e), i = new AbortController(), { signal: o } = i;
449
+ let c;
450
+ this.timeout && (c = setTimeout(() => i.abort(), this.timeout));
451
+ try {
452
+ const a = await this.fetch(`${r}`, {
453
+ method: t,
454
+ headers: this.headers,
455
+ body: s,
456
+ signal: o
457
+ });
458
+ this.timeout && clearTimeout(c);
459
+ const l = await this._responseHandler(a);
460
+ return this.responseInterceptor && !this.ejectInterceptor ? this._statusHandler(this.responseInterceptor(l)) : this._statusHandler(l);
461
+ } catch (a) {
462
+ return {
463
+ message: a
464
+ };
465
+ }
466
+ }
467
+ eject() {
468
+ this.ejectInterceptor = !0;
469
+ }
470
+ _statusHandler(t) {
471
+ const e = /20[0-6]/g;
472
+ return new Promise((s, r) => {
473
+ if (e.test(`${t.status}`))
474
+ return s(t);
475
+ const i = {
476
+ message: new Error(t.statusText),
477
+ status: t.status,
478
+ response: Array.isArray(t.data) ? t.data[0] : t.data.error || t.data.slug
479
+ };
480
+ r(i);
481
+ });
482
+ }
483
+ }
484
+ const x = "SB-Agent", k = {
485
+ defaultAgentName: "SB-JS-CLIENT",
486
+ defaultAgentVersion: "SB-Agent-Version",
487
+ packageVersion: "2.2.3"
488
+ };
489
+ let m = {};
490
+ const f = {};
491
+ class ct {
492
+ /**
493
+ *
494
+ * @param config ISbConfig interface
495
+ * @param endpoint string, optional
496
+ */
497
+ constructor(t, e) {
498
+ h(this, "client"), h(this, "maxRetries"), h(this, "throttle"), h(this, "accessToken"), h(this, "cache"), h(this, "helpers"), h(this, "resolveCounter"), h(this, "relations"), h(this, "links"), h(this, "richTextResolver"), h(this, "resolveNestedRelations");
499
+ let s = t.endpoint || e;
500
+ if (!s) {
501
+ const o = new w().getRegionURL, c = t.https === !1 ? "http" : "https";
502
+ t.oauthToken ? s = `${c}://${o(t.region)}/v1` : s = `${c}://${o(t.region)}/v2`;
503
+ }
504
+ const r = new Headers();
505
+ if (r.set("Content-Type", "application/json"), r.set("Accept", "application/json"), t.headers)
506
+ for (const o in t.headers)
507
+ r.set(o, t.headers[o]);
508
+ r.has(x) || (r.set(x, k.defaultAgentName), r.set(k.defaultAgentVersion, k.packageVersion));
509
+ let i = 5;
510
+ t.oauthToken && (r.set("Authorization", t.oauthToken), i = 3), t.rateLimit && (i = t.rateLimit), t.richTextSchema ? this.richTextResolver = new y(t.richTextSchema) : this.richTextResolver = new y(), t.componentResolver && this.setComponentResolver(t.componentResolver), this.maxRetries = t.maxRetries || 5, this.throttle = O(this.throttledRequest, i, 1e3), this.accessToken = t.accessToken || "", this.relations = {}, this.links = {}, this.cache = t.cache || { clear: "manual" }, this.helpers = new w(), this.resolveCounter = 0, this.resolveNestedRelations = t.resolveNestedRelations || !0, this.client = new lt({
511
+ baseURL: s,
512
+ timeout: t.timeout || 0,
513
+ headers: r,
514
+ responseInterceptor: t.responseInterceptor,
515
+ fetch: t.fetch
516
+ });
517
+ }
518
+ setComponentResolver(t) {
519
+ this.richTextResolver.addNode("blok", (e) => {
520
+ let s = "";
521
+ return e.attrs.body && e.attrs.body.forEach((r) => {
522
+ s += t(r.component, r);
523
+ }), {
524
+ html: s
525
+ };
526
+ });
527
+ }
528
+ parseParams(t) {
529
+ return t.version || (t.version = "published"), t.token || (t.token = this.getToken()), t.cv || (t.cv = f[t.token]), Array.isArray(t.resolve_relations) && (t.resolve_relations = t.resolve_relations.join(",")), t;
530
+ }
531
+ factoryParamOptions(t, e) {
532
+ return this.helpers.isCDNUrl(t) ? this.parseParams(e) : e;
533
+ }
534
+ makeRequest(t, e, s, r) {
535
+ const i = this.factoryParamOptions(
536
+ t,
537
+ this.helpers.getOptionsPage(e, s, r)
538
+ );
539
+ return this.cacheResponse(t, i);
540
+ }
541
+ get(t, e) {
542
+ e || (e = {});
543
+ const s = `/${t}`, r = this.factoryParamOptions(s, e);
544
+ return this.cacheResponse(s, r);
545
+ }
546
+ async getAll(t, e, s) {
547
+ const r = (e == null ? void 0 : e.per_page) || 25, i = `/${t}`, o = i.split("/"), c = s || o[o.length - 1], a = 1, l = await this.makeRequest(i, e, r, a), u = l.total ? Math.ceil(l.total / r) : 1, d = await this.helpers.asyncMap(
548
+ this.helpers.range(a, u),
549
+ (g) => this.makeRequest(i, e, r, g + 1)
550
+ );
551
+ return this.helpers.flatMap(
552
+ [l, ...d],
553
+ (g) => Object.values(g.data[c])
554
+ );
555
+ }
556
+ post(t, e) {
557
+ const s = `/${t}`;
558
+ return Promise.resolve(this.throttle("post", s, e));
559
+ }
560
+ put(t, e) {
561
+ const s = `/${t}`;
562
+ return Promise.resolve(this.throttle("put", s, e));
563
+ }
564
+ delete(t, e) {
565
+ const s = `/${t}`;
566
+ return Promise.resolve(this.throttle("delete", s, e));
567
+ }
568
+ getStories(t) {
569
+ return this.get("cdn/stories", t);
570
+ }
571
+ getStory(t, e) {
572
+ return this.get(`cdn/stories/${t}`, e);
573
+ }
574
+ getToken() {
575
+ return this.accessToken;
576
+ }
577
+ ejectInterceptor() {
578
+ this.client.eject();
579
+ }
580
+ _cleanCopy(t) {
581
+ return JSON.parse(JSON.stringify(t));
582
+ }
583
+ _insertLinks(t, e, s) {
584
+ const r = t[e];
585
+ r && r.fieldtype == "multilink" && r.linktype == "story" && typeof r.id == "string" && this.links[s][r.id] ? r.story = this._cleanCopy(this.links[s][r.id]) : r && r.linktype === "story" && typeof r.uuid == "string" && this.links[s][r.uuid] && (r.story = this._cleanCopy(this.links[s][r.uuid]));
586
+ }
587
+ _insertRelations(t, e, s, r) {
588
+ if (s.indexOf(`${t.component}.${e}`) > -1) {
589
+ if (typeof t[e] == "string")
590
+ this.relations[r][t[e]] && (t[e] = this._cleanCopy(
591
+ this.relations[r][t[e]]
592
+ ));
593
+ else if (t[e] && t[e].constructor === Array) {
594
+ const i = [];
595
+ t[e].forEach((o) => {
596
+ this.relations[r][o] && i.push(this._cleanCopy(this.relations[r][o]));
597
+ }), t[e] = i;
598
+ }
599
+ }
600
+ }
601
+ iterateTree(t, e, s) {
602
+ const r = (i) => {
603
+ if (i != null) {
604
+ if (i.constructor === Array)
605
+ for (let o = 0; o < i.length; o++)
606
+ r(i[o]);
607
+ else if (i.constructor === Object) {
608
+ if (i._stopResolving)
609
+ return;
610
+ for (const o in i)
611
+ (i.component && i._uid || i.type === "link") && (this._insertRelations(
612
+ i,
613
+ o,
614
+ e,
615
+ s
616
+ ), this._insertLinks(
617
+ i,
618
+ o,
619
+ s
620
+ )), r(i[o]);
621
+ }
622
+ }
623
+ };
624
+ r(t.content);
625
+ }
626
+ async resolveLinks(t, e, s) {
627
+ let r = [];
628
+ if (t.link_uuids) {
629
+ const i = t.link_uuids.length, o = [], c = 50;
630
+ for (let a = 0; a < i; a += c) {
631
+ const l = Math.min(i, a + c);
632
+ o.push(t.link_uuids.slice(a, l));
633
+ }
634
+ for (let a = 0; a < o.length; a++)
635
+ (await this.getStories({
636
+ per_page: c,
637
+ language: e.language,
638
+ version: e.version,
639
+ by_uuids: o[a].join(",")
640
+ })).data.stories.forEach(
641
+ (l) => {
642
+ r.push(l);
643
+ }
644
+ );
645
+ } else
646
+ r = t.links;
647
+ r.forEach((i) => {
648
+ this.links[s][i.uuid] = {
649
+ ...i,
650
+ _stopResolving: !0
651
+ };
652
+ });
653
+ }
654
+ async resolveRelations(t, e, s) {
655
+ let r = [];
656
+ if (t.rel_uuids) {
657
+ const i = t.rel_uuids.length, o = [], c = 50;
658
+ for (let a = 0; a < i; a += c) {
659
+ const l = Math.min(i, a + c);
660
+ o.push(t.rel_uuids.slice(a, l));
661
+ }
662
+ for (let a = 0; a < o.length; a++)
663
+ (await this.getStories({
664
+ per_page: c,
665
+ language: e.language,
666
+ version: e.version,
667
+ by_uuids: o[a].join(",")
668
+ })).data.stories.forEach((l) => {
669
+ r.push(l);
670
+ });
671
+ } else
672
+ r = t.rels;
673
+ r && r.length > 0 && r.forEach((i) => {
674
+ this.relations[s][i.uuid] = {
675
+ ...i,
676
+ _stopResolving: !0
677
+ };
678
+ });
679
+ }
680
+ async resolveStories(t, e, s) {
681
+ var r, i;
682
+ let o = [];
683
+ if (this.links[s] = {}, this.relations[s] = {}, 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, s)), e.resolve_links && ["1", "story", "url"].indexOf(e.resolve_links) > -1 && ((r = t.links) != null && r.length || (i = t.link_uuids) != null && i.length) && await this.resolveLinks(t, e, s), this.resolveNestedRelations)
684
+ for (const c in this.relations[s])
685
+ this.iterateTree(
686
+ this.relations[s][c],
687
+ o,
688
+ s
689
+ );
690
+ t.story ? this.iterateTree(t.story, o, s) : t.stories.forEach((c) => {
691
+ this.iterateTree(c, o, s);
692
+ }), delete this.links[s], delete this.relations[s];
693
+ }
694
+ async cacheResponse(t, e, s) {
695
+ const r = this.helpers.stringify({ url: t, params: e }), i = this.cacheProvider();
696
+ if (this.cache.clear === "auto" && e.version === "draft" && await this.flushCache(), e.version === "published" && t != "/cdn/spaces/me") {
697
+ const o = await i.get(r);
698
+ if (o)
699
+ return Promise.resolve(o);
700
+ }
701
+ return new Promise((o, c) => {
702
+ try {
703
+ (async () => {
704
+ var a;
705
+ try {
706
+ const l = await this.throttle("get", t, e);
707
+ let u = { data: l.data, headers: l.headers };
708
+ if ((a = l.headers) != null && a["per-page"] && (u = Object.assign({}, u, {
709
+ perPage: l.headers["per-page"] ? parseInt(l.headers["per-page"]) : 0,
710
+ total: l.headers["per-page"] ? parseInt(l.headers.total) : 0
711
+ })), l.status != 200)
712
+ return c(l);
713
+ if (u.data.story || u.data.stories) {
714
+ const d = this.resolveCounter = ++this.resolveCounter % 1e3;
715
+ await this.resolveStories(u.data, e, `${d}`);
716
+ }
717
+ return e.version === "published" && t != "/cdn/spaces/me" && await i.set(r, u), u.data.cv && e.token && (e.version == "draft" && f[e.token] != u.data.cv && await this.flushCache(), f[e.token] = u.data.cv), o(u);
718
+ } catch (l) {
719
+ return c(l);
720
+ }
721
+ })();
722
+ } catch {
723
+ }
724
+ });
725
+ }
726
+ throttledRequest(t, e, s) {
727
+ return this.client[t](e, s);
728
+ }
729
+ cacheVersions() {
730
+ return f;
731
+ }
732
+ cacheVersion() {
733
+ return f[this.accessToken];
734
+ }
735
+ setCacheVersion(t) {
736
+ this.accessToken && (f[this.accessToken] = t);
737
+ }
738
+ cacheProvider() {
739
+ switch (this.cache.type) {
740
+ case "memory":
741
+ return {
742
+ get(t) {
743
+ return Promise.resolve(m[t]);
744
+ },
745
+ getAll() {
746
+ return Promise.resolve(m);
747
+ },
748
+ set(t, e) {
749
+ return m[t] = e, Promise.resolve(void 0);
750
+ },
751
+ flush() {
752
+ return m = {}, Promise.resolve(void 0);
753
+ }
754
+ };
755
+ case "custom":
756
+ if (this.cache.custom)
757
+ return this.cache.custom;
758
+ default:
759
+ return {
760
+ get() {
761
+ return Promise.resolve(void 0);
762
+ },
763
+ getAll() {
764
+ return Promise.resolve(void 0);
765
+ },
766
+ set() {
767
+ return Promise.resolve(void 0);
768
+ },
769
+ flush() {
770
+ return Promise.resolve(void 0);
771
+ }
772
+ };
773
+ }
774
+ }
775
+ async flushCache() {
776
+ return await this.cacheProvider().flush(), this;
777
+ }
778
+ }
779
+ const ut = (n = {}) => {
780
+ const { apiOptions: t } = n;
781
+ if (!t.accessToken) {
782
+ console.error(
783
+ "You need to provide an access token to interact with Storyblok API. Read https://www.storyblok.com/docs/api/content-delivery#topics/authentication"
784
+ );
785
+ return;
786
+ }
787
+ return { storyblokApi: new ct(t) };
788
+ }, dt = (n) => {
789
+ if (typeof n != "object" || typeof n._editable > "u")
790
+ return {};
791
+ const t = JSON.parse(
792
+ n._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, "")
793
+ );
794
+ return {
795
+ "data-blok-c": JSON.stringify(t),
796
+ "data-blok-uid": t.id + "-" + t.uid
797
+ };
798
+ };
799
+ let $;
800
+ const P = "https://app.storyblok.com/f/storyblok-v2-latest.js", pt = (n, t, e = {}) => {
801
+ var s;
802
+ const r = !(typeof window > "u") && typeof window.storyblokRegisterEvent < "u", i = +new URL((s = window.location) == null ? void 0 : s.href).searchParams.get(
803
+ "_storyblok"
804
+ ) === n;
805
+ if (!(!r || !i)) {
806
+ if (!n) {
807
+ console.warn("Story ID is not defined. Please provide a valid ID.");
808
+ return;
809
+ }
810
+ window.storyblokRegisterEvent(() => {
811
+ new window.StoryblokBridge(e).on(["input", "published", "change"], (o) => {
812
+ o.action === "input" && o.story.id === n ? t(o.story) : (o.action === "change" || o.action === "published") && o.storyId === n && window.location.reload();
813
+ });
814
+ });
815
+ }
816
+ }, gt = (n = {}) => {
817
+ var t, e;
818
+ const {
819
+ bridge: s,
820
+ accessToken: r,
821
+ use: i = [],
822
+ apiOptions: o = {},
823
+ richText: c = {}
824
+ } = n;
825
+ o.accessToken = o.accessToken || r;
826
+ const a = { bridge: s, apiOptions: o };
827
+ let l = {};
828
+ i.forEach((d) => {
829
+ l = { ...l, ...d(a) };
830
+ });
831
+ const u = !(typeof window > "u") && ((e = (t = window.location) == null ? void 0 : t.search) == null ? void 0 : e.includes("_storyblok_tk"));
832
+ return s !== !1 && u && E(P), $ = new y(c.schema), c.resolver && I($, c.resolver), l;
833
+ }, I = (n, t) => {
834
+ n.addNode("blok", (e) => {
835
+ let s = "";
836
+ return e.attrs.body.forEach((r) => {
837
+ s += t(r.component, r);
838
+ }), {
839
+ html: s
840
+ };
841
+ });
842
+ }, ht = (n) => {
843
+ var t, e;
844
+ return !n || ((t = n == null ? void 0 : n.content) == null ? void 0 : t[0].type) !== "blok" && !((e = n == null ? void 0 : n.content) != null && e[0].content);
845
+ }, ft = (n, t, e) => {
846
+ let s = e || $;
847
+ if (!s) {
848
+ console.error(
849
+ "Please initialize the Storyblok SDK before calling the renderRichText function"
850
+ );
851
+ return;
852
+ }
853
+ return ht(n) ? "" : (t && (s = new y(t.schema), t.resolver && I(s, t.resolver)), s.render(n));
854
+ }, mt = () => E(P);
855
+ export {
856
+ y as RichTextResolver,
857
+ nt as RichTextSchema,
858
+ ut as apiPlugin,
859
+ ht as isRichTextEmpty,
860
+ mt as loadStoryblokBridge,
861
+ pt as registerStoryblokBridge,
862
+ ft as renderRichText,
863
+ dt as storyblokEditable,
864
+ gt as storyblokInit,
865
+ pt as useStoryblokBridge
866
+ };