From 9a9a63c15a393cdc43c80fe1a905a739ae5a4904 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Fri, 25 Mar 2016 19:58:03 +0800 Subject: [PATCH] Use axios instead of request --- src/infer/inferUserAgent.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/infer/inferUserAgent.js b/src/infer/inferUserAgent.js index b5caf79..3a3db0f 100644 --- a/src/infer/inferUserAgent.js +++ b/src/infer/inferUserAgent.js @@ -1,29 +1,25 @@ -import request from 'request'; +import axios from 'axios'; import _ from 'lodash'; const ELECTRON_VERSIONS_URL = 'https://atom.io/download/atom-shell/index.json'; function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) { - return new Promise((resolve, reject) => { - request(url, (error, response, body) => { - if (error) { - reject(error); - return; + + return axios.get(url) + .then(response => { + if (response.status !== 200) { + throw `Bad request: Status code ${response.status}`; } - if (response.statusCode === 200) { - const data = JSON.parse(body); - const electronVersionToChromeVersion = _.zipObject(data.map(d => d.version), data.map(d => d.chrome)); - if (!(electronVersion in electronVersionToChromeVersion)) { - reject(`Electron version '${ electronVersion }' not found in retrieved version list!`); - return; - } - resolve(electronVersionToChromeVersion[electronVersion]); - return; + + const data = response.data; + const electronVersionToChromeVersion = _.zipObject(data.map(d => d.version), data.map(d => d.chrome)); + + if (!(electronVersion in electronVersionToChromeVersion)) { + throw `Electron version '${ electronVersion }' not found in retrieved version list!`; } - reject('Bad request: ' + response.statusCode); - return; + + return electronVersionToChromeVersion[electronVersion]; }); - }); } export function getUserAgentString(chromeVersion, platform) {