refactor: treat fetch
as public utils function
This commit is contained in:
29
src/fetch.ts
29
src/fetch.ts
@@ -1,29 +0,0 @@
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
|
||||
function fetch(url: string): Promise<string> {
|
||||
// determine the library to use (based on the url protocol)
|
||||
const lib = url.startsWith('https://') ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
lib.get(url, (response) => {
|
||||
// make sure the status code is 200
|
||||
if (response.statusCode !== 200) {
|
||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
|
||||
let data = '';
|
||||
response.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
response.on('end', () => {
|
||||
resolve(data);
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default fetch;
|
29
src/utils.ts
29
src/utils.ts
@@ -1,4 +1,31 @@
|
||||
import { exec } from 'child_process';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
|
||||
export function fetch(url: string): Promise<string> {
|
||||
// determine the library to use (based on the url protocol)
|
||||
const lib = url.startsWith('https://') ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
lib.get(url, (response) => {
|
||||
// make sure the status code is 200
|
||||
if (response.statusCode !== 200) {
|
||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
|
||||
let data = '';
|
||||
response.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
response.on('end', () => {
|
||||
resolve(data);
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export const Sleep = (ms:number)=> {
|
||||
return new Promise(resolve=>setTimeout(resolve, ms))
|
||||
@@ -24,4 +51,4 @@ export function getVsCodeCommitId(): Promise<string> {
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user