mirror of
https://github.com/NohamR/lanyard-profile-readme.git
synced 2026-05-25 20:00:40 +00:00
🚑 Preventing the error when user enters an invalid user ID.
This commit is contained in:
@@ -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<Data>,
|
||||
) {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user