microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/request.ts
27lines · 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 | |
| 6 | export class Request { |
| 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 | }); |
| 21 | }); |
| 22 | req.on("error", (err: Error) => { |
| 23 | reject(err); |
| 24 | }); |
| 25 | }); |
| 26 | } |
| 27 | } |
| 28 | |