@localazy/cdn-client 1.5.12 → 1.5.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,11 +11,11 @@
11
11
 
12
12
  Node.js module that allows you to easily interact with the [Localazy CDN](https://localazy.com/docs/cdn/cdn-introduction).
13
13
 
14
- [![npm](.github/badges/npm.svg)](https://www.npmjs.com/package/@localazy/cdn-client)
15
- [![maintainer](.github/badges/maintainer.svg)](https://github.com/localazy)
16
- [![license](.github/badges/license.svg)](https://github.com/localazy/cdn-client/blob/main/LICENSE)<br>
17
- [![coverage](.github/badges/coverage.svg)](https://github.com/localazy/cdn-client/actions)
18
- [![size](.github/badges/size.svg)](https://bundlephobia.com/package/@localazy/cdn-client)
14
+ [![npm](https://raw.githubusercontent.com/localazy/cdn-client/main/.github/badges/npm.svg)](https://www.npmjs.com/package/@localazy/cdn-client)
15
+ [![maintainer](https://raw.githubusercontent.com/localazy/cdn-client/main/.github/badges/maintainer.svg)](https://github.com/localazy)
16
+ [![license](https://raw.githubusercontent.com/localazy/cdn-client/main/.github/badges/license.svg)](https://github.com/localazy/cdn-client/blob/main/LICENSE)<br>
17
+ [![coverage](https://raw.githubusercontent.com/localazy/cdn-client/main/.github/badges/coverage.svg)](https://github.com/localazy/cdn-client/actions)
18
+ [![size](https://raw.githubusercontent.com/localazy/cdn-client/main/.github/badges/size.svg)](https://bundlephobia.com/package/@localazy/cdn-client)
19
19
 
20
20
  </div>
21
21
 
@@ -89,4 +89,4 @@ Discover all available [integration options](https://github.com/localazy) and [l
89
89
 
90
90
  ## 📜 License
91
91
 
92
- Code released under the [MIT license](LICENSE).
92
+ Code released under the [MIT license](https://github.com/localazy/cdn-client/blob/main/packages/cdn-client/LICENSE).
@@ -1,2 +1,419 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).LocalazyCDN={})}(this,function(e){"use strict";class t{context;constructor(e){this.context=e}async fetchLocale(e){return this.context.cache.has(e)?new Promise(t=>{t(this.context.cache.get(e))}):this.context.client.get(e.metafileLocale.uri)}async fetchMetafile(){return await this.context.client.get(this.context.metafile.params.jsonPath)}}class s{map;constructor(){this.map=new Map}get(e){return this.map.get(e)}has(e){return this.map.has(e)}set(e,t){this.map.set(e,t)}flush(){this.map=new Map}}const a=e=>"string"==typeof e,i=e=>void 0===e,l=e=>Array.isArray(e),r=e=>"[object Object]"===Object.prototype.toString.call(e),o=e=>[...new Set(e)],n=(e,t)=>{const s={};return e.filter(e=>{const a=t(e);return!Object.hasOwn(s,a)&&(s[a]=!0,!0)})};class c{context;cacheAdapter;constructor(e){this.context=e,this.cacheAdapter=new s}setIfMissed(e){const{metafileFile:t,metafileLocale:s,data:a}=e,i=this.keyFromMetafile({metafileFile:t,metafileLocale:s});this.cacheAdapter.has(i)||this.cacheAdapter.set(i,a)}has(e){const t=this.keyFromMetafile(e);return this.cacheAdapter.has(t)}get(e){const t=this.keyFromMetafile(e);return this.cacheAdapter.get(t)}flush(){this.cacheAdapter.flush()}keyFromMetafile(e){const{metafileFile:t,metafileLocale:s}=e,a=[...o(t.productFlavors)].sort().join("-");return[this.context.metafile.params.cdnId,t.id,t.file,t.path,t.library,t.module,t.buildType,a,s.locale,s.timestamp.toString()].filter(e=>""!==e).join("-")}}class h{context;constructor(e){this.context=e}createCdnResponse(e){const{requests:t,responses:s,hasSingleFileResponse:a,hasSingleLocaleResponse:i}=e;return 0===s.length||void 0===s[0]?{}:(this.cacheResponses(t,s),a&&i?s[0]:h.transformResponses(e))}cacheResponses(e,t){t.forEach((t,s)=>{if(void 0!==e[s]){const{metafileFile:a,metafileLocale:i}=e[s];a&&i&&this.context.cache.setIfMissed({metafileFile:a,metafileLocale:i,data:t})}})}static transformResponses(e){const{requests:t,responses:s,hasSingleFileResponse:a}=e;return s.reduce((e,s,i)=>{if(void 0!==t[i]){const{metafileFile:l,metafileLocale:r}=t[i];l&&r&&(a?e[r.locale]=s:(e[l.id]||(e[l.id]={}),e[l.id][r.locale]=s))}return e},{})}}class u{metafile;cdn;client;api;cache;responseFactory;constructor(e){this.metafile=e.metafileContext,this.cdn=e.cdn,this.client=e.client,this.api=new t(this),this.cache=new c(this),this.responseFactory=new h(this)}}class d{id;file;path;library;module;buildType;timestamp;productFlavors;locales;baseUrl;constructor(e){this.id=e.id,this.file=e.file,this.path=e.path,this.library=e.library,this.module=e.module,this.buildType=e.buildType,this.timestamp=e.timestamp,this.productFlavors=e.productFlavors,this.locales=e.locales,this.baseUrl=e.baseUrl}toCdnFile(){return{id:this.id,file:this.file,path:this.path,library:this.library,module:this.module,buildType:this.buildType,productFlavors:this.productFlavors,locales:this.locales.map(e=>({locale:e.locale,isBaseLocale:e.isBaseLocale,uri:`${this.baseUrl}${e.uri}`}))}}}class f{language;region;script;isRtl;name;localizedName;uri;timestamp;baseLocale;constructor(e,t){this.language=e.language,this.region=e.region,this.script=e.script,this.isRtl=e.isRtl,this.name=e.name,this.localizedName=e.localizedName,this.uri=e.uri,this.timestamp=e.timestamp,this.baseLocale=t}get locale(){return this.language&&this.region&&this.script?`${this.language}_${this.region}#${this.script}`:this.language&&this.script?`${this.language}#${this.script}`:this.language&&this.region?`${this.language}_${this.region}`:this.language}get isBaseLocale(){return this.locale===this.baseLocale}toCdnLocale(){return{locale:this.locale,isBaseLocale:this.isBaseLocale,language:this.language,region:this.region,script:this.script,isRtl:this.isRtl,name:this.name,localizedName:this.localizedName}}}class p{projectUrl;baseLocale;locales;timestamp;files;filesMap;constructor(e,t){this.projectUrl=e.projectUrl,this.timestamp=e.timestamp,this.files=p.filesFactory(e.files,e.baseLocale,t),this.filesMap=p.filesMapFactory(this.files),this.locales=p.localesFactory(this.files),this.baseLocale=this.locales.find(e=>e.isBaseLocale)}static createEmpty(e){return new p({projectUrl:"",baseLocale:"",timestamp:0,files:{}},e)}static filesFactory(e,t,s){return Object.keys(e).reduce((a,i)=>{if(void 0!==e[i]){const l=e[i].locales.map(e=>new f(e,t));a.push(new d({...e[i],id:i,locales:l,baseUrl:s.baseUrl}))}return a},[])}static filesMapFactory(e){return e.reduce((e,t)=>(e[t.id]=t,e),{})}static localesFactory(e){const t=e.reduce((e,t)=>(e.push(...t.locales.map(e=>e.toCdnLocale())),e),[]);return n(t,e=>e.locale)}}class m{options;constructor(e){this.options=m.parseMetafileUrl(e)}get url(){return this.options.url}get baseUrl(){return this.options.baseUrl}get cdnId(){return this.options.cdnId}get jsonPath(){return this.options.jsonPath}static parseMetafileUrl(e){let t;try{t=new URL(e)}catch{throw new Error('Invalid param: "options.metafile" cannot be parsed as url.')}const s=/^\/(.*?)\/(.*?)\.(v2.json|js|ts)$/.exec(t.pathname);if(null===s||4!==s.length||void 0===s[1]||void 0===s[2])throw new Error('Invalid param: "options.metafile" contains invalid metafile url.');const a=s[1],i=s[2];return{url:e,baseUrl:t.origin,cdnId:a,jsonPath:`/${a}/${i}.v2.json`}}}class g{params;parsedData;data;constructor(e){this.params=new m(e.metafile),this.parsedData=null,this.data=p.createEmpty(this.params)}setMetafile(e){this.parsedData=e,this.data=new p(e,this.params)}}class w{baseUrl;constructor(e){this.baseUrl=e}async get(e){const t=await fetch(`${this.baseUrl}${e}`),s=t.headers.get("content-type"),a="application/json5"===s||"application/json"===s;if(t.status>=400)throw new Error(`Request failed with status code ${t.status.toString()}`);let i;return i=a?await t.json():await t.text(),i}}class x{context;constructor(e){this.context=e}}class y extends x{flush=()=>{this.context.cache.flush()}}class b extends x{get projectUrl(){return this.context.metafile.data.projectUrl}get baseLocale(){return this.context.metafile.data.baseLocale}get url(){return this.context.metafile.params.url}get files(){return this.context.metafile.data.files.map(e=>e.toCdnFile())}locales=e=>{const{excludeBaseLocale:t}=e||{},{locales:s}=this.context.metafile.data;return t?s.filter(e=>!e.isBaseLocale):s};refresh=async()=>{const e=await this.context.api.fetchMetafile();this.context.metafile.setMetafile(e)};switch=async e=>{this.context.metafile.params=new m(e.metafile),await this.refresh()}}class F{data;context;constructor(e){this.context=e.context,this.data=e.data||{}}}class L{files;localesMap;hasSingleFileResponse;hasSingleLocaleResponse;context;constructor(e){this.files=[],this.localesMap=new F({context:e}),this.hasSingleFileResponse=!1,this.hasSingleLocaleResponse=!1,this.context=e}async execute(){const e=this.getPromises(),t=e.map(e=>e[0]),s=e.map(e=>e[1]),a=await Promise.all(t);return this.context.responseFactory.createCdnResponse({requests:s,responses:a,localesMap:this.localesMap,hasSingleFileResponse:this.hasSingleFileResponse,hasSingleLocaleResponse:this.hasSingleLocaleResponse})}getPromises(){return this.files.reduce((e,t)=>(void 0!==this.localesMap.data?.[t.id]&&e.push(...this.localesMap.data[t.id].map(e=>{const s={metafileFile:t,metafileLocale:e};return[this.context.api.fetchLocale(s),s]})),e),[])}}class M{context;request;constructor(e){this.context=e,this.request=new L(this.context)}addFiles(e){if(!(r(e)||a(e)||i(e)||l(e)))throw new Error('Invalid param: "request.files" must be object, array, string or undefined.');if(l(e)&&e.forEach(e=>{if(!r(e)&&!a(e))throw new Error('Invalid param: array "request.files" must contain objects or strings.')}),a(e)){this.request.hasSingleFileResponse=!0;const t=this.context.metafile.data.files.find(t=>t.id===e);if(!(t instanceof d))throw new Error(`File not found: "${e}".`);this.request.files=[t]}else if(i(e))this.request.files=[...this.context.metafile.data.files];else if(l(e))this.request.files=e.map(e=>{let t;if(a(e)){const s=this.context.metafile.data.files.find(t=>t.id===e);if(i(s))throw new Error(`File not found: "${e}".`);t=s}else{const s=this.context.metafile.data.files.find(t=>t.id===e.id);if(i(s))throw new Error(`File not found: "${e.id}".`);t=s}return t});else if(r(e)){this.request.hasSingleFileResponse=!0;const t=this.context.metafile.data.files.find(t=>t.id===e.id);if(i(t))throw new Error(`File not found: "${e.id}".`);this.request.files=[t]}return this}addLocales(e,t){if(!(a(e)||i(e)||l(e)))throw new Error('Invalid param: "request.locales" must be array, string or undefined.');return l(e)&&e.forEach(e=>{if(!a(e))throw new Error('Invalid param: array "request.locales" must contain strings.')}),a(e)?(this.request.hasSingleLocaleResponse=!0,this.request.files.reduce((t,s)=>(t.data[s.id]=s.locales.filter(t=>t.locale===e),t),this.request.localesMap)):i(e)?this.request.files.reduce((e,s)=>(e.data[s.id]=t?s.locales.filter(e=>!e.isBaseLocale):s.locales,e),this.request.localesMap):l(e)&&this.request.files.reduce((t,s)=>(t.data[s.id]=s.locales.filter(t=>e.includes(t.locale)),t),this.request.localesMap),this}getCdnRequest(){const e=this.request;return this.request=new L(this.context),e}}class q{metafile;cache;context;static version="1.5.12";constructor(e){const t=new g(e),s=new w(t.params.baseUrl);this.context=new u({metafileContext:t,cdn:this,client:s}),this.metafile=new b(this.context),this.cache=new y(this.context)}fetch=async e=>{const{files:t,locales:s,excludeBaseLocale:a}=e||{};return new M(this.context).addFiles(t).addLocales(s,a).getCdnRequest().execute()};static async create(e){if(!e)throw new Error('Invalid param: missing required "options" parameter.');if(!a(e.metafile))throw new Error('Invalid param: "options.metafile" must be string.');const t=new q(e);return await t.metafile.refresh(),t}}e.Api=t,e.CdnBase=x,e.CdnCache=y,e.CdnClient=q,e.CdnMetafile=b,e.Context=u,e.FetchHttpAdapter=w,e.LocalesCache=c,e.LocalesMap=F,e.MemoryCacheAdapter=s,e.MetafileContext=g,e.MetafileData=p,e.MetafileFile=d,e.MetafileLocale=f,e.MetafileParams=m,e.Request=L,e.RequestBuilder=M,e.ResponseFactory=h,e.isArray=l,e.isPlainObject=r,e.isString=a,e.isUndefined=i,e.uniq=o,e.uniqBy=n,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
2
- //# sourceMappingURL=localazy-cdn-client.umd.min.js.map
1
+ /* @localazy/cdn-client@1.5.14
2
+ * (c) 2026 Localazy <team@localazy.com>
3
+ * @license MIT */
4
+ (function(e, t) {
5
+ "object" == typeof exports && "undefined" != typeof module ? t(exports) : "function" == typeof define && define.amd ? define(["exports"], t) : t((e = "undefined" != typeof globalThis ? globalThis : e || self).LocalazyCDN = {});
6
+ })(this, function(e) {
7
+ Object.defineProperty(e, Symbol.toStringTag, { value: "Module" });
8
+ var t = class {
9
+ context;
10
+ constructor(e) {
11
+ this.context = e;
12
+ }
13
+ async fetchLocale(e) {
14
+ return this.context.cache.has(e) ? new Promise((t) => {
15
+ t(this.context.cache.get(e));
16
+ }) : this.context.client.get(e.metafileLocale.uri);
17
+ }
18
+ async fetchMetafile() {
19
+ return await this.context.client.get(this.context.metafile.params.jsonPath);
20
+ }
21
+ }, s = class {
22
+ map;
23
+ constructor() {
24
+ this.map = /* @__PURE__ */ new Map();
25
+ }
26
+ get(e) {
27
+ return this.map.get(e);
28
+ }
29
+ has(e) {
30
+ return this.map.has(e);
31
+ }
32
+ set(e, t) {
33
+ this.map.set(e, t);
34
+ }
35
+ flush() {
36
+ this.map = /* @__PURE__ */ new Map();
37
+ }
38
+ }, a = (e) => "string" == typeof e, i = (e) => void 0 === e, l = (e) => Array.isArray(e), r = (e) => "[object Object]" === Object.prototype.toString.call(e), o = (e) => [...new Set(e)], n = (e, t) => {
39
+ const s = {};
40
+ return e.filter((e) => {
41
+ const a = t(e);
42
+ return !Object.hasOwn(s, a) && (s[a] = !0, !0);
43
+ });
44
+ }, c = class {
45
+ context;
46
+ cacheAdapter;
47
+ constructor(e) {
48
+ this.context = e, this.cacheAdapter = new s();
49
+ }
50
+ setIfMissed(e) {
51
+ const { metafileFile: t, metafileLocale: s, data: a } = e, i = this.keyFromMetafile({
52
+ metafileFile: t,
53
+ metafileLocale: s
54
+ });
55
+ this.cacheAdapter.has(i) || this.cacheAdapter.set(i, a);
56
+ }
57
+ has(e) {
58
+ const t = this.keyFromMetafile(e);
59
+ return this.cacheAdapter.has(t);
60
+ }
61
+ get(e) {
62
+ const t = this.keyFromMetafile(e);
63
+ return this.cacheAdapter.get(t);
64
+ }
65
+ flush() {
66
+ this.cacheAdapter.flush();
67
+ }
68
+ keyFromMetafile(e) {
69
+ const { metafileFile: t, metafileLocale: s } = e, a = [...o(t.productFlavors)].sort().join("-");
70
+ return [
71
+ this.context.metafile.params.cdnId,
72
+ t.id,
73
+ t.file,
74
+ t.path,
75
+ t.library,
76
+ t.module,
77
+ t.buildType,
78
+ a,
79
+ s.locale,
80
+ s.timestamp.toString()
81
+ ].filter((e) => "" !== e).join("-");
82
+ }
83
+ }, h = class e {
84
+ context;
85
+ constructor(e) {
86
+ this.context = e;
87
+ }
88
+ createCdnResponse(t) {
89
+ const { requests: s, responses: a, hasSingleFileResponse: i, hasSingleLocaleResponse: l } = t;
90
+ return 0 === a.length || void 0 === a[0] ? {} : (this.cacheResponses(s, a), i && l ? a[0] : e.transformResponses(t));
91
+ }
92
+ cacheResponses(e, t) {
93
+ t.forEach((t, s) => {
94
+ if (void 0 !== e[s]) {
95
+ const { metafileFile: a, metafileLocale: i } = e[s];
96
+ a && i && this.context.cache.setIfMissed({
97
+ metafileFile: a,
98
+ metafileLocale: i,
99
+ data: t
100
+ });
101
+ }
102
+ });
103
+ }
104
+ static transformResponses(e) {
105
+ const { requests: t, responses: s, hasSingleFileResponse: a } = e;
106
+ return s.reduce((e, s, i) => {
107
+ if (void 0 !== t[i]) {
108
+ const { metafileFile: l, metafileLocale: r } = t[i];
109
+ l && r && (a ? e[r.locale] = s : (e[l.id] || (e[l.id] = {}), e[l.id][r.locale] = s));
110
+ }
111
+ return e;
112
+ }, {});
113
+ }
114
+ }, u = class {
115
+ metafile;
116
+ cdn;
117
+ client;
118
+ api;
119
+ cache;
120
+ responseFactory;
121
+ constructor(e) {
122
+ this.metafile = e.metafileContext, this.cdn = e.cdn, this.client = e.client, this.api = new t(this), this.cache = new c(this), this.responseFactory = new h(this);
123
+ }
124
+ }, d = class {
125
+ id;
126
+ file;
127
+ path;
128
+ library;
129
+ module;
130
+ buildType;
131
+ timestamp;
132
+ productFlavors;
133
+ locales;
134
+ baseUrl;
135
+ constructor(e) {
136
+ this.id = e.id, this.file = e.file, this.path = e.path, this.library = e.library, this.module = e.module, this.buildType = e.buildType, this.timestamp = e.timestamp, this.productFlavors = e.productFlavors, this.locales = e.locales, this.baseUrl = e.baseUrl;
137
+ }
138
+ toCdnFile() {
139
+ return {
140
+ id: this.id,
141
+ file: this.file,
142
+ path: this.path,
143
+ library: this.library,
144
+ module: this.module,
145
+ buildType: this.buildType,
146
+ productFlavors: this.productFlavors,
147
+ locales: this.locales.map((e) => ({
148
+ locale: e.locale,
149
+ isBaseLocale: e.isBaseLocale,
150
+ uri: `${this.baseUrl}${e.uri}`
151
+ }))
152
+ };
153
+ }
154
+ }, f = class {
155
+ language;
156
+ region;
157
+ script;
158
+ isRtl;
159
+ name;
160
+ localizedName;
161
+ uri;
162
+ timestamp;
163
+ baseLocale;
164
+ constructor(e, t) {
165
+ this.language = e.language, this.region = e.region, this.script = e.script, this.isRtl = e.isRtl, this.name = e.name, this.localizedName = e.localizedName, this.uri = e.uri, this.timestamp = e.timestamp, this.baseLocale = t;
166
+ }
167
+ get locale() {
168
+ return this.language && this.region && this.script ? `${this.language}_${this.region}#${this.script}` : this.language && this.script ? `${this.language}#${this.script}` : this.language && this.region ? `${this.language}_${this.region}` : this.language;
169
+ }
170
+ get isBaseLocale() {
171
+ return this.locale === this.baseLocale;
172
+ }
173
+ toCdnLocale() {
174
+ return {
175
+ locale: this.locale,
176
+ isBaseLocale: this.isBaseLocale,
177
+ language: this.language,
178
+ region: this.region,
179
+ script: this.script,
180
+ isRtl: this.isRtl,
181
+ name: this.name,
182
+ localizedName: this.localizedName
183
+ };
184
+ }
185
+ }, p = class e {
186
+ projectUrl;
187
+ baseLocale;
188
+ locales;
189
+ timestamp;
190
+ files;
191
+ filesMap;
192
+ constructor(t, s) {
193
+ this.projectUrl = t.projectUrl, this.timestamp = t.timestamp, this.files = e.filesFactory(t.files, t.baseLocale, s), this.filesMap = e.filesMapFactory(this.files), this.locales = e.localesFactory(this.files), this.baseLocale = this.locales.find((e) => e.isBaseLocale);
194
+ }
195
+ static createEmpty(t) {
196
+ return new e({
197
+ projectUrl: "",
198
+ baseLocale: "",
199
+ timestamp: 0,
200
+ files: {}
201
+ }, t);
202
+ }
203
+ static filesFactory(e, t, s) {
204
+ return Object.keys(e).reduce((a, i) => {
205
+ if (void 0 !== e[i]) {
206
+ const l = e[i].locales.map((e) => new f(e, t));
207
+ a.push(new d({
208
+ ...e[i],
209
+ id: i,
210
+ locales: l,
211
+ baseUrl: s.baseUrl
212
+ }));
213
+ }
214
+ return a;
215
+ }, []);
216
+ }
217
+ static filesMapFactory(e) {
218
+ return e.reduce((e, t) => (e[t.id] = t, e), {});
219
+ }
220
+ static localesFactory(e) {
221
+ return n(e.reduce((e, t) => (e.push(...t.locales.map((e) => e.toCdnLocale())), e), []), (e) => e.locale);
222
+ }
223
+ }, m = class e {
224
+ options;
225
+ constructor(t) {
226
+ this.options = e.parseMetafileUrl(t);
227
+ }
228
+ get url() {
229
+ return this.options.url;
230
+ }
231
+ get baseUrl() {
232
+ return this.options.baseUrl;
233
+ }
234
+ get cdnId() {
235
+ return this.options.cdnId;
236
+ }
237
+ get jsonPath() {
238
+ return this.options.jsonPath;
239
+ }
240
+ static parseMetafileUrl(e) {
241
+ let t;
242
+ try {
243
+ t = new URL(e);
244
+ } catch {
245
+ throw new Error("Invalid param: \"options.metafile\" cannot be parsed as url.");
246
+ }
247
+ const s = /^\/(.*?)\/(.*?)\.(v2.json|js|ts)$/.exec(t.pathname);
248
+ if (null === s || 4 !== s.length || void 0 === s[1] || void 0 === s[2]) throw new Error("Invalid param: \"options.metafile\" contains invalid metafile url.");
249
+ const a = s[1], i = s[2];
250
+ return {
251
+ url: e,
252
+ baseUrl: t.origin,
253
+ cdnId: a,
254
+ jsonPath: `/${a}/${i}.v2.json`
255
+ };
256
+ }
257
+ }, g = class {
258
+ params;
259
+ parsedData;
260
+ data;
261
+ constructor(e) {
262
+ this.params = new m(e.metafile), this.parsedData = null, this.data = p.createEmpty(this.params);
263
+ }
264
+ setMetafile(e) {
265
+ this.parsedData = e, this.data = new p(e, this.params);
266
+ }
267
+ }, w = class {
268
+ baseUrl;
269
+ constructor(e) {
270
+ this.baseUrl = e;
271
+ }
272
+ async get(e) {
273
+ const t = await fetch(`${this.baseUrl}${e}`), s = t.headers.get("content-type"), a = "application/json5" === s || "application/json" === s;
274
+ if (t.status >= 400) throw new Error(`Request failed with status code ${t.status.toString()}`);
275
+ let i;
276
+ return i = a ? await t.json() : await t.text(), i;
277
+ }
278
+ }, x = class {
279
+ context;
280
+ constructor(e) {
281
+ this.context = e;
282
+ }
283
+ }, y = class extends x {
284
+ flush = () => {
285
+ this.context.cache.flush();
286
+ };
287
+ }, b = class extends x {
288
+ get projectUrl() {
289
+ return this.context.metafile.data.projectUrl;
290
+ }
291
+ get baseLocale() {
292
+ return this.context.metafile.data.baseLocale;
293
+ }
294
+ get url() {
295
+ return this.context.metafile.params.url;
296
+ }
297
+ get files() {
298
+ return this.context.metafile.data.files.map((e) => e.toCdnFile());
299
+ }
300
+ locales = (e) => {
301
+ const { excludeBaseLocale: t } = e || {}, { locales: s } = this.context.metafile.data;
302
+ return t ? s.filter((e) => !e.isBaseLocale) : s;
303
+ };
304
+ refresh = async () => {
305
+ const e = await this.context.api.fetchMetafile();
306
+ this.context.metafile.setMetafile(e);
307
+ };
308
+ switch = async (e) => {
309
+ this.context.metafile.params = new m(e.metafile), await this.refresh();
310
+ };
311
+ }, F = class {
312
+ data;
313
+ context;
314
+ constructor(e) {
315
+ this.context = e.context, this.data = e.data || {};
316
+ }
317
+ }, L = class {
318
+ files;
319
+ localesMap;
320
+ hasSingleFileResponse;
321
+ hasSingleLocaleResponse;
322
+ context;
323
+ constructor(e) {
324
+ this.files = [], this.localesMap = new F({ context: e }), this.hasSingleFileResponse = !1, this.hasSingleLocaleResponse = !1, this.context = e;
325
+ }
326
+ async execute() {
327
+ const e = this.getPromises(), t = e.map((e) => e[0]), s = e.map((e) => e[1]), a = await Promise.all(t);
328
+ return this.context.responseFactory.createCdnResponse({
329
+ requests: s,
330
+ responses: a,
331
+ localesMap: this.localesMap,
332
+ hasSingleFileResponse: this.hasSingleFileResponse,
333
+ hasSingleLocaleResponse: this.hasSingleLocaleResponse
334
+ });
335
+ }
336
+ getPromises() {
337
+ return this.files.reduce((e, t) => (void 0 !== this.localesMap.data?.[t.id] && e.push(...this.localesMap.data[t.id].map((e) => {
338
+ const s = {
339
+ metafileFile: t,
340
+ metafileLocale: e
341
+ };
342
+ return [this.context.api.fetchLocale(s), s];
343
+ })), e), []);
344
+ }
345
+ }, M = class {
346
+ context;
347
+ request;
348
+ constructor(e) {
349
+ this.context = e, this.request = new L(this.context);
350
+ }
351
+ addFiles(e) {
352
+ if (!(r(e) || a(e) || i(e) || l(e))) throw new Error("Invalid param: \"request.files\" must be object, array, string or undefined.");
353
+ if (l(e) && e.forEach((e) => {
354
+ if (!r(e) && !a(e)) throw new Error("Invalid param: array \"request.files\" must contain objects or strings.");
355
+ }), a(e)) {
356
+ this.request.hasSingleFileResponse = !0;
357
+ const t = this.context.metafile.data.files.find((t) => t.id === e);
358
+ if (!(t instanceof d)) throw new Error(`File not found: "${e}".`);
359
+ this.request.files = [t];
360
+ } else if (i(e)) this.request.files = [...this.context.metafile.data.files];
361
+ else if (l(e)) this.request.files = e.map((e) => {
362
+ let t;
363
+ if (a(e)) {
364
+ const s = this.context.metafile.data.files.find((t) => t.id === e);
365
+ if (i(s)) throw new Error(`File not found: "${e}".`);
366
+ t = s;
367
+ } else {
368
+ const s = this.context.metafile.data.files.find((t) => t.id === e.id);
369
+ if (i(s)) throw new Error(`File not found: "${e.id}".`);
370
+ t = s;
371
+ }
372
+ return t;
373
+ });
374
+ else if (r(e)) {
375
+ this.request.hasSingleFileResponse = !0;
376
+ const t = this.context.metafile.data.files.find((t) => t.id === e.id);
377
+ if (i(t)) throw new Error(`File not found: "${e.id}".`);
378
+ this.request.files = [t];
379
+ }
380
+ return this;
381
+ }
382
+ addLocales(e, t) {
383
+ if (!(a(e) || i(e) || l(e))) throw new Error("Invalid param: \"request.locales\" must be array, string or undefined.");
384
+ return l(e) && e.forEach((e) => {
385
+ if (!a(e)) throw new Error("Invalid param: array \"request.locales\" must contain strings.");
386
+ }), a(e) ? (this.request.hasSingleLocaleResponse = !0, this.request.files.reduce((t, s) => (t.data[s.id] = s.locales.filter((t) => t.locale === e), t), this.request.localesMap)) : i(e) ? this.request.files.reduce((e, s) => (e.data[s.id] = t ? s.locales.filter((e) => !e.isBaseLocale) : s.locales, e), this.request.localesMap) : l(e) && this.request.files.reduce((t, s) => (t.data[s.id] = s.locales.filter((t) => e.includes(t.locale)), t), this.request.localesMap), this;
387
+ }
388
+ getCdnRequest() {
389
+ const e = this.request;
390
+ return this.request = new L(this.context), e;
391
+ }
392
+ };
393
+ e.Api = t, e.CdnBase = x, e.CdnCache = y, e.CdnClient = class e {
394
+ metafile;
395
+ cache;
396
+ context;
397
+ static version = "1.5.14";
398
+ constructor(e) {
399
+ const t = new g(e), s = new w(t.params.baseUrl);
400
+ this.context = new u({
401
+ metafileContext: t,
402
+ cdn: this,
403
+ client: s
404
+ }), this.metafile = new b(this.context), this.cache = new y(this.context);
405
+ }
406
+ fetch = async (e) => {
407
+ const { files: t, locales: s, excludeBaseLocale: a } = e || {};
408
+ return new M(this.context).addFiles(t).addLocales(s, a).getCdnRequest().execute();
409
+ };
410
+ static async create(t) {
411
+ if (!t) throw new Error("Invalid param: missing required \"options\" parameter.");
412
+ if (!a(t.metafile)) throw new Error("Invalid param: \"options.metafile\" must be string.");
413
+ const s = new e(t);
414
+ return await s.metafile.refresh(), s;
415
+ }
416
+ }, e.CdnMetafile = b, e.Context = u, e.FetchHttpAdapter = w, e.LocalesCache = c, e.LocalesMap = F, e.MemoryCacheAdapter = s, e.MetafileContext = g, e.MetafileData = p, e.MetafileFile = d, e.MetafileLocale = f, e.MetafileParams = m, e.Request = L, e.RequestBuilder = M, e.ResponseFactory = h, e.isArray = l, e.isPlainObject = r, e.isString = a, e.isUndefined = i, e.uniq = o, e.uniqBy = n;
417
+ });
418
+
419
+ //# sourceMappingURL=localazy-cdn-client.umd.min.js.map