将响应映射到模型对象

Map response to model object

我正在尝试将 GET 调用的响应映射到我的对象,该对象具有属性 1:1 和 JSON 对象。这是我的代码:

线程模型:

export class Thread {
  private source: Source;
  private target: Target;
  private messages: Message[];

  constructor(data) {
    this.source = data.source;
    this.target = data.target;
    this.messages = data.messages;
  }

  sum() {
    return 'Hello from Thread';
  }
}

线程服务:

@Injectable()
export class ThreadService {
  constructor(private http: HttpClient) {
  }
  getAll(): Observable<Thread[]> {
    return this.http.get<Thread[]>('/api/thread').map(value => new Thread(value))
  }
}

ThreadService 中的 getAll 方法给我以下错误:

src/app/service/thread.service.ts(13,5): error TS2322: Type 'Observable<Thread>' is not assignable to type 'Observable<Thread[]>'. Type 'Thread' is not assignable to type 'Thread[]'. Property 'includes' is missing in type 'Thread'.

你能告诉我将响应映射到允许我在该模型中调用自定义方法的模型的正确方法是什么吗?我读了一些关于拦截器的文章来做到这一点,但我找不到任何例子。感谢您的帮助。

@Injectable()
export class ThreadService {
  constructor(private http: HttpClient) {
  }
  getAll(): Observable<Thread[]> {
    return this.http.get<Thread[]>('/api/thread').map(threads=> threads.map(thread => new Thread(thread)))
  }
}