CI / changes (push) Successful in 14s
CI / commitlint (push) Has been skipped
CI / openapi (push) Has been skipped
CI / web (push) Successful in 59s
CI / go (push) Has been skipped
CI / bird2 (push) Has been skipped
CI / release (push) Successful in 3m47s
Added new mutations for creating, revoking, and rotating API keys in the api-keys query file. Updated the Access component to utilize these mutations, improving the user interface with better feedback and session management. Introduced a new AccessApiKeysCard for displaying API key information and enhanced the overall layout and user experience in the access route.
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { queryOptions, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { apiJSON, apiMutate } from '@/lib/api-client'
|
|
import type { ApiKey, ApiKeyCreate, ApiKeyCreated, ApiKeysResponse } from '@/types/api'
|
|
|
|
export const apiKeysKeys = {
|
|
all: ['api-keys'] as const,
|
|
list: () => [...apiKeysKeys.all, 'list'] as const,
|
|
}
|
|
|
|
export function apiKeysQueryOptions() {
|
|
return queryOptions<ApiKey[]>({
|
|
queryKey: apiKeysKeys.list(),
|
|
queryFn: async () => {
|
|
const page = await apiJSON<ApiKeysResponse>('/v1/api-keys?limit=500')
|
|
return page.items ?? []
|
|
},
|
|
staleTime: 60_000,
|
|
})
|
|
}
|
|
|
|
function invalidateApiKeysList(qc: ReturnType<typeof useQueryClient>) {
|
|
void qc.invalidateQueries({ queryKey: apiKeysKeys.list() })
|
|
}
|
|
|
|
export function useCreateApiKeyMutation() {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: ApiKeyCreate) =>
|
|
apiMutate<ApiKeyCreated>('/v1/api-keys', 'POST', body, { idempotent: false }),
|
|
onSuccess: () => {
|
|
toast.success('Ключ создан')
|
|
invalidateApiKeysList(qc)
|
|
},
|
|
onError: (e) => toast.error(e instanceof Error ? e.message : 'Не удалось создать ключ'),
|
|
})
|
|
}
|
|
|
|
export function useRevokeApiKeyMutation() {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) =>
|
|
apiMutate(`/v1/api-keys/${id}`, 'DELETE', undefined, { idempotent: false }),
|
|
onSuccess: () => {
|
|
toast.success('Ключ отозван')
|
|
invalidateApiKeysList(qc)
|
|
},
|
|
onError: (e) => toast.error(e instanceof Error ? e.message : 'Не удалось отозвать'),
|
|
})
|
|
}
|
|
|
|
export function useRotateApiKeyMutation() {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) =>
|
|
apiMutate<ApiKeyCreated>(`/v1/api-keys/${id}/rotate`, 'POST', undefined, {
|
|
idempotent: false,
|
|
}),
|
|
onSuccess: () => {
|
|
toast.success('Ключ ротирован')
|
|
invalidateApiKeysList(qc)
|
|
},
|
|
onError: (e) => toast.error(e instanceof Error ? e.message : 'Не удалось ротировать'),
|
|
})
|
|
}
|