-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsheets-api.js
232 lines (178 loc) · 6.03 KB
/
tsheets-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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const request = require('request');
const url = require('url');
const TSheetsApiError = require('./errors/tsheetsapi-error');
function sleep(s = 0) {
return new Promise(r => setTimeout(r, s * 1000));
}
/**
* A simple TSheetsApi handler that makes use of the Async/Await featues..
* Supports almost any endpoint and follows the TSheets method nomenclature.
* @param {object} params A init object that contains your bearerToken and optionally allows to set the API version & url used.
*/
class TSheetsApi{
createEndpoint(el, return_object_name) {
// Specialty named routes are assumed to only support HTTP POST via 'get' method
if (return_object_name) {
TSheetsApi.prototype[el.replace(/\//g, '_')] = () => {
return {
get : async (params) => {
return await this.build(el, 'POST', params, return_object_name);
}
}
}
}
else {
TSheetsApi.prototype[el] = () => {
return {
list : async (params) => {
return await this.build(el, 'GET', params);
},
add : async (params) => {
return await this.build(el, 'POST', params);
},
update : async (params) => {
return await this.build(el, 'PUT', params);
},
delete : async (params) => {
return await this.build(el, 'DELETE', params);
}
}
};
}
}
constructor(params){
const defaults = {
baseUrl : 'https://rest.tsheets.com/api',
version : 'v1',
bearerToken : ''
};
if(typeof params === "undefined"){
throw new TSheetsApiError('Please provide at least a Bearer Token.');
}else{
if(!params.hasOwnProperty('baseUrl')){
params['baseUrl'] = defaults.baseUrl;
}
if(!params.hasOwnProperty('version')){
params['version'] = defaults.version;
}
if(!params.hasOwnProperty('bearerToken') || typeof params['bearerToken'] === "undefined"){
throw new TSheetsApiError('Please provide an API key.');
}
}
this.baseUrl = `${params.baseUrl}/${params.version}`;
this.bearerToken = params.bearerToken;
const endpoints = ['users',
'groups',
'jobcodes',
'jobcode_assignments',
'timesheets',
'timesheets_deleted',
'geolocations',
'last_modified_timestamps',
'notifications',
'reminders',
'schedule_calendars',
'schedule_events',
'managed_clients',
{
'reports': [
{
name: 'current_totals',
return_object_name: 'current_totals_report'
},
{
name: 'payroll',
return_object_name: 'payroll_report'
},
{
name: 'payroll_by_jobcode',
return_object_name: 'payroll_by_jobcode_report'
},
{
name: 'project',
return_object_name: 'project_report'
}
]
}];
endpoints.forEach( el => {
if (typeof el === 'object') {
Object.keys(el).forEach( i => {
el[i].forEach( j => {
this.createEndpoint(`${i}/${j.name}`, j.return_object_name);
});
});
}
else {
this.createEndpoint(el);
}
});
}
/**
* Build request to API and execute it.
* @param {string} endpoint Can be one of the support TSheets endpoint such as 'users', 'jobcodes' or 'timesheets'
* @param {string} method HTTP request method
* @param {object} params Object that contains either the query string parameters or request body
* @returns {Promise}
*/
async build(endpoint, method, params, return_name) {
let queryObject = {
method : method,
url: `${this.baseUrl}/${endpoint}`,
auth : {
bearer : this.bearerToken
},
json : true
}
if(method === 'GET' && (typeof params === "undefined" || !params.hasOwnProperty('page')) && endpoint != 'last_modified_timestamps'){
params = typeof params === "undefined" ? {} : params;
params['page'] = 1;
}
if(method === 'GET' || method === 'DELETE') queryObject['qs'] = params;
if(method === 'POST' || method === 'PUT') queryObject['body'] = params;
return await this.doRequest(queryObject, endpoint, return_name);
}
/**
* Perform a request. Pagination is enabled by
* @param {object} queryObject The query parameters.
* @param {string} endpoint The relevant endpoint.
* @returns {object} res
* {
* data : [The response data gathered from TSheets],
* next : [null or a Promise to get the next batch]
* }
*/
async doRequest(queryObject, endpoint, return_name){
try{
var [data, supplementalData, hasNext] = await new Promise((resolve, reject) => {
request(queryObject, (err, res, body) => {
if(err){
return reject([err, 500]);
}
if (body.hasOwnProperty('error')){
return reject([body.error.message, body.error.code]);
}
const entries = body['results'][return_name || endpoint];
let result = Object.keys(entries).map(key => entries[key]);
if(body.hasOwnProperty("supplemental_data")){
return resolve([result, body["supplemental_data"], body["more"]]);
}
resolve([result, null, body["more"]]);
});
});
}catch(e){
let [,code] = e;
if(parseInt(code) === 429){
console.log("Got 'Too many requests' from TSheets. Let's wait 2min until next try...");
await sleep(120);
return await this.doRequest(queryObject, endpoint);
}
return Promise.reject(e);
}
if(hasNext && queryObject["method"] === 'GET'){
queryObject['qs']['page'] = queryObject['qs']['page'] + 1;
return Promise.resolve({data:data, supplemental_data: supplementalData, next: this.doRequest(queryObject, endpoint)});
}
return Promise.resolve({data:data, supplemental_data: supplementalData, next:null});
}
}
module.exports = TSheetsApi;