feat: 前端集成 Prettier 代码格式化
This commit is contained in:
@@ -1,73 +1,72 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { MessagePlugin } from 'tdesign-react';
|
||||
import * as api from '@/api/models';
|
||||
import type { CreateModelInput, UpdateModelInput, ApiError } from '@/types';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { MessagePlugin } from 'tdesign-react'
|
||||
import * as api from '@/api/models'
|
||||
import type { CreateModelInput, UpdateModelInput, ApiError } from '@/types'
|
||||
|
||||
const ERROR_MESSAGES: Record<string, string> = {
|
||||
duplicate_model: '同一供应商下模型名称已存在',
|
||||
invalid_provider_id: '供应商 ID 仅允许字母、数字、下划线,长度 1-64',
|
||||
immutable_field: '供应商 ID 不允许修改',
|
||||
provider_not_found: '供应商不存在',
|
||||
};
|
||||
}
|
||||
|
||||
function getErrorMessage(error: ApiError): string {
|
||||
return error.code ? ERROR_MESSAGES[error.code] || error.message : error.message;
|
||||
return error.code ? ERROR_MESSAGES[error.code] || error.message : error.message
|
||||
}
|
||||
|
||||
export const modelKeys = {
|
||||
all: ['models'] as const,
|
||||
filtered: (providerId?: string) => ['models', providerId] as const,
|
||||
};
|
||||
}
|
||||
|
||||
export function useModels(providerId?: string) {
|
||||
return useQuery({
|
||||
queryKey: modelKeys.filtered(providerId),
|
||||
queryFn: () => api.listModels(providerId),
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateModel() {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (input: CreateModelInput) => api.createModel(input),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: modelKeys.all });
|
||||
MessagePlugin.success('模型创建成功');
|
||||
queryClient.invalidateQueries({ queryKey: modelKeys.all })
|
||||
MessagePlugin.success('模型创建成功')
|
||||
},
|
||||
onError: (error: ApiError) => {
|
||||
MessagePlugin.error(getErrorMessage(error));
|
||||
MessagePlugin.error(getErrorMessage(error))
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateModel() {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ id, input }: { id: string; input: UpdateModelInput }) =>
|
||||
api.updateModel(id, input),
|
||||
mutationFn: ({ id, input }: { id: string; input: UpdateModelInput }) => api.updateModel(id, input),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: modelKeys.all });
|
||||
MessagePlugin.success('模型更新成功');
|
||||
queryClient.invalidateQueries({ queryKey: modelKeys.all })
|
||||
MessagePlugin.success('模型更新成功')
|
||||
},
|
||||
onError: (error: ApiError) => {
|
||||
MessagePlugin.error(getErrorMessage(error));
|
||||
MessagePlugin.error(getErrorMessage(error))
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteModel() {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => api.deleteModel(id),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: modelKeys.all });
|
||||
MessagePlugin.success('模型删除成功');
|
||||
queryClient.invalidateQueries({ queryKey: modelKeys.all })
|
||||
MessagePlugin.success('模型删除成功')
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
MessagePlugin.error(error.message);
|
||||
MessagePlugin.error(error.message)
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,72 +1,71 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { MessagePlugin } from 'tdesign-react';
|
||||
import * as api from '@/api/providers';
|
||||
import type { CreateProviderInput, UpdateProviderInput, ApiError } from '@/types';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { MessagePlugin } from 'tdesign-react'
|
||||
import * as api from '@/api/providers'
|
||||
import type { CreateProviderInput, UpdateProviderInput, ApiError } from '@/types'
|
||||
|
||||
const ERROR_MESSAGES: Record<string, string> = {
|
||||
duplicate_model: '同一供应商下模型名称已存在',
|
||||
invalid_provider_id: '供应商 ID 仅允许字母、数字、下划线,长度 1-64',
|
||||
immutable_field: '供应商 ID 不允许修改',
|
||||
provider_not_found: '供应商不存在',
|
||||
};
|
||||
}
|
||||
|
||||
function getErrorMessage(error: ApiError): string {
|
||||
return error.code ? ERROR_MESSAGES[error.code] || error.message : error.message;
|
||||
return error.code ? ERROR_MESSAGES[error.code] || error.message : error.message
|
||||
}
|
||||
|
||||
export const providerKeys = {
|
||||
all: ['providers'] as const,
|
||||
};
|
||||
}
|
||||
|
||||
export function useProviders() {
|
||||
return useQuery({
|
||||
queryKey: providerKeys.all,
|
||||
queryFn: api.listProviders,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateProvider() {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (input: CreateProviderInput) => api.createProvider(input),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: providerKeys.all });
|
||||
MessagePlugin.success('供应商创建成功');
|
||||
queryClient.invalidateQueries({ queryKey: providerKeys.all })
|
||||
MessagePlugin.success('供应商创建成功')
|
||||
},
|
||||
onError: (error: ApiError) => {
|
||||
MessagePlugin.error(getErrorMessage(error));
|
||||
MessagePlugin.error(getErrorMessage(error))
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateProvider() {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ id, input }: { id: string; input: UpdateProviderInput }) =>
|
||||
api.updateProvider(id, input),
|
||||
mutationFn: ({ id, input }: { id: string; input: UpdateProviderInput }) => api.updateProvider(id, input),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: providerKeys.all });
|
||||
MessagePlugin.success('供应商更新成功');
|
||||
queryClient.invalidateQueries({ queryKey: providerKeys.all })
|
||||
MessagePlugin.success('供应商更新成功')
|
||||
},
|
||||
onError: (error: ApiError) => {
|
||||
MessagePlugin.error(getErrorMessage(error));
|
||||
MessagePlugin.error(getErrorMessage(error))
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteProvider() {
|
||||
const queryClient = useQueryClient();
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => api.deleteProvider(id),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: providerKeys.all });
|
||||
MessagePlugin.success('供应商删除成功');
|
||||
queryClient.invalidateQueries({ queryKey: providerKeys.all })
|
||||
MessagePlugin.success('供应商删除成功')
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
MessagePlugin.error(error.message);
|
||||
MessagePlugin.error(error.message)
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import * as api from '@/api/stats';
|
||||
import type { StatsQueryParams } from '@/types';
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import * as api from '@/api/stats'
|
||||
import type { StatsQueryParams } from '@/types'
|
||||
|
||||
export const statsKeys = {
|
||||
filtered: (params?: StatsQueryParams) => ['stats', params] as const,
|
||||
};
|
||||
}
|
||||
|
||||
export function useStats(params?: StatsQueryParams) {
|
||||
return useQuery({
|
||||
queryKey: statsKeys.filtered(params),
|
||||
queryFn: () => api.getStats(params),
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user