fix(providers): preserve custom endpoints in meta during add/edit operations
Fixed two critical data loss bugs where user-added custom endpoints were discarded: 1. **AddProviderDialog**: Form submission ignored values.meta from ProviderForm and re-inferred URLs only from presets/config, causing loss of endpoints added via speed test modal. Now prioritizes form-collected meta and uses fallback inference only when custom_endpoints is missing. 2. **ProviderForm**: Edit mode always returned initialData.meta, discarding any changes made in the speed test modal. Now uses mergeProviderMeta to properly merge customEndpointsMap with existing meta fields. Changes: - Extract mergeProviderMeta utility to handle meta field merging logic - Preserve other meta fields (e.g., usage_script) during endpoint updates - Unify new/edit code paths to use consistent meta handling - Add comprehensive unit tests for meta merging scenarios - Add integration tests for AddProviderDialog submission flow Impact: - Third-party and custom providers can now reliably manage multiple endpoints - Edit operations correctly reflect user modifications - No data loss for existing meta fields like usage_script
This commit is contained in:
36
src/utils/providerMetaUtils.ts
Normal file
36
src/utils/providerMetaUtils.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { CustomEndpoint, ProviderMeta } from "@/types";
|
||||
|
||||
/**
|
||||
* 合并供应商元数据中的自定义端点。
|
||||
* - 当 customEndpoints 为空对象或 null 时,移除自定义端点但保留其它元数据。
|
||||
* - 当 customEndpoints 存在时,覆盖原有自定义端点。
|
||||
* - 若结果为空对象则返回 undefined,避免写入空 meta。
|
||||
*/
|
||||
export function mergeProviderMeta(
|
||||
initialMeta: ProviderMeta | undefined,
|
||||
customEndpoints:
|
||||
| Record<string, CustomEndpoint>
|
||||
| null
|
||||
| undefined,
|
||||
): ProviderMeta | undefined {
|
||||
const hasCustomEndpoints =
|
||||
!!customEndpoints && Object.keys(customEndpoints).length > 0;
|
||||
|
||||
if (hasCustomEndpoints) {
|
||||
return {
|
||||
...(initialMeta ? { ...initialMeta } : {}),
|
||||
custom_endpoints: customEndpoints!,
|
||||
};
|
||||
}
|
||||
|
||||
if (!initialMeta) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if ("custom_endpoints" in initialMeta) {
|
||||
const { custom_endpoints, ...rest } = initialMeta;
|
||||
return Object.keys(rest).length > 0 ? rest : undefined;
|
||||
}
|
||||
|
||||
return { ...initialMeta };
|
||||
}
|
||||
Reference in New Issue
Block a user