microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/request.ts
28lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
3fb37ad5unknown10 years ago | 4 | import http = require("http"); |
f338085detatanova4 years ago | 5 | import https = require("https"); |
3fb37ad5unknown10 years ago | 6 | |
| 7 | export class Request { | |
f338085detatanova4 years ago | 8 | public static request(url: string, expectStatusOK = false, isHttps = false): Promise<string> { |
ce5e88eeYuri Skorokhodov5 years ago | 9 | return new Promise((resolve, reject) => { |
f338085detatanova4 years ago | 10 | let req = (isHttps ? https : http).get(url, function (res) { |
ce5e88eeYuri Skorokhodov5 years ago | 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 | }); | |
2f10b3adunknown10 years ago | 22 | }); |
ce5e88eeYuri Skorokhodov5 years ago | 23 | req.on("error", (err: Error) => { |
| 24 | reject(err); | |
2f10b3adunknown10 years ago | 25 | }); |
3fb37ad5unknown10 years ago | 26 | }); |
| 27 | } | |
| 28 | } |