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
server-ts: Convert routes/api/script
Elian Dorande42df40bb82
diff --git a/src/routes/api/script.js b/src/routes/api/script.tssimilarity index 70%rename from src/routes/api/script.jsrename to src/routes/api/script.tsindex 8bd7ba712e..8a1b3d0728 100644--- a/src/routes/api/script.js+++ b/src/routes/api/script.ts@@ -1,19 +1,30 @@"use strict";-const scriptService = require('../../services/script');-const attributeService = require('../../services/attributes');-const becca = require('../../becca/becca');-const syncService = require('../../services/sync');-const sql = require('../../services/sql');+import scriptService = require('../../services/script');+import attributeService = require('../../services/attributes');+import becca = require('../../becca/becca');+import syncService = require('../../services/sync');+import sql = require('../../services/sql');+import { Request } from 'express';++interface ScriptBody {+ script: string;+ params: any[];+ startNoteId: string;+ currentNoteId: string;+ originEntityName: string;+ originEntityId: string;+ transactional: boolean;+}// The async/await here is very confusing, because the body.script may, but may not be async. If it is async, then we// need to await it and make the complete response including metadata available in a Promise, so that the route detects// this and does result.then().-async function exec(req) {+async function exec(req: Request) {try {- const { body } = req;+ const body = (req.body as ScriptBody);- const execute = body => scriptService.executeScript(+ const execute = (body: ScriptBody) => scriptService.executeScript(body.script,body.params,body.startNoteId,@@ -32,20 +43,20 @@ async function exec(req) {maxEntityChangeId: syncService.getMaxEntityChangeId()};}- catch (e) {+ catch (e: any) {return { success: false, error: e.message };}}-function run(req) {- const note = becca.getNote(req.params.noteId);+function run(req: Request) {+ const note = becca.getNoteOrThrow(req.params.noteId);const result = scriptService.executeNote(note, { originEntity: note });return { executionResult: result };}-function getBundlesWithLabel(label, value) {+function getBundlesWithLabel(label: string, value?: string) {const notes = attributeService.getNotesWithLabel(label, value);const bundles = [];@@ -61,7 +72,7 @@ function getBundlesWithLabel(label, value) {return bundles;}-function getStartupBundles(req) {+function getStartupBundles(req: Request) {if (!process.env.TRILIUM_SAFE_MODE) {if (req.query.mobile === "true") {return getBundlesWithLabel("run", "mobileStartup");@@ -84,9 +95,9 @@ function getWidgetBundles() {}}-function getRelationBundles(req) {+function getRelationBundles(req: Request) {const noteId = req.params.noteId;- const note = becca.getNote(noteId);+ const note = becca.getNoteOrThrow(noteId);const relationName = req.params.relationName;const attributes = note.getAttributes();@@ -97,7 +108,7 @@ function getRelationBundles(req) {const bundles = [];for (const noteId of uniqueNoteIds) {- const note = becca.getNote(noteId);+ const note = becca.getNoteOrThrow(noteId);if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') {continue;@@ -113,14 +124,14 @@ function getRelationBundles(req) {return bundles;}-function getBundle(req) {- const note = becca.getNote(req.params.noteId);+function getBundle(req: Request) {+ const note = becca.getNoteOrThrow(req.params.noteId);const { script, params } = req.body;return scriptService.getScriptBundleForFrontend(note, script, params);}-module.exports = {+export = {exec,run,getStartupBundles,diff --git a/src/routes/routes.js b/src/routes/routes.jsindex 17d0ab3d5c..3285b5b979 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -43,7 +43,7 @@ const sqlRoute = require('./api/sql');const databaseRoute = require('./api/database');const imageRoute = require('./api/image');const attributesRoute = require('./api/attributes');-const scriptRoute = require('./api/script.js');+const scriptRoute = require('./api/script');const senderRoute = require('./api/sender.js');const filesRoute = require('./api/files');const searchRoute = require('./api/search');diff --git a/src/services/script.ts b/src/services/script.tsindex 1df79290e1..e798584d97 100644--- a/src/services/script.ts+++ b/src/services/script.ts@@ -106,7 +106,7 @@ function execute(ctx: any, script: string) {return function () { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx);}-function getParams(params: ScriptParams) {+function getParams(params?: ScriptParams) {if (!params) {return params;}@@ -121,7 +121,7 @@ function getParams(params: ScriptParams) {}).join(",");}-function getScriptBundleForFrontend(note: BNote, script: string, params: ScriptParams) {+function getScriptBundleForFrontend(note: BNote, script?: string, params?: ScriptParams) {let overrideContent = null;if (script) {