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 });
}
}