File tree Expand file tree Collapse file tree 8 files changed +103
-8
lines changed Expand file tree Collapse file tree 8 files changed +103
-8
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ import { ProductsModule } from './products/products.module';
14
14
import { HttpClientModule } from '@angular/common/http' ;
15
15
import { MatBadgeModule } from '@angular/material/badge' ;
16
16
import { CartModule } from './cart/cart.module' ;
17
+ import { CONFIG_TOKEN } from './core/injection-tokens/config.token' ;
18
+ import { environment } from '../environments/environment' ;
17
19
18
20
@NgModule ( {
19
21
declarations : [ AppComponent , HeaderComponent ] ,
@@ -31,7 +33,12 @@ import { CartModule } from './cart/cart.module';
31
33
HttpClientModule ,
32
34
MatBadgeModule ,
33
35
] ,
34
- providers : [ ] ,
36
+ providers : [
37
+ {
38
+ provide : CONFIG_TOKEN ,
39
+ useValue : environment ,
40
+ } ,
41
+ ] ,
35
42
bootstrap : [ AppComponent ] ,
36
43
} )
37
44
export class AppModule { }
Original file line number Diff line number Diff line change
1
+ import { TestBed } from '@angular/core/testing' ;
2
+
3
+ import { ApiService } from './api.service' ;
4
+
5
+ describe ( 'ApiService' , ( ) => {
6
+ let service : ApiService ;
7
+
8
+ beforeEach ( ( ) => {
9
+ TestBed . configureTestingModule ( { } ) ;
10
+ service = TestBed . inject ( ApiService ) ;
11
+ } ) ;
12
+
13
+ it ( 'should be created' , ( ) => {
14
+ expect ( service ) . toBeTruthy ( ) ;
15
+ } ) ;
16
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { Injectable , Injector } from '@angular/core' ;
2
+ import { ApiEndpoint , Config } from '../../environments/config.interface' ;
3
+ import { CONFIG_TOKEN } from './injection-tokens/config.token' ;
4
+ import { Location } from '@angular/common' ;
5
+ import { HttpClient } from '@angular/common/http' ;
6
+
7
+ /** Base class for services working with APIs */
8
+ @Injectable ( )
9
+ export abstract class ApiService {
10
+ protected readonly config : Config ;
11
+ protected readonly http : HttpClient ;
12
+
13
+ protected constructor ( protected readonly injector : Injector ) {
14
+ this . config = injector . get ( CONFIG_TOKEN ) ;
15
+ this . http = injector . get ( HttpClient ) ;
16
+ }
17
+
18
+ endpointEnabled ( api : ApiEndpoint ) : boolean {
19
+ return this . config . apiEndpointsEnabled [ api ] ;
20
+ }
21
+
22
+ /** Combines API endpoint and path into a single URL */
23
+ protected getUrl ( api : ApiEndpoint , path : string ) : string {
24
+ return Location . joinWithSlash ( this . config . apiEndpoints [ api ] , path ) ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ import { InjectionToken } from '@angular/core' ;
2
+ import { Config } from '../../../environments/config.interface' ;
3
+
4
+ export const CONFIG_TOKEN = new InjectionToken < Config > ( 'CONFIG_TOKEN' ) ;
Original file line number Diff line number Diff line change 1
1
import { Injectable } from '@angular/core' ;
2
- import { HttpClient } from '@angular/common/http' ;
3
2
import { Observable , of } from 'rxjs' ;
4
3
import { Product } from './product.interface' ;
5
4
import { map } from 'rxjs/operators' ;
5
+ import { ApiService } from '../core/api.service' ;
6
6
7
7
@Injectable ( {
8
8
providedIn : 'root' ,
9
9
} )
10
- export class ProductsService {
11
- constructor ( private readonly http : HttpClient ) { }
12
-
10
+ export class ProductsService extends ApiService {
13
11
getProducts ( ) : Observable < Product [ ] > {
14
- return this . http . get < Product [ ] > ( '/assets/products.json' ) ;
12
+ if ( ! this . endpointEnabled ( 'bff' ) ) {
13
+ return this . http . get < Product [ ] > ( '/assets/products.json' ) ;
14
+ }
15
+
16
+ const url = this . getUrl ( 'bff' , 'products' ) ;
17
+ return this . http . get < Product [ ] > ( url ) ;
15
18
}
16
19
17
20
getProductsForCheckout ( ids : string [ ] ) : Observable < Product [ ] > {
Original file line number Diff line number Diff line change
1
+ export type ApiEndpoint = 'product' | 'order' | 'import' | 'bff' | 'cart' ;
2
+
3
+ export interface Config {
4
+ production : boolean ;
5
+ apiEndpoints : Record < ApiEndpoint , string > ;
6
+ apiEndpointsEnabled : Record < ApiEndpoint , boolean > ;
7
+ }
Original file line number Diff line number Diff line change 1
- export const environment = {
1
+ import { Config } from './config.interface' ;
2
+
3
+ export const environment : Config = {
2
4
production : true ,
5
+ apiEndpoints : {
6
+ product : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
7
+ order : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
8
+ import : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
9
+ bff : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
10
+ cart : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
11
+ } ,
12
+ apiEndpointsEnabled : {
13
+ product : false ,
14
+ order : false ,
15
+ import : false ,
16
+ bff : false ,
17
+ cart : false ,
18
+ } ,
3
19
} ;
Original file line number Diff line number Diff line change 2
2
// `ng build --prod` replaces `environment.ts` with `environment.prod.ts`.
3
3
// The list of file replacements can be found in `angular.json`.
4
4
5
- export const environment = {
5
+ import { Config } from './config.interface' ;
6
+
7
+ export const environment : Config = {
6
8
production : false ,
9
+ apiEndpoints : {
10
+ product : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
11
+ order : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
12
+ import : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
13
+ bff : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
14
+ cart : 'https://.execute-api.eu-west-1.amazonaws.com/dev' ,
15
+ } ,
16
+ apiEndpointsEnabled : {
17
+ product : false ,
18
+ order : false ,
19
+ import : false ,
20
+ bff : false ,
21
+ cart : false ,
22
+ } ,
7
23
} ;
8
24
9
25
/*
You can’t perform that action at this time.
0 commit comments