Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Bump version. Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vkhomiv-hubspot committed Nov 21, 2019
1 parent d817c51 commit 0290165
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"bracketSpacing": true,
"arrowParens": "always",
"semi": false,
"printWidth": 80,
"printWidth": 120,
"trailingComma": "es5",
"endOfLine": "lf"
}
7 changes: 7 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 2.3.3 / 2019-11-21

- Add multipart form-data upload
- Fix tests, update typing
- Extended client with expiresIn and updatedAt values
- Add hubspot.forms.getUploadedFileByUrl method

# 2.3.2 / 2019-10-21

- Update hubspot.contacts.getById method
Expand Down
65 changes: 27 additions & 38 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,34 @@ const debug = require('debug')('hubspot:client')
const API_TIMEOUT = 15000
const MAX_USE_PERCENT_DEFAULT = 90

const getLimiter = (options) =>
new Bottleneck(
Object.assign(
{
maxConcurrent: 2,
minTime: 1000 / 9,
},
options.limiter
)
)

class Client extends EventEmitter {
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 || 'https://api.hubapi.com'
this.apiTimeout = options.timeout || API_TIMEOUT
this.apiCalls = 0
this.on('apiCall', (params) => {
debug('apiCall', _.pick(params, ['method', 'url']))
this.apiCalls += 1
})
this.checkLimit =
options.checkLimit !== undefined ? options.checkLimit : true
this.limiter = new Bottleneck(
Object.assign(
{
maxConcurrent: 2,
minTime: 1000 / 9,
},
options.limiter
)
)

this.checkLimit = options.checkLimit !== undefined ? options.checkLimit : true
this.limiter = getLimiter(options)
this.broadcasts = new Broadcast(this)
this.campaigns = new Campaign(this)
this.companies = new Company(this)
Expand Down Expand Up @@ -89,8 +87,8 @@ class Client extends EventEmitter {
setAccessToken(accessToken, expiresIn = 0, updatedAt = 0) {
this.accessToken = accessToken
this.accessTokenExpiresIn = expiresIn
this.accessTokenUpdatedAt =
updatedAt !== 0 ? updatedAt : Math.floor(Date.now() / 1000) // current timestamp in seconds
// current timestamp in seconds
this.accessTokenUpdatedAt = updatedAt !== 0 ? updatedAt : Math.floor(Date.now() / 1000)
this.auth = { bearer: accessToken }
}

Expand All @@ -113,8 +111,7 @@ class Client extends EventEmitter {
this.qs.access_token = options.accessToken
} else {
// defaults to OAuth2
const updatedAtTimestamp =
_.get(options, 'updatedAtTimestamp') || Math.floor(Date.now() / 1000) // current timestamp in seconds
const updatedAtTimestamp = _.get(options, 'updatedAtTimestamp') || Math.floor(Date.now() / 1000) // current timestamp in seconds
const expiresIn = _.get(options, 'expiresIn') || 21600
this.setAccessToken(options.accessToken, expiresIn, updatedAtTimestamp)
}
Expand Down Expand Up @@ -158,30 +155,22 @@ class Client extends EventEmitter {
this.usageLimit = headers['x-hubspot-ratelimit-daily']
}
if (this.usageLimit !== undefined) {
this.currentUsage =
this.usageLimit - headers['x-hubspot-ratelimit-daily-remaining']
this.currentUsage = this.usageLimit - headers['x-hubspot-ratelimit-daily-remaining']
}
return Promise.resolve()
}

checkApiLimit(params) {
return new Promise((resolve, reject) => {
if (this.auth) {
// don't check the api limit for the api call
resolve()
}
if (/integrations\/v1\/limit|oauth/.test(params.url)) {
// don't check the api limit for the api call
resolve()
}
if (!this.checkLimit) {
// don't check the api limit for the api call
resolve()
}
if (this.maxUsePercent === 0) {
// if maxUsePercent set to 0, do not check for the API limit (use at your own risk)
resolve()
}
// don't check the api limit for the api call
if (this.auth) return resolve()
// don't check the api limit for the api call
if (/integrations\/v1\/limit|oauth/.test(params.url)) return resolve()
// don't check the api limit for the api call
if (!this.checkLimit) return resolve()
// if maxUsePercent set to 0, do not check for the API limit (use at your own risk)
if (this.maxUsePercent === 0) return resolve()

if (this.currentUsage !== undefined) {
const usagePercent = (100.0 * this.currentUsage) / this.usageLimit
debug('usagePercent', usagePercent, 'apiCalls', this.apiCalls)
Expand Down
7 changes: 5 additions & 2 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ class File {
formData.append('file_names', file.name)
formData.append('files', Buffer.from(arrayBuffer), file.name)

const hapiKey = this.client.qs.hapikey
return fetch(
`${this.client.baseUrl}/filemanager/api/v2/files?hapikey=${this.client.qs.hapikey}&override=${override}&hidden=${hidden}`,
`${this.client.baseUrl}/filemanager/api/v2/files?hapikey=${hapiKey}&override=${override}&hidden=${hidden}`,
{
method: 'POST',
body: formData,
}
) // can't use the request-promise based client because they don't handle formdata inside of body correctly. See: https://github.com/request/request-promise/issues/271
)
// can't use the request-promise based client because they don't handle formdata inside
// of body correctly. See: https://github.com/request/request-promise/issues/271
})
}
}
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": "hubspot",
"version": "2.3.2",
"version": "2.3.3",
"description": "A node wrapper for the HubSpot API",
"engines": {
"node": ">=10.0.0"
Expand Down

0 comments on commit 0290165

Please sign in to comment.