From 8edf6d566cecd94d251a4e29ae8c35b77f88d6db Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 9 Jan 2021 01:07:35 +0800 Subject: ... --- FrontEnd/src/app/http/timeline.ts | 122 ++++++++++---------------------------- 1 file changed, 32 insertions(+), 90 deletions(-) (limited to 'FrontEnd/src/app/http/timeline.ts') diff --git a/FrontEnd/src/app/http/timeline.ts b/FrontEnd/src/app/http/timeline.ts index 6be0a183..ed02a65b 100644 --- a/FrontEnd/src/app/http/timeline.ts +++ b/FrontEnd/src/app/http/timeline.ts @@ -1,8 +1,9 @@ -import axios, { AxiosError } from "axios"; +import { AxiosError } from "axios"; import { updateQueryString, applyQueryParameters } from "../utilities/url"; import { + axios, apiBaseUrl, extractResponseData, convertToNetworkError, @@ -30,6 +31,8 @@ export interface HttpTimelineInfo { visibility: TimelineVisibility; lastModified: Date; members: HttpUser[]; + isHighlight: boolean; + isBookmark: boolean; } export interface HttpTimelineListQuery { @@ -130,6 +133,8 @@ export interface RawHttpTimelineInfo { visibility: TimelineVisibility; lastModified: string; members: HttpUser[]; + isHighlight: boolean; + isBookmark: boolean; } interface RawTimelinePostTextContent { @@ -229,33 +234,17 @@ export interface IHttpTimelineClient { ifModifiedSince: Date; } ): Promise; - postTimeline( - req: HttpTimelinePostRequest, - token: string - ): Promise; + postTimeline(req: HttpTimelinePostRequest): Promise; patchTimeline( timelineName: string, - req: HttpTimelinePatchRequest, - token: string + req: HttpTimelinePatchRequest ): Promise; - deleteTimeline(timelineName: string, token: string): Promise; - memberPut( - timelineName: string, - username: string, - token: string - ): Promise; - memberDelete( - timelineName: string, - username: string, - token: string - ): Promise; + deleteTimeline(timelineName: string): Promise; + memberPut(timelineName: string, username: string): Promise; + memberDelete(timelineName: string, username: string): Promise; + listPost(timelineName: string): Promise; listPost( timelineName: string, - token?: string - ): Promise; - listPost( - timelineName: string, - token: string | undefined, query: { modifiedSince?: Date; includeDeleted?: false; @@ -263,33 +252,22 @@ export interface IHttpTimelineClient { ): Promise; listPost( timelineName: string, - token: string | undefined, query: { modifiedSince?: Date; includeDeleted: true; } ): Promise; + getPostData(timelineName: string, postId: number): Promise; getPostData( timelineName: string, postId: number, - token?: string - ): Promise; - getPostData( - timelineName: string, - postId: number, - token: string | undefined, etag: string ): Promise; postPost( timelineName: string, - req: HttpTimelinePostPostRequest, - token: string + req: HttpTimelinePostPostRequest ): Promise; - deletePost( - timelineName: string, - postId: number, - token: string - ): Promise; + deletePost(timelineName: string, postId: number): Promise; } export class HttpTimelineClient implements IHttpTimelineClient { @@ -339,12 +317,9 @@ export class HttpTimelineClient implements IHttpTimelineClient { .catch(convertToNetworkError); } - postTimeline( - req: HttpTimelinePostRequest, - token: string - ): Promise { + postTimeline(req: HttpTimelinePostRequest): Promise { return axios - .post(`${apiBaseUrl}/timelines?token=${token}`, req) + .post(`${apiBaseUrl}/timelines`, req) .then(extractResponseData) .then(processRawTimelineInfo) .catch(convertToIfErrorCodeIs(11040101, HttpTimelineNameConflictError)) @@ -353,12 +328,11 @@ export class HttpTimelineClient implements IHttpTimelineClient { patchTimeline( timelineName: string, - req: HttpTimelinePatchRequest, - token: string + req: HttpTimelinePatchRequest ): Promise { return axios .patch( - `${apiBaseUrl}/timelines/${timelineName}?token=${token}`, + `${apiBaseUrl}/timelines/${timelineName}`, req ) .then(extractResponseData) @@ -366,46 +340,30 @@ export class HttpTimelineClient implements IHttpTimelineClient { .catch(convertToNetworkError); } - deleteTimeline(timelineName: string, token: string): Promise { + deleteTimeline(timelineName: string): Promise { return axios - .delete(`${apiBaseUrl}/timelines/${timelineName}?token=${token}`) + .delete(`${apiBaseUrl}/timelines/${timelineName}`) .catch(convertToNetworkError) .then(); } - memberPut( - timelineName: string, - username: string, - token: string - ): Promise { + memberPut(timelineName: string, username: string): Promise { return axios - .put( - `${apiBaseUrl}/timelines/${timelineName}/members/${username}?token=${token}` - ) + .put(`${apiBaseUrl}/timelines/${timelineName}/members/${username}`) .catch(convertToNetworkError) .then(); } - memberDelete( - timelineName: string, - username: string, - token: string - ): Promise { + memberDelete(timelineName: string, username: string): Promise { return axios - .delete( - `${apiBaseUrl}/timelines/${timelineName}/members/${username}?token=${token}` - ) + .delete(`${apiBaseUrl}/timelines/${timelineName}/members/${username}`) .catch(convertToNetworkError) .then(); } + listPost(timelineName: string): Promise; listPost( timelineName: string, - token?: string - ): Promise; - listPost( - timelineName: string, - token: string | undefined, query: { modifiedSince?: Date; includeDeleted?: false; @@ -413,7 +371,6 @@ export class HttpTimelineClient implements IHttpTimelineClient { ): Promise; listPost( timelineName: string, - token: string | undefined, query: { modifiedSince?: Date; includeDeleted: true; @@ -421,14 +378,12 @@ export class HttpTimelineClient implements IHttpTimelineClient { ): Promise; listPost( timelineName: string, - token?: string, query?: { modifiedSince?: Date; includeDeleted?: boolean; } ): Promise { let url = `${apiBaseUrl}/timelines/${timelineName}/posts`; - url = updateQueryString("token", token, url); if (query != null) { if (query.modifiedSince != null) { url = updateQueryString( @@ -457,15 +412,10 @@ export class HttpTimelineClient implements IHttpTimelineClient { ); } + getPostData(timelineName: string, postId: number): Promise; getPostData( timelineName: string, postId: number, - token: string - ): Promise; - getPostData( - timelineName: string, - postId: number, - token?: string, etag?: string ): Promise { const headers = @@ -475,8 +425,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { } : undefined; - let url = `${apiBaseUrl}/timelines/${timelineName}/posts/${postId}/data`; - url = updateQueryString("token", token, url); + const url = `${apiBaseUrl}/timelines/${timelineName}/posts/${postId}/data`; return axios .get(url, { @@ -491,8 +440,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { async postPost( timelineName: string, - req: HttpTimelinePostPostRequest, - token: string + req: HttpTimelinePostPostRequest ): Promise { let content: RawTimelinePostPostRequestContent; if (req.content.type === "image") { @@ -512,7 +460,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { } return await axios .post( - `${apiBaseUrl}/timelines/${timelineName}/posts?token=${token}`, + `${apiBaseUrl}/timelines/${timelineName}/posts`, rawReq ) .then(extractResponseData) @@ -520,15 +468,9 @@ export class HttpTimelineClient implements IHttpTimelineClient { .then((rawPost) => processRawTimelinePostInfo(rawPost)); } - deletePost( - timelineName: string, - postId: number, - token: string - ): Promise { + deletePost(timelineName: string, postId: number): Promise { return axios - .delete( - `${apiBaseUrl}/timelines/${timelineName}/posts/${postId}?token=${token}` - ) + .delete(`${apiBaseUrl}/timelines/${timelineName}/posts/${postId}`) .catch(convertToNetworkError) .then(); } -- cgit v1.2.3 From d3cd08d6234332a6f79bdcb080d490c38741e0e0 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 9 Jan 2021 17:19:29 +0800 Subject: ... --- FrontEnd/src/app/http/timeline.ts | 27 ++++++--------------- FrontEnd/src/app/utilities/url.ts | 50 ++++++--------------------------------- 2 files changed, 14 insertions(+), 63 deletions(-) (limited to 'FrontEnd/src/app/http/timeline.ts') diff --git a/FrontEnd/src/app/http/timeline.ts b/FrontEnd/src/app/http/timeline.ts index ed02a65b..228b6105 100644 --- a/FrontEnd/src/app/http/timeline.ts +++ b/FrontEnd/src/app/http/timeline.ts @@ -1,6 +1,6 @@ import { AxiosError } from "axios"; -import { updateQueryString, applyQueryParameters } from "../utilities/url"; +import { applyQueryParameters } from "../utilities/url"; import { axios, @@ -383,26 +383,13 @@ export class HttpTimelineClient implements IHttpTimelineClient { includeDeleted?: boolean; } ): Promise { - let url = `${apiBaseUrl}/timelines/${timelineName}/posts`; - if (query != null) { - if (query.modifiedSince != null) { - url = updateQueryString( - "modifiedSince", - query.modifiedSince.toISOString(), - url - ); - } - if (query.includeDeleted != null) { - url = updateQueryString( - "includeDeleted", - query.includeDeleted ? "true" : "false", - url - ); - } - } - return axios - .get(url) + .get( + applyQueryParameters( + `${apiBaseUrl}/timelines/${timelineName}/posts`, + query + ) + ) .then(extractResponseData) .catch(convertToIfStatusCodeIs(404, HttpTimelineNotExistError)) .catch(convertToForbiddenError) diff --git a/FrontEnd/src/app/utilities/url.ts b/FrontEnd/src/app/utilities/url.ts index 17ead5b2..21ad6304 100644 --- a/FrontEnd/src/app/utilities/url.ts +++ b/FrontEnd/src/app/utilities/url.ts @@ -1,52 +1,16 @@ -//copied from https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter -export function updateQueryString( - key: string, - value: undefined | string | null, - url: string -): string { - const re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"); - let hash; - - if (re.test(url)) { - if (typeof value !== "undefined" && value !== null) { - return url.replace(re, "$1" + key + "=" + value + "$2$3"); - } else { - hash = url.split("#"); - url = hash[0].replace(re, "$1$3").replace(/(&|\?)$/, ""); - if (typeof hash[1] !== "undefined" && hash[1] !== null) { - url += "#" + hash[1]; - } - return url; - } - } else { - if (typeof value !== "undefined" && value !== null) { - const separator = url.includes("?") ? "&" : "?"; - hash = url.split("#"); - url = hash[0] + separator + key + "=" + value; - if (typeof hash[1] !== "undefined" && hash[1] !== null) { - url += "#" + hash[1]; - } - return url; - } else { - return url; - } - } -} - export function applyQueryParameters(url: string, query: T): string { if (query == null) return url; + const params = new URLSearchParams(); + for (const [key, value] of Object.entries(query)) { - if (typeof value === "string") url = updateQueryString(key, value, url); - else if (typeof value === "number") - url = updateQueryString(key, String(value), url); - else if (typeof value === "boolean") - url = updateQueryString(key, value ? "true" : "false", url); - else if (value instanceof Date) - url = updateQueryString(key, value.toISOString(), url); + if (typeof value === "string") params.set(key, value); + else if (typeof value === "number") params.set(key, String(value)); + else if (typeof value === "boolean") params.set(key, String(value)); + else if (value instanceof Date) params.set(key, value.toISOString()); else { console.error("Unknown query parameter type. Param: ", value); } } - return url; + return url + "?" + params.toString(); } -- cgit v1.2.3