diff options
author | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
commit | 05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33 (patch) | |
tree | 929e514de85eb82a5acb96ecffc6e6d2d95f878f /FrontEnd/src/app/http/user.ts | |
parent | 986c6f2e3b858d6332eba0b42acc6861cd4d0227 (diff) | |
download | timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.tar.gz timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.tar.bz2 timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.zip |
Split front and back end.
Diffstat (limited to 'FrontEnd/src/app/http/user.ts')
-rw-r--r-- | FrontEnd/src/app/http/user.ts | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/FrontEnd/src/app/http/user.ts b/FrontEnd/src/app/http/user.ts new file mode 100644 index 00000000..a0a02cce --- /dev/null +++ b/FrontEnd/src/app/http/user.ts @@ -0,0 +1,134 @@ +import axios, { AxiosError } from "axios"; + +import { + apiBaseUrl, + convertToNetworkError, + extractResponseData, + convertToIfStatusCodeIs, + convertToIfErrorCodeIs, + NotModified, + BlobWithEtag, + convertToBlobWithEtag, + convertToNotModified, +} from "./common"; + +export interface HttpUser { + uniqueId: string; + username: string; + administrator: boolean; + nickname: string; +} + +export interface HttpUserPatchRequest { + nickname?: string; +} + +export interface HttpChangePasswordRequest { + oldPassword: string; + newPassword: string; +} + +export class HttpUserNotExistError extends Error { + constructor(public innerError?: AxiosError) { + super(); + } +} + +export class HttpChangePasswordBadCredentialError extends Error { + constructor(public innerError?: AxiosError) { + super(); + } +} + +export interface IHttpUserClient { + get(username: string): Promise<HttpUser>; + patch( + username: string, + req: HttpUserPatchRequest, + token: string + ): Promise<HttpUser>; + getAvatar(username: string): Promise<BlobWithEtag>; + getAvatar( + username: string, + etag: string + ): Promise<BlobWithEtag | NotModified>; + putAvatar(username: string, data: Blob, token: string): Promise<void>; + changePassword(req: HttpChangePasswordRequest, token: string): Promise<void>; +} + +export class HttpUserClient implements IHttpUserClient { + get(username: string): Promise<HttpUser> { + return axios + .get<HttpUser>(`${apiBaseUrl}/users/${username}`) + .then(extractResponseData) + .catch(convertToIfStatusCodeIs(404, HttpUserNotExistError)) + .catch(convertToNetworkError); + } + + patch( + username: string, + req: HttpUserPatchRequest, + token: string + ): Promise<HttpUser> { + return axios + .patch<HttpUser>(`${apiBaseUrl}/users/${username}?token=${token}`, req) + .then(extractResponseData) + .catch(convertToNetworkError); + } + + getAvatar(username: string): Promise<BlobWithEtag>; + getAvatar( + username: string, + etag?: string + ): Promise<BlobWithEtag | NotModified> { + const headers = + etag != null + ? { + "If-None-Match": etag, + } + : undefined; + + return axios + .get(`${apiBaseUrl}/users/${username}/avatar`, { + responseType: "blob", + headers, + }) + .then(convertToBlobWithEtag) + .catch(convertToNotModified) + .catch(convertToIfStatusCodeIs(404, HttpUserNotExistError)) + .catch(convertToNetworkError); + } + + putAvatar(username: string, data: Blob, token: string): Promise<void> { + return axios + .put(`${apiBaseUrl}/users/${username}/avatar?token=${token}`, data, { + headers: { + "Content-Type": data.type, + }, + }) + .catch(convertToNetworkError) + .then(); + } + + changePassword(req: HttpChangePasswordRequest, token: string): Promise<void> { + return axios + .post(`${apiBaseUrl}/userop/changepassword?token=${token}`, req) + .catch( + convertToIfErrorCodeIs(11020201, HttpChangePasswordBadCredentialError) + ) + .catch(convertToNetworkError) + .then(); + } +} + +let client: IHttpUserClient = new HttpUserClient(); + +export function getHttpUserClient(): IHttpUserClient { + return client; +} + +export function setHttpUserClient(newClient: IHttpUserClient): IHttpUserClient { + const old = client; + client = newClient; + return old; +} |