0

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 Answer 1

0

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

return { apiBase: `${parts[0]}/`, collectionName: set, id: key } as ParsedRequestUrl;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.