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
// This approach directly uses axios to request the Pinboard API.
// It requires a Pinboard API token, which is set as a secret in Val Town.
import axios from "npm:axios";
export default async function main(req: Request): Promise<Response> {
const token = Deno.env.get("PINBOARD_TOKEN");
if (!token) {
return Response.json({ error: "Pinboard API token not set" }, { status: 500 });
}
try {
const response = await axios.get(
`https://api.pinboard.in/v1/posts/recent?auth_token=${token}&format=json&count=10`,
);
if (response.status !== 200) {
throw new Error(`Pinboard API returned status ${response.status}`);
}
if (!response.data || !Array.isArray(response.data.posts)) {
throw new Error("Unexpected response format from Pinboard API");
}
const bookmarks = response.data.posts.map(post => ({
title: post.description,
url: post.href,
time: post.time,
tags: post.tags.split(" "),
}));
console.log(`Fetched ${bookmarks.length} bookmarks`);
return Response.json(bookmarks);
} catch (error) {
console.error("Error fetching bookmarks:", error);
let errorMessage = "An error occurred while fetching bookmarks";
if (axios.isAxiosError(error)) {
if (error.response) {
errorMessage += `: ${error.response.status} ${error.response.statusText}`;
if (error.response.data) {
errorMessage += ` - ${JSON.stringify(error.response.data)}`;
}
} else if (error.request) {
errorMessage += ": No response received from Pinboard API";
} else {
errorMessage += `: ${error.message}`;
}
} else if (error instanceof Error) {
errorMessage += `: ${error.message}`;
}
return Response.json({ error: errorMessage }, { status: 500 });
}
}