From a19cf402c5a877232c021e41ff8f732add037684 Mon Sep 17 00:00:00 2001 From: barbarbar338 Date: Tue, 22 Jun 2021 19:24:56 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20Preventing=20the=20error=20when?= =?UTF-8?q?=20user=20enters=20an=20invalid=20user=20ID.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/api/[...id].ts | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/pages/api/[...id].ts b/pages/api/[...id].ts index 27aff90..3cd6616 100644 --- a/pages/api/[...id].ts +++ b/pages/api/[...id].ts @@ -1,6 +1,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import axios from "axios"; import renderCard from "../../src/renderCard"; +import { isSnowflake } from "../../src/snowflake"; type Data = { id?: string | string[]; @@ -15,9 +16,24 @@ export default async function handler( req: NextApiRequest, res: NextApiResponse, ) { - let params: Parameters = req.query, - userid = req.query.id[0], - lanyardData: any; + const params: Parameters = req.query, + userid = req.query.id[0]; + + if (!isSnowflake(userid)) + return res.send({ + error: `Specify a valid Discord user ID! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, + }); + + let err: any; + const axiosRes = await axios + .get(`https://api.lanyard.rest/v1/users/${userid}`) + .catch((e) => (err = e)); + + if (err) console.log(err); + if (err || axiosRes.status != 200) + return res.send({ + error: `Something went wrong! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, + }); res.setHeader("Content-Type", "image/svg+xml; charset=utf-8"); res.setHeader( @@ -25,17 +41,6 @@ export default async function handler( "default-src 'none'; img-src * data:; style-src 'unsafe-inline'", ); - try { - lanyardData = await axios.get( - `https://api.lanyard.rest/v1/users/${userid}`, - ); - } catch (e) { - console.log(e); - res.send({ - error: `Something went wrong! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, - }); - } - - let svg = await renderCard(lanyardData.data, params); + let svg = await renderCard(axiosRes.data, params); res.status(200).send(svg as any); }