-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeoplehr-api.js
57 lines (51 loc) · 1.37 KB
/
peoplehr-api.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
'use strict';
const url = 'https://api.peoplehr.net';
let APIKey = '';
let fetch;
class PeopleHR {
/**
* Create an instance
*
* @param {string} apiKey - obtained from the peoplehr web admin
* @param {(function|Object)} _fetch - optional use to override fetch
*/
constructor(apiKey, _fetch = null) {
if (apiKey && typeof apiKey === 'string') {
APIKey = apiKey;
} else if (process.env.hasOwnProperty('PEOPLEHR_API_KEY')) {
APIKey = process.env.PEOPLEHR_API_KEY;
} else if (process.env.hasOwnProperty('REACT_APP_PEOPLEHR_API_KEY')) {
APIKey = process.env.REACT_APP_PEOPLEHR_API_KEY;
} else {
throw new Error('No apiKey set, see readme.');
}
fetch =
_fetch && typeof _fetch === 'function' ? _fetch : require('node-fetch');
}
/**
* Returns a Promise
*
* @param {string} endpoint
* @param {string} action
* @param {Object} params
* @returns {Promise} JSON object
*/
async query(endpoint, action, params = {}) {
try {
const response = await fetch(`${url}/${endpoint}`, {
method: 'POST',
body: JSON.stringify({
...params,
Action: action,
APIKey
})
});
const json = await response.json();
return json;
} catch (error) {
return error;
}
}
}
module.exports = PeopleHR;
module.exports.default = module.exports;