重构ossStore

This commit is contained in:
2025-06-19 23:07:06 +08:00
parent 69b8967014
commit 4ae87be385
5 changed files with 89 additions and 67 deletions

View File

@@ -3,30 +3,22 @@ import { StsToken } from "@/lib/api/oss";
import OSS from "ali-oss";
import { useEffect } from "react";
export function useOssStore(options: { region: string; bucket: string; onStsTokenDataChanged?: (data: StsToken | undefined) => void; }) {
const { stsTokenData, isLoading, error } = useOssSts();
export function useOssStore(options: { onStsTokenDataChanged?: (data: StsToken | undefined) => void; } = {}) {
const { stsTokenData, isLoading, error, mutate } = useOssSts();
useEffect(() => {
options.onStsTokenDataChanged?.(stsTokenData);
}, [stsTokenData])
}, [stsTokenData]);
/** @todo */
const refresh = async () => {
await mutate();
}
return {
stsTokenData,
isLoading,
error,
store: stsTokenData ? new OSS({
region: options.region,
bucket: options.bucket,
accessKeyId: stsTokenData.AccessKeyId,
accessKeySecret: stsTokenData.AccessKeySecret,
stsToken: stsTokenData.SecurityToken,
refreshSTSToken: () => new Promise(resolve => {
resolve({
accessKeyId: stsTokenData.AccessKeyId,
accessKeySecret: stsTokenData.AccessKeySecret,
stsToken: stsTokenData.SecurityToken,
})
}),
}) : undefined,
refresh,
}
}