microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/request.ts
27lines · 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"); |
| 5 | | |
| 6 | export class Request { | |
ce5e88eeYuri Skorokhodov5 years ago | 7 | public static request(url: string, expectStatusOK = false): Promise<any> { |
| 8 | return new Promise((resolve, reject) => { | |
| 9 | let req = http.get(url, function(res) { | |
| 10 | let responseString = ""; | |
| 11 | res.on("data", (data: Buffer) => { | |
| 12 | responseString += data.toString(); | |
| 13 | }); | |
| 14 | res.on("end", () => { | |
| 15 | if (expectStatusOK && res.statusCode !== 200) { | |
| 16 | reject(new Error(responseString)); | |
| 17 | } else { | |
| 18 | resolve(responseString); | |
| 19 | } | |
| 20 | }); | |
2f10b3adunknown10 years ago | 21 | }); |
ce5e88eeYuri Skorokhodov5 years ago | 22 | req.on("error", (err: Error) => { |
| 23 | reject(err); | |
2f10b3adunknown10 years ago | 24 | }); |
3fb37ad5unknown10 years ago | 25 | }); |
| 26 | } | |
| 27 | } |