Skip to content

Commit 8156b3d

Browse files
JeanMechealxhub
authored andcommitted
fix(http): Don't override the backend when using the InMemoryWebAPI (angular#52425)
When using `withFetch`, the `PRIMARY_HTTP_BACKEND` token is set. The InMemory Backend services will also set that token. This means that providers order will matter and the latest on the list will be the one instantiated PR Close angular#52425
1 parent 59a3475 commit 8156b3d

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

packages/common/http/src/private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS} from './interceptor';
9+
export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS, PRIMARY_HTTP_BACKEND as ɵPRIMARY_HTTP_BACKEND} from './interceptor';

packages/misc/angular-in-memory-web-api/src/http-client-in-memory-web-api-module.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {XhrFactory} from '@angular/common';
10-
import {HttpBackend} from '@angular/common/http';
9+
import {HttpBackend, ɵPRIMARY_HTTP_BACKEND as PRIMARY_HTTP_BACKEND} from '@angular/common/http';
1110
import {ModuleWithProviders, NgModule, Type} from '@angular/core';
1211

1312
import {HttpClientBackendService} from './http-client-backend-service';
1413
import {InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService} from './interfaces';
1514

16-
// Internal - Creates the in-mem backend for the HttpClient module
17-
// AoT requires factory to be exported
18-
export function httpClientInMemBackendServiceFactory(
19-
dbService: InMemoryDbService, options: InMemoryBackendConfig,
20-
xhrFactory: XhrFactory): HttpBackend {
21-
return new HttpClientBackendService(dbService, options, xhrFactory) as HttpBackend;
22-
}
23-
2415
@NgModule()
2516
export class HttpClientInMemoryWebApiModule {
2617
/**
@@ -31,6 +22,8 @@ export class HttpClientInMemoryWebApiModule {
3122
* Usually imported in the root application module.
3223
* Can import in a lazy feature module too, which will shadow modules loaded earlier
3324
*
25+
* Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list
26+
*
3427
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
3528
* InMemoryDbService.
3629
* @param [options]
@@ -44,12 +37,10 @@ export class HttpClientInMemoryWebApiModule {
4437
return {
4538
ngModule: HttpClientInMemoryWebApiModule,
4639
providers: [
47-
{provide: InMemoryDbService, useClass: dbCreator},
48-
{provide: InMemoryBackendConfig, useValue: options}, {
49-
provide: HttpBackend,
50-
useFactory: httpClientInMemBackendServiceFactory,
51-
deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory]
52-
}
40+
HttpClientBackendService, {provide: InMemoryDbService, useClass: dbCreator},
41+
{provide: InMemoryBackendConfig, useValue: options},
42+
{provide: HttpBackend, useExisting: HttpClientBackendService},
43+
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService}
5344
]
5445
};
5546
}

packages/misc/angular-in-memory-web-api/src/in-memory-web-api-module.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {XhrFactory} from '@angular/common';
10-
import {HttpBackend} from '@angular/common/http';
9+
import {HttpBackend, ɵPRIMARY_HTTP_BACKEND as PRIMARY_HTTP_BACKEND} from '@angular/common/http';
1110
import {ModuleWithProviders, NgModule, Type} from '@angular/core';
1211

13-
import {httpClientInMemBackendServiceFactory} from './http-client-in-memory-web-api-module';
12+
import {HttpClientBackendService} from './http-client-backend-service';
1413
import {InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService} from './interfaces';
1514

1615
@NgModule()
@@ -23,6 +22,8 @@ export class InMemoryWebApiModule {
2322
* Usually imported in the root application module.
2423
* Can import in a lazy feature module too, which will shadow modules loaded earlier
2524
*
25+
* Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list
26+
*
2627
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
2728
* InMemoryDbService.
2829
* @param [options]
@@ -37,11 +38,9 @@ export class InMemoryWebApiModule {
3738
ngModule: InMemoryWebApiModule,
3839
providers: [
3940
{provide: InMemoryDbService, useClass: dbCreator},
40-
{provide: InMemoryBackendConfig, useValue: options}, {
41-
provide: HttpBackend,
42-
useFactory: httpClientInMemBackendServiceFactory,
43-
deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory]
44-
}
41+
{provide: InMemoryBackendConfig, useValue: options},
42+
{provide: HttpBackend, useClass: HttpClientBackendService},
43+
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService}
4544
]
4645
};
4746
}

packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import 'jasmine-ajax';
1010

11-
import {HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
12-
import {Injectable} from '@angular/core';
11+
import {FetchBackend, HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse, provideHttpClient, withFetch} from '@angular/common/http';
12+
import {importProvidersFrom, Injectable} from '@angular/core';
1313
import {TestBed, waitForAsync} from '@angular/core/testing';
1414
import {HttpClientBackendService, HttpClientInMemoryWebApiModule} from 'angular-in-memory-web-api';
1515
import {Observable, zip} from 'rxjs';
@@ -565,6 +565,34 @@ describe('HttpClient Backend Service', () => {
565565
failRequest);
566566
}));
567567
});
568+
569+
describe('when using the FetchBackend', () => {
570+
it('should be the an InMemory Service', () => {
571+
TestBed.configureTestingModule({
572+
providers: [
573+
provideHttpClient(withFetch()),
574+
importProvidersFrom(
575+
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {delay})),
576+
{provide: HeroService, useClass: HttpClientHeroService}
577+
]
578+
});
579+
580+
expect(TestBed.inject(HttpBackend)).toBeInstanceOf(HttpClientBackendService);
581+
});
582+
583+
it('should be a FetchBackend', () => {
584+
// In this test, providers order matters
585+
TestBed.configureTestingModule({
586+
providers: [
587+
importProvidersFrom(
588+
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {delay})),
589+
provideHttpClient(withFetch()), {provide: HeroService, useClass: HttpClientHeroService}
590+
]
591+
});
592+
593+
expect(TestBed.inject(HttpBackend)).toBeInstanceOf(FetchBackend);
594+
});
595+
});
568596
});
569597

570598

0 commit comments

Comments
 (0)