76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import { apiWrapper, boolApiWrapper } from './index'
|
|||
|
|
|
||
|
|
// User interface
|
||
|
|
interface User {
|
||
|
|
username: string
|
||
|
|
isAdmin: boolean
|
||
|
|
// Add other user-related fields as needed
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all users (admin only)
|
||
|
|
* @param token - Authentication token
|
||
|
|
* @returns Array of users or null if operation failed
|
||
|
|
*/
|
||
|
|
export async function get(token: string): Promise<User[] | undefined> {
|
||
|
|
return apiWrapper('/api/admin/get', { token });
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add new user (admin only)
|
||
|
|
* @param token - Authentication token
|
||
|
|
* @param username - Username
|
||
|
|
* @returns Created user or null if operation failed
|
||
|
|
*/
|
||
|
|
export async function add(token: string, username: string): Promise<User | undefined> {
|
||
|
|
return apiWrapper('/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
|
||
|
|
*/
|
||
|
|
export async function update(
|
||
|
|
token: string,
|
||
|
|
username: string,
|
||
|
|
params: {
|
||
|
|
password?: string
|
||
|
|
isAdmin?: boolean
|
||
|
|
}
|
||
|
|
): Promise<boolean> {
|
||
|
|
const data: any = {
|
||
|
|
token,
|
||
|
|
username
|
||
|
|
};
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
return boolApiWrapper('/api/admin/update', data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete user (admin only)
|
||
|
|
* @param token - Authentication token
|
||
|
|
* @param username - Username
|
||
|
|
* @returns true if deletion successful, false otherwise
|
||
|
|
*/
|
||
|
|
export async function deleteItem(token: string, username: string): Promise<boolean> {
|
||
|
|
return boolApiWrapper('/api/admin/delete', { token, username });
|
||
|
|
}
|