chrome-store-api
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -17,2 +17,3 @@ /** | ||
exports.checkResponseErrors = checkResponseErrors; | ||
exports.isError = isError; | ||
@@ -107,11 +108,16 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function checkResponseErrors(data) { | ||
if (data.status > 299 || data.status < 200) { | ||
if (isError(data)) { | ||
return Promise.reject(data); | ||
} | ||
return data.response; | ||
} | ||
if (_lodash2['default'].size(_lodash2['default'].get(data, 'response.itemError')) > 0) { | ||
return Promise.reject(data); | ||
} | ||
/** | ||
* Check api method error | ||
* @param {Object} data | ||
* @returns {boolean} | ||
*/ | ||
return data.response; | ||
function isError(data) { | ||
return data.status > 299 || data.status < 200 || _lodash2['default'].size(_lodash2['default'].get(data, 'response.itemError')) > 0; | ||
} |
@@ -9,3 +9,3 @@ /** | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
value: true | ||
}); | ||
@@ -19,2 +19,4 @@ | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
@@ -36,68 +38,101 @@ | ||
/** | ||
* @param {TokenManager} tokenManager | ||
*/ | ||
function _default(tokenManager) { | ||
_classCallCheck(this, _default); | ||
this.tokenManager = tokenManager; | ||
} | ||
/** | ||
* Publish item in chrome web store | ||
* @param {String} itemId | ||
* @returns {Promise<ChromeStorePublishInfo>} | ||
*/ | ||
_createClass(_default, [{ | ||
key: 'publish', | ||
value: function publish(itemId) { | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.publish(token, itemId); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'publish')); | ||
} | ||
/** | ||
* Create new item | ||
* @param {Blob} content | ||
* @returns {Promise<ChromeStoreItem>} | ||
* @param {TokenManager} tokenManager | ||
*/ | ||
}, { | ||
key: 'insert', | ||
value: function insert(content) { | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.insert(token, content); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'insert')); | ||
} | ||
/** | ||
* Update existing item | ||
* @param {String} itemId | ||
* @param {Blob} content | ||
* @returns {Promise<ChromeStoreItem>} | ||
*/ | ||
}, { | ||
key: 'update', | ||
value: function update(itemId, content) { | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.update(token, itemId, content); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'update')); | ||
function _default(tokenManager) { | ||
_classCallCheck(this, _default); | ||
this.tokenManager = tokenManager; | ||
} | ||
/** | ||
* Get info about item | ||
* Publish item in chrome web store | ||
* @param {String} itemId | ||
* @returns {Promise<ChromeStoreItem>} | ||
* @returns {Promise<ChromeStorePublishInfo>} | ||
*/ | ||
}, { | ||
key: 'get', | ||
value: function get(itemId) { | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.get(token, itemId); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'get')); | ||
} | ||
}]); | ||
return _default; | ||
_createClass(_default, [{ | ||
key: 'publish', | ||
value: function publish(itemId) { | ||
var _this = this; | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.publish(token, itemId); | ||
}).then(function (data) { | ||
return _this.retry(data, itemsApi.publish, [itemId]); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'publish')); | ||
} | ||
/** | ||
* Create new item | ||
* @param {Blob} content | ||
* @returns {Promise<ChromeStoreItem>} | ||
*/ | ||
}, { | ||
key: 'insert', | ||
value: function insert(content) { | ||
var _this2 = this; | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.insert(token, content); | ||
}).then(function (data) { | ||
return _this2.retry(data, itemsApi.insert, [content]); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'insert')); | ||
} | ||
/** | ||
* Update existing item | ||
* @param {String} itemId | ||
* @param {Blob} content | ||
* @returns {Promise<ChromeStoreItem>} | ||
*/ | ||
}, { | ||
key: 'update', | ||
value: function update(itemId, content) { | ||
var _this3 = this; | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.update(token, itemId, content); | ||
}).then(function (data) { | ||
return _this3.retry(data, itemsApi.update, [itemId, content]); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'update')); | ||
} | ||
/** | ||
* Get info about item | ||
* @param {String} itemId | ||
* @returns {Promise<ChromeStoreItem>} | ||
*/ | ||
}, { | ||
key: 'get', | ||
value: function get(itemId) { | ||
var _this4 = this; | ||
return this.tokenManager.get().then(function (token) { | ||
return itemsApi.get(token, itemId); | ||
}).then(function (data) { | ||
return _this4.retry(data, itemsApi.get, [itemId]); | ||
}).then(_utilsIndex.checkResponseErrors).then((0, _utilsIndex.toLog)(log, 'get')); | ||
} | ||
/** | ||
* Refresh token and retry | ||
* @param {Object} data | ||
* @param {Function} itemApiMethod | ||
* @param {Array} args | ||
*/ | ||
}, { | ||
key: 'retry', | ||
value: function retry(data, itemApiMethod, args) { | ||
if ((0, _utilsIndex.isError)(data) && data.status === 401) { | ||
return this.tokenManager.refresh().then(function (token) { | ||
return itemApiMethod.apply(itemsApi, [token].concat(_toConsumableArray(args))); | ||
}); | ||
} | ||
return data; | ||
} | ||
}]); | ||
return _default; | ||
})(); | ||
@@ -104,0 +139,0 @@ |
{ | ||
"name": "chrome-store-api", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Chrome webstore API", | ||
@@ -5,0 +5,0 @@ "main": "out/index.js", |
@@ -79,11 +79,15 @@ /** | ||
export function checkResponseErrors(data) { | ||
if (data.status > 299 || data.status < 200) { | ||
if (isError(data)) { | ||
return Promise.reject(data); | ||
} | ||
if (_.size(_.get(data, 'response.itemError')) > 0) { | ||
return Promise.reject(data); | ||
} | ||
return data.response; | ||
} | ||
/** | ||
* Check api method error | ||
* @param {Object} data | ||
* @returns {boolean} | ||
*/ | ||
export function isError(data) { | ||
return data.status > 299 || data.status < 200 || _.size(_.get(data, 'response.itemError')) > 0; | ||
} |
@@ -8,3 +8,3 @@ /** | ||
import {checkResponseErrors, toLog} from '../utils/index'; | ||
import {checkResponseErrors, toLog, isError} from '../utils/index'; | ||
import * as itemsApi from './items'; | ||
@@ -31,2 +31,3 @@ | ||
.then(token => itemsApi.publish(token, itemId)) | ||
.then(data => this.retry(data, itemsApi.publish, [itemId])) | ||
.then(checkResponseErrors) | ||
@@ -44,2 +45,3 @@ .then(toLog(log, 'publish')); | ||
.then(token => itemsApi.insert(token, content)) | ||
.then(data => this.retry(data, itemsApi.insert, [content])) | ||
.then(checkResponseErrors) | ||
@@ -58,2 +60,3 @@ .then(toLog(log, 'insert')); | ||
.then(token => itemsApi.update(token, itemId, content)) | ||
.then(data => this.retry(data, itemsApi.update, [itemId, content])) | ||
.then(checkResponseErrors) | ||
@@ -71,5 +74,22 @@ .then(toLog(log, 'update')); | ||
.then(token => itemsApi.get(token, itemId)) | ||
.then(data => this.retry(data, itemsApi.get, [itemId])) | ||
.then(checkResponseErrors) | ||
.then(toLog(log, 'get')); | ||
} | ||
/** | ||
* Refresh token and retry | ||
* @param {Object} data | ||
* @param {Function} itemApiMethod | ||
* @param {Array} args | ||
*/ | ||
retry (data, itemApiMethod, args) { | ||
if (isError(data) && data.status === 401) { | ||
return this.tokenManager.refresh() | ||
.then(token => { | ||
return itemApiMethod.apply(itemsApi, [token, ...args]); | ||
}); | ||
} | ||
return data; | ||
} | ||
} |
@@ -32,2 +32,16 @@ /** | ||
const REFRESH_BODY = { | ||
client_id: 'client_id', | ||
client_secret: 'client_secret', | ||
grant_type: 'refresh_token', | ||
refresh_token: '1/IPM_hwoL8eVDCuNaVh6PZvObENFrMlZsvUZj8JK7Lc0' | ||
}; | ||
const refreshTokenFixture = { | ||
access_token: 'ya29.RQLq6tgOfG1UQ_gDe0IZNJ3fJiufaPcumcXn8L_qQf5XwOZJl8Zk0VgSan_GcbMFm0Wz', | ||
token_type: 'Bearer', | ||
expires_in: 3600, | ||
refresh_token: '1/IPM_hwoL8eVDCuNaVh6PZvObENFrMlZsvUZj8JK7Lc0' | ||
}; | ||
describe('webstore', function () { | ||
@@ -74,2 +88,6 @@ | ||
.post('/o/oauth2/token', GET_BODY) | ||
.reply(200, fixtureTokenGet.ok) | ||
.post('/o/oauth2/token', REFRESH_BODY) | ||
.reply(200, refreshTokenFixture) | ||
.post('/o/oauth2/token', GET_BODY) | ||
.reply(200, fixtureTokenGet.ok); | ||
@@ -79,2 +97,3 @@ | ||
.get(`/chromewebstore/v1.1/items/${ITEM_ID}?projection=draft`) | ||
.times(2) | ||
.reply(401, fixture401); | ||
@@ -101,2 +120,6 @@ | ||
.post('/o/oauth2/token', GET_BODY) | ||
.reply(200, fixtureTokenGet.ok) | ||
.post('/o/oauth2/token', REFRESH_BODY) | ||
.reply(200, refreshTokenFixture) | ||
.post('/o/oauth2/token', GET_BODY) | ||
.reply(200, fixtureTokenGet.ok); | ||
@@ -106,2 +129,3 @@ | ||
.post(`/upload/chromewebstore/v1.1/items/`) | ||
.times(2) | ||
.reply(200, fixtureInsert.ok); | ||
@@ -140,2 +164,6 @@ | ||
.post('/o/oauth2/token', GET_BODY) | ||
.reply(200, fixtureTokenGet.ok) | ||
.post('/o/oauth2/token', REFRESH_BODY) | ||
.reply(200, refreshTokenFixture) | ||
.post('/o/oauth2/token', GET_BODY) | ||
.reply(200, fixtureTokenGet.ok); | ||
@@ -145,2 +173,3 @@ | ||
.post(`/upload/chromewebstore/v1.1/items/`) | ||
.times(2) | ||
.reply(401, fixture401); | ||
@@ -147,0 +176,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79600
2186