2

For example, I want do something like this:

{hostURL}/api/entities/14/15/16/17

which in turn would bring back all the data for the corresponding IDs. This approach doesn't work. I also tried this, which didn't work either:

{hostURL}/api/entities?id=16&id=17

The docs show how to use it with one ID (under HTTP request handling): https://github.com/angular/in-memory-web-api

Is there any way to pass in multiple IDs?

Thanks.

James Barrett
  • 2,757
  • 4
  • 25
  • 35

2 Answers2

1

Got around it. I found this GitHub file on the angular-in-memory-web-api docs: https://github.com/angular/in-memory-web-api/blob/master/src/app/hero-in-mem-data-override.service.ts and constructed the following solution:

import { getStatusText, STATUS } from 'angular-in-memory-web-api/http-status-codes';
import { InMemoryDbService, RequestInfo } from 'angular-in-memory-web-api';

  get(reqInfo: RequestInfo) {
    // Extract ids from URL
    const ids: Array<number> = reqInfo.req.urlWithParams.match(/[0-9]+/g).map(n => +(n));
    // If there's more than one ID in the URL then we call the appropriate function
    if (ids.length > 1) {
      return this.getRelationshipDetails(reqInfo, ids)
    }
  }

private getRelationshipDetails(reqInfo: RequestInfo, ids: Array<number>) {
  console.log('HTTP GET override')
  const entities = reqInfo.collection;
  return reqInfo.utils.createResponse$(() => {
    const data = entities.filter(entity => ids.indexOf(entity.id) !== -1)
    const dataEncapsulation = reqInfo.utils.getConfig().dataEncapsulation;
    const options: any = data ?
      {
        body: dataEncapsulation ? { data } : data,
        status: STATUS.OK
      } :
      {
        body: { error: `Entities with ids='${ids}' not found` },
        status: STATUS.NOT_FOUND
      };
    return this.finishOptions(options, reqInfo)
  });
}

private finishOptions(options: any, { headers, url }: RequestInfo) {
  options.statusText = getStatusText(options.status);
  options.headers = headers;
  options.url = url;
  return options;
}
James Barrett
  • 2,757
  • 4
  • 25
  • 35
0

Try to pass an array as Ids

let IDs = [16, 17];

{hostURL}/api/entities?ids={IDs}

Maybe this will work for you.

Dhaval
  • 868
  • 12
  • 22