Merge pull request #17 from Microsoft/markda/ResponseHasRequest
0.1.3: Expose request bits from WebResponse...
This commit is contained in:
Коммит
ef5c8d3341
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "simplerestclients",
|
"name": "simplerestclients",
|
||||||
"version": "0.1.2",
|
"version": "0.1.3",
|
||||||
"description": "A library of components for accessing RESTful services with javascript/typescript.",
|
"description": "A library of components for accessing RESTful services with javascript/typescript.",
|
||||||
"author": "David de Regt <David.de.Regt@microsoft.com>",
|
"author": "David de Regt <David.de.Regt@microsoft.com>",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -15,6 +15,8 @@ import { ExponentialTime } from './ExponentialTime';
|
||||||
export interface WebResponseBase {
|
export interface WebResponseBase {
|
||||||
url: string;
|
url: string;
|
||||||
method: string;
|
method: string;
|
||||||
|
requestOptions: WebRequestOptions;
|
||||||
|
requestHeaders: _.Dictionary<string>;
|
||||||
statusCode: number;
|
statusCode: number;
|
||||||
statusText: string|undefined;
|
statusText: string|undefined;
|
||||||
headers: _.Dictionary<string>;
|
headers: _.Dictionary<string>;
|
||||||
|
@ -161,6 +163,7 @@ let timeoutSupportStatus = FeatureSupportStatus.Unknown;
|
||||||
|
|
||||||
export abstract class SimpleWebRequestBase {
|
export abstract class SimpleWebRequestBase {
|
||||||
protected _xhr: XMLHttpRequest|undefined;
|
protected _xhr: XMLHttpRequest|undefined;
|
||||||
|
protected _xhrRequestheaders: _.Dictionary<string>|undefined;
|
||||||
protected _requestTimeoutTimer: number|undefined;
|
protected _requestTimeoutTimer: number|undefined;
|
||||||
protected _options: WebRequestOptions;
|
protected _options: WebRequestOptions;
|
||||||
|
|
||||||
|
@ -199,6 +202,7 @@ export abstract class SimpleWebRequestBase {
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
private _fire(): void {
|
private _fire(): void {
|
||||||
this._xhr = new XMLHttpRequest();
|
this._xhr = new XMLHttpRequest();
|
||||||
|
this._xhrRequestheaders = {};
|
||||||
|
|
||||||
// xhr.open() can throw an exception for a CSP violation.
|
// xhr.open() can throw an exception for a CSP violation.
|
||||||
const openError = _.attempt(() => {
|
const openError = _.attempt(() => {
|
||||||
|
@ -306,7 +310,7 @@ export abstract class SimpleWebRequestBase {
|
||||||
|
|
||||||
const acceptType = this._options.acceptType || 'json';
|
const acceptType = this._options.acceptType || 'json';
|
||||||
this._xhr.responseType = SimpleWebRequestBase._getResponseType(acceptType);
|
this._xhr.responseType = SimpleWebRequestBase._getResponseType(acceptType);
|
||||||
this._xhr.setRequestHeader('Accept', SimpleWebRequestBase.mapContentType(acceptType));
|
this._setRequestHeader('Accept', SimpleWebRequestBase.mapContentType(acceptType));
|
||||||
|
|
||||||
this._xhr.withCredentials = !!this._options.withCredentials;
|
this._xhr.withCredentials = !!this._options.withCredentials;
|
||||||
|
|
||||||
|
@ -331,12 +335,12 @@ export abstract class SimpleWebRequestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
headersCheck[headerLower] = true;
|
headersCheck[headerLower] = true;
|
||||||
this._xhr!!!.setRequestHeader(key, val);
|
this._setRequestHeader(key, val);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this._options.sendData) {
|
if (this._options.sendData) {
|
||||||
const contentType = SimpleWebRequestBase.mapContentType(this._options.contentType || 'json');
|
const contentType = SimpleWebRequestBase.mapContentType(this._options.contentType || 'json');
|
||||||
this._xhr.setRequestHeader('Content-Type', contentType);
|
this._setRequestHeader('Content-Type', contentType);
|
||||||
|
|
||||||
const sendData = SimpleWebRequestBase.mapBody(this._options.sendData, contentType);
|
const sendData = SimpleWebRequestBase.mapBody(this._options.sendData, contentType);
|
||||||
|
|
||||||
|
@ -346,6 +350,11 @@ export abstract class SimpleWebRequestBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _setRequestHeader(key: string, val: string): void {
|
||||||
|
this._xhr!!!.setRequestHeader(key, val);
|
||||||
|
this._xhrRequestheaders!!![key] = val;
|
||||||
|
}
|
||||||
|
|
||||||
static mapContentType(contentType: string): string {
|
static mapContentType(contentType: string): string {
|
||||||
if (contentType === 'json') {
|
if (contentType === 'json') {
|
||||||
return 'application/json';
|
return 'application/json';
|
||||||
|
@ -634,6 +643,8 @@ export class SimpleWebRequest<T> extends SimpleWebRequestBase {
|
||||||
const resp: WebResponse<T> = {
|
const resp: WebResponse<T> = {
|
||||||
url: this._xhr.responseURL || this._url,
|
url: this._xhr.responseURL || this._url,
|
||||||
method: this._action,
|
method: this._action,
|
||||||
|
requestOptions: this._options,
|
||||||
|
requestHeaders: this._xhrRequestheaders || {},
|
||||||
statusCode: statusCode,
|
statusCode: statusCode,
|
||||||
statusText: statusText,
|
statusText: statusText,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
|
@ -644,6 +655,8 @@ export class SimpleWebRequest<T> extends SimpleWebRequestBase {
|
||||||
let errResp: WebErrorResponse = {
|
let errResp: WebErrorResponse = {
|
||||||
url: (this._xhr ? this._xhr.responseURL : undefined) || this._url,
|
url: (this._xhr ? this._xhr.responseURL : undefined) || this._url,
|
||||||
method: this._action,
|
method: this._action,
|
||||||
|
requestOptions: this._options,
|
||||||
|
requestHeaders: this._xhrRequestheaders || {},
|
||||||
statusCode: statusCode,
|
statusCode: statusCode,
|
||||||
statusText: statusText,
|
statusText: statusText,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
|
@ -688,6 +701,8 @@ export class SimpleWebRequest<T> extends SimpleWebRequestBase {
|
||||||
this._xhr.onreadystatechange = null!!!;
|
this._xhr.onreadystatechange = null!!!;
|
||||||
this._xhr.ontimeout = null!!!;
|
this._xhr.ontimeout = null!!!;
|
||||||
this._xhr = undefined;
|
this._xhr = undefined;
|
||||||
|
|
||||||
|
this._xhrRequestheaders = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handleResponse === ErrorHandlingType.PauseUntilResumed) {
|
if (handleResponse === ErrorHandlingType.PauseUntilResumed) {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче