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

Example of constructing responses in HTTP method interceptors + typings problem #243

Open arturhun opened 5 years ago

arturhun commented 5 years ago

Please provide a simple example of constructing a valid response in a method interceptor.

The ticket #44 refers to something that apparently has been moved, and the example in the README is very general, succinct and I find it not helpful at all.

arturhun commented 5 years ago

By looking into interfaces.d.ts I managed to come up with something like:

delete(reqInfo: RequestInfo) {
  reqInfo.utils.createResponse$(() => {
    return of({
      collection: collection.filter(item => !deleteSet.has(item.id)),
    });
  });
}

Functionally, this seems to work, however:

We have type RequestInfo = Request | string;

but (reqInfo as Request).utils. still leaves the squiggles underneath utils.

The above shows that the docs leave much of guesswork for a developer and it could be avoided by provided a decent example.

vishal423 commented 5 years ago

did you check under this directory for examples? https://github.com/angular/in-memory-web-api/tree/master/src/app

arturhun commented 5 years ago

OK, the typing problem came from that I didn't have an explicit import from the library and so TypeScript used RequestInfo from lib.dom.d.ts

arturhun commented 5 years ago

@vishal423 Yes I did. and the code that I used based on that

return of({
    items: collection.filter(item => !deleteSet.has(item.id)),  // my collection is `items`
});

didn't work.