Merge pull request #2188 from aaronraimist/promise-defer-scalarauthclient

Fix Promise.defer warning in ScalarAuthClient.js
pull/21833/head
David Baker 2018-10-02 12:14:18 +01:00 committed by GitHub
commit 3cc4ea5ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 60 deletions

View File

@ -63,7 +63,7 @@ class ScalarAuthClient {
validateToken(token) { validateToken(token) {
let url = SdkConfig.get().integrations_rest_url + "/account"; let url = SdkConfig.get().integrations_rest_url + "/account";
const defer = Promise.defer(); return new Promise(function(resolve, reject) {
request({ request({
method: "GET", method: "GET",
uri: url, uri: url,
@ -71,17 +71,16 @@ class ScalarAuthClient {
json: true, json: true,
}, (err, response, body) => { }, (err, response, body) => {
if (err) { if (err) {
defer.reject(err); reject(err);
} else if (response.statusCode / 100 !== 2) { } else if (response.statusCode / 100 !== 2) {
defer.reject({statusCode: response.statusCode}); reject({statusCode: response.statusCode});
} else if (!body || !body.user_id) { } else if (!body || !body.user_id) {
defer.reject(new Error("Missing user_id in response")); reject(new Error("Missing user_id in response"));
} else { } else {
defer.resolve(body.user_id); resolve(body.user_id);
} }
}); });
})
return defer.promise;
} }
registerForToken() { registerForToken() {
@ -96,9 +95,9 @@ class ScalarAuthClient {
} }
exchangeForScalarToken(openid_token_object) { exchangeForScalarToken(openid_token_object) {
const defer = Promise.defer();
const scalar_rest_url = SdkConfig.get().integrations_rest_url; const scalar_rest_url = SdkConfig.get().integrations_rest_url;
return new Promise(function(resolve, reject) {
request({ request({
method: 'POST', method: 'POST',
uri: scalar_rest_url+'/register', uri: scalar_rest_url+'/register',
@ -106,46 +105,44 @@ class ScalarAuthClient {
json: true, json: true,
}, (err, response, body) => { }, (err, response, body) => {
if (err) { if (err) {
defer.reject(err); reject(err);
} else if (response.statusCode / 100 !== 2) { } else if (response.statusCode / 100 !== 2) {
defer.reject({statusCode: response.statusCode}); reject({statusCode: response.statusCode});
} else if (!body || !body.scalar_token) { } else if (!body || !body.scalar_token) {
defer.reject(new Error("Missing scalar_token in response")); reject(new Error("Missing scalar_token in response"));
} else { } else {
defer.resolve(body.scalar_token); resolve(body.scalar_token);
} }
}); });
})
return defer.promise;
} }
getScalarPageTitle(url) { getScalarPageTitle(url) {
const defer = Promise.defer();
let scalarPageLookupUrl = SdkConfig.get().integrations_rest_url + '/widgets/title_lookup'; let scalarPageLookupUrl = SdkConfig.get().integrations_rest_url + '/widgets/title_lookup';
scalarPageLookupUrl = this.getStarterLink(scalarPageLookupUrl); scalarPageLookupUrl = this.getStarterLink(scalarPageLookupUrl);
scalarPageLookupUrl += '&curl=' + encodeURIComponent(url); scalarPageLookupUrl += '&curl=' + encodeURIComponent(url);
return new Promise(function(resolve, reject) {
request({ request({
method: 'GET', method: 'GET',
uri: scalarPageLookupUrl, uri: scalarPageLookupUrl,
json: true, json: true,
}, (err, response, body) => { }, (err, response, body) => {
if (err) { if (err) {
defer.reject(err); reject(err);
} else if (response.statusCode / 100 !== 2) { } else if (response.statusCode / 100 !== 2) {
defer.reject({statusCode: response.statusCode}); reject({statusCode: response.statusCode});
} else if (!body) { } else if (!body) {
defer.reject(new Error("Missing page title in response")); reject(new Error("Missing page title in response"));
} else { } else {
let title = ""; let title = "";
if (body.page_title_cache_item && body.page_title_cache_item.cached_title) { if (body.page_title_cache_item && body.page_title_cache_item.cached_title) {
title = body.page_title_cache_item.cached_title; title = body.page_title_cache_item.cached_title;
} }
defer.resolve(title); resolve(title);
} }
}); });
})
return defer.promise;
} }
/** /**