mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	server-ts: Convert routes/api/script
This commit is contained in:
		| @@ -1,19 +1,30 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| const scriptService = require('../../services/script'); | import scriptService = require('../../services/script'); | ||||||
| const attributeService = require('../../services/attributes'); | import attributeService = require('../../services/attributes'); | ||||||
| const becca = require('../../becca/becca'); | import becca = require('../../becca/becca'); | ||||||
| const syncService = require('../../services/sync'); | import syncService = require('../../services/sync'); | ||||||
| const sql = require('../../services/sql'); | 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
 | // 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
 | // 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().
 | // this and does result.then().
 | ||||||
| async function exec(req) { | async function exec(req: Request) { | ||||||
|     try { |     try { | ||||||
|         const { body } = req; |         const body = (req.body as ScriptBody); | ||||||
| 
 | 
 | ||||||
|         const execute = body => scriptService.executeScript( |         const execute = (body: ScriptBody) => scriptService.executeScript( | ||||||
|             body.script, |             body.script, | ||||||
|             body.params, |             body.params, | ||||||
|             body.startNoteId, |             body.startNoteId, | ||||||
| @@ -32,20 +43,20 @@ async function exec(req) { | |||||||
|             maxEntityChangeId: syncService.getMaxEntityChangeId() |             maxEntityChangeId: syncService.getMaxEntityChangeId() | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e: any) { | ||||||
|         return { success: false, error: e.message }; |         return { success: false, error: e.message }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function run(req) { | function run(req: Request) { | ||||||
|     const note = becca.getNote(req.params.noteId); |     const note = becca.getNoteOrThrow(req.params.noteId); | ||||||
| 
 | 
 | ||||||
|     const result = scriptService.executeNote(note, { originEntity: note }); |     const result = scriptService.executeNote(note, { originEntity: note }); | ||||||
| 
 | 
 | ||||||
|     return { executionResult: result }; |     return { executionResult: result }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function getBundlesWithLabel(label, value) { | function getBundlesWithLabel(label: string, value?: string) { | ||||||
|     const notes = attributeService.getNotesWithLabel(label, value); |     const notes = attributeService.getNotesWithLabel(label, value); | ||||||
| 
 | 
 | ||||||
|     const bundles = []; |     const bundles = []; | ||||||
| @@ -61,7 +72,7 @@ function getBundlesWithLabel(label, value) { | |||||||
|     return bundles; |     return bundles; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function getStartupBundles(req) { | function getStartupBundles(req: Request) { | ||||||
|     if (!process.env.TRILIUM_SAFE_MODE) { |     if (!process.env.TRILIUM_SAFE_MODE) { | ||||||
|         if (req.query.mobile === "true") { |         if (req.query.mobile === "true") { | ||||||
|             return getBundlesWithLabel("run", "mobileStartup"); |             return getBundlesWithLabel("run", "mobileStartup"); | ||||||
| @@ -84,9 +95,9 @@ function getWidgetBundles() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function getRelationBundles(req) { | function getRelationBundles(req: Request) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const note = becca.getNote(noteId); |     const note = becca.getNoteOrThrow(noteId); | ||||||
|     const relationName = req.params.relationName; |     const relationName = req.params.relationName; | ||||||
| 
 | 
 | ||||||
|     const attributes = note.getAttributes(); |     const attributes = note.getAttributes(); | ||||||
| @@ -97,7 +108,7 @@ function getRelationBundles(req) { | |||||||
|     const bundles = []; |     const bundles = []; | ||||||
| 
 | 
 | ||||||
|     for (const noteId of uniqueNoteIds) { |     for (const noteId of uniqueNoteIds) { | ||||||
|         const note = becca.getNote(noteId); |         const note = becca.getNoteOrThrow(noteId); | ||||||
| 
 | 
 | ||||||
|         if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') { |         if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') { | ||||||
|             continue; |             continue; | ||||||
| @@ -113,14 +124,14 @@ function getRelationBundles(req) { | |||||||
|     return bundles; |     return bundles; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function getBundle(req) { | function getBundle(req: Request) { | ||||||
|     const note = becca.getNote(req.params.noteId); |     const note = becca.getNoteOrThrow(req.params.noteId); | ||||||
|     const { script, params } = req.body; |     const { script, params } = req.body; | ||||||
| 
 | 
 | ||||||
|     return scriptService.getScriptBundleForFrontend(note, script, params); |     return scriptService.getScriptBundleForFrontend(note, script, params); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | export = { | ||||||
|     exec, |     exec, | ||||||
|     run, |     run, | ||||||
|     getStartupBundles, |     getStartupBundles, | ||||||
| @@ -43,7 +43,7 @@ const sqlRoute = require('./api/sql'); | |||||||
| const databaseRoute = require('./api/database'); | const databaseRoute = require('./api/database'); | ||||||
| const imageRoute = require('./api/image'); | const imageRoute = require('./api/image'); | ||||||
| const attributesRoute = require('./api/attributes'); | const attributesRoute = require('./api/attributes'); | ||||||
| const scriptRoute = require('./api/script.js'); | const scriptRoute = require('./api/script'); | ||||||
| const senderRoute = require('./api/sender.js'); | const senderRoute = require('./api/sender.js'); | ||||||
| const filesRoute = require('./api/files'); | const filesRoute = require('./api/files'); | ||||||
| const searchRoute = require('./api/search'); | const searchRoute = require('./api/search'); | ||||||
|   | |||||||
| @@ -106,7 +106,7 @@ function execute(ctx: any, script: string) { | |||||||
|     return function () { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx); |     return function () { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx); | ||||||
| } | } | ||||||
|  |  | ||||||
| function getParams(params: ScriptParams) { | function getParams(params?: ScriptParams) { | ||||||
|     if (!params) { |     if (!params) { | ||||||
|         return params; |         return params; | ||||||
|     } |     } | ||||||
| @@ -121,7 +121,7 @@ function getParams(params: ScriptParams) { | |||||||
|     }).join(","); |     }).join(","); | ||||||
| } | } | ||||||
|  |  | ||||||
| function getScriptBundleForFrontend(note: BNote, script: string, params: ScriptParams) { | function getScriptBundleForFrontend(note: BNote, script?: string, params?: ScriptParams) { | ||||||
|     let overrideContent = null; |     let overrideContent = null; | ||||||
|  |  | ||||||
|     if (script) { |     if (script) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user