microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/request.ts
28lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import http = require("http"); |
| 5 | import https = require("https"); |
| 6 | |
| 7 | export class Request { |
| 8 | public static request(url: string, expectStatusOK = false, isHttps = false): Promise<string> { |
| 9 | return new Promise((resolve, reject) => { |
| 10 | let req = (isHttps ? https : http).get(url, function (res) { |
| 11 | let responseString = ""; |
| 12 | res.on("data", (data: Buffer) => { |
| 13 | responseString += data.toString(); |
| 14 | }); |
| 15 | res.on("end", () => { |
| 16 | if (expectStatusOK && res.statusCode !== 200) { |
| 17 | reject(new Error(responseString)); |
| 18 | } else { |
| 19 | resolve(responseString); |
| 20 | } |
| 21 | }); |
| 22 | }); |
| 23 | req.on("error", (err: Error) => { |
| 24 | reject(err); |
| 25 | }); |
| 26 | }); |
| 27 | } |
| 28 | } |
| 29 | |