代码之家  ›  专栏  ›  技术社区  ›  joshcomley

InMemoryWebApi使用键解析OData URL

  •  0
  • joshcomley  · 技术社区  · 4 年前

    InMemoryWebApiModule

    这是我的模块配置:

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

    这是我的服务:

    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;
      }
    }
    

    api/stuff(1)
    

    我的代码可以记录 Found with key 具有正确的值 set key 但我还是得到了404。

    我做错什么了?

    0 回复  |  直到 4 年前
        1
  •  0
  •   joshcomley    4 年前

    我刚刚解决了这个问题。橡皮鸭,嗯。

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