mirror of
https://github.com/NohamR/lanyard-profile-readme.git
synced 2026-05-26 13:41:32 +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 type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import renderCard from "../../src/renderCard";
|
import renderCard from "../../src/renderCard";
|
||||||
|
import { isSnowflake } from "../../src/snowflake";
|
||||||
|
|
||||||
type Data = {
|
type Data = {
|
||||||
id?: string | string[];
|
id?: string | string[];
|
||||||
@@ -15,9 +16,24 @@ export default async function handler(
|
|||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse<Data>,
|
res: NextApiResponse<Data>,
|
||||||
) {
|
) {
|
||||||
let params: Parameters = req.query,
|
const params: Parameters = req.query,
|
||||||
userid = req.query.id[0],
|
userid = req.query.id[0];
|
||||||
lanyardData: any;
|
|
||||||
|
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("Content-Type", "image/svg+xml; charset=utf-8");
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
@@ -25,17 +41,6 @@ export default async function handler(
|
|||||||
"default-src 'none'; img-src * data:; style-src 'unsafe-inline'",
|
"default-src 'none'; img-src * data:; style-src 'unsafe-inline'",
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
let svg = await renderCard(axiosRes.data, params);
|
||||||
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);
|
|
||||||
res.status(200).send(svg as any);
|
res.status(200).send(svg as any);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user