angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 231 forks source link

Use of HttpInterceptor in forFeature in-memory will not work #167

Open jsgoupil opened 6 years ago

jsgoupil commented 6 years ago

When trying to register the InMemoryDbService it is required to have the HttpClientModule loaded as a sibling. This is problematic when the HttpInterceptors are attached to the root HttpClientModule. In a forFeature (loadChildren route), my root interceptors will be ignored.

I understand my scenario might be a little out of scope for handling basic GET/POST. But if I reduce it to the bare bone, this is what I get.

app.module

imports: [
        HttpClientModule,
        HttpClientInMemoryWebApiModule.forRoot(MyInMemoryDbService, { passThruUnknownUrl: true })
],
providers: [
{
        provide: HTTP_INTERCEPTORS,
        useClass: MyInterceptor,
        multi: true
    }
]

admin.module

import: [
HttpClientInMemoryWebApiModule.forFeature(AdminMemoryDbService) // Does not load without HttpClientModule, but adding HttpClientModule will clear my HttpInterceptor
}

My ugly workaround is to load MyHttpClientModule with my interceptors everywhere where I add the HttpClientInMemoryWebApiModule.

xmlking commented 6 years ago

here is the scenario how we like to use HttpClientInMemoryWebApiModule with root and feature modules: Best practice is not to inject HttpClientModule twice. but in-memory-web-api is forcing us to do so.

HttpClientInMemoryWebApiModule forRoot at Root Module https://github.com/xmlking/nx-starter-kit/blob/master/apps/default/src/app/core/core.module.ts#L13

HttpClientInMemoryWebApiModule for forFeature Module https://github.com/xmlking/nx-starter-kit/blob/master/libs/dashboard/src/dashboard.module.ts#L23

damienwebdev commented 6 years ago

@wardbell While not hyper critical, I still think this issue has merit to at least be discussed.

Functionally, the issue exists because of having to import HttpClientModule in feature modules in order for the Mock API's parseRequestUrl to get picked up.