@localazy/cdn-client 1.1.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.
@@ -0,0 +1,465 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { AxiosRequestConfig } from 'axios';
3
+ import { AxiosResponse } from 'axios';
4
+ import { CreateAxiosDefaults } from 'axios';
5
+
6
+ export declare class Api {
7
+ protected context: Context;
8
+ constructor(context: Context);
9
+ fetchLocale(options: ApiLocaleRequest): Promise<AxiosResponse>;
10
+ fetchMetafile(config?: AxiosRequestConfig): Promise<AxiosResponse<IMetafile>>;
11
+ }
12
+
13
+ export declare type ApiLocaleRequest = ILocalesCacheItem;
14
+
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
+ }
21
+
22
+ export declare type CacheGetLocalesRequest = ILocalesCacheItem;
23
+
24
+ export declare type CacheHasLocalesRequest = ILocalesCacheItem;
25
+
26
+ export declare type CacheKeyMetafileOptions = ILocalesCacheItem;
27
+
28
+ export declare type CacheStoreLocalesRequest = ILocalesCacheItem & {
29
+ data: object | string;
30
+ };
31
+
32
+ export declare abstract class CdnBase {
33
+ protected context: Context;
34
+ constructor(context: Context);
35
+ }
36
+
37
+ export declare class CdnCache extends CdnBase {
38
+ flush: () => void;
39
+ }
40
+
41
+ export declare class CdnClient {
42
+ metafile: CdnMetafile;
43
+ cache: CdnCache;
44
+ protected context: Context;
45
+ protected constructor(options: CdnClientOptions, config?: CreateAxiosDefaults);
46
+ fetch: (options?: CdnFetchOptions) => Promise<CdnResponse>;
47
+ static create(options: CdnClientOptions, config?: CreateAxiosDefaults): Promise<CdnClient>;
48
+ }
49
+
50
+ export declare type CdnClientOptions = {
51
+ /**
52
+ * Project metafile URL.
53
+ */
54
+ metafile: string;
55
+ };
56
+
57
+ export declare type CdnFetchOptions = {
58
+ /**
59
+ * Select single or multiple files to fetch from the CDN.
60
+ *
61
+ * Default: all files in the metafile.
62
+ */
63
+ files?: (CdnFile | string)[] | CdnFile | string;
64
+ /**
65
+ * Select single or multiple locales to fetch from the CDN.
66
+ *
67
+ * Default: all locales in the metafile.
68
+ */
69
+ locales?: string[] | string;
70
+ /**
71
+ * Exclude the base locale from the list of locales to fetch.
72
+ */
73
+ excludeBaseLocale?: boolean;
74
+ };
75
+
76
+ export declare type CdnFile = {
77
+ /**
78
+ * File ID.
79
+ */
80
+ id: string;
81
+ /**
82
+ * File name.
83
+ */
84
+ file: string;
85
+ /**
86
+ * File path.
87
+ */
88
+ path: string;
89
+ /**
90
+ * File library.
91
+ */
92
+ library: string;
93
+ /**
94
+ * File module.
95
+ */
96
+ module: string;
97
+ /**
98
+ * File build type.
99
+ */
100
+ buildType: string;
101
+ /**
102
+ * File product flavors.
103
+ */
104
+ productFlavors: string[];
105
+ /**
106
+ * File locales.
107
+ */
108
+ locales: CdnFileLocale[];
109
+ };
110
+
111
+ export declare type CdnFileLocale = {
112
+ /**
113
+ * Locale code.
114
+ */
115
+ locale: string;
116
+ /**
117
+ * Boolean indicating if this is the base locale.
118
+ */
119
+ isBaseLocale: boolean;
120
+ /**
121
+ * File URI.
122
+ */
123
+ uri: string;
124
+ };
125
+
126
+ export declare type CdnFilesSearchOptions = ((file: CdnFile, index?: number, array?: CdnFile[]) => boolean) | {
127
+ id?: string;
128
+ file?: string;
129
+ path?: string;
130
+ library?: string;
131
+ module?: string;
132
+ buildType?: string;
133
+ productFlavors?: string[];
134
+ };
135
+
136
+ export declare type CdnLocale = {
137
+ /**
138
+ * Locale code.
139
+ */
140
+ locale: string;
141
+ /**
142
+ * Boolean indicating if this is the base locale.
143
+ */
144
+ isBaseLocale: boolean;
145
+ /**
146
+ * Language code.
147
+ */
148
+ language: string;
149
+ /**
150
+ * Region code.
151
+ */
152
+ region?: string;
153
+ /**
154
+ * Script code.
155
+ */
156
+ script?: string;
157
+ /**
158
+ * Boolean indicating if the locale is right-to-left.
159
+ */
160
+ isRtl: boolean;
161
+ /**
162
+ * Locale name.
163
+ */
164
+ name: string;
165
+ /**
166
+ * Localized locale name.
167
+ */
168
+ localizedName: string;
169
+ };
170
+
171
+ export declare type CdnLocalesOptions = {
172
+ /**
173
+ * Exclude the base locale from the list of locales.
174
+ */
175
+ excludeBaseLocale?: boolean;
176
+ };
177
+
178
+ export declare class CdnMetafile extends CdnBase {
179
+ files: CdnMetafileFiles;
180
+ constructor(context: Context);
181
+ get projectUrl(): string;
182
+ get baseLocale(): CdnLocale;
183
+ get url(): string;
184
+ locales: (options?: CdnLocalesOptions) => CdnLocale[];
185
+ refresh: (config?: AxiosRequestConfig) => Promise<void>;
186
+ switch: (options: CdnClientOptions, config?: AxiosRequestConfig) => Promise<void>;
187
+ }
188
+
189
+ export declare class CdnMetafileFiles extends CdnBase {
190
+ list: () => CdnFile[];
191
+ filter: (options: CdnFilesSearchOptions) => CdnFile[];
192
+ find: (options: CdnFilesSearchOptions) => CdnFile | undefined;
193
+ first: () => CdnFile;
194
+ protected static transformFiles: (files: MetafileFile[]) => CdnFile[];
195
+ }
196
+
197
+ export declare type CdnResponse =
198
+ /**
199
+ * Map of file IDs with locales map as value.
200
+ */
201
+ {
202
+ [fileId: string]: {
203
+ /**
204
+ * Map of locales with file content as value.
205
+ */
206
+ [locale: string]: object | string;
207
+ };
208
+ }
209
+ /**
210
+ * Map of locales with file content as value.
211
+ */
212
+ | {
213
+ [locale: string]: object | string;
214
+ }
215
+ /**
216
+ * File content.
217
+ */
218
+ | object
219
+ /**
220
+ * File content as string for non-JSON files.
221
+ */
222
+ | string;
223
+
224
+ export declare class Context {
225
+ metafile: MetafileContext;
226
+ cdn: CdnClient;
227
+ client: IHttpAdapter;
228
+ api: Api;
229
+ cache: LocalesCache;
230
+ responseFactory: ResponseFactory;
231
+ constructor(options: ContextOptions);
232
+ }
233
+
234
+ export declare type ContextOptions = {
235
+ metafileContext: MetafileContext;
236
+ cdn: CdnClient;
237
+ client: IHttpAdapter;
238
+ };
239
+
240
+ export declare type ExcludeBaseLocale = {
241
+ excludeBaseLocale?: boolean;
242
+ };
243
+
244
+ export declare type FileResponse = {
245
+ [fileId: string]: LocaleResponse;
246
+ };
247
+
248
+ export declare type FilesMap = {
249
+ [id: string]: MetafileFile;
250
+ };
251
+
252
+ export declare interface ICacheAdapter<K, V> {
253
+ has: (key: K) => boolean;
254
+ get: (key: K) => V | undefined;
255
+ set: (key: K, value: V) => void;
256
+ flush: () => void;
257
+ }
258
+
259
+ export declare interface IHttpAdapter {
260
+ get<T = unknown, D = unknown>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
261
+ }
262
+
263
+ export declare interface ILocalesCacheItem {
264
+ metafileFile: MetafileFile;
265
+ metafileLocale: MetafileLocale;
266
+ }
267
+
268
+ export declare interface IMetafile {
269
+ projectUrl: string;
270
+ baseLocale: string;
271
+ timestamp: number;
272
+ files: IMetafileFiles;
273
+ }
274
+
275
+ export declare interface IMetafileFile {
276
+ file: string;
277
+ path: string;
278
+ library: string;
279
+ module: string;
280
+ buildType: string;
281
+ timestamp: number;
282
+ productFlavors: string[];
283
+ locales: IMetafileFileLocale[];
284
+ }
285
+
286
+ export declare interface IMetafileFileLocale {
287
+ language: string;
288
+ region: string;
289
+ script: string;
290
+ isRtl: boolean;
291
+ name: string;
292
+ localizedName: string;
293
+ uri: string;
294
+ timestamp: number;
295
+ }
296
+
297
+ export declare interface IMetafileFiles {
298
+ [id: string]: IMetafileFile;
299
+ }
300
+
301
+ export declare interface IMetafileParams {
302
+ url: string;
303
+ baseUrl: string;
304
+ cdnId: string;
305
+ jsonPath: string;
306
+ }
307
+
308
+ export declare interface IRequestBuilder {
309
+ addFiles(request?: (CdnFile | string)[] | CdnFile | string): IRequestBuilder;
310
+ addLocales(request?: string[] | string, excludeBaseLocale?: boolean): IRequestBuilder;
311
+ getCdnRequest(): Request_2;
312
+ }
313
+
314
+ export declare type JsonResponse = {
315
+ [key: string]: Record<string, object>;
316
+ };
317
+
318
+ export declare type LocaleResponse = {
319
+ [locale: string]: JsonResponse;
320
+ };
321
+
322
+ export declare class LocalesCache {
323
+ protected context: Context;
324
+ protected cacheAdapter: ICacheAdapter<string, object | string>;
325
+ constructor(context: Context);
326
+ setIfMissed(options: CacheStoreLocalesRequest): void;
327
+ has(options: CacheHasLocalesRequest): boolean;
328
+ get(options: CacheGetLocalesRequest): object | string | undefined;
329
+ flush(): void;
330
+ protected keyFromMetafile(options: CacheKeyMetafileOptions): string;
331
+ }
332
+
333
+ export declare class LocalesMap {
334
+ data: LocalesMapData;
335
+ protected context: Context;
336
+ constructor(options: LocalesMapOptions);
337
+ }
338
+
339
+ export declare type LocalesMapData = {
340
+ [key: string]: MetafileLocale[];
341
+ };
342
+
343
+ export declare type LocalesMapOptions = {
344
+ context: Context;
345
+ data?: LocalesMapData;
346
+ };
347
+
348
+ export declare class MemoryCacheAdapter<K, V> implements ICacheAdapter<K, V> {
349
+ protected map: Map<K, V>;
350
+ constructor();
351
+ get(key: K): V | undefined;
352
+ has(key: K): boolean;
353
+ set(key: K, value: V): void;
354
+ flush(): void;
355
+ }
356
+
357
+ export declare class MetafileContext {
358
+ params: MetafileParams;
359
+ parsedData: IMetafile | null;
360
+ data: MetafileData;
361
+ constructor(options: CdnClientOptions);
362
+ setMetafile(metafile: IMetafile): void;
363
+ }
364
+
365
+ export declare class MetafileData implements Omit<IMetafile, 'files' | 'baseLocale'> {
366
+ projectUrl: string;
367
+ baseLocale: CdnLocale;
368
+ locales: CdnLocale[];
369
+ timestamp: number;
370
+ files: MetafileFile[];
371
+ filesMap: FilesMap;
372
+ constructor(options: MetafileOptions, params: MetafileParams);
373
+ static createEmpty(params: MetafileParams): MetafileData;
374
+ protected static filesFactory(files: IMetafileFiles, baseLocale: string, params: MetafileParams): MetafileFile[];
375
+ protected static filesMapFactory(files: MetafileFile[]): FilesMap;
376
+ protected static localesFactory(files: MetafileFile[]): CdnLocale[];
377
+ }
378
+
379
+ export declare class MetafileFile implements Omit<IMetafileFile, 'locales'> {
380
+ id: string;
381
+ file: string;
382
+ path: string;
383
+ library: string;
384
+ module: string;
385
+ buildType: string;
386
+ timestamp: number;
387
+ productFlavors: string[];
388
+ locales: MetafileLocale[];
389
+ protected baseUrl: string;
390
+ constructor(options: MetafileFileOptions);
391
+ toCdnFile(): CdnFile;
392
+ }
393
+
394
+ export declare type MetafileFileOptions = Omit<IMetafileFile, 'locales'> & {
395
+ id: string;
396
+ locales: MetafileLocale[];
397
+ baseUrl: string;
398
+ };
399
+
400
+ export declare class MetafileLocale implements IMetafileFileLocale {
401
+ language: string;
402
+ region: string;
403
+ script: string;
404
+ isRtl: boolean;
405
+ name: string;
406
+ localizedName: string;
407
+ uri: string;
408
+ timestamp: number;
409
+ protected baseLocale: string;
410
+ constructor(options: IMetafileFileLocale, baseLocale: string);
411
+ get locale(): string;
412
+ get isBaseLocale(): boolean;
413
+ toCdnLocale(): CdnLocale;
414
+ }
415
+
416
+ export declare type MetafileOptions = IMetafile;
417
+
418
+ export declare class MetafileParams implements IMetafileParams {
419
+ protected options: IMetafileParams;
420
+ constructor(metafileUrl: string);
421
+ get url(): string;
422
+ get baseUrl(): string;
423
+ get cdnId(): string;
424
+ get jsonPath(): string;
425
+ protected static parseMetafileUrl(metafileUrl: string): IMetafileParams;
426
+ }
427
+
428
+ declare class Request_2 {
429
+ files: MetafileFile[];
430
+ localesMap: LocalesMap;
431
+ hasSingleFileResponse: boolean;
432
+ hasSingleLocaleResponse: boolean;
433
+ protected context: Context;
434
+ constructor(context: Context);
435
+ execute(): Promise<CdnResponse>;
436
+ protected getPromises(): Promise<AxiosResponse<CdnResponse>>[];
437
+ }
438
+ export { Request_2 as Request }
439
+
440
+ export declare class RequestBuilder implements IRequestBuilder {
441
+ protected context: Context;
442
+ protected request: Request_2;
443
+ constructor(context: Context);
444
+ addFiles(files?: (CdnFile | string)[] | CdnFile | string): RequestBuilder;
445
+ addLocales(locales?: string[] | string, excludeBaseLocale?: boolean): RequestBuilder;
446
+ getCdnRequest(): Request_2;
447
+ }
448
+
449
+ export declare class ResponseFactory {
450
+ protected context: Context;
451
+ constructor(context: Context);
452
+ 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>;
456
+ }
457
+
458
+ export declare type ResponseFactoryOptions = {
459
+ responses: AxiosResponse<CdnResponse>[];
460
+ localesMap: LocalesMap;
461
+ hasSingleFileResponse: boolean;
462
+ hasSingleLocaleResponse: boolean;
463
+ };
464
+
465
+ export { }