aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/http/bookmark.ts
blob: 40e121cc179bde176d389b927c4734055dcf94a1 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { withQuery } from "@/utilities/url";

import { axios, apiBaseUrl, extractResponseData, Page } from "./common";

export interface TimelineBookmark {
  timelineOwner: string;
  timelineName: string;
  position: number;
}

export interface IHttpBookmarkClient {
  list(
    username: string,
    page?: number,
    pageSize?: number
  ): Promise<Page<TimelineBookmark>>;
  post(
    username: string,
    timelineOwner: string,
    timelineName: string
  ): Promise<TimelineBookmark>;
  delete(
    username: string,
    timelineOwner: string,
    timelineName: string
  ): Promise<void>;
  move(
    username: string,
    timelineOwner: string,
    timelineName: string,
    position: number
  ): Promise<TimelineBookmark>;
}

export class HttpHighlightClient implements IHttpBookmarkClient {
  list(
    username: string,
    page?: number,
    pageSize?: number
  ): Promise<Page<TimelineBookmark>> {
    const url = withQuery(`${apiBaseUrl}/v2/users/${username}/bookmarks`, {
      page,
      pageSize,
    });

    return axios.get<Page<TimelineBookmark>>(url).then(extractResponseData);
  }

  post(
    username: string,
    timelineOwner: string,
    timelineName: string
  ): Promise<TimelineBookmark> {
    const url = `${apiBaseUrl}/v2/users/${username}/bookmarks`;

    return axios
      .post<TimelineBookmark>(url, {
        timelineOwner,
        timelineName,
      })
      .then(extractResponseData);
  }

  delete(
    username: string,
    timelineOwner: string,
    timelineName: string
  ): Promise<void> {
    const url = `${apiBaseUrl}/v2/users/${username}/bookmarks/delete`;

    return axios.post(url, {
      timelineOwner,
      timelineName,
    });
  }

  move(
    username: string,
    timelineOwner: string,
    timelineName: string,
    position: number
  ): Promise<TimelineBookmark> {
    const url = `${apiBaseUrl}/v2/users/${username}/bookmarks/move`;

    return axios
      .post<TimelineBookmark>(url, {
        timelineOwner,
        timelineName,
        position,
      })
      .then(extractResponseData);
  }
}

let client: IHttpBookmarkClient = new HttpHighlightClient();

export function getHttpBookmarkClient(): IHttpBookmarkClient {
  return client;
}

export function setHttpBookmarkClient(
  newClient: IHttpBookmarkClient
): IHttpBookmarkClient {
  const old = client;
  client = newClient;
  return old;
}