diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f08707..d2afce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # changelog +## 6.4.3 - 15 Feb 2017 + +* Create a wrapper for `listBuildTargets` in `Client.js`. +* Marked `compileCode`, `signalDevice`, `listDevices` and `listBuildTargets` as deprecated. Those methods will be removed in 6.5 + ## 6.4.2 - 05 Jan 2017 * Create a wrapper for `listDevices` in `Client.js`. 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 bbb6634..7b46a2b 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); @@ -104,15 +104,56 @@ export default class Client { return this.api.downloadFile({ url }); } + /** + * @param {Object} files Object containing files to be compiled + * @param {Number} platformId Platform id number of the device you are compiling for + * @param {String} targetVersion System firmware version to compile against + * @return {Promise} + * @deprecated Will be removed in 6.5 + */ compileCode(files, platformId, targetVersion) { return this.api.compileCode({ files, platformId, targetVersion, auth: this.auth }); } + /** + * @param {String} $0.deviceId Device ID or Name + * @param {Boolean} $0.signal Signal on or off + * @return {Promise} + * @deprecated Will be removed in 6.5 + */ signalDevice({ signal, deviceId }) { return this.api.signalDevice({ signal, deviceId, auth: this.auth }); } + /** + * @return {Promise} + * @deprecated Will be removed in 6.5 + */ listDevices() { return this.api.listDevices({ auth: this.auth }); } + + /** + * @return {Promise} + * @deprecated Will be removed in 6.5 + */ + listBuildTargets() { + return this.api.listBuildTargets({ onlyFeatured: true, auth: this.auth }) + .then(payload => { + let targets = []; + for (let target of payload.body.targets) { + for (let platform of target.platforms) { + targets.push({ + version: target.version, + platform: platform, + prerelease: target.prereleases.indexOf(platform) > -1, + firmware_vendor: target.firmware_vendor + }); + } + } + return targets; + }, error => { + + }); + } } 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))); -}; +} diff --git a/test/Client.spec.js b/test/Client.spec.js index 689e248..207ad52 100644 --- a/test/Client.spec.js +++ b/test/Client.spec.js @@ -145,4 +145,52 @@ describe('Client', () => { }); }); + describe('listBuildTargets', () => { + it('delegates to api', () => { + const response = { + targets: [ + { + platforms: [0, 6], + prereleases: [], + version: '1.2.3', + firmware_vendor: 'Foo' + }, { + platforms: [6, 8], + prereleases: [6], + version: '4.5.6', + firmware_vendor: 'Bar' + } + ] + }; + const expected = [ + { + version: '1.2.3', + platform: 0, + prerelease: false, + firmware_vendor: 'Foo' + }, { + version: '1.2.3', + platform: 6, + prerelease: false, + + firmware_vendor: 'Foo' + }, { + version: '4.5.6', + platform: 6, + prerelease: true, + firmware_vendor: 'Bar' + }, { + version: '4.5.6', + platform: 8, + prerelease: false, + firmware_vendor: 'Bar' + }, + ]; + api.listBuildTargets = ({auth}) => { + return Promise.resolve({body: response}); + }; + return expect(client.listBuildTargets()).to.eventually.eql(expected); + }); + }); + });