diff options
author | crupest <crupest@outlook.com> | 2021-06-15 14:14:28 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-15 14:14:28 +0800 |
commit | 47587812b809fee2a95c76266d9d0e42fc4ac1ca (patch) | |
tree | bfaa7320c838e21edf88b5a037263f89a8012222 /FrontEnd/src/http/user.ts | |
parent | da26373c7fc13cded47428b27638b349b0432437 (diff) | |
download | timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.tar.gz timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.tar.bz2 timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.zip |
...
Diffstat (limited to 'FrontEnd/src/http/user.ts')
-rw-r--r-- | FrontEnd/src/http/user.ts | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/FrontEnd/src/http/user.ts b/FrontEnd/src/http/user.ts new file mode 100644 index 00000000..dcf24cba --- /dev/null +++ b/FrontEnd/src/http/user.ts @@ -0,0 +1,161 @@ +import { AxiosError } from "axios"; + +import { + axios, + apiBaseUrl, + extractResponseData, + convertToIfStatusCodeIs, + convertToIfErrorCodeIs, + extractEtag, +} from "./common"; + +export const kUserManagement = "UserManagement"; +export const kAllTimelineManagement = "AllTimelineManagement"; +export const kHighlightTimelineManagement = "HighlightTimelineManagement"; + +export const kUserPermissionList = [ + kUserManagement, + kAllTimelineManagement, + kHighlightTimelineManagement, +] as const; + +export type UserPermission = typeof kUserPermissionList[number]; + +export interface HttpUser { + uniqueId: string; + username: string; + permissions: UserPermission[]; + nickname: string; +} + +export interface HttpUserPatchRequest { + username?: string; + password?: string; + nickname?: string; +} + +export interface HttpChangePasswordRequest { + oldPassword: string; + newPassword: string; +} + +export interface HttpCreateUserRequest { + username: string; + password: string; +} + +export class HttpUserNotExistError extends Error { + constructor(public innerError?: AxiosError) { + super(); + } +} + +export class HttpChangePasswordBadCredentialError extends Error { + constructor(public innerError?: AxiosError) { + super(); + } +} + +export interface IHttpUserClient { + list(): Promise<HttpUser[]>; + get(username: string): Promise<HttpUser>; + post(req: HttpCreateUserRequest): Promise<HttpUser>; + patch(username: string, req: HttpUserPatchRequest): Promise<HttpUser>; + 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 + ): Promise<void>; + deleteUserPermission( + username: string, + permission: UserPermission + ): Promise<void>; +} + +export class HttpUserClient implements IHttpUserClient { + list(): Promise<HttpUser[]> { + return axios + .get<HttpUser[]>(`${apiBaseUrl}/users`) + .then(extractResponseData); + } + + get(username: string): Promise<HttpUser> { + return axios + .get<HttpUser>(`${apiBaseUrl}/users/${username}`) + .then(extractResponseData) + .catch(convertToIfStatusCodeIs(404, HttpUserNotExistError)); + } + + post(req: HttpCreateUserRequest): Promise<HttpUser> { + return axios + .post<HttpUser>(`${apiBaseUrl}/users`, req) + .then(extractResponseData) + .then(); + } + + patch(username: string, req: HttpUserPatchRequest): Promise<HttpUser> { + return axios + .patch<HttpUser>(`${apiBaseUrl}/users/${username}`, req) + .then(extractResponseData); + } + + delete(username: string): Promise<void> { + return axios.delete(`${apiBaseUrl}/users/${username}`).then(); + } + + generateAvatarUrl(username: string): string { + return `${apiBaseUrl}/users/${username}/avatar`; + } + + putAvatar(username: string, data: Blob): Promise<string> { + return axios + .put(`${apiBaseUrl}/users/${username}/avatar`, data, { + headers: { + "Content-Type": data.type, + }, + }) + .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}`) + .then(); + } + + deleteUserPermission( + username: string, + permission: UserPermission + ): Promise<void> { + return axios + .delete(`${apiBaseUrl}/users/${username}/permissions/${permission}`) + .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; +} |