-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.js
74 lines (67 loc) · 1.98 KB
/
helpers.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
//// Core modules
//// External modules
const axios = require('axios');
//// Modules
/**
* Authorize API calls
*
* @private
* @param {string} authType
* @param {string} tokenKeyOrUsername
* @param {string} password
* @param {string} contentType
* @returns {Object} Instance of axios
* @throws {Error}
*/
function authorize(authType, tokenKeyOrUsername, password = '', contentType = 'json') {
if (!['App', 'Basic', 'IBSSO'].includes(authType)) {
throw new Error('Invalid authorization type.')
}
if (authType === 'Basic') {
tokenKeyOrUsername = Buffer.from(`${tokenKeyOrUsername}:${password}`).toString('base64')
}
let accept = 'application/json'
if (contentType === 'xml') {
accept = 'application/xml'
}
return axios.create({
headers: {
'Authorization': `${authType} ${tokenKeyOrUsername}`,
'Content-Type': accept,
'Accept': accept
}
});
}
/**
* Axios returns a lengthy error message. Trim it down to just the result data.
*
* @private
* @param {*} error
* @returns {*}
*/
function trimError(error) {
let errorValue = error;
if (error instanceof Error) {
if (error.response) { // This is axios
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
if (error.response.data) {
errorValue = error.response.data
} else {
errorValue = error.response
}
} else if (error.request) { // This is axios
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
errorValue = error.request
} else { // Regular error
errorValue = error;
}
}
return errorValue
}
module.exports = {
authorize: authorize,
trimError: trimError
}