feat: improve api

This commit is contained in:
2026-06-08 10:45:35 +08:00
parent c4b68400b3
commit ebe7ea5f56
7 changed files with 398 additions and 358 deletions
+38 -50
View File
@@ -1,75 +1,63 @@
import { apiWrapper, boolApiWrapper } from './index'
import { apiWrapper, boolApiWrapper } from './index';
// User interface
interface User {
username: string
isAdmin: boolean
// Add other user-related fields as needed
}
/** A raw admin user row as returned by the API (positional from SQL columns) */
export type AdminUserRow = [
name: string,
isAdmin: boolean,
];
/**
* Get all users (admin only)
* @param token - Authentication token
* @returns Array of users or null if operation failed
* 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<User[] | undefined> {
return apiWrapper('/api/admin/get', { token });
export async function get(token: string): Promise<AdminUserRow[] | undefined> {
return apiWrapper<AdminUserRow[]>('/api/admin/get', { token });
}
/**
* Add new user (admin only)
* @param token - Authentication token
* @param username - Username
* @returns Created user or null if operation failed
* 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<User | undefined> {
return apiWrapper('/api/admin/add', { token, username });
export async function add(token: string, username: string): Promise<AdminUserRow | undefined> {
return apiWrapper<AdminUserRow>('/api/admin/add', { token, username });
}
/**
* Update user (admin only)
* @param token - Authentication token
* @param username - Username
* @param params - Update parameters (partial)
* @returns true if update successful, false otherwise
* 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,
params: {
password?: string
isAdmin?: boolean
}
password?: string,
isAdmin?: boolean,
): Promise<boolean> {
const data: any = {
token,
username
};
const data: Record<string, any> = { token, username };
if (password !== undefined) data.password = password;
if (isAdmin !== undefined) data.isAdmin = isAdmin;
let count = 0;
if (typeof params.password !== 'undefined') {
data.password = params.password;
count++;
}
if (typeof params.isAdmin !== 'undefined') {
data.isAdmin = params.isAdmin;
count++;
}
// If no update parameters provided, return true
if (count === 0) {
return true;
}
if (Object.keys(data).length <= 2) return false;
return boolApiWrapper('/api/admin/update', data);
}
/**
* Delete user (admin only)
* @param token - Authentication token
* @param username - Username
* @returns true if deletion successful, false otherwise
* 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 deleteItem(token: string, username: string): Promise<boolean> {
export async function del(token: string, username: string): Promise<boolean> {
return boolApiWrapper('/api/admin/delete', { token, username });
}