Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Convert services/script_context

Elian Doran884b6618fbb5
diff --git a/src/services/backend_script_api.ts b/src/services/backend_script_api.ts
index 54b6c5e95f..1e4afb4468 100644
--- a/src/services/backend_script_api.ts
+++ b/src/services/backend_script_api.ts
@@ -33,6 +33,7 @@ import BOption = require('../becca/entities/boption');
import { AttributeRow, AttributeType, NoteType } from '../becca/entities/rows';
import Becca from '../becca/becca-interface';
import { NoteParams } from './note-interface';
+import { ApiParams } from './backend_script_api_interface';
/**
@@ -46,11 +47,6 @@ import { NoteParams } from './note-interface';
* @var {BackendScriptApi} api
*/
-interface ApiParams {
- startNote: BNote;
- originEntity: AbstractBeccaEntity<any>;
-}
-
interface SearchParams {
includeArchivedNotes?: boolean;
ignoreHoistedNote?: boolean;
@@ -384,6 +380,7 @@ interface Api {
};
}
+// TODO: Convert to class.
/**
* <p>This is the main backend API interface for scripts. All the properties and methods are published in the "api" object
* available in the JS backend notes. You can use e.g. <code>api.log(api.startNote.title);</code></p>
@@ -654,4 +651,6 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
}
}
-export = BackendScriptApi;
+export = BackendScriptApi as any as {
+ new (currentNote: BNote, apiParams: ApiParams): Api
+};
diff --git a/src/services/backend_script_api_interface.ts b/src/services/backend_script_api_interface.ts
new file mode 100644
index 0000000000..2d1cdec3fc
--- /dev/null
+++ b/src/services/backend_script_api_interface.ts
@@ -0,0 +1,7 @@
+import AbstractBeccaEntity = require("../becca/entities/abstract_becca_entity");
+import BNote = require("../becca/entities/bnote");
+
+export interface ApiParams {
+ startNote: BNote;
+ originEntity: AbstractBeccaEntity<any>;
+}
\ No newline at end of file
diff --git a/src/services/script.js b/src/services/script.js
index f82f50444d..ad9e7c6ef5 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -1,4 +1,4 @@
-const ScriptContext = require('./script_context.js');
+const ScriptContext = require('./script_context');
const cls = require('./cls');
const log = require('./log');
const becca = require('../becca/becca');
@@ -76,7 +76,7 @@ function executeScript(script, params, startNoteId, currentNoteId, originEntityN
}
function execute(ctx, script) {
- 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) {
diff --git a/src/services/script_context.js b/src/services/script_context.js
deleted file mode 100644
index 7586dab24b..0000000000
--- a/src/services/script_context.js
+++ /dev/null
@@ -1,22 +0,0 @@
-const utils = require('./utils');
-const BackendScriptApi = require('./backend_script_api');
-
-function ScriptContext(allNotes, apiParams = {}) {
- this.modules = {};
- this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
- this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]);
- this.require = moduleNoteIds => {
- return moduleName => {
- const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
- const note = candidates.find(c => c.title === moduleName);
-
- if (!note) {
- return require(moduleName);
- }
-
- return this.modules[note.noteId].exports;
- }
- };
-}
-
-module.exports = ScriptContext;
diff --git a/src/services/script_context.ts b/src/services/script_context.ts
new file mode 100644
index 0000000000..4de4863d04
--- /dev/null
+++ b/src/services/script_context.ts
@@ -0,0 +1,37 @@
+import utils = require('./utils');
+import BackendScriptApi = require('./backend_script_api');
+import BNote = require('../becca/entities/bnote');
+import { ApiParams } from './backend_script_api_interface';
+
+type Module = {
+ exports: any[];
+};
+
+class ScriptContext {
+ modules: Record<string, Module>;
+ notes: {};
+ apis: {};
+ allNotes: BNote[];
+
+ constructor(allNotes: BNote[], apiParams: ApiParams) {
+ this.allNotes = allNotes;
+ this.modules = {};
+ this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
+ this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]);
+ }
+
+ require(moduleNoteIds: string[]) {
+ return (moduleName: string) => {
+ const candidates = this.allNotes.filter(note => moduleNoteIds.includes(note.noteId));
+ const note = candidates.find(c => c.title === moduleName);
+
+ if (!note) {
+ return require(moduleName);
+ }
+
+ return this.modules[note.noteId].exports;
+ }
+ };
+}
+
+export = ScriptContext;