mirror of
https://github.com/NohamR/lanyard-profile-readme.git
synced 2026-05-25 20:00:40 +00:00
feat: migrate to the new system.
This commit is contained in:
67
old/pages/api/[...id].ts
Normal file
67
old/pages/api/[...id].ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import renderCard from "../../src/renderCard";
|
||||
import { isSnowflake } from "../../src/snowflake";
|
||||
import redis from "../../src/redis";
|
||||
|
||||
type Data = {
|
||||
id?: string | string[];
|
||||
error?: any;
|
||||
code?: string;
|
||||
};
|
||||
|
||||
type Parameters = {
|
||||
theme?: string;
|
||||
bg?: string;
|
||||
hideStatus?: string;
|
||||
hideTimestamp?: string;
|
||||
hideDiscrim?: string;
|
||||
borderRadius?: string;
|
||||
animated?: string;
|
||||
};
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
|
||||
let getUser: any = {};
|
||||
|
||||
if (!req.query.id)
|
||||
return res.send({
|
||||
error: `No ID provided.`,
|
||||
});
|
||||
|
||||
const params: Parameters = req.query,
|
||||
userId = req.query.id[0];
|
||||
|
||||
if (!isSnowflake(userId))
|
||||
return res.send({
|
||||
error: `That is not a valid snowflake ID!`,
|
||||
});
|
||||
|
||||
try {
|
||||
getUser.data = await fetch(`https://api.lanyard.rest/v1/users/${userId}`).then(res => res.json());
|
||||
} catch (error: any) {
|
||||
if (error.response.data && error.response.data.error.message)
|
||||
return res
|
||||
.status(404)
|
||||
.send({ error: error.response.data.error.message, code: error.response.data.error.code });
|
||||
|
||||
if (error.response.status === 404) return res.status(404).send({ error: "Invalid user!" });
|
||||
|
||||
console.log(error); // Only console log the error if its not a 404
|
||||
|
||||
return res.status(400).send({
|
||||
error: `Something went wrong! If everything looks correct and this still occurs, please contact @notcnrad on Twitter.`,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
let user = await redis.hget("users", userId);
|
||||
if (!user) await redis.hset("users", userId, "true");
|
||||
} catch {
|
||||
null;
|
||||
}
|
||||
|
||||
res.setHeader("Content-Type", "image/svg+xml; charset=utf-8");
|
||||
res.setHeader("content-security-policy", "default-src 'none'; img-src * data:; style-src 'unsafe-inline'");
|
||||
|
||||
const svg = await renderCard(getUser.data, params);
|
||||
res.status(200).send(svg as any);
|
||||
}
|
||||
Reference in New Issue
Block a user