clearbit
Version:
Client for Clearbit.co business intelligence APIs
119 lines (100 loc) • 2.67 kB
JavaScript
'use strict';
var createError = require('create-error');
var _ = require('lodash');
function ClearbitResource (data) {
this.options = {};
_.assign(this, data);
}
ClearbitResource.get = function (path, options) {
options = _.assign({
path: path,
method: 'get',
query: extractParams(options)
}, this.options, options);
return this.client.request(options)
.bind(this)
.then(cast)
.catch(isQueued, function () {
throw new this.QueuedError(this.name + ' lookup queued');
})
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
};
ClearbitResource.post = function (path, options) {
options = _.assign({
path: path,
method: 'post',
json: true,
body: extractParams(options)
}, this.options, options);
return this.client.request(options)
.bind(this)
.then(cast)
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
};
ClearbitResource.del = function (path, options) {
options = _.assign({
path: path,
method: 'delete'
}, this.options, options);
return this.client.request(options)
.bind(this)
.then(cast)
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
};
ClearbitResource.setVersion = function(value){
this.options.headers = this.options.headers || {};
this.options.headers['API-Version'] = value;
};
exports.create = function (name, options) {
var Resource = function () {
ClearbitResource.apply(this, arguments);
};
_.assign(Resource, ClearbitResource, createErrors(name), {
name: name,
options: options
});
return _.assign(function (client) {
return _.assign(Resource, {
client: client
});
},
{
extend: function (proto, ctor) {
_.assign(Resource.prototype, proto);
_.assign(Resource, ctor);
return this;
}
});
};
function cast (data) {
/* jshint validthis:true */
return !Array.isArray(data) ? new this(data) : data.map(function (result) {
return new this(result);
}, this);
}
function isQueued (err) {
return err.type === 'queued';
}
function isUnknownRecord (err) {
return err.type === 'unknown_record';
}
function createErrors (name) {
return {
NotFoundError: createError(name + 'NotFoundError'),
QueuedError: createError(name + 'QueuedError')
};
}
function extractParams (options) {
var params = _.omit(options || {}, [
'path', 'method', 'params',
'client', 'api', 'stream',
'headers', 'timeout'
]);
return _.isEmpty(params) ? null : params;
}