I am following this tutorial (oficial Angular2 "Quick Start" tutorial)
At the end of it, there are couple of http requests (GET, POST, PUT, DELETE...), which, instead of calling a real server, are simulated using this lines in the app.module.ts
:
// The HttpClientInMemoryWebApiModule module intercepts HTTP requests
// and returns simulated server responses.
// Remove it when a real server is ready to receive requests.
HttpClientInMemoryWebApiModule.forRoot(
InMemoryDataService, { dataEncapsulation: false }
)
Here is an example of a HTTP Request made by the application:
private heroesUrl = 'api/heroes'; // URL to web api
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
);
}
The question: is there any way to configure the endpoints expected by this InMemoryDataService? So, for instance, I could change the Url to api/heroes/getAll
.
Following @Szarik recommendations, I follow this link, which has the answer. Actually, it's the official specification of the Library I am using: in-memory-web-api
Diving a little in the README.md, we reach this:
You can override the default parser by implementing a parseRequestUrl method in your InMemoryDbService.
The service calls your method with two arguments.
- url - the request URL string
- requestInfoUtils - utility methods in a RequestInfoUtilities object, including the default parser. Note that some values have not yet been set as they depend on the outcome of parsing.
Your method must either return a ParsedRequestUrl object or null|undefined, in which case the service uses the default parser. In this way you can intercept and parse some URLs and leave the others to the default parser.
So that was it!