forked from replicate/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathog.js
More file actions
57 lines (50 loc) · 1.46 KB
/
Copy pathog.js
File metadata and controls
57 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { ImageResponse } from "@vercel/og";
export const config = {
runtime: "edge",
};
// This endpoint take a query parameter `id` which is the ID of a prediction
// and returns an Open Graph image for that prediction
export default async function handler(req) {
const { searchParams } = req.nextUrl;
const predictionId = searchParams.get("id");
let outputImageURL;
// extract protocol and host from the request url, so we can call the local API
const url = new URL(req.url);
const protocol = url.protocol;
const host = url.host;
if (predictionId) {
const response = await fetch(
`${protocol}//${host}/api/predictions/${predictionId}`
);
const prediction = await response.json();
if (response.status === 200) {
outputImageURL = prediction.output?.[prediction.output.length - 1];
}
}
// Fallback to a default image
if (!outputImageURL) {
outputImageURL =
"https://user-images.githubusercontent.com/14149230/220761866-8f11bb7c-030d-4e14-bfb6-2e114cad2663.png";
}
return new ImageResponse(
(
<div
style={{
display: "flex",
background: "#f6f6f6",
width: "100%",
height: "100%",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
backgroundImage: `url(${outputImageURL})`,
backgroundRepeat: "repeat",
}}
></div>
),
{
width: 1200,
height: 630,
}
);
}