Skip to content

Commit

Permalink
Add prettier to the project
Browse files Browse the repository at this point in the history
Why:

We would like to have a consistent style throughout the codebase.

This PR:

Adds prettier as a developer dependency and wires up appropriate
scripts.

Co-Authored-By: Edward Loveall <[email protected]>
  • Loading branch information
MattMSumner and edwardloveall committed Dec 4, 2018
1 parent 0bff56b commit ccfe17f
Show file tree
Hide file tree
Showing 44 changed files with 1,564 additions and 1,148 deletions.
10 changes: 5 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
'extends': 'standard',
'env': {
'es6': true,
'node': true,
'mocha': true
extends: ['standard', 'plugin:prettier/recommended'],
env: {
es6: true,
node: true,
mocha: true
}
}
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"semi": false,
"printWidth": 80,
"trailingComma": "all",
}
17 changes: 10 additions & 7 deletions lib/broadcast.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
class Broadcast {
constructor (client) {
constructor(client) {
this.client = client
}

get (options, cb) {
get(options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}

return this.client._request({
method: 'GET',
path: '/broadcast/v1/broadcasts',
qs: options
}, cb)
return this.client._request(
{
method: 'GET',
path: '/broadcast/v1/broadcasts',
qs: options,
},
cb,
)
}
}

Expand Down
53 changes: 31 additions & 22 deletions lib/campaign.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
class Campaign {
constructor (client) {
constructor(client) {
this.client = client
}

get (options, cb) {
get(options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}

return this.client._request({
method: 'GET',
path: '/email/public/v1/campaigns',
qs: options
}, cb)
return this.client._request(
{
method: 'GET',
path: '/email/public/v1/campaigns',
qs: options,
},
cb,
)
}

getById (options, cb) {
getById(options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}

return this.client._request({
method: 'GET',
path: '/email/public/v1/campaigns/by-id',
qs: options
}, cb)
return this.client._request(
{
method: 'GET',
path: '/email/public/v1/campaigns/by-id',
qs: options,
},
cb,
)
}

getOne (id, appId, cb) {
getOne(id, appId, cb) {
if (!id || typeof id === 'function') {
return cb(new Error('id parameter must be provided.'))
}
Expand All @@ -41,29 +47,32 @@ class Campaign {

var call = {
method: 'GET',
path: '/email/public/v1/campaigns/' + id
path: '/email/public/v1/campaigns/' + id,
}

if (appId) {
call.qs = {
appId: appId
appId: appId,
}
}

return this.client._request(call, cb)
}

events (options, cb) {
events(options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}

return this.client._request({
method: 'GET',
path: '/email/public/v1/events',
qs: options
}, cb)
return this.client._request(
{
method: 'GET',
path: '/email/public/v1/events',
qs: options,
},
cb,
)
}
}

Expand Down
80 changes: 45 additions & 35 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const EventEmitter = require('events').EventEmitter
const Bottleneck = require('bottleneck')
const limiter = new Bottleneck({
maxConcurrent: 9,
minTime: 1000
minTime: 1000,
})

const debug = require('debug')('hubspot:client')
Expand All @@ -28,13 +28,16 @@ const API_TIMEOUT = 15000
const MAX_USE_PERCENT_DEFAULT = 90

class Client extends EventEmitter {
constructor (options = {}) {
constructor(options = {}) {
super()
this.qs = {}
this.auth = undefined
this.setAuth(options)
this.setOAuth(options)
this.maxUsePercent = typeof options.maxUsePercent !== 'undefined' ? options.maxUsePercent : MAX_USE_PERCENT_DEFAULT
this.maxUsePercent =
typeof options.maxUsePercent !== 'undefined'
? options.maxUsePercent
: MAX_USE_PERCENT_DEFAULT
this.baseUrl = options.baseUrl || 'http://api.hubapi.com'
this.apiTimeout = options.timeout || API_TIMEOUT
this.apiCalls = 0
Expand All @@ -59,85 +62,92 @@ class Client extends EventEmitter {
this.workflows = new Workflow(this)
}

requestStats () {
requestStats() {
return {
running: limiter.running(),
queued: limiter.queued()
queued: limiter.queued(),
}
}

setAccessToken (accessToken) {
setAccessToken(accessToken) {
this.accessToken = accessToken
this.auth = { 'bearer': accessToken }
this.auth = { bearer: accessToken }
}

refreshAccessToken () {
refreshAccessToken() {
return this.oauth.refreshAccessToken()
}

setOAuth (options = {}) {
setOAuth(options = {}) {
this.clientId = options.clientId
this.clientSecret = options.clientSecret
this.redirectUri = options.redirectUri
this.refreshToken = options.refreshToken
}

setAuth (options = {}) {
setAuth(options = {}) {
if (options.apiKey) {
this.qs.hapikey = options.apiKey
} else if (options.accessToken) {
if (options.useOAuth1) {
this.qs.access_token = options.accessToken
} else { // defaults to OAuth2
} else {
// defaults to OAuth2
this.setAccessToken(options.accessToken)
}
}
}

_request (opts, cb = () => {}) {
_request(opts, cb = () => {}) {
const params = _.cloneDeep(opts)
if (this.auth) { params.auth = this.auth }
if (this.auth) {
params.auth = this.auth
}
params.json = true

params.url = this.baseUrl + params.path
delete params.path
params.qs = Object.assign({}, this.qs, params.qs)

params.qsStringifyOptions = {
arrayFormat: 'repeat'
arrayFormat: 'repeat',
}

params.timeout = this.apiTimeout

return this.checkApiLimit(params)
.then(() => {
this.emit('apiCall', params)
return limiter.schedule(() => request(params)) // limit the number of concurrent requests
.then(body => {
cb(null, body)
return body
}).catch(err => {
cb(err)
throw err
})
})
return this.checkApiLimit(params).then(() => {
this.emit('apiCall', params)
return limiter
.schedule(() => request(params)) // limit the number of concurrent requests
.then(body => {
cb(null, body)
return body
})
.catch(err => {
cb(err)
throw err
})
})
}

checkApiLimit (params) {
if (this.auth) { // don't check the api limit for the api call
checkApiLimit(params) {
if (this.auth) {
// don't check the api limit for the api call
return Promise.resolve()
}
if ((/integrations\/v1\/limit|oauth/).test(params.url)) { // don't check the api limit for the api call
if (/integrations\/v1\/limit|oauth/.test(params.url)) {
// don't check the api limit for the api call
return Promise.resolve()
}
if (this.maxUsePercent === 0) { // if maxUsePercent set to 0, do not check for the API limit (use at your own risk)
if (this.maxUsePercent === 0) {
// if maxUsePercent set to 0, do not check for the API limit (use at your own risk)
return Promise.resolve()
}
return this.getApiLimit().then(results => {
const { usageLimit = 40000, currentUsage = 0 } = results
const usagePercent = 100.0 * currentUsage / usageLimit
const usagePercent = (100.0 * currentUsage) / usageLimit
debug('usagePercent', usagePercent, 'apiCalls', this.apiCalls)
if (usagePercent > (this.maxUsePercent)) {
if (usagePercent > this.maxUsePercent) {
const err = new Error('Too close to the API limit')
err.usageLimit = usageLimit
err.currentUsage = currentUsage
Expand All @@ -147,7 +157,7 @@ class Client extends EventEmitter {
})
}

getApiLimit () {
getApiLimit() {
this.limit = this.limit || {}
const collectedAt = this.limit.collectedAt || 0
const recencyMinutes = (Date.now() - collectedAt) / (60 * 1000)
Expand All @@ -157,9 +167,9 @@ class Client extends EventEmitter {
}
return this._request({
method: 'GET',
path: '/integrations/v1/limit/daily'
path: '/integrations/v1/limit/daily',
}).then(results => {
this.limit = results.filter((r) => r.name === 'api-calls-daily')[0]
this.limit = results.filter(r => r.name === 'api-calls-daily')[0]
return this.limit
})
}
Expand Down
Loading

0 comments on commit ccfe17f

Please sign in to comment.