Set X-Requested-With header and add HttpError (#623)

This commit is contained in:
Mikael Mengistu 2017-07-05 16:02:04 -07:00 committed by GitHub
parent f21f5039b2
commit ed6badbabe
2 changed files with 13 additions and 9 deletions

View File

@ -1,3 +1,5 @@
import { HttpError } from "./HttpError"
export interface IHttpClient {
get(url: string, headers?: Map<string, string>): Promise<string>;
options(url: string, headers?: Map<string, string>): Promise<string>;
@ -22,6 +24,7 @@ export class HttpClient implements IHttpClient {
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));
@ -33,19 +36,13 @@ export class HttpClient implements IHttpClient {
resolve(xhr.response);
}
else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
reject(new HttpError(xhr.statusText, xhr.status));
}
};
xhr.onerror = () => {
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
reject(new HttpError(xhr.statusText, xhr.status));
}
});
}
}

View File

@ -0,0 +1,7 @@
export class HttpError extends Error {
statusCode: number;
constructor(errorMessage: string, statusCode: number) {
super(errorMessage);
this.statusCode = statusCode;
}
}