Skip to content

Commit

Permalink
Added in support for user-agent and custom api url.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdelliott committed Aug 21, 2017
1 parent b17d225 commit b2394c2
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules/
.vscode/
npm-debug*
/coverage
.DS_Store
.DS_Store
.idea
*.iml
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ var fullcontact = require('fullcontact-node')({
clientId: '<Client ID>', //for v3 APIs. See: https://app.fullcontact.com/apps
clientSecret: '<Client Secret>',
redirectUri: '<Redirect URI>',
scope: 'list,of,scopes'
scope: 'list,of,scopes',
userAgent: '<AppName/Version>',
baseUrl: '<URL>' //to override base api url
});
```

Expand Down
39 changes: 31 additions & 8 deletions lib/apis/api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const request = require('request-promise');
const config = require('./config');
const _ = require('lodash');

/**
* Base Class for all API Resources.
*
* @class API
*/
class API {

constructor(c = {}) {
this.__baseUrl = c.baseUrl || config.url;
this.__ua = c.userAgent || 'fullcontact-node';
}

/**
* Gets the base URL from the config.
*
Expand All @@ -15,7 +22,18 @@ class API {
* @memberOf API
*/
get apiUrl() {
return config.url;
return this.__baseUrl;
}

/**
* Gets the base URL from the config.
*
* @readonly
*
* @memberOf API
*/
get userAgent() {
return this.__ua;
}

/**
Expand Down Expand Up @@ -46,7 +64,12 @@ class API {
url: `${this.apiUrl}${opts.path}`,
qs: opts.qs,
body: opts.body,
headers,
headers: _.extend(
{
'User-Agent': this.userAgent
},
headers
),
form: opts.form,
formData: opts.formData,
json: opts.json
Expand All @@ -65,13 +88,13 @@ class APIKeyBased extends API {
/**
* Creates an instance of APIKeyBased.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf APIKeyBased
*/
constructor(apiKey) {
super();
this.apiKey = apiKey;
constructor(config) {
super(config);
this.apiKey = config.apiKey;
}

/**
Expand Down Expand Up @@ -115,8 +138,8 @@ class OAuthBased extends API {
*
* @memberOf OAuthBased
*/
constructor() {
super();
constructor(config) {
super(config);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/apis/v2/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class CompanyAPI extends API.APIKeyBased {
/**
* Creates an instance of CompanyAPI.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf CompanyAPI
*/
constructor(apiKey) {
super(apiKey);
constructor(config) {
super(config);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/apis/v2/email-verification.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class EmailVerificationAPI extends API.APIKeyBased {
/**
* Creates an instance of EmailVerificationAPI.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf EmailVerificationAPI
*/
constructor(apiKey) {
super(apiKey);
constructor(config) {
super(config);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/apis/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ class V2 {
/**
* Creates an instance of V2.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf V2
*/
constructor(apiKey) {
constructor(config) {
this.__instances = {
person: new PersonAPI(apiKey),
company: new CompanyAPI(apiKey),
name: new NameAPI(apiKey),
location: new LocationAPI(apiKey),
stats: new StatsAPI(apiKey),
emailVerification: new EmailVerificationAPI(apiKey)
person: new PersonAPI(config),
company: new CompanyAPI(config),
name: new NameAPI(config),
location: new LocationAPI(config),
stats: new StatsAPI(config),
emailVerification: new EmailVerificationAPI(config)
};
}

Expand Down
6 changes: 3 additions & 3 deletions lib/apis/v2/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class LocationAPI extends API.APIKeyBased {
/**
* Creates an instance of LocationAPI.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf LocationAPI
*/
constructor(apiKey) {
super(apiKey);
constructor(config) {
super(config);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/apis/v2/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class NameAPI extends API.APIKeyBased {
/**
* Creates an instance of NameAPI.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf NameAPI
*/
constructor(apiKey) {
super(apiKey);
constructor(config) {
super(config);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/apis/v2/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class PersonAPI extends API.APIKeyBased {
/**
* Creates an instance of PersonAPI.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf PersonAPI
*/
constructor(apiKey) {
super(apiKey);
constructor(config) {
super(config);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/apis/v2/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class StatsAPI extends API.APIKeyBased {
/**
* Creates an instance of StatsAPI.
*
* @param {String} apiKey
* @param {Object} config
*
* @memberOf StatsAPI
*/
constructor(apiKey) {
super(apiKey);
constructor(config) {
super(config);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/fullcontact.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class FullContact {
* clientId,
* clientSecret,
* redirectUri,
* scope
* scope,
* baseUrl
* }
*
* @param {Object} config
Expand All @@ -25,8 +26,8 @@ class FullContact {
constructor(config) {
this.__instances = {
oauth: new OAuth(config),
v2: new V2(config.apiKey),
v3: new V3()
v2: new V2(config),
v3: new V3(config)
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fullcontact-node",
"version": "1.2.1",
"version": "1.3.0",
"description": "NodeJS SDK for FullContact APIs",
"main": "lib/fullcontact.js",
"scripts": {
Expand Down
22 changes: 20 additions & 2 deletions test/api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const chance = require('chance')();

describe('APIKeyBased', () => {
it('should add auth header', done => {
const instance = new api.APIKeyBased();
const apiKey = chance.string();
const instance = new api.APIKeyBased({ apiKey });
const headers = {};

instance.authorize(apiKey, headers)
Expand All @@ -31,4 +31,22 @@ describe('OAuthBased', () => {
})
.catch(e => assert.fail(e));
});
});
});

describe('API', () => {
it('should replace url', () => {
const baseUrl = chance.url();
const instance = new api.OAuthBased({ baseUrl });

assert.equal(instance.apiUrl, baseUrl);
});
});

describe('API', () => {
it('should replace user-agent', () => {
const userAgent = chance.string();
const instance = new api.OAuthBased({ userAgent });

assert.equal(instance.userAgent, userAgent);
});
});
8 changes: 4 additions & 4 deletions test/v2/company-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ nock.disableNetConnect();
describe('V2-Company', () => {
it('should set apiKey', () => {
const apiKey = chance.string();
const instance = new CompanyAPI(apiKey);
const instance = new CompanyAPI({ apiKey });
assert.equal(instance.apiKey, apiKey);
});

it('should send apiKey as header', done => {
const apiKey = chance.string();
const instance = new CompanyAPI(apiKey);
const instance = new CompanyAPI({ apiKey });
const opts = {
domain: chance.domain(),
keyPeople: true,
Expand All @@ -40,7 +40,7 @@ describe('V2-Company', () => {

it('should lookup by domain', done => {
const apiKey = chance.string();
const instance = new CompanyAPI(apiKey);
const instance = new CompanyAPI({ apiKey });
const opts = {
domain: chance.domain(),
keyPeople: true,
Expand All @@ -65,7 +65,7 @@ describe('V2-Company', () => {

it('should search by name', done => {
const apiKey = chance.string();
const instance = new CompanyAPI(apiKey);
const instance = new CompanyAPI({ apiKey });
const opts = {
companyName: chance.name(),
keyPeople: true,
Expand Down
10 changes: 5 additions & 5 deletions test/v2/email-verification-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ nock.disableNetConnect();
describe('V2-EmailVerification', () => {
it('should set apiKey', () => {
const apiKey = chance.string();
const instance = new EmailVerificationAPI(apiKey);
const instance = new EmailVerificationAPI({ apiKey });
assert.equal(instance.apiKey, apiKey);
});

it('should send apiKey as header', done => {
const apiKey = chance.string();
const instance = new EmailVerificationAPI(apiKey);
const instance = new EmailVerificationAPI({ apiKey });
const opts = {
email: chance.email()
};
Expand All @@ -35,7 +35,7 @@ describe('V2-EmailVerification', () => {

it('should lookup single', done => {
const apiKey = chance.string();
const instance = new EmailVerificationAPI(apiKey);
const instance = new EmailVerificationAPI({ apiKey });
const opts = {
email: chance.email()
};
Expand All @@ -55,7 +55,7 @@ describe('V2-EmailVerification', () => {

it('should batch emails', done => {
const apiKey = chance.string();
const instance = new EmailVerificationAPI(apiKey);
const instance = new EmailVerificationAPI({ apiKey });

const opts = {
emails: [chance.email(), chance.email()],
Expand All @@ -78,7 +78,7 @@ describe('V2-EmailVerification', () => {
const apiKey = chance.string();
const id = chance.word();
const path = `/v2/verification/emails/${id}`;
const instance = new EmailVerificationAPI(apiKey);
const instance = new EmailVerificationAPI({ apiKey });

const scope = nock(config.url)
.get(path)
Expand Down
7 changes: 4 additions & 3 deletions test/v2/index-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const LocationAPI = require('../../lib/apis/v2/location');
const NameAPI = require('../../lib/apis/v2/name');
const PersonAPI = require('../../lib/apis/v2/person');
const StatsAPI = require('../../lib/apis/v2/stats');
const chance = require('chance')();

describe('V2', () => {
it('should create instances', () => {
const instance = new V2();
const instance = new V2({ apiKey: chance.string() });
assert.ok(instance.person instanceof PersonAPI);
assert.ok(instance.company instanceof CompanyAPI);
assert.ok(instance.name instanceof NameAPI);
Expand All @@ -20,8 +21,8 @@ describe('V2', () => {

it('cardReader should not be implemented', () => {
try {
const instance = new V2();
const cr =instance.cardReader;
const instance = new V2({});
const cr = instance.cardReader;
} catch(e) {
assert.equal(e, 'Not Implemented');
}
Expand Down
Loading

0 comments on commit b2394c2

Please sign in to comment.