aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'FrontEnd/src/http')
-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
4 files changed, 32 insertions, 47 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> {