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 (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();
|
||||
return data.results.map((page) => {
|
||||
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);
|
||||
return {
|
||||
id: page.id,
|
||||
name: emoji ? `${emoji} ${title}` : title
|
||||
};
|
||||
});
|
||||
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.
@@ -1,4 +1,5 @@
|
||||
const NOTION_BASE = "https://api.notion.com/v1";
|
||||
const TIMEOUT_MS = 10_000;
|
||||
|
||||
export interface NotionProject {
|
||||
id: string;
|
||||
@@ -13,17 +14,17 @@ function headers(token: string) {
|
||||
};
|
||||
}
|
||||
|
||||
function fetchWithTimeout(url: string, options: RequestInit): Promise<Response> {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
||||
return fetch(url, { ...options, signal: controller.signal }).finally(() => clearTimeout(timer));
|
||||
}
|
||||
|
||||
const BOOK_COLOR: Record<string, string> = {
|
||||
green: "📗", blue: "📘", orange: "📙", red: "📕",
|
||||
yellow: "📒", pink: "📔", purple: "📓",
|
||||
};
|
||||
|
||||
const COLOR_CIRCLE: Record<string, string> = {
|
||||
green: "🟢", blue: "🔵", red: "🔴", orange: "🟠",
|
||||
yellow: "🟡", purple: "🟣", pink: "🩷", brown: "🟤",
|
||||
gray: "⚫", lightgray: "⬜", default: "⬜",
|
||||
};
|
||||
|
||||
const ICON_NAME: Record<string, string> = {
|
||||
graduate: "🎓", science: "🔬", gears: "⚙️", comment: "💬",
|
||||
building: "🏢", chart: "📊", code: "💻", document: "📄",
|
||||
@@ -51,7 +52,7 @@ export interface NotionUser {
|
||||
}
|
||||
|
||||
export async function fetchUsers(token: string): Promise<NotionUser[]> {
|
||||
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}`);
|
||||
@@ -63,27 +64,37 @@ export async function fetchUsers(token: string): Promise<NotionUser[]> {
|
||||
}
|
||||
|
||||
export async function fetchProjects(token: string, dbId: string): Promise<NotionProject[]> {
|
||||
const resp = await fetch(`${NOTION_BASE}/databases/${dbId}/query`, {
|
||||
method: "POST",
|
||||
headers: headers(token),
|
||||
body: JSON.stringify({
|
||||
const results: NotionProject[] = [];
|
||||
let cursor: string | undefined;
|
||||
|
||||
do {
|
||||
const body: Record<string, unknown> = {
|
||||
page_size: 100,
|
||||
sorts: [{ property: "Project name", direction: "ascending" }],
|
||||
}),
|
||||
};
|
||||
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()) as { results: unknown[] };
|
||||
return data.results.map((page: any) => {
|
||||
const data = (await resp.json()) as { results: unknown[]; has_more: boolean; next_cursor: string | null };
|
||||
|
||||
for (const page of data.results as any[]) {
|
||||
const titleArr: any[] = page.properties?.["Project name"]?.title ?? [];
|
||||
const title = titleArr.map((t: any) => t.plain_text).join("").trim() || "Untitled";
|
||||
const emoji = notionIconToEmoji(page);
|
||||
return {
|
||||
id: page.id as string,
|
||||
name: emoji ? `${emoji} ${title}` : title,
|
||||
};
|
||||
});
|
||||
results.push({ id: page.id as string, name: emoji ? `${emoji} ${title}` : title });
|
||||
}
|
||||
|
||||
cursor = data.has_more && data.next_cursor ? data.next_cursor : undefined;
|
||||
} while (cursor);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function startTimer(
|
||||
@@ -94,9 +105,8 @@ export async function startTimer(
|
||||
userId: string
|
||||
): Promise<string> {
|
||||
const now = new Date().toISOString();
|
||||
const date = 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({
|
||||
@@ -121,7 +131,7 @@ export async function startTimer(
|
||||
export async function stopTimer(token: string, entryId: string): Promise<void> {
|
||||
const now = 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({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const CURRENT_VERSION = "1.0.5";
|
||||
const CURRENT_VERSION = "1.0.6";
|
||||
const GITEA_BASE = "http://100.120.125.113:3000/pdm/stream_deck_notion_timer/raw/branch/master";
|
||||
const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
||||
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
|
||||
@@ -13,17 +13,23 @@ function isNewerVersion(remote: string, current: string): boolean {
|
||||
return rPat > cPat;
|
||||
}
|
||||
|
||||
function fetchWithTimeout(url: string): Promise<Response> {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), 10_000);
|
||||
return fetch(url, { signal: controller.signal }).finally(() => clearTimeout(timer));
|
||||
}
|
||||
|
||||
async function checkForUpdates(): Promise<void> {
|
||||
try {
|
||||
const resp = await fetch(`${GITEA_BASE}/version.json`);
|
||||
const resp = await fetchWithTimeout(`${GITEA_BASE}/version.json`);
|
||||
if (!resp.ok) return;
|
||||
const { version } = await resp.json() as { version: string };
|
||||
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`),
|
||||
fetchWithTimeout(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js`),
|
||||
fetchWithTimeout(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js.sig`),
|
||||
]);
|
||||
if (!pluginResp.ok || !sigResp.ok) return;
|
||||
|
||||
@@ -113,11 +119,14 @@ class TimerToggle extends SingletonAction<TimerSettings> {
|
||||
await streamDeck.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) => {
|
||||
streamDeck.logger.error("Failed to fetch users:", err);
|
||||
return [];
|
||||
}),
|
||||
]);
|
||||
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: projects, users, version: CURRENT_VERSION });
|
||||
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: projects, users: usersResult, version: CURRENT_VERSION });
|
||||
} catch (err) {
|
||||
streamDeck.logger.error("Failed to fetch projects:", err);
|
||||
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: [], error: String(err), version: CURRENT_VERSION });
|
||||
|
||||
@@ -1 +1 @@
|
||||
{ "version": "1.0.5" }
|
||||
{ "version": "1.0.6" }
|
||||
|
||||
Reference in New Issue
Block a user