64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import { apiWrapper, boolApiWrapper } from './index';
|
|
|
|
/** A raw admin user row as returned by the API (positional from SQL columns) */
|
|
export type AdminUserRow = [
|
|
name: string,
|
|
isAdmin: boolean,
|
|
];
|
|
|
|
/**
|
|
* Fetch all users (admin only).
|
|
*
|
|
* @param token - The auth token (must belong to an admin user)
|
|
* @returns An array of {@link AdminUserRow} on success, or `undefined` on failure
|
|
*/
|
|
export async function get(token: string): Promise<AdminUserRow[] | undefined> {
|
|
return apiWrapper<AdminUserRow[]>('/api/admin/get', { token });
|
|
}
|
|
|
|
/**
|
|
* Create a new user (admin only).
|
|
*
|
|
* @param token - The auth token (must belong to an admin user)
|
|
* @param username - The new username
|
|
* @returns A single {@link AdminUserRow} of the newly created user on success, or `undefined` on failure
|
|
*/
|
|
export async function add(token: string, username: string): Promise<AdminUserRow | undefined> {
|
|
return apiWrapper<AdminUserRow>('/api/admin/add', { token, username });
|
|
}
|
|
|
|
/**
|
|
* Update an existing user's password and/or admin status (admin only).
|
|
*
|
|
* @param token - The auth token (must belong to an admin user)
|
|
* @param username - The target user's username
|
|
* @param password - (optional) The new password (plaintext, will be hashed server-side)
|
|
* @param isAdmin - (optional) Whether the user should be an admin
|
|
* @returns `true` on success, `false` on failure (or if no changes were provided)
|
|
*/
|
|
export async function update(
|
|
token: string,
|
|
username: string,
|
|
password?: string,
|
|
isAdmin?: boolean,
|
|
): Promise<boolean> {
|
|
const data: Record<string, any> = { token, username };
|
|
if (password !== undefined) data.password = password;
|
|
if (isAdmin !== undefined) data.isAdmin = isAdmin;
|
|
|
|
if (Object.keys(data).length <= 2) return false;
|
|
|
|
return boolApiWrapper('/api/admin/update', data);
|
|
}
|
|
|
|
/**
|
|
* Delete a user (admin only).
|
|
*
|
|
* @param token - The auth token (must belong to an admin user)
|
|
* @param username - The username to delete
|
|
* @returns `true` on success, `false` on failure
|
|
*/
|
|
export async function del(token: string, username: string): Promise<boolean> {
|
|
return boolApiWrapper('/api/admin/delete', { token, username });
|
|
}
|