-
Notifications
You must be signed in to change notification settings - Fork 5
/
FollowSearches.js
159 lines (147 loc) · 3.94 KB
/
FollowSearches.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import fetch from "unfetch";
import { getToken } from "./token";
/**
* @typedef Search
* @property {string} searchTitle
* @property {string} searchQuery
*/
/**
* @typedef Material
* @property {string} pid
*/
/**
* https://github.com/reload/follow-searches/blob/develop/spec/follow-searches-1.1.0.yaml
*
* @class FollowSearches
*/
class FollowSearches {
constructor({ baseUrl }) {
this.baseUrl = baseUrl;
}
/**
* Get all the users searches.
* /list/{listName}
*
* @param {object} options
* @param {string} options.listName
* @param {number} options.page min: 1 - Has no effect if not set.
* @param {number} options.size min: 1 - If not set returns the full list.
* @returns {Promise<Search[]>} a list of searches.
* @memberof FollowSearches
*/
async getSearches({ listName = "default", page, size } = {}) {
if (!listName) {
throw Error("listName must be provided.");
}
const query = [page && `page=${page}`, size && `size=${size}`]
.filter(parameter => parameter)
.join("&");
const raw = await fetch(
`${this.baseUrl}/list/${listName}${query ? `?${query}` : ""}`,
{
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${getToken("user")}`
}
}
);
if (raw.status !== 200) {
throw Error(raw.status);
}
return raw.json();
}
/**
* Add search to the list.
* /list/{listName}/add
*
* @param {object} options
* @param {string} options.listName
* @param {Search} options.query
* @returns {Promise}
* @memberof FollowSearches
*/
async addSearch({ listName = "default", query, title } = {}) {
if (!query) {
throw Error("query must be provided.");
}
if (!title) {
throw Error("title must be provided");
}
const response = await fetch(`${this.baseUrl}/list/${listName}/add`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken("user")}`
},
body: JSON.stringify({ title, query })
});
if (response.status !== 201) {
throw Error(response.status);
}
}
/**
* Get the search results (pids) for a specific search.
* /list/{listName}/{searchId}
*
* @param {object} options
* @param {string} options.listName
* @param {number} options.searchId
* @param {string[]} options.fields https://raw.githubusercontent.com/DBCDK/serviceprovider/master/doc/work-context.jsonld
* @returns {Promise<Material[]>} materials pids of the search.
* @memberof FollowSearches
*/
async getResultsForSearch({
listName = "default",
searchId,
fields = ["pid", "dcTitleFull"]
} = {}) {
if (!searchId) {
throw Error("searchId must be provided");
}
const formattedFields = fields.map(encodeURIComponent).join(",");
const raw = await fetch(
`${this.baseUrl}/list/${listName}/${searchId}?fields=${formattedFields}`,
{
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${getToken("user")}`
}
}
);
if (raw.status !== 200) {
throw Error(raw.status);
}
const response = await raw.json();
return response.materials;
}
/**
* Delete search from list.
* /list/{listName}/{searchId}
*
* @param {object} options
* @param {string} options.listName
* @param {number} options.searchId
* @returns {Promise}
* @memberof FollowSearches
*/
async deleteSearch({ listName = "default", searchId }) {
if (!searchId) {
throw Error("searchId must be provided");
}
const response = await fetch(
`${this.baseUrl}/list/${listName}/${searchId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${getToken("user")}`
}
}
);
if (response.status !== 204) {
throw Error(response.status);
}
}
}
export default FollowSearches;