Files
sub2api/frontend/src/api/keys.ts

115 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-12-18 13:50:39 +08:00
/**
* API Keys management endpoints
* Handles CRUD operations for user API keys
*/
import { apiClient } from './client'
import type { ApiKey, CreateApiKeyRequest, UpdateApiKeyRequest, PaginatedResponse } from '@/types'
2025-12-18 13:50:39 +08:00
/**
* List all API keys for current user
* @param page - Page number (default: 1)
* @param pageSize - Items per page (default: 10)
* @param options - Optional request options
2025-12-18 13:50:39 +08:00
* @returns Paginated list of API keys
*/
export async function list(
page: number = 1,
pageSize: number = 10,
options?: {
signal?: AbortSignal
}
): Promise<PaginatedResponse<ApiKey>> {
2025-12-18 13:50:39 +08:00
const { data } = await apiClient.get<PaginatedResponse<ApiKey>>('/keys', {
params: { page, page_size: pageSize },
signal: options?.signal
})
return data
2025-12-18 13:50:39 +08:00
}
/**
* Get API key by ID
* @param id - API key ID
* @returns API key details
*/
export async function getById(id: number): Promise<ApiKey> {
const { data } = await apiClient.get<ApiKey>(`/keys/${id}`)
return data
2025-12-18 13:50:39 +08:00
}
/**
* Create new API key
* @param name - Key name
* @param groupId - Optional group ID
* @param customKey - Optional custom key value
* @param ipWhitelist - Optional IP whitelist
* @param ipBlacklist - Optional IP blacklist
2025-12-18 13:50:39 +08:00
* @returns Created API key
*/
export async function create(
name: string,
groupId?: number | null,
customKey?: string,
ipWhitelist?: string[],
ipBlacklist?: string[]
): Promise<ApiKey> {
const payload: CreateApiKeyRequest = { name }
2025-12-18 13:50:39 +08:00
if (groupId !== undefined) {
payload.group_id = groupId
2025-12-18 13:50:39 +08:00
}
if (customKey) {
payload.custom_key = customKey
2025-12-18 13:50:39 +08:00
}
if (ipWhitelist && ipWhitelist.length > 0) {
payload.ip_whitelist = ipWhitelist
}
if (ipBlacklist && ipBlacklist.length > 0) {
payload.ip_blacklist = ipBlacklist
}
2025-12-18 13:50:39 +08:00
const { data } = await apiClient.post<ApiKey>('/keys', payload)
return data
2025-12-18 13:50:39 +08:00
}
/**
* Update API key
* @param id - API key ID
* @param updates - Fields to update
* @returns Updated API key
*/
export async function update(id: number, updates: UpdateApiKeyRequest): Promise<ApiKey> {
const { data } = await apiClient.put<ApiKey>(`/keys/${id}`, updates)
return data
2025-12-18 13:50:39 +08:00
}
/**
* Delete API key
* @param id - API key ID
* @returns Success confirmation
*/
export async function deleteKey(id: number): Promise<{ message: string }> {
const { data } = await apiClient.delete<{ message: string }>(`/keys/${id}`)
return data
2025-12-18 13:50:39 +08:00
}
/**
* Toggle API key status (active/inactive)
* @param id - API key ID
* @param status - New status
* @returns Updated API key
*/
export async function toggleStatus(id: number, status: 'active' | 'inactive'): Promise<ApiKey> {
return update(id, { status })
2025-12-18 13:50:39 +08:00
}
export const keysAPI = {
list,
getById,
create,
update,
delete: deleteKey,
toggleStatus
}
2025-12-18 13:50:39 +08:00
export default keysAPI