You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-06 09:19:22 +02:00
API: Resolves #1148: Added support for search end-point and improved error handling
This commit is contained in:
@@ -3,15 +3,17 @@ const Note = require('lib/models/Note');
|
||||
|
||||
class SearchEngineUtils {
|
||||
|
||||
static async notesForQuery(query) {
|
||||
static async notesForQuery(query, options = null) {
|
||||
if (!options) options = {};
|
||||
|
||||
const results = await SearchEngine.instance().search(query);
|
||||
const noteIds = results.map(n => n.id);
|
||||
|
||||
const previewOptions = {
|
||||
const previewOptions = Object.assign({}, {
|
||||
order: [],
|
||||
fields: Note.previewFields(),
|
||||
conditions: ['id IN ("' + noteIds.join('","') + '")'],
|
||||
}
|
||||
}, options);
|
||||
|
||||
const notes = await Note.previews(null, previewOptions);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ const { shim } = require('lib/shim');
|
||||
const HtmlToMd = require('lib/HtmlToMd');
|
||||
const { fileExtension, safeFileExtension, safeFilename, filename } = require('lib/path-utils');
|
||||
const ApiResponse = require('lib/services/rest/ApiResponse');
|
||||
const SearchEngineUtils = require('lib/services/SearchEngineUtils');
|
||||
|
||||
class ApiError extends Error {
|
||||
|
||||
@@ -207,6 +208,15 @@ class Api {
|
||||
throw new ErrorMethodNotAllowed();
|
||||
}
|
||||
|
||||
async action_search(request) {
|
||||
if (request.method !== 'GET') throw new ErrorMethodNotAllowed();
|
||||
|
||||
const query = request.query.query;
|
||||
if (!query) throw new ErrorBadRequest('Missing "query" parameter');
|
||||
|
||||
return await SearchEngineUtils.notesForQuery(query, this.notePreviewsOptions_(request));
|
||||
}
|
||||
|
||||
async action_folders(request, id = null, link = null) {
|
||||
if (request.method === 'GET' && !id) {
|
||||
return await Folder.allAsTree({ fields: this.fields_(request, ['id', 'parent_id', 'title']) });
|
||||
|
||||
Reference in New Issue
Block a user