Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lib): use async/await #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response = await send(options):
if (statusCode === 400) {
  throw new Error();
}
return response.body;

}
);
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}`);
maximilianoforlenza marked this conversation as resolved.
Show resolved Hide resolved
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