aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/http/search.ts
blob: 2da9295e3815af89db4dee06877d3b93449bbbd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {
  apiBaseUrl,
  axios,
  convertToNetworkError,
  extractResponseData,
} from "./common";
import {
  HttpTimelineInfo,
  processRawTimelineInfo,
  RawHttpTimelineInfo,
} from "./timeline";
import { HttpUser } from "./user";

export interface IHttpSearchClient {
  searchTimelines(query: string): Promise<HttpTimelineInfo[]>;
  searchUsers(query: string): Promise<HttpUser[]>;
}

export class HttpSearchClient implements IHttpSearchClient {
  searchTimelines(query: string): Promise<HttpTimelineInfo[]> {
    return axios
      .get<RawHttpTimelineInfo[]>(`${apiBaseUrl}/search/timelines?q=${query}`)
      .then(extractResponseData)
      .then((ts) => ts.map(processRawTimelineInfo))
      .catch(convertToNetworkError);
  }

  searchUsers(query: string): Promise<HttpUser[]> {
    return axios
      .get<HttpUser[]>(`${apiBaseUrl}/search/users?q=${query}`)
      .then(extractResponseData)
      .catch(convertToNetworkError);
  }
}

let client: IHttpSearchClient = new HttpSearchClient();

export function getHttpSearchClient(): IHttpSearchClient {
  return client;
}

export function setHttpSearchClient(
  newClient: IHttpSearchClient
): IHttpSearchClient {
  const old = client;
  client = newClient;
  return old;
}