diff --git a/apps/blog/pages/[param]/communities.tsx b/apps/blog/pages/[param]/communities.tsx index 501bf3753cf53ae736145be55e9461dfb30ad212..9f07f910a888a1aa0b2a6d5b6692d4d2e05d8259 100644 --- a/apps/blog/pages/[param]/communities.tsx +++ b/apps/blog/pages/[param]/communities.tsx @@ -90,8 +90,8 @@ const UserCommunities = ({ export default UserCommunities; export const getServerSideProps: GetServerSideProps = async (context) => { - let hivebuzzJsonStateOn = []; - let peakdJsonMapedWithURL = []; + let hivebuzzJsonStateOn: Badge[] = []; + let peakdJsonMapedWithURL: Badge[] = []; let errorCode = 0; try { @@ -100,24 +100,51 @@ export const getServerSideProps: GetServerSideProps = async (context) => { const validationResult = validateHiveAccountName(username); logger.info('validationResult: %s', validationResult); if (validationResult !== null) { - errorCode = 404; - throw new Error({ statusCode: 404 }); + errorCode = 400; + throw new Error({ statusCode: 400, message: validationResult }); } - const hivebuzzRes = await fetch(`https://hivebuzz.me/api/badges/${username}`); - if (hivebuzzRes.ok) { - const hivebuzzJson = await hivebuzzRes.json(); - hivebuzzJsonStateOn = hivebuzzJson.filter((badge: Badge) => badge.state === 'on'); - } + // Encode username for URL safety + const encodedUsername = encodeURIComponent(username); + + try { + const hivebuzzRes = await fetch(`https://hivebuzz.me/api/badges/${encodedUsername}`, { + headers: { + 'Accept': 'application/json', + 'User-Agent': 'HiveBlog/1.0' + } + }); + + if (hivebuzzRes.ok) { + const hivebuzzJson = await hivebuzzRes.json(); + if (Array.isArray(hivebuzzJson)) { + hivebuzzJsonStateOn = hivebuzzJson.filter((badge: Badge) => badge.state === 'on'); + } + } - const peakdRes = await fetch(`https://peakd.com/api/public/badge/${username}`); - if (peakdRes.ok) { - const peakdJson = await peakdRes.json(); - peakdJsonMapedWithURL = peakdJson?.map((obj: Badge) => ({ - id: obj.title, - url: `https://images.hive.blog/u/${obj.name}/avatar`, - title: obj.title - })); + const peakdRes = await fetch(`https://peakd.com/api/public/badge/${encodedUsername}`, { + headers: { + 'Accept': 'application/json', + 'User-Agent': 'HiveBlog/1.0' + } + }); + + if (peakdRes.ok) { + const peakdJson = await peakdRes.json(); + if (Array.isArray(peakdJson)) { + peakdJsonMapedWithURL = peakdJson?.map((obj: any) => ({ + id: obj.title, + url: `https://images.hive.blog/u/${encodeURIComponent(obj.name)}/avatar`, + title: obj.title, + name: obj.name, + state: 'on', + type: 'badge' + })); + } + } + } catch (error) { + logger.error('Error fetching badges: %o', error); + // Don't throw here, just log the error and continue with empty arrays } } catch (error) { logger.error('Error in getServerSideProps: %o', error);