aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/http
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-21 19:44:47 +0800
committercrupest <crupest@outlook.com>2021-01-21 19:44:47 +0800
commit91ef5098e57ca4824ae4adc95c23d3689583f6a8 (patch)
tree1652d6e9bb0da83587d455687584f6af9443b1d7 /FrontEnd/src/app/http
parent2bf83a531a15e7895854f160b727d2dd150290d0 (diff)
downloadtimeline-91ef5098e57ca4824ae4adc95c23d3689583f6a8.tar.gz
timeline-91ef5098e57ca4824ae4adc95c23d3689583f6a8.tar.bz2
timeline-91ef5098e57ca4824ae4adc95c23d3689583f6a8.zip
feat: Add search page.
Diffstat (limited to 'FrontEnd/src/app/http')
-rw-r--r--FrontEnd/src/app/http/search.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/FrontEnd/src/app/http/search.ts b/FrontEnd/src/app/http/search.ts
new file mode 100644
index 00000000..8ff1c499
--- /dev/null
+++ b/FrontEnd/src/app/http/search.ts
@@ -0,0 +1,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;
+}