import { axios, apiBaseUrl, extractResponseData, extractEtag, Page, } 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 interface HttpBookmarkVisibility { visibility: "Private" | "Register" | "Public"; } export interface HttpRegisterCode { registerCode?: string; } 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; putUserPermission( username: string, permission: UserPermission ): Promise; deleteUserPermission( username: string, permission: UserPermission ): Promise; changePassword(req: HttpChangePasswordRequest): Promise; getBookmarkVisibility(username: string): Promise; putBookmarkVisibility( username: string, req: HttpBookmarkVisibility ): Promise; getRegisterCode(username: string): Promise; renewRegisterCode(username: string): Promise; } export class HttpUserClient implements IHttpUserClient { list(): Promise> { return axios .get>(`${apiBaseUrl}/v2/users`) .then(extractResponseData); } get(username: string): Promise { return axios .get(`${apiBaseUrl}/v2/users/${username}`) .then(extractResponseData); } post(req: HttpCreateUserRequest): Promise { return axios .post(`${apiBaseUrl}/v2/users`, req) .then(extractResponseData) .then(); } patch(username: string, req: HttpUserPatchRequest): Promise { return axios .patch(`${apiBaseUrl}/v2/users/${username}`, req) .then(extractResponseData); } delete(username: string): Promise { return axios.delete(`${apiBaseUrl}/v2/users/${username}`).then(); } generateAvatarUrl(username: string): string { return `${apiBaseUrl}/v2/users/${username}/avatar`; } putAvatar(username: string, data: Blob): Promise { return axios .put(`${apiBaseUrl}/v2/users/${username}/avatar`, data, { headers: { "Content-Type": data.type, }, }) .then(extractEtag); } putUserPermission( username: string, permission: UserPermission ): Promise { return axios .put(`${apiBaseUrl}/v2/users/${username}/permissions/${permission}`) .then(); } deleteUserPermission( username: string, permission: UserPermission ): Promise { return axios .delete(`${apiBaseUrl}/v2/users/${username}/permissions/${permission}`) .then(); } changePassword(req: HttpChangePasswordRequest): Promise { return axios.post(`${apiBaseUrl}/v2/self/changepassword`, req).then(); } getBookmarkVisibility(username: string): Promise { return axios .get( `${apiBaseUrl}/v2/users/${username}/bookmarks/visibility` ) .then(extractResponseData); } putBookmarkVisibility( username: string, req: HttpBookmarkVisibility ): Promise { return axios .put(`${apiBaseUrl}/v2/users/${username}/bookmarks/visibility`, req) .then(); } getRegisterCode(username: string): Promise { return axios .get(`${apiBaseUrl}/v2/users/${username}/registercode`) .then(extractResponseData); } renewRegisterCode(username: string): Promise { return axios .post(`${apiBaseUrl}/v2/users/${username}/renewregistercode`) .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; }