-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPClient.js
More file actions
190 lines (167 loc) · 6.23 KB
/
HTTPClient.js
File metadata and controls
190 lines (167 loc) · 6.23 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Provides HTTP-connection functionality.
*/
export class HTTPClient {
#host = "http://localhost:6050/";
constructor() {
/**
* The default return value for PUT requests.
*/
this.NULL_MAP = { map: {} };
this.currentLibrary = "demo";
}
/**
* Sends a HTTP-request to JabRef's server.
* @param { string } url - The server's URL to make a request to.
* @param { object } options - Optional request's options.
* @returns
* - An **object** in case of a `GET request` or
* - An **object** { map: {} } in case of a `PUT / POST request`
* or if any request failed.
*/
async #performRequest(url, options = null) {
let result = this.NULL_MAP;
let logMessage = "";
const requestUrl = this.#host.concat(url);
// Setting default options if none's been provided
// * Note: this includes only cases when options are null or undefined
options = options ??
{
method: "PUT",
headers: { "Content-Type": "application/json" }
};
// Sending the request and waiting for the response
try {
const response = await fetch(requestUrl, options);
if (!response.ok) {
throw new Error("Request's result is not ok ( -.-)");
}
// If some output is awaited, save it
if (options.method !== "PUT") {
if (options.headers["Content-Type"] === 'application/json') {
result = await response.json();
}
if (options.headers["Accept"] === 'text/plain') {
result = await response.text();
}
if (options.headers["Accept"] === 'text/html') {
result = await response.text();
}
}
// Providing infos about the request
logMessage =
`${options.method} ${requestUrl} Request succeeded (~ UwU)~(${response.status}).\n` +
`Output:\n` +
`${JSON.stringify(result, null, 2)}`;
} catch (e) {
// Logging basic information about the error
console.error(e);
logMessage = `${options.method} ${requestUrl} Request failed (.'T_T)`;
// If connection was present, provide more details
if (typeof (response) !== "undefined") {
logMessage +=
`-(${response.status}).\n` +
`Options:\n` +
`${options}`;
}
}
// Finally showing the resulting log
console.log(logMessage);
return result;
}
/**
* Requests a mind map (.jmp file) from JabRef's server.
* @param { string } library - The library of the requested mind map.
* @returns The requested mind map object.
*/
async loadMap(library = "demo") {
const url = `libraries/${library}/map`;
const options = {
method: "GET",
headers: { "Content-Type": "application/json" }
}
// Changing current library
this.currentLibrary = library;
console.log(`Current library is now: ${this.currentLibrary}`);
return this.#performRequest(url, options);
}
/**
* Sends a mind map to JabRef's server to save next to currently active library.
* @param { object } mindMap - The mind map to save.
* @returns An empty map object (NULL_MAP).
*/
async saveMap(mindMap) {
const url = `libraries/${this.currentLibrary}/map`;
const options = {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ map: mindMap })
}
return this.#performRequest(url, options)
}
/**
* Requests a list of stored mind maps saved on the server.
* @returns A list of available mind maps stored on the server.
*/
async listMaps() {
const url = 'libraries'
const options = {
method: "GET",
headers: { "Content-Type": "application/json" }
}
return this.#performRequest(url, options)
}
/**
* Requests a list of all entries in the current library.
* @returns A list of all entries in the current library in json format.
*/
async listEntries() {
const options = {
method: "GET",
headers: { "Content-Type": "application/json" }
}
// TODO - format the list to contain the keys, titles, authors and releases of the entries and return it
return this.#performRequest(this.currentLibrary, options)
}
/**
* Sends a request to open a cite-as-you-write window
* to select saved citation keys.
* @returns A list of selected citation keys.
*/
async getCiteKeysWithCAYW() {
const url = 'better-bibtex/cayw';
const options = {
method: "GET",
headers: { "Content-Type": "application/json" }
}
return this.#performRequest(url, options)
}
/**
* Requests the preview for a certain BibEntry from the current library.
* @param { string } citationKey - The citation key (identifier) of the entry.
* @returns A string containing the preview with relevant information
* about the entry (e.g. author, title, release date, etc.).
*/
async getPreviewString(citationKey) {
const url = `libraries/${this.currentLibrary}/entries/${citationKey}`;
const options = {
method: "GET",
headers: { "Accept": "text/plain" }
}
return this.#performRequest(url, options);
}
/**
* Requests the preview for a certain BibEntry from the current library.
* @param { string } citationKey - The citation key (identifier) of the entry.
* @returns A string containing the preview with relevant information
* about the entry (e.g. author, title, release date, etc.).
*/
async getPreviewHTML(citationKey) {
const url = `libraries/${this.currentLibrary}/entries/${citationKey}`;
const options = {
method: "GET",
headers: { "Accept": "text/html" }
}
return this.#performRequest(url, options);
}
}