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

Selectively Switch off XHRBackend Provider per call #110

Open momoadeli opened 7 years ago

momoadeli commented 7 years ago

As real endpoints come on line we would like for THAT particular endpoint to be called for real while others continue with their in mem calls.

I'm aware of InMemoryWebApiModule.forRoot(InMemoryMockAPIDataService, {passThruUnknownUrl: true}). However, this universally overrides the XHRBackend and disables all in memory http calls. Is there a way to switch off the in memory mock selectively and make the real http call PER endpoint?

Thanks!

vikikamath commented 7 years ago

I believe it can be achieved with following workaround

From above plnkr:

Hero Service - requires to be served from Backend Hero Search Service - requires to be served from In Memory Web Api

Note: both services use the same collection: heroes

In app/app.module.ts set config option apiBase to something like api/root/ ( key: apiBase path should contain more than one token, apiBase config must be set and external api must not be identical to apiBase value set):

...
InMemoryWebApiModule.forRoot(InMemoryDataService, {apiBase:"api/root/", passThruUnknownUrl: true }),
...

In hero-search.service, that is to be served from In Memory

private heroSearchUrl = 'api/root/heroes/?name='

In hero.service, that is to be served from real external service

private heroesUrl = 'https://58ac43fef989751200f99333.mockapi.io/v1/heroes/';