v1.0.6: fetch timeouts and project pagination
This commit is contained in:
@@ -6311,6 +6311,7 @@ var plugin_default = streamDeck;
|
|||||||
|
|
||||||
// src/notion.ts
|
// src/notion.ts
|
||||||
var NOTION_BASE = "https://api.notion.com/v1";
|
var NOTION_BASE = "https://api.notion.com/v1";
|
||||||
|
var TIMEOUT_MS = 1e4;
|
||||||
function headers(token) {
|
function headers(token) {
|
||||||
return {
|
return {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
@@ -6318,6 +6319,11 @@ function headers(token) {
|
|||||||
"Content-Type": "application/json"
|
"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 = {
|
var BOOK_COLOR = {
|
||||||
green: "\u{1F4D7}",
|
green: "\u{1F4D7}",
|
||||||
blue: "\u{1F4D8}",
|
blue: "\u{1F4D8}",
|
||||||
@@ -6362,7 +6368,7 @@ function notionIconToEmoji(page) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
async function fetchUsers(token) {
|
async function fetchUsers(token) {
|
||||||
const resp = await fetch(`${NOTION_BASE}/users`, {
|
const resp = await fetchWithTimeout(`${NOTION_BASE}/users`, {
|
||||||
headers: headers(token)
|
headers: headers(token)
|
||||||
});
|
});
|
||||||
if (!resp.ok) throw new Error(`Failed to fetch users: ${resp.status}`);
|
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));
|
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) {
|
async function fetchProjects(token, dbId) {
|
||||||
const resp = await fetch(`${NOTION_BASE}/databases/${dbId}/query`, {
|
const results = [];
|
||||||
method: "POST",
|
let cursor;
|
||||||
headers: headers(token),
|
do {
|
||||||
body: JSON.stringify({
|
const body = {
|
||||||
page_size: 100,
|
page_size: 100,
|
||||||
sorts: [{ property: "Project name", direction: "ascending" }]
|
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}`);
|
if (!resp.ok) throw new Error(`Notion error ${resp.status}`);
|
||||||
const data = await resp.json();
|
const data = await resp.json();
|
||||||
return data.results.map((page) => {
|
for (const page of data.results) {
|
||||||
const titleArr = page.properties?.["Project name"]?.title ?? [];
|
const titleArr = page.properties?.["Project name"]?.title ?? [];
|
||||||
const title = titleArr.map((t) => t.plain_text).join("").trim() || "Untitled";
|
const title = titleArr.map((t) => t.plain_text).join("").trim() || "Untitled";
|
||||||
const emoji = notionIconToEmoji(page);
|
const emoji = notionIconToEmoji(page);
|
||||||
return {
|
results.push({ id: page.id, name: emoji ? `${emoji} ${title}` : title });
|
||||||
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) {
|
async function startTimer(token, timingDbId, projectId, projectName, userId) {
|
||||||
const now = (/* @__PURE__ */ new Date()).toISOString();
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
||||||
const date = (/* @__PURE__ */ new Date()).toLocaleDateString("en-GB");
|
const resp = await fetchWithTimeout(`${NOTION_BASE}/pages`, {
|
||||||
const resp = await fetch(`${NOTION_BASE}/pages`, {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: headers(token),
|
headers: headers(token),
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -6414,7 +6424,7 @@ async function startTimer(token, timingDbId, projectId, projectName, userId) {
|
|||||||
}
|
}
|
||||||
async function stopTimer(token, entryId) {
|
async function stopTimer(token, entryId) {
|
||||||
const now = (/* @__PURE__ */ new Date()).toISOString();
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
||||||
const resp = await fetch(`${NOTION_BASE}/pages/${entryId}`, {
|
const resp = await fetchWithTimeout(`${NOTION_BASE}/pages/${entryId}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
headers: headers(token),
|
headers: headers(token),
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -6428,7 +6438,7 @@ async function stopTimer(token, entryId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// src/plugin.ts
|
// 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 GITEA_BASE = "http://100.120.125.113:3000/pdm/stream_deck_notion_timer/raw/branch/master";
|
||||||
var SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
var SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
||||||
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
|
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
|
||||||
@@ -6441,16 +6451,21 @@ function isNewerVersion(remote, current) {
|
|||||||
if (rMin !== cMin) return rMin > cMin;
|
if (rMin !== cMin) return rMin > cMin;
|
||||||
return rPat > cPat;
|
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() {
|
async function checkForUpdates() {
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(`${GITEA_BASE}/version.json`);
|
const resp = await fetchWithTimeout2(`${GITEA_BASE}/version.json`);
|
||||||
if (!resp.ok) return;
|
if (!resp.ok) return;
|
||||||
const { version } = await resp.json();
|
const { version } = await resp.json();
|
||||||
if (!/^\d+\.\d+\.\d+$/.test(version)) return;
|
if (!/^\d+\.\d+\.\d+$/.test(version)) return;
|
||||||
if (!isNewerVersion(version, CURRENT_VERSION)) return;
|
if (!isNewerVersion(version, CURRENT_VERSION)) return;
|
||||||
const [pluginResp, sigResp] = await Promise.all([
|
const [pluginResp, sigResp] = await Promise.all([
|
||||||
fetch(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js`),
|
fetchWithTimeout2(`${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.sig`)
|
||||||
]);
|
]);
|
||||||
if (!pluginResp.ok || !sigResp.ok) return;
|
if (!pluginResp.ok || !sigResp.ok) return;
|
||||||
const newCode = await pluginResp.text();
|
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 });
|
await plugin_default.ui.sendToPropertyInspector({ event: "projects", data: [], error: "Configure Notion credentials in plugin settings first.", version: CURRENT_VERSION });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const [projects, users] = await Promise.all([
|
const [projects, usersResult] = await Promise.all([
|
||||||
fetchProjects(global.notionToken, global.projectsDbId),
|
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) {
|
} catch (err) {
|
||||||
plugin_default.logger.error("Failed to fetch projects:", err);
|
plugin_default.logger.error("Failed to fetch projects:", err);
|
||||||
await plugin_default.ui.sendToPropertyInspector({ event: "projects", data: [], error: String(err), version: CURRENT_VERSION });
|
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 NOTION_BASE = "https://api.notion.com/v1";
|
||||||
|
const TIMEOUT_MS = 10_000;
|
||||||
|
|
||||||
export interface NotionProject {
|
export interface NotionProject {
|
||||||
id: string;
|
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> = {
|
const BOOK_COLOR: Record<string, string> = {
|
||||||
green: "📗", blue: "📘", orange: "📙", red: "📕",
|
green: "📗", blue: "📘", orange: "📙", red: "📕",
|
||||||
yellow: "📒", pink: "📔", purple: "📓",
|
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> = {
|
const ICON_NAME: Record<string, string> = {
|
||||||
graduate: "🎓", science: "🔬", gears: "⚙️", comment: "💬",
|
graduate: "🎓", science: "🔬", gears: "⚙️", comment: "💬",
|
||||||
building: "🏢", chart: "📊", code: "💻", document: "📄",
|
building: "🏢", chart: "📊", code: "💻", document: "📄",
|
||||||
@@ -51,7 +52,7 @@ export interface NotionUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchUsers(token: string): Promise<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),
|
headers: headers(token),
|
||||||
});
|
});
|
||||||
if (!resp.ok) throw new Error(`Failed to fetch users: ${resp.status}`);
|
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[]> {
|
export async function fetchProjects(token: string, dbId: string): Promise<NotionProject[]> {
|
||||||
const resp = await fetch(`${NOTION_BASE}/databases/${dbId}/query`, {
|
const results: NotionProject[] = [];
|
||||||
method: "POST",
|
let cursor: string | undefined;
|
||||||
headers: headers(token),
|
|
||||||
body: JSON.stringify({
|
do {
|
||||||
|
const body: Record<string, unknown> = {
|
||||||
page_size: 100,
|
page_size: 100,
|
||||||
sorts: [{ property: "Project name", direction: "ascending" }],
|
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}`);
|
if (!resp.ok) throw new Error(`Notion error ${resp.status}`);
|
||||||
|
|
||||||
const data = (await resp.json()) as { results: unknown[] };
|
const data = (await resp.json()) as { results: unknown[]; has_more: boolean; next_cursor: string | null };
|
||||||
return data.results.map((page: any) => {
|
|
||||||
|
for (const page of data.results as any[]) {
|
||||||
const titleArr: any[] = page.properties?.["Project name"]?.title ?? [];
|
const titleArr: any[] = page.properties?.["Project name"]?.title ?? [];
|
||||||
const title = titleArr.map((t: any) => t.plain_text).join("").trim() || "Untitled";
|
const title = titleArr.map((t: any) => t.plain_text).join("").trim() || "Untitled";
|
||||||
const emoji = notionIconToEmoji(page);
|
const emoji = notionIconToEmoji(page);
|
||||||
return {
|
results.push({ id: page.id as string, name: emoji ? `${emoji} ${title}` : title });
|
||||||
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(
|
export async function startTimer(
|
||||||
@@ -94,9 +105,8 @@ export async function startTimer(
|
|||||||
userId: string
|
userId: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const now = new Date().toISOString();
|
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",
|
method: "POST",
|
||||||
headers: headers(token),
|
headers: headers(token),
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -121,7 +131,7 @@ export async function startTimer(
|
|||||||
export async function stopTimer(token: string, entryId: string): Promise<void> {
|
export async function stopTimer(token: string, entryId: string): Promise<void> {
|
||||||
const now = new Date().toISOString();
|
const now = new Date().toISOString();
|
||||||
|
|
||||||
const resp = await fetch(`${NOTION_BASE}/pages/${entryId}`, {
|
const resp = await fetchWithTimeout(`${NOTION_BASE}/pages/${entryId}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
headers: headers(token),
|
headers: headers(token),
|
||||||
body: JSON.stringify({
|
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 GITEA_BASE = "http://100.120.125.113:3000/pdm/stream_deck_notion_timer/raw/branch/master";
|
||||||
const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
||||||
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
|
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
|
||||||
@@ -13,17 +13,23 @@ function isNewerVersion(remote: string, current: string): boolean {
|
|||||||
return rPat > cPat;
|
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> {
|
async function checkForUpdates(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(`${GITEA_BASE}/version.json`);
|
const resp = await fetchWithTimeout(`${GITEA_BASE}/version.json`);
|
||||||
if (!resp.ok) return;
|
if (!resp.ok) return;
|
||||||
const { version } = await resp.json() as { version: string };
|
const { version } = await resp.json() as { version: string };
|
||||||
if (!/^\d+\.\d+\.\d+$/.test(version)) return;
|
if (!/^\d+\.\d+\.\d+$/.test(version)) return;
|
||||||
if (!isNewerVersion(version, CURRENT_VERSION)) return;
|
if (!isNewerVersion(version, CURRENT_VERSION)) return;
|
||||||
|
|
||||||
const [pluginResp, sigResp] = await Promise.all([
|
const [pluginResp, sigResp] = await Promise.all([
|
||||||
fetch(`${GITEA_BASE}/com.pdma.notion-timer.sdPlugin/bin/plugin.js`),
|
fetchWithTimeout(`${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.sig`),
|
||||||
]);
|
]);
|
||||||
if (!pluginResp.ok || !sigResp.ok) return;
|
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 });
|
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: [], error: "Configure Notion credentials in plugin settings first.", version: CURRENT_VERSION });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const [projects, users] = await Promise.all([
|
const [projects, usersResult] = await Promise.all([
|
||||||
fetchProjects(global.notionToken, global.projectsDbId),
|
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) {
|
} catch (err) {
|
||||||
streamDeck.logger.error("Failed to fetch projects:", err);
|
streamDeck.logger.error("Failed to fetch projects:", err);
|
||||||
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: [], error: String(err), version: CURRENT_VERSION });
|
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