Skip to content

Commit 530dc05

Browse files
committed
src: Convert .then to async/await.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 97135e6 commit 530dc05

File tree

4 files changed

+43
-57
lines changed

4 files changed

+43
-57
lines changed

src/api.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const helper = require('./helper');
22

3-
function api(baseUrl, config, method, params) {
3+
async function api(baseUrl, config, method, params) {
44
const url = new URL(baseUrl);
55
const auth = Buffer.from(`${config.username}:${config.apiKey}`).toString(
66
'base64'
@@ -21,32 +21,28 @@ function api(baseUrl, config, method, params) {
2121
url.searchParams.append(key, value);
2222
});
2323
}
24-
let response = null;
25-
return helper
26-
.fetch(url.href, options)
27-
.then((res) => {
28-
response = res;
29-
return res.json();
30-
})
31-
.catch((e) => {
32-
if (e instanceof SyntaxError) {
33-
// We probably got a non-JSON response from the server.
34-
// We should inform the user of the same.
35-
let message = 'Server Returned a non-JSON response.';
36-
if (response.status === 404) {
37-
message += ` Maybe endpoint: ${method} ${response.url.replace(
38-
config.apiURL,
39-
''
40-
)} doesn't exist.`;
41-
} else {
42-
message += ' Please check the API documentation.';
43-
}
44-
const error = new Error(message);
45-
error.res = response;
46-
throw error;
24+
const response = await helper.fetch(url.href, options);
25+
try {
26+
return response.json();
27+
} catch (e) {
28+
if (e instanceof SyntaxError) {
29+
// We probably got a non-JSON response from the server.
30+
// We should inform the user of the same.
31+
let message = 'Server Returned a non-JSON response.';
32+
if (response.status === 404) {
33+
message += ` Maybe endpoint: ${method} ${response.url.replace(
34+
config.apiURL,
35+
''
36+
)} doesn't exist.`;
37+
} else {
38+
message += ' Please check the API documentation.';
4739
}
48-
throw e;
49-
});
40+
const error = new Error(message);
41+
error.res = response;
42+
throw error;
43+
}
44+
throw e;
45+
}
5046
}
5147

5248
module.exports = api;

src/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ function resources(config) {
4646
};
4747
}
4848

49-
function zulip(initialConfig) {
49+
async function zulip(initialConfig) {
5050
if (initialConfig.zuliprc) {
51-
return parseConfigFile(initialConfig.zuliprc).then((config) =>
52-
resources(config)
53-
);
51+
return resources(await parseConfigFile(initialConfig.zuliprc));
5452
}
5553
const config = initialConfig;
5654
if (config.realm.endsWith('/api')) {
@@ -60,14 +58,10 @@ function zulip(initialConfig) {
6058
}
6159

6260
if (!config.apiKey) {
63-
return accounts(config)
64-
.retrieve()
65-
.then((res) => {
66-
config.apiKey = res.api_key;
67-
return resources(config);
68-
});
61+
const res = await accounts(config).retrieve();
62+
config.apiKey = res.api_key;
6963
}
70-
return Promise.resolve(resources(config));
64+
return resources(config);
7165
}
7266

7367
module.exports = zulip;

src/resources/accounts.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ const helper = require('../helper');
22

33
function accounts(config) {
44
return {
5-
retrieve: () => {
5+
retrieve: async () => {
66
const url = `${config.apiURL}/fetch_api_key`;
77
const form = new helper.FormData();
88
form.append('username', config.username);
99
form.append('password', config.password);
10-
return helper
11-
.fetch(url, {
12-
method: 'POST',
13-
body: form,
14-
})
15-
.then((res) => res.json());
10+
const res = await helper.fetch(url, {
11+
method: 'POST',
12+
body: form,
13+
});
14+
return res.json();
1615
},
1716
};
1817
}

src/zuliprc.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import readFile from 'fs-readfile-promise';
22
import { parse } from 'ini';
33

4-
function parseConfigFile(filename) {
5-
return readFile(filename)
6-
.then((buf) => buf.toString())
7-
.then(parse)
8-
.then((parsedConfig) => {
9-
const config = {
10-
realm: parsedConfig.api.site,
11-
username: parsedConfig.api.email,
12-
apiKey: parsedConfig.api.key,
13-
};
14-
config.apiURL = `${parsedConfig.api.site}/api/v1`;
15-
return config;
16-
});
4+
async function parseConfigFile(filename) {
5+
const buf = await readFile(filename);
6+
const parsedConfig = parse(buf.toString());
7+
const config = {
8+
realm: parsedConfig.api.site,
9+
username: parsedConfig.api.email,
10+
apiKey: parsedConfig.api.key,
11+
};
12+
config.apiURL = `${parsedConfig.api.site}/api/v1`;
13+
return config;
1714
}
1815

1916
export default parseConfigFile;

0 commit comments

Comments
 (0)