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