Skip to content

Commit 1c83e8e

Browse files
refactor(lib): use async/await
1 parent eaeee32 commit 1c83e8e

3 files changed

Lines changed: 21 additions & 32 deletions

File tree

.eslintrc-base.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"env": {
66
"es6": true
77
},
8+
"parserOptions": {
9+
"ecmaVersion": 2019
10+
},
811
"rules": {
912
"indent": [
1013
"error",

lib/services/rol.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ module.exports = class RolService {
77
* Fetch all the roles.
88
* @returns {Promise<Array<Rol>>} A promise with the array of roles.
99
*/
10-
static fetch() {
11-
return request.get(ENDPOINT).then(
12-
response => response.body.roles.map(
13-
rol => new Rol(rol)
14-
)
15-
);
10+
static async fetch() {
11+
const {body} = await request.get(ENDPOINT);
12+
return body.roles.map(rol => new Rol(rol));
1613
}
1714
};

lib/services/user.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,30 @@ module.exports = class UserService {
77
* Fetch all the users.
88
* @returns {Promise<Array<User>>} A promise with the array of users.
99
*/
10-
static fetchAll() {
11-
return request.get(`${ENDPOINT}all`).then(
12-
response => response.body.users.map(
13-
user => new User(user)
14-
)
15-
);
10+
static async fetchAll() {
11+
const {body} = await request.get(`${ENDPOINT}all`);
12+
return body.users.map(user => new User(user));
1613
}
1714

1815
/**
1916
* Fetch one user by id.
2017
* @param id
2118
* @returns {Promise<User>} A promise with the found user.an
2219
*/
23-
static fetchOne(id) {
24-
return request.get(`${ENDPOINT}${id}`).then(
25-
response => new User(response.body.user)
26-
);
20+
static async fetchOne(id) {
21+
const {body} = await request.get(`${ENDPOINT}${id}`);
22+
return new User(body.user);
2723
}
2824

2925
/**
3026
* Fetch the users based on the given IDs.
3127
* @param {Array<String>} ids
3228
* @returns {Promise<Array<User>>}
3329
*/
34-
static fetch(ids) {
30+
static async fetch(ids) {
3531
const idsString = ids && ids.length ? ids.join(',') : undefined;
36-
return request.get(ENDPOINT, {ids: idsString}).then(
37-
response => response.body.users.map(
38-
user => new User(user)
39-
)
40-
);
32+
const {body} = await request.get(ENDPOINT, {ids: idsString});
33+
return body.users.map(user => new User(user));
4134
}
4235

4336
// static fetch(rol = undefined, ids = undefined, term = undefined) {
@@ -49,12 +42,9 @@ module.exports = class UserService {
4942
* @param {String} opts.term Terms to filter.
5043
* @returns {Promise<Array<User>>}
5144
*/
52-
static search({rol, state, term}) {
53-
return request.get(ENDPOINT, {rol, state, term}).then(
54-
response => response.body.users.map(
55-
user => new User(user)
56-
)
57-
);
45+
static async search({rol, state, term}) {
46+
const {body} = await request.get(ENDPOINT, {rol, state, term});
47+
return body.users.map(user => new User(user));
5848
}
5949

6050
/**
@@ -63,10 +53,9 @@ module.exports = class UserService {
6353
* @param {Array<String>} roles
6454
* @returns {Promise<User>}
6555
*/
66-
static add(id, roles) {
67-
return request.put(`${ENDPOINT}${id}`, {roles}).then(
68-
response => new User(response.body.user)
69-
);
56+
static async add(id, roles) {
57+
const {body} = await request.put(`${ENDPOINT}${id}`, {roles});
58+
return new User(body.user);
7059
}
7160

7261
/**

0 commit comments

Comments
 (0)