本文整理汇总了TypeScript中https.get函数的典型用法代码示例。如果您正苦于以下问题:TypeScript get函数的具体用法?TypeScript get怎么用?TypeScript get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: download
return new Promise<stream.Readable>((resolve, reject) => {
return https.get(options, res => {
// handle redirection
if (res.statusCode === 302) {
return download(res.headers.location);
}
else if (res.statusCode !== 200) {
return reject(Error(`Download failed with code ${res.statusCode}.`));
}
return resolve(res);
});
});
示例2: res
return new Promise<void>((res, rej) => {
let file = fs.createWriteStream(f);
file.on("close", () => {
console.log("Downloaded " + f);
res();
});
https.get(consts.base_url + f, (r) => {
r.pipe(file);
}).on("error", (e: any) => {
console.log("Failed to download " + f);
rej(e);
});
});
示例3: downloadJDK
/**
* Downloads the JDK at JDK_URL into the destination path.
* Creates the path if it doesn't exist.
*/
function downloadJDK(url: string, destPath: string, cb: (err?: Error) => void) {
let ended = false;
mkdirp(destPath);
https.get(url, (res) => {
if (res.statusCode === 302) {
// Redirected.
ended = true;
downloadJDK(res.headers['location'], destPath, cb);
return;
}
// Will be NaN if not given by server.
let contentLength = parseInt(res.headers['content-length']);
let progressBytes = 0;
let startTime = new Date().getTime();
function printStatus() {
let percent = "??";
if (!isNaN(contentLength)) {
percent = ((progressBytes/contentLength)*100).toFixed(0);
}
let lastPrint = new Date().getTime();
// bytes => kbytes
let dlSoFar = progressBytes >> 10;
let rate = dlSoFar / ((lastPrint - startTime) / 1000);
console.log(`[${percent}%] Received ${dlSoFar} KB [${rate.toFixed(2)} KB/s]`);
}
let interval = setInterval(function() {
printStatus();
}, 5000);
function end(err?: Error) {
if (!ended) {
ended = true;
clearInterval(interval);
cb(err);
}
}
res.pipe(gunzip()).pipe(tarFs.extract(destPath)).on('error', end).on('finish', end);
res.on('data', (d: Buffer) => {
progressBytes += d.length;
});
}).on('error', (err: Error) => {
if (!ended) {
ended = true;
cb(err);
}
});
}
示例4: Promise
return new Promise((resolve, reject) => {
get(url, (res) => {
let body = '';
res.on('data', (data) => body = body + data);
res.on('error', (err) => console.log(err));
res.on('end', () => {
if (IGNORED_CONTENTS.some((content) => body === content)) {
resolve();
} else {
outputFileAsync(outputPath, body).then(() => resolve()).catch(reject);
}
});
}).on('error', reject);
});
示例5: resolve
const httpsGet = (url: string) => new Promise<string>((resolve, reject) => {
https.get(url, res => {
const frames: Buffer[] = [];
res.on("data", (data: Buffer) => {
frames.push(data);
});
res.on("end", () => {
resolve(Buffer.concat(frames).toString("utf8"));
});
res.on("error", (err: Error) => {
reject(err);
});
});
});
示例6: resolve
return new Promise<string>( (resolve, reject) => {
let fileName = prefix + requestTimestamp + "." + extension;
let filePath = config.get("save_path") + fileName;
let file = fs.createWriteStream(filePath);
https.get(url, (metaResponse: any) => {
metaResponse.pipe(file);
metaResponse.on('end', () => {
resolve(fileName);
});
});
});
示例7: function
users.forEach(element => {
var user = db.default.getUserByName(element);
if (user) {
options.headers["if-none-match"] = user.etag;
} else {
user = { name: element, languages: [], etag: null };
}
options.path = `/users/${element}/repos?access_token=${config.Config.access_token}`;
var req = https.get(options, function(res) {
onResponse(res, user);
}).on("error", handleRequestError);
});
示例8: ensureDirectoryExistence
const downloadFile = (status: string, filePath: string, url: string) => {
ensureDirectoryExistence(filePath);
const file = fs.createWriteStream(filePath);
https.get(url, (response: any) => {
response.pipe(file);
file.on('finish', () => {
file.close();
console.info(`${status}: ${filePath}`);
});
}).on('error', () => {
deleteFile(status, filePath);
});
};
示例9: Promise
return new Promise((resolve, reject) => {
get(format(url), response => {
let body = '';
response.setEncoding('utf8');
response.on('data', chunk => body += chunk);
response.on('end', () => {
if (response.statusCode !== 200) {
reject(new Error(`unable to get latest version (code=${response.statusCode}): ${body}`));
}
let packageInfo = JSON.parse(body);
resolve(packageInfo['dist-tags']['latest']);
});
}).on('error', reject);
});
示例10:
return Observable.create<string>((subscriber) => {
var urlRawData:string = "";
https.get({
host: host,
path: dirPath
}, (response) => {
response.on('data', (chunk) => urlRawData += chunk)
response.on('end', () => {
subscriber.onNext(urlRawData);
subscriber.onCompleted();
});
});
});