diff options
author | crupest <crupest@outlook.com> | 2020-12-19 21:17:05 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-12-19 21:17:05 +0800 |
commit | 0751d07c55dd26a8bc825e695bfa6c6442857b0b (patch) | |
tree | cfb2e847d6f3c95b0a297777b12d0214dc29ada1 /FrontEnd/src | |
parent | 328ec15a1d4d927c53c4b9da4219d58ca12a2f20 (diff) | |
download | timeline-0751d07c55dd26a8bc825e695bfa6c6442857b0b.tar.gz timeline-0751d07c55dd26a8bc825e695bfa6c6442857b0b.tar.bz2 timeline-0751d07c55dd26a8bc825e695bfa6c6442857b0b.zip |
feat: Add highlight timeline http client.
Diffstat (limited to 'FrontEnd/src')
-rw-r--r-- | FrontEnd/src/app/http/highlight.ts | 70 | ||||
-rw-r--r-- | FrontEnd/src/app/http/timeline.ts | 14 |
2 files changed, 78 insertions, 6 deletions
diff --git a/FrontEnd/src/app/http/highlight.ts b/FrontEnd/src/app/http/highlight.ts new file mode 100644 index 00000000..1f226c19 --- /dev/null +++ b/FrontEnd/src/app/http/highlight.ts @@ -0,0 +1,70 @@ +import axios from "axios"; + +import { + apiBaseUrl, + convertToNetworkError, + extractResponseData, +} from "./common"; + +import { + HttpTimelineInfo, + processRawTimelineInfo, + RawHttpTimelineInfo, +} from "./timeline"; + +export interface HttpHighlightMoveRequest { + timeline: string; + newPosition: number; +} + +export interface IHttpHighlightClient { + list(): Promise<HttpTimelineInfo[]>; + put(timeline: string, token: string): Promise<void>; + delete(timeline: string, token: string): Promise<void>; + move(req: HttpHighlightMoveRequest, token: string): Promise<void>; +} + +export class HttpHighlightClient implements IHttpHighlightClient { + list(): Promise<HttpTimelineInfo[]> { + return axios + .get<RawHttpTimelineInfo[]>(`${apiBaseUrl}/highlights`) + .then(extractResponseData) + .then((list) => list.map(processRawTimelineInfo)) + .catch(convertToNetworkError); + } + + put(timeline: string, token: string): Promise<void> { + return axios + .put(`${apiBaseUrl}/highlights/${timeline}?token=${token}`) + .catch(convertToNetworkError) + .then(); + } + + delete(timeline: string, token: string): Promise<void> { + return axios + .delete(`${apiBaseUrl}/highlights/${timeline}?token=${token}`) + .catch(convertToNetworkError) + .then(); + } + + move(req: HttpHighlightMoveRequest, token: string): Promise<void> { + return axios + .post(`${apiBaseUrl}/highlightop/move?token=${token}`, req) + .catch(convertToNetworkError) + .then(); + } +} + +let client: IHttpHighlightClient = new HttpHighlightClient(); + +export function getHttpHighlightClient(): IHttpHighlightClient { + return client; +} + +export function setHttpHighlightClient( + newClient: IHttpHighlightClient +): IHttpHighlightClient { + const old = client; + client = newClient; + return old; +} diff --git a/FrontEnd/src/app/http/timeline.ts b/FrontEnd/src/app/http/timeline.ts index 71c49852..6be0a183 100644 --- a/FrontEnd/src/app/http/timeline.ts +++ b/FrontEnd/src/app/http/timeline.ts @@ -121,7 +121,7 @@ export class HttpTimelineNameConflictError extends Error { //-------------------- begin: internal model -------------------- -interface RawTimelineInfo { +export interface RawHttpTimelineInfo { uniqueId: string; title: string; name: string; @@ -188,7 +188,9 @@ interface RawTimelinePostPostRequest { //-------------------- end: internal model -------------------- -function processRawTimelineInfo(raw: RawTimelineInfo): HttpTimelineInfo { +export function processRawTimelineInfo( + raw: RawHttpTimelineInfo +): HttpTimelineInfo { return { ...raw, lastModified: new Date(raw.lastModified), @@ -293,7 +295,7 @@ export interface IHttpTimelineClient { export class HttpTimelineClient implements IHttpTimelineClient { listTimeline(query: HttpTimelineListQuery): Promise<HttpTimelineInfo[]> { return axios - .get<RawTimelineInfo[]>( + .get<RawHttpTimelineInfo[]>( applyQueryParameters(`${apiBaseUrl}/timelines`, query) ) .then(extractResponseData) @@ -323,7 +325,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { } ): Promise<HttpTimelineInfo | NotModified> { return axios - .get<RawTimelineInfo>( + .get<RawHttpTimelineInfo>( applyQueryParameters(`${apiBaseUrl}/timelines/${timelineName}`, query) ) .then((res) => { @@ -342,7 +344,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { token: string ): Promise<HttpTimelineInfo> { return axios - .post<RawTimelineInfo>(`${apiBaseUrl}/timelines?token=${token}`, req) + .post<RawHttpTimelineInfo>(`${apiBaseUrl}/timelines?token=${token}`, req) .then(extractResponseData) .then(processRawTimelineInfo) .catch(convertToIfErrorCodeIs(11040101, HttpTimelineNameConflictError)) @@ -355,7 +357,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { token: string ): Promise<HttpTimelineInfo> { return axios - .patch<RawTimelineInfo>( + .patch<RawHttpTimelineInfo>( `${apiBaseUrl}/timelines/${timelineName}?token=${token}`, req ) |