harvest
Version:
Harvest API client library
64 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const async_1 = require("async");
const Request = require("request-promise");
class RequestClient {
constructor(config) {
this.accessToken = config.auth.accessToken;
this.accountId = config.auth.accountId;
this.request = Request.defaults({
baseUrl: 'https://api.harvestapp.com',
headers: {
'User-Agent': config.userAgent,
Authorization: `Bearer ${this.accessToken}`,
'Harvest-Account-Id': this.accountId,
'Content-Type': 'application/json'
},
transform: this.preprocess
});
this.concurrency = config.throttleConcurrency || 40;
this.queue = async_1.queue(this.requestGenerator(), this.concurrency);
}
preprocess(body, response) {
return { headers: response.headers, data: body };
}
push(task) {
this.queue.push(task);
}
requestGenerator() {
return (task, done) => {
let options = {};
options.method = task.method;
options.uri = task.uri;
if (task.method === 'GET') {
options.qs = task.data;
}
else {
options.body = JSON.stringify(task.data);
}
this.request(options)
.then(({ headers, data }) => {
task.callback(undefined, JSON.parse(data));
done();
})
.catch(error => {
if (error.statusCode === 429) {
return this.retryAfter(task, error.response.headers['retry-after'], done);
}
task.callback(error, undefined);
done();
});
};
}
retryAfter(task, retryAfter, done) {
this.queue.pause();
this.queue.unshift(task);
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.queue.resume();
done();
}, retryAfter * 1000);
}
}
exports.RequestClient = RequestClient;
//# sourceMappingURL=client.js.map