Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow err params in callbacks and support objects in Client #18

Merged
merged 1 commit into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ import {Tag} from './tag';
import {Segment} from './segment';

export class Client {
constructor(appId, appApiKey) {
this.appId = appId;
this.appApiKey = appApiKey;
constructor(...args) {
if (args.length === 2) {
this.appId = args[0];
this.appApiKey = args[1];
} else if (args.length === 1) {
this.appId = args[0].appId;
this.appApiKey = args[0].appApiKey;
}
if (!this.appId || !this.appApiKey) {
throw new Error('Could not construct a client with those parameters');
}
this.users = new User(this);
this.events = new Event(this);
this.companies = new Company(this);
Expand All @@ -36,7 +44,7 @@ export class Client {
.send(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/0.1.2')
.end(r => f(r));
.end(r => this.callback(f, r));
}
get(endpoint, data, f) {
unirest.get(`https://api.intercom.io${endpoint}`)
Expand All @@ -45,15 +53,15 @@ export class Client {
.query(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/0.1.2')
.end(r => f(r));
.end(r => this.callback(f, r));
}
nextPage(paginationObject, f) {
unirest.get(paginationObject.next)
.auth(this.appId, this.appApiKey)
.type('json')
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/0.1.2')
.end(r => f(r));
.end(r => this.callback(f, r));
}
delete(endpoint, data, f) {
unirest.delete(`https://api.intercom.io${endpoint}`)
Expand All @@ -62,6 +70,18 @@ export class Client {
.query(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/0.1.2')
.end(r => f(r));
.end(r => this.callback(f, r));
}
callback(f, data) {
if (f.length >= 2) {
let hasErrors = data.body && data.body.type === 'error.list';
if (hasErrors) {
f(data, null);
} else {
f(null, data);
}
} else {
f(data);
}
}
}
30 changes: 30 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'assert';
import {Client} from '../lib';

describe('users', function () {
it('should callback with errors', function (done) {
let callback = function (err, d) {
assert.equal('error.list', err.body.type);
assert.equal(null, d);
done();
};
let client = new Client('foo', 'bar');
client.callback(callback, { body: { type: 'error.list' }});
});
it('should construct with two fields', function () {
let client = new Client('foo', 'bar');
assert.equal('foo', client.appId);
assert.equal('bar', client.appApiKey);
});
it('should construct with an object', function () {
let client = new Client({ appId: 'foo', appApiKey: 'bar' });
assert.equal('foo', client.appId);
assert.equal('bar', client.appApiKey);
});
it('should throw if no credentials found', function () {
assert.throws(function () {
let client = new Client('baz');
console.log(client.appId);
}, /Could not construct a client with those parameters/);
});
});