Versions

  • v0

    12/15/2023
    Open: Version
    +33
    -0

    import { GITHUB_EMOJIS } from "https://esm.town/v/karfau/GITHUB_EMOJIS";
    import { fetch } from "https://esm.town/v/std/fetch?v=4";

    export const isEmoji = (key: string) => Object.hasOwn(GITHUB_EMOJIS, key);

    export const githubEmojiUrl = (name: string, fallback = "octocat"): string =>
    GITHUB_EMOJIS[isEmoji(name) ? name : fallback];

    export const githubEmojiNames = (): string[] => Object.keys(GITHUB_EMOJIS);

    export default async function githubEmoji(req: Request): Promise<Response> {
    const contentType = req.headers.get("Content-Type");

    const nameFromURL = new URL(req.url).pathname.split("/").filter(Boolean)[0];
    if (nameFromURL === "names") {
    return Response.json(githubEmojiNames());
    }

    const url = githubEmojiUrl(nameFromURL);

    if (contentType || contentType === "application/json") {
    return Response.json(url);
    }
    try {
    return fetch(url);
    } catch (error) {
    console.error(req.url, nameFromURL, error);
    return new Response(
    "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"64\" height=\"64\" viewBox=\"218.329 117.251 67.385 67.385\" xml:space=\"preserve\"><path style=\"stroke:#e9d535;stroke-width:0;stroke-dasharray:none;stroke-linecap:butt;stroke-dashoffset:0;stroke-linejoin:miter;stroke-miterlimit:4;fill:#f72f3a;fill-rule:nonzero;opacity:1\" transform=\"translate(218.329 117.25)\" d=\"M33.693 0C52.29 0 67.385 15.094 67.385 33.693c0 18.598-15.094 33.692-33.692 33.692C15.094 67.385 0 52.291 0 33.693 0 15.094 15.094 0 33.693 0Z\"/><path style=\"stroke:none;stroke-width:1;stroke-dasharray:none;stroke-linecap:butt;stroke-dashoffset:0;stroke-linejoin:miter;stroke-miterlimit:4;fill:#fff;fill-rule:nonzero;opacity:1\" transform=\"matrix(.67385 0 0 .67385 225.067 123.99)\" d=\"M63.366 80 40 56.634 16.634 80 0 63.366 23.366 40 0 16.634 16.634 0 40 23.366 63.366 0 80 16.634 56.634 40 80 63.366 63.366 80z\"/></svg>",
    { headers: { "Content-Type": "image/svg" }, status: 307, statusText: "Currently not available" },
    );
    }
    }
1
Next
stevekrouse-githubemoji.web.val.run
Updated: December 15, 2023