-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
50 lines (43 loc) · 1.12 KB
/
index.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
const axios = require('axios');
const stringifyDates = require('./stringifyDates');
class Forecast {
constructor({ accountId, token } = {}, instance) {
if (!accountId || !token) {
throw new Error(
'Forecast module requires accountId and token to be configured.'
);
}
const baseURL = 'https://api.forecastapp.com/';
const headers = {
Authorization: `Bearer ${token}`,
'Forecast-Account-Id': accountId,
'User-Agent': 'https://www.npmjs.com/package/forecast-promise',
};
this.instance =
instance ||
axios.create({
baseURL,
headers,
});
const methods = [
['whoAmI', 'current_user'],
['clients'],
['people'],
['projects'],
['assignments'],
['milestones'],
['roles'],
];
methods.forEach(([name, dataLocation]) => {
const route = '/' + name.toLowerCase();
const prop = dataLocation || name;
this[name] = options =>
this._request(route, options).then(response => response.data[prop]);
});
}
_request(relativeUrl, options = {}) {
const params = stringifyDates(options);
return this.instance.get(relativeUrl, { params });
}
}
module.exports = Forecast;