diff --git a/.eslintrc-base.json b/.eslintrc-base.json index bdc20ed..abc4514 100644 --- a/.eslintrc-base.json +++ b/.eslintrc-base.json @@ -5,6 +5,9 @@ "env": { "es6": true }, + "parserOptions": { + "ecmaVersion": 2019 + }, "rules": { "indent": [ "error", diff --git a/lib/request.js b/lib/request.js index 7905ece..1cd8472 100644 --- a/lib/request.js +++ b/lib/request.js @@ -33,17 +33,15 @@ const send = options => new Promise((resolve, reject) => { }); }); -const sendAndRetry = options => send(options).then( - response => { - if (response.statusCode === 401) { - return refreshToken().then(token => { - _token = token; - return send(options); - }); - } - return response; +const sendAndRetry = async options => { + const response = await send(options); + if (response.statusCode === 401) { + const token = await refreshToken(); + _token = token; + return send(options); } -); + return response.body; +}; /** * Send a GET request to the authorization authority. diff --git a/lib/services/rol.js b/lib/services/rol.js index fd7cc1b..b6153af 100644 --- a/lib/services/rol.js +++ b/lib/services/rol.js @@ -7,11 +7,8 @@ module.exports = class RolService { * Fetch all the roles. * @returns {Promise>} A promise with the array of roles. */ - static fetch() { - return request.get(ENDPOINT).then( - response => response.body.roles.map( - rol => new Rol(rol) - ) - ); + static async fetch() { + const {roles} = await request.get(ENDPOINT); + return roles.map(rol => new Rol(rol)); } }; diff --git a/lib/services/user.js b/lib/services/user.js index 2137cfa..f525acf 100644 --- a/lib/services/user.js +++ b/lib/services/user.js @@ -7,12 +7,9 @@ module.exports = class UserService { * Fetch all the users. * @returns {Promise>} A promise with the array of users. */ - static fetchAll() { - return request.get(`${ENDPOINT}all`).then( - response => response.body.users.map( - user => new User(user) - ) - ); + static async fetchAll() { + const {users} = await request.get(`${ENDPOINT}all`); + return users.map(user => new User(user)); } /** @@ -20,10 +17,9 @@ module.exports = class UserService { * @param id * @returns {Promise} A promise with the found user.an */ - static fetchOne(id) { - return request.get(`${ENDPOINT}${id}`).then( - response => new User(response.body.user) - ); + static async fetchOne(id) { + const {user} = await request.get(`${ENDPOINT}${id}`); + return new User(user); } /** @@ -31,13 +27,10 @@ module.exports = class UserService { * @param {Array} ids * @returns {Promise>} */ - static fetch(ids) { + static async fetch(ids) { const idsString = ids && ids.length ? ids.join(',') : undefined; - return request.get(ENDPOINT, {ids: idsString}).then( - response => response.body.users.map( - user => new User(user) - ) - ); + const {users} = await request.get(ENDPOINT, {ids: idsString}); + return users.map(user => new User(user)); } // static fetch(rol = undefined, ids = undefined, term = undefined) { @@ -49,12 +42,9 @@ module.exports = class UserService { * @param {String} opts.term Terms to filter. * @returns {Promise>} */ - static search({rol, state, term}) { - return request.get(ENDPOINT, {rol, state, term}).then( - response => response.body.users.map( - user => new User(user) - ) - ); + static async search({rol, state, term}) { + const {users} = await request.get(ENDPOINT, {rol, state, term}); + return users.map(user => new User(user)); } /** @@ -63,10 +53,9 @@ module.exports = class UserService { * @param {Array} roles * @returns {Promise} */ - static add(id, roles) { - return request.put(`${ENDPOINT}${id}`, {roles}).then( - response => new User(response.body.user) - ); + static async add(id, roles) { + const {user} = await request.put(`${ENDPOINT}${id}`, {roles}); + return new User(user); } /**