Skip to content

Commit

Permalink
address feedback + fix issue with fetching spdx data
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey800 committed May 10, 2024
1 parent 75bf50f commit 57965a4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
35 changes: 19 additions & 16 deletions frontend/src/pages/plugins/[name].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,49 @@ export const getServerSideProps = getServerSidePropsHandler<Props, Params>({
*/
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,
},
};
},
});

Expand Down
26 changes: 15 additions & 11 deletions frontend/src/pages/plugins/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,26 +24,30 @@ const logger = new Logger('pages/plugins/index.tsx');

export const getServerSideProps = getServerSidePropsHandler<Props>({
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,
},
};
},
});

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function retryAxios<T>({
config,
instance = axios,
logger,
url = '/',
url = '',
...options
}: AsyncAxiosRetryOptions<T> = {}) {
const method = config?.method ?? 'GET';
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 10 additions & 14 deletions frontend/src/utils/spdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpdxLicenseData[]> {
try {
const {
data: { licenses },
} = await retryAxios<SpdxLicenseResponse>();
props.licenses = licenses;
} = await retryAxios<SpdxLicenseResponse>({ 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 [];
}
}

0 comments on commit 57965a4

Please sign in to comment.