v1.0.6: fetch timeouts and project pagination
This commit is contained in:
@@ -6311,6 +6311,7 @@ var plugin_default = streamDeck;
|
||||
|
||||
// src/notion.ts
|
||||
var NOTION_BASE = "https://api.notion.com/v1";
|
||||
var TIMEOUT_MS = 1e4;
|
||||
function headers(token) {
|
||||
return {
|
||||
Authorization: `Bearer ${token}`,
|
||||
@@ -6318,6 +6319,11 @@ function headers(token) {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
}
|
||||
function fetchWithTimeout(url, options) {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
||||
return fetch(url, { ...options, signal: controller.signal }).finally(() => clearTimeout(timer));
|
||||
}
|
||||
var BOOK_COLOR = {
|
||||
green: "\u{1F4D7}",
|
||||
blue: "\u{1F4D8}",
|
||||
@@ -6362,7 +6368,7 @@ function notionIconToEmoji(page) {
|
||||
return "";
|
||||
}
|
||||
async function fetchUsers(token) {
|
||||
const resp = await fetch(`${NOTION_BASE}/users`, {
|
||||
const resp = await fetchWithTimeout(`${NOTION_BASE}/users`, {
|
||||
headers: headers(token)
|
||||
});
|
||||
if (!resp.ok) throw new Error(`Failed to fetch users: ${resp.status}`);
|
||||
@@ -6370,30 +6376,34 @@ async function fetchUsers(token) {
|
||||
return data.results.filter((u) => u.type === "person" && u.person?.email).map((u) => ({ id: u.id, name: u.name })).sort((a, b) => a.name.localeCompare(b.name));
|
||||
}
|
||||
async function fetchProjects(token, dbId) {
|
||||
const resp = await fetch(`${NOTION_BASE}/databases/${dbId}/query`, {
|
||||
method: "POST",
|
||||
headers: headers(token),
|
||||
body: JSON.stringify({
|
||||
const results = [];
|
||||
let cursor;
|
||||
do {
|
||||
const body = {
|
||||
page_size: 100,
|
||||
sorts: [{ property: "Project name", direction: "ascending" }]
|
||||
})
|
||||
});
|
||||
if (!resp.ok) throw new Error(`Notion error ${resp.status}`);
|
||||
const data = await resp.json();
|
||||
return data.results.map((page) => {
|
||||
const titleArr = page.properties?.["Project name"]?.title ?? [];
|
||||
const title = titleArr.map((t) => t.plain_text).join("").trim() || "Untitled";
|
||||
const emoji = notionIconToEmoji(page);
|
||||
return {
|
||||
id: page.id,
|
||||
name: emoji ? `${emoji} ${title}` : title
|
||||
};
|
||||
});
|
||||
if (cursor) body.start_cursor = cursor;
|
||||
const resp = await fetchWithTimeout(`${NOTION_BASE}/databases/${dbId}/query`, {
|
||||
method: "POST",
|
||||
headers: headers(token),
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
if (!resp.ok) throw new Error(`Notion error ${resp.status}`);
|
||||
const data = await resp.json();
|
||||
for (const page of data.results) {
|
||||
const titleArr = page.properties?.["Project name"]?.title ?? [];
|
||||
const title = titleArr.map((t) => t.plain_text).join("").trim() || "Untitled";
|
||||
const emoji = notionIconToEmoji(page);
|
||||
results.push({ id: page.id, name: emoji ? `${emoji} ${title}` : title });
|
||||
}
|
||||
cursor = data.has_more && data.next_cursor ? data.next_cursor : void 0;
|
||||
} while (cursor);
|
||||
return results;
|
||||
}
|
||||
async function startTimer(token, timingDbId, projectId, projectName, userId) {
|
||||
const now = (/* @__PURE__ */ new Date()).toISOString();
|
||||
const date = (/* @__PURE__ */ new Date()).toLocaleDateString("en-GB");
|
||||
const resp = await fetch(`${NOTION_BASE}/pages`, {
|
||||
const resp = await fetchWithTimeout(`${NOTION_BASE}/pages`, {
|
||||
method: "POST",
|
||||
headers: headers(token),
|
||||
body: JSON.stringify({
|
||||
@@ -6414,7 +6424,7 @@ async function startTimer(token, timingDbId, projectId, projectName, userId) {
|
||||
}
|
||||
async function stopTimer(token, entryId) {
|
||||
const now = (/* @__PURE__ */ new Date()).toISOString();
|
||||
const resp = await fetch(`${NOTION_BASE}/pages/${entryId}`, {
|
||||
const resp = await fetchWithTimeout(`${NOTION_BASE}/pages/${entryId}`, {
|
||||
method: "PATCH",
|
||||
headers: headers(token),
|
||||
body: JSON.stringify({
|
||||
@@ -6428,7 +6438,7 @@ async function stopTimer(token, entryId) {
|
||||
}
|
||||
|
||||
// src/plugin.ts
|
||||
var CURRENT_VERSION = "1.0.5";
|
||||
var CURRENT_VERSION = "1.0.6";
|
||||
var GITEA_BASE = "http://100.120.125.113:3000/pdm/stream_deck_notion_timer/raw/branch/master";
|
||||
var SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
||||
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
|
||||
@@ -6441,16 +6451,21 @@ function isNewerVersion(remote, current) {
|
||||
if (rMin !== cMin) return rMin > cMin;
|
||||
return rPat > cPat;
|
||||
}
|
||||
function fetchWithTimeout2(url) {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), 1e4);
|
||||
return fetch(url, { signal: controller.signal }).finally(() => clearTimeout(timer));
|
||||
}
|
||||
async function checkForUpdates() {
|
||||
try {
|
||||
const resp = await fetch(`${GITEA_BASE}/version.json`);
|
||||
const resp = await fetchWithTimeout2(`${GITEA_BASE}/version.json`);
|
||||
if (!resp.ok) return;
|
||||
const { version } = await resp.json();
|
||||
if (!/^\d+\.\d+\.\d+$/.test(version)) return;
|
||||
if (!isNewerVersion(version, CURRENT_VERSION)) return;
|
||||
const [pluginResp, sigResp] = await Promise.all([
|
||||
fetch(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js`),
|
||||
fetch(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js.sig`)
|
||||
fetchWithTimeout2(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js`),
|
||||
fetchWithTimeout2(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js.sig`)
|
||||
]);
|
||||
if (!pluginResp.ok || !sigResp.ok) return;
|
||||
const newCode = await pluginResp.text();
|
||||
@@ -6505,11 +6520,14 @@ var TimerToggle = class extends SingletonAction {
|
||||
await plugin_default.ui.sendToPropertyInspector({ event: "projects", data: [], error: "Configure Notion credentials in plugin settings first.", version: CURRENT_VERSION });
|
||||
return;
|
||||
}
|
||||
const [projects, users] = await Promise.all([
|
||||
const [projects, usersResult] = await Promise.all([
|
||||
fetchProjects(global.notionToken, global.projectsDbId),
|
||||
fetchUsers(global.notionToken)
|
||||
fetchUsers(global.notionToken).catch((err) => {
|
||||
plugin_default.logger.error("Failed to fetch users:", err);
|
||||
return [];
|
||||
})
|
||||
]);
|
||||
await plugin_default.ui.sendToPropertyInspector({ event: "projects", data: projects, users, version: CURRENT_VERSION });
|
||||
await plugin_default.ui.sendToPropertyInspector({ event: "projects", data: projects, users: usersResult, version: CURRENT_VERSION });
|
||||
} catch (err) {
|
||||
plugin_default.logger.error("Failed to fetch projects:", err);
|
||||
await plugin_default.ui.sendToPropertyInspector({ event: "projects", data: [], error: String(err), version: CURRENT_VERSION });
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user