From becdca24df9973fb7eeb97833ede355c4593d4db Mon Sep 17 00:00:00 2001 From: twwu Date: Wed, 4 Dec 2024 16:49:49 +0800 Subject: [PATCH 1/3] fix: enhance GitHub releases fetching with optional authentication --- .../components/plugins/install-plugin/hooks.ts | 17 ++++++++++++++--- web/app/layout.tsx | 1 - web/config/index.ts | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/web/app/components/plugins/install-plugin/hooks.ts b/web/app/components/plugins/install-plugin/hooks.ts index b2a5af4a2f..b0021f76b7 100644 --- a/web/app/components/plugins/install-plugin/hooks.ts +++ b/web/app/components/plugins/install-plugin/hooks.ts @@ -2,13 +2,24 @@ import Toast, { type IToastProps } from '@/app/components/base/toast' import { uploadGitHub } from '@/service/plugins' import { compareVersion, getLatestVersion } from '@/utils/semver' import type { GitHubRepoReleaseResponse } from '../types' +import { GITHUB_ACCESS_TOKEN } from '@/config' export const useGitHubReleases = () => { const fetchReleases = async (owner: string, repo: string) => { try { - const res = await fetch(`/repos/${owner}/${repo}/releases`) - const bodyJson = await res.json() - if (bodyJson.status !== 200) throw new Error(bodyJson.data.message) + let res, bodyJson + if (!GITHUB_ACCESS_TOKEN) { + // Fetch releases without authentication from client + res = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases`) + if (!res.ok) throw new Error('Failed to fetch releases') + bodyJson = await res.json() + } + else { + // Fetch releases with authentication from server + res = await fetch(`/repos/${owner}/${repo}/releases`) + bodyJson = await res.json() + if (bodyJson.status !== 200) throw new Error(bodyJson.data.message) + } const formattedReleases = bodyJson.data.map((release: any) => ({ tag_name: release.tag_name, diff --git a/web/app/layout.tsx b/web/app/layout.tsx index 8fa7f92851..0fc56c4509 100644 --- a/web/app/layout.tsx +++ b/web/app/layout.tsx @@ -45,7 +45,6 @@ const LocaleLayout = ({ data-public-maintenance-notice={process.env.NEXT_PUBLIC_MAINTENANCE_NOTICE} data-public-site-about={process.env.NEXT_PUBLIC_SITE_ABOUT} data-public-text-generation-timeout-ms={process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS} - data-public-github-access-token={process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN} > diff --git a/web/config/index.ts b/web/config/index.ts index c3f03c1235..52acd2e9fc 100644 --- a/web/config/index.ts +++ b/web/config/index.ts @@ -272,6 +272,6 @@ export const TEXT_GENERATION_TIMEOUT_MS = textGenerationTimeoutMs export const DISABLE_UPLOAD_IMAGE_AS_ICON = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true' -export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || globalThis.document?.body?.getAttribute('data-public-github-access-token') || '' +export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || '' export const SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS = '.difypkg,.difybndl' From 44b0039e8b85fd0fb4dcce8b7c8c57c09bd08997 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 4 Dec 2024 18:05:50 +0800 Subject: [PATCH 2/3] fix: install plugin type show error --- web/app/components/plugins/card/index.tsx | 2 +- .../install-plugin/install-from-local-package/steps/install.tsx | 1 + web/app/components/plugins/types.ts | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/web/app/components/plugins/card/index.tsx b/web/app/components/plugins/card/index.tsx index 06253aed7b..4827f4af4c 100644 --- a/web/app/components/plugins/card/index.tsx +++ b/web/app/components/plugins/card/index.tsx @@ -44,7 +44,7 @@ const Card = ({ const locale = localeFromProps ? getLanguage(localeFromProps) : defaultLocale const { categoriesMap } = useCategories() const { type, category, name, org, label, brief, icon, verified } = payload - const cornerMark = type !== 'plugin' ? categoriesMap.bundle?.label : categoriesMap[category]?.label + const cornerMark = !['plugin', 'model', 'tool', 'extension'].includes(type) ? categoriesMap.bundle?.label : categoriesMap[category]?.label const getLocalizedText = (obj: Record | undefined) => obj?.[locale] || obj?.['en-US'] || obj?.en_US || '' diff --git a/web/app/components/plugins/install-plugin/install-from-local-package/steps/install.tsx b/web/app/components/plugins/install-plugin/install-from-local-package/steps/install.tsx index 13d766cbad..19baa86d73 100644 --- a/web/app/components/plugins/install-plugin/install-from-local-package/steps/install.tsx +++ b/web/app/components/plugins/install-plugin/install-from-local-package/steps/install.tsx @@ -45,6 +45,7 @@ const Installed: FC = ({ useEffect(() => { if (hasInstalled && uniqueIdentifier === installedInfoPayload.uniqueIdentifier) onInstalled() + // eslint-disable-next-line react-hooks/exhaustive-deps }, [hasInstalled]) const [isInstalling, setIsInstalling] = React.useState(false) diff --git a/web/app/components/plugins/types.ts b/web/app/components/plugins/types.ts index dd59ed6c57..d130b08e42 100644 --- a/web/app/components/plugins/types.ts +++ b/web/app/components/plugins/types.ts @@ -107,7 +107,7 @@ export type PluginDetail = { } export type Plugin = { - type: 'plugin' | 'bundle' + type: 'plugin' | 'bundle' | 'model' | 'extension' | 'tool' org: string author?: string name: string From 0ac5e53c2ea34a276e3fc85068b557ca35be8d70 Mon Sep 17 00:00:00 2001 From: twwu Date: Wed, 4 Dec 2024 18:08:25 +0800 Subject: [PATCH 3/3] fix: refactor GitHub releases fetching to improve error handling and format response --- .../plugins/install-plugin/hooks.ts | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/web/app/components/plugins/install-plugin/hooks.ts b/web/app/components/plugins/install-plugin/hooks.ts index b0021f76b7..5da4c75c51 100644 --- a/web/app/components/plugins/install-plugin/hooks.ts +++ b/web/app/components/plugins/install-plugin/hooks.ts @@ -4,32 +4,33 @@ import { compareVersion, getLatestVersion } from '@/utils/semver' import type { GitHubRepoReleaseResponse } from '../types' import { GITHUB_ACCESS_TOKEN } from '@/config' +const formatReleases = (releases: any) => { + return releases.map((release: any) => ({ + tag_name: release.tag_name, + assets: release.assets.map((asset: any) => ({ + browser_download_url: asset.browser_download_url, + name: asset.name, + })), + })) +} + export const useGitHubReleases = () => { const fetchReleases = async (owner: string, repo: string) => { try { - let res, bodyJson if (!GITHUB_ACCESS_TOKEN) { // Fetch releases without authentication from client - res = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases`) - if (!res.ok) throw new Error('Failed to fetch releases') - bodyJson = await res.json() + const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases`) + if (!res.ok) throw new Error('Failed to fetch repository releases') + const data = await res.json() + return formatReleases(data) } else { // Fetch releases with authentication from server - res = await fetch(`/repos/${owner}/${repo}/releases`) - bodyJson = await res.json() + const res = await fetch(`/repos/${owner}/${repo}/releases`) + const bodyJson = await res.json() if (bodyJson.status !== 200) throw new Error(bodyJson.data.message) + return formatReleases(bodyJson.data) } - - const formattedReleases = bodyJson.data.map((release: any) => ({ - tag_name: release.tag_name, - assets: release.assets.map((asset: any) => ({ - browser_download_url: asset.browser_download_url, - name: asset.name, - })), - })) - - return formattedReleases } catch (error) { if (error instanceof Error) {