diff options
author | crupest <crupest@outlook.com> | 2021-06-15 14:14:28 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-15 14:14:28 +0800 |
commit | 47587812b809fee2a95c76266d9d0e42fc4ac1ca (patch) | |
tree | bfaa7320c838e21edf88b5a037263f89a8012222 /FrontEnd/src/http/timeline.ts | |
parent | da26373c7fc13cded47428b27638b349b0432437 (diff) | |
download | timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.tar.gz timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.tar.bz2 timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.zip |
...
Diffstat (limited to 'FrontEnd/src/http/timeline.ts')
-rw-r--r-- | FrontEnd/src/http/timeline.ts | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/FrontEnd/src/http/timeline.ts b/FrontEnd/src/http/timeline.ts new file mode 100644 index 00000000..9697c1a0 --- /dev/null +++ b/FrontEnd/src/http/timeline.ts @@ -0,0 +1,234 @@ +import { AxiosError } from "axios"; + +import { applyQueryParameters } from "../utilities/url"; + +import { + axios, + apiBaseUrl, + extractResponseData, + convertToIfErrorCodeIs, + getHttpToken, +} from "./common"; +import { HttpUser } from "./user"; + +export const kTimelineVisibilities = ["Public", "Register", "Private"] as const; + +export type TimelineVisibility = typeof kTimelineVisibilities[number]; + +export interface HttpTimelineInfo { + uniqueId: string; + title: string; + name: string; + description: string; + owner: HttpUser; + visibility: TimelineVisibility; + color: string; + lastModified: string; + members: HttpUser[]; + isHighlight: boolean; + isBookmark: boolean; + manageable: boolean; + postable: boolean; +} + +export interface HttpTimelineListQuery { + visibility?: TimelineVisibility; + relate?: string; + relateType?: "own" | "join"; +} + +export interface HttpTimelinePostRequest { + name: string; +} + +export interface HttpTimelinePostDataDigest { + kind: string; + eTag: string; + lastUpdated: string; +} + +export interface HttpTimelinePostInfo { + id: number; + time: string; + author: HttpUser; + dataList: HttpTimelinePostDataDigest[]; + color: string; + lastUpdated: string; + timelineName: string; + editable: boolean; +} + +export interface HttpTimelinePostPostRequestData { + contentType: string; + data: string; +} + +export interface HttpTimelinePostPostRequest { + time?: string; + color?: string; + dataList: HttpTimelinePostPostRequestData[]; +} + +export interface HttpTimelinePatchRequest { + name?: string; + title?: string; + color?: string; + visibility?: TimelineVisibility; + description?: string; +} + +export interface HttpTimelinePostPatchRequest { + time?: string; + color?: string; +} + +export class HttpTimelineNameConflictError extends Error { + constructor(public innerError?: AxiosError) { + super(); + } +} + +export interface IHttpTimelineClient { + listTimeline(query: HttpTimelineListQuery): Promise<HttpTimelineInfo[]>; + getTimeline(timelineName: string): Promise<HttpTimelineInfo>; + postTimeline(req: HttpTimelinePostRequest): Promise<HttpTimelineInfo>; + patchTimeline( + timelineName: string, + req: HttpTimelinePatchRequest + ): Promise<HttpTimelineInfo>; + deleteTimeline(timelineName: string): Promise<void>; + memberPut(timelineName: string, username: string): Promise<void>; + memberDelete(timelineName: string, username: string): Promise<void>; + listPost(timelineName: string): Promise<HttpTimelinePostInfo[]>; + generatePostDataUrl(timelineName: string, postId: number): string; + getPostDataAsString(timelineName: string, postId: number): Promise<string>; + postPost( + timelineName: string, + req: HttpTimelinePostPostRequest + ): Promise<HttpTimelinePostInfo>; + patchPost( + timelineName: string, + postId: number, + req: HttpTimelinePostPatchRequest + ): Promise<HttpTimelinePostInfo>; + deletePost(timelineName: string, postId: number): Promise<void>; +} + +export class HttpTimelineClient implements IHttpTimelineClient { + listTimeline(query: HttpTimelineListQuery): Promise<HttpTimelineInfo[]> { + return axios + .get<HttpTimelineInfo[]>( + applyQueryParameters(`${apiBaseUrl}/timelines`, query) + ) + .then(extractResponseData); + } + + getTimeline(timelineName: string): Promise<HttpTimelineInfo> { + return axios + .get<HttpTimelineInfo>(`${apiBaseUrl}/timelines/${timelineName}`) + .then(extractResponseData); + } + + postTimeline(req: HttpTimelinePostRequest): Promise<HttpTimelineInfo> { + return axios + .post<HttpTimelineInfo>(`${apiBaseUrl}/timelines`, req) + .then(extractResponseData) + .catch(convertToIfErrorCodeIs(11040101, HttpTimelineNameConflictError)); + } + + patchTimeline( + timelineName: string, + req: HttpTimelinePatchRequest + ): Promise<HttpTimelineInfo> { + return axios + .patch<HttpTimelineInfo>(`${apiBaseUrl}/timelines/${timelineName}`, req) + .then(extractResponseData); + } + + deleteTimeline(timelineName: string): Promise<void> { + return axios.delete(`${apiBaseUrl}/timelines/${timelineName}`).then(); + } + + memberPut(timelineName: string, username: string): Promise<void> { + return axios + .put(`${apiBaseUrl}/timelines/${timelineName}/members/${username}`) + .then(); + } + + memberDelete(timelineName: string, username: string): Promise<void> { + return axios + .delete(`${apiBaseUrl}/timelines/${timelineName}/members/${username}`) + .then(); + } + + listPost(timelineName: string): Promise<HttpTimelinePostInfo[]> { + return axios + .get<HttpTimelinePostInfo[]>( + `${apiBaseUrl}/timelines/${timelineName}/posts` + ) + .then(extractResponseData); + } + + generatePostDataUrl(timelineName: string, postId: number): string { + return applyQueryParameters( + `${apiBaseUrl}/timelines/${timelineName}/posts/${postId}/data`, + { token: getHttpToken() } + ); + } + + getPostDataAsString(timelineName: string, postId: number): Promise<string> { + return axios + .get<string>( + `${apiBaseUrl}/timelines/${timelineName}/posts/${postId}/data`, + { + responseType: "text", + } + ) + .then(extractResponseData); + } + + postPost( + timelineName: string, + req: HttpTimelinePostPostRequest + ): Promise<HttpTimelinePostInfo> { + return axios + .post<HttpTimelinePostInfo>( + `${apiBaseUrl}/timelines/${timelineName}/posts`, + req + ) + .then(extractResponseData); + } + + patchPost( + timelineName: string, + postId: number, + req: HttpTimelinePostPatchRequest + ): Promise<HttpTimelinePostInfo> { + return axios + .patch<HttpTimelinePostInfo>( + `${apiBaseUrl}/timelines/${timelineName}/posts/${postId}`, + req + ) + .then(extractResponseData); + } + + deletePost(timelineName: string, postId: number): Promise<void> { + return axios + .delete(`${apiBaseUrl}/timelines/${timelineName}/posts/${postId}`) + .then(); + } +} + +let client: IHttpTimelineClient = new HttpTimelineClient(); + +export function getHttpTimelineClient(): IHttpTimelineClient { + return client; +} + +export function setHttpTimelineClient( + newClient: IHttpTimelineClient +): IHttpTimelineClient { + const old = client; + client = newClient; + return old; +} |