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
import { nhttp } from "npm:nhttp-land@1";
import fetchCast from "https://esm.town/v/stevedylandev/fetchCastBlog"
import satoriHandler from "https://esm.town/v/stevedylandev/satoriHandlerBlog"
import contentUploadHandler from "https://esm.town/v/stevedylandev/contentUploadHandlerBlog"
import castMintHandler from "https://esm.town/v/stevedylandev/castMintHanderBlog"
export const pocketCastApi = async (request) => {
const app = nhttp();
app.get("/", () => {
return "Hello, World";
});
app.post("/mint", async () => {
try {
const searchParams = new URL(request.url).searchParams;
const castUrl = searchParams.get("cast")
if (!castUrl.startsWith("https://warpcast.com")) {
return {
status: 400,
body: "Invalid cast URL. URL must start with 'https://warpcast.com'."
};
}
const address = searchParams.get("address")
const parts = castUrl.split('/');
const username = parts[3];
const hash = parts[4];
const rawCast = await fetchCast(hash, username)
const date = new Date(rawCast.timestamp).toDateString()
const svg = await satoriHandler(
rawCast.author.pfp.url,
rawCast.author.displayName,
rawCast.author.username,
date,
rawCast.text,
hash,
rawCast.embeds.images[0] ? rawCast.embeds.images[0].url : ""
)
const cid = await contentUploadHandler(
svg,
hash,
username,
rawCast
)
console.log(cid)
const nft = await castMintHandler(cid, address)
return nft;
} catch (error){
console.log(error)
return {
status: 500,
body: error.message
};
}
});
return app.handleRequest(request);
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.