* Scaffold API docs * fixup! Scaffold API docs * Add versioning * Scaffold API Access * fixup! Scaffold API Access * fixup! fixup! Scaffold API Access * Remove DEBUG logs, add tests * fixup! Remove DEBUG logs, add tests * Add rate limiter * Only enable swagger in development * Update docs * fixup! Update docs * fixup! fixup! Update docs * Fix issue with markdown table headers
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
export interface ApiKeySummary {
|
|
id: number;
|
|
name: string;
|
|
token_prefix: string;
|
|
created_at: string;
|
|
updated_at: string;
|
|
last_used_at: string | null;
|
|
expires_at: string | null;
|
|
revoked_at: string | null;
|
|
}
|
|
|
|
export interface CreateApiKeyResponse {
|
|
token: string;
|
|
apiKey: ApiKeySummary;
|
|
}
|
|
|
|
async function handleResponse<T>(response: Response): Promise<T> {
|
|
if (!response.ok) {
|
|
const errorBody = await response.json().catch(() => null);
|
|
const message = errorBody?.error || 'Request failed';
|
|
throw new Error(message);
|
|
}
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
export async function fetchApiKeys(): Promise<ApiKeySummary[]> {
|
|
const response = await fetch('/api/profile/api-keys', {
|
|
credentials: 'include',
|
|
});
|
|
return handleResponse<ApiKeySummary[]>(response);
|
|
}
|
|
|
|
export async function createApiKey(payload: {
|
|
name: string;
|
|
expires_at?: string | null;
|
|
}): Promise<CreateApiKeyResponse> {
|
|
const response = await fetch('/api/profile/api-keys', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'include',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
return handleResponse<CreateApiKeyResponse>(response);
|
|
}
|
|
|
|
export async function revokeApiKey(id: number): Promise<ApiKeySummary> {
|
|
const response = await fetch(`/api/profile/api-keys/${id}/revoke`, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
});
|
|
return handleResponse<ApiKeySummary>(response);
|
|
}
|
|
|
|
export async function deleteApiKey(id: number): Promise<void> {
|
|
const response = await fetch(`/api/profile/api-keys/${id}`, {
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
});
|
|
if (!response.ok) {
|
|
const errorBody = await response.json().catch(() => null);
|
|
const message = errorBody?.error || 'Failed to delete API key';
|
|
throw new Error(message);
|
|
}
|
|
}
|