- Changed service creation to default `enabled` to true in the database schema. - Updated service group schema to set default values for `icon`, `domain`, and health check parameters. - Refactored service routes to use `await` for fetching service views, ensuring proper asynchronous handling. - Improved error handling in service groups query to provide clearer feedback on response validation. This commit enhances the overall service management experience by ensuring services are enabled by default and improving the robustness of the service group schema.
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { queryOptions } from '@tanstack/react-query'
|
|
import { api } from '@/lib/api-client'
|
|
import {
|
|
serviceBindingSchema,
|
|
serviceGroupsResponseSchema,
|
|
serviceViewSchema,
|
|
} from '@/lib/schemas'
|
|
import { z } from 'zod'
|
|
|
|
export const serviceKeys = {
|
|
all: ['services'] as const,
|
|
}
|
|
|
|
export const serviceGroupKeys = {
|
|
all: ['service-groups'] as const,
|
|
}
|
|
|
|
export const serviceGroupsQueryOptions = () =>
|
|
queryOptions({
|
|
queryKey: serviceGroupKeys.all,
|
|
queryFn: async () => {
|
|
const data = await api.get<unknown>('/api/v1/service-groups')
|
|
const parsed = serviceGroupsResponseSchema.safeParse(data)
|
|
if (!parsed.success) {
|
|
const detail = parsed.error.issues
|
|
.slice(0, 3)
|
|
.map((issue) => `${issue.path.join('.') || '(root)'}: ${issue.message}`)
|
|
.join('; ')
|
|
throw new Error(
|
|
detail
|
|
? `Некорректный ответ /service-groups: ${detail}`
|
|
: 'Некорректный ответ /service-groups',
|
|
)
|
|
}
|
|
return parsed.data
|
|
},
|
|
})
|
|
|
|
export const servicesQueryOptions = () =>
|
|
queryOptions({
|
|
queryKey: serviceKeys.all,
|
|
queryFn: async () => {
|
|
const data = await api.get<unknown[]>('/api/v1/services')
|
|
return z.array(serviceViewSchema).parse(data)
|
|
},
|
|
})
|
|
|
|
export const serviceBindingKeys = {
|
|
all: ['service-bindings'] as const,
|
|
byDomain: (domainId: number) => [...serviceBindingKeys.all, 'domain', domainId] as const,
|
|
}
|
|
|
|
export const serviceBindingsQueryOptions = () =>
|
|
queryOptions({
|
|
queryKey: serviceBindingKeys.all,
|
|
queryFn: async () => {
|
|
const data = await api.get<unknown[]>('/api/v1/service-bindings')
|
|
return z.array(serviceBindingSchema).parse(data)
|
|
},
|
|
})
|
|
|
|
export const domainServiceBindingsQueryOptions = (domainId: number) =>
|
|
queryOptions({
|
|
queryKey: serviceBindingKeys.byDomain(domainId),
|
|
queryFn: async () => {
|
|
const data = await api.get<unknown[]>(`/api/v1/domains/${domainId}/service-bindings`)
|
|
return z.array(serviceBindingSchema).parse(data)
|
|
},
|
|
})
|
|
|
|
export interface CreateServiceBindingBody {
|
|
domain_id: number
|
|
service_id: number
|
|
hostname?: string
|
|
target_ip?: string
|
|
}
|
|
|
|
export async function createServiceBinding(body: CreateServiceBindingBody) {
|
|
const data = await api.post<unknown>('/api/v1/service-bindings', body)
|
|
return serviceBindingSchema.parse(data)
|
|
}
|
|
|
|
export async function deleteServiceBinding(id: number) {
|
|
return api.delete<{ deleted: boolean }>(`/api/v1/service-bindings/${id}`)
|
|
}
|