Angular In-Memory Web API with Embedded

76 Views Asked by At

I'm trying to use Angular In-Memory Web API. I have many API that are used as CRUD but the issue is that I have responses with embedded and pagination.

Here is an example of one of my CRUD:

readPage(externalId: string, page: number, size: number, sort: string, search: string): Observable<FooPage>;

The FooPage is declare like this:

export interface FooPage {
   embedded: {
      foos: Foo[]
   },
   _links: Link[],
   page: PageDetail
}

The readOne:

readOne(externalId: string, fooId: string): Observable<Foo>;

The create

createFoo(externalId: string, fooCreate: FooCreate): Observable<Foo>;

The update

updateFoo(externalId: string, fooId: string, fooUpdate: FooUpdate): Observable<Foo>;

Do you have any idea for using In-Memory Web API with this CRUD structure ? Are any advice about using another mock library ?

1

There are 1 best solutions below

0
pred On BEST ANSWER

I found the solution by using this method:

protected responseInterceptor(res: ResponseOptions, ri: RequestInfo): ResponseOptions;

Then in this method I just have to set the response body as follow:

res.body = {
  _embedded: {
    foos: res.body
  },
  page: {
    total_elements: foos.length
  }
} as FooPage;