refactor(xhr_backend): remove facade
This commit is contained in:
parent
2524d510bc
commit
d3eff6c483
|
@ -8,19 +8,16 @@
|
||||||
|
|
||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {__platform_browser_private__} from '@angular/platform-browser';
|
import {__platform_browser_private__} from '@angular/platform-browser';
|
||||||
import {Observable} from 'rxjs/Observable';
|
|
||||||
import {Observer} from 'rxjs/Observer';
|
|
||||||
|
|
||||||
import {ResponseOptions} from '../base_response_options';
|
import {ResponseOptions} from '../base_response_options';
|
||||||
import {ContentType, ReadyState, RequestMethod, ResponseContentType, ResponseType} from '../enums';
|
import {ContentType, ReadyState, RequestMethod, ResponseContentType, ResponseType} from '../enums';
|
||||||
import {isPresent} from '../facade/lang';
|
|
||||||
import {Headers} from '../headers';
|
import {Headers} from '../headers';
|
||||||
import {getResponseURL, isSuccess} from '../http_utils';
|
import {getResponseURL, isSuccess} from '../http_utils';
|
||||||
import {Connection, ConnectionBackend, XSRFStrategy} from '../interfaces';
|
import {Connection, ConnectionBackend, XSRFStrategy} from '../interfaces';
|
||||||
import {Request} from '../static_request';
|
import {Request} from '../static_request';
|
||||||
import {Response} from '../static_response';
|
import {Response} from '../static_response';
|
||||||
|
|
||||||
import {BrowserXhr} from './browser_xhr';
|
import {BrowserXhr} from './browser_xhr';
|
||||||
|
import {Observable} from 'rxjs/Observable';
|
||||||
|
import {Observer} from 'rxjs/Observer';
|
||||||
|
|
||||||
const XSSI_PREFIX = /^\)\]\}',?\n/;
|
const XSSI_PREFIX = /^\)\]\}',?\n/;
|
||||||
|
|
||||||
|
@ -47,7 +44,7 @@ export class XHRConnection implements Connection {
|
||||||
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
|
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
|
||||||
const _xhr: XMLHttpRequest = browserXHR.build();
|
const _xhr: XMLHttpRequest = browserXHR.build();
|
||||||
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
|
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
|
||||||
if (isPresent(req.withCredentials)) {
|
if (req.withCredentials != null) {
|
||||||
_xhr.withCredentials = req.withCredentials;
|
_xhr.withCredentials = req.withCredentials;
|
||||||
}
|
}
|
||||||
// load event handler
|
// load event handler
|
||||||
|
@ -75,7 +72,7 @@ export class XHRConnection implements Connection {
|
||||||
const statusText = _xhr.statusText || 'OK';
|
const statusText = _xhr.statusText || 'OK';
|
||||||
|
|
||||||
let responseOptions = new ResponseOptions({body, status, headers, statusText, url});
|
let responseOptions = new ResponseOptions({body, status, headers, statusText, url});
|
||||||
if (isPresent(baseResponseOptions)) {
|
if (baseResponseOptions != null) {
|
||||||
responseOptions = baseResponseOptions.merge(responseOptions);
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
||||||
}
|
}
|
||||||
const response = new Response(responseOptions);
|
const response = new Response(responseOptions);
|
||||||
|
@ -96,7 +93,7 @@ export class XHRConnection implements Connection {
|
||||||
status: _xhr.status,
|
status: _xhr.status,
|
||||||
statusText: _xhr.statusText,
|
statusText: _xhr.statusText,
|
||||||
});
|
});
|
||||||
if (isPresent(baseResponseOptions)) {
|
if (baseResponseOptions != null) {
|
||||||
responseOptions = baseResponseOptions.merge(responseOptions);
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
||||||
}
|
}
|
||||||
responseObserver.error(new Response(responseOptions));
|
responseObserver.error(new Response(responseOptions));
|
||||||
|
@ -104,12 +101,12 @@ export class XHRConnection implements Connection {
|
||||||
|
|
||||||
this.setDetectedContentType(req, _xhr);
|
this.setDetectedContentType(req, _xhr);
|
||||||
|
|
||||||
if (isPresent(req.headers)) {
|
if (req.headers != null) {
|
||||||
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
|
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select the correct buffer type to store the response
|
// Select the correct buffer type to store the response
|
||||||
if (isPresent(req.responseType) && isPresent(_xhr.responseType)) {
|
if (req.responseType != null && _xhr.responseType != null) {
|
||||||
switch (req.responseType) {
|
switch (req.responseType) {
|
||||||
case ResponseContentType.ArrayBuffer:
|
case ResponseContentType.ArrayBuffer:
|
||||||
_xhr.responseType = 'arraybuffer';
|
_xhr.responseType = 'arraybuffer';
|
||||||
|
@ -141,9 +138,9 @@ export class XHRConnection implements Connection {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setDetectedContentType(req: any /** TODO #9100 */, _xhr: any /** TODO #9100 */) {
|
setDetectedContentType(req: any /** TODO #9100 */, _xhr: XMLHttpRequest) {
|
||||||
// Skip if a custom Content-Type header is provided
|
// Skip if a custom Content-Type header is provided
|
||||||
if (isPresent(req.headers) && isPresent(req.headers.get('Content-Type'))) {
|
if (req.headers != null && req.headers.get('Content-Type') != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +158,7 @@ export class XHRConnection implements Connection {
|
||||||
_xhr.setRequestHeader('content-type', 'text/plain');
|
_xhr.setRequestHeader('content-type', 'text/plain');
|
||||||
break;
|
break;
|
||||||
case ContentType.BLOB:
|
case ContentType.BLOB:
|
||||||
let blob = req.blob();
|
const blob = req.blob();
|
||||||
if (blob.type) {
|
if (blob.type) {
|
||||||
_xhr.setRequestHeader('content-type', blob.type);
|
_xhr.setRequestHeader('content-type', blob.type);
|
||||||
}
|
}
|
||||||
|
@ -185,7 +182,7 @@ export class CookieXSRFStrategy implements XSRFStrategy {
|
||||||
constructor(
|
constructor(
|
||||||
private _cookieName: string = 'XSRF-TOKEN', private _headerName: string = 'X-XSRF-TOKEN') {}
|
private _cookieName: string = 'XSRF-TOKEN', private _headerName: string = 'X-XSRF-TOKEN') {}
|
||||||
|
|
||||||
configureRequest(req: Request) {
|
configureRequest(req: Request): void {
|
||||||
const xsrfToken = __platform_browser_private__.getDOM().getCookie(this._cookieName);
|
const xsrfToken = __platform_browser_private__.getDOM().getCookie(this._cookieName);
|
||||||
if (xsrfToken) {
|
if (xsrfToken) {
|
||||||
req.headers.set(this._headerName, xsrfToken);
|
req.headers.set(this._headerName, xsrfToken);
|
||||||
|
|
Loading…
Reference in New Issue