aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-04-27 20:57:41 +0800
committercrupest <crupest@outlook.com>2022-04-27 20:57:41 +0800
commita4f5080c6dc8c3fc7f76aebb13cbf54c0ed7ef15 (patch)
tree067f053e57260dd3f876590dcbc7802721eaac50
parent6c10782e909427a8e6ddd56d1d081d19346b9688 (diff)
downloadtimeline-a4f5080c6dc8c3fc7f76aebb13cbf54c0ed7ef15.tar.gz
timeline-a4f5080c6dc8c3fc7f76aebb13cbf54c0ed7ef15.tar.bz2
timeline-a4f5080c6dc8c3fc7f76aebb13cbf54c0ed7ef15.zip
...
-rw-r--r--FrontEnd/src/http/common.ts16
-rw-r--r--FrontEnd/src/http/timeline.ts17
-rw-r--r--FrontEnd/src/http/token.ts27
-rw-r--r--FrontEnd/src/http/user.ts19
-rw-r--r--FrontEnd/src/services/user.ts12
5 files changed, 39 insertions, 52 deletions
diff --git a/FrontEnd/src/http/common.ts b/FrontEnd/src/http/common.ts
index 25c69012..b7d579c7 100644
--- a/FrontEnd/src/http/common.ts
+++ b/FrontEnd/src/http/common.ts
@@ -27,6 +27,12 @@ export class HttpNotFoundError extends Error {
}
}
+export class HttpBadRequestError extends Error {
+ constructor(public innerError?: AxiosError) {
+ super();
+ }
+}
+
function convertNetworkError(error: AxiosError): never {
if (error.isAxiosError && error.response == null) {
throw new HttpNetworkError(error);
@@ -56,10 +62,20 @@ function convertNotFoundError(error: AxiosError): never {
}
}
+function convertBadRequestError(error: AxiosError): never {
+ const statusCode = error.response?.status;
+ if (statusCode === 422) {
+ throw new HttpBadRequestError(error);
+ } else {
+ throw error;
+ }
+}
+
export function configureAxios(axios: Axios): void {
axios.interceptors.response.use(identity, convertNetworkError);
axios.interceptors.response.use(identity, convertForbiddenError);
axios.interceptors.response.use(identity, convertNotFoundError);
+ axios.interceptors.response.use(identity, convertBadRequestError);
}
configureAxios(axios);
diff --git a/FrontEnd/src/http/timeline.ts b/FrontEnd/src/http/timeline.ts
index 37a9ebc9..f9f2a6c7 100644
--- a/FrontEnd/src/http/timeline.ts
+++ b/FrontEnd/src/http/timeline.ts
@@ -1,5 +1,3 @@
-import { AxiosError } from "axios";
-
import { withQuery } from "@/utilities/url";
import {
@@ -101,12 +99,6 @@ export interface HttpTimelinePostPatchRequest {
color?: string;
}
-export class HttpTimelineNameConflictError extends Error {
- constructor(public innerError?: AxiosError) {
- super();
- }
-}
-
export interface IHttpTimelineClient {
listTimeline(query: HttpTimelineListQuery): Promise<HttpTimelineInfo[]>;
getTimeline(
@@ -185,14 +177,7 @@ export class HttpTimelineClient implements IHttpTimelineClient {
postTimeline(req: HttpTimelinePostRequest): Promise<HttpTimelineInfo> {
return axios
.post<HttpTimelineInfo>(`${apiBaseUrl}/v2/timelines`, req)
- .then(extractResponseData, (error: AxiosError) => {
- const statusCode = error.response?.status;
- if (statusCode === 422) {
- throw new HttpTimelineNameConflictError(error);
- } else {
- throw error;
- }
- });
+ .then(extractResponseData);
}
patchTimeline(
diff --git a/FrontEnd/src/http/token.ts b/FrontEnd/src/http/token.ts
index 2eba9566..7cb3d9c3 100644
--- a/FrontEnd/src/http/token.ts
+++ b/FrontEnd/src/http/token.ts
@@ -1,6 +1,6 @@
// Don't use axios in common because it will contains
// authorization header, which shouldn't be used in token apis.
-import originalAxios, { AxiosError } from "axios";
+import originalAxios from "axios";
import { apiBaseUrl, extractResponseData, configureAxios } from "./common";
@@ -28,29 +28,24 @@ export interface HttpVerifyTokenResponse {
user: HttpUser;
}
-export class HttpCreateTokenBadCredentialError extends Error {
- constructor(public innerError?: AxiosError) {
- super();
- }
+export interface HttpRegisterRequest {
+ username: string;
+ password: string;
+ nickname?: string;
+ registerCode: string;
}
export interface IHttpTokenClient {
create(req: HttpCreateTokenRequest): Promise<HttpCreateTokenResponse>;
verify(req: HttpVerifyTokenRequest): Promise<HttpVerifyTokenResponse>;
+ register(req: HttpRegisterRequest): Promise<HttpUser>;
}
export class HttpTokenClient implements IHttpTokenClient {
create(req: HttpCreateTokenRequest): Promise<HttpCreateTokenResponse> {
return axios
.post<HttpCreateTokenResponse>(`${apiBaseUrl}/v2/token/create`, req, {})
- .then(extractResponseData, (error: AxiosError) => {
- const statusCode = error.response?.status;
- if (statusCode === 422) {
- throw new HttpCreateTokenBadCredentialError(error);
- } else {
- throw error;
- }
- });
+ .then(extractResponseData);
}
verify(req: HttpVerifyTokenRequest): Promise<HttpVerifyTokenResponse> {
@@ -58,6 +53,12 @@ export class HttpTokenClient implements IHttpTokenClient {
.post<HttpVerifyTokenResponse>(`${apiBaseUrl}/v2/token/verify`, req)
.then(extractResponseData);
}
+
+ register(req: HttpRegisterRequest): Promise<HttpUser> {
+ return axios
+ .post<HttpUser>(`${apiBaseUrl}/v2/register`, req)
+ .then(extractResponseData);
+ }
}
let client: IHttpTokenClient = new HttpTokenClient();
diff --git a/FrontEnd/src/http/user.ts b/FrontEnd/src/http/user.ts
index 17c4adbb..c62f852c 100644
--- a/FrontEnd/src/http/user.ts
+++ b/FrontEnd/src/http/user.ts
@@ -1,5 +1,3 @@
-import { AxiosError } from "axios";
-
import { axios, apiBaseUrl, extractResponseData, extractEtag } from "./common";
export const kUserManagement = "UserManagement";
@@ -41,12 +39,6 @@ export interface HttpBookmarkVisibility {
visibility: "Private" | "Register" | "Public";
}
-export class HttpChangePasswordBadCredentialError extends Error {
- constructor(public innerError?: AxiosError) {
- super();
- }
-}
-
export interface IHttpUserClient {
list(): Promise<HttpUser[]>;
get(username: string): Promise<HttpUser>;
@@ -135,16 +127,7 @@ export class HttpUserClient implements IHttpUserClient {
}
changePassword(req: HttpChangePasswordRequest): Promise<void> {
- return axios
- .post(`${apiBaseUrl}/v2/self/changepassword`, req)
- .then(undefined, (error: AxiosError) => {
- const statusCode = error.response?.status;
- if (statusCode === 422) {
- throw new HttpChangePasswordBadCredentialError(error);
- } else {
- throw error;
- }
- });
+ return axios.post(`${apiBaseUrl}/v2/self/changepassword`, req).then();
}
getBookmarkVisibility(username: string): Promise<HttpBookmarkVisibility> {
diff --git a/FrontEnd/src/services/user.ts b/FrontEnd/src/services/user.ts
index bc33d113..2d6b6e33 100644
--- a/FrontEnd/src/services/user.ts
+++ b/FrontEnd/src/services/user.ts
@@ -4,11 +4,13 @@ import { AxiosError } from "axios";
import { UiLogicError } from "@/common";
-import { HttpNetworkError, setHttpToken, axios } from "@/http/common";
import {
- getHttpTokenClient,
- HttpCreateTokenBadCredentialError,
-} from "@/http/token";
+ HttpNetworkError,
+ setHttpToken,
+ axios,
+ HttpBadRequestError,
+} from "@/http/common";
+import { getHttpTokenClient } from "@/http/token";
import { getHttpUserClient, HttpUser, UserPermission } from "@/http/user";
import { pushAlert } from "./alert";
@@ -137,7 +139,7 @@ export class UserService {
}
this.userSubject.next(user);
} catch (e) {
- if (e instanceof HttpCreateTokenBadCredentialError) {
+ if (e instanceof HttpBadRequestError) {
throw new BadCredentialError();
} else {
throw e;