Skip to content

Commit 5890f89

Browse files
authored
Refactor http.js for consistency: improve error handling in downloadFile, and enhance readability. (#114)
1 parent 61d44e4 commit 5890f89

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

packages/download/http.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import axios from 'axios'
2-
import dotenv from 'dotenv'
3-
import nPath, { resolve } from 'path'
4-
import sig from 'signale'
5-
import fs from 'fs'
1+
import axios from "axios";
2+
import dotenv from "dotenv";
3+
import nPath, { resolve } from "path";
4+
import sig from "signale";
5+
import fs from "fs";
66

7-
dotenv.config()
7+
dotenv.config();
88

9-
const GITHUB_AUTHORIZATION_TOKEN = process.env.GITHUB_AUTHORIZATION_TOKEN
10-
const baseURL = 'https://api.github.com'
9+
const GITHUB_AUTHORIZATION_TOKEN = process.env.GITHUB_AUTHORIZATION_TOKEN;
10+
const baseURL = "https://api.github.com";
1111
const defaultHeaders = {
12-
Accept: 'application/vnd.github.v3+json',
13-
}
12+
Accept: "application/vnd.github.v3+json",
13+
};
1414

1515
export const http = axios.create({
1616
baseURL,
@@ -20,67 +20,73 @@ export const http = axios.create({
2020
Authorization: `token ${GITHUB_AUTHORIZATION_TOKEN}`,
2121
}
2222
: defaultHeaders,
23-
})
23+
});
2424

2525
export function getContent(repo, ref, path) {
26-
const url = nPath.join(`/repos/${repo}/contents`, path)
26+
const url = nPath.join(`/repos/${repo}/contents`, path);
2727

28-
sig.start(`Get content(ref: ${ref}) from:`, url)
28+
sig.start(`Get content(ref: ${ref}) from:`, url);
2929

3030
return http.get(url, {
3131
params: {
3232
ref,
3333
},
34-
})
34+
});
3535
}
3636

3737
export function compare(repo, base, head) {
38-
const url = `/repos/${repo}/compare/${base}...${head}`
38+
const url = `/repos/${repo}/compare/${base}...${head}`;
3939

40-
sig.info(`Compare: ${base}...${head}`)
40+
sig.info(`Compare: ${base}...${head}`);
4141

42-
return http.get(url)
42+
return http.get(url);
4343
}
4444

45-
export function downloadFile(reqUrl, fileName = '') {
45+
export function downloadFile(reqUrl, fileName = "") {
4646
return axios({
47-
method: 'GET',
47+
method: "GET",
4848
url: reqUrl,
49-
responseType: 'stream',
49+
responseType: "stream",
5050
headers: GITHUB_AUTHORIZATION_TOKEN
5151
? {
5252
...defaultHeaders,
5353
Authorization: `token ${GITHUB_AUTHORIZATION_TOKEN}`,
5454
}
5555
: defaultHeaders,
5656
})
57-
.then(res => {
57+
.then((res) => {
5858
if (res.status == 200) {
59-
fileName = fileName || reqUrl.split('/').pop()
60-
const dir = resolve(fileName)
61-
sig.start(`Download file(fileName: ${fileName}) reqUrl:${reqUrl}`)
62-
res.data.pipe(fs.createWriteStream(dir))
63-
// res.data.on('end', () => {
64-
// sig.success('download completed')
65-
// })
59+
fileName = fileName || reqUrl.split("/").pop();
60+
const dir = resolve(fileName);
61+
sig.start(`Download file(fileName: ${fileName}) reqUrl:${reqUrl}`);
62+
63+
const writeStream = fs.createWriteStream(dir);
64+
65+
res.data.pipe(writeStream);
66+
6667
return new Promise((resolve, reject) => {
67-
res.data.on('end', () => {
68-
sig.success('download completed')
69-
resolve()
70-
})
71-
72-
res.data.on('error', (err) => {
73-
sig.error('Failed to save file: ', err)
74-
reject()
75-
})
76-
})
68+
writeStream.on("finish", () => {
69+
sig.success("download completed");
70+
resolve();
71+
});
72+
73+
writeStream.on("error", (err) => {
74+
sig.error("Failed to save file: ", err);
75+
reject(err);
76+
});
77+
78+
res.data.on("error", (err) => {
79+
sig.error("Failed to download data: ", err);
80+
reject(err);
81+
});
82+
});
7783
} else {
78-
sig.error(`ERROR >> ${res.status}`)
84+
sig.error(`ERROR >> ${res.status}`);
7985
}
8086
})
81-
.catch(err => {
82-
sig.error('Error ', err)
83-
})
87+
.catch((err) => {
88+
sig.error("Error ", err);
89+
});
8490
}
8591

8692
/**
@@ -91,7 +97,7 @@ export function downloadFile(reqUrl, fileName = '') {
9197
* @returns Promise
9298
*/
9399
export function getArchiveFile(repo, ref, fileName) {
94-
const url = `https://api.github.com/repos/${repo}/zipball/${ref}`
95-
sig.start(`Get content(ref: ${ref}) from:`, url)
96-
return downloadFile(url, fileName)
100+
const url = `https://api.github.com/repos/${repo}/zipball/${ref}`;
101+
sig.start(`Get content(ref: ${ref}) from:`, url);
102+
return downloadFile(url, fileName);
97103
}

0 commit comments

Comments
 (0)