Skip to content

Commit

Permalink
refactor(xhr_backend): remove facade
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Shylovich authored and chuckjaz committed Nov 17, 2016
1 parent 2524d51 commit 82b3483
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions modules/@angular/http/src/backends/xhr_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@

import {Injectable} from '@angular/core';
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 {ContentType, ReadyState, RequestMethod, ResponseContentType, ResponseType} from '../enums';
import {isPresent} from '../facade/lang';
import {Headers} from '../headers';
import {getResponseURL, isSuccess} from '../http_utils';
import {Connection, ConnectionBackend, XSRFStrategy} from '../interfaces';
import {Request} from '../static_request';
import {Response} from '../static_response';

import {BrowserXhr} from './browser_xhr';
import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';

const XSSI_PREFIX = /^\)\]\}',?\n/;

Expand All @@ -47,7 +44,7 @@ export class XHRConnection implements Connection {
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
const _xhr: XMLHttpRequest = browserXHR.build();
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
if (isPresent(req.withCredentials)) {
if (req.withCredentials != null) {
_xhr.withCredentials = req.withCredentials;
}
// load event handler
Expand Down Expand Up @@ -75,7 +72,7 @@ export class XHRConnection implements Connection {
const statusText = _xhr.statusText || 'OK';

let responseOptions = new ResponseOptions({body, status, headers, statusText, url});
if (isPresent(baseResponseOptions)) {
if (baseResponseOptions != null) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
const response = new Response(responseOptions);
Expand All @@ -96,20 +93,20 @@ export class XHRConnection implements Connection {
status: _xhr.status,
statusText: _xhr.statusText,
});
if (isPresent(baseResponseOptions)) {
if (baseResponseOptions != null) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
responseObserver.error(new Response(responseOptions));
};

this.setDetectedContentType(req, _xhr);

if (isPresent(req.headers)) {
if (req.headers != null) {
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
}

// 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) {
case ResponseContentType.ArrayBuffer:
_xhr.responseType = 'arraybuffer';
Expand Down Expand Up @@ -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
if (isPresent(req.headers) && isPresent(req.headers.get('Content-Type'))) {
if (req.headers != null && req.headers.get('Content-Type') != null) {
return;
}

Expand All @@ -161,7 +158,7 @@ export class XHRConnection implements Connection {
_xhr.setRequestHeader('content-type', 'text/plain');
break;
case ContentType.BLOB:
let blob = req.blob();
const blob = req.blob();
if (blob.type) {
_xhr.setRequestHeader('content-type', blob.type);
}
Expand All @@ -185,7 +182,7 @@ export class CookieXSRFStrategy implements XSRFStrategy {
constructor(
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);
if (xsrfToken) {
req.headers.set(this._headerName, xsrfToken);
Expand Down

0 comments on commit 82b3483

Please sign in to comment.