Skip to content

Commit

Permalink
refactor(lib): use async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianoforlenza committed Jan 15, 2019
1 parent eaeee32 commit 66a634e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .eslintrc-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"env": {
"es6": true
},
"parserOptions": {
"ecmaVersion": 2019
},
"rules": {
"indent": [
"error",
Expand Down
18 changes: 8 additions & 10 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions lib/services/rol.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ module.exports = class RolService {
* Fetch all the roles.
* @returns {Promise<Array<Rol>>} 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));
}
};
41 changes: 15 additions & 26 deletions lib/services/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,30 @@ module.exports = class UserService {
* Fetch all the users.
* @returns {Promise<Array<User>>} 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));
}

/**
* Fetch one user by id.
* @param id
* @returns {Promise<User>} 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);
}

/**
* Fetch the users based on the given IDs.
* @param {Array<String>} ids
* @returns {Promise<Array<User>>}
*/
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) {
Expand All @@ -49,12 +42,9 @@ module.exports = class UserService {
* @param {String} opts.term Terms to filter.
* @returns {Promise<Array<User>>}
*/
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));
}

/**
Expand All @@ -63,10 +53,9 @@ module.exports = class UserService {
* @param {Array<String>} roles
* @returns {Promise<User>}
*/
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);
}

/**
Expand Down

0 comments on commit 66a634e

Please sign in to comment.