X Tutup
Skip to content

Commit f2c7946

Browse files
jeffbcrossalexeagle
authored andcommitted
chore(http): make all typings explicit
1 parent da1fcfd commit f2c7946

File tree

14 files changed

+203
-163
lines changed

14 files changed

+203
-163
lines changed

modules/angular2/http.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ export const HTTP_PROVIDERS: any[] = [
156156
// issue: https://github.com/angular/angular/issues/3183
157157
provide(Http,
158158
{
159-
useFactory: (xhrBackend, requestOptions) => new Http(xhrBackend, requestOptions),
159+
useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) =>
160+
new Http(xhrBackend, requestOptions),
160161
deps: [XHRBackend, RequestOptions]
161162
}),
162163
BrowserXhr,
@@ -283,7 +284,8 @@ export const JSONP_PROVIDERS: any[] = [
283284
// issue: https://github.com/angular/angular/issues/3183
284285
provide(Jsonp,
285286
{
286-
useFactory: (jsonpBackend, requestOptions) => new Jsonp(jsonpBackend, requestOptions),
287+
useFactory: (jsonpBackend: JSONPBackend, requestOptions: RequestOptions) =>
288+
new Jsonp(jsonpBackend, requestOptions),
287289
deps: [JSONPBackend, RequestOptions]
288290
}),
289291
BrowserJsonp,

modules/angular2/src/http/backends/browser_jsonp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import {global} from 'angular2/src/facade/lang';
33

44
let _nextRequestId = 0;
55
export const JSONP_HOME = '__ng_jsonp__';
6-
var _jsonpConnections = null;
6+
var _jsonpConnections: {[key: string]: any} = null;
77

88
function _getJsonpConnections(): {[key: string]: any} {
99
if (_jsonpConnections === null) {
10-
_jsonpConnections = global[JSONP_HOME] = {};
10+
_jsonpConnections = (<{[key: string]: any}>global)[JSONP_HOME] = {};
1111
}
1212
return _jsonpConnections;
1313
}

modules/angular2/src/http/backends/jsonp_backend.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {BrowserJsonp} from './browser_jsonp';
88
import {makeTypeError} from 'angular2/src/facade/exceptions';
99
import {StringWrapper, isPresent} from 'angular2/src/facade/lang';
1010
import {Observable} from 'rxjs/Observable';
11+
import {Observer} from 'rxjs/Observer';
1112

1213
const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
1314
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
@@ -51,7 +52,7 @@ export class JSONPConnection_ extends JSONPConnection {
5152
throw makeTypeError(JSONP_ERR_WRONG_METHOD);
5253
}
5354
this.request = req;
54-
this.response = new Observable(responseObserver => {
55+
this.response = new Observable((responseObserver: Observer<Response>) => {
5556

5657
this.readyState = ReadyState.Loading;
5758
let id = this._id = _dom.nextRequestID();
@@ -70,7 +71,7 @@ export class JSONPConnection_ extends JSONPConnection {
7071

7172
let script = this._script = _dom.build(url);
7273

73-
let onLoad = event => {
74+
let onLoad = (event: Event) => {
7475
if (this.readyState === ReadyState.Cancelled) return;
7576
this.readyState = ReadyState.Done;
7677
_dom.cleanup(script);
@@ -93,7 +94,7 @@ export class JSONPConnection_ extends JSONPConnection {
9394
responseObserver.complete();
9495
};
9596

96-
let onError = error => {
97+
let onError = (error: Error) => {
9798
if (this.readyState === ReadyState.Cancelled) return;
9899
this.readyState = ReadyState.Done;
99100
_dom.cleanup(script);

modules/angular2/src/http/backends/mock_backend.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class MockConnection implements Connection {
3232
* {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a
3333
* response is available.
3434
*/
35-
response: any; // Subject<Response>
35+
response: ReplaySubject<Response>;
3636

3737
constructor(req: Request) {
3838
this.response = take.call(new ReplaySubject(1), 1);
@@ -176,7 +176,8 @@ export class MockBackend implements ConnectionBackend {
176176
constructor() {
177177
this.connectionsArray = [];
178178
this.connections = new Subject();
179-
this.connections.subscribe(connection => this.connectionsArray.push(connection));
179+
this.connections.subscribe((connection: MockConnection) =>
180+
this.connectionsArray.push(connection));
180181
this.pendingConnections = new Subject();
181182
}
182183

@@ -187,7 +188,7 @@ export class MockBackend implements ConnectionBackend {
187188
*/
188189
verifyNoPendingRequests() {
189190
let pending = 0;
190-
this.pendingConnections.subscribe(c => pending++);
191+
this.pendingConnections.subscribe((c: MockConnection) => pending++);
191192
if (pending > 0) throw new BaseException(`${pending} pending connections to be resolved`);
192193
}
193194

@@ -197,15 +198,15 @@ export class MockBackend implements ConnectionBackend {
197198
*
198199
* This method only exists in the mock implementation, not in real Backends.
199200
*/
200-
resolveAllConnections() { this.connections.subscribe(c => c.readyState = 4); }
201+
resolveAllConnections() { this.connections.subscribe((c: MockConnection) => c.readyState = 4); }
201202

202203
/**
203204
* Creates a new {@link MockConnection}. This is equivalent to calling `new
204205
* MockConnection()`, except that it also will emit the new `Connection` to the `connections`
205206
* emitter of this `MockBackend` instance. This method will usually only be used by tests
206207
* against the framework itself, not by end-users.
207208
*/
208-
createConnection(req: Request): Connection {
209+
createConnection(req: Request): MockConnection {
209210
if (!isPresent(req) || !(req instanceof Request)) {
210211
throw new BaseException(`createConnection requires an instance of Request, got ${req}`);
211212
}

modules/angular2/src/http/backends/xhr_backend.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {Injectable} from 'angular2/core';
88
import {BrowserXhr} from './browser_xhr';
99
import {isPresent} from 'angular2/src/facade/lang';
1010
import {Observable} from 'rxjs/Observable';
11+
import {Observer} from 'rxjs/Observer';
1112
import {isSuccess, getResponseURL} from '../http_utils';
13+
1214
/**
1315
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
1416
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
@@ -27,7 +29,7 @@ export class XHRConnection implements Connection {
2729
readyState: ReadyState;
2830
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
2931
this.request = req;
30-
this.response = new Observable(responseObserver => {
32+
this.response = new Observable((responseObserver: Observer<Response>) => {
3133
let _xhr: XMLHttpRequest = browserXHR.build();
3234
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
3335
// load event handler
@@ -64,7 +66,7 @@ export class XHRConnection implements Connection {
6466
responseObserver.error(response);
6567
};
6668
// error event handler
67-
let onError = (err) => {
69+
let onError = (err: any) => {
6870
var responseOptions = new ResponseOptions({body: err, type: ResponseType.Error});
6971
if (isPresent(baseResponseOptions)) {
7072
responseOptions = baseResponseOptions.merge(responseOptions);

modules/angular2/src/http/headers.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ export class Headers {
5757
}
5858

5959
// headers instanceof StringMap
60-
StringMapWrapper.forEach(
61-
headers, (v, k) => { this._headersMap.set(k, isListLikeIterable(v) ? v : [v]); });
60+
StringMapWrapper.forEach(headers, (v: any, k: string) => {
61+
this._headersMap.set(k, isListLikeIterable(v) ? v : [v]);
62+
});
6263
}
6364

6465
/**
@@ -110,13 +111,13 @@ export class Headers {
110111
* Sets or overrides header value for given name.
111112
*/
112113
set(header: string, value: string | string[]): void {
113-
var list = [];
114+
var list: string[] = [];
114115

115116
if (isListLikeIterable(value)) {
116117
var pushValue = (<string[]>value).join(',');
117118
list.push(pushValue);
118119
} else {
119-
list.push(value);
120+
list.push(<string>value);
120121
}
121122

122123
this._headersMap.set(header, list);

modules/angular2/src/http/http.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function httpRequest(backend: ConnectionBackend, request: Request): Observable<R
1212
return backend.createConnection(request).response;
1313
}
1414

15-
function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
15+
function mergeOptions(defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs,
16+
method: RequestMethod, url: string): RequestOptions {
1617
var newOptions = defaultOpts;
1718
if (isPresent(providedOpts)) {
1819
// Hack so Dart can used named parameters
@@ -104,7 +105,7 @@ export class Http {
104105
if (isString(url)) {
105106
responseObservable = httpRequest(
106107
this._backend,
107-
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
108+
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)));
108109
} else if (url instanceof Request) {
109110
responseObservable = httpRequest(this._backend, url);
110111
} else {
@@ -183,7 +184,8 @@ export class Jsonp extends Http {
183184
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
184185
var responseObservable: any;
185186
if (isString(url)) {
186-
url = new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url));
187+
url =
188+
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url));
187189
}
188190
if (url instanceof Request) {
189191
if (url.method !== RequestMethod.Get) {

modules/angular2/src/http/http_utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import {RequestMethod} from './enums';
33
import {makeTypeError} from 'angular2/src/facade/exceptions';
44
import {Response} from './static_response';
55

6-
export function normalizeMethodName(method): RequestMethod {
6+
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
77
if (isString(method)) {
88
var originalMethod = method;
9-
method = method.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
10-
method = RequestMethod[method];
9+
method = (<string>method)
10+
.replace(/(\w)(\w*)/g, (g0: string, g1: string, g2: string) =>
11+
g1.toUpperCase() + g2.toLowerCase());
12+
method = <number>(<{[key: string]: any}>RequestMethod)[method];
1113
if (typeof method !== 'number')
1214
throw makeTypeError(
1315
`Invalid request method. The method "${originalMethod}" is not supported.`);
1416
}
15-
return method;
17+
return <RequestMethod>method;
1618
}
1719

1820
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);

modules/angular2/src/http/static_response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class Response {
9292
* Attempts to return body as parsed `JSON` object, or raises an exception.
9393
*/
9494
json(): any {
95-
var jsonResponse;
95+
var jsonResponse: string | Object;
9696
if (isJsObject(this._body)) {
9797
jsonResponse = this._body;
9898
} else if (isString(this._body)) {

modules/angular2/src/http/url_search_params.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class URLSearchParams {
121121
}
122122

123123
toString(): string {
124-
var paramsList = [];
124+
var paramsList: string[] = [];
125125
this.paramsMap.forEach((values, k) => { values.forEach(v => paramsList.push(k + '=' + v)); });
126126
return paramsList.join('&');
127127
}

0 commit comments

Comments
 (0)
X Tutup