Files
cc-switch/src/utils/providerMetaUtils.ts

37 lines
1.0 KiB
TypeScript
Raw Normal View History

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 };
}