export const getAllNotionDbRows = async (databaseId, notionAccessToken) => {
if (!databaseId) {
console.error("No Database ID Provided");
return null;
}
if (!notionAccessToken) {
console.warn("Notion Access Token is invalid");
return null;
}
const { default: axios } = await import("npm:axios");
const getNotionDbContent = async (start_cursor = undefined) => {
return await axios({
method: "POST",
url: `https://api.notion.com/v1/databases/${databaseId}/query`,
headers: {
Authorization: `Bearer ${notionAccessToken}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
},
data: {
start_cursor,
},
});
};
let result = await getNotionDbContent();
let items = result.data.results;
while (result?.data?.has_more) {
result = await getNotionDbContent(result.data.next_cursor);
items = items.concat(result?.data?.results);
}
const { length } = items;
console.log(
length
? `Successfully fetched ${length} row${length === 1 ? "" : "s"}`
: "No rows found",
);
return items;
};