angular/in-memory-web-api

Example of constructing responses in HTTP method interceptors + typings problem

arturhun opened this issue · 4 comments

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.

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:

  • I assume one would also need to clone the reqInfo parameter to get all the headers, and other props
  • TS complains about the .utils type which allegedly doesn't exist on type RequestInfo, it doesn't exist on type string.

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.

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

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

@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.