Skip to content

Commit da429c5

Browse files
alxhubpkozlowski-opensource
authored andcommitted
Revert "fix(http): Don't override the backend when using the InMemoryWebAPI (#52425)" (#52630)
This reverts commit 49b037f. Reason: it breaks tests in aio-local. PR Close #52630
1 parent fc6b2f2 commit da429c5

File tree

4 files changed

+27
-45
lines changed

4 files changed

+27
-45
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, PRIMARY_HTTP_BACKEND as ɵPRIMARY_HTTP_BACKEND} from './interceptor';
9+
export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS} from './interceptor';

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

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

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

1213
import {HttpClientBackendService} from './http-client-backend-service';
1314
import {InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService} from './interfaces';
1415

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+
1524
@NgModule()
1625
export class HttpClientInMemoryWebApiModule {
1726
/**
@@ -22,8 +31,6 @@ export class HttpClientInMemoryWebApiModule {
2231
* Usually imported in the root application module.
2332
* Can import in a lazy feature module too, which will shadow modules loaded earlier
2433
*
25-
* Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list
26-
*
2734
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
2835
* InMemoryDbService.
2936
* @param [options]
@@ -37,10 +44,12 @@ export class HttpClientInMemoryWebApiModule {
3744
return {
3845
ngModule: HttpClientInMemoryWebApiModule,
3946
providers: [
40-
HttpClientBackendService, {provide: InMemoryDbService, useClass: dbCreator},
41-
{provide: InMemoryBackendConfig, useValue: options},
42-
{provide: HttpBackend, useExisting: HttpClientBackendService},
43-
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService}
47+
{provide: InMemoryDbService, useClass: dbCreator},
48+
{provide: InMemoryBackendConfig, useValue: options}, {
49+
provide: HttpBackend,
50+
useFactory: httpClientInMemBackendServiceFactory,
51+
deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory]
52+
}
4453
]
4554
};
4655
}

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

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

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

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

1516
@NgModule()
@@ -22,8 +23,6 @@ export class InMemoryWebApiModule {
2223
* Usually imported in the root application module.
2324
* Can import in a lazy feature module too, which will shadow modules loaded earlier
2425
*
25-
* Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list
26-
*
2726
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
2827
* InMemoryDbService.
2928
* @param [options]
@@ -38,9 +37,11 @@ export class InMemoryWebApiModule {
3837
ngModule: InMemoryWebApiModule,
3938
providers: [
4039
{provide: InMemoryDbService, useClass: dbCreator},
41-
{provide: InMemoryBackendConfig, useValue: options},
42-
{provide: HttpBackend, useClass: HttpClientBackendService},
43-
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService}
40+
{provide: InMemoryBackendConfig, useValue: options}, {
41+
provide: HttpBackend,
42+
useFactory: httpClientInMemBackendServiceFactory,
43+
deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory]
44+
}
4445
]
4546
};
4647
}

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

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

99
import 'jasmine-ajax';
1010

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';
11+
import {HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
12+
import {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,34 +565,6 @@ 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-
});
596568
});
597569

598570

0 commit comments

Comments
 (0)