Skip to content

Commit c36ec9b

Browse files
mheverytbosch
authored andcommitted
fix(http): Update types for TypeScript nullability support
1 parent 86396a4 commit c36ec9b

18 files changed

Lines changed: 138 additions & 128 deletions

‎packages/http/src/backends/browser_jsonp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Injectable} from '@angular/core';
1010

1111
let _nextRequestId = 0;
1212
export const JSONP_HOME = '__ng_jsonp__';
13-
let _jsonpConnections: {[key: string]: any} = null;
13+
let _jsonpConnections: {[key: string]: any}|null = null;
1414

1515
function _getJsonpConnections(): {[key: string]: any} {
1616
const w: {[key: string]: any} = typeof window == 'object' ? window : {};

‎packages/http/src/backends/xhr_backend.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class XHRConnection implements Connection {
115115
if (!req.headers.has('Accept')) {
116116
req.headers.append('Accept', 'application/json, text/plain, */*');
117117
}
118-
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
118+
req.headers.forEach((values, name) => _xhr.setRequestHeader(name !, values.join(',')));
119119

120120
// Select the correct buffer type to store the response
121121
if (req.responseType != null && _xhr.responseType != null) {

‎packages/http/src/base_request_options.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ export class RequestOptions {
4444
* Http method with which to execute a {@link Request}.
4545
* Acceptable methods are defined in the {@link RequestMethod} enum.
4646
*/
47-
method: RequestMethod|string;
47+
method: RequestMethod|string|null;
4848
/**
4949
* {@link Headers} to be attached to a {@link Request}.
5050
*/
51-
headers: Headers;
51+
headers: Headers|null;
5252
/**
5353
* Body to be used when creating a {@link Request}.
5454
*/
5555
body: any;
5656
/**
5757
* Url with which to perform a {@link Request}.
5858
*/
59-
url: string;
59+
url: string|null;
6060
/**
6161
* Search parameters to be included in a {@link Request}.
6262
*/
@@ -72,11 +72,11 @@ export class RequestOptions {
7272
/**
7373
* Enable use credentials for a {@link Request}.
7474
*/
75-
withCredentials: boolean;
75+
withCredentials: boolean|null;
7676
/*
7777
* Select a buffer to store the response, such as ArrayBuffer, Blob, Json (or Document)
7878
*/
79-
responseType: ResponseContentType;
79+
responseType: ResponseContentType|null;
8080

8181
// TODO(Dzmitry): remove search when this.search is removed
8282
constructor(
@@ -128,8 +128,8 @@ export class RequestOptions {
128128
});
129129
}
130130

131-
private _mergeSearchParams(params: string|URLSearchParams|
132-
{[key: string]: any | any[]}): URLSearchParams {
131+
private _mergeSearchParams(params?: string|URLSearchParams|{[key: string]: any | any[]}|
132+
null): URLSearchParams {
133133
if (!params) return this.params;
134134

135135
if (params instanceof URLSearchParams) {

‎packages/http/src/base_response_options.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,25 @@ export class ResponseOptions {
4646
/**
4747
* String, Object, ArrayBuffer or Blob representing the body of the {@link Response}.
4848
*/
49-
body: string|Object|ArrayBuffer|Blob;
49+
body: string|Object|ArrayBuffer|Blob|null;
5050
/**
5151
* Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code}
5252
* associated with the response.
5353
*/
54-
status: number;
54+
status: number|null;
5555
/**
5656
* Response {@link Headers headers}
5757
*/
58-
headers: Headers;
58+
headers: Headers|null;
5959
/**
6060
* @internal
6161
*/
62-
statusText: string;
62+
statusText: string|null;
6363
/**
6464
* @internal
6565
*/
66-
type: ResponseType;
67-
url: string;
66+
type: ResponseType|null;
67+
url: string|null;
6868
constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) {
6969
this.body = body != null ? body : null;
7070
this.status = status != null ? status : null;

‎packages/http/src/headers.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Headers {
4141
_normalizedNames: Map<string, string> = new Map();
4242

4343
// TODO(vicb): any -> string|string[]
44-
constructor(headers?: Headers|{[name: string]: any}) {
44+
constructor(headers?: Headers|{[name: string]: any}|null) {
4545
if (!headers) {
4646
return;
4747
}
@@ -100,15 +100,16 @@ export class Headers {
100100
this._headers.delete(lcName);
101101
}
102102

103-
forEach(fn: (values: string[], name: string, headers: Map<string, string[]>) => void): void {
103+
forEach(fn: (values: string[], name: string|undefined, headers: Map<string, string[]>) => void):
104+
void {
104105
this._headers.forEach(
105106
(values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
106107
}
107108

108109
/**
109110
* Returns first header that matches given name.
110111
*/
111-
get(name: string): string {
112+
get(name: string): string|null {
112113
const values = this.getAll(name);
113114

114115
if (values === null) {
@@ -157,7 +158,7 @@ export class Headers {
157158
this._headers.forEach((values: string[], name: string) => {
158159
const split: string[] = [];
159160
values.forEach(v => split.push(...v.split(',')));
160-
serialized[this._normalizedNames.get(name)] = split;
161+
serialized[this._normalizedNames.get(name) !] = split;
161162
});
162163

163164
return serialized;
@@ -166,8 +167,8 @@ export class Headers {
166167
/**
167168
* Returns list of header values for a given name.
168169
*/
169-
getAll(name: string): string[] {
170-
return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
170+
getAll(name: string): string[]|null {
171+
return this.has(name) ? this._headers.get(name.toLowerCase()) || null : null;
171172
}
172173

173174
/**

‎packages/http/src/http.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
import {Injectable} from '@angular/core';
1010
import {Observable} from 'rxjs/Observable';
11+
1112
import {BaseRequestOptions, RequestOptions} from './base_request_options';
1213
import {RequestMethod} from './enums';
13-
import {ConnectionBackend, RequestOptionsArgs} from './interfaces';
14+
import {ConnectionBackend, RequestArgs, RequestOptionsArgs} from './interfaces';
1415
import {Request} from './static_request';
1516
import {Response} from './static_response';
1617

@@ -19,8 +20,8 @@ function httpRequest(backend: ConnectionBackend, request: Request): Observable<R
1920
}
2021

2122
function mergeOptions(
22-
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod,
23-
url: string): RequestOptions {
23+
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs | undefined,
24+
method: RequestMethod, url: string): RequestArgs {
2425
const newOptions = defaultOpts;
2526
if (providedOpts) {
2627
// Hack so Dart can used named parameters
@@ -33,10 +34,10 @@ function mergeOptions(
3334
body: providedOpts.body,
3435
withCredentials: providedOpts.withCredentials,
3536
responseType: providedOpts.responseType
36-
}));
37+
})) as RequestArgs;
3738
}
3839

39-
return newOptions.merge(new RequestOptions({method, url}));
40+
return newOptions.merge(new RequestOptions({method, url})) as RequestArgs;
4041
}
4142

4243
/**

‎packages/http/src/http_utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ export function normalizeMethodName(method: string | RequestMethod): RequestMeth
3232

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

35-
export function getResponseURL(xhr: any): string {
35+
export function getResponseURL(xhr: any): string|null {
3636
if ('responseURL' in xhr) {
3737
return xhr.responseURL;
3838
}
3939
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
4040
return xhr.getResponseHeader('X-Request-URL');
4141
}
42-
return;
42+
return null;
4343
}
4444

4545
export function stringToArrayBuffer(input: String): ArrayBuffer {

‎packages/http/src/interfaces.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ export abstract class XSRFStrategy { abstract configureRequest(req: Request): vo
4646
* @experimental
4747
*/
4848
export interface RequestOptionsArgs {
49-
url?: string;
50-
method?: string|RequestMethod;
49+
url?: string|null;
50+
method?: string|RequestMethod|null;
5151
/** @deprecated from 4.0.0. Use params instead. */
52-
search?: string|URLSearchParams|{[key: string]: any | any[]};
53-
params?: string|URLSearchParams|{[key: string]: any | any[]};
54-
headers?: Headers;
52+
search?: string|URLSearchParams|{[key: string]: any | any[]}|null;
53+
params?: string|URLSearchParams|{[key: string]: any | any[]}|null;
54+
headers?: Headers|null;
5555
body?: any;
56-
withCredentials?: boolean;
57-
responseType?: ResponseContentType;
56+
withCredentials?: boolean|null;
57+
responseType?: ResponseContentType|null;
5858
}
5959

6060
/**
6161
* Required structure when constructing new Request();
6262
*/
63-
export interface RequestArgs extends RequestOptionsArgs { url: string; }
63+
export interface RequestArgs extends RequestOptionsArgs { url: string|null; }
6464

6565
/**
6666
* Interface for options to construct a Response, based on
@@ -69,10 +69,10 @@ export interface RequestArgs extends RequestOptionsArgs { url: string; }
6969
* @experimental
7070
*/
7171
export interface ResponseOptionsArgs {
72-
body?: string|Object|FormData|ArrayBuffer|Blob;
73-
status?: number;
74-
statusText?: string;
75-
headers?: Headers;
76-
type?: ResponseType;
77-
url?: string;
72+
body?: string|Object|FormData|ArrayBuffer|Blob|null;
73+
status?: number|null;
74+
statusText?: string|null;
75+
headers?: Headers|null;
76+
type?: ResponseType|null;
77+
url?: string|null;
7878
}

‎packages/http/src/static_request.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Request extends Body {
7575
super();
7676
// TODO: assert that url is present
7777
const url = requestOptions.url;
78-
this.url = requestOptions.url;
78+
this.url = requestOptions.url !;
7979
if (requestOptions.params) {
8080
const params = requestOptions.params.toString();
8181
if (params.length > 0) {
@@ -88,13 +88,13 @@ export class Request extends Body {
8888
}
8989
}
9090
this._body = requestOptions.body;
91-
this.method = normalizeMethodName(requestOptions.method);
91+
this.method = normalizeMethodName(requestOptions.method !);
9292
// TODO(jeffbcross): implement behavior
9393
// Defaults to 'omit', consistent with browser
9494
this.headers = new Headers(requestOptions.headers);
9595
this.contentType = this.detectContentType();
96-
this.withCredentials = requestOptions.withCredentials;
97-
this.responseType = requestOptions.responseType;
96+
this.withCredentials = requestOptions.withCredentials !;
97+
this.responseType = requestOptions.responseType !;
9898
}
9999

100100
/**

‎packages/http/src/static_response.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Response extends Body {
6363
*
6464
* Defaults to "OK"
6565
*/
66-
statusText: string;
66+
statusText: string|null;
6767
/**
6868
* Non-standard property
6969
*
@@ -81,17 +81,17 @@ export class Response extends Body {
8181
* Headers object based on the `Headers` class in the [Fetch
8282
* Spec](https://fetch.spec.whatwg.org/#headers-class).
8383
*/
84-
headers: Headers;
84+
headers: Headers|null;
8585

8686
constructor(responseOptions: ResponseOptions) {
8787
super();
8888
this._body = responseOptions.body;
89-
this.status = responseOptions.status;
89+
this.status = responseOptions.status !;
9090
this.ok = (this.status >= 200 && this.status <= 299);
9191
this.statusText = responseOptions.statusText;
9292
this.headers = responseOptions.headers;
93-
this.type = responseOptions.type;
94-
this.url = responseOptions.url;
93+
this.type = responseOptions.type !;
94+
this.url = responseOptions.url !;
9595
}
9696

9797
toString(): string {

0 commit comments

Comments
 (0)