-
Notifications
You must be signed in to change notification settings - Fork 10
/
Search.ts
30 lines (25 loc) · 1.12 KB
/
Search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import EndpointInterface from '../types/EndpointInterface';
// @todo: consider auth login through abstract class
export default class Search implements EndpointInterface {
// @todo: move those into either decorators or interface
supportedMethods = ['POST'];
supportedDataTypes = ['application/json'];
permitBookmarklet = false;
public async init(request: any) {
// @todo: validate request data
const searchResults = await this.search(request.data);
const items = await Zotero.Items.getAsync(searchResults);
// @todo: improve response handing
return [200, 'application/json', JSON.stringify(items)];
}
private search(conditions: any[]) {
const s = new Zotero.Search();
s.libraryID = Zotero.Libraries.userLibraryID;
// @todo: make all "everything" queries be "contains"
// @todo: docs on possible conditions and operators wouldn't hurt here
conditions.forEach(({ condition, operator = 'contains', value, required = true}) => {
s.addCondition(condition, operator, value, required)
});
return s.search();
}
}