26 lines
799 B
TypeScript
26 lines
799 B
TypeScript
export type ExtendProviderCredential = {
|
|
apiKey?: string | null;
|
|
updatedAt?: Date | string | null;
|
|
};
|
|
|
|
function getUpdatedAtTimestamp(value: Date | string | null | undefined): number {
|
|
if (!value) return 0;
|
|
const timestamp = value instanceof Date ? value.getTime() : Date.parse(value);
|
|
return Number.isFinite(timestamp) ? timestamp : 0;
|
|
}
|
|
|
|
export function resolveAlibabaExtendApiKey(
|
|
providers: ExtendProviderCredential[],
|
|
legacyApiKey?: string | null,
|
|
): string | null {
|
|
const currentProvider = providers
|
|
.filter((provider) => Boolean(provider.apiKey?.trim()))
|
|
.sort(
|
|
(left, right) =>
|
|
getUpdatedAtTimestamp(right.updatedAt) -
|
|
getUpdatedAtTimestamp(left.updatedAt),
|
|
)[0];
|
|
|
|
return currentProvider?.apiKey?.trim() || legacyApiKey?.trim() || null;
|
|
}
|