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; get(username: string): Promise; post(req: HttpCreateUserRequest): Promise; patch(username: string, req: HttpUserPatchRequest): Promise; delete(username: string): Promise; generateAvatarUrl(username: string): string; putAvatar(username: string, data: Blob): Promise; changePassword(req: HttpChangePasswordRequest): Promise; putUserPermission( username: string, permission: UserPermission ): Promise; deleteUserPermission( username: string, permission: UserPermission ): Promise; } export class HttpUserClient implements IHttpUserClient { list(): Promise { return axios .get(`${apiBaseUrl}/users`) .then(extractResponseData); } get(username: string): Promise { return axios .get(`${apiBaseUrl}/users/${username}`) .then(extractResponseData) .catch(convertToIfStatusCodeIs(404, HttpUserNotExistError)); } post(req: HttpCreateUserRequest): Promise { return axios .post(`${apiBaseUrl}/users`, req) .then(extractResponseData) .then(); } patch(username: string, req: HttpUserPatchRequest): Promise { return axios .patch(`${apiBaseUrl}/users/${username}`, req) .then(extractResponseData); } delete(username: string): Promise { return axios.delete(`${apiBaseUrl}/users/${username}`).then(); } generateAvatarUrl(username: string): string { return `${apiBaseUrl}/users/${username}/avatar`; } putAvatar(username: string, data: Blob): Promise { return axios .put(`${apiBaseUrl}/users/${username}/avatar`, data, { headers: { "Content-Type": data.type, }, }) .then(extractEtag); } changePassword(req: HttpChangePasswordRequest): Promise { return axios .post(`${apiBaseUrl}/userop/changepassword`, req) .catch( convertToIfErrorCodeIs(11020201, HttpChangePasswordBadCredentialError) ) .then(); } putUserPermission( username: string, permission: UserPermission ): Promise { return axios .put(`${apiBaseUrl}/users/${username}/permissions/${permission}`) .then(); } deleteUserPermission( username: string, permission: UserPermission ): Promise { 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; }