diff --git a/joplin.js b/joplin.js new file mode 100644 index 0000000..a231db9 --- /dev/null +++ b/joplin.js @@ -0,0 +1,43 @@ +import fetch from 'node-fetch' +import 'dotenv/config' + +// This class is used to interact with the Joplin Data API. It provides a method for getting notes from a specified notebook, using the notebook's ID. The fields parameter specifies which fields to include in the response. +class Joplin { + constructor(joplinToken, joplinPort) { + this.token = joplinToken + this.port = joplinPort + + this.getNotesFromNotebook.bind(this) + } + + + //This function gets notes from a specified notebook, using the notebook's ID. The fields parameter specifies which fields to include in the response. + // @param {string=} notebookID - The id of the notebook you want to get notes from, as extracted from the notebook's external link + // @param {Array[string]=} fields - The specific fields wanted in the returned response. Valid fields can be found in this table: https://joplinapp.org/api/references/rest_api/#properties + async getNotesFromNotebook(notebookID, fields) { + const fieldsString = fields.join() + + let items = [] + let pageNum = 1 + let data = null + + do { + let tempURL = "http://localhost:" + this.port + "/folders/" + notebookID + "/notes" + let foldersURL = new URL(tempURL) + foldersURL.searchParams.append("token", this.token) + foldersURL.searchParams.append("fields", fieldsString) + foldersURL.searchParams.append("page", pageNum) + + const res = await fetch(foldersURL.href) + data = await res.json() + items = items.concat(data.items) + + pageNum++ + } + while (data.has_more) + + return items + } +} + +export default Joplin diff --git a/package.json b/package.json index e1b46c7..296324a 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "type": "module", "dependencies": { "@notionhq/client": "^2.1.1", "dotenv": "^16.0.1",