@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/index.d.ts CHANGED
@@ -1,23 +1,15 @@
1
- import { AxiosInstance } from 'axios';
2
- import { AxiosRequestConfig } from 'axios';
3
- import { AxiosResponse } from 'axios';
4
- import { CreateAxiosDefaults } from 'axios';
5
-
6
1
  export declare class Api {
7
2
  protected context: Context;
8
3
  constructor(context: Context);
9
- fetchLocale(options: ApiLocaleRequest): Promise<AxiosResponse>;
10
- fetchMetafile(config?: AxiosRequestConfig): Promise<AxiosResponse<IMetafile>>;
4
+ fetchLocale(options: ApiLocaleRequest): Promise<object | string>;
5
+ fetchMetafile(): Promise<IMetafile>;
11
6
  }
12
7
 
13
8
  export declare type ApiLocaleRequest = ILocalesCacheItem;
14
9
 
15
- export declare class AxiosHttpAdapter implements IHttpAdapter {
16
- client: AxiosInstance;
17
- constructor(baseUrl: string, config?: CreateAxiosDefaults);
18
- protected static clientFactory(baseURL: string, config?: CreateAxiosDefaults): AxiosInstance;
19
- get<T = unknown, D = unknown>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
20
- }
10
+ export declare type ApiRequestConfig = object & {
11
+ reference: ApiLocaleRequest;
12
+ };
21
13
 
22
14
  export declare type CacheGetLocalesRequest = ILocalesCacheItem;
23
15
 
@@ -42,9 +34,9 @@ export declare class CdnClient {
42
34
  metafile: CdnMetafile;
43
35
  cache: CdnCache;
44
36
  protected context: Context;
45
- protected constructor(options: CdnClientOptions, config?: CreateAxiosDefaults);
37
+ protected constructor(options: CdnClientOptions);
46
38
  fetch: (options?: CdnFetchOptions) => Promise<CdnResponse>;
47
- static create(options: CdnClientOptions, config?: CreateAxiosDefaults): Promise<CdnClient>;
39
+ static create(options: CdnClientOptions): Promise<CdnClient>;
48
40
  }
49
41
 
