// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. import { HttpError } from "./HttpError" export interface IHttpClient { get(url: string, headers?: Map): Promise; post(url: string, content: string, headers?: Map): Promise; } export class HttpClient implements IHttpClient { get(url: string, headers?: Map): Promise { return this.xhr("GET", url, headers); } post(url: string, content: string, headers?: Map): Promise { return this.xhr("POST", url, headers, content); } private xhr(method: string, url: string, headers?: Map, content?: string): Promise { return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); if (headers) { headers.forEach((value, header) => xhr.setRequestHeader(header, value)); } xhr.send(content); xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 300) { resolve(xhr.response || xhr.responseText); } else { reject(new HttpError(xhr.statusText, xhr.status)); } }; xhr.onerror = () => { reject(new HttpError(xhr.statusText, xhr.status)); } }); } }