1- import {
2- HttpClient ,
3- HttpContext ,
4- HttpContextToken ,
5- } from '@angular/common/http' ;
6- import { inject , Injectable , Injector } from '@angular/core' ;
7- import { pendingUntilEvent } from '@angular/core/rxjs-interop' ;
1+ import { inject , Injectable } from '@angular/core' ;
82import { Exception } from '@angularity/core' ;
9- import { catchError , map , Observable , switchMap } from 'rxjs' ;
3+ import { Observable } from 'rxjs' ;
104
115import { CacheConfigFiles } from './behaviors' ;
126import { ConfigFileDefinition } from './definition' ;
7+ import { HttpClientConfigFileLoader } from './loader.http' ;
138
149/**
1510 * Service for loading configuration files.
@@ -35,48 +30,3 @@ export abstract class ConfigFileLoader {
3530}
3631
3732export class ConfigFileNotFoundException extends Exception { }
38-
39- /**
40- * `HttpContextToken` that will be set to `true` in an endpoint request
41- * sent by `HttpClientConfigFileLoader`.
42- */
43- export const IS_CONFIG_FILE_REQUEST = new HttpContextToken < boolean > (
44- ( ) => false ,
45- ) ;
46-
47- /**
48- * Implementation of {@link ConfigFileLoader}
49- * based on Angular's built-in {@link HttpClient}.
50- */
51- @Injectable ( {
52- providedIn : 'root' ,
53- } )
54- export class HttpClientConfigFileLoader implements ConfigFileLoader {
55- protected httpClient = inject ( HttpClient ) ;
56- protected injector = inject ( Injector ) ;
57-
58- load < T , Schema > ( def : ConfigFileDefinition < T , Schema > ) : Observable < T > {
59- const parser = this . injector . get ( def . parser ) ;
60- const validator = this . injector . get ( def . validator ) ;
61-
62- return this . fetch ( def . path ) . pipe (
63- map ( ( res ) => {
64- if ( ! res ) throw new ConfigFileNotFoundException ( def . path ) ;
65- return res ;
66- } ) ,
67- switchMap ( async ( raw ) => {
68- const parsed = await parser . parse ( raw ) ;
69- await validator . validate ( def . schema , parsed ) ;
70- return parsed as T ;
71- } ) ,
72- pendingUntilEvent ( this . injector ) ,
73- ) ;
74- }
75-
76- protected fetch ( path : string ) : Observable < string | null > {
77- const context = new HttpContext ( ) . set ( IS_CONFIG_FILE_REQUEST , true ) ;
78- return this . httpClient
79- . get ( path , { responseType : 'text' , context } )
80- . pipe ( catchError ( ( ) => [ null ] ) ) ;
81- }
82- }
0 commit comments