50
42
  export declare type CdnClientOptions = {
@@ -182,8 +174,8 @@ export declare class CdnMetafile extends CdnBase {
182
174
  get baseLocale(): CdnLocale;
183
175
  get url(): string;
184
176
  locales: (options?: CdnLocalesOptions) => CdnLocale[];
185
- refresh: (config?: AxiosRequestConfig) => Promise<void>;
186
- switch: (options: CdnClientOptions, config?: AxiosRequestConfig) => Promise<void>;
177
+ refresh: () => Promise<void>;
178
+ switch: (options: CdnClientOptions) => Promise<void>;
187
179
  }
188
180
 
189
181
  export declare class CdnMetafileFiles extends CdnBase {
@@ -241,6 +233,12 @@ export declare type ExcludeBaseLocale = {
241
233
  excludeBaseLocale?: boolean;
242
234
  };
243
235
 
236
+ export declare class FetchHttpAdapter implements IHttpAdapter {
237
+ protected baseUrl: string;
238
+ constructor(baseUrl: string);
239
+ get(url: string): Promise<string | object>;
240
+ }
241
+
244
242
  export declare type FileResponse = {
245
243
  [fileId: string]: LocaleResponse;
246
244
  };
@@ -256,8 +254,8 @@ export declare interface ICacheAdapter<K, V> {
256
254
  flush: () => void;
257
255
  }
258
256
 
259
- export declare interface IHttpAdapter {
260
- get<T = unknown, D = unknown>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
257
+ export declare interface IHttpAdapter<T = object> {
258
+ get: (url: string, config?: T) => Promise<object | string>;
261
259
  }
262
260
 
263
261
  export declare interface ILocalesCacheItem {
@@ -433,7 +431,7 @@ declare class Request_2 {
433
431
  protected context: Context;
434
432
  constructor(context: Context);
435
433
  execute(): Promise<CdnResponse>;
436
- protected getPromises(): Promise<AxiosResponse<CdnResponse>>[];
434
+ protected getPromises(): [Promise<string | object>, ApiLocaleRequest][];
437
435
  }
438
436
  export { Request_2 as Request }
439
437
 
@@ -450,13 +448,13 @@ export declare class ResponseFactory {
450
448
  protected context: Context;
451
449
  constructor(context: Context);
452
450
  createCdnResponse(options: ResponseFactoryOptions): CdnResponse;
453
- protected cacheResponses(responses: AxiosResponse<CdnResponse>[]): void;
454
- protected static transformResponsesToOutput(options: ResponseFactoryOptions): CdnResponse;
455
- protected static extractReference(response: AxiosResponse<CdnResponse>): Partial<CacheStoreLocalesRequest>;
451
+ protected cacheResponses(requests: ApiLocaleRequest[], responses: (object | string)[]): void;
452
+ protected static transformResponses(options: ResponseFactoryOptions): CdnResponse;
456
453
  }
457
454
 
458
455
  export declare type ResponseFactoryOptions = {
459
- responses: AxiosResponse<CdnResponse>[];
456
+ requests: ApiLocaleRequest[];
457
+ responses: (string | object)[];
460
458
  localesMap: LocalesMap;
461
459
  hasSingleFileResponse: boolean;
462
460
  hasSingleLocaleResponse: boolean;
@@ -4,33 +4,25 @@ var __publicField = (obj, key, value) => {
4
4
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  return value;
6
6
  };
7
- /* @localazy/cdn-client@1.1.2
7
+ /* @localazy/cdn-client@1.2.1
8
8
  * (c) 2024 Localazy <team@localazy.com>
9
9
  * @license MIT */
10
- import axios from "axios";
10
+ import "isomorphic-fetch";
11
11
  class Api {
12
12
  constructor(context) {
13
13
  __publicField(this, "context");
14
14
  this.context = context;
15
15
  }
16
16
  async fetchLocale(options) {
17
- const config = { reference: options };
18
17
  if (this.context.cache.has(options)) {
19
18
  return new Promise((resolve) => {
20
- resolve({
21
- data: this.context.cache.get(options),
22
- config
23
- });
19
+ resolve(this.context.cache.get(options));
24
20
  });
25
21
  }
26
- const responseType = /\.(json|json5)$/.test(options.metafileFile.file) ? "json" : "text";
27
- return this.context.client.get(options.metafileLocale.uri, { ...config, responseType });
22
+ return this.context.client.get(options.metafileLocale.uri);
28
23
  }
29
- async fetchMetafile(config) {
30
- return this.context.client.get(
31
- this.context.metafile.params.jsonPath,
32
- config
33
- );
24
+ async fetchMetafile() {
25
+ return await this.context.client.get(this.context.metafile.params.jsonPath);
34
26
  }
35
27
  }
36
28
  var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
@@ -1582,40 +1574,45 @@ class ResponseFactory {
1582
1574
  this.context = context;
1583
1575
  }
1584
1576
  createCdnResponse(options) {
1585
- const { responses, hasSingleFileResponse, hasSingleLocaleResponse } = options;
1577
+ const {
1578
+ requests,
1579
+ responses,
1580
+ hasSingleFileResponse,
1581
+ hasSingleLocaleResponse
1582
+ } = options;
1586
1583
  if (responses.length === 0) {
1587
1584
  return {};
1588
1585
  }
1589
- this.cacheResponses(responses);
1590
- return hasSingleFileResponse && hasSingleLocaleResponse ? responses[0].data : ResponseFactory.transformResponsesToOutput(options);
1586
+ this.cacheResponses(requests, responses);
1587
+ return hasSingleFileResponse && hasSingleLocaleResponse ? responses[0] : ResponseFactory.transformResponses(options);
1591
1588
  }
1592
- cacheResponses(responses) {
1593
- responses.forEach((response) => {
1589
+ cacheResponses(requests, responses) {
1590
+ responses.forEach((response, index) => {
1594
1591
  const {
1595
1592
  metafileFile,
1596
1593
  metafileLocale
1597
- } = ResponseFactory.extractReference(response);
1594
+ } = requests[index];
1598
1595
  if (metafileFile && metafileLocale) {
1599
- this.context.cache.setIfMissed({ metafileFile, metafileLocale, data: response.data });
1596
+ this.context.cache.setIfMissed({ metafileFile, metafileLocale, data: response });
1600
1597
  }
1601
1598
  });
1602
1599
  }
1603
- static transformResponsesToOutput(options) {
1604
- const { responses, hasSingleFileResponse } = options;
1600
+ static transformResponses(options) {
1601
+ const { requests, responses, hasSingleFileResponse } = options;
1605
1602
  return responses.reduce(
1606
- (acc, cur) => {
1603
+ (acc, cur, index) => {
1607
1604
  const {
1608
1605
  metafileFile,
1609
1606
  metafileLocale
1610
- } = ResponseFactory.extractReference(cur);
1607
+ } = requests[index];
1611
1608
  if (metafileFile && metafileLocale) {
1612
1609
  if (hasSingleFileResponse) {
1613
- acc[metafileLocale.locale] = cur.data;
1610
+ acc[metafileLocale.locale] = cur;
1614
1611
  } else {
1615
1612
  if (!acc[metafileFile.id]) {
1616
1613
  acc[metafileFile.id] = {};
1617
1614
  }
1618
- acc[metafileFile.id][metafileLocale.locale] = cur.data;
1615
+ acc[metafileFile.id][metafileLocale.locale] = cur;
1619
1616
  }
1620
1617
  }
1621
1618
  return acc;
@@ -1623,17 +1620,6 @@ class ResponseFactory {
1623
1620
  {}
1624
1621
  );
1625
1622
  }
1626
- static extractReference(response) {
1627
- var _a, _b;
1628
- const metafileFile = (_a = response.config.reference) == null ? void 0 : _a.metafileFile;
1629
- const metafileLocale = (_b = response.config.reference) == null ? void 0 : _b.metafileLocale;
1630
- const { data } = response;
1631
- return {
1632
- metafileFile,
1633
- metafileLocale,
1634
- data
1635
- };
1636
- }
1637
1623
  }
1638
1624
  class Context {
1639
1625
  constructor(options) {
@@ -2237,13 +2223,13 @@ class CdnMetafile extends CdnBase {
2237
2223
  const { locales } = this.context.metafile.data;
2238
2224
  return excludeBaseLocale ? locales.filter((cdnLocale) => !cdnLocale.isBaseLocale) : locales;
2239
2225
  });
2240
- __publicField(this, "refresh", async (config) => {
2241
- const response = await this.context.api.fetchMetafile(config);
2242
- this.context.metafile.setMetafile(response.data);
2226
+ __publicField(this, "refresh", async () => {
2227
+ const response = await this.context.api.fetchMetafile();
2228
+ this.context.metafile.setMetafile(response);
2243
2229
  });
2244
- __publicField(this, "switch", async (options, config) => {
2230
+ __publicField(this, "switch", async (options) => {
2245
2231
  this.context.metafile.params = new MetafileParams(options.metafile);
2246
- await this.refresh(config);
2232
+ await this.refresh();
2247
2233
  });
2248
2234
  this.files = new CdnMetafileFiles(context);
2249
2235
  }
@@ -2284,8 +2270,17 @@ class Request {
2284
2270
  this.context = context;
2285
2271
  }
2286
2272
  async execute() {
2273
+ const payload = this.getPromises();
2274
+ const promises = payload.map(
2275
+ (item) => item[0]
2276
+ );
2277
+ const requests = payload.map(
2278
+ (item) => item[1]
2279
+ );
2280
+ const responses = await Promise.all(promises);
2287
2281
  return this.context.responseFactory.createCdnResponse({
2288
- responses: await Promise.all(this.getPromises()),
2282
+ requests,
2283
+ responses,
2289
2284
  localesMap: this.localesMap,
2290
2285
  hasSingleFileResponse: this.hasSingleFileResponse,
2291
2286
  hasSingleLocaleResponse: this.hasSingleLocaleResponse
@@ -2297,10 +2292,16 @@ class Request {
2297
2292
  if (this.localesMap.data[cur.id]) {
2298
2293
  acc.push(
2299
2294
  ...this.localesMap.data[cur.id].map(
2300
- (metafileLocale) => this.context.api.fetchLocale({
2301
- metafileFile: cur,
2302
- metafileLocale
2303
- })
2295
+ (metafileLocale) => {
2296
+ const request = {
2297
+ metafileFile: cur,
2298
+ metafileLocale
2299
+ };
2300
+ return [
2301
+ this.context.api.fetchLocale(request),
2302
+ request
2303
+ ];
2304
+ }
2304
2305
  )
2305
2306
  );
2306
2307
  }
@@ -2422,21 +2423,40 @@ class RequestBuilder {
2422
2423
  return result;
2423
2424
  }
2424
2425
  }
2425
- class AxiosHttpAdapter {
2426
- constructor(baseUrl, config) {
2427
- __publicField(this, "client");
2428
- this.client = AxiosHttpAdapter.clientFactory(baseUrl, config);
2429
- }
2430
- static clientFactory(baseURL, config) {
2431
- const clientConfig = { baseURL, ...config };
2432
- return axios.create(clientConfig);
2433
- }
2434
- get(url, config) {
2435
- return this.client.get(url, config);
2426
+ class FetchHttpAdapter {
2427
+ constructor(baseUrl) {
2428
+ __publicField(this, "baseUrl");
2429
+ this.baseUrl = baseUrl;
2430
+ }
2431
+ async get(url) {
2432
+ const response = await fetch(`${this.baseUrl}${url}`);
2433
+ const contentType = response.headers.get("content-type") || "";
2434
+ const isJson = [
2435
+ "application/json5",
2436
+ "application/json"
2437
+ ].includes(contentType);
2438
+ if (response.status >= 400) {
2439
+ throw new Error(`Request failed with status code ${response.status}`);
2440
+ }
2441
+ let result = "";
2442
+ if (isJson) {
2443
+ try {
2444
+ result = await response.json();
2445
+ } catch (error) {
2446
+ result = {};
2447
+ }
2448
+ } else {
2449
+ try {
2450
+ result = await response.text();
2451
+ } catch (error) {
2452
+ result = "";
2453
+ }
2454
+ }
2455
+ return result;
2436
2456
  }
2437
2457
  }
2438
2458
  class CdnClient {
2439
- constructor(options, config) {
2459
+ constructor(options) {
2440
2460
  __publicField(this, "metafile");
2441
2461
  __publicField(this, "cache");
2442
2462
  __publicField(this, "context");
@@ -2446,32 +2466,32 @@ class CdnClient {
2446
2466
  return requestBuilder.getCdnRequest().execute();
2447
2467
  });
2448
2468
  const metafileContext = new MetafileContext(options);
2449
- const client = new AxiosHttpAdapter(metafileContext.params.baseUrl, config);
2469
+ const client = new FetchHttpAdapter(metafileContext.params.baseUrl);
2450
2470
  this.context = new Context({ metafileContext, cdn: this, client });
2451
2471
  this.metafile = new CdnMetafile(this.context);
2452
2472
  this.cache = new CdnCache(this.context);
2453
2473
  }
2454
- static async create(options, config) {
2474
+ static async create(options) {
2455
2475
  if (!options) {
2456
2476
  throw new Error('Invalid param: missing required "options" parameter.');
2457
2477
  }
2458
2478
  if (!isString$1(options.metafile)) {
2459
2479
  throw new Error('Invalid param: "options.metafile" must be string.');
2460
2480
  }
2461
- const cdn = new CdnClient(options, config);
2462
- await cdn.metafile.refresh(config);
2481
+ const cdn = new CdnClient(options);
2482
+ await cdn.metafile.refresh();
2463
2483
  return cdn;
2464
2484
  }
2465
2485
  }
2466
2486
  export {
2467
2487
  Api,
2468
- AxiosHttpAdapter,
2469
2488
  CdnBase,
2470
2489
  CdnCache,
2471
2490
  CdnClient,
2472
2491
  CdnMetafile,
2473
2492
  CdnMetafileFiles,
2474
2493
  Context,
2494
+ FetchHttpAdapter,
2475
2495
  LocalesCache,
2476
2496
  LocalesMap,
2477
2497
  MemoryCacheAdapter,