Skip to content

Instantly share code, notes, and snippets.

@hasparus
Created March 30, 2020 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasparus/c70b93781b0081232e2470aa99f26c18 to your computer and use it in GitHub Desktop.
Save hasparus/c70b93781b0081232e2470aa99f26c18 to your computer and use it in GitHub Desktop.
avatar = facebook || tinygraphs
import { NextApiResponse, NextApiRequest } from "next";
import fetch from "isomorphic-unfetch";
export default async function avatar(
req: NextApiRequest,
res: NextApiResponse
) {
const {
query: { username_slug },
} = req;
let userAvatar =
"https://www.tinygraphs.com/spaceinvaders/" +
`${username_slug}?theme=heatwave&numcolors=4&size=220&fmt=svg`;
try {
const gravatarResponse = await fetch(
`https://unavatar.now.sh/facebook/${username_slug}?json&fallback=false`
).then((r) => r.json());
userAvatar = gravatarResponse.url || userAvatar;
} catch {
// nah, we don't care
}
res.writeHead(302, { Location: userAvatar }).end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment