Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(import): share OneNote types via @triliumnext/commons
Elian Doran3acf65f7ada3
diff --git a/apps/client/src/services/onenote_import.ts b/apps/client/src/services/onenote_import.tsindex 6521bc0eda..9960ae8f7a 100644--- a/apps/client/src/services/onenote_import.ts+++ b/apps/client/src/services/onenote_import.ts@@ -3,8 +3,14 @@// without this side-effect import the progress/finished toasts would never appear.import "./import.js";+import type { OneNoteFolderRef, OneNoteNotebook, OneNoteSection, OneNoteSectionGroup, OneNoteSectionSelection } from "@triliumnext/commons";+import server from "./server.js";+// The notebook/section/selection types are shared with the server; re-export them so existing callers+// (the import dialog, tests) keep importing them from this service.+export type { OneNoteFolderRef, OneNoteNotebook, OneNoteSection, OneNoteSectionGroup, OneNoteSectionSelection };+export interface OneNoteAccount {name: string;email: string;@@ -15,52 +21,6 @@ export interface OneNoteStatus {account: OneNoteAccount | null;}-export interface OneNoteSection {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;-}--export interface OneNoteSectionGroup {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;- sections: OneNoteSection[];- sectionGroups: OneNoteSectionGroup[];-}--export interface OneNoteNotebook {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;- sections: OneNoteSection[];- sectionGroups: OneNoteSectionGroup[];-}--/** Mirrors `OneNoteFolderRef` on the server. */-export interface OneNoteFolderRef {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;-}--/** Mirrors `SectionSelection` on the server. */-export interface OneNoteSectionSelection {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;- groupPath: OneNoteFolderRef[];- notebookId: string;- notebookTitle: string;- notebookCreatedDateTime?: string;- notebookLastModifiedDateTime?: string;-}-function getAuthUrl() {return server.get<{ authUrl: string }>("onenote-import/auth-url");}diff --git a/apps/server/src/routes/api/onenote_import.ts b/apps/server/src/routes/api/onenote_import.tsindex fe33061984..b3da99924c 100644--- a/apps/server/src/routes/api/onenote_import.ts+++ b/apps/server/src/routes/api/onenote_import.ts@@ -11,13 +11,14 @@* 5. POST /api/onenote-import/import -> { noteId }*/+import type { OneNoteSectionSelection } from "@triliumnext/commons";import { becca, ValidationError } from "@triliumnext/core";import type { Request, Response } from "express";import { isInternalElectronRequest } from "../../services/electron_request.js";import { getDesktopSession, type OneNoteTokenSession, setDesktopSession } from "../../services/import/onenote/desktop_session.js";import graph from "../../services/import/onenote/graph.js";-import importer, { type SectionSelection } from "../../services/import/onenote/importer.js";+import importer from "../../services/import/onenote/importer.js";import { ONENOTE_OAUTH } from "../../services/import/onenote/oauth.js";import oauth from "../../services/oauth/oauth.js";@@ -86,7 +87,7 @@ async function runImport(req: Request) {return [401, "Not connected to OneNote."];}- const { parentNoteId, sections, taskId, debug } = req.body as { parentNoteId: string; sections: SectionSelection[]; taskId: string; debug?: boolean };+ const { parentNoteId, sections, taskId, debug } = req.body as { parentNoteId: string; sections: OneNoteSectionSelection[]; taskId: string; debug?: boolean };if (!parentNoteId || !taskId) {throw new ValidationError("parentNoteId and taskId are required.");}diff --git a/apps/server/src/services/import/onenote/graph.ts b/apps/server/src/services/import/onenote/graph.tsindex e7df7435b6..5f630fd9ea 100644--- a/apps/server/src/services/import/onenote/graph.ts+++ b/apps/server/src/services/import/onenote/graph.ts@@ -5,6 +5,7 @@* Graph reference: https://learn.microsoft.com/en-us/graph/api/resources/onenote-api-overview*/+import type { OneNoteNotebook, OneNoteSection, OneNoteSectionGroup } from "@triliumnext/commons";import { getLog } from "@triliumnext/core";import { safeFetch } from "../../safe_fetch.js";@@ -78,31 +79,6 @@ export interface GraphAccount {email: string;}-export interface OneNoteSection {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;-}--export interface OneNoteSectionGroup {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;- sections: OneNoteSection[];- sectionGroups: OneNoteSectionGroup[];-}--export interface OneNoteNotebook {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;- sections: OneNoteSection[];- sectionGroups: OneNoteSectionGroup[];-}-export interface OneNotePage {id: string;title: string;diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.tsindex 33a09357dc..a3fa580d16 100644--- a/apps/server/src/services/import/onenote/importer.ts+++ b/apps/server/src/services/import/onenote/importer.ts@@ -5,6 +5,7 @@* client-side import toasts apply unchanged.*/+import type { OneNoteFolderRef, OneNoteSectionSelection } from "@triliumnext/commons";import { becca, binary_utils, type BNote, date_utils, getLog, imageService, note_service as noteService, protected_session as protectedSession, TaskContext } from "@triliumnext/core";import { t } from "i18next";import { parse } from "node-html-parser";@@ -15,31 +16,6 @@ import graph, { type OneNotePage } from "./graph.js";import { inkmlToSvg } from "./inkml.js";import { type LinkTarget, rewritePageLinks } from "./links.js";-/** A section group on the path from the notebook down to a selected section, recreated as a folder. */-export interface OneNoteFolderRef {- id: string;- title: string;- createdDateTime?: string;- lastModifiedDateTime?: string;-}--export interface SectionSelection {- id: string;- title: string;- /** OneNote's section creation timestamp (ISO 8601), preserved on the imported section folder. */- createdDateTime?: string;- /** OneNote's section last-modified timestamp (ISO 8601), preserved on the imported section folder. */- lastModifiedDateTime?: string;- /** Section groups from the notebook root down to this section's immediate group (empty if none). */- groupPath: OneNoteFolderRef[];- notebookId: string;- notebookTitle: string;- /** OneNote's notebook creation timestamp (ISO 8601), preserved on the imported notebook folder. */- notebookCreatedDateTime?: string;- /** OneNote's notebook last-modified timestamp (ISO 8601), preserved on the imported notebook folder. */- notebookLastModifiedDateTime?: string;-}-interface FetchedPage {title: string;/** OneNote's indentation level in the page list: 0 for a top-level page, 1+ for a subpage. */@@ -89,7 +65,7 @@ interface FetchedSection {* reported over the WebSocket via the "importNotes" TaskContext, so this never throws to the caller:* any error is caught and surfaced as a task error toast instead.*/-export async function importSelection({ accessToken, parentNoteId, sections, taskId, debug = false }: { accessToken: string; parentNoteId: string; sections: SectionSelection[]; taskId: string; debug?: boolean }): Promise<void> {+export async function importSelection({ accessToken, parentNoteId, sections, taskId, debug = false }: { accessToken: string; parentNoteId: string; sections: OneNoteSectionSelection[]; taskId: string; debug?: boolean }): Promise<void> {const taskContext = TaskContext.getInstance(taskId, "importNotes", { safeImport: true });try {@@ -98,7 +74,7 @@ export async function importSelection({ accessToken, parentNoteId, sections, tas// Enumerate every selected section's pages up front so the total page count is known before// any content is fetched — this lets the client show a real progress bar rather than a bare count.- const sectionPages: { section: SectionSelection; pages: OneNotePage[] }[] = [];+ const sectionPages: { section: OneNoteSectionSelection; pages: OneNotePage[] }[] = [];for (const section of sections) {sectionPages.push({ section, pages: await graph.listPages(accessToken, section.id) });}diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.tsindex 4c1b0cb42d..6039ca8ca7 100644--- a/packages/commons/src/index.ts+++ b/packages/commons/src/index.ts@@ -14,6 +14,7 @@ export * from "./lib/attribute_names.js";export * from "./lib/utils.js";export * from "./lib/dayjs.js";export * from "./lib/notes.js";+export * from "./lib/onenote.js";export * from "./lib/week_utils.js";export { default as BUILTIN_ATTRIBUTES } from "./lib/builtin_attributes.js";// The spreadsheet modules are deliberately not re-exported here: they pull in numfmt, and thediff --git a/packages/commons/src/lib/onenote.ts b/packages/commons/src/lib/onenote.tsnew file mode 100644index 0000000000..5bcf870f87--- /dev/null+++ b/packages/commons/src/lib/onenote.ts@@ -0,0 +1,54 @@+/**+ * Shared OneNote importer types, used by both the client (import dialog and service) and the server+ * (Graph client and importer). Keeping them in one place keeps the import payload type-safe end to end+ * rather than relying on two hand-kept copies staying in sync.+ */++export interface OneNoteSection {+ id: string;+ title: string;+ createdDateTime?: string;+ lastModifiedDateTime?: string;+}++export interface OneNoteSectionGroup {+ id: string;+ title: string;+ createdDateTime?: string;+ lastModifiedDateTime?: string;+ sections: OneNoteSection[];+ sectionGroups: OneNoteSectionGroup[];+}++export interface OneNoteNotebook {+ id: string;+ title: string;+ createdDateTime?: string;+ lastModifiedDateTime?: string;+ sections: OneNoteSection[];+ sectionGroups: OneNoteSectionGroup[];+}++/** A section group on the path from the notebook down to a selected section, recreated as a folder. */+export interface OneNoteFolderRef {+ id: string;+ title: string;+ createdDateTime?: string;+ lastModifiedDateTime?: string;+}++/**+ * A section the user chose to import, carrying the notebook and section-group path (notebook root down+ * to the section's immediate group) the server needs to recreate the folder nesting.+ */+export interface OneNoteSectionSelection {+ id: string;+ title: string;+ createdDateTime?: string;+ lastModifiedDateTime?: string;+ groupPath: OneNoteFolderRef[];+ notebookId: string;+ notebookTitle: string;+ notebookCreatedDateTime?: string;+ notebookLastModifiedDateTime?: string;+}