From a4f5080c6dc8c3fc7f76aebb13cbf54c0ed7ef15 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 27 Apr 2022 20:57:41 +0800 Subject: ... --- FrontEnd/src/http/common.ts | 16 ++++++++++++++++ FrontEnd/src/http/timeline.ts | 17 +---------------- FrontEnd/src/http/token.ts | 27 ++++++++++++++------------- FrontEnd/src/http/user.ts | 19 +------------------ FrontEnd/src/services/user.ts | 12 +++++++----- 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; getTimeline( @@ -185,14 +177,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { postTimeline(req: HttpTimelinePostRequest): Promise { return axios .post(`${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; verify(req: HttpVerifyTokenRequest): Promise; + register(req: HttpRegisterRequest): Promise; } export class HttpTokenClient implements IHttpTokenClient { create(req: HttpCreateTokenRequest): Promise { return axios .post(`${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 { @@ -58,6 +53,12 @@ export class HttpTokenClient implements IHttpTokenClient { .post(`${apiBaseUrl}/v2/token/verify`, req) .then(extractResponseData); } + + register(req: HttpRegisterRequest): Promise { + return axios + .post(`${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; get(username: string): Promise; @@ -135,16 +127,7 @@ export class HttpUserClient implements IHttpUserClient { } changePassword(req: HttpChangePasswordRequest): Promise { - 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 { 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; -- cgit v1.2.3