InMemoryWebApi parsing OData URL with key

85 Views Asked by At

For the life of me I cannot get InMemoryWebApiModule to work for me with a moderately simple custom URL parser (specifically for OData).

This is my module configuration:

InMemoryWebApiModule.forRoot(MockApiService, { apiBase: 'api/', delay: 0, passThruUnknownUrl: true }),

This is my service:

import { InMemoryDbService, RequestInfoUtilities, ParsedRequestUrl } from 'angular-in-memory-web-api';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class MockApiService implements InMemoryDbService {
  constructor() {
  }

  public createDb() {
    const stuff = [
      { id: 1, name: 'Windstorm' },
      { id: 2, name: 'Bombasto' },
      { id: 3, name: 'Magneta' },
      { id: 4, name: 'Tornado' }
    ];
    return { stuff };
  }

  public parseRequestUrl(url: string, requestInfoUtils: RequestInfoUtilities): ParsedRequestUrl {
    const isId = /^([^?]+?)\(([^?]*?)\)/.exec(url);
    if (!!isId) {
      const path = isId[1];
      const parts = path.split('/').filter(_ => !!_);
      const set = parts[parts.length - 1];
      const key = isId[2];
      console.log('Found with key');
      return requestInfoUtils.parseRequestUrl(`/api/${set}/${key}`)
      // Have tried all the below, too
      // return requestInfoUtils.parseRequestUrl(`api/${set}/${key}`)
      // return requestInfoUtils.parseRequestUrl(`/${set}/${key}`)
      // return requestInfoUtils.parseRequestUrl(`${set}/${key}`)
    }
    return undefined;
  }
}

This is my request:

api/stuff(1)

My code gets so far as logging Found with key with the correct values for set and key, but I still get a 404.

What am I doing wrong?

1

There are 1 best solutions below

0
joshcomley On

I have solved this just moments after. Rubber duck, eh.

return { apiBase: `${parts[0]}/`, collectionName: set, id: key } as ParsedRequestUrl;