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"); |
| 5 | import Q = require("q"); | |
| 6 | | |
| 7 | export class Request { | |
833e37c7Vladimir Kotikov8 years ago | 8 | public static request(url: string, expectStatusOK = false): Q.Promise<any> { |
2f10b3adunknown10 years ago | 9 | let deferred = Q.defer<string>(); |
| 10 | let req = 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) { | |
cb6d0922digeff10 years ago | 17 | deferred.reject(new Error(responseString)); |
e00f7325unknown10 years ago | 18 | } else { |
2f10b3adunknown10 years ago | 19 | deferred.resolve(responseString); |
e00f7325unknown10 years ago | 20 | } |
2f10b3adunknown10 years ago | 21 | }); |
3fb37ad5unknown10 years ago | 22 | }); |
2f10b3adunknown10 years ago | 23 | req.on("error", (err: Error) => { |
| 24 | deferred.reject(err); | |
| 25 | }); | |
| 26 | return deferred.promise; | |
3fb37ad5unknown10 years ago | 27 | } |
| 28 | } |