• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FAIRsharing / fairsharing.github.io / 24769584990

22 Apr 2026 08:57AM UTC coverage: 95.892% (-4.1%) from 100.0%
24769584990

push

github

web-flow
Merge pull request #2746 from FAIRsharing/dev

Dev

3653 of 3813 branches covered (95.8%)

Branch coverage included in aggregate %.

17344 of 18229 new or added lines in 282 files covered. (95.15%)

766 existing lines in 50 files now uncovered.

38617 of 40268 relevant lines covered (95.9%)

5.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.01
/src/lib/Client/RESTClient.js
1
import axios from "axios";
1✔
2

3
//import {toBase64} from "@/utils/generalUtils"
1✔
4
class RESTClient {
1✔
5
  /**
1✔
6
   * The RESTClient is a singleton class that handles the connection and data exchange from the back-end
1✔
7
   * REST API.
1✔
8
   */
1✔
9
  constructor() {
1✔
10
    if (RESTClient._instance) {
251✔
11
      return RESTClient._instance;
150✔
12
    }
150✔
13
    RESTClient._instance = this;
101✔
14
    this.baseURL = import.meta.env.VITE_API_ENDPOINT;
101✔
15
    this.headers = {
101✔
16
      Accept: "application/json",
101✔
17
      "Content-Type": "application/json",
101✔
18
      "Cache-Control": "no-cache",
101✔
19
    };
101✔
20
    this.headers["X-Client-Id"] = import.meta.env.VITE_CLIENT_ID;
101✔
21
  }
251✔
22

23
  /* USERS: all methods below related to handling user authentication */
1✔
24

25
  /**
1✔
26
   * Method to add authorisation to the headers.
1✔
27
   * @param jwt - the user's json web token
1✔
28
   * @return {JSON} - headers with authorisation field added
1✔
29
   */
1✔
30
  auth_headers(jwt) {
1✔
31
    let headers = JSON.parse(JSON.stringify(this.headers));
136✔
32
    headers["Authorization"] = "Bearer " + jwt;
136✔
33
    return headers;
136✔
34
  }
136✔
35

36
  /**
1✔
37
   * Method to log in the user
1✔
38
   * @param username - name of the user
1✔
39
   * @param password - password of the user
1✔
40
   * @return {Promise} - the response of the server
1✔
41
   */
1✔
42
  async login(username, password) {
1✔
43
    const endpoint = "/users/sign_in";
10✔
44
    const body = {
10✔
45
      user: {
10✔
46
        login: username,
10✔
47
        password: password,
10✔
48
      },
10✔
49
    };
10✔
50
    const request = {
10✔
51
      method: "post",
10✔
52
      baseURL: this.baseURL + endpoint,
10✔
53
      data: body,
10✔
54
      headers: this.headers,
10✔
55
    };
10✔
56
    let response = await this.executeQuery(request);
10✔
57
    return response.data;
10✔
58
  }
10✔
59

60
  /**
1✔
61
   * Logout the user from the back, expiring the current jwt.
1✔
62
   * @param {String} jwt - the user token to expire.
1✔
63
   * @returns {Promise}
1✔
64
   */
1✔
65
  async logout(jwt) {
1✔
66
    const request = {
3✔
67
      method: "delete",
3✔
68
      baseURL: this.baseURL + "/users/sign_out",
3✔
69
      headers: this.auth_headers(jwt),
3✔
70
    };
3✔
71
    let response = await this.executeQuery(request);
3✔
72
    return response.data;
3✔
73
  }
3✔
74

75
  /**
1✔
76
   * Delete the user's account (they must be logged in).
1✔
77
   * @param {String} jwt - the user token to expire.
1✔
78
   * @returns {Promise}
1✔
79
   */
1✔
80
  async delete(jwt) {
1✔
81
    const request = {
2✔
82
      method: "delete",
2✔
83
      baseURL: this.baseURL + "/users",
2✔
84
      headers: this.auth_headers(jwt),
2✔
85
    };
2✔
86
    let response = await this.executeQuery(request);
2✔
87
    return response.data;
2✔
88
  }
2✔
89

90
  /**
1✔
91
   *  Method to create a new user
1✔
92
   * @param {Object} userLogin - the user account to create
1✔
93
   * @returns {Promise} response - server response
1✔
94
   */
1✔
95
  async createAccount(userLogin) {
1✔
96
    const request = {
1✔
97
      method: "post",
1✔
98
      baseURL: this.baseURL + "/users",
1✔
99
      data: { user: userLogin },
1✔
100
      headers: this.headers,
1✔
101
    };
1✔
102
    let response = await this.executeQuery(request);
1✔
103
    return response.data;
1✔
104
  }
1✔
105

106
  /**
1✔
107
   * Validate the account given the corresponding token
1✔
108
   * @param {String} token - the account token to validate
1✔
109
   * @returns {Promise}
1✔
110
   */
1✔
111
  async confirmAccount(token) {
1✔
112
    const request = {
5✔
113
      method: "get",
5✔
114
      baseURL: this.baseURL + "/users/confirmation?confirmation_token=" + token,
5✔
115
      headers: this.headers,
5✔
116
    };
5✔
117
    let response = await this.executeQuery(request);
5✔
118
    return response.data;
5✔
119
  }
5✔
120

121
  /**
1✔
122
   * Method to send a reset password link to the given email address
1✔
123
   * @param {String} email to send the link to
1✔
124
   * @returns {Promise}
1✔
125
   */
1✔
126
  async requestResetPwd(email) {
1✔
127
    const request = {
1✔
128
      method: "post",
1✔
129
      baseURL: this.baseURL + "/users/password",
1✔
130
      headers: this.headers,
1✔
131
      data: {
1✔
132
        user: { email: email },
1✔
133
      },
1✔
134
    };
1✔
135
    let response = await this.executeQuery(request);
1✔
136
    return response.data;
1✔
137
  }
1✔
138

139
  /**
1✔
140
   * Resend the validation link for a given user
1✔
141
   * @param {Object} user - contains the email of the user.
1✔
142
   * @returns {Promise}
1✔
143
   */
1✔
144
  async resendConfirmation(user) {
1✔
145
    const request = {
1✔
146
      method: "post",
1✔
147
      baseURL: this.baseURL + "/users/confirmation",
1✔
148
      headers: this.headers,
1✔
149
      data: { user: user },
1✔
150
    };
1✔
151
    let response = await this.executeQuery(request);
1✔
152
    return response.data;
1✔
153
  }
1✔
154

155
  /**
1✔
156
   * Reset the password of the given user
1✔
157
   * @param {Object} user - contains the new pwd, repeated pwd and token.
1✔
158
   * @returns {Promise}
1✔
159
   */
1✔
160
  async resetPassword(user) {
1✔
161
    const request = {
4✔
162
      method: "put",
4✔
163
      baseURL: this.baseURL + "/users/password",
4✔
164
      headers: this.headers,
4✔
165
      data: { user: user },
4✔
166
    };
4✔
167
    let response = await this.executeQuery(request);
4✔
168
    return response.data;
4✔
169
  }
4✔
170

171
  /**
1✔
172
   * Changes the password of the logged in user
1✔
173
   * @param {String} jwt - the user token
1✔
174
   * @param {Object} user - contains the current, new and repeated new password
1✔
175
   * @returns {Promise}
1✔
176
   */
1✔
177
  async resetPasswordWithoutToken(jwt, user) {
1✔
178
    const request = {
2✔
179
      method: "put",
2✔
180
      baseURL: this.baseURL + "/users/",
2✔
181
      headers: this.auth_headers(jwt),
2✔
182
      data: { user: user },
2✔
183
    };
2✔
184
    let response = await this.executeQuery(request);
2✔
185
    return response.data;
2✔
186
  }
2✔
187

188
  /**
1✔
189
   * Verify the validity of the given password
1✔
190
   * @param {String} password - the password to test
1✔
191
   * @returns {Promise}
1✔
192
   */
1✔
193
  async verifyPassword(password) {
1✔
194
    let headers = JSON.parse(JSON.stringify(this.headers));
3✔
195
    const request = {
3✔
196
      method: "post",
3✔
197
      baseURL: this.baseURL + "/users/check_password",
3✔
198
      headers: headers,
3✔
199
      data: { password: password },
3✔
200
    };
3✔
201
    let response = await this.executeQuery(request);
3✔
202
    return response.data;
3✔
203
  }
3✔
204

205
  /**
1✔
206
   * Get the current user data
1✔
207
   * @param jwt
1✔
208
   * @returns {Promise}
1✔
209
   */
1✔
210
  async getUser(jwt) {
1✔
211
    const request = {
18✔
212
      method: "get",
18✔
213
      baseURL: this.baseURL + "/users/edit",
18✔
214
      headers: this.auth_headers(jwt),
18✔
215
    };
18✔
216
    let response = await this.executeQuery(request);
18✔
217
    return response.data;
18✔
218
  }
18✔
219

220
  /**
1✔
221
   * Get the current user data
1✔
222
   * @param jwt
1✔
223
   * @param id
1✔
224
   * @returns {Promise}
1✔
225
   */
1✔
226
  async getPublicUser(jwt, id) {
1✔
227
    const request = {
1✔
228
      method: "get",
1✔
229
      baseURL: this.baseURL + `/user_admin/${id}`,
1✔
230
      headers: this.auth_headers(jwt),
1✔
231
    };
1✔
232
    let response = await this.executeQuery(request);
1✔
233
    return response.data;
1✔
234
  }
1✔
235

236
  /**
1✔
237
   * Get all users list
1✔
238
   * @param jwt
1✔
239
   * @returns {Promise}
1✔
240
   */
1✔
241
  async getUsersList(jwt) {
1✔
242
    const request = {
1✔
243
      method: "get",
1✔
244
      baseURL: this.baseURL + "/user_admin/",
1✔
245
      headers: this.auth_headers(jwt),
1✔
246
    };
1✔
247
    let response = await this.executeQuery(request);
1✔
248
    return response.data;
1✔
249
  }
1✔
250

251
  /**
1✔
252
   * Edit the current logged in user profile
1✔
253
   * @param {Object} newUser - the new values for the logged in user
1✔
254
   * @param {String} jwt - JWT of the logged in user
1✔
255
   * @returns {Promise}
1✔
256
   */
1✔
257
  async editUser(newUser, jwt) {
1✔
258
    const request = {
4✔
259
      method: "put",
4✔
260
      baseURL: this.baseURL + "/users",
4✔
261
      headers: this.auth_headers(jwt),
4✔
262
      data: { user: newUser },
4✔
263
    };
4✔
264
    let response = await this.executeQuery(request);
4✔
265
    return response.data;
4✔
266
  }
4✔
267
  /**
1✔
268
   * Edit the current logged in user profile
1✔
269
   * @param {Object} newUser - the new values for the logged in user
1✔
270
   * @param {String} jwt - JWT of the logged in user
1✔
271
   * @returns {Promise}
1✔
272
   */
1✔
273
  async editPublicUser(newUser, jwt) {
1✔
274
    const request = {
2✔
275
      method: "put",
2✔
276
      baseURL: this.baseURL + `/user_admin/${newUser.id}`,
2✔
277
      headers: this.auth_headers(jwt),
2✔
278
      data: { user: newUser },
2✔
279
    };
2✔
280
    let response = await this.executeQuery(request);
2✔
281
    return response.data;
2✔
282
  }
2✔
283
  /**
1✔
284
   * Delete the user
1✔
285
   * @param userID
1✔
286
   * @param {String} jwt - JWT of the logged in user
1✔
287
   * @returns {Promise}
1✔
288
   */
1✔
289
  async deletePublicUser(userID, jwt) {
1✔
290
    const request = {
2✔
291
      method: "delete",
2✔
292
      baseURL: this.baseURL + `/user_admin/${userID}`,
2✔
293
      headers: this.auth_headers(jwt),
2✔
294
    };
2✔
295
    let response = await this.executeQuery(request);
2✔
296
    return response.data;
2✔
297
  }
2✔
298
  /**
1✔
299
   * Verify that the given JWT is still valid
1✔
300
   * @param {String} jwt - the token to validate
1✔
301
   * @returns {Promise}
1✔
302
   */
1✔
303
  async validateToken(jwt) {
1✔
304
    const request = {
2✔
305
      method: "get",
2✔
306
      baseURL: this.baseURL + "/users/valid",
2✔
307
      headers: this.auth_headers(jwt),
2✔
308
    };
2✔
309
    let response = await this.executeQuery(request);
2✔
310
    return response.data;
2✔
311
  }
2✔
312

313
  /* EDITORS METHODS */
1✔
314

315
  /**
1✔
316
   * Post the given object to the API to create the corresponding record.
1✔
317
   * @param record
1✔
318
   * @param {String} jwt - JWT of the logged in user
1✔
319
   * @returns {Promise}
1✔
320
   */
1✔
321
  async createRecord(record, jwt) {
1✔
322
    const request = {
4✔
323
      method: "post",
4✔
324
      baseURL: this.baseURL + "/fairsharing_records",
4✔
325
      headers: this.auth_headers(jwt),
4✔
326
      data: { fairsharing_record: record },
4✔
327
    };
4✔
328
    let response = await this.executeQuery(request);
4✔
329
    return response.data;
4✔
330
  }
4✔
331

332
  /**
1✔
333
   * Update the given record
1✔
334
   * @param {Object} record - the record to update containing the ID to target, the new values
1✔
335
   * and the user token
1✔
336
   * @returns {Promise}
1✔
337
   */
1✔
338
  async updateRecord(record) {
1✔
339
    const request = {
34✔
340
      method: "put",
34✔
341
      baseURL: this.baseURL + "/fairsharing_records/" + record.id,
34✔
342
      headers: this.auth_headers(record.token),
34✔
343
      data: { fairsharing_record: record.record },
34✔
344
    };
34✔
345
    let response = await this.executeQuery(request);
34✔
346
    return response.data;
34✔
347
  }
34✔
348

349
  /**
1✔
350
   * Determine if a user has permission to edit this record.
1✔
351
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
352
   * @param {String} userToken - JWT of the logged in user
1✔
353
   * @returns {Promise}
1✔
354
   */
1✔
355
  async canEdit(recordID, userToken) {
1✔
356
    const request = {
6✔
357
      method: "get",
6✔
358
      baseURL: this.baseURL + "/fairsharing_records/can_edit/" + recordID,
6✔
359
      headers: this.auth_headers(userToken),
6✔
360
    };
6✔
361
    let response = await this.executeQuery(request);
6✔
362
    return response.data;
6✔
363
  }
6✔
364

365
  /**
1✔
366
   * Attempt to create a MaintenanceRequest for a user for a FairsharingRecord.
1✔
367
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
368
   * @param {String} userToken - JWT of the logged in user
1✔
369
   * @returns {Promise}
1✔
370
   */
1✔
371
  async claimRecord(recordID, userToken) {
1✔
372
    const request = {
1✔
373
      method: "post",
1✔
374
      baseURL: this.baseURL + "/maintenance_requests",
1✔
375
      headers: this.auth_headers(userToken),
1✔
376
      data: { maintenance_request: { fairsharing_record_id: recordID } },
1✔
377
    };
1✔
378
    let response = await this.executeQuery(request);
1✔
379
    return response.data;
1✔
380
  }
1✔
381

382
  /**
1✔
383
   * Determine if a user has permission to create a MaintenanceRequest for a FairsharingRecord.
1✔
384
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
385
   * @param {String} userToken - JWT of the logged in user
1✔
386
   * @returns {Promise}
1✔
387
   */
1✔
388
  async canClaim(recordID, userToken) {
1✔
389
    const request = {
1✔
390
      method: "get",
1✔
391
      baseURL: this.baseURL + "/maintenance_requests/existing/" + recordID,
1✔
392
      headers: this.auth_headers(userToken),
1✔
393
    };
1✔
394
    let response = await this.executeQuery(request);
1✔
395
    return response.data;
1✔
396
  }
1✔
397

398
  /**
1✔
399
   * Remove an existing maintainer from a FairsharingRecord.
1✔
400
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
401
   * @param {String} userToken - JWT of the logged in user
1✔
402
   * @returns {Promise}
1✔
403
   */
1✔
404
  // TODO: Coverage steadfastly refuses to see this even though it is mocked and called
1✔
405
  // in a test (see Record.spec.js).
1✔
406
  /* istanbul ignore next */
1✔
407
  async removeMaintainer(recordID, userToken) {
1✔
UNCOV
408
    const request = {
×
UNCOV
409
      method: "post",
×
UNCOV
410
      baseURL: this.baseURL + "/maintenance_requests/remove",
×
UNCOV
411
      headers: this.auth_headers(userToken),
×
UNCOV
412
      data: { maintenance_request: { fairsharing_record_id: recordID } },
×
UNCOV
413
    };
×
UNCOV
414
    let response = await this.executeQuery(request);
×
UNCOV
415
    return response.data;
×
UNCOV
416
  }
×
417

418
  /**
1✔
419
   * Add or remove an watcher to/from a FairsharingRecord.
1✔
420
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
421
   * @param {String} operation - 'add' or 'remove'.
1✔
422
   * @param {String} userToken - JWT of the logged in user
1✔
423
   * @returns {Promise}
1✔
424
   */
1✔
425
  // TODO: Mocking this in a test (5c40b02ba3d78a49a088ab6dd5145c9ea10ea24b)
1✔
426
  // caused it to no longer be covered!
1✔
427
  /* istanbul ignore next */
1✔
428
  async changeWatcher(recordID, operation, userToken) {
1✔
UNCOV
429
    const request = {
×
UNCOV
430
      method: "post",
×
UNCOV
431
      baseURL: this.baseURL + "/fairsharing_records/watch",
×
UNCOV
432
      headers: this.auth_headers(userToken),
×
UNCOV
433
      data: {
×
UNCOV
434
        record_id: recordID,
×
UNCOV
435
        operation: operation,
×
UNCOV
436
      },
×
UNCOV
437
    };
×
UNCOV
438
    let response = await this.executeQuery(request);
×
UNCOV
439
    return response.data;
×
UNCOV
440
  }
×
441

442
  /**
1✔
443
   * Attempt to create a RecordReview for a user for a FairsharingRecord.
1✔
444
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
445
   * @param {String} userToken - JWT of the logged in user
1✔
446
   * @returns {Promise}
1✔
447
   */
1✔
448
  async reviewRecord(recordID, userToken) {
1✔
449
    const request = {
2✔
450
      method: "post",
2✔
451
      baseURL: this.baseURL + "/record_reviews",
2✔
452
      headers: this.auth_headers(userToken),
2✔
453
      data: { record_review: { fairsharing_record_id: recordID } },
2✔
454
    };
2✔
455
    let response = await this.executeQuery(request);
2✔
456
    return response.data;
2✔
457
  }
2✔
458

459
  /* USER DEFINED TAGS */
1✔
460

461
  /**
1✔
462
   * Create a new user defined tag in the database for users to tag their records.
1✔
463
   * @param {String} term - the string value of the term
1✔
464
   * @param {String} token - the user JWT
1✔
465
   * @returns {Promise}
1✔
466
   */
1✔
467
  async createNewUserDefinedTag(term, token) {
1✔
468
    const request = {
2✔
469
      method: "post",
2✔
470
      baseURL: this.baseURL + "/user_defined_tags",
2✔
471
      headers: this.auth_headers(token),
2✔
472
      data: { user_defined_tag: { label: term } },
2✔
473
    };
2✔
474
    let response = await this.executeQuery(request);
2✔
475
    return response.data;
2✔
476
  }
2✔
477

478
  /* LICENCES */
1✔
479

480
  /**
1✔
481
   * Create new a licence link
1✔
482
   * @param {Object} licenceLink - the licence link to create
1✔
483
   * @param {String} token - the user token
1✔
484
   * @returns {Promise}
1✔
485
   */
1✔
486
  async createLicenceLink(licenceLink, token) {
1✔
487
    let _client = this;
3✔
488
    const request = {
3✔
489
      method: "post",
3✔
490
      baseURL: _client.baseURL + "/licence_links",
3✔
491
      headers: this.auth_headers(token),
3✔
492
      data: { licence_link: licenceLink },
3✔
493
    };
3✔
494
    let response = await _client.executeQuery(request);
3✔
495
    return response.data;
3✔
496
  }
3✔
497

498
  /**
1✔
499
   * Delete the given licence
1✔
500
   * @param {Number} id - id of the licence link to delete
1✔
501
   * @param {String} token - the user token
1✔
502
   * @returns {Promise}
1✔
503
   */
1✔
504
  async deleteLicenceLink(id, token) {
1✔
505
    let _client = this;
1✔
506
    const request = {
1✔
507
      method: "delete",
1✔
508
      baseURL: _client.baseURL + "/licence_links/" + id,
1✔
509
      headers: this.auth_headers(token),
1✔
510
    };
1✔
511
    let response = await _client.executeQuery(request);
1✔
512
    return response.data;
1✔
513
  }
1✔
514

515
  /**
1✔
516
   * Update the licenceLink
1✔
517
   * @param {Object} licenceLink - the new values for the licence link
1✔
518
   * @param {String} token the user token
1✔
519
   * @returns {Promise}
1✔
520
   */
1✔
521
  async updateLicenceLink(licenceLink, token) {
1✔
522
    let _client = this;
1✔
523
    const request = {
1✔
524
      method: "put",
1✔
525
      baseURL: _client.baseURL + "/licence_links/" + licenceLink.id,
1✔
526
      headers: this.auth_headers(token),
1✔
527
      data: { licence_link: licenceLink },
1✔
528
    };
1✔
529
    let response = await _client.executeQuery(request);
1✔
530
    return response.data;
1✔
531
  }
1✔
532

533
  /* PUBLICATIONS */
1✔
534

535
  async createPublication(publication, token) {
1✔
536
    let _client = this;
2✔
537
    const request = {
2✔
538
      method: "post",
2✔
539
      baseURL: _client.baseURL + "/publications",
2✔
540
      headers: this.auth_headers(token),
2✔
541
      data: { publication: publication },
2✔
542
    };
2✔
543
    let response = await _client.executeQuery(request);
2✔
544
    return response.data;
2✔
545
  }
2✔
546

547
  async editPublication(publication, token) {
1✔
548
    let _client = this;
2✔
549
    const request = {
2✔
550
      method: "put",
2✔
551
      baseURL: _client.baseURL + "/publications/" + publication.id,
2✔
552
      headers: this.auth_headers(token),
2✔
553
      data: { publication: publication },
2✔
554
    };
2✔
555
    let response = await _client.executeQuery(request);
2✔
556
    return response.data;
2✔
557
  }
2✔
558

559
  /* RELATIONSHIPS BETWEEN RECORDS */
1✔
560
  /**
1✔
561
   * Saves the relationships as an array of items containing a targetID, a sourceID and a labelID
1✔
562
   * @param {Object} options - the options to pass as {token: String, relations: Array}
1✔
563
   * @returns {Promise}
1✔
564
   */
1✔
565
  async saveRelations(options) {
1✔
UNCOV
566
    if (options.relations.length > 0) {
×
UNCOV
567
      const request = {
×
UNCOV
568
        method: "put",
×
UNCOV
569
        baseURL: this.baseURL + "/fairsharing_records/" + options.target,
×
UNCOV
570
        headers: this.auth_headers(options.token),
×
UNCOV
571
        data: {
×
UNCOV
572
          fairsharing_record: {
×
UNCOV
573
            record_associations_attributes: options.relations,
×
UNCOV
574
          },
×
UNCOV
575
        },
×
UNCOV
576
      };
×
UNCOV
577
      let response = await this.executeQuery(request);
×
UNCOV
578
      return response.data;
×
UNCOV
579
    }
×
UNCOV
580
    return {};
×
UNCOV
581
  }
×
582

583
  /**
1✔
584
   * Deletes the relationships as an array of items containing a targetID, a sourceID and a labelID
1✔
585
   * @param {Object} options - the options to pass as {token: String, relations: Array}
1✔
586
   * @returns {Promise}
1✔
587
   */
1✔
588
  async deleteRelations(options) {
1✔
UNCOV
589
    if (options.relations.length > 0) {
×
UNCOV
590
      const request = {
×
UNCOV
591
        method: "put",
×
UNCOV
592
        baseURL: this.baseURL + "/fairsharing_records/" + options.target,
×
UNCOV
593
        headers: this.auth_headers(options.token),
×
UNCOV
594
        data: {
×
UNCOV
595
          fairsharing_record: {
×
UNCOV
596
            record_associations_attributes: options.relations,
×
UNCOV
597
          },
×
UNCOV
598
        },
×
UNCOV
599
      };
×
UNCOV
600
      let response = await this.executeQuery(request);
×
UNCOV
601
      return response.data;
×
UNCOV
602
    }
×
UNCOV
603
    return {};
×
UNCOV
604
  }
×
605

606
  /* ORGANISATIONS AND GRANTS */
1✔
607
  /**
1✔
608
   * Create a given organisation
1✔
609
   * @param {Object} organisation
1✔
610
   * @param {String} userToken - the user jwt
1✔
611
   * @returns {Promise}
1✔
612
   */
1✔
613
  async createOrganisation(organisation, userToken) {
1✔
614
    let _client = this;
5✔
615
    const request = {
5✔
616
      method: "post",
5✔
617
      baseURL: _client.baseURL + "/organisations",
5✔
618
      headers: this.auth_headers(userToken),
5✔
619
      data: { organisation: organisation },
5✔
620
    };
5✔
621
    let response = await _client.executeQuery(request);
5✔
622
    return response.data;
5✔
623
  }
5✔
624

625
  /**
1✔
626
   * Edit a given organisation
1✔
627
   * @param {Object} organisation
1✔
628
   * @param {Number} id
1✔
629
   * @param {String} userToken - the user jwt
1✔
630
   * @returns {Promise}
1✔
631
   */
1✔
632
  async editOrganisation(organisation, id, userToken) {
1✔
UNCOV
633
    let _client = this;
×
UNCOV
634
    const request = {
×
UNCOV
635
      method: "put",
×
UNCOV
636
      baseURL: _client.baseURL + "/organisations/" + id,
×
UNCOV
637
      headers: this.auth_headers(userToken),
×
UNCOV
638
      data: { organisation: organisation },
×
UNCOV
639
    };
×
UNCOV
640
    let response = await _client.executeQuery(request);
×
UNCOV
641
    return response.data;
×
UNCOV
642
  }
×
643

644
  /**
1✔
645
   * Delete the given organisation link
1✔
646
   * @param {Number} organisationId - the id of the link to remove
1✔
647
   * @param {String} userToken - the user jwt
1✔
648
   * @returns {Promise}
1✔
649
   */
1✔
650
  async deleteOrganisation(organisationId, userToken) {
1✔
UNCOV
651
    let _client = this;
×
UNCOV
652
    const request = {
×
UNCOV
653
      method: "delete",
×
UNCOV
654
      baseURL: _client.baseURL + "/organisations/" + organisationId,
×
UNCOV
655
      headers: this.auth_headers(userToken),
×
UNCOV
656
    };
×
UNCOV
657
    let response = await _client.executeQuery(request);
×
UNCOV
658
    return response.data;
×
UNCOV
659
  }
×
660

661
  /**
1✔
662
   * Create a given grant
1✔
663
   * @param {Object} grant
1✔
664
   * @param {String} userToken - the user jwt
1✔
665
   * @returns {Promise}
1✔
666
   */
1✔
667
  async createGrant(grant, userToken) {
1✔
668
    let _client = this;
2✔
669
    const request = {
2✔
670
      method: "post",
2✔
671
      baseURL: _client.baseURL + "/grants",
2✔
672
      headers: this.auth_headers(userToken),
2✔
673
      data: { grant: grant },
2✔
674
    };
2✔
675
    let response = await _client.executeQuery(request);
2✔
676
    return response.data;
2✔
677
  }
2✔
678

679
  /**
1✔
680
   * Create a new link between an organisation, a record and an optional grant.
1✔
681
   * @param {Object} organisationLink - the organisation link to create
1✔
682
   * @param {String} userToken - the user jwt
1✔
683
   * @returns {Promise}
1✔
684
   */
1✔
685
  async createOrganisationLink(organisationLink, userToken) {
1✔
UNCOV
686
    let _client = this;
×
UNCOV
687
    const request = {
×
UNCOV
688
      method: "post",
×
UNCOV
689
      baseURL: _client.baseURL + "/organisation_links",
×
UNCOV
690
      headers: this.auth_headers(userToken),
×
UNCOV
691
      data: { organisation_link: organisationLink },
×
UNCOV
692
    };
×
UNCOV
693
    let response = await _client.executeQuery(request);
×
UNCOV
694
    return response.data;
×
UNCOV
695
  }
×
696

697
  /**
1✔
698
   * Update the organisationLink given from linkID input with the given organisationLink
1✔
699
   * @param {Object} organisationLink - the new organisation link value
1✔
700
   * @param {Number} linkID - ID of the organisationLink to update
1✔
701
   * @param {String} userToken - the user jwt
1✔
702
   * @returns {Promise}
1✔
703
   */
1✔
704
  async updateOrganisationLink(organisationLink, linkID, userToken) {
1✔
705
    let _client = this;
2✔
706
    const request = {
2✔
707
      method: "put",
2✔
708
      baseURL: _client.baseURL + "/organisation_links/" + linkID,
2✔
709
      headers: this.auth_headers(userToken),
2✔
710
      data: { organisation_link: organisationLink },
2✔
711
    };
2✔
712
    let response = await _client.executeQuery(request);
2✔
713
    return response.data;
2✔
714
  }
2✔
715

716
  /**
1✔
717
   * Delete the given organisation link
1✔
718
   * @param {Number} linkID - the id of the link to remove
1✔
719
   * @param {String} userToken - the user jwt
1✔
720
   * @returns {Promise}
1✔
721
   */
1✔
722
  async deleteOrganisationLink(linkID, userToken) {
1✔
723
    let _client = this;
1✔
724
    const request = {
1✔
725
      method: "delete",
1✔
726
      baseURL: _client.baseURL + "/organisation_links/" + linkID,
1✔
727
      headers: this.auth_headers(userToken),
1✔
728
    };
1✔
729
    let response = await _client.executeQuery(request);
1✔
730
    return response.data;
1✔
731
  }
1✔
732

733
  /**
1✔
734
   * Get the extra metadata fields for a RecordType
1✔
735
   * @param {String} type - name of the record type.
1✔
736
   * @param {String} userToken - the user jwt
1✔
737
   * @returns {Promise}
1✔
738
   */
1✔
739
  async extraMetadataFields(type, userToken) {
1✔
740
    const request = {
11✔
741
      method: "post",
11✔
742
      baseURL: this.baseURL + "/fairsharing_records/metadata_fields",
11✔
743
      headers: this.auth_headers(userToken),
11✔
744
      data: { type: type },
11✔
745
    };
11✔
746
    let response = await this.executeQuery(request);
11✔
747
    return response.data;
11✔
748
  }
11✔
749

750
  /* METHODS FOR CURATION */
1✔
751
  /**
1✔
752
   * Update the maintenanceRequest given the new status value
1✔
753
   * @param {Number} maintenanceRequest  ID of the maintenanceRequest to update
1✔
754
   * @param {string} newStatus - new status to update
1✔
755
   * @param {String} userToken - the user jwt
1✔
756
   * @returns {Promise}
1✔
757
   */
1✔
758
  async updateStatusMaintenanceRequest(
1✔
UNCOV
759
    maintenanceRequest,
×
UNCOV
760
    newStatus,
×
NEW
761
    userToken,
×
UNCOV
762
  ) {
×
UNCOV
763
    let _client = this;
×
UNCOV
764
    const request = {
×
UNCOV
765
      method: "put",
×
UNCOV
766
      baseURL: _client.baseURL + "/maintenance_requests/" + maintenanceRequest,
×
UNCOV
767
      headers: this.auth_headers(userToken),
×
UNCOV
768
      data: { maintenance_request: { status: newStatus } },
×
UNCOV
769
    };
×
UNCOV
770
    let response = await _client.executeQuery(request);
×
UNCOV
771
    return response.data;
×
UNCOV
772
  }
×
773

774
  /**
1✔
775
   * Delete Record
1✔
776
   * @param {Number} id - id of the record link to delete
1✔
777
   * @param {String} token - the user token
1✔
778
   * @returns {Promise}
1✔
779
   */
1✔
780
  async deleteRecord(id, token) {
1✔
781
    let _client = this;
4✔
782
    const request = {
4✔
783
      method: "delete",
4✔
784
      baseURL: _client.baseURL + "/fairsharing_records/" + id,
4✔
785
      headers: this.auth_headers(token),
4✔
786
    };
4✔
787
    let response = await _client.executeQuery(request);
4✔
788
    return response.data;
4✔
789
  }
4✔
790

791
  /**
1✔
792
   * Get the list of allowed relation types for editing record's relationships.
1✔
793
   * @returns {Promise}
1✔
794
   */
1✔
795
  /* EDITOR DATA */
1✔
796
  async getRelationsTypes() {
1✔
UNCOV
797
    let _client = this;
×
UNCOV
798
    const request = {
×
UNCOV
799
      method: "get",
×
UNCOV
800
      baseURL: _client.baseURL + "/record_associations/allowed",
×
UNCOV
801
    };
×
UNCOV
802
    let response = await _client.executeQuery(request);
×
UNCOV
803
    return response.data;
×
UNCOV
804
  }
×
805

806
  /**
1✔
807
   * Get the list of available profile types for a user.
1✔
808
   * @returns {Promise}
1✔
809
   */
1✔
810
  async getProfileTypes() {
1✔
811
    const request = {
1✔
812
      method: "get",
1✔
813
      baseURL: this.baseURL + "/users/profile_types",
1✔
814
      headers: this.headers,
1✔
815
    };
1✔
816
    let response = await this.executeQuery(request);
1✔
817
    return response.data;
1✔
818
  }
1✔
819

820
  /**
1✔
821
   * Get the list of available roles for a user.
1✔
822
   * @returns {Promise}
1✔
823
   * @param {String} token - the user token
1✔
824
   */
1✔
825
  // Coverage steadfastly refuses to see this even though it is mocked and called
1✔
826
  // in a test (see EditPublicProfile.spec.js).
1✔
827
  /* istanbul ignore next */
1✔
828
  async getUserRoles(userToken) {
1✔
UNCOV
829
    const request = {
×
UNCOV
830
      method: "get",
×
UNCOV
831
      baseURL: this.baseURL + "/user_roles",
×
UNCOV
832
      headers: this.auth_headers(userToken),
×
UNCOV
833
    };
×
UNCOV
834
    let response = await this.executeQuery(request);
×
UNCOV
835
    return response.data;
×
UNCOV
836
  }
×
837

838
  /* SYSTEM MESSAGES */
1✔
839
  /**
1✔
840
   * Update the given message
1✔
841
   * @param {Object} message - the message to update containing the ID to target and the new values
1✔
842
   * @param {String} userToken - the user jwt
1✔
843
   * @returns {Promise}
1✔
844
   */
1✔
845
  async updateMessage(message, userToken) {
1✔
846
    const request = {
2✔
847
      method: "put",
2✔
848
      baseURL: this.baseURL + "/messages/" + message.id,
2✔
849
      headers: this.auth_headers(userToken),
2✔
850
      data: { message: message },
2✔
851
    };
2✔
852
    let response = await this.executeQuery(request);
2✔
853
    return response.data;
2✔
854
  }
2✔
855

856
  /**
1✔
857
   * Update the given message
1✔
858
   * @param {Object} message - the message to create
1✔
859
   * @param {String} userToken - the user jwt
1✔
860
   * @returns {Promise}
1✔
861
   */
1✔
862
  async createMessage(message, userToken) {
1✔
863
    const request = {
2✔
864
      method: "post",
2✔
865
      baseURL: this.baseURL + "/messages/",
2✔
866
      headers: this.auth_headers(userToken),
2✔
867
      data: { message: message },
2✔
868
    };
2✔
869
    let response = await this.executeQuery(request);
2✔
870
    return response.data;
2✔
871
  }
2✔
872

873
  /**
1✔
874
   * Update the given message
1✔
875
   * @param {String} messageId - the ID of the message to delete
1✔
876
   * @param {String} userToken - the user jwt
1✔
877
   * @returns {Promise}
1✔
878
   */
1✔
879
  async deleteMessage(messageId, userToken) {
1✔
880
    const request = {
2✔
881
      method: "delete",
2✔
882
      baseURL: this.baseURL + "/messages/" + messageId,
2✔
883
      headers: this.auth_headers(userToken),
2✔
884
    };
2✔
885
    let response = await this.executeQuery(request);
2✔
886
    return response.data;
2✔
887
  }
2✔
888

889
  /* EXTRA METHODS */
1✔
890

891
  /**
1✔
892
   * Trigger the given query with Axios
1✔
893
   * @param query
1✔
894
   * @returns {Promise<*>}
1✔
895
   */
1✔
896
  async executeQuery(query) {
1✔
897
    try {
1✔
898
      return await axios(query);
1✔
899
    } catch (e) {
1✔
900
      return { data: { error: e } };
1✔
901
    }
1✔
902
  }
1✔
903

904
  /**
1✔
905
   * Get records without DOIS
1✔
906
   * @param {String} token - the user token that needs to be a curator
1✔
907
   * @returns {Promise}
1✔
908
   */
1✔
909
  async getRecordsWoDOIs(userToken) {
1✔
UNCOV
910
    let _client = this;
×
UNCOV
911
    const request = {
×
UNCOV
912
      method: "get",
×
UNCOV
913
      baseURL: _client.baseURL + "/files/no_dois",
×
UNCOV
914
      headers: this.auth_headers(userToken),
×
UNCOV
915
    };
×
UNCOV
916
    let response = await _client.executeQuery(request);
×
UNCOV
917
    return response.data;
×
UNCOV
918
  }
×
919

920
  /**
1✔
921
   * Get count of records created by month
1✔
922
   * @param {String} token - the user token that needs to be a curator
1✔
923
   * @returns {Promise}
1✔
924
   */
1✔
925
  async getRecordCreatedByMonth(userToken) {
1✔
UNCOV
926
    let _client = this;
×
UNCOV
927
    const request = {
×
UNCOV
928
      method: "get",
×
UNCOV
929
      baseURL: _client.baseURL + "/files/creation_dates",
×
UNCOV
930
      headers: this.auth_headers(userToken),
×
UNCOV
931
    };
×
UNCOV
932
    let response = await _client.executeQuery(request);
×
UNCOV
933
    return response.data;
×
UNCOV
934
  }
×
935

936
  /**
1✔
937
   * Get count of edits by month
1✔
938
   * @param {String} token - the user token that needs to be a curator
1✔
939
   * @returns {Promise}
1✔
940
   */
1✔
941
  async getEditByMonth(userToken) {
1✔
UNCOV
942
    let _client = this;
×
UNCOV
943
    const request = {
×
UNCOV
944
      method: "get",
×
UNCOV
945
      baseURL: _client.baseURL + "/files/edit_dates",
×
UNCOV
946
      headers: this.auth_headers(userToken),
×
UNCOV
947
    };
×
UNCOV
948
    let response = await _client.executeQuery(request);
×
UNCOV
949
    return response.data;
×
UNCOV
950
  }
×
951
  /**
1✔
952
   * Get Zenodo call
1✔
953
   * @param {String} call
1✔
954
   * @param {String} userToken - the user jwt
1✔
955
   * @returns {Promise}
1✔
956
   */
1✔
957
  async getZenodoSearch(text, userToken) {
1✔
958
    let _client = this;
7✔
959
    const request = {
7✔
960
      method: "post",
7✔
961
      baseURL: _client.baseURL + "/zenodo",
7✔
962
      headers: this.auth_headers(userToken),
7✔
963
      data: { doi: text },
7✔
964
    };
7✔
965
    let response = await _client.executeQuery(request);
7✔
966
    return response.data;
7✔
967
  }
7✔
968

969
  // This is covered in BaseFields.spec.js but doesn't show in coverage.
1✔
970
  /* istanbul ignore next */
1✔
971
  async clearLogo(id, token) {
1✔
UNCOV
972
    let _client = this;
×
UNCOV
973
    const request = {
×
UNCOV
974
      method: "put",
×
UNCOV
975
      baseURL: _client.baseURL + "/fairsharing_records/" + id,
×
UNCOV
976
      headers: _client.auth_headers(token),
×
UNCOV
977
      data: {
×
UNCOV
978
        fairsharing_record: {
×
UNCOV
979
          logo: {},
×
UNCOV
980
        },
×
UNCOV
981
      },
×
UNCOV
982
    };
×
UNCOV
983
    let response = await _client.executeQuery(request);
×
UNCOV
984
    // here the data returned always should be either an array of images or one string of image
×
UNCOV
985
    return response.data;
×
UNCOV
986
  }
×
987

988
  /**
1✔
989
   * Get statistics data without users
1✔
990
   * @returns {Promise}
1✔
991
   */
1✔
992
  async getStatisticsData() {
1✔
993
    let _client = this;
4✔
994
    const request = {
4✔
995
      method: "get",
4✔
996
      baseURL: _client.baseURL + "/homepage_stats",
4✔
997
    };
4✔
998
    let response = await _client.executeQuery(request);
4✔
999
    return response.data;
4✔
1000
  }
4✔
1001

1002
  /**
1✔
1003
   * Get JSON+LD for homepage.
1✔
1004
   * @returns {Promise}
1✔
1005
   */
1✔
1006
  async getHomepageJsonld() {
1✔
1007
    let _client = this;
3✔
1008
    const request = {
3✔
1009
      method: "get",
3✔
1010
      baseURL: _client.baseURL + "/homepage_jsonld",
3✔
1011
    };
3✔
1012
    let response = await _client.executeQuery(request);
3✔
1013
    return response.data;
3✔
1014
  }
3✔
1015

1016
  /**
1✔
1017
   * Send the verification link for a given user
1✔
1018
   * @param {Object} user - contains the email, uid, identifier of the user.
1✔
1019
   * @returns {Promise}
1✔
1020
   */
1✔
1021
  async sendOrcidVerification(user) {
1✔
1022
    let _client = this;
1✔
1023
    const request = {
1✔
1024
      method: "post",
1✔
1025
      baseURL: this.baseURL + "/users/auth/identify_user",
1✔
1026
      headers: this.auth_headers(user),
1✔
1027
      data: { user: user },
1✔
1028
    };
1✔
1029
    let response = await _client.executeQuery(request);
1✔
1030
    return response.data;
1✔
1031
  }
1✔
1032

1033
  /**
1✔
1034
   * Save the advancedSearch results to the user
1✔
1035
   * @param {Object} saveSearchObj - save search object having required parameters
1✔
1036
   * @param {String} jwt - the user jwt
1✔
1037
   * @returns {Promise}
1✔
1038
   */
1✔
1039
  /* Ignoring because of 401 access denied in test case */
1✔
1040
  /* istanbul ignore next */
1✔
1041
  async saveSearch(saveSearchObj, jwt) {
1✔
UNCOV
1042
    let _client = this;
×
UNCOV
1043
    const request = {
×
UNCOV
1044
      method: "post",
×
UNCOV
1045
      baseURL: _client.baseURL + "/saved_searches",
×
UNCOV
1046
      headers: this.auth_headers(jwt),
×
UNCOV
1047
      data: { saved_search: saveSearchObj },
×
UNCOV
1048
    };
×
UNCOV
1049
    let response = await _client.executeQuery(request);
×
UNCOV
1050
    return response.data;
×
UNCOV
1051
  }
×
1052

1053
  /**
1✔
1054
   * Delete the saved search
1✔
1055
   * @param {Number} savedSearchId - the id of the savedSearch to remove
1✔
1056
   * @param {String} userToken - the user jwt
1✔
1057
   * @returns {Promise}
1✔
1058
   */
1✔
1059
  /* Ignoring because of 401 access denied in test case */
1✔
1060
  /* istanbul ignore next */
1✔
1061
  async deleteSavedSearch(savedSearchId, jwt) {
1✔
UNCOV
1062
    let _client = this;
×
UNCOV
1063
    const request = {
×
UNCOV
1064
      method: "delete",
×
UNCOV
1065
      baseURL: _client.baseURL + "/saved_searches/" + savedSearchId,
×
UNCOV
1066
      headers: this.auth_headers(jwt),
×
UNCOV
1067
    };
×
UNCOV
1068
    let response = await _client.executeQuery(request);
×
UNCOV
1069
    return response.data;
×
UNCOV
1070
  }
×
1071

1072
  /**
1✔
1073
   * Update the saved search
1✔
1074
   * @param {Number} saveSearchId - save search id
1✔
1075
   * @param {Object} saveSearchObj - save search object having required parameters
1✔
1076
   * @param {String} jwt the user token
1✔
1077
   * @returns {Promise}
1✔
1078
   */
1✔
1079
  /* Ignoring because of 401 access denied in test case */
1✔
1080
  /* istanbul ignore next */
1✔
1081
  async updateSaveSearch(saveSearchId, saveSearchObj, jwt) {
1✔
UNCOV
1082
    let _client = this;
×
UNCOV
1083
    const request = {
×
UNCOV
1084
      method: "put",
×
UNCOV
1085
      baseURL: _client.baseURL + "/saved_searches/" + saveSearchId,
×
UNCOV
1086
      headers: this.auth_headers(jwt),
×
UNCOV
1087
      data: { saved_search: saveSearchObj },
×
UNCOV
1088
    };
×
UNCOV
1089
    let response = await _client.executeQuery(request);
×
UNCOV
1090
    return response.data;
×
UNCOV
1091
  }
×
1092
}
1✔
1093

1094
export default RESTClient;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc