From de42df40bb825ce22bfda08b1b4057a4f446ff90 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 6 Apr 2024 22:38:17 +0300 Subject: [PATCH] server-ts: Convert routes/api/script --- src/routes/api/{script.js => script.ts} | 49 +++++++++++++++---------- src/routes/routes.js | 2 +- src/services/script.ts | 4 +- 3 files changed, 33 insertions(+), 22 deletions(-) rename src/routes/api/{script.js => script.ts} (70%) diff --git a/src/routes/api/script.js b/src/routes/api/script.ts similarity index 70% rename from src/routes/api/script.js rename to src/routes/api/script.ts index 8bd7ba712..8a1b3d072 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.js index 17d0ab3d5..3285b5b97 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.ts index 1df79290e..e798584d9 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) {