Skip to content

Commit 2796041

Browse files
committed
v0.0.8 : release
1 parent 2ea9cd8 commit 2796041

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

projects/ngx-translate-toolkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@robmanganelly/ngx-translate-toolkit",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"peerDependencies": {
55
"@angular/common": "^20.0.0",
66
"@angular/core": "^20.0.0",

projects/ngx-translate-toolkit/src/lib/multi-loader.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,35 @@ import {
2323
} from '@angular/common/http';
2424
import { isDevMode, Type } from '@angular/core';
2525

26-
type Fetch = NonNullable<Document['defaultView']>['fetch'];
27-
type Handle = InstanceType<typeof HttpBackend>['handle'];
28-
29-
const fallback: Fetch = () =>
26+
const fallback = () =>
3027
Promise.reject('fetch not available, is this server side ?');
3128

32-
const noHandle = () =>
33-
throwError(() => ({
34-
statusText: 'HttpBackend not defined. Are providers missing?',
35-
}));
3629

3730
export class MultiLoader implements TranslateLoader {
38-
#fetch: Fetch;
39-
#handle: Handle;
4031
#isHttp: boolean;
4132

42-
constructor(_api: Document | HttpBackend, private _sources: string[]) {
43-
if (_api instanceof HttpBackend) {
44-
this.#isHttp = false;
45-
this.#fetch = fallback;
46-
this.#handle = _api.handle ?? noHandle;
47-
} else {
48-
this.#isHttp = true;
49-
this.#fetch = _api.defaultView?.fetch ?? fallback;
50-
this.#handle = noHandle;
51-
}
33+
constructor(
34+
private _api: Document | HttpBackend,
35+
private _sources: string[]
36+
) {
37+
this.#isHttp = _api instanceof HttpBackend;
5238
}
5339

5440
#buildUrl(source: string, lang: string) {
5541
return source.replace(languageSourcePlaceholder, lang);
5642
}
5743

5844
async #get(urls: string[]): Promise<TranslationObject> {
45+
46+
if (!this.#isHttp) {
47+
throw new Error('MultiLoader must be used with HttpBackend');
48+
}
49+
5950
// TODO optimize for httpBackend <Observable API>
6051
const translations = await Promise.all(
6152
urls.map((url) =>
6253
firstValueFrom(
63-
this.#handle(new HttpRequest('GET', url)).pipe(
54+
(this._api as HttpBackend).handle(new HttpRequest('GET', url)).pipe(
6455
filter((e) => e instanceof HttpResponse),
6556
map((r) => r.body),
6657
catchError((r: HttpErrorResponse) => {
@@ -78,7 +69,7 @@ export class MultiLoader implements TranslateLoader {
7869
}
7970

8071
async #fetchData(urls: string[]): Promise<TranslationObject> {
81-
const responses = await Promise.all(urls.map((u) => this.#fetch(u)));
72+
const responses = await Promise.all(urls.map((u) => ((this._api as Document).defaultView?.fetch ?? fallback)(u)));
8273
// log errors if any
8374
responses.forEach((r) => {
8475
if (!r.ok)

0 commit comments

Comments
 (0)