From bb242d781374c0e7d2049c866da5013c9f4008c6 Mon Sep 17 00:00:00 2001 From: Paul O'Fallon Date: Tue, 15 Jan 2019 15:15:31 -0500 Subject: [PATCH] Make AlksError extend Error --- .eslintrc.json | 3 +-- dist/alks.js | 15 +++++++++++---- dist/alks.min.js | 2 +- lib/alks.node.js | 13 +++++++------ package.json | 2 +- src/alks.js | 13 +++++++------ 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index d5caf7c7..eab735be 100755 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,8 +6,7 @@ "mocha": true }, "parserOptions": { - "sourceType": "module", - "ecmaVersion": 2018 + "sourceType": "module" }, "extends": "eslint:recommended", "rules": { diff --git a/dist/alks.js b/dist/alks.js index 868d9fe8..2c1f9a7e 100644 --- a/dist/alks.js +++ b/dist/alks.js @@ -95,10 +95,17 @@ var alks = (function () { }) }; var pick = function (obj, props) { return props.reduce(function (a, e) { return (a[e] = obj[e], a); }, {}); }; - var AlksError = function (response, json) { return (Object.assign({}, {name: 'AlksError', - message: response.statusText, - status: response.status}, - json)); }; + var AlksError = (function (Error) { + function AlksError(response, json) { + Error.call(this, response.statusText); + this.status = response.status; + Object.assign(this, json); + } + if ( Error ) AlksError.__proto__ = Error; + AlksError.prototype = Object.create( Error && Error.prototype ); + AlksError.prototype.constructor = AlksError; + return AlksError; + }(Error)); var alks$1 = new alks(); return alks$1; diff --git a/dist/alks.min.js b/dist/alks.min.js index 620ce249..128abfb8 100644 --- a/dist/alks.min.js +++ b/dist/alks.min.js @@ -1 +1 @@ -var alks=function(){"use strict";var n=window.fetch.bind(window),t=function(e,t){void 0===t&&(t={}),this.defaults=Object.assign({},t,{_fetch:n},e)};t.prototype.create=function(e){return new t(e,this.defaults)},t.prototype.getAccounts=function(e){return this._doFetch("getAccounts",e).then(function(t){return Object.keys(t.accountListRole).map(function(e){return{account:e,role:t.accountListRole[e][0].role,iamKeyActive:t.accountListRole[e][0].iamKeyActive}})})},t.prototype.getKeys=function(e){return this._doFetch("getKeys",e).then(function(e){return o(e,["accessKey","secretKey","sessionToken"])})},t.prototype.getIAMKeys=function(e){return this._doFetch("getIAMKeys",e).then(function(e){return o(e,["accessKey","secretKey","sessionToken"])})},t.prototype.getAWSRoleTypes=function(e){return this._doFetch("getAWSRoleTypes",e).then(function(e){return JSON.parse(e.roleTypes)})},t.prototype.getNonServiceAWSRoleTypes=function(e){return this._doFetch("getNonServiceAWSRoleTypes",e).then(function(e){return JSON.parse(e.roleTypes)})},t.prototype.createRole=function(e){return this._doFetch("createRole",e).then(function(e){return e.denyArns=e.denyArns.split(","),o(e,["roleArn","denyArns","instanceProfileArn","addedRoleToInstanceProfile"])})},t.prototype.createNonServiceRole=function(e){return this._doFetch("createNonServiceRole",e).then(function(e){return e.denyArns=e.denyArns.split(","),o(e,["roleArn","denyArns","instanceProfileArn","addedRoleToInstanceProfile"])})},t.prototype.listAWSAccountRoles=function(e){return this._doFetch("listAWSAccountRoles",e).then(function(e){return JSON.parse(e.jsonAWSRoleList).map(function(e){return e.split("/").slice(-1)[0]})})},t.prototype.getAccountRole=function(t){return this._doFetch("getAccountRole",t).then(function(e){if(!e.roleExists)throw new Error("Role "+t.roleName+" does not exist in this account");return e.roleARN})},t.prototype.deleteRole=function(e){return this._doFetch("deleteRole",e).then(function(){return!0})},t.prototype.createAccessKeys=function(e){return this._doFetch("accessKeys",e).then(function(e){return o(e,["iamUserArn","accessKey","secretKey","addedIAMUserToGroup"])})},t.prototype.deleteIAMUser=function(e){return this._doFetch("IAMUser",e,"DELETE").then(function(){return!0})},t.prototype._doFetch=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n="POST");var o=Object.assign({},this.defaults,t),r={"Content-Type":"application/json"};o.accessToken&&(r.Authorization="Bearer "+o.accessToken,delete o.accessToken),(o.userid||o.password)&&console.error("The userid and password properties are deprecated and should be replaced with an access token");var s=o._fetch(o.baseUrl+"/"+e+"/",{method:n,headers:r,body:JSON.stringify(o)}),c=s.then(function(e){return e.json()}).catch(function(){});return Promise.all([s,c]).then(function(e){var t=e[0],n=e[1];if(!t.ok)throw new i(t,n);return n})};var o=function(n,e){return e.reduce(function(e,t){return e[t]=n[t],e},{})},i=function(e,t){return Object.assign({},{name:"AlksError",message:e.statusText,status:e.status},t)};return new t}(); +var alks=function(){"use strict";var n=window.fetch.bind(window),t=function(e,t){void 0===t&&(t={}),this.defaults=Object.assign({},t,{_fetch:n},e)};t.prototype.create=function(e){return new t(e,this.defaults)},t.prototype.getAccounts=function(e){return this._doFetch("getAccounts",e).then(function(t){return Object.keys(t.accountListRole).map(function(e){return{account:e,role:t.accountListRole[e][0].role,iamKeyActive:t.accountListRole[e][0].iamKeyActive}})})},t.prototype.getKeys=function(e){return this._doFetch("getKeys",e).then(function(e){return o(e,["accessKey","secretKey","sessionToken"])})},t.prototype.getIAMKeys=function(e){return this._doFetch("getIAMKeys",e).then(function(e){return o(e,["accessKey","secretKey","sessionToken"])})},t.prototype.getAWSRoleTypes=function(e){return this._doFetch("getAWSRoleTypes",e).then(function(e){return JSON.parse(e.roleTypes)})},t.prototype.getNonServiceAWSRoleTypes=function(e){return this._doFetch("getNonServiceAWSRoleTypes",e).then(function(e){return JSON.parse(e.roleTypes)})},t.prototype.createRole=function(e){return this._doFetch("createRole",e).then(function(e){return e.denyArns=e.denyArns.split(","),o(e,["roleArn","denyArns","instanceProfileArn","addedRoleToInstanceProfile"])})},t.prototype.createNonServiceRole=function(e){return this._doFetch("createNonServiceRole",e).then(function(e){return e.denyArns=e.denyArns.split(","),o(e,["roleArn","denyArns","instanceProfileArn","addedRoleToInstanceProfile"])})},t.prototype.listAWSAccountRoles=function(e){return this._doFetch("listAWSAccountRoles",e).then(function(e){return JSON.parse(e.jsonAWSRoleList).map(function(e){return e.split("/").slice(-1)[0]})})},t.prototype.getAccountRole=function(t){return this._doFetch("getAccountRole",t).then(function(e){if(!e.roleExists)throw new Error("Role "+t.roleName+" does not exist in this account");return e.roleARN})},t.prototype.deleteRole=function(e){return this._doFetch("deleteRole",e).then(function(){return!0})},t.prototype.createAccessKeys=function(e){return this._doFetch("accessKeys",e).then(function(e){return o(e,["iamUserArn","accessKey","secretKey","addedIAMUserToGroup"])})},t.prototype.deleteIAMUser=function(e){return this._doFetch("IAMUser",e,"DELETE").then(function(){return!0})},t.prototype._doFetch=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n="POST");var o=Object.assign({},this.defaults,t),r={"Content-Type":"application/json"};o.accessToken&&(r.Authorization="Bearer "+o.accessToken,delete o.accessToken),(o.userid||o.password)&&console.error("The userid and password properties are deprecated and should be replaced with an access token");var c=o._fetch(o.baseUrl+"/"+e+"/",{method:n,headers:r,body:JSON.stringify(o)}),s=c.then(function(e){return e.json()}).catch(function(){});return Promise.all([c,s]).then(function(e){var t=e[0],n=e[1];if(!t.ok)throw new i(t,n);return n})};var o=function(n,e){return e.reduce(function(e,t){return e[t]=n[t],e},{})},i=function(n){function e(e,t){n.call(this,e.statusText),this.status=e.status,Object.assign(this,t)}return n&&(e.__proto__=n),(e.prototype=Object.create(n&&n.prototype)).constructor=e}(Error);return new t}(); diff --git a/lib/alks.node.js b/lib/alks.node.js index 89b5cb1f..2914cb64 100644 --- a/lib/alks.node.js +++ b/lib/alks.node.js @@ -419,12 +419,13 @@ class alks { const pick = (obj, props) => props.reduce((a, e) => (a[e] = obj[e], a), {}); -const AlksError = (response, json) => ({ - name: 'AlksError', - message: response.statusText, - status: response.status, - ...json -}); +class AlksError extends Error { + constructor(response, json) { + super(response.statusText); + this.status = response.status; + Object.assign(this, json); + } +} var alks$1 = new alks(); diff --git a/package.json b/package.json index eb7a3e46..35d47f91 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alks.js", - "version": "1.0.0", + "version": "1.0.1", "description": "JavaScript client for the ALKS API, usable in both modern browsers and node.js", "main": "lib/alks.node.js", "scripts": { diff --git a/src/alks.js b/src/alks.js index 7c15dffa..8229ed61 100755 --- a/src/alks.js +++ b/src/alks.js @@ -417,11 +417,12 @@ class alks { const pick = (obj, props) => props.reduce((a, e) => (a[e] = obj[e], a), {}) -const AlksError = (response, json) => ({ - name: 'AlksError', - message: response.statusText, - status: response.status, - ...json -}) +class AlksError extends Error { + constructor(response, json) { + super(response.statusText) + this.status = response.status + Object.assign(this, json) + } +} export default new alks()