diff --git a/frontend/src/pages/plugins/[name].tsx b/frontend/src/pages/plugins/[name].tsx index 8c37805d8..e5dd94d61 100644 --- a/frontend/src/pages/plugins/[name].tsx +++ b/frontend/src/pages/plugins/[name].tsx @@ -41,46 +41,49 @@ export const getServerSideProps = getServerSidePropsHandler({ */ async getProps({ params }) { const name = String(params?.name); - const props: Props = { - repo: DEFAULT_REPO_DATA, - }; - let codeRepo = ''; + let plugin: PluginData | undefined; try { - const plugin = await hubAPI.getPlugin(name); + plugin = await hubAPI.getPlugin(name); codeRepo = plugin.code_repository; - props.plugin = plugin; } catch (err) { - props.error = getErrorMessage(err); + const error = getErrorMessage(err); logger.error({ message: 'Failed to fetch plugin data', plugin: name, - error: props.error, + error, }); - return { props }; + return { + props: { error }, + }; } const repoData = await fetchRepoData(codeRepo); - Object.assign(props, repoData); - if (props.repoFetchError) { - const logType = inRange(props.repoFetchError.status, 400, 500) + if (repoData.repoFetchError) { + const logType = inRange(repoData.repoFetchError.status, 400, 500) ? 'info' : 'error'; logger[logType]({ message: 'Failed to fetch repo data', plugin: name, - error: props.error, + error: repoData.repoFetchError, }); } - const spdxProps = await getSpdxProps(logger); - Object.assign(props, spdxProps); + const licenses = await getSpdxProps(logger); - return { props }; + return { + props: { + plugin, + licenses, + repo: DEFAULT_REPO_DATA, + ...repoData, + }, + }; }, }); diff --git a/frontend/src/pages/plugins/index.tsx b/frontend/src/pages/plugins/index.tsx index df19d00fa..329f93782 100644 --- a/frontend/src/pages/plugins/index.tsx +++ b/frontend/src/pages/plugins/index.tsx @@ -10,7 +10,7 @@ import { PluginIndexData } from '@/types'; import { Logger } from '@/utils'; import { getErrorMessage } from '@/utils/error'; import { hubAPI } from '@/utils/HubAPIClient'; -import { getSpdxProps } from '@/utils/spdx'; +import { getSpdxProps as getSpdxLicenses } from '@/utils/spdx'; import { getServerSidePropsHandler } from '@/utils/ssr'; interface Props { @@ -24,26 +24,30 @@ const logger = new Logger('pages/plugins/index.tsx'); export const getServerSideProps = getServerSidePropsHandler({ async getProps() { - const props: Props = { - status: 200, - }; + let index: PluginIndexData[]; try { - const index = await hubAPI.getPluginIndex(); - props.index = index; + index = await hubAPI.getPluginIndex(); } catch (err) { - props.error = getErrorMessage(err); + const error = getErrorMessage(err); logger.error({ message: 'Failed to plugin index', - error: props.error, + error, }); + + return { props: { error } }; } - const spdxProps = await getSpdxProps(logger); - Object.assign(props, spdxProps); + const licenses = await getSpdxLicenses(logger); - return { props }; + return { + props: { + index, + licenses, + status: 200, + }, + }; }, }); diff --git a/frontend/src/utils/async.ts b/frontend/src/utils/async.ts index 5747d42af..7c85aa468 100644 --- a/frontend/src/utils/async.ts +++ b/frontend/src/utils/async.ts @@ -79,7 +79,7 @@ export async function retryAxios({ config, instance = axios, logger, - url = '/', + url = '', ...options }: AsyncAxiosRetryOptions = {}) { const method = config?.method ?? 'GET'; diff --git a/frontend/src/utils/repo.ts b/frontend/src/utils/repo.ts index 36a2849cd..be285e41c 100644 --- a/frontend/src/utils/repo.ts +++ b/frontend/src/utils/repo.ts @@ -11,7 +11,7 @@ import { PluginRepoData, PluginRepoFetchError } from '@/types'; const REPO_REGEX = /(?:git@|https:\/\/)(github).com[/:](.*)(?:.git)?/; export interface FetchRepoDataResult { - repo: PluginRepoData; + repo?: PluginRepoData; repoFetchError?: PluginRepoFetchError; } diff --git a/frontend/src/utils/spdx.ts b/frontend/src/utils/spdx.ts index 53a18b44b..26fba52b3 100644 --- a/frontend/src/utils/spdx.ts +++ b/frontend/src/utils/spdx.ts @@ -13,26 +13,22 @@ export const spdxLicenseDataAPI = axios.create({ 'https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json', }); -interface PropsResult { - licenses?: SpdxLicenseData[]; - error?: string; -} - -export async function getSpdxProps(logger?: Logger) { - const props: PropsResult = {}; - +export async function getSpdxProps( + logger?: Logger, +): Promise { try { const { data: { licenses }, - } = await retryAxios(); - props.licenses = licenses; + } = await retryAxios({ instance: spdxLicenseDataAPI }); + + return licenses; } catch (err) { - props.error = getErrorMessage(err); + const error = getErrorMessage(err); logger?.error({ message: 'Failed to fetch spdx license data', - error: props.error, + error, }); - } - return props; + return []; + } }