diff --git a/packages/skin-museum-og/pages/api/hello.js b/packages/skin-museum-og/pages/api/hello.js deleted file mode 100644 index df63de88..00000000 --- a/packages/skin-museum-og/pages/api/hello.js +++ /dev/null @@ -1,5 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction - -export default function handler(req, res) { - res.status(200).json({ name: 'John Doe' }) -} diff --git a/packages/skin-museum-og/pages/api/og.js b/packages/skin-museum-og/pages/api/og.js index c07bb2f8..7ec135eb 100644 --- a/packages/skin-museum-og/pages/api/og.js +++ b/packages/skin-museum-og/pages/api/og.js @@ -1,42 +1,28 @@ import { ImageResponse } from "@vercel/og"; +import Frame from "../../src/og/Frame"; +import { fetchGraphql, stripExt } from "../../src/og/ogUtils"; export const config = { runtime: "experimental-edge", }; -async function fetchGraphql(query, variables) { - const response = await fetch("https://api.webampskins.org/graphql", { - headers: { - accept: "application/json", - "content-type": "application/json", - }, - referrer: "https://skins.webamp.org/", - referrerPolicy: "strict-origin-when-cross-origin", - body: JSON.stringify({ query, variables }), - method: "POST", - }); - if (!response.ok) { - throw new Error("Failed to fetch skin info"); - } - const json = await response.json(); - const { data } = json; - return data; -} - -function stripExt(filename) { - return filename.replace(/\.[^/.]+$/, ""); -} - export const SCREENSHOT_WIDTH = 275; export const SCREENSHOT_HEIGHT = 348; -const QUERY = ` -query Skin($md5: String!) { - skin: fetch_skin_by_md5(md5: $md5) { - filename - screenshot_url - } -}`; +const aspectRatio = SCREENSHOT_WIDTH / SCREENSHOT_HEIGHT; + +export default async function (req) { + const { searchParams } = req.nextUrl; + const skinMd5 = searchParams.get("md5"); + const query = searchParams.get("query"); + if (query) { + return searchImage(query); + } + if (skinMd5) { + return permalinkImage(skinMd5); + } + throw new Error("Expected skin md5 or query"); +} async function searchImage(query) { const data = await fetchGraphql( @@ -52,7 +38,6 @@ async function searchImage(query) { }`, { query } ); - const aspectRatio = SCREENSHOT_WIDTH / SCREENSHOT_HEIGHT; return new ImageResponse( ( @@ -111,19 +96,18 @@ async function searchImage(query) { ); } -export default async function (req) { - const { searchParams } = req.nextUrl; - const skinMd5 = searchParams.get("md5"); - const query = searchParams.get("query"); - if (query) { - return searchImage(query); - } - if (skinMd5 == null) { - throw new Error("Expected skin md5"); - } - const data = await fetchGraphql(QUERY, { md5: skinMd5 }); - const aspectRatio = SCREENSHOT_WIDTH / SCREENSHOT_HEIGHT; - const FOO = 900; +async function permalinkImage(skinMd5) { + const data = await fetchGraphql( + ` + query Skin($md5: String!) { + skin: fetch_skin_by_md5(md5: $md5) { + filename + screenshot_url + } + }`, + { md5: skinMd5 } + ); + const size = 900; return new ImageResponse( ( @@ -139,12 +123,12 @@ export default async function (req) { >