@localazy/cdn-client 1.1.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/localazy-cdn-client.umd.js +86 -66
- package/dist/browser/localazy-cdn-client.umd.js.map +1 -1
- package/dist/browser/localazy-cdn-client.umd.min.js +3 -3
- package/dist/browser/localazy-cdn-client.umd.min.js.map +1 -1
- package/dist/index.d.ts +22 -24
- package/dist/localazy-cdn-client.js +85 -65
- package/dist/localazy-cdn-client.js.map +1 -1
- package/dist/localazy-cdn-client.min.js +2 -2
- package/dist/localazy-cdn-client.min.js.map +1 -1
- package/dist/node/localazy-cdn-client.cjs +85 -65
- package/dist/node/localazy-cdn-client.cjs.map +1 -1
- package/dist/node/localazy-cdn-client.min.cjs +2 -2
- package/dist/node/localazy-cdn-client.min.cjs.map +1 -1
- package/package.json +11 -8
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
(function(global2, factory) {
|
|
2
|
-
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("
|
|
3
|
-
})(this, function(exports2
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("isomorphic-fetch")) : typeof define === "function" && define.amd ? define(["exports", "isomorphic-fetch"], factory) : (global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self, factory(global2.LocalazyCDN = {}));
|
|
3
|
+
})(this, function(exports2) {
|
|
4
4
|
"use strict";var __defProp = Object.defineProperty;
|
|
5
5
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
6
|
var __publicField = (obj, key, value) => {
|
|
7
7
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
8
|
return value;
|
|
9
9
|
};
|
|
10
|
-
/* @localazy/cdn-client@1.1
|
|
10
|
+
/* @localazy/cdn-client@1.2.1
|
|
11
11
|
* (c) 2024 Localazy <team@localazy.com>
|
|
12
12
|
* @license MIT */
|
|
13
13
|
|
|
@@ -17,23 +17,15 @@ var __publicField = (obj, key, value) => {
|
|
|
17
17
|
this.context = context;
|
|
18
18
|
}
|
|
19
19
|
async fetchLocale(options) {
|
|
20
|
-
const config = { reference: options };
|
|
21
20
|
if (this.context.cache.has(options)) {
|
|
22
21
|
return new Promise((resolve) => {
|
|
23
|
-
resolve(
|
|
24
|
-
data: this.context.cache.get(options),
|
|
25
|
-
config
|
|
26
|
-
});
|
|
22
|
+
resolve(this.context.cache.get(options));
|
|
27
23
|
});
|
|
28
24
|
}
|
|
29
|
-
|
|
30
|
-
return this.context.client.get(options.metafileLocale.uri, { ...config, responseType });
|
|
25
|
+
return this.context.client.get(options.metafileLocale.uri);
|
|
31
26
|
}
|
|
32
|
-
async fetchMetafile(
|
|
33
|
-
return this.context.client.get(
|
|
34
|
-
this.context.metafile.params.jsonPath,
|
|
35
|
-
config
|
|
36
|
-
);
|
|
27
|
+
async fetchMetafile() {
|
|
28
|
+
return await this.context.client.get(this.context.metafile.params.jsonPath);
|
|
37
29
|
}
|
|
38
30
|
}
|
|
39
31
|
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
@@ -1585,40 +1577,45 @@ var __publicField = (obj, key, value) => {
|
|
|
1585
1577
|
this.context = context;
|
|
1586
1578
|
}
|
|
1587
1579
|
createCdnResponse(options) {
|
|
1588
|
-
const {
|
|
1580
|
+
const {
|
|
1581
|
+
requests,
|
|
1582
|
+
responses,
|
|
1583
|
+
hasSingleFileResponse,
|
|
1584
|
+
hasSingleLocaleResponse
|
|
1585
|
+
} = options;
|
|
1589
1586
|
if (responses.length === 0) {
|
|
1590
1587
|
return {};
|
|
1591
1588
|
}
|
|
1592
|
-
this.cacheResponses(responses);
|
|
1593
|
-
return hasSingleFileResponse && hasSingleLocaleResponse ? responses[0]
|
|
1589
|
+
this.cacheResponses(requests, responses);
|
|
1590
|
+
return hasSingleFileResponse && hasSingleLocaleResponse ? responses[0] : ResponseFactory.transformResponses(options);
|
|
1594
1591
|
}
|
|
1595
|
-
cacheResponses(responses) {
|
|
1596
|
-
responses.forEach((response) => {
|
|
1592
|
+
cacheResponses(requests, responses) {
|
|
1593
|
+
responses.forEach((response, index) => {
|
|
1597
1594
|
const {
|
|
1598
1595
|
metafileFile,
|
|
1599
1596
|
metafileLocale
|
|
1600
|
-
} =
|
|
1597
|
+
} = requests[index];
|
|
1601
1598
|
if (metafileFile && metafileLocale) {
|
|
1602
|
-
this.context.cache.setIfMissed({ metafileFile, metafileLocale, data: response
|
|
1599
|
+
this.context.cache.setIfMissed({ metafileFile, metafileLocale, data: response });
|
|
1603
1600
|
}
|
|
1604
1601
|
});
|
|
1605
1602
|
}
|
|
1606
|
-
static
|
|
1607
|
-
const { responses, hasSingleFileResponse } = options;
|
|
1603
|
+
static transformResponses(options) {
|
|
1604
|
+
const { requests, responses, hasSingleFileResponse } = options;
|
|
1608
1605
|
return responses.reduce(
|
|
1609
|
-
(acc, cur) => {
|
|
1606
|
+
(acc, cur, index) => {
|
|
1610
1607
|
const {
|
|
1611
1608
|
metafileFile,
|
|
1612
1609
|
metafileLocale
|
|
1613
|
-
} =
|
|
1610
|
+
} = requests[index];
|
|
1614
1611
|
if (metafileFile && metafileLocale) {
|
|
1615
1612
|
if (hasSingleFileResponse) {
|
|
1616
|
-
acc[metafileLocale.locale] = cur
|
|
1613
|
+
acc[metafileLocale.locale] = cur;
|
|
1617
1614
|
} else {
|
|
1618
1615
|
if (!acc[metafileFile.id]) {
|
|
1619
1616
|
acc[metafileFile.id] = {};
|
|
1620
1617
|
}
|
|
1621
|
-
acc[metafileFile.id][metafileLocale.locale] = cur
|
|
1618
|
+
acc[metafileFile.id][metafileLocale.locale] = cur;
|
|
1622
1619
|
}
|
|
1623
1620
|
}
|
|
1624
1621
|
return acc;
|
|
@@ -1626,17 +1623,6 @@ var __publicField = (obj, key, value) => {
|
|
|
1626
1623
|
{}
|
|
1627
1624
|
);
|
|
1628
1625
|
}
|
|
1629
|
-
static extractReference(response) {
|
|
1630
|
-
var _a, _b;
|
|
1631
|
-
const metafileFile = (_a = response.config.reference) == null ? void 0 : _a.metafileFile;
|
|
1632
|
-
const metafileLocale = (_b = response.config.reference) == null ? void 0 : _b.metafileLocale;
|
|
1633
|
-
const { data } = response;
|
|
1634
|
-
return {
|
|
1635
|
-
metafileFile,
|
|
1636
|
-
metafileLocale,
|
|
1637
|
-
data
|
|
1638
|
-
};
|
|
1639
|
-
}
|
|
1640
1626
|
}
|
|
1641
1627
|
class Context {
|
|
1642
1628
|
constructor(options) {
|
|
@@ -2240,13 +2226,13 @@ var __publicField = (obj, key, value) => {
|
|
|
2240
2226
|
const { locales } = this.context.metafile.data;
|
|
2241
2227
|
return excludeBaseLocale ? locales.filter((cdnLocale) => !cdnLocale.isBaseLocale) : locales;
|
|
2242
2228
|
});
|
|
2243
|
-
__publicField(this, "refresh", async (
|
|
2244
|
-
const response = await this.context.api.fetchMetafile(
|
|
2245
|
-
this.context.metafile.setMetafile(response
|
|
2229
|
+
__publicField(this, "refresh", async () => {
|
|
2230
|
+
const response = await this.context.api.fetchMetafile();
|
|
2231
|
+
this.context.metafile.setMetafile(response);
|
|
2246
2232
|
});
|
|
2247
|
-
__publicField(this, "switch", async (options
|
|
2233
|
+
__publicField(this, "switch", async (options) => {
|
|
2248
2234
|
this.context.metafile.params = new MetafileParams(options.metafile);
|
|
2249
|
-
await this.refresh(
|
|
2235
|
+
await this.refresh();
|
|
2250
2236
|
});
|
|
2251
2237
|
this.files = new CdnMetafileFiles(context);
|
|
2252
2238
|
}
|
|
@@ -2287,8 +2273,17 @@ var __publicField = (obj, key, value) => {
|
|
|
2287
2273
|
this.context = context;
|
|
2288
2274
|
}
|
|
2289
2275
|
async execute() {
|
|
2276
|
+
const payload = this.getPromises();
|
|
2277
|
+
const promises = payload.map(
|
|
2278
|
+
(item) => item[0]
|
|
2279
|
+
);
|
|
2280
|
+
const requests = payload.map(
|
|
2281
|
+
(item) => item[1]
|
|
2282
|
+
);
|
|
2283
|
+
const responses = await Promise.all(promises);
|
|
2290
2284
|
return this.context.responseFactory.createCdnResponse({
|
|
2291
|
-
|
|
2285
|
+
requests,
|
|
2286
|
+
responses,
|
|
2292
2287
|
localesMap: this.localesMap,
|
|
2293
2288
|
hasSingleFileResponse: this.hasSingleFileResponse,
|
|
2294
2289
|
hasSingleLocaleResponse: this.hasSingleLocaleResponse
|
|
@@ -2300,10 +2295,16 @@ var __publicField = (obj, key, value) => {
|
|
|
2300
2295
|
if (this.localesMap.data[cur.id]) {
|
|
2301
2296
|
acc.push(
|
|
2302
2297
|
...this.localesMap.data[cur.id].map(
|
|
2303
|
-
(metafileLocale) =>
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2298
|
+
(metafileLocale) => {
|
|
2299
|
+
const request = {
|
|
2300
|
+
metafileFile: cur,
|
|
2301
|
+
metafileLocale
|
|
2302
|
+
};
|
|
2303
|
+
return [
|
|
2304
|
+
this.context.api.fetchLocale(request),
|
|
2305
|
+
request
|
|
2306
|
+
];
|
|
2307
|
+
}
|
|
2307
2308
|
)
|
|
2308
2309
|
);
|
|
2309
2310
|
}
|
|
@@ -2425,21 +2426,40 @@ var __publicField = (obj, key, value) => {
|
|
|
2425
2426
|
return result;
|
|
2426
2427
|
}
|
|
2427
2428
|
}
|
|
2428
|
-
class
|
|
2429
|
-
constructor(baseUrl
|
|
2430
|
-
__publicField(this, "
|
|
2431
|
-
this.
|
|
2432
|
-
}
|
|
2433
|
-
|
|
2434
|
-
const
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2429
|
+
class FetchHttpAdapter {
|
|
2430
|
+
constructor(baseUrl) {
|
|
2431
|
+
__publicField(this, "baseUrl");
|
|
2432
|
+
this.baseUrl = baseUrl;
|
|
2433
|
+
}
|
|
2434
|
+
async get(url) {
|
|
2435
|
+
const response = await fetch(`${this.baseUrl}${url}`);
|
|
2436
|
+
const contentType = response.headers.get("content-type") || "";
|
|
2437
|
+
const isJson = [
|
|
2438
|
+
"application/json5",
|
|
2439
|
+
"application/json"
|
|
2440
|
+
].includes(contentType);
|
|
2441
|
+
if (response.status >= 400) {
|
|
2442
|
+
throw new Error(`Request failed with status code ${response.status}`);
|
|
2443
|
+
}
|
|
2444
|
+
let result = "";
|
|
2445
|
+
if (isJson) {
|
|
2446
|
+
try {
|
|
2447
|
+
result = await response.json();
|
|
2448
|
+
} catch (error) {
|
|
2449
|
+
result = {};
|
|
2450
|
+
}
|
|
2451
|
+
} else {
|
|
2452
|
+
try {
|
|
2453
|
+
result = await response.text();
|
|
2454
|
+
} catch (error) {
|
|
2455
|
+
result = "";
|
|
2456
|
+
}
|
|
2457
|
+
}
|
|
2458
|
+
return result;
|
|
2439
2459
|
}
|
|
2440
2460
|
}
|
|
2441
2461
|
class CdnClient {
|
|
2442
|
-
constructor(options
|
|
2462
|
+
constructor(options) {
|
|
2443
2463
|
__publicField(this, "metafile");
|
|
2444
2464
|
__publicField(this, "cache");
|
|
2445
2465
|
__publicField(this, "context");
|
|
@@ -2449,31 +2469,31 @@ var __publicField = (obj, key, value) => {
|
|
|
2449
2469
|
return requestBuilder.getCdnRequest().execute();
|
|
2450
2470
|
});
|
|
2451
2471
|
const metafileContext = new MetafileContext(options);
|
|
2452
|
-
const client = new
|
|
2472
|
+
const client = new FetchHttpAdapter(metafileContext.params.baseUrl);
|
|
2453
2473
|
this.context = new Context({ metafileContext, cdn: this, client });
|
|
2454
2474
|
this.metafile = new CdnMetafile(this.context);
|
|
2455
2475
|
this.cache = new CdnCache(this.context);
|
|
2456
2476
|
}
|
|
2457
|
-
static async create(options
|
|
2477
|
+
static async create(options) {
|
|
2458
2478
|
if (!options) {
|
|
2459
2479
|
throw new Error('Invalid param: missing required "options" parameter.');
|
|
2460
2480
|
}
|
|
2461
2481
|
if (!isString$1(options.metafile)) {
|
|
2462
2482
|
throw new Error('Invalid param: "options.metafile" must be string.');
|
|
2463
2483
|
}
|
|
2464
|
-
const cdn = new CdnClient(options
|
|
2465
|
-
await cdn.metafile.refresh(
|
|
2484
|
+
const cdn = new CdnClient(options);
|
|
2485
|
+
await cdn.metafile.refresh();
|
|
2466
2486
|
return cdn;
|
|
2467
2487
|
}
|
|
2468
2488
|
}
|
|
2469
2489
|
exports2.Api = Api;
|
|
2470
|
-
exports2.AxiosHttpAdapter = AxiosHttpAdapter;
|
|
2471
2490
|
exports2.CdnBase = CdnBase;
|
|
2472
2491
|
exports2.CdnCache = CdnCache;
|
|
2473
2492
|
exports2.CdnClient = CdnClient;
|
|
2474
2493
|
exports2.CdnMetafile = CdnMetafile;
|
|
2475
2494
|
exports2.CdnMetafileFiles = CdnMetafileFiles;
|
|
2476
2495
|
exports2.Context = Context;
|
|
2496
|
+
exports2.FetchHttpAdapter = FetchHttpAdapter;
|
|
2477
2497
|
exports2.LocalesCache = LocalesCache;
|
|
2478
2498
|
exports2.LocalesMap = LocalesMap;
|
|
2479
2499
|
exports2.MemoryCacheAdapter = MemoryCacheAdapter;
|