UNPKG

1.15 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
2// Node module: @loopback/testlab
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import http, {IncomingMessage} from 'http';
7import https from 'https';
8import url from 'url';
9
10/**
11 * Async wrapper for making HTTP GET requests
12 * @param urlString
13 */
14export function httpGetAsync(
15 urlString: string,
16 agent?: http.Agent,
17): Promise<IncomingMessage> {
18 return new Promise((resolve, reject) => {
19 const urlOptions = url.parse(urlString);
20 const options = {agent, ...urlOptions};
21 http.get(options, resolve).on('error', reject);
22 });
23}
24
25/**
26 * Async wrapper for making HTTPS GET requests
27 * @param urlString
28 */
29export function httpsGetAsync(
30 urlString: string,
31 agent?: https.Agent,
32): Promise<IncomingMessage> {
33 agent =
34 agent ??
35 new https.Agent({
36 rejectUnauthorized: false,
37 });
38
39 const urlOptions = url.parse(urlString);
40 const options = {agent, ...urlOptions};
41
42 return new Promise((resolve, reject) => {
43 https.get(options, resolve).on('error', reject);
44 });
45}