fuc-new/doc/node_modules/vuepress-theme-plume/lib/client/features/composables/repo.js
hellcat 6adbc04d4b 1
2025-10-03 17:17:08 +08:00

59 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useLocalStorage } from "@vueuse/core";
import { computed, ref, toValue, watch } from "vue";
//#region src/client/features/composables/repo.ts
/**
* 由于 github repo api 请求频率过高vercel 免费额度有限,因此使用本地缓存。
* 默认缓存 6 小时 时间
*/
const storage = useLocalStorage("__VUEPRESS_GITHUB_REPO__", {});
function useGithubRepo(repo, provider) {
const repoRef = computed(() => {
const [owner = "", name = ""] = toValue(repo).split("/");
return {
owner,
name
};
});
const providerRef = computed(() => toValue(provider) ?? "github");
const data = ref(null);
const loaded = ref(false);
async function fetchData() {
const { owner, name } = toValue(repoRef);
if (__VUEPRESS_SSR__ || !owner || !name) return;
const key = `${providerRef.value === "github" ? "" : `${providerRef.value}:`}${owner}/${name}`;
const cached = storage.value[key];
if (cached?.info?.name && Date.now() - cached.updatedAt <= 864e5) {
data.value = cached.info;
loaded.value = true;
return;
}
loaded.value = false;
try {
const res = await fetch(`https://api.pengzhanbo.cn/${providerRef.value}/repo/${owner}/${name}`).then((res$1) => res$1.json());
loaded.value = true;
res.convertStars = convertThousand(res.stars);
res.convertForks = convertThousand(res.forks);
data.value = res;
storage.value[key] = {
info: res,
updatedAt: Date.now()
};
} catch (e) {
loaded.value = true;
console.error("github repo error:", e);
}
}
if (!__VUEPRESS_SSR__) watch(repoRef, fetchData, { immediate: true });
return {
data,
loaded
};
}
function convertThousand(num) {
if (num < 1e3) return num;
return `${(num / 1e3).toFixed(1)}k`;
}
//#endregion
export { useGithubRepo };