@@ -23,44 +23,35 @@ import {
23
23
} from '@angular/common/http' ;
24
24
import { isDevMode , Type } from '@angular/core' ;
25
25
26
- type Fetch = NonNullable < Document [ 'defaultView' ] > [ 'fetch' ] ;
27
- type Handle = InstanceType < typeof HttpBackend > [ 'handle' ] ;
28
-
29
- const fallback : Fetch = ( ) =>
26
+ const fallback = ( ) =>
30
27
Promise . reject ( 'fetch not available, is this server side ?' ) ;
31
28
32
- const noHandle = ( ) =>
33
- throwError ( ( ) => ( {
34
- statusText : 'HttpBackend not defined. Are providers missing?' ,
35
- } ) ) ;
36
29
37
30
export class MultiLoader implements TranslateLoader {
38
- #fetch: Fetch ;
39
- #handle: Handle ;
40
31
#isHttp: boolean ;
41
32
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 ;
52
38
}
53
39
54
40
#buildUrl( source : string , lang : string ) {
55
41
return source . replace ( languageSourcePlaceholder , lang ) ;
56
42
}
57
43
58
44
async #get( urls : string [ ] ) : Promise < TranslationObject > {
45
+
46
+ if ( ! this . #isHttp) {
47
+ throw new Error ( 'MultiLoader must be used with HttpBackend' ) ;
48
+ }
49
+
59
50
// TODO optimize for httpBackend <Observable API>
60
51
const translations = await Promise . all (
61
52
urls . map ( ( url ) =>
62
53
firstValueFrom (
63
- this . # handle( new HttpRequest ( 'GET' , url ) ) . pipe (
54
+ ( this . _api as HttpBackend ) . handle ( new HttpRequest ( 'GET' , url ) ) . pipe (
64
55
filter ( ( e ) => e instanceof HttpResponse ) ,
65
56
map ( ( r ) => r . body ) ,
66
57
catchError ( ( r : HttpErrorResponse ) => {
@@ -78,7 +69,7 @@ export class MultiLoader implements TranslateLoader {
78
69
}
79
70
80
71
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 ) ) ) ;
82
73
// log errors if any
83
74
responses . forEach ( ( r ) => {
84
75
if ( ! r . ok )
0 commit comments