refactor: treat fetch as public utils function

This commit is contained in:
Levi Yan
2024-09-18 13:53:31 +08:00
parent 1c2486b972
commit f164a3dfed
2 changed files with 28 additions and 30 deletions

View File

@@ -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;

View File

@@ -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> {
}
});
})
}
}