diff options
author | crupest <crupest@outlook.com> | 2022-04-25 19:27:44 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-04-25 19:27:44 +0800 |
commit | f2ead327344fdacdf3fb1e761b4fb8ec89330f1e (patch) | |
tree | c04e46a1517191440a72932f4314bd1f64503fab /FrontEnd/src/http | |
parent | 60ec53aa2ad9687f5d8661322fb3e1e03e291eba (diff) | |
download | timeline-f2ead327344fdacdf3fb1e761b4fb8ec89330f1e.tar.gz timeline-f2ead327344fdacdf3fb1e761b4fb8ec89330f1e.tar.bz2 timeline-f2ead327344fdacdf3fb1e761b4fb8ec89330f1e.zip |
...
Diffstat (limited to 'FrontEnd/src/http')
-rw-r--r-- | FrontEnd/src/http/bookmark.ts | 11 | ||||
-rw-r--r-- | FrontEnd/src/http/common.ts | 132 | ||||
-rw-r--r-- | FrontEnd/src/http/timeline.ts | 10 | ||||
-rw-r--r-- | FrontEnd/src/http/token.ts | 24 | ||||
-rw-r--r-- | FrontEnd/src/http/user.ts | 60 |
5 files changed, 63 insertions, 174 deletions
diff --git a/FrontEnd/src/http/bookmark.ts b/FrontEnd/src/http/bookmark.ts index 382543ff..40e121cc 100644 --- a/FrontEnd/src/http/bookmark.ts +++ b/FrontEnd/src/http/bookmark.ts @@ -1,4 +1,5 @@ -import { applyQueryParameters } from "@/utilities/url"; +import { withQuery } from "@/utilities/url"; + import { axios, apiBaseUrl, extractResponseData, Page } from "./common"; export interface TimelineBookmark { @@ -37,10 +38,10 @@ export class HttpHighlightClient implements IHttpBookmarkClient { page?: number, pageSize?: number ): Promise<Page<TimelineBookmark>> { - const url = applyQueryParameters( - `${apiBaseUrl}/v2/users/${username}/bookmarks`, - { page, pageSize } - ); + const url = withQuery(`${apiBaseUrl}/v2/users/${username}/bookmarks`, { + page, + pageSize, + }); return axios.get<Page<TimelineBookmark>>(url).then(extractResponseData); } diff --git a/FrontEnd/src/http/common.ts b/FrontEnd/src/http/common.ts index 5a8c0346..25c69012 100644 --- a/FrontEnd/src/http/common.ts +++ b/FrontEnd/src/http/common.ts @@ -1,7 +1,6 @@ import axios, { Axios, AxiosError, AxiosResponse } from "axios"; -import { Base64 } from "js-base64"; -import { identity } from "lodash"; import { BehaviorSubject, Observable } from "rxjs"; +import { identity } from "lodash"; export { axios }; @@ -14,7 +13,10 @@ export class HttpNetworkError extends Error { } export class HttpForbiddenError extends Error { - constructor(public innerError?: AxiosError) { + constructor( + public type: "unauthorized" | "forbidden", + public innerError?: AxiosError + ) { super(); } } @@ -34,23 +36,20 @@ function convertNetworkError(error: AxiosError): never { } function convertForbiddenError(error: AxiosError): never { - if ( - error.isAxiosError && - error.response != null && - (error.response.status == 401 || error.response.status == 403) - ) { - throw new HttpForbiddenError(error); + const statusCode = error.response?.status; + if (statusCode === 401 || statusCode === 403) { + throw new HttpForbiddenError( + statusCode === 401 ? "unauthorized" : "forbidden", + error + ); } else { throw error; } } function convertNotFoundError(error: AxiosError): never { - if ( - error.isAxiosError && - error.response != null && - error.response.status == 404 - ) { + const statusCode = error.response?.status; + if (statusCode === 404) { throw new HttpNotFoundError(error); } else { throw error; @@ -85,47 +84,6 @@ export function setHttpToken(token: string | null): void { export const token$: Observable<string | null> = tokenSubject.asObservable(); -export function base64(blob: Blob | string): Promise<string> { - if (typeof blob === "string") { - return Promise.resolve(Base64.encode(blob)); - } - - return new Promise<string>((resolve) => { - const reader = new FileReader(); - reader.onload = function () { - resolve((reader.result as string).replace(/^data:.*;base64,/, "")); - }; - reader.readAsDataURL(blob); - }); -} - -export function extractStatusCode(error: AxiosError): number | null { - if (error.isAxiosError) { - const code = error?.response?.status; - if (typeof code === "number") { - return code; - } - } - return null; -} - -export interface CommonErrorResponse { - code: number; - message: string; -} - -export function extractErrorCode( - error: AxiosError<CommonErrorResponse> -): number | null { - if (error.isAxiosError) { - const code = error.response?.data?.code; - if (typeof code === "number") { - return code; - } - } - return null; -} - export class NotModified {} export interface BlobWithEtag { @@ -137,67 +95,9 @@ export function extractResponseData<T>(res: AxiosResponse<T>): T { return res.data; } -export function catchIfStatusCodeIs< - TResult, - TErrorHandlerResult extends TResult | PromiseLike<TResult> | null | undefined ->( - statusCode: number, - errorHandler: (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult -): (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult { - return (error: AxiosError<CommonErrorResponse>) => { - if (extractStatusCode(error) == statusCode) { - return errorHandler(error); - } else { - throw error; - } - }; -} - -export function convertToIfStatusCodeIs<NewError>( - statusCode: number, - newErrorType: { - new (innerError: AxiosError): NewError; - } -): (error: AxiosError<CommonErrorResponse>) => never { - return catchIfStatusCodeIs(statusCode, (error) => { - throw new newErrorType(error); - }); -} - -export function catchIfErrorCodeIs< - TResult, - TErrorHandlerResult extends TResult | PromiseLike<TResult> | null | undefined ->( - errorCode: number, - errorHandler: (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult -): (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult { - return (error: AxiosError<CommonErrorResponse>) => { - if (extractErrorCode(error) == errorCode) { - return errorHandler(error); - } else { - throw error; - } - }; -} -export function convertToIfErrorCodeIs<NewError>( - errorCode: number, - newErrorType: { - new (innerError: AxiosError): NewError; - } -): (error: AxiosError<CommonErrorResponse>) => never { - return catchIfErrorCodeIs(errorCode, (error) => { - throw new newErrorType(error); - }); -} - -export function convertToNotModified( - error: AxiosError<CommonErrorResponse> -): NotModified { - if ( - error.isAxiosError && - error.response != null && - error.response.status == 304 - ) { +export function convertToNotModified(error: AxiosError): NotModified { + const statusCode = error.response?.status; + if (statusCode == 304) { return new NotModified(); } else { throw error; diff --git a/FrontEnd/src/http/timeline.ts b/FrontEnd/src/http/timeline.ts index d9e679ea..e95d52a8 100644 --- a/FrontEnd/src/http/timeline.ts +++ b/FrontEnd/src/http/timeline.ts @@ -1,6 +1,6 @@ import { AxiosError } from "axios"; -import { applyQueryParameters } from "../utilities/url"; +import { withQuery } from "@/utilities/url"; import { axios, @@ -168,9 +168,7 @@ export interface IHttpTimelineClient { export class HttpTimelineClient implements IHttpTimelineClient { listTimeline(query: HttpTimelineListQuery): Promise<HttpTimelineInfo[]> { return axios - .get<HttpTimelineInfo[]>( - applyQueryParameters(`${apiBaseUrl}/timelines`, query) - ) + .get<HttpTimelineInfo[]>(withQuery(`${apiBaseUrl}/timelines`, query)) .then(extractResponseData); } @@ -243,7 +241,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { ): Promise<Page<HttpTimelineGenericPostInfo>> { return axios .get<Page<HttpTimelineGenericPostInfo>>( - applyQueryParameters( + withQuery( `${apiBaseUrl}/v2/timelines/${ownerUsername}/${timelineName}/posts`, { page, @@ -259,7 +257,7 @@ export class HttpTimelineClient implements IHttpTimelineClient { timelineName: string, postId: number ): string { - return applyQueryParameters( + return withQuery( `${apiBaseUrl}/v2/timelines/${ownerUsername}/${timelineName}/posts/${postId}/data`, { token: getHttpToken() } ); diff --git a/FrontEnd/src/http/token.ts b/FrontEnd/src/http/token.ts index 3de42d21..2eba9566 100644 --- a/FrontEnd/src/http/token.ts +++ b/FrontEnd/src/http/token.ts @@ -2,12 +2,8 @@ // authorization header, which shouldn't be used in token apis. import originalAxios, { AxiosError } from "axios"; -import { - apiBaseUrl, - convertToIfErrorCodeIs, - extractResponseData, - configureAxios, -} from "./common"; +import { apiBaseUrl, extractResponseData, configureAxios } from "./common"; + import { HttpUser } from "./user"; const axios = originalAxios.create(); @@ -46,16 +42,20 @@ export interface IHttpTokenClient { export class HttpTokenClient implements IHttpTokenClient { create(req: HttpCreateTokenRequest): Promise<HttpCreateTokenResponse> { return axios - .post<HttpCreateTokenResponse>(`${apiBaseUrl}/token/create`, req, {}) - .then(extractResponseData) - .catch( - convertToIfErrorCodeIs(11010101, HttpCreateTokenBadCredentialError) - ); + .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; + } + }); } verify(req: HttpVerifyTokenRequest): Promise<HttpVerifyTokenResponse> { return axios - .post<HttpVerifyTokenResponse>(`${apiBaseUrl}/token/verify`, req) + .post<HttpVerifyTokenResponse>(`${apiBaseUrl}/v2/token/verify`, req) .then(extractResponseData); } } diff --git a/FrontEnd/src/http/user.ts b/FrontEnd/src/http/user.ts index dcf24cba..7313e484 100644 --- a/FrontEnd/src/http/user.ts +++ b/FrontEnd/src/http/user.ts @@ -1,13 +1,6 @@ import { AxiosError } from "axios"; -import { - axios, - apiBaseUrl, - extractResponseData, - convertToIfStatusCodeIs, - convertToIfErrorCodeIs, - extractEtag, -} from "./common"; +import { axios, apiBaseUrl, extractResponseData, extractEtag } from "./common"; export const kUserManagement = "UserManagement"; export const kAllTimelineManagement = "AllTimelineManagement"; @@ -44,12 +37,6 @@ export interface HttpCreateUserRequest { password: string; } -export class HttpUserNotExistError extends Error { - constructor(public innerError?: AxiosError) { - super(); - } -} - export class HttpChangePasswordBadCredentialError extends Error { constructor(public innerError?: AxiosError) { super(); @@ -64,7 +51,6 @@ export interface IHttpUserClient { delete(username: string): Promise<void>; generateAvatarUrl(username: string): string; putAvatar(username: string, data: Blob): Promise<string>; - changePassword(req: HttpChangePasswordRequest): Promise<void>; putUserPermission( username: string, permission: UserPermission @@ -73,46 +59,46 @@ export interface IHttpUserClient { username: string, permission: UserPermission ): Promise<void>; + changePassword(req: HttpChangePasswordRequest): Promise<void>; } export class HttpUserClient implements IHttpUserClient { list(): Promise<HttpUser[]> { return axios - .get<HttpUser[]>(`${apiBaseUrl}/users`) + .get<HttpUser[]>(`${apiBaseUrl}/v2/users`) .then(extractResponseData); } get(username: string): Promise<HttpUser> { return axios - .get<HttpUser>(`${apiBaseUrl}/users/${username}`) - .then(extractResponseData) - .catch(convertToIfStatusCodeIs(404, HttpUserNotExistError)); + .get<HttpUser>(`${apiBaseUrl}/v2/users/${username}`) + .then(extractResponseData); } post(req: HttpCreateUserRequest): Promise<HttpUser> { return axios - .post<HttpUser>(`${apiBaseUrl}/users`, req) + .post<HttpUser>(`${apiBaseUrl}/v2/users`, req) .then(extractResponseData) .then(); } patch(username: string, req: HttpUserPatchRequest): Promise<HttpUser> { return axios - .patch<HttpUser>(`${apiBaseUrl}/users/${username}`, req) + .patch<HttpUser>(`${apiBaseUrl}/v2/users/${username}`, req) .then(extractResponseData); } delete(username: string): Promise<void> { - return axios.delete(`${apiBaseUrl}/users/${username}`).then(); + return axios.delete(`${apiBaseUrl}/v2/users/${username}`).then(); } generateAvatarUrl(username: string): string { - return `${apiBaseUrl}/users/${username}/avatar`; + return `${apiBaseUrl}/v2/users/${username}/avatar`; } putAvatar(username: string, data: Blob): Promise<string> { return axios - .put(`${apiBaseUrl}/users/${username}/avatar`, data, { + .put(`${apiBaseUrl}/v2/users/${username}/avatar`, data, { headers: { "Content-Type": data.type, }, @@ -120,21 +106,12 @@ export class HttpUserClient implements IHttpUserClient { .then(extractEtag); } - changePassword(req: HttpChangePasswordRequest): Promise<void> { - return axios - .post(`${apiBaseUrl}/userop/changepassword`, req) - .catch( - convertToIfErrorCodeIs(11020201, HttpChangePasswordBadCredentialError) - ) - .then(); - } - putUserPermission( username: string, permission: UserPermission ): Promise<void> { return axios - .put(`${apiBaseUrl}/users/${username}/permissions/${permission}`) + .put(`${apiBaseUrl}/v2/users/${username}/permissions/${permission}`) .then(); } @@ -143,9 +120,22 @@ export class HttpUserClient implements IHttpUserClient { permission: UserPermission ): Promise<void> { return axios - .delete(`${apiBaseUrl}/users/${username}/permissions/${permission}`) + .delete(`${apiBaseUrl}/v2/users/${username}/permissions/${permission}`) .then(); } + + 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; + } + }); + } } let client: IHttpUserClient = new HttpUserClient(); |