chore(http): make all typings explicit

This commit is contained in:
Jeff Cross
2016-02-01 17:05:50 -08:00
committed by Alex Eagle
parent da1fcfd820
commit f2c7946cca
14 changed files with 203 additions and 163 deletions

View File

@ -3,11 +3,11 @@ import {global} from 'angular2/src/facade/lang';
let _nextRequestId = 0;
export const JSONP_HOME = '__ng_jsonp__';
var _jsonpConnections = null;
var _jsonpConnections: {[key: string]: any} = null;
function _getJsonpConnections(): {[key: string]: any} {
if (_jsonpConnections === null) {
_jsonpConnections = global[JSONP_HOME] = {};
_jsonpConnections = (<{[key: string]: any}>global)[JSONP_HOME] = {};
}
return _jsonpConnections;
}

View File

@ -8,6 +8,7 @@ import {BrowserJsonp} from './browser_jsonp';
import {makeTypeError} from 'angular2/src/facade/exceptions';
import {StringWrapper, isPresent} from 'angular2/src/facade/lang';
import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
@ -51,7 +52,7 @@ export class JSONPConnection_ extends JSONPConnection {
throw makeTypeError(JSONP_ERR_WRONG_METHOD);
}
this.request = req;
this.response = new Observable(responseObserver => {
this.response = new Observable((responseObserver: Observer<Response>) => {
this.readyState = ReadyState.Loading;
let id = this._id = _dom.nextRequestID();
@ -70,7 +71,7 @@ export class JSONPConnection_ extends JSONPConnection {
let script = this._script = _dom.build(url);
let onLoad = event => {
let onLoad = (event: Event) => {
if (this.readyState === ReadyState.Cancelled) return;
this.readyState = ReadyState.Done;
_dom.cleanup(script);
@ -93,7 +94,7 @@ export class JSONPConnection_ extends JSONPConnection {
responseObserver.complete();
};
let onError = error => {
let onError = (error: Error) => {
if (this.readyState === ReadyState.Cancelled) return;
this.readyState = ReadyState.Done;
_dom.cleanup(script);

View File

@ -32,7 +32,7 @@ export class MockConnection implements Connection {
* {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a
* response is available.
*/
response: any; // Subject<Response>
response: ReplaySubject<Response>;
constructor(req: Request) {
this.response = take.call(new ReplaySubject(1), 1);
@ -176,7 +176,8 @@ export class MockBackend implements ConnectionBackend {
constructor() {
this.connectionsArray = [];
this.connections = new Subject();
this.connections.subscribe(connection => this.connectionsArray.push(connection));
this.connections.subscribe((connection: MockConnection) =>
this.connectionsArray.push(connection));
this.pendingConnections = new Subject();
}
@ -187,7 +188,7 @@ export class MockBackend implements ConnectionBackend {
*/
verifyNoPendingRequests() {
let pending = 0;
this.pendingConnections.subscribe(c => pending++);
this.pendingConnections.subscribe((c: MockConnection) => pending++);
if (pending > 0) throw new BaseException(`${pending} pending connections to be resolved`);
}
@ -197,7 +198,7 @@ export class MockBackend implements ConnectionBackend {
*
* This method only exists in the mock implementation, not in real Backends.
*/
resolveAllConnections() { this.connections.subscribe(c => c.readyState = 4); }
resolveAllConnections() { this.connections.subscribe((c: MockConnection) => c.readyState = 4); }
/**
* Creates a new {@link MockConnection}. This is equivalent to calling `new
@ -205,7 +206,7 @@ export class MockBackend implements ConnectionBackend {
* emitter of this `MockBackend` instance. This method will usually only be used by tests
* against the framework itself, not by end-users.
*/
createConnection(req: Request): Connection {
createConnection(req: Request): MockConnection {
if (!isPresent(req) || !(req instanceof Request)) {
throw new BaseException(`createConnection requires an instance of Request, got ${req}`);
}

View File

@ -8,7 +8,9 @@ import {Injectable} from 'angular2/core';
import {BrowserXhr} from './browser_xhr';
import {isPresent} from 'angular2/src/facade/lang';
import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import {isSuccess, getResponseURL} from '../http_utils';
/**
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
@ -27,7 +29,7 @@ export class XHRConnection implements Connection {
readyState: ReadyState;
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
this.request = req;
this.response = new Observable(responseObserver => {
this.response = new Observable((responseObserver: Observer<Response>) => {
let _xhr: XMLHttpRequest = browserXHR.build();
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
// load event handler
@ -64,7 +66,7 @@ export class XHRConnection implements Connection {
responseObserver.error(response);
};
// error event handler
let onError = (err) => {
let onError = (err: any) => {
var responseOptions = new ResponseOptions({body: err, type: ResponseType.Error});
if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions);

View File

@ -57,8 +57,9 @@ export class Headers {
}
// headers instanceof StringMap
StringMapWrapper.forEach(
headers, (v, k) => { this._headersMap.set(k, isListLikeIterable(v) ? v : [v]); });
StringMapWrapper.forEach(headers, (v: any, k: string) => {
this._headersMap.set(k, isListLikeIterable(v) ? v : [v]);
});
}
/**
@ -110,13 +111,13 @@ export class Headers {
* Sets or overrides header value for given name.
*/
set(header: string, value: string | string[]): void {
var list = [];
var list: string[] = [];
if (isListLikeIterable(value)) {
var pushValue = (<string[]>value).join(',');
list.push(pushValue);
} else {
list.push(value);
list.push(<string>value);
}
this._headersMap.set(header, list);

View File

@ -12,7 +12,8 @@ function httpRequest(backend: ConnectionBackend, request: Request): Observable<R
return backend.createConnection(request).response;
}
function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
function mergeOptions(defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs,
method: RequestMethod, url: string): RequestOptions {
var newOptions = defaultOpts;
if (isPresent(providedOpts)) {
// Hack so Dart can used named parameters
@ -104,7 +105,7 @@ export class Http {
if (isString(url)) {
responseObservable = httpRequest(
this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)));
} else if (url instanceof Request) {
responseObservable = httpRequest(this._backend, url);
} else {
@ -183,7 +184,8 @@ export class Jsonp extends Http {
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
var responseObservable: any;
if (isString(url)) {
url = new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url));
url =
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url));
}
if (url instanceof Request) {
if (url.method !== RequestMethod.Get) {

View File

@ -3,16 +3,18 @@ import {RequestMethod} from './enums';
import {makeTypeError} from 'angular2/src/facade/exceptions';
import {Response} from './static_response';
export function normalizeMethodName(method): RequestMethod {
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
if (isString(method)) {
var originalMethod = method;
method = method.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
method = RequestMethod[method];
method = (<string>method)
.replace(/(\w)(\w*)/g, (g0: string, g1: string, g2: string) =>
g1.toUpperCase() + g2.toLowerCase());
method = <number>(<{[key: string]: any}>RequestMethod)[method];
if (typeof method !== 'number')
throw makeTypeError(
`Invalid request method. The method "${originalMethod}" is not supported.`);
}
return method;
return <RequestMethod>method;
}
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);

View File

@ -92,7 +92,7 @@ export class Response {
* Attempts to return body as parsed `JSON` object, or raises an exception.
*/
json(): any {
var jsonResponse;
var jsonResponse: string | Object;
if (isJsObject(this._body)) {
jsonResponse = this._body;
} else if (isString(this._body)) {

View File

@ -121,7 +121,7 @@ export class URLSearchParams {
}
toString(): string {
var paramsList = [];
var paramsList: string[] = [];
this.paramsMap.forEach((values, k) => { values.forEach(v => paramsList.push(k + '=' + v)); });
return paramsList.join('&');
}