diff --git a/src/Agent.js b/src/Agent.js index 3c4b5c5..93b1456 100644 --- a/src/Agent.js +++ b/src/Agent.js @@ -29,23 +29,23 @@ export default class Agent { } get(uri, auth, query) { - return this.request({uri, auth, method: 'get', query: query}); + return this.request({ uri, auth, method: 'get', query: query }); } head(uri, auth) { - return this.request({uri, auth, method: 'head'}); + return this.request({ uri, auth, method: 'head' }); } post(uri, data, auth) { - return this.request({uri, data, auth, method: 'post'}); + return this.request({ uri, data, auth, method: 'post' }); } put(uri, data, auth) { - return this.request({uri, data, auth, method: 'put'}); + return this.request({ uri, data, auth, method: 'put' }); } delete(uri, data, auth) { - return this.request({uri, data, auth, method: 'delete'}); + return this.request({ uri, data, auth, method: 'delete' }); } @@ -60,9 +60,9 @@ export default class Agent { * @param {Object} files array of file names and file content * @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body } */ - request({uri, method, data = undefined, auth, query = undefined, form = undefined, files = undefined}) { + request({ uri, method, data = undefined, auth, query = undefined, form = undefined, files = undefined }) { const requestFiles = this._sanitizeFiles(files); - return this._request({uri, method, data, auth, query, form, files: requestFiles}); + return this._request({ uri, method, data, auth, query, form, files: requestFiles }); } /** @@ -76,8 +76,8 @@ export default class Agent { * @param {Object} files array of file names and file content * @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body } */ - _request({uri, method, data, auth, query, form, files}) { - const req = this._buildRequest({uri, method, data, auth, query, form, files}); + _request({ uri, method, data, auth, query, form, files }) { + const req = this._buildRequest({ uri, method, data, auth, query, form, files }); return this._promiseResponse(req); } @@ -112,7 +112,7 @@ export default class Agent { shortErrorDescription = body.error_description; } const reason = new Error(errorDescription); - Object.assign(reason, {statusCode, errorDescription, shortErrorDescription, error, body}); + Object.assign(reason, { statusCode, errorDescription, shortErrorDescription, error, body }); reject(reason); } else { fulfill({ @@ -123,7 +123,7 @@ export default class Agent { }); } - _buildRequest({uri, method, data, auth, query, form, files, makerequest=request}) { + _buildRequest({ uri, method, data, auth, query, form, files, makerequest=request }) { const req = makerequest(method, uri); if (this.prefix) { req.use(this.prefix); @@ -165,7 +165,7 @@ export default class Agent { if (auth.username !== undefined) { req.auth(auth.username, auth.password); } else { - req.set({Authorization: `Bearer ${auth}`}); + req.set({ Authorization: `Bearer ${auth}` }); } } return req; diff --git a/src/Client.js b/src/Client.js index 29fd7dc..e34c05f 100644 --- a/src/Client.js +++ b/src/Client.js @@ -68,7 +68,7 @@ export default class Client { * @return {Promise} To publish the library */ publishLibrary(name) { - return this.api.publishLibrary({name, auth: this.auth }) + return this.api.publishLibrary({ name, auth: this.auth }) .then(payload => { const library = payload.body.data || {}; return new Library(this, library); diff --git a/src/Defaults.js b/src/Defaults.js index 3a97520..7a2e8a5 100644 --- a/src/Defaults.js +++ b/src/Defaults.js @@ -2,5 +2,5 @@ export default { baseUrl: 'https://api.particle.io', clientSecret: 'particle-api', clientId: 'particle-api', - tokenDuration: 7776000, // 90 days + tokenDuration: 7776000 // 90 days }; diff --git a/src/EventStream.js b/src/EventStream.js index e67adb9..b6c345b 100644 --- a/src/EventStream.js +++ b/src/EventStream.js @@ -47,17 +47,18 @@ class EventStream extends EventEmitter { res.on('end', () => { try { body = JSON.parse(body); - } catch (e) {} - this.emit('response', { - statusCode, - body - }); - let errorDescription = `HTTP error ${statusCode} from ${this.uri}`; - if (body && body.error_description) { - errorDescription += ' - ' + body.error_description; + } finally { + this.emit('response', { + statusCode, + body + }); + let errorDescription = `HTTP error ${statusCode} from ${this.uri}`; + if (body && body.error_description) { + errorDescription += ' - ' + body.error_description; + } + reject({ statusCode, errorDescription, body }); + this.req = undefined; } - reject({ statusCode, errorDescription, body }); - this.req = undefined; }); return; } diff --git a/src/Particle.js b/src/Particle.js index b0a3f4a..a7a3357 100644 --- a/src/Particle.js +++ b/src/Particle.js @@ -41,7 +41,7 @@ class Particle { grant_type: 'password', client_id: this.clientId, client_secret: this.clientSecret, - expires_in: tokenDuration, + expires_in: tokenDuration }, method: 'post' }); } @@ -56,7 +56,7 @@ class Particle { return this.post('/v1/users', { username, password, - account_info : accountInfo, + account_info : accountInfo }); } @@ -89,7 +89,7 @@ class Particle { */ removeAccessToken({ username, password, token }) { return this.delete(`/v1/access_tokens/${token}`, { - access_token: token, + access_token: token }, { username, password }); } @@ -166,14 +166,14 @@ class Particle { return this.post('/v1/device_claims', { iccid }, auth); } - validatePromoCode({auth, promoCode}) { + validatePromoCode({ auth, promoCode }) { return this.get(`/v1/promo_code/${promoCode}`, auth); } changeProduct({ deviceId, productId, shouldUpdate, auth }) { return this.put(`/v1/devices/${deviceId}`, { product_id: productId, - update_after_claim: shouldUpdate || false, + update_after_claim: shouldUpdate || false }, auth); } @@ -197,7 +197,7 @@ class Particle { */ signalDevice({ deviceId, signal, auth }) { return this.put(`/v1/devices/${deviceId}`, { - signal: ( !!signal ? '1' : '0' ), + signal: ( signal ? '1' : '0' ) }, auth); } @@ -228,7 +228,7 @@ class Particle { */ flashTinker({ deviceId, auth }) { return this.put(`/v1/devices/${deviceId}`, { - app: 'tinker', + app: 'tinker' }, auth); } @@ -296,7 +296,7 @@ class Particle { */ callFunction({ deviceId, name, argument, auth }) { return this.post(`/v1/devices/${deviceId}/${name}`, { - args: argument, + args: argument }, auth); } @@ -433,7 +433,7 @@ class Particle { return this.put(`/v1/sims/${iccid}`, { country: countryCode, promo_code: promoCode, - action: 'activate', + action: 'activate' }, auth); } @@ -530,8 +530,8 @@ class Particle { files, auth, method: 'post' }); } - publishLibrary({auth, name}) { - return this.request({ uri: `/v1/libraries/${name}`, auth, method: 'patch', data: {visibility:'public'}}); + publishLibrary({ auth, name }) { + return this.request({ uri: `/v1/libraries/${name}`, auth, method: 'patch', data: { visibility:'public' } }); } /** @@ -588,7 +588,7 @@ class Particle { } client(options = {}) { - return new Client(Object.assign({ api: this}, options)); + return new Client(Object.assign({ api: this }, options)); } } diff --git a/src/superagent-binary-parser.js b/src/superagent-binary-parser.js index 637607f..72d7b66 100644 --- a/src/superagent-binary-parser.js +++ b/src/superagent-binary-parser.js @@ -5,4 +5,4 @@ export default function binaryParser(res, fn) { let data = []; res.on('data', chunk => data.push(chunk)); res.on('end', () => fn(null, Buffer.concat(data))); -}; +